From 4d4a57d1bfb1c57541d82eb7910a9754fe0fc8cf Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Wed, 3 Mar 2021 22:11:00 +0000 Subject: [PATCH] Converted some tests from BrowserKit, Updated shared helpers --- app/Auth/User.php | 1 + tests/Auth/UserInviteTest.php | 8 +- tests/BrowserKitTest.php | 1 - tests/Entity/SortTest.php | 20 +-- tests/Permissions/EntityPermissionsTest.php | 106 ++++++------- tests/PublicActionTest.php | 155 ++++++++++---------- tests/SharedTestHelpers.php | 71 +++------ 7 files changed, 170 insertions(+), 192 deletions(-) diff --git a/app/Auth/User.php b/app/Auth/User.php index 9d7eaa72e..9d2210101 100644 --- a/app/Auth/User.php +++ b/app/Auth/User.php @@ -30,6 +30,7 @@ use Illuminate\Support\Collection; * @property int $image_id * @property string $external_auth_id * @property string $system_name + * @property Collection $roles */ class User extends Model implements AuthenticatableContract, CanResetPasswordContract, Loggable { diff --git a/tests/Auth/UserInviteTest.php b/tests/Auth/UserInviteTest.php index f2a1d0e78..b6f521eaa 100644 --- a/tests/Auth/UserInviteTest.php +++ b/tests/Auth/UserInviteTest.php @@ -18,13 +18,15 @@ class UserInviteTest extends TestCase Notification::fake(); $admin = $this->getAdmin(); - $this->actingAs($admin)->post('/settings/users/create', [ + $email = Str::random(16) . '@example.com'; + $resp = $this->actingAs($admin)->post('/settings/users/create', [ 'name' => 'Barry', - 'email' => 'tester@example.com', + 'email' => $email, 'send_invite' => 'true', ]); + $resp->assertRedirect('/settings/users'); - $newUser = User::query()->where('email', '=', 'tester@example.com')->orderBy('id', 'desc')->first(); + $newUser = User::query()->where('email', '=', $email)->orderBy('id', 'desc')->first(); Notification::assertSentTo($newUser, UserInvite::class); $this->assertDatabaseHas('user_invites', [ diff --git a/tests/BrowserKitTest.php b/tests/BrowserKitTest.php index 6c332a984..135b9a28e 100644 --- a/tests/BrowserKitTest.php +++ b/tests/BrowserKitTest.php @@ -4,7 +4,6 @@ use BookStack\Auth\User; use BookStack\Entities\Models\Book; use BookStack\Entities\Models\Chapter; use BookStack\Entities\Models\Entity; -use BookStack\Auth\Role; use BookStack\Auth\Permissions\PermissionService; use BookStack\Entities\Models\Page; use BookStack\Settings\SettingService; diff --git a/tests/Entity/SortTest.php b/tests/Entity/SortTest.php index d75a134ea..f3f6a8a1a 100644 --- a/tests/Entity/SortTest.php +++ b/tests/Entity/SortTest.php @@ -91,19 +91,19 @@ class SortTest extends TestCase public function test_page_move_requires_create_permissions_on_parent() { - $page = Page::first(); + $page = Page::query()->first(); $currentBook = $page->book; - $newBook = Book::where('id', '!=', $currentBook->id)->first(); + $newBook = Book::query()->where('id', '!=', $currentBook->id)->first(); $editor = $this->getEditor(); - $this->setEntityRestrictions($newBook, ['view', 'update', 'delete'], $editor->roles); + $this->setEntityRestrictions($newBook, ['view', 'update', 'delete'], $editor->roles->all()); $movePageResp = $this->actingAs($editor)->put($page->getUrl('/move'), [ 'entity_selection' => 'book:' . $newBook->id ]); $this->assertPermissionError($movePageResp); - $this->setEntityRestrictions($newBook, ['view', 'update', 'delete', 'create'], $editor->roles); + $this->setEntityRestrictions($newBook, ['view', 'update', 'delete', 'create'], $editor->roles->all()); $movePageResp = $this->put($page->getUrl('/move'), [ 'entity_selection' => 'book:' . $newBook->id ]); @@ -121,8 +121,8 @@ class SortTest extends TestCase $newBook = Book::where('id', '!=', $currentBook->id)->first(); $editor = $this->getEditor(); - $this->setEntityRestrictions($newBook, ['view', 'update', 'create', 'delete'], $editor->roles); - $this->setEntityRestrictions($page, ['view', 'update', 'create'], $editor->roles); + $this->setEntityRestrictions($newBook, ['view', 'update', 'create', 'delete'], $editor->roles->all()); + $this->setEntityRestrictions($page, ['view', 'update', 'create'], $editor->roles->all()); $movePageResp = $this->actingAs($editor)->put($page->getUrl('/move'), [ 'entity_selection' => 'book:' . $newBook->id @@ -131,7 +131,7 @@ class SortTest extends TestCase $pageView = $this->get($page->getUrl()); $pageView->assertDontSee($page->getUrl('/move')); - $this->setEntityRestrictions($page, ['view', 'update', 'create', 'delete'], $editor->roles); + $this->setEntityRestrictions($page, ['view', 'update', 'create', 'delete'], $editor->roles->all()); $movePageResp = $this->put($page->getUrl('/move'), [ 'entity_selection' => 'book:' . $newBook->id ]); @@ -176,8 +176,8 @@ class SortTest extends TestCase $newBook = Book::where('id', '!=', $currentBook->id)->first(); $editor = $this->getEditor(); - $this->setEntityRestrictions($newBook, ['view', 'update', 'create', 'delete'], $editor->roles); - $this->setEntityRestrictions($chapter, ['view', 'update', 'create'], $editor->roles); + $this->setEntityRestrictions($newBook, ['view', 'update', 'create', 'delete'], $editor->roles->all()); + $this->setEntityRestrictions($chapter, ['view', 'update', 'create'], $editor->roles->all()); $moveChapterResp = $this->actingAs($editor)->put($chapter->getUrl('/move'), [ 'entity_selection' => 'book:' . $newBook->id @@ -186,7 +186,7 @@ class SortTest extends TestCase $pageView = $this->get($chapter->getUrl()); $pageView->assertDontSee($chapter->getUrl('/move')); - $this->setEntityRestrictions($chapter, ['view', 'update', 'create', 'delete'], $editor->roles); + $this->setEntityRestrictions($chapter, ['view', 'update', 'create', 'delete'], $editor->roles->all()); $moveChapterResp = $this->put($chapter->getUrl('/move'), [ 'entity_selection' => 'book:' . $newBook->id ]); diff --git a/tests/Permissions/EntityPermissionsTest.php b/tests/Permissions/EntityPermissionsTest.php index 1e6d1cc32..8dc112e57 100644 --- a/tests/Permissions/EntityPermissionsTest.php +++ b/tests/Permissions/EntityPermissionsTest.php @@ -29,13 +29,13 @@ class EntityPermissionsTest extends BrowserKitTest $this->viewer = $this->getViewer(); } - protected function setEntityRestrictions(Entity $entity, $actions = [], $roles = []) + protected function setRestrictionsForTestRoles(Entity $entity, array $actions = []) { $roles = [ $this->user->roles->first(), $this->viewer->roles->first(), ]; - parent::setEntityRestrictions($entity, $actions, $roles); + $this->setEntityRestrictions($entity, $actions, $roles); } public function test_bookshelf_view_restriction() @@ -46,12 +46,12 @@ class EntityPermissionsTest extends BrowserKitTest ->visit($shelf->getUrl()) ->seePageIs($shelf->getUrl()); - $this->setEntityRestrictions($shelf, []); + $this->setRestrictionsForTestRoles($shelf, []); $this->forceVisit($shelf->getUrl()) ->see('Bookshelf not found'); - $this->setEntityRestrictions($shelf, ['view']); + $this->setRestrictionsForTestRoles($shelf, ['view']); $this->visit($shelf->getUrl()) ->see($shelf->name); @@ -65,12 +65,12 @@ class EntityPermissionsTest extends BrowserKitTest ->visit($shelf->getUrl('/edit')) ->see('Edit Book'); - $this->setEntityRestrictions($shelf, ['view', 'delete']); + $this->setRestrictionsForTestRoles($shelf, ['view', 'delete']); $this->forceVisit($shelf->getUrl('/edit')) ->see('You do not have permission')->seePageIs('/'); - $this->setEntityRestrictions($shelf, ['view', 'update']); + $this->setRestrictionsForTestRoles($shelf, ['view', 'update']); $this->visit($shelf->getUrl('/edit')) ->seePageIs($shelf->getUrl('/edit')); @@ -84,12 +84,12 @@ class EntityPermissionsTest extends BrowserKitTest ->visit($shelf->getUrl('/delete')) ->see('Delete Book'); - $this->setEntityRestrictions($shelf, ['view', 'update']); + $this->setRestrictionsForTestRoles($shelf, ['view', 'update']); $this->forceVisit($shelf->getUrl('/delete')) ->see('You do not have permission')->seePageIs('/'); - $this->setEntityRestrictions($shelf, ['view', 'delete']); + $this->setRestrictionsForTestRoles($shelf, ['view', 'delete']); $this->visit($shelf->getUrl('/delete')) ->seePageIs($shelf->getUrl('/delete'))->see('Delete Book'); @@ -106,7 +106,7 @@ class EntityPermissionsTest extends BrowserKitTest ->visit($bookUrl) ->seePageIs($bookUrl); - $this->setEntityRestrictions($book, []); + $this->setRestrictionsForTestRoles($book, []); $this->forceVisit($bookUrl) ->see('Book not found'); @@ -115,7 +115,7 @@ class EntityPermissionsTest extends BrowserKitTest $this->forceVisit($bookChapter->getUrl()) ->see('Chapter not found'); - $this->setEntityRestrictions($book, ['view']); + $this->setRestrictionsForTestRoles($book, ['view']); $this->visit($bookUrl) ->see($book->name); @@ -139,7 +139,7 @@ class EntityPermissionsTest extends BrowserKitTest ->seeInElement('.actions', 'New Page') ->seeInElement('.actions', 'New Chapter'); - $this->setEntityRestrictions($book, ['view', 'delete', 'update']); + $this->setRestrictionsForTestRoles($book, ['view', 'delete', 'update']); $this->forceVisit($bookUrl . '/create-chapter') ->see('You do not have permission')->seePageIs('/'); @@ -148,7 +148,7 @@ class EntityPermissionsTest extends BrowserKitTest $this->visit($bookUrl)->dontSeeInElement('.actions', 'New Page') ->dontSeeInElement('.actions', 'New Chapter'); - $this->setEntityRestrictions($book, ['view', 'create']); + $this->setRestrictionsForTestRoles($book, ['view', 'create']); $this->visit($bookUrl . '/create-chapter') ->type('test chapter', 'name') @@ -175,7 +175,7 @@ class EntityPermissionsTest extends BrowserKitTest ->visit($bookUrl . '/edit') ->see('Edit Book'); - $this->setEntityRestrictions($book, ['view', 'delete']); + $this->setRestrictionsForTestRoles($book, ['view', 'delete']); $this->forceVisit($bookUrl . '/edit') ->see('You do not have permission')->seePageIs('/'); @@ -184,7 +184,7 @@ class EntityPermissionsTest extends BrowserKitTest $this->forceVisit($bookChapter->getUrl() . '/edit') ->see('You do not have permission')->seePageIs('/'); - $this->setEntityRestrictions($book, ['view', 'update']); + $this->setRestrictionsForTestRoles($book, ['view', 'update']); $this->visit($bookUrl . '/edit') ->seePageIs($bookUrl . '/edit'); @@ -205,7 +205,7 @@ class EntityPermissionsTest extends BrowserKitTest ->visit($bookUrl . '/delete') ->see('Delete Book'); - $this->setEntityRestrictions($book, ['view', 'update']); + $this->setRestrictionsForTestRoles($book, ['view', 'update']); $this->forceVisit($bookUrl . '/delete') ->see('You do not have permission')->seePageIs('/'); @@ -214,7 +214,7 @@ class EntityPermissionsTest extends BrowserKitTest $this->forceVisit($bookChapter->getUrl() . '/delete') ->see('You do not have permission')->seePageIs('/'); - $this->setEntityRestrictions($book, ['view', 'delete']); + $this->setRestrictionsForTestRoles($book, ['view', 'delete']); $this->visit($bookUrl . '/delete') ->seePageIs($bookUrl . '/delete')->see('Delete Book'); @@ -234,14 +234,14 @@ class EntityPermissionsTest extends BrowserKitTest ->visit($chapterUrl) ->seePageIs($chapterUrl); - $this->setEntityRestrictions($chapter, []); + $this->setRestrictionsForTestRoles($chapter, []); $this->forceVisit($chapterUrl) ->see('Chapter not found'); $this->forceVisit($chapterPage->getUrl()) ->see('Page not found'); - $this->setEntityRestrictions($chapter, ['view']); + $this->setRestrictionsForTestRoles($chapter, ['view']); $this->visit($chapterUrl) ->see($chapter->name); @@ -258,13 +258,13 @@ class EntityPermissionsTest extends BrowserKitTest ->visit($chapterUrl) ->seeInElement('.actions', 'New Page'); - $this->setEntityRestrictions($chapter, ['view', 'delete', 'update']); + $this->setRestrictionsForTestRoles($chapter, ['view', 'delete', 'update']); $this->forceVisit($chapterUrl . '/create-page') ->see('You do not have permission')->seePageIs('/'); $this->visit($chapterUrl)->dontSeeInElement('.actions', 'New Page'); - $this->setEntityRestrictions($chapter, ['view', 'create']); + $this->setRestrictionsForTestRoles($chapter, ['view', 'create']); $this->visit($chapterUrl . '/create-page') @@ -286,14 +286,14 @@ class EntityPermissionsTest extends BrowserKitTest ->visit($chapterUrl . '/edit') ->see('Edit Chapter'); - $this->setEntityRestrictions($chapter, ['view', 'delete']); + $this->setRestrictionsForTestRoles($chapter, ['view', 'delete']); $this->forceVisit($chapterUrl . '/edit') ->see('You do not have permission')->seePageIs('/'); $this->forceVisit($chapterPage->getUrl() . '/edit') ->see('You do not have permission')->seePageIs('/'); - $this->setEntityRestrictions($chapter, ['view', 'update']); + $this->setRestrictionsForTestRoles($chapter, ['view', 'update']); $this->visit($chapterUrl . '/edit') ->seePageIs($chapterUrl . '/edit')->see('Edit Chapter'); @@ -311,14 +311,14 @@ class EntityPermissionsTest extends BrowserKitTest ->visit($chapterUrl . '/delete') ->see('Delete Chapter'); - $this->setEntityRestrictions($chapter, ['view', 'update']); + $this->setRestrictionsForTestRoles($chapter, ['view', 'update']); $this->forceVisit($chapterUrl . '/delete') ->see('You do not have permission')->seePageIs('/'); $this->forceVisit($chapterPage->getUrl() . '/delete') ->see('You do not have permission')->seePageIs('/'); - $this->setEntityRestrictions($chapter, ['view', 'delete']); + $this->setRestrictionsForTestRoles($chapter, ['view', 'delete']); $this->visit($chapterUrl . '/delete') ->seePageIs($chapterUrl . '/delete')->see('Delete Chapter'); @@ -335,12 +335,12 @@ class EntityPermissionsTest extends BrowserKitTest ->visit($pageUrl) ->seePageIs($pageUrl); - $this->setEntityRestrictions($page, ['update', 'delete']); + $this->setRestrictionsForTestRoles($page, ['update', 'delete']); $this->forceVisit($pageUrl) ->see('Page not found'); - $this->setEntityRestrictions($page, ['view']); + $this->setRestrictionsForTestRoles($page, ['view']); $this->visit($pageUrl) ->see($page->name); @@ -355,12 +355,12 @@ class EntityPermissionsTest extends BrowserKitTest ->visit($pageUrl . '/edit') ->seeInField('name', $page->name); - $this->setEntityRestrictions($page, ['view', 'delete']); + $this->setRestrictionsForTestRoles($page, ['view', 'delete']); $this->forceVisit($pageUrl . '/edit') ->see('You do not have permission')->seePageIs('/'); - $this->setEntityRestrictions($page, ['view', 'update']); + $this->setRestrictionsForTestRoles($page, ['view', 'update']); $this->visit($pageUrl . '/edit') ->seePageIs($pageUrl . '/edit')->seeInField('name', $page->name); @@ -375,12 +375,12 @@ class EntityPermissionsTest extends BrowserKitTest ->visit($pageUrl . '/delete') ->see('Delete Page'); - $this->setEntityRestrictions($page, ['view', 'update']); + $this->setRestrictionsForTestRoles($page, ['view', 'update']); $this->forceVisit($pageUrl . '/delete') ->see('You do not have permission')->seePageIs('/'); - $this->setEntityRestrictions($page, ['view', 'delete']); + $this->setRestrictionsForTestRoles($page, ['view', 'delete']); $this->visit($pageUrl . '/delete') ->seePageIs($pageUrl . '/delete')->see('Delete Page'); @@ -460,7 +460,7 @@ class EntityPermissionsTest extends BrowserKitTest $page = $chapter->pages->first(); $page2 = $chapter->pages[2]; - $this->setEntityRestrictions($page, []); + $this->setRestrictionsForTestRoles($page, []); $this->actingAs($this->user) ->visit($page2->getUrl()) @@ -472,7 +472,7 @@ class EntityPermissionsTest extends BrowserKitTest $chapter = Chapter::first(); $page = $chapter->pages->first(); - $this->setEntityRestrictions($page, []); + $this->setRestrictionsForTestRoles($page, []); $this->actingAs($this->user) ->visit($chapter->getUrl()) @@ -484,7 +484,7 @@ class EntityPermissionsTest extends BrowserKitTest $chapter = Chapter::first(); $page = $chapter->pages->first(); - $this->setEntityRestrictions($page, []); + $this->setRestrictionsForTestRoles($page, []); $this->actingAs($this->user) ->visit($chapter->getUrl()) @@ -499,7 +499,7 @@ class EntityPermissionsTest extends BrowserKitTest ->see($chapter->pages->first()->name); foreach ($chapter->pages as $page) { - $this->setEntityRestrictions($page, []); + $this->setRestrictionsForTestRoles($page, []); } $this->actingAs($this->user) @@ -515,12 +515,12 @@ class EntityPermissionsTest extends BrowserKitTest ->visit($shelf->getUrl('/edit')) ->dontSee('Edit Book'); - $this->setEntityRestrictions($shelf, ['view', 'delete']); + $this->setRestrictionsForTestRoles($shelf, ['view', 'delete']); $this->forceVisit($shelf->getUrl('/edit')) ->see('You do not have permission')->seePageIs('/'); - $this->setEntityRestrictions($shelf, ['view', 'update']); + $this->setRestrictionsForTestRoles($shelf, ['view', 'update']); $this->visit($shelf->getUrl('/edit')) ->seePageIs($shelf->getUrl('/edit')); @@ -534,12 +534,12 @@ class EntityPermissionsTest extends BrowserKitTest ->visit($shelf->getUrl('/delete')) ->dontSee('Delete Book'); - $this->setEntityRestrictions($shelf, ['view', 'update']); + $this->setRestrictionsForTestRoles($shelf, ['view', 'update']); $this->forceVisit($shelf->getUrl('/delete')) ->see('You do not have permission')->seePageIs('/'); - $this->setEntityRestrictions($shelf, ['view', 'delete']); + $this->setRestrictionsForTestRoles($shelf, ['view', 'delete']); $this->visit($shelf->getUrl('/delete')) ->seePageIs($shelf->getUrl('/delete'))->see('Delete Book'); @@ -555,7 +555,7 @@ class EntityPermissionsTest extends BrowserKitTest ->dontSeeInElement('.actions', 'New Page') ->dontSeeInElement('.actions', 'New Chapter'); - $this->setEntityRestrictions($book, ['view', 'delete', 'update']); + $this->setRestrictionsForTestRoles($book, ['view', 'delete', 'update']); $this->forceVisit($bookUrl . '/create-chapter') ->see('You do not have permission')->seePageIs('/'); @@ -564,7 +564,7 @@ class EntityPermissionsTest extends BrowserKitTest $this->visit($bookUrl)->dontSeeInElement('.actions', 'New Page') ->dontSeeInElement('.actions', 'New Chapter'); - $this->setEntityRestrictions($book, ['view', 'create']); + $this->setRestrictionsForTestRoles($book, ['view', 'create']); $this->visit($bookUrl . '/create-chapter') ->type('test chapter', 'name') @@ -591,7 +591,7 @@ class EntityPermissionsTest extends BrowserKitTest ->visit($bookUrl . '/edit') ->dontSee('Edit Book'); - $this->setEntityRestrictions($book, ['view', 'delete']); + $this->setRestrictionsForTestRoles($book, ['view', 'delete']); $this->forceVisit($bookUrl . '/edit') ->see('You do not have permission')->seePageIs('/'); @@ -600,7 +600,7 @@ class EntityPermissionsTest extends BrowserKitTest $this->forceVisit($bookChapter->getUrl() . '/edit') ->see('You do not have permission')->seePageIs('/'); - $this->setEntityRestrictions($book, ['view', 'update']); + $this->setRestrictionsForTestRoles($book, ['view', 'update']); $this->visit($bookUrl . '/edit') ->seePageIs($bookUrl . '/edit'); @@ -621,7 +621,7 @@ class EntityPermissionsTest extends BrowserKitTest ->visit($bookUrl . '/delete') ->dontSee('Delete Book'); - $this->setEntityRestrictions($book, ['view', 'update']); + $this->setRestrictionsForTestRoles($book, ['view', 'update']); $this->forceVisit($bookUrl . '/delete') ->see('You do not have permission')->seePageIs('/'); @@ -630,7 +630,7 @@ class EntityPermissionsTest extends BrowserKitTest $this->forceVisit($bookChapter->getUrl() . '/delete') ->see('You do not have permission')->seePageIs('/'); - $this->setEntityRestrictions($book, ['view', 'delete']); + $this->setRestrictionsForTestRoles($book, ['view', 'delete']); $this->visit($bookUrl . '/delete') ->seePageIs($bookUrl . '/delete')->see('Delete Book'); @@ -651,8 +651,8 @@ class EntityPermissionsTest extends BrowserKitTest $entity->save(); } - $this->setEntityRestrictions($book, []); - $this->setEntityRestrictions($bookPage, ['view']); + $this->setRestrictionsForTestRoles($book, []); + $this->setRestrictionsForTestRoles($bookPage, ['view']); $this->actingAs($this->viewer); $this->get($bookPage->getUrl()); @@ -667,8 +667,8 @@ class EntityPermissionsTest extends BrowserKitTest $firstBook = Book::first(); $secondBook = Book::find(2); - $this->setEntityRestrictions($firstBook, ['view', 'update']); - $this->setEntityRestrictions($secondBook, ['view']); + $this->setRestrictionsForTestRoles($firstBook, ['view', 'update']); + $this->setRestrictionsForTestRoles($secondBook, ['view']); // Test sort page visibility $this->actingAs($this->user)->visit($secondBook->getUrl() . '/sort') @@ -683,8 +683,8 @@ class EntityPermissionsTest extends BrowserKitTest $firstBook = Book::first(); $secondBook = Book::find(2); - $this->setEntityRestrictions($firstBook, ['view', 'update']); - $this->setEntityRestrictions($secondBook, ['view']); + $this->setRestrictionsForTestRoles($firstBook, ['view', 'update']); + $this->setRestrictionsForTestRoles($secondBook, ['view']); $firstBookChapter = $this->newChapter(['name' => 'first book chapter'], $firstBook); $secondBookChapter = $this->newChapter(['name' => 'second book chapter'], $secondBook); @@ -726,14 +726,14 @@ class EntityPermissionsTest extends BrowserKitTest public function test_can_create_page_if_chapter_has_permissions_when_book_not_visible() { $book = Book::first(); - $this->setEntityRestrictions($book, []); + $this->setRestrictionsForTestRoles($book, []); $bookChapter = $book->chapters->first(); - $this->setEntityRestrictions($bookChapter, ['view']); + $this->setRestrictionsForTestRoles($bookChapter, ['view']); $this->actingAs($this->user)->visit($bookChapter->getUrl()) ->dontSee('New Page'); - $this->setEntityRestrictions($bookChapter, ['view', 'create']); + $this->setRestrictionsForTestRoles($bookChapter, ['view', 'create']); $this->actingAs($this->user)->visit($bookChapter->getUrl()) ->click('New Page') diff --git a/tests/PublicActionTest.php b/tests/PublicActionTest.php index 194190124..7caefd0ac 100644 --- a/tests/PublicActionTest.php +++ b/tests/PublicActionTest.php @@ -9,71 +9,73 @@ use BookStack\Entities\Models\Book; use BookStack\Entities\Models\Chapter; use BookStack\Entities\Models\Page; -class PublicActionTest extends BrowserKitTest +class PublicActionTest extends TestCase { public function test_app_not_public() { $this->setSettings(['app-public' => 'false']); - $book = Book::orderBy('name', 'asc')->first(); - $this->visit('/books')->seePageIs('/login'); - $this->visit($book->getUrl())->seePageIs('/login'); + $book = Book::query()->first(); + $this->get('/books')->assertRedirect('/login'); + $this->get($book->getUrl())->assertRedirect('/login'); - $page = Page::first(); - $this->visit($page->getUrl())->seePageIs('/login'); + $page = Page::query()->first(); + $this->get($page->getUrl())->assertRedirect('/login'); } public function test_login_link_visible() { $this->setSettings(['app-public' => 'true']); - $this->visit('/')->see(url('/login')); + $this->get('/')->assertElementExists('a[href="'.url('/login').'"]'); } public function test_register_link_visible_when_enabled() { $this->setSettings(['app-public' => 'true']); - - $this->visit('/')->see(url('/login')); - $this->visit('/')->dontSee(url('/register')); + $home = $this->get('/'); + $home->assertSee(url('/login')); + $home->assertDontSee(url('/register')); $this->setSettings(['app-public' => 'true', 'registration-enabled' => 'true']); - $this->visit('/')->see(url('/login')); - $this->visit('/')->see(url('/register')); + $home = $this->get('/'); + $home->assertSee(url('/login')); + $home->assertSee(url('/register')); } public function test_books_viewable() { $this->setSettings(['app-public' => 'true']); - $books = Book::orderBy('name', 'asc')->take(10)->get(); + $books = Book::query()->orderBy('name', 'asc')->take(10)->get(); $bookToVisit = $books[1]; // Check books index page is showing - $this->visit('/books') - ->seeStatusCode(200) - ->see($books[0]->name) - // Check individual book page is showing and it's child contents are visible. - ->click($bookToVisit->name) - ->seePageIs($bookToVisit->getUrl()) - ->see($bookToVisit->name) - ->see($bookToVisit->chapters()->first()->name); + $resp = $this->get('/books'); + $resp->assertStatus(200); + $resp->assertSee($books[0]->name); + + // Check individual book page is showing and it's child contents are visible. + $resp = $this->get($bookToVisit->getUrl()); + $resp->assertSee($bookToVisit->name); + $resp->assertSee($bookToVisit->chapters()->first()->name); } public function test_chapters_viewable() { $this->setSettings(['app-public' => 'true']); - $chapterToVisit = Chapter::first(); + /** @var Chapter $chapterToVisit */ + $chapterToVisit = Chapter::query()->first(); $pageToVisit = $chapterToVisit->pages()->first(); // Check chapters index page is showing - $this->visit($chapterToVisit->getUrl()) - ->seeStatusCode(200) - ->see($chapterToVisit->name) - // Check individual chapter page is showing and it's child contents are visible. - ->see($pageToVisit->name) - ->click($pageToVisit->name) - ->see($chapterToVisit->book->name) - ->see($chapterToVisit->name) - ->seePageIs($pageToVisit->getUrl()); + $resp = $this->get($chapterToVisit->getUrl()); + $resp->assertStatus(200); + $resp->assertSee($chapterToVisit->name); + // Check individual chapter page is showing and it's child contents are visible. + $resp->assertSee($pageToVisit->name); + $resp = $this->get($pageToVisit->getUrl()); + $resp->assertStatus(200); + $resp->assertSee($chapterToVisit->book->name); + $resp->assertSee($chapterToVisit->name); } public function test_public_page_creation() @@ -87,19 +89,22 @@ class PublicActionTest extends BrowserKitTest } $this->app[PermissionService::class]->buildJointPermissionForRole($publicRole); - $chapter = Chapter::first(); - $this->visit($chapter->book->getUrl()); - $this->visit($chapter->getUrl()) - ->click('New Page') - ->see('New Page') - ->seePageIs($chapter->getUrl('/create-page')); + /** @var Chapter $chapter */ + $chapter = Chapter::query()->first(); + $resp = $this->get($chapter->getUrl()); + $resp->assertSee('New Page'); + $resp->assertElementExists('a[href="'.$chapter->getUrl('/create-page').'"]'); - $this->submitForm('Continue', [ - 'name' => 'My guest page' - ])->seePageIs($chapter->book->getUrl('/page/my-guest-page/edit')); + $resp = $this->get($chapter->getUrl('/create-page')); + $resp->assertSee('Continue'); + $resp->assertSee('Page Name'); + $resp->assertElementExists('form[action="'.$chapter->getUrl('/create-guest-page').'"]'); + + $resp = $this->post($chapter->getUrl('/create-guest-page'), ['name' => 'My guest page']); + $resp->assertRedirect($chapter->book->getUrl('/page/my-guest-page/edit')); $user = User::getDefault(); - $this->seeInDatabase('pages', [ + $this->assertDatabaseHas('pages', [ 'name' => 'My guest page', 'chapter_id' => $chapter->id, 'created_by' => $user->id, @@ -109,75 +114,71 @@ class PublicActionTest extends BrowserKitTest public function test_content_not_listed_on_404_for_public_users() { - $page = Page::first(); - $this->asAdmin()->visit($page->getUrl()); + $page = Page::query()->first(); + $this->asAdmin()->get($page->getUrl()); // Fake visit to show on recents + $resp = $this->get('/cats/dogs/hippos'); + $resp->assertStatus(404); + $resp->assertSee($page->name); + Auth::logout(); - view()->share('pageTitle', ''); - $this->forceVisit('/cats/dogs/hippos'); - $this->dontSee($page->name); + $resp = $this->get('/cats/dogs/hippos'); + $resp->assertStatus(404); + $resp->assertDontSee($page->name); } public function test_robots_effected_by_public_status() { - $this->visit('/robots.txt'); - $this->seeText("User-agent: *\nDisallow: /"); + $this->get('/robots.txt')->assertSee("User-agent: *\nDisallow: /"); $this->setSettings(['app-public' => 'true']); - $this->visit('/robots.txt'); - $this->seeText("User-agent: *\nDisallow:"); - $this->dontSeeText("Disallow: /"); + $resp = $this->get('/robots.txt'); + $resp->assertSee("User-agent: *\nDisallow:"); + $resp->assertDontSee("Disallow: /"); } public function test_robots_effected_by_setting() { - $this->visit('/robots.txt'); - $this->seeText("User-agent: *\nDisallow: /"); + $this->get('/robots.txt')->assertSee("User-agent: *\nDisallow: /"); config()->set('app.allow_robots', true); - $this->visit('/robots.txt'); - $this->seeText("User-agent: *\nDisallow:"); - $this->dontSeeText("Disallow: /"); + $resp = $this->get('/robots.txt'); + $resp->assertSee("User-agent: *\nDisallow:"); + $resp->assertDontSee("Disallow: /"); // Check config overrides app-public setting config()->set('app.allow_robots', false); $this->setSettings(['app-public' => 'true']); - $this->visit('/robots.txt'); - - $this->seeText("User-agent: *\nDisallow: /"); + $this->get('/robots.txt')->assertSee("User-agent: *\nDisallow: /"); } public function test_public_view_then_login_redirects_to_previous_content() { $this->setSettings(['app-public' => 'true']); + /** @var Book $book */ $book = Book::query()->first(); - $this->visit($book->getUrl()) - ->see($book->name) - ->visit('/login') - ->type('admin@admin.com', '#email') - ->type('password', '#password') - ->press('Log In') - ->seePageUrlIs($book->getUrl()); + $resp = $this->get($book->getUrl()); + $resp->assertSee($book->name); + + $this->get('/login'); + $resp = $this->post('/login', ['email' => 'admin@admin.com', 'password' => 'password']); + $resp->assertRedirect($book->getUrl()); } public function test_access_hidden_content_then_login_redirects_to_intended_content() { $this->setSettings(['app-public' => 'true']); + /** @var Book $book */ $book = Book::query()->first(); $this->setEntityRestrictions($book); - try { - $this->visit($book->getUrl()); - } catch (\Exception $exception) {} + $resp = $this->get($book->getUrl()); + $resp->assertSee('Book not found'); - $this->see('Book not found') - ->dontSee($book->name) - ->visit('/login') - ->type('admin@admin.com', '#email') - ->type('password', '#password') - ->press('Log In') - ->seePageUrlIs($book->getUrl()) - ->see($book->name); + $this->get('/login'); + $resp = $this->post('/login', ['email' => 'admin@admin.com', 'password' => 'password']); + $resp->assertRedirect($book->getUrl()); + $this->followRedirects($resp)->assertSee($book->name); } } \ No newline at end of file diff --git a/tests/SharedTestHelpers.php b/tests/SharedTestHelpers.php index 02f7caae1..78c1f3b18 100644 --- a/tests/SharedTestHelpers.php +++ b/tests/SharedTestHelpers.php @@ -15,13 +15,11 @@ use BookStack\Auth\Permissions\PermissionService; use BookStack\Entities\Repos\PageRepo; use BookStack\Settings\SettingService; use BookStack\Uploads\HttpFetcher; -use Illuminate\Http\Response; use Illuminate\Support\Env; use Illuminate\Support\Facades\Log; use Mockery; use Monolog\Handler\TestHandler; use Monolog\Logger; -use Throwable; use Illuminate\Foundation\Testing\Assert as PHPUnit; trait SharedTestHelpers @@ -32,7 +30,6 @@ trait SharedTestHelpers /** * Set the current user context to be an admin. - * @return $this */ public function asAdmin() { @@ -41,19 +38,19 @@ trait SharedTestHelpers /** * Get the current admin user. - * @return mixed */ - public function getAdmin() { - if($this->admin === null) { + public function getAdmin(): User + { + if (is_null($this->admin)) { $adminRole = Role::getSystemRole('admin'); $this->admin = $adminRole->users->first(); } + return $this->admin; } /** * Set the current user context to be an editor. - * @return $this */ public function asEditor() { @@ -63,10 +60,10 @@ trait SharedTestHelpers /** * Get a editor user. - * @return mixed */ - protected function getEditor() { - if($this->editor === null) { + protected function getEditor(): User + { + if ($this->editor === null) { $editorRole = Role::getRole('editor'); $this->editor = $editorRole->users->first(); } @@ -87,10 +84,8 @@ trait SharedTestHelpers /** * Regenerate the permission for an entity. - * @param Entity $entity - * @throws Throwable */ - protected function regenEntityPermissions(Entity $entity) + protected function regenEntityPermissions(Entity $entity): void { $entity->rebuildPermissions(); $entity->load('jointPermissions'); @@ -98,40 +93,34 @@ trait SharedTestHelpers /** * Create and return a new bookshelf. - * @param array $input - * @return Bookshelf */ - public function newShelf($input = ['name' => 'test shelf', 'description' => 'My new test shelf']) { + public function newShelf(array $input = ['name' => 'test shelf', 'description' => 'My new test shelf']): Bookshelf + { return app(BookshelfRepo::class)->create($input, []); } /** * Create and return a new book. - * @param array $input - * @return Book */ - public function newBook($input = ['name' => 'test book', 'description' => 'My new test book']) { + public function newBook(array $input = ['name' => 'test book', 'description' => 'My new test book']): Book + { return app(BookRepo::class)->create($input); } /** * Create and return a new test chapter - * @param array $input - * @param Book $book - * @return Chapter */ - public function newChapter($input = ['name' => 'test chapter', 'description' => 'My new test chapter'], Book $book) { + public function newChapter(array $input = ['name' => 'test chapter', 'description' => 'My new test chapter'], Book $book): Chapter + { return app(ChapterRepo::class)->create($input, $book); } /** * Create and return a new test page - * @param array $input - * @return Page - * @throws Throwable */ - public function newPage($input = ['name' => 'test page', 'html' => 'My new test page']) { - $book = Book::first(); + public function newPage(array $input = ['name' => 'test page', 'html' => 'My new test page']): Page + { + $book = Book::query()->first(); $pageRepo = app(PageRepo::class); $draftPage = $pageRepo->getNewDraftPage($book); return $pageRepo->publishDraft($draftPage, $input); @@ -139,9 +128,8 @@ trait SharedTestHelpers /** * Quickly sets an array of settings. - * @param $settingsArray */ - protected function setSettings($settingsArray) + protected function setSettings(array $settingsArray): void { $settings = app(SettingService::class); foreach ($settingsArray as $key => $value) { @@ -151,11 +139,8 @@ trait SharedTestHelpers /** * Manually set some permissions on an entity. - * @param Entity $entity - * @param array $actions - * @param array $roles */ - protected function setEntityRestrictions(Entity $entity, $actions = [], $roles = []) + protected function setEntityRestrictions(Entity $entity, array $actions = [], array $roles = []): void { $entity->restricted = true; $entity->permissions()->delete(); @@ -180,7 +165,7 @@ trait SharedTestHelpers /** * Give the given user some permissions. */ - protected function giveUserPermissions(User $user, array $permissions = []) + protected function giveUserPermissions(User $user, array $permissions = []): void { $newRole = $this->createNewRole($permissions); $user->attachRole($newRole); @@ -190,10 +175,8 @@ trait SharedTestHelpers /** * Create a new basic role for testing purposes. - * @param array $permissions - * @return Role */ - protected function createNewRole($permissions = []) + protected function createNewRole(array $permissions = []): Role { $permissionRepo = app(PermissionsRepo::class); $roleData = factory(Role::class)->make()->toArray(); @@ -203,8 +186,6 @@ trait SharedTestHelpers /** * Mock the HttpFetcher service and return the given data on fetch. - * @param $returnData - * @param int $times */ protected function mockHttpFetch($returnData, int $times = 1) { @@ -218,9 +199,6 @@ trait SharedTestHelpers /** * Run a set test with the given env variable. * Remembers the original and resets the value after test. - * @param string $name - * @param $value - * @param callable $callback */ protected function runWithEnv(string $name, $value, callable $callback) { @@ -246,11 +224,8 @@ trait SharedTestHelpers /** * Check the keys and properties in the given map to include * exist, albeit not exclusively, within the map to check. - * @param array $mapToInclude - * @param array $mapToCheck - * @param string $message */ - protected function assertArrayMapIncludes(array $mapToInclude, array $mapToCheck, string $message = '') : void + protected function assertArrayMapIncludes(array $mapToInclude, array $mapToCheck, string $message = ''): void { $passed = true; @@ -301,7 +276,7 @@ trait SharedTestHelpers $testHandler = new TestHandler(); $monolog->pushHandler($testHandler); - Log::extend('testing', function() use ($monolog) { + Log::extend('testing', function () use ($monolog) { return $monolog; }); Log::setDefaultDriver('testing');