Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add popularity sorting options for books #5132

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 deletions app/Entities/Controllers/BookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,49 @@ public function __construct(
public function index(Request $request)
{
$view = setting()->getForCurrentUser('books_view_type');
$listOptions = SimpleListOptions::fromRequest($request, 'books')->withSortOptions([
'name' => trans('common.sort_name'),
'created_at' => trans('common.sort_created_at'),
'updated_at' => trans('common.sort_updated_at'),
]);
$listOptions = $this->getListOptions($request);

$booksQuery = $this->queries->visibleForListWithCover();
$this->applySortOptions($booksQuery, $listOptions);

$books = $booksQuery->paginate(18);

$books = $this->queries->visibleForListWithCover()
->orderBy($listOptions->getSort(), $listOptions->getOrder())
->paginate(18);
$recents = $this->isSignedIn() ? $this->queries->recentlyViewedForCurrentUser()->take(4)->get() : false;
$popular = $this->queries->popularForList()->take(4)->get();
$new = $this->queries->visibleForList()->orderBy('created_at', 'desc')->take(4)->get();

$this->shelfContext->clearShelfContext();

$this->setPageTitle(trans('entities.books'));

return view('books.index', [
'books' => $books,
'recents' => $recents,
'popular' => $popular,
'new' => $new,
'view' => $view,
'books' => $books,
'recents' => $recents,
'popular' => $popular,
'new' => $new,
'view' => $view,
'listOptions' => $listOptions,
]);
}

private function getListOptions(Request $request): SimpleListOptions
{
return SimpleListOptions::fromRequest($request, 'books')->withSortOptions([
'name' => trans('common.sort_name'),
'created_at' => trans('common.sort_created_at'),
'updated_at' => trans('common.sort_updated_at'),
'view_count' => trans('common.sort_popularity'),
]);
}

private function applySortOptions($booksQuery, SimpleListOptions $listOptions): void
{
if ($listOptions->getSort() === 'view_count') {
$booksQuery->scopes('withViewCount')->orderBy('view_count', $listOptions->getOrder());
} else {
$booksQuery->orderBy($listOptions->getSort(), $listOptions->getOrder());
}
}

/**
* Show the form for creating a new book.
*/
Expand Down
1 change: 1 addition & 0 deletions lang/en/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
'sort_default' => 'Default',
'sort_created_at' => 'Created Date',
'sort_updated_at' => 'Updated Date',
'sort_popularity' => 'Popularity',

// Misc
'deleted_user' => 'Deleted User',
Expand Down
130 changes: 130 additions & 0 deletions tests/Entity/BookSortingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

namespace Tests\Entity;

use BookStack\Entities\Models\Book;
use BookStack\Users\Models\Role;
use Tests\TestCase;
use Symfony\Component\DomCrawler\Crawler;

class BookSortingTest extends TestCase
{
public function test_update_sort_preference()
{
// Step 1: Set up the test environment
$editor = $this->users->editor();
$this->actingAs($editor);

// Step 2: Send a PATCH request to the endpoint
$updateRequest = $this->patch('/preferences/change-sort/books', [
'sort' => 'created_at',
'order' => 'desc',
]);

// Step 3: Assert the response status
$updateRequest->assertStatus(302);

// Step 4: Verify database changes
$this->assertDatabaseHas('settings', [
'setting_key' => 'user:' . $editor->id . ':books_sort',
'value' => 'created_at',
]);
$this->assertDatabaseHas('settings', [
'setting_key' => 'user:' . $editor->id . ':books_sort_order',
'value' => 'desc',
]);

// Step 5: Check user settings
$this->assertEquals('created_at', setting()->getForCurrentUser('books_sort'));
$this->assertEquals('desc', setting()->getForCurrentUser('books_sort_order'));

// Step 6: Check the order of books on the page
$response = $this->get('/books');
$response->assertStatus(200);
$allBooks = Book::all();
$this->assertBooksOrder($response->content(), $allBooks->sortByDesc('created_at')->pluck('name')->toArray());
}

// Test with different sort fields and orders
public function test_update_sort_preference_with_different_fields_and_orders()
{
// Step 1: Set up the test environment
$editor = $this->users->editor();
$this->actingAs($editor);

// Step 2: Send a PATCH request to the endpoint with different sort field and order
$updateRequest = $this->patch('/preferences/change-sort/books', [
'sort' => 'updated_at',
'order' => 'asc',
]);

// Step 3: Assert the response status
$updateRequest->assertStatus(302);

// Step 4: Verify database changes for the new sort field and order
$this->assertDatabaseHas('settings', [
'setting_key' => 'user:' . $editor->id . ':books_sort',
'value' => 'updated_at',
]);
$this->assertDatabaseHas('settings', [
'setting_key' => 'user:' . $editor->id . ':books_sort_order',
'value' => 'asc',
]);

// Step 5: Check user settings for the updated sort field and order
$this->assertEquals('updated_at', setting()->getForCurrentUser('books_sort'));
$this->assertEquals('asc', setting()->getForCurrentUser('books_sort_order'));

// Step 6: Check the order of books on the page
$response = $this->get('/books');
$response->assertStatus(200);
$allBooks = Book::all();
$this->assertBooksOrder($response->content(), $allBooks->sortBy('updated_at')->pluck('name')->toArray());
}

// Test the sort field popularity
public function test_sort_by_popularity()
{
// Step 1: Set up the test environment
$editor = $this->users->editor();
$this->actingAs($editor);

// Step 2: Send a PATCH request to the endpoint with sort field popularity
$updateRequest = $this->patch('/preferences/change-sort/books', [
'sort' => 'view_count',
'order' => 'desc',
]);

// Step 3: Assert the response status
$updateRequest->assertStatus(302);

// Step 4: Verify database changes for the sort field popularity
$this->assertDatabaseHas('settings', [
'setting_key' => 'user:' . $editor->id . ':books_sort',
'value' => 'view_count',
]);
$this->assertDatabaseHas('settings', [
'setting_key' => 'user:' . $editor->id . ':books_sort_order',
'value' => 'desc',
]);

// Step 5: Check user settings for the sort field popularity
$this->assertEquals('view_count', setting()->getForCurrentUser('books_sort'));
$this->assertEquals('desc', setting()->getForCurrentUser('books_sort_order'));

// Step 6: Check the order of books on the page
$response = $this->get('/books');
$response->assertStatus(200);
$allBooks = Book::all();
$this->assertBooksOrder($response->content(), $allBooks->sortByDesc('view_count')->pluck('name')->toArray());
}

private function assertBooksOrder($htmlContent, $expectedOrder)
{
$crawler = new Crawler($htmlContent);
$bookNames = $crawler->filter('.grid-card-content h2.text-limit-lines-2')->each(function (Crawler $node) {
return $node->text();
});
$this->assertEquals($expectedOrder, $bookNames);
}
}