Fixed issue blocking tags on book update

For #3527
This commit is contained in:
Dan Brown 2022-06-25 13:46:55 +01:00
parent 36f0a68f1b
commit 3ed1ffdbeb
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
2 changed files with 61 additions and 2 deletions

View 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')) {

View 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();