From 5d93dd258ebc7fe5f63567597fea75c3059ddbf0 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Fri, 17 Sep 2021 21:29:16 +0100 Subject: [PATCH] Finished moving EntityTests out to new TestCase files --- tests/Entity/BookTest.php | 65 +++++++++++- tests/Entity/ChapterTest.php | 26 ++++- tests/Entity/EntityAccessTest.php | 52 ++++++++++ tests/Entity/EntityTest.php | 164 ------------------------------ tests/Entity/PageTest.php | 27 +++++ 5 files changed, 167 insertions(+), 167 deletions(-) create mode 100644 tests/Entity/EntityAccessTest.php delete mode 100644 tests/Entity/EntityTest.php 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 array $entities + */ + private function checkEntitiesViewable(array $entities) + { + // Check pages and books are visible. + $this->asAdmin(); + foreach ($entities as $entity) { + $this->get($entity->getUrl()) + ->assertStatus(200) + ->assertSee($entity->name); + } + + // Check revision listing shows no errors. + $this->get($entities['page']->getUrl('/revisions'))->assertStatus(200); + } +} diff --git a/tests/Entity/EntityTest.php b/tests/Entity/EntityTest.php deleted file mode 100644 index 16f21bd5d..000000000 --- a/tests/Entity/EntityTest.php +++ /dev/null @@ -1,164 +0,0 @@ -bookCreation(); - $chapter = $this->chapterCreation($book); - $this->pageCreation($chapter); - - // Test Updating - $this->bookUpdate($book); - } - - public function bookUpdate(Book $book) - { - $newName = $book->name . ' Updated'; - $this->asAdmin() - // Go to edit screen - ->visit($book->getUrl() . '/edit') - ->see($book->name) - // Submit new name - ->type($newName, '#name') - ->press('Save Book') - // Check page url and text - ->seePageIs($book->getUrl() . '-updated') - ->see($newName); - - return Book::find($book->id); - } - - public function pageCreation($chapter) - { - $page = factory(Page::class)->make([ - 'name' => 'My First Page', - ]); - - $this->asAdmin() - // Navigate to page create form - ->visit($chapter->getUrl()) - ->click('New Page'); - - $draftPage = Page::where('draft', '=', true)->orderBy('created_at', 'desc')->first(); - - $this->seePageIs($draftPage->getUrl()) - // Fill out form - ->type($page->name, '#name') - ->type($page->html, '#html') - ->press('Save Page') - // Check redirect and page - ->seePageIs($chapter->book->getUrl() . '/page/my-first-page') - ->see($page->name); - - $page = Page::where('slug', '=', 'my-first-page')->where('chapter_id', '=', $chapter->id)->first(); - - return $page; - } - - public function chapterCreation(Book $book) - { - $chapter = factory(Chapter::class)->make([ - 'name' => 'My First Chapter', - ]); - - $this->asAdmin() - // Navigate to chapter create page - ->visit($book->getUrl()) - ->click('New Chapter') - ->seePageIs($book->getUrl() . '/create-chapter') - // Fill out form - ->type($chapter->name, '#name') - ->type($chapter->description, '#description') - ->press('Save Chapter') - // Check redirect and landing page - ->seePageIs($book->getUrl() . '/chapter/my-first-chapter') - ->see($chapter->name)->see($chapter->description); - - $chapter = Chapter::where('slug', '=', 'my-first-chapter')->where('book_id', '=', $book->id)->first(); - - return $chapter; - } - - public function bookCreation() - { - $book = factory(Book::class)->make([ - 'name' => 'My First Book', - ]); - $this->asAdmin() - ->visit('/books') - // Choose to create a book - ->click('Create New Book') - ->seePageIs('/create-book') - // Fill out form & save - ->type($book->name, '#name') - ->type($book->description, '#description') - ->press('Save Book') - // Check it redirects correctly - ->seePageIs('/books/my-first-book') - ->see($book->name)->see($book->description); - - // Ensure duplicate names are given different slugs - $this->asAdmin() - ->visit('/create-book') - ->type($book->name, '#name') - ->type($book->description, '#description') - ->press('Save Book'); - - $expectedPattern = '/\/books\/my-first-book-[0-9a-zA-Z]{3}/'; - $this->assertMatchesRegularExpression($expectedPattern, $this->currentUri, "Did not land on expected page [$expectedPattern].\n"); - - $book = Book::where('slug', '=', 'my-first-book')->first(); - - return $book; - } - - public function test_entities_viewable_after_creator_deletion() - { - // Create required assets and revisions - $creator = $this->getEditor(); - $updater = $this->getEditor(); - $entities = $this->createEntityChainBelongingToUser($creator, $updater); - $this->actingAs($creator); - app(UserRepo::class)->destroy($creator); - app(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->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();