From 3ed1ffdbeb043431788c87f761cabda52fd379db Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Sat, 25 Jun 2022 13:46:55 +0100 Subject: [PATCH] Fixed issue blocking tags on book update For #3527 --- app/Http/Controllers/BookController.php | 6 ++- tests/Entity/BookTest.php | 57 +++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/BookController.php b/app/Http/Controllers/BookController.php index 681ed96bb..c5b6d0bf6 100644 --- a/app/Http/Controllers/BookController.php +++ b/app/Http/Controllers/BookController.php @@ -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')) { diff --git a/tests/Entity/BookTest.php b/tests/Entity/BookTest.php index 8b2702b46..285769b73 100644 --- a/tests/Entity/BookTest.php +++ b/tests/Entity/BookTest.php @@ -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();