diff --git a/tests/Entity/BookTest.php b/tests/Entity/BookTest.php index a3effe779..fa63c0bf9 100644 --- a/tests/Entity/BookTest.php +++ b/tests/Entity/BookTest.php @@ -3,12 +3,73 @@ namespace Tests\Entity; use BookStack\Entities\Models\Book; -use BookStack\Entities\Models\Bookshelf; use Tests\TestCase; class BookTest extends TestCase { - public function test_book_delete() + public function test_create() + { + $book = factory(Book::class)->make([ + 'name' => 'My First Book', + ]); + + $resp = $this->asEditor()->get('/books'); + $resp->assertElementContains('a[href="' . url('/create-book') . '"]', 'Create New Book'); + + $resp = $this->get('/create-book'); + $resp->assertElementContains('form[action="' . url('/books') . '"][method="POST"]', 'Save Book'); + + $resp = $this->post('/books', $book->only('name', 'description')); + $resp->assertRedirect('/books/my-first-book'); + + $resp = $this->get('/books/my-first-book'); + $resp->assertSee($book->name); + $resp->assertSee($book->description); + } + + public function test_create_uses_different_slugs_when_name_reused() + { + $book = factory(Book::class)->make([ + 'name' => 'My First Book', + ]); + + $this->asEditor()->post('/books', $book->only('name', 'description')); + $this->asEditor()->post('/books', $book->only('name', 'description')); + + $books = Book::query()->where('name', '=', $book->name) + ->orderBy('id', 'desc') + ->take(2) + ->get(); + + $this->assertMatchesRegularExpression('/my-first-book-[0-9a-zA-Z]{3}/', $books[0]->slug); + $this->assertEquals('my-first-book', $books[1]->slug); + } + + public function test_update() + { + /** @var Book $book */ + $book = Book::query()->first(); + // Cheeky initial update to refresh slug + $this->asEditor()->put($book->getUrl(), ['name' => $book->name . '5', 'description' => $book->description]); + $book->refresh(); + + $newName = $book->name . ' Updated'; + $newDesc = $book->description . ' with more content'; + + $resp = $this->get($book->getUrl('/edit')); + $resp->assertSee($book->name); + $resp->assertSee($book->description); + $resp->assertElementContains('form[action="' . $book->getUrl() . '"]', 'Save Book'); + + $resp = $this->put($book->getUrl(), ['name' => $newName, 'description' => $newDesc]); + $resp->assertRedirect($book->getUrl() . '-updated'); + + $resp = $this->get($book->getUrl() . '-updated'); + $resp->assertSee($newName); + $resp->assertSee($newDesc); + } + + public function test_delete() { $book = Book::query()->whereHas('pages')->whereHas('chapters')->first(); $this->assertNull($book->deleted_at); diff --git a/tests/Entity/ChapterTest.php b/tests/Entity/ChapterTest.php index 45c132e89..ea29ece5d 100644 --- a/tests/Entity/ChapterTest.php +++ b/tests/Entity/ChapterTest.php @@ -2,12 +2,36 @@ namespace Tests\Entity; +use BookStack\Entities\Models\Book; use BookStack\Entities\Models\Chapter; use Tests\TestCase; class ChapterTest extends TestCase { - public function test_chapter_delete() + public function test_create() + { + /** @var Book $book */ + $book = Book::query()->first(); + + $chapter = factory(Chapter::class)->make([ + 'name' => 'My First Chapter', + ]); + + $resp = $this->asEditor()->get($book->getUrl()); + $resp->assertElementContains('a[href="' . $book->getUrl('/create-chapter') . '"]', 'New Chapter'); + + $resp = $this->get($book->getUrl('/create-chapter')); + $resp->assertElementContains('form[action="' . $book->getUrl('/create-chapter') . '"][method="POST"]', 'Save Chapter'); + + $resp = $this->post($book->getUrl('/create-chapter'), $chapter->only('name', 'description')); + $resp->assertRedirect($book->getUrl('/chapter/my-first-chapter')); + + $resp = $this->get($book->getUrl('/chapter/my-first-chapter')); + $resp->assertSee($chapter->name); + $resp->assertSee($chapter->description); + } + + public function test_delete() { $chapter = Chapter::query()->whereHas('pages')->first(); $this->assertNull($chapter->deleted_at); diff --git a/tests/Entity/EntityAccessTest.php b/tests/Entity/EntityAccessTest.php new file mode 100644 index 000000000..f2f244538 --- /dev/null +++ b/tests/Entity/EntityAccessTest.php @@ -0,0 +1,52 @@ +getEditor(); + $updater = $this->getViewer(); + $entities = $this->createEntityChainBelongingToUser($creator, $updater); + app()->make(UserRepo::class)->destroy($creator); + app()->make(PageRepo::class)->update($entities['page'], ['html' => '
hello!
>']); + + $this->checkEntitiesViewable($entities); + } + + public function test_entities_viewable_after_updater_deletion() + { + // Create required assets and revisions + $creator = $this->getViewer(); + $updater = $this->getEditor(); + $entities = $this->createEntityChainBelongingToUser($creator, $updater); + app()->make(UserRepo::class)->destroy($updater); + app()->make(PageRepo::class)->update($entities['page'], ['html' => 'Hello there!
']); + + $this->checkEntitiesViewable($entities); + } + + /** + * @param arrayhello!
>']); - - $this->checkEntitiesViewable($entities); - } - - public function test_entities_viewable_after_updater_deletion() - { - // Create required assets and revisions - $creator = $this->getEditor(); - $updater = $this->getEditor(); - $entities = $this->createEntityChainBelongingToUser($creator, $updater); - $this->actingAs($updater); - app(UserRepo::class)->destroy($updater); - app(PageRepo::class)->update($entities['page'], ['html' => 'Hello there!
']); - - $this->checkEntitiesViewable($entities); - } - - private function checkEntitiesViewable($entities) - { - // Check pages and books are visible. - $this->asAdmin(); - $this->visit($entities['book']->getUrl())->seeStatusCode(200) - ->visit($entities['chapter']->getUrl())->seeStatusCode(200) - ->visit($entities['page']->getUrl())->seeStatusCode(200); - // Check revision listing shows no errors. - $this->visit($entities['page']->getUrl()) - ->click('Revisions')->seeStatusCode(200); - } - -} diff --git a/tests/Entity/PageTest.php b/tests/Entity/PageTest.php index 3fb847e42..e7118aae5 100644 --- a/tests/Entity/PageTest.php +++ b/tests/Entity/PageTest.php @@ -10,6 +10,33 @@ use Tests\TestCase; class PageTest extends TestCase { + public function test_create() + { + /** @var Chapter $chapter */ + $chapter = Chapter::query()->first(); + $page = factory(Page::class)->make([ + 'name' => 'My First Page', + ]); + + $resp = $this->asEditor()->get($chapter->getUrl()); + $resp->assertElementContains('a[href="' . $chapter->getUrl('/create-page') . '"]', 'New Page'); + + $resp = $this->get($chapter->getUrl('/create-page')); + /** @var Page $draftPage */ + $draftPage = Page::query() + ->where('draft', '=', true) + ->orderBy('created_at', 'desc') + ->first(); + $resp->assertRedirect($draftPage->getUrl()); + + $resp = $this->get($draftPage->getUrl()); + $resp->assertElementContains('form[action="' . $draftPage->getUrl() . '"][method="POST"]', 'Save Page'); + + $resp = $this->post($draftPage->getUrl(), $draftPage->only('name', 'html')); + $draftPage->refresh(); + $resp->assertRedirect($draftPage->getUrl()); + } + public function test_page_view_when_creator_is_deleted_but_owner_exists() { $page = Page::query()->first();