]> BookStack Code Mirror - bookstack/blobdiff - tests/Sorting/SortRuleTest.php
Opensearch: Fixed XML declaration when php short tags enabled
[bookstack] / tests / Sorting / SortRuleTest.php
index d0ff263df27362162653b0bb9a1dce0898f1f800..4a9d3a7b33f2365754380b24fd1be9d30189e2b4 100644 (file)
@@ -1,10 +1,11 @@
 <?php
 
-namespace Sorting;
+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;
 
@@ -165,6 +166,65 @@ class SortRuleTest extends TestCase
         ]);
     }
 
+    public function test_auto_book_sort_does_not_touch_timestamps()
+    {
+        $book = $this->entities->bookHasChaptersAndPages();
+        $rule = SortRule::factory()->create(['sequence' => 'name_asc,chapters_first']);
+        $book->sort_rule_id = $rule->id;
+        $book->save();
+        $page = $book->pages()->first();
+        $chapter = $book->chapters()->first();
+
+        $resp = $this->actingAsApiEditor()->put("/api/pages/{$page->id}", [
+            'name' => '1111 page',
+        ]);
+        $resp->assertOk();
+
+        $oldTime = $chapter->updated_at->unix();
+        $oldPriority = $chapter->priority;
+        $chapter->refresh();
+        $this->assertEquals($oldTime, $chapter->updated_at->unix());
+        $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();
@@ -181,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,
@@ -197,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);
+        }
+    }
 }