Skip to content

Commit 9ae17ef

Browse files
committed
Shelf view: Updated books to be database sorted
Fixes issue where sorting would not match other database-sorted parts of app due to case sensitivity differences. Added test to cover. For #4341
1 parent 0a485ba commit 9ae17ef

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

app/Entities/Controllers/BookshelfController.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct(BookshelfRepo $shelfRepo, ShelfContext $shelfContext
3030
}
3131

3232
/**
33-
* Display a listing of the book.
33+
* Display a listing of bookshelves.
3434
*/
3535
public function index(Request $request)
3636
{
@@ -111,8 +111,9 @@ public function show(Request $request, ActivityQueries $activities, string $slug
111111
]);
112112

113113
$sort = $listOptions->getSort();
114-
$sortedVisibleShelfBooks = $shelf->visibleBooks()->get()
115-
->sortBy($sort === 'default' ? 'pivot.order' : $sort, SORT_REGULAR, $listOptions->getOrder() === 'desc')
114+
$sortedVisibleShelfBooks = $shelf->visibleBooks()
115+
->reorder($sort === 'default' ? 'order' : $sort, $listOptions->getOrder())
116+
->get()
116117
->values()
117118
->all();
118119

tests/Entity/BookShelfTest.php

+25
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,31 @@ public function test_shelf_view_sort_takes_action()
196196
$this->withHtml($resp)->assertElementContains('.book-content a.grid-card:nth-child(3)', 'adsfsdfsdfsd');
197197
}
198198

199+
public function test_shelf_view_sorts_by_name_case_insensitively()
200+
{
201+
$shelf = Bookshelf::query()->whereHas('books')->with('books')->first();
202+
$books = Book::query()->take(3)->get(['id', 'name']);
203+
$books[0]->fill(['name' => 'Book Ab'])->save();
204+
$books[1]->fill(['name' => 'Book ac'])->save();
205+
$books[2]->fill(['name' => 'Book AD'])->save();
206+
207+
// Set book ordering
208+
$this->asAdmin()->put($shelf->getUrl(), [
209+
'books' => $books->implode('id', ','),
210+
'tags' => [], 'description' => 'abc', 'name' => 'abc',
211+
]);
212+
$this->assertEquals(3, $shelf->books()->count());
213+
$shelf->refresh();
214+
215+
setting()->putUser($this->users->editor(), 'shelf_books_sort', 'name');
216+
setting()->putUser($this->users->editor(), 'shelf_books_sort_order', 'asc');
217+
$html = $this->withHtml($this->asEditor()->get($shelf->getUrl()));
218+
219+
$html->assertElementContains('.book-content a.grid-card:nth-child(1)', 'Book Ab');
220+
$html->assertElementContains('.book-content a.grid-card:nth-child(2)', 'Book ac');
221+
$html->assertElementContains('.book-content a.grid-card:nth-child(3)', 'Book AD');
222+
}
223+
199224
public function test_shelf_edit()
200225
{
201226
$shelf = $this->entities->shelf();

0 commit comments

Comments
 (0)