]> BookStack Code Mirror - bookstack/commitdiff
Fixed issue blocking tags on book update
authorDan Brown <redacted>
Sat, 25 Jun 2022 12:46:55 +0000 (13:46 +0100)
committerDan Brown <redacted>
Sat, 25 Jun 2022 12:46:55 +0000 (13:46 +0100)
For #3527

app/Http/Controllers/BookController.php
tests/Entity/BookTest.php

index 681ed96bb7cfd4bfb2afcd520e6f005956c7f59d..c5b6d0bf6def5ee529769aba77de0c8e42194ad1 100644 (file)
@@ -88,10 +88,11 @@ class BookController extends Controller
     public function store(Request $request, string $shelfSlug = null)
     {
         $this->checkPermission('book-create-all');
-        $this->validate($request, [
+        $validated = $this->validate($request, [
             'name'        => ['required', 'string', 'max:255'],
             'description' => ['string', 'max:1000'],
             'image'       => array_merge(['nullable'], $this->getImageValidationRules()),
+            'tags'        => ['array'],
         ]);
 
         $bookshelf = null;
@@ -100,7 +101,7 @@ class BookController extends Controller
             $this->checkOwnablePermission('bookshelf-update', $bookshelf);
         }
 
-        $book = $this->bookRepo->create($request->all());
+        $book = $this->bookRepo->create($validated);
 
         if ($bookshelf) {
             $bookshelf->appendBook($book);
@@ -163,6 +164,7 @@ class BookController extends Controller
             'name'        => ['required', 'string', 'max:255'],
             'description' => ['string', 'max:1000'],
             'image'       => array_merge(['nullable'], $this->getImageValidationRules()),
+            'tags'        => ['array'],
         ]);
 
         if ($request->has('image_reset')) {
index 8b2702b46e7c1bacfdcf72456cdd4a163a9f054f..285769b73d9cfcb5eb4c578b97157a8e265ed101 100644 (file)
@@ -50,6 +50,33 @@ class BookTest extends TestCase
         $this->assertEquals('my-first-book', $books[1]->slug);
     }
 
+    public function test_create_sets_tags()
+    {
+        // Cheeky initial update to refresh slug
+        $this->asEditor()->post('books', [
+            'name' => 'My book with tags',
+            'description' => 'A book with tags',
+            'tags' => [
+                [
+                    'name' => 'Category',
+                    'value' => 'Donkey Content',
+                ],
+                [
+                    'name' => 'Level',
+                    'value' => '5',
+                ]
+            ],
+        ]);
+
+        /** @var Book $book */
+        $book = Book::query()->where('name', '=', 'My book with tags')->firstOrFail();
+        $tags = $book->tags()->get();
+
+        $this->assertEquals(2, $tags->count());
+        $this->assertEquals('Donkey Content', $tags[0]->value);
+        $this->assertEquals('Level', $tags[1]->name);
+    }
+
     public function test_update()
     {
         /** @var Book $book */
@@ -74,6 +101,36 @@ class BookTest extends TestCase
         $resp->assertSee($newDesc);
     }
 
+    public function test_update_sets_tags()
+    {
+        /** @var Book $book */
+        $book = Book::query()->first();
+
+        $this->assertEquals(0, $book->tags()->count());
+
+        // Cheeky initial update to refresh slug
+        $this->asEditor()->put($book->getUrl(), [
+            'name' => $book->name,
+            'tags' => [
+                [
+                    'name' => 'Category',
+                    'value' => 'Dolphin Content',
+                ],
+                [
+                    'name' => 'Level',
+                    'value' => '5',
+                ]
+            ],
+        ]);
+
+        $book->refresh();
+        $tags = $book->tags()->get();
+
+        $this->assertEquals(2, $tags->count());
+        $this->assertEquals('Dolphin Content', $tags[0]->value);
+        $this->assertEquals('Level', $tags[1]->name);
+    }
+
     public function test_delete()
     {
         $book = Book::query()->whereHas('pages')->whereHas('chapters')->first();