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;
$this->checkOwnablePermission('bookshelf-update', $bookshelf);
}
- $book = $this->bookRepo->create($request->all());
+ $book = $this->bookRepo->create($validated);
if ($bookshelf) {
$bookshelf->appendBook($book);
'name' => ['required', 'string', 'max:255'],
'description' => ['string', 'max:1000'],
'image' => array_merge(['nullable'], $this->getImageValidationRules()),
+ 'tags' => ['array'],
]);
if ($request->has('image_reset')) {
$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 */
$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();