* @property int $image_id
* @property string $external_auth_id
* @property string $system_name
+ * @property Collection $roles
*/
class User extends Model implements AuthenticatableContract, CanResetPasswordContract, Loggable
{
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' => $email,
'send_invite' => 'true',
]);
+ $resp->assertRedirect('/settings/users');
- $newUser = User::query()->where('email', '=',
'[email protected]')->orderBy('id', 'desc')->first();
+ $newUser = User::query()->where('email', '=', $email)->orderBy('id', 'desc')->first();
Notification::assertSentTo($newUser, UserInvite::class);
$this->assertDatabaseHas('user_invites', [
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;
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
]);
$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
$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
]);
$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
$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
]);
$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()
->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);
->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'));
->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');
->visit($bookUrl)
->seePageIs($bookUrl);
- $this->setEntityRestrictions($book, []);
+ $this->setRestrictionsForTestRoles($book, []);
$this->forceVisit($bookUrl)
->see('Book not found');
$this->forceVisit($bookChapter->getUrl())
->see('Chapter not found');
- $this->setEntityRestrictions($book, ['view']);
+ $this->setRestrictionsForTestRoles($book, ['view']);
$this->visit($bookUrl)
->see($book->name);
->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('/');
$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')
->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('/');
$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');
->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('/');
$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');
->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);
->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')
->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');
->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');
->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);
->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);
->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');
$page = $chapter->pages->first();
$page2 = $chapter->pages[2];
- $this->setEntityRestrictions($page, []);
+ $this->setRestrictionsForTestRoles($page, []);
$this->actingAs($this->user)
->visit($page2->getUrl())
$chapter = Chapter::first();
$page = $chapter->pages->first();
- $this->setEntityRestrictions($page, []);
+ $this->setRestrictionsForTestRoles($page, []);
$this->actingAs($this->user)
->visit($chapter->getUrl())
$chapter = Chapter::first();
$page = $chapter->pages->first();
- $this->setEntityRestrictions($page, []);
+ $this->setRestrictionsForTestRoles($page, []);
$this->actingAs($this->user)
->visit($chapter->getUrl())
->see($chapter->pages->first()->name);
foreach ($chapter->pages as $page) {
- $this->setEntityRestrictions($page, []);
+ $this->setRestrictionsForTestRoles($page, []);
}
$this->actingAs($this->user)
->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'));
->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');
->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('/');
$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')
->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('/');
$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');
->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('/');
$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');
$entity->save();
}
- $this->setEntityRestrictions($book, []);
- $this->setEntityRestrictions($bookPage, ['view']);
+ $this->setRestrictionsForTestRoles($book, []);
+ $this->setRestrictionsForTestRoles($bookPage, ['view']);
$this->actingAs($this->viewer);
$this->get($bookPage->getUrl());
$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')
$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);
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')
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()
}
$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,
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('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' => '
[email protected]', '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) {}
-
- $this->see('Book not found')
- ->dontSee($book->name)
- ->visit('/login')
- ->type('password', '#password')
- ->press('Log In')
- ->seePageUrlIs($book->getUrl())
- ->see($book->name);
+ $resp = $this->get($book->getUrl());
+ $resp->assertSee('Book not found');
+
+ $this->get('/login');
+ $resp = $this->post('/login', ['email' => '
[email protected]', 'password' => 'password']);
+ $resp->assertRedirect($book->getUrl());
+ $this->followRedirects($resp)->assertSee($book->name);
}
}
\ No newline at end of file
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
/**
* Set the current user context to be an admin.
- * @return $this
*/
public function asAdmin()
{
/**
* 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()
{
/**
* 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();
}
/**
* 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');
/**
* 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);
/**
* 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) {
/**
* 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();
/**
* 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);
/**
* 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();
/**
* Mock the HttpFetcher service and return the given data on fetch.
- * @param $returnData
- * @param int $times
*/
protected function mockHttpFetch($returnData, int $times = 1)
{
/**
* 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)
{
/**
* 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;
$testHandler = new TestHandler();
$monolog->pushHandler($testHandler);
- Log::extend('testing', function() use ($monolog) {
+ Log::extend('testing', function () use ($monolog) {
return $monolog;
});
Log::setDefaultDriver('testing');