]> BookStack Code Mirror - bookstack/commitdiff
Touched entity timestamps on entity tag update
authorDan Brown <redacted>
Mon, 4 Apr 2022 16:24:05 +0000 (17:24 +0100)
committerDan Brown <redacted>
Mon, 4 Apr 2022 16:24:05 +0000 (17:24 +0100)
Decided it's relevant to entity updated_at since tags are now indexed
alongside content.

- Also fixed tags not applied on shelf.
- Also enforced proper page API update validation.
- Adds tests to cover.

For #3319
Fixes #3370

app/Entities/Repos/BaseRepo.php
app/Http/Controllers/Api/BookshelfApiController.php
app/Http/Controllers/Api/PageApiController.php
tests/Api/BooksApiTest.php
tests/Api/ChaptersApiTest.php
tests/Api/PagesApiTest.php
tests/Api/ShelvesApiTest.php

index 6b29dad7b547623058aef4b84760cfc56ef7302f..9e1b41672128b92054b5fda8efaa93df8ed9d7cb 100644 (file)
@@ -11,8 +11,8 @@ use Illuminate\Http\UploadedFile;
 
 class BaseRepo
 {
-    protected $tagRepo;
-    protected $imageRepo;
+    protected TagRepo $tagRepo;
+    protected ImageRepo $imageRepo;
 
     public function __construct(TagRepo $tagRepo, ImageRepo $imageRepo)
     {
@@ -58,6 +58,7 @@ class BaseRepo
 
         if (isset($input['tags'])) {
             $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
+            $entity->touch();
         }
 
         $entity->rebuildPermissions();
index bd4f23a1093257cecd971a030d5c48109fc3a313..63275a72a66191fb9880c425629cbc99126ddf65 100644 (file)
@@ -11,21 +11,20 @@ use Illuminate\Validation\ValidationException;
 
 class BookshelfApiController extends ApiController
 {
-    /**
-     * @var BookshelfRepo
-     */
-    protected $bookshelfRepo;
+    protected BookshelfRepo $bookshelfRepo;
 
     protected $rules = [
         'create' => [
             'name'        => ['required', 'string', 'max:255'],
             'description' => ['string', 'max:1000'],
             'books'       => ['array'],
+            'tags'        => ['array'],
         ],
         'update' => [
             'name'        => ['string', 'min:1', 'max:255'],
             'description' => ['string', 'max:1000'],
             'books'       => ['array'],
+            'tags'        => ['array'],
         ],
     ];
 
index 6f3a71e029ba9b4bab542cb29c32a100dc5e6fac..9749985a52214cecbec3ee8cfecf9d0bc2b84e54 100644 (file)
@@ -12,7 +12,7 @@ use Illuminate\Http\Request;
 
 class PageApiController extends ApiController
 {
-    protected $pageRepo;
+    protected PageRepo $pageRepo;
 
     protected $rules = [
         'create' => [
@@ -24,8 +24,8 @@ class PageApiController extends ApiController
             'tags'       => ['array'],
         ],
         'update' => [
-            'book_id'    => ['required', 'integer'],
-            'chapter_id' => ['required', 'integer'],
+            'book_id'    => ['integer'],
+            'chapter_id' => ['integer'],
             'name'       => ['string', 'min:1', 'max:255'],
             'html'       => ['string'],
             'markdown'   => ['string'],
@@ -103,6 +103,8 @@ class PageApiController extends ApiController
      */
     public function update(Request $request, string $id)
     {
+        $requestData = $this->validate($request, $this->rules['update']);
+
         $page = $this->pageRepo->getById($id, []);
         $this->checkOwnablePermission('page-update', $page);
 
@@ -127,7 +129,7 @@ class PageApiController extends ApiController
             }
         }
 
-        $updatedPage = $this->pageRepo->update($page, $request->all());
+        $updatedPage = $this->pageRepo->update($page, $requestData);
 
         return response()->json($updatedPage->forJsonDisplay());
     }
index 91e2db9e52de5c4cf7bddd0df659e6cdc79c42c8..9625c9f2db840c17f017f9e309334f24aa3ee63f 100644 (file)
@@ -3,13 +3,15 @@
 namespace Tests\Api;
 
 use BookStack\Entities\Models\Book;
+use Carbon\Carbon;
+use Illuminate\Support\Facades\DB;
 use Tests\TestCase;
 
 class BooksApiTest extends TestCase
 {
     use TestsApi;
 
-    protected $baseEndpoint = '/api/books';
+    protected string $baseEndpoint = '/api/books';
 
     public function test_index_endpoint_returns_expected_book()
     {
@@ -101,6 +103,21 @@ class BooksApiTest extends TestCase
         $this->assertActivityExists('book_update', $book);
     }
 
+    public function test_update_increments_updated_date_if_only_tags_are_sent()
+    {
+        $this->actingAsApiEditor();
+        $book = Book::visible()->first();
+        DB::table('books')->where('id', '=', $book->id)->update(['updated_at' => Carbon::now()->subWeek()]);
+
+        $details = [
+            'tags' => [['name' => 'Category', 'value' => 'Testing']]
+        ];
+
+        $this->putJson($this->baseEndpoint . "/{$book->id}", $details);
+        $book->refresh();
+        $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $book->updated_at->unix());
+    }
+
     public function test_delete_endpoint()
     {
         $this->actingAsApiEditor();
index c9ed1a2892e19a715dd344e4a4ac8e6b5302b41a..6f00f9eade1c8596c8719be837f38eed0f0bcac0 100644 (file)
@@ -4,13 +4,15 @@ namespace Tests\Api;
 
 use BookStack\Entities\Models\Book;
 use BookStack\Entities\Models\Chapter;
+use Carbon\Carbon;
+use Illuminate\Support\Facades\DB;
 use Tests\TestCase;
 
 class ChaptersApiTest extends TestCase
 {
     use TestsApi;
 
-    protected $baseEndpoint = '/api/chapters';
+    protected string $baseEndpoint = '/api/chapters';
 
     public function test_index_endpoint_returns_expected_chapter()
     {
@@ -147,6 +149,21 @@ class ChaptersApiTest extends TestCase
         $this->assertActivityExists('chapter_update', $chapter);
     }
 
+    public function test_update_increments_updated_date_if_only_tags_are_sent()
+    {
+        $this->actingAsApiEditor();
+        $chapter = Chapter::visible()->first();
+        DB::table('chapters')->where('id', '=', $chapter->id)->update(['updated_at' => Carbon::now()->subWeek()]);
+
+        $details = [
+            'tags' => [['name' => 'Category', 'value' => 'Testing']]
+        ];
+
+        $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
+        $chapter->refresh();
+        $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $chapter->updated_at->unix());
+    }
+
     public function test_delete_endpoint()
     {
         $this->actingAsApiEditor();
index 4eb109d9dec3acf35653740aa51f5af714d122c4..b91d96d892c74402e9ac79990bbcdcad663d56f0 100644 (file)
@@ -5,13 +5,15 @@ namespace Tests\Api;
 use BookStack\Entities\Models\Book;
 use BookStack\Entities\Models\Chapter;
 use BookStack\Entities\Models\Page;
+use Carbon\Carbon;
+use Illuminate\Support\Facades\DB;
 use Tests\TestCase;
 
 class PagesApiTest extends TestCase
 {
     use TestsApi;
 
-    protected $baseEndpoint = '/api/pages';
+    protected string $baseEndpoint = '/api/pages';
 
     public function test_index_endpoint_returns_expected_page()
     {
@@ -240,6 +242,21 @@ class PagesApiTest extends TestCase
         $this->assertEquals($originalContent, $page->html);
     }
 
+    public function test_update_increments_updated_date_if_only_tags_are_sent()
+    {
+        $this->actingAsApiEditor();
+        $page = Page::visible()->first();
+        DB::table('pages')->where('id', '=', $page->id)->update(['updated_at' => Carbon::now()->subWeek()]);
+
+        $details = [
+            'tags' => [['name' => 'Category', 'value' => 'Testing']]
+        ];
+
+        $this->putJson($this->baseEndpoint . "/{$page->id}", $details);
+        $page->refresh();
+        $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $page->updated_at->unix());
+    }
+
     public function test_delete_endpoint()
     {
         $this->actingAsApiEditor();
index 8868c686e5086231fb26f5c7d2a7ce75e202c572..5953b0c0dc5bad57a0e1c52cb9e8a429dc2ca31e 100644 (file)
@@ -4,13 +4,15 @@ namespace Tests\Api;
 
 use BookStack\Entities\Models\Book;
 use BookStack\Entities\Models\Bookshelf;
+use Carbon\Carbon;
+use Illuminate\Support\Facades\DB;
 use Tests\TestCase;
 
 class ShelvesApiTest extends TestCase
 {
     use TestsApi;
 
-    protected $baseEndpoint = '/api/shelves';
+    protected string $baseEndpoint = '/api/shelves';
 
     public function test_index_endpoint_returns_expected_shelf()
     {
@@ -111,6 +113,21 @@ class ShelvesApiTest extends TestCase
         $this->assertActivityExists('bookshelf_update', $shelf);
     }
 
+    public function test_update_increments_updated_date_if_only_tags_are_sent()
+    {
+        $this->actingAsApiEditor();
+        $shelf = Bookshelf::visible()->first();
+        DB::table('bookshelves')->where('id', '=', $shelf->id)->update(['updated_at' => Carbon::now()->subWeek()]);
+
+        $details = [
+            'tags' => [['name' => 'Category', 'value' => 'Testing']]
+        ];
+
+        $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
+        $shelf->refresh();
+        $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $shelf->updated_at->unix());
+    }
+
     public function test_update_only_assigns_books_if_param_provided()
     {
         $this->actingAsApiEditor();