X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/c3a1fabbf09c55f3625a7b9e7b616919e03dbe16..b80992ca59a4803fe81d577add6a0611e976c83b:/tests/Sorting/SortRuleTest.php diff --git a/tests/Sorting/SortRuleTest.php b/tests/Sorting/SortRuleTest.php index 10c7bc17d..4a9d3a7b3 100644 --- a/tests/Sorting/SortRuleTest.php +++ b/tests/Sorting/SortRuleTest.php @@ -5,6 +5,7 @@ namespace Tests\Sorting; use BookStack\Activity\ActivityType; use BookStack\Entities\Models\Book; use BookStack\Sorting\SortRule; +use BookStack\Sorting\SortRuleOperation; use Tests\Api\TestsApi; use Tests\TestCase; @@ -186,6 +187,44 @@ class SortRuleTest extends TestCase $this->assertNotEquals($oldPriority, $chapter->priority); } + public function test_name_alphabetical_ordering() + { + $book = Book::factory()->create(); + $rule = SortRule::factory()->create(['sequence' => 'name_asc']); + $book->sort_rule_id = $rule->id; + $book->save(); + $this->permissions->regenerateForEntity($book); + + $namesToAdd = [ + "Beans", + "bread", + "Éclaire", + "egg", + "É😀ire", + "É🫠ire", + "Milk", + "pizza", + "Tomato", + ]; + + $reverseNamesToAdd = array_reverse($namesToAdd); + foreach ($reverseNamesToAdd as $name) { + $this->actingAsApiEditor()->post("/api/pages", [ + 'book_id' => $book->id, + 'name' => $name, + 'markdown' => 'Hello' + ]); + } + + foreach ($namesToAdd as $index => $name) { + $this->assertDatabaseHas('pages', [ + 'book_id' => $book->id, + 'name' => $name, + 'priority' => $index + 1, + ]); + } + } + public function test_name_numeric_ordering() { $book = Book::factory()->create(); @@ -202,7 +241,8 @@ class SortRuleTest extends TestCase "20 - Milk", ]; - foreach ($namesToAdd as $name) { + $reverseNamesToAdd = array_reverse($namesToAdd); + foreach ($reverseNamesToAdd as $name) { $this->actingAsApiEditor()->post("/api/pages", [ 'book_id' => $book->id, 'name' => $name, @@ -218,4 +258,14 @@ class SortRuleTest extends TestCase ]); } } + + public function test_each_sort_rule_operation_has_a_comparison_function() + { + $operations = SortRuleOperation::cases(); + + foreach ($operations as $operation) { + $comparisonFunc = $operation->getSortFunction(); + $this->assertIsCallable($comparisonFunc); + } + } }