diff --git a/app/Models/LinkList.php b/app/Models/LinkList.php index 6fb814df4..2e3c0aff5 100644 --- a/app/Models/LinkList.php +++ b/app/Models/LinkList.php @@ -65,6 +65,7 @@ class LinkList extends Model implements Auditable 'created_at', 'updated_at', 'random', + 'links_count', ]; public string $langBase = 'list'; diff --git a/app/Models/Tag.php b/app/Models/Tag.php index cd4842d8b..9f83a4a02 100644 --- a/app/Models/Tag.php +++ b/app/Models/Tag.php @@ -60,6 +60,7 @@ class Tag extends Model implements Auditable 'created_at', 'updated_at', 'random', + 'links_count', ]; public string $langBase = 'tag'; diff --git a/tests/Controller/Models/ListControllerTest.php b/tests/Controller/Models/ListControllerTest.php index 3797dc40a..1d200a871 100644 --- a/tests/Controller/Models/ListControllerTest.php +++ b/tests/Controller/Models/ListControllerTest.php @@ -53,6 +53,36 @@ public function test_index_view(): void ]); } + public function test_index_view_with_sorting(): void + { + $list1 = LinkList::factory()->create([ + 'name' => 'Test List', + 'user_id' => $this->user->id, + ]); + $list2 = LinkList::factory()->create([ + 'name' => 'Example List', + 'user_id' => $this->user->id, + ]); + + $linksGroups = Link::factory()->count(5)->create()->split(2); + $linksGroups[0]->each(fn($link) => $link->lists()->attach([$list1->id])); + $linksGroups[1]->each(fn($link) => $link->lists()->attach([$list2->id])); + + $this->get('lists?orderBy=links_count&orderDir=desc') + ->assertOk() + ->assertSeeInOrder([ + 'Test List', + 'Example List', + ]); + + $this->get('lists?orderBy=links_count&orderDir=asc') + ->assertOk() + ->assertSeeInOrder([ + 'Example List', + 'Test List', + ]); + } + public function test_index_view_with_valid_filter_result(): void { LinkList::factory()->create([ diff --git a/tests/Controller/Models/TagControllerTest.php b/tests/Controller/Models/TagControllerTest.php index 96339de7f..ceb112ee6 100644 --- a/tests/Controller/Models/TagControllerTest.php +++ b/tests/Controller/Models/TagControllerTest.php @@ -52,6 +52,36 @@ public function test_index_view(): void ]); } + public function test_index_view_with_sorting(): void + { + $tag1 = Tag::factory()->create([ + 'name' => 'Test Tag', + 'user_id' => $this->user->id, + ]); + $tag2 = Tag::factory()->create([ + 'name' => 'Example Tag', + 'user_id' => $this->user->id, + ]); + + $linksGroups = Link::factory()->count(5)->create()->split(2); + $linksGroups[0]->each(fn($link) => $link->tags()->attach([$tag1->id])); + $linksGroups[1]->each(fn($link) => $link->tags()->attach([$tag2->id])); + + $this->get('tags?orderBy=links_count&orderDir=desc') + ->assertOk() + ->assertSeeInOrder([ + 'Test Tag', + 'Example Tag', + ]); + + $this->get('tags?orderBy=links_count&orderDir=asc') + ->assertOk() + ->assertSeeInOrder([ + 'Example Tag', + 'Test Tag', + ]); + } + public function test_index_view_with_valid_filter_result(): void { Tag::factory()->create([