X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/f139cded789908efce3ac2ed1be26b947df647db..c89865b5741aeecf521ebecd47056f9ff2ca2a7c:/tests/Entity/BookTest.php diff --git a/tests/Entity/BookTest.php b/tests/Entity/BookTest.php index 2894fbb98..6b3c6aa38 100644 --- a/tests/Entity/BookTest.php +++ b/tests/Entity/BookTest.php @@ -3,10 +3,15 @@ namespace Tests\Entity; use BookStack\Entities\Models\Book; +use BookStack\Entities\Models\BookChild; +use BookStack\Entities\Repos\BookRepo; use Tests\TestCase; +use Tests\Uploads\UsesImages; class BookTest extends TestCase { + use UsesImages; + public function test_create() { $book = Book::factory()->make([ @@ -45,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 */ @@ -69,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(); @@ -204,4 +266,89 @@ class BookTest extends TestCase $this->assertEquals('parta-partb-partc', $book->slug); } + + public function test_show_view_has_copy_button() + { + /** @var Book $book */ + $book = Book::query()->first(); + $resp = $this->asEditor()->get($book->getUrl()); + + $resp->assertElementContains("a[href=\"{$book->getUrl('/copy')}\"]", 'Copy'); + } + + public function test_copy_view() + { + /** @var Book $book */ + $book = Book::query()->first(); + $resp = $this->asEditor()->get($book->getUrl('/copy')); + + $resp->assertOk(); + $resp->assertSee('Copy Book'); + $resp->assertElementExists("input[name=\"name\"][value=\"{$book->name}\"]"); + } + + public function test_copy() + { + /** @var Book $book */ + $book = Book::query()->whereHas('chapters')->whereHas('pages')->first(); + $resp = $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']); + + /** @var Book $copy */ + $copy = Book::query()->where('name', '=', 'My copy book')->first(); + + $resp->assertRedirect($copy->getUrl()); + $this->assertEquals($book->getDirectChildren()->count(), $copy->getDirectChildren()->count()); + } + + public function test_copy_does_not_copy_non_visible_content() + { + /** @var Book $book */ + $book = Book::query()->whereHas('chapters')->whereHas('pages')->first(); + + // Hide child content + /** @var BookChild $page */ + foreach ($book->getDirectChildren() as $child) { + $child->restricted = true; + $child->save(); + $this->regenEntityPermissions($child); + } + + $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']); + /** @var Book $copy */ + $copy = Book::query()->where('name', '=', 'My copy book')->first(); + + $this->assertEquals(0, $copy->getDirectChildren()->count()); + } + + public function test_copy_does_not_copy_pages_or_chapters_if_user_cant_create() + { + /** @var Book $book */ + $book = Book::query()->whereHas('chapters')->whereHas('directPages')->whereHas('chapters')->first(); + $viewer = $this->getViewer(); + $this->giveUserPermissions($viewer, ['book-create-all']); + + $this->actingAs($viewer)->post($book->getUrl('/copy'), ['name' => 'My copy book']); + /** @var Book $copy */ + $copy = Book::query()->where('name', '=', 'My copy book')->first(); + + $this->assertEquals(0, $copy->pages()->count()); + $this->assertEquals(0, $copy->chapters()->count()); + } + + public function test_copy_clones_cover_image_if_existing() + { + /** @var Book $book */ + $book = Book::query()->first(); + $bookRepo = $this->app->make(BookRepo::class); + $coverImageFile = $this->getTestImage('cover.png'); + $bookRepo->updateCoverImage($book, $coverImageFile); + + $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']); + + /** @var Book $copy */ + $copy = Book::query()->where('name', '=', 'My copy book')->first(); + + $this->assertNotNull($copy->cover); + $this->assertNotEquals($book->cover->id, $copy->cover->id); + } }