Started updating RolesTest away from Browserkit

This commit is contained in:
Dan Brown 2021-09-18 00:33:03 +01:00
parent a4d9bca9e1
commit d74255df5d
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
2 changed files with 231 additions and 228 deletions

View File

@ -7,10 +7,11 @@ use BookStack\Traits\HasCreatorAndUpdater;
use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Database\Eloquent\Relations\MorphTo;
/** /**
* @property string text * @property int $id
* @property string html * @property string $text
* @property int|null parent_id * @property string $html
* @property int local_id * @property int|null $parent_id
* @property int $local_id
*/ */
class Comment extends Model class Comment extends Model
{ {

View File

@ -10,10 +10,10 @@ use BookStack\Entities\Models\Bookshelf;
use BookStack\Entities\Models\Chapter; use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Page; use BookStack\Entities\Models\Page;
use BookStack\Uploads\Image; use BookStack\Uploads\Image;
use Laravel\BrowserKitTesting\HttpException; use Tests\TestCase;
use Tests\BrowserKitTest; use Tests\TestResponse;
class RolesTest extends BrowserKitTest class RolesTest extends TestCase
{ {
protected $user; protected $user;
@ -25,17 +25,17 @@ class RolesTest extends BrowserKitTest
public function test_admin_can_see_settings() public function test_admin_can_see_settings()
{ {
$this->asAdmin()->visit('/settings')->see('Settings'); $this->asAdmin()->get('/settings')->assertSee('Settings');
} }
public function test_cannot_delete_admin_role() public function test_cannot_delete_admin_role()
{ {
$adminRole = Role::getRole('admin'); $adminRole = Role::getRole('admin');
$deletePageUrl = '/settings/roles/delete/' . $adminRole->id; $deletePageUrl = '/settings/roles/delete/' . $adminRole->id;
$this->asAdmin()->visit($deletePageUrl)
->press('Confirm') $this->asAdmin()->get($deletePageUrl);
->seePageIs($deletePageUrl) $this->delete($deletePageUrl)->assertRedirect($deletePageUrl);
->see('cannot be deleted'); $this->get($deletePageUrl)->assertSee('cannot be deleted');
} }
public function test_role_cannot_be_deleted_if_default() public function test_role_cannot_be_deleted_if_default()
@ -44,10 +44,9 @@ class RolesTest extends BrowserKitTest
$this->setSettings(['registration-role' => $newRole->id]); $this->setSettings(['registration-role' => $newRole->id]);
$deletePageUrl = '/settings/roles/delete/' . $newRole->id; $deletePageUrl = '/settings/roles/delete/' . $newRole->id;
$this->asAdmin()->visit($deletePageUrl) $this->asAdmin()->get($deletePageUrl);
->press('Confirm') $this->delete($deletePageUrl)->assertRedirect($deletePageUrl);
->seePageIs($deletePageUrl) $this->get($deletePageUrl)->assertSee('cannot be deleted');
->see('cannot be deleted');
} }
public function test_role_create_update_delete_flow() public function test_role_create_update_delete_flow()
@ -57,35 +56,37 @@ class RolesTest extends BrowserKitTest
$testRoleUpdateName = 'An Super Updated role'; $testRoleUpdateName = 'An Super Updated role';
// Creation // Creation
$this->asAdmin()->visit('/settings') $this->asAdmin()->get('/settings')
->click('Roles') ->click('Roles')
->seePageIs('/settings/roles') ->seePageIs('/settings/roles')
->click('Create New Role') ->click('Create New Role')
->type('Test Role', 'display_name') ->type('Test Role', 'display_name')
->type('A little test description', 'description') ->type('A little test description', 'description')
->press('Save Role') ->press('Save Role')
->seeInDatabase('roles', ['display_name' => $testRoleName, 'description' => $testRoleDesc, 'mfa_enforced' => false]) ->assertDatabaseHas('roles', ['display_name' => $testRoleName, 'description' => $testRoleDesc, 'mfa_enforced' => false])
->seePageIs('/settings/roles'); ->seePageIs('/settings/roles');
// Updating // Updating
$this->asAdmin()->visit('/settings/roles') $this->asAdmin()->get('/settings/roles')
->see($testRoleDesc) ->assertSee($testRoleDesc)
->click($testRoleName) ->click($testRoleName)
->type($testRoleUpdateName, '#display_name') ->type($testRoleUpdateName, '#display_name')
->check('#mfa_enforced') ->check('#mfa_enforced')
->press('Save Role') ->press('Save Role')
->seeInDatabase('roles', ['display_name' => $testRoleUpdateName, 'description' => $testRoleDesc, 'mfa_enforced' => true]) ->assertDatabaseHas('roles', ['display_name' => $testRoleUpdateName, 'description' => $testRoleDesc, 'mfa_enforced' => true])
->seePageIs('/settings/roles'); ->seePageIs('/settings/roles');
// Deleting // Deleting
$this->asAdmin()->visit('/settings/roles') $this->asAdmin()->get('/settings/roles')
->click($testRoleUpdateName) ->click($testRoleUpdateName)
->click('Delete Role') ->click('Delete Role')
->see($testRoleUpdateName) ->assertSee($testRoleUpdateName)
->press('Confirm') ->press('Confirm')
->seePageIs('/settings/roles') ->seePageIs('/settings/roles')
->dontSee($testRoleUpdateName); ->assertDontSee($testRoleUpdateName);
} }
public function test_admin_role_cannot_be_removed_if_last_admin() public function test_admin_role_cannot_be_removed_if_user_last_admin()
{ {
$adminRole = Role::where('system_name', '=', 'admin')->first(); $adminRole = Role::where('system_name', '=', 'admin')->first();
$adminUser = $this->getAdmin(); $adminUser = $this->getAdmin();
@ -104,7 +105,7 @@ class RolesTest extends BrowserKitTest
])->followRedirects(); ])->followRedirects();
$this->seePageIs($editUrl); $this->seePageIs($editUrl);
$this->see('This user is the only user assigned to the administrator role'); $this->assertSee('This user is the only user assigned to the administrator role');
} }
public function test_migrate_users_on_delete_works() public function test_migrate_users_on_delete_works()
@ -117,7 +118,7 @@ class RolesTest extends BrowserKitTest
$this->assertCount(1, $roleB->users()->get()); $this->assertCount(1, $roleB->users()->get());
$deletePage = $this->asAdmin()->get("/settings/roles/delete/{$roleB->id}"); $deletePage = $this->asAdmin()->get("/settings/roles/delete/{$roleB->id}");
$deletePage->seeElement('select[name=migrate_role_id]'); $deletePage->assertElementExists('select[name=migrate_role_id]');
$this->asAdmin()->delete("/settings/roles/delete/{$roleB->id}", [ $this->asAdmin()->delete("/settings/roles/delete/{$roleB->id}", [
'migrate_role_id' => $roleA->id, 'migrate_role_id' => $roleA->id,
]); ]);
@ -128,21 +129,21 @@ class RolesTest extends BrowserKitTest
public function test_manage_user_permission() public function test_manage_user_permission()
{ {
$this->actingAs($this->user)->visit('/settings/users') $this->actingAs($this->user)->get('/settings/users')
->seePageIs('/'); ->seePageIs('/');
$this->giveUserPermissions($this->user, ['users-manage']); $this->giveUserPermissions($this->user, ['users-manage']);
$this->actingAs($this->user)->visit('/settings/users') $this->actingAs($this->user)->get('/settings/users')
->seePageIs('/settings/users'); ->seePageIs('/settings/users');
} }
public function test_manage_users_permission_shows_link_in_header_if_does_not_have_settings_manage_permision() public function test_manage_users_permission_shows_link_in_header_if_does_not_have_settings_manage_permision()
{ {
$usersLink = 'href="' . url('/settings/users') . '"'; $usersLink = 'href="' . url('/settings/users') . '"';
$this->actingAs($this->user)->visit('/')->dontSee($usersLink); $this->actingAs($this->user)->get('/')->assertDontSee($usersLink);
$this->giveUserPermissions($this->user, ['users-manage']); $this->giveUserPermissions($this->user, ['users-manage']);
$this->actingAs($this->user)->visit('/')->see($usersLink); $this->actingAs($this->user)->get('/')->assertSee($usersLink);
$this->giveUserPermissions($this->user, ['settings-manage', 'users-manage']); $this->giveUserPermissions($this->user, ['settings-manage', 'users-manage']);
$this->actingAs($this->user)->visit('/')->dontSee($usersLink); $this->actingAs($this->user)->get('/')->assertDontSee($usersLink);
} }
public function test_user_cannot_change_email_unless_they_have_manage_users_permission() public function test_user_cannot_change_email_unless_they_have_manage_users_permission()
@ -151,14 +152,14 @@ class RolesTest extends BrowserKitTest
$originalEmail = $this->user->email; $originalEmail = $this->user->email;
$this->actingAs($this->user); $this->actingAs($this->user);
$this->visit($userProfileUrl) $this->get($userProfileUrl)
->assertResponseOk() ->assertOk()
->seeElement('input[name=email][disabled]'); ->assertElementExists('input[name=email][disabled]');
$this->put($userProfileUrl, [ $this->put($userProfileUrl, [
'name' => 'my_new_name', 'name' => 'my_new_name',
'email' => 'new_email@example.com', 'email' => 'new_email@example.com',
]); ]);
$this->seeInDatabase('users', [ $this->assertDatabaseHas('users', [
'id' => $this->user->id, 'id' => $this->user->id,
'email' => $originalEmail, 'email' => $originalEmail,
'name' => 'my_new_name', 'name' => 'my_new_name',
@ -166,16 +167,16 @@ class RolesTest extends BrowserKitTest
$this->giveUserPermissions($this->user, ['users-manage']); $this->giveUserPermissions($this->user, ['users-manage']);
$this->visit($userProfileUrl) $this->get($userProfileUrl)
->assertResponseOk() ->assertOk()
->dontSeeElement('input[name=email][disabled]') ->assertElementNotExists('input[name=email][disabled]')
->seeElement('input[name=email]'); ->assertElementExists('input[name=email]');
$this->put($userProfileUrl, [ $this->put($userProfileUrl, [
'name' => 'my_new_name_2', 'name' => 'my_new_name_2',
'email' => 'new_email@example.com', 'email' => 'new_email@example.com',
]); ]);
$this->seeInDatabase('users', [ $this->assertDatabaseHas('users', [
'id' => $this->user->id, 'id' => $this->user->id,
'email' => 'new_email@example.com', 'email' => 'new_email@example.com',
'name' => 'my_new_name_2', 'name' => 'my_new_name_2',
@ -184,40 +185,41 @@ class RolesTest extends BrowserKitTest
public function test_user_roles_manage_permission() public function test_user_roles_manage_permission()
{ {
$this->actingAs($this->user)->visit('/settings/roles') $this->actingAs($this->user)->get('/settings/roles')
->seePageIs('/')->visit('/settings/roles/1')->seePageIs('/'); ->seePageIs('/')->get('/settings/roles/1')->seePageIs('/');
$this->giveUserPermissions($this->user, ['user-roles-manage']); $this->giveUserPermissions($this->user, ['user-roles-manage']);
$this->actingAs($this->user)->visit('/settings/roles') $this->actingAs($this->user)->get('/settings/roles')
->seePageIs('/settings/roles')->click('Admin') ->seePageIs('/settings/roles')->click('Admin')
->see('Edit Role'); ->assertSee('Edit Role');
} }
public function test_settings_manage_permission() public function test_settings_manage_permission()
{ {
$this->actingAs($this->user)->visit('/settings') $this->actingAs($this->user)->get('/settings')
->seePageIs('/'); ->seePageIs('/');
$this->giveUserPermissions($this->user, ['settings-manage']); $this->giveUserPermissions($this->user, ['settings-manage']);
$this->actingAs($this->user)->visit('/settings') $this->actingAs($this->user)->get('/settings')
->seePageIs('/settings')->press('Save Settings')->see('Settings Saved'); ->seePageIs('/settings')->press('Save Settings')->assertSee('Settings Saved');
} }
public function test_restrictions_manage_all_permission() public function test_restrictions_manage_all_permission()
{ {
$page = Page::take(1)->get()->first(); $page = Page::take(1)->get()->first();
$this->actingAs($this->user)->visit($page->getUrl()) $this->actingAs($this->user)->get($page->getUrl())
->dontSee('Permissions') ->assertDontSee('Permissions')
->visit($page->getUrl() . '/permissions') ->get($page->getUrl() . '/permissions')
->seePageIs('/'); ->seePageIs('/');
$this->giveUserPermissions($this->user, ['restrictions-manage-all']); $this->giveUserPermissions($this->user, ['restrictions-manage-all']);
$this->actingAs($this->user)->visit($page->getUrl()) $this->actingAs($this->user)->get($page->getUrl())
->see('Permissions') ->assertSee('Permissions')
->click('Permissions') ->click('Permissions')
->see('Page Permissions')->seePageIs($page->getUrl() . '/permissions'); ->assertSee('Page Permissions')->seePageIs($page->getUrl() . '/permissions');
} }
public function test_restrictions_manage_own_permission() public function test_restrictions_manage_own_permission()
{ {
$otherUsersPage = Page::first(); /** @var Page $otherUsersPage */
$otherUsersPage = Page::query()->first();
$content = $this->createEntityChainBelongingToUser($this->user); $content = $this->createEntityChainBelongingToUser($this->user);
// Set a different creator on the page we're checking to ensure // Set a different creator on the page we're checking to ensure
@ -228,26 +230,26 @@ class RolesTest extends BrowserKitTest
$page->save(); $page->save();
// Check can't restrict other's content // Check can't restrict other's content
$this->actingAs($this->user)->visit($otherUsersPage->getUrl()) $this->actingAs($this->user)->get($otherUsersPage->getUrl())
->dontSee('Permissions') ->assertDontSee('Permissions')
->visit($otherUsersPage->getUrl() . '/permissions') ->get($otherUsersPage->getUrl() . '/permissions')
->seePageIs('/'); ->seePageIs('/');
// Check can't restrict own content // Check can't restrict own content
$this->actingAs($this->user)->visit($page->getUrl()) $this->actingAs($this->user)->get($page->getUrl())
->dontSee('Permissions') ->assertDontSee('Permissions')
->visit($page->getUrl() . '/permissions') ->get($page->getUrl() . '/permissions')
->seePageIs('/'); ->seePageIs('/');
$this->giveUserPermissions($this->user, ['restrictions-manage-own']); $this->giveUserPermissions($this->user, ['restrictions-manage-own']);
// Check can't restrict other's content // Check can't restrict other's content
$this->actingAs($this->user)->visit($otherUsersPage->getUrl()) $this->actingAs($this->user)->get($otherUsersPage->getUrl())
->dontSee('Permissions') ->assertDontSee('Permissions')
->visit($otherUsersPage->getUrl() . '/permissions') ->get($otherUsersPage->getUrl() . '/permissions')
->seePageIs('/'); ->seePageIs('/');
// Check can restrict own content // Check can restrict own content
$this->actingAs($this->user)->visit($page->getUrl()) $this->actingAs($this->user)->get($page->getUrl())
->see('Permissions') ->assertSee('Permissions')
->click('Permissions') ->click('Permissions')
->seePageIs($page->getUrl() . '/permissions'); ->seePageIs($page->getUrl() . '/permissions');
} }
@ -262,23 +264,23 @@ class RolesTest extends BrowserKitTest
private function checkAccessPermission($permission, $accessUrls = [], $visibles = []) private function checkAccessPermission($permission, $accessUrls = [], $visibles = [])
{ {
foreach ($accessUrls as $url) { foreach ($accessUrls as $url) {
$this->actingAs($this->user)->visit($url) $this->actingAs($this->user)->get($url)
->seePageIs('/'); ->seePageIs('/');
} }
foreach ($visibles as $url => $text) { foreach ($visibles as $url => $text) {
$this->actingAs($this->user)->visit($url) $this->actingAs($this->user)->get($url)
->dontSeeInElement('.action-buttons', $text); ->assertElementNotContains('.action-buttons', $text);
} }
$this->giveUserPermissions($this->user, [$permission]); $this->giveUserPermissions($this->user, [$permission]);
foreach ($accessUrls as $url) { foreach ($accessUrls as $url) {
$this->actingAs($this->user)->visit($url) $this->actingAs($this->user)->get($url)
->seePageIs($url); ->seePageIs($url);
} }
foreach ($visibles as $url => $text) { foreach ($visibles as $url => $text) {
$this->actingAs($this->user)->visit($url) $this->actingAs($this->user)->get($url)
->see($text); ->assertSee($text);
} }
} }
@ -290,7 +292,7 @@ class RolesTest extends BrowserKitTest
'/shelves' => 'New Shelf', '/shelves' => 'New Shelf',
]); ]);
$this->visit('/create-shelf') $this->get('/create-shelf')
->type('test shelf', 'name') ->type('test shelf', 'name')
->type('shelf desc', 'description') ->type('shelf desc', 'description')
->press('Save Shelf') ->press('Save Shelf')
@ -299,7 +301,8 @@ class RolesTest extends BrowserKitTest
public function test_bookshelves_edit_own_permission() public function test_bookshelves_edit_own_permission()
{ {
$otherShelf = Bookshelf::first(); /** @var Bookshelf $otherShelf */
$otherShelf = Bookshelf::query()->first();
$ownShelf = $this->newShelf(['name' => 'test-shelf', 'slug' => 'test-shelf']); $ownShelf = $this->newShelf(['name' => 'test-shelf', 'slug' => 'test-shelf']);
$ownShelf->forceFill(['owned_by' => $this->user->id, 'updated_by' => $this->user->id])->save(); $ownShelf->forceFill(['owned_by' => $this->user->id, 'updated_by' => $this->user->id])->save();
$this->regenEntityPermissions($ownShelf); $this->regenEntityPermissions($ownShelf);
@ -310,15 +313,16 @@ class RolesTest extends BrowserKitTest
$ownShelf->getUrl() => 'Edit', $ownShelf->getUrl() => 'Edit',
]); ]);
$this->visit($otherShelf->getUrl()) $this->get($otherShelf->getUrl())
->dontSeeInElement('.action-buttons', 'Edit') ->assertElementNotContains('.action-buttons', 'Edit')
->visit($otherShelf->getUrl('/edit')) ->get($otherShelf->getUrl('/edit'))
->seePageIs('/'); ->seePageIs('/');
} }
public function test_bookshelves_edit_all_permission() public function test_bookshelves_edit_all_permission()
{ {
$otherShelf = Bookshelf::first(); /** @var Bookshelf $otherShelf */
$otherShelf = Bookshelf::query()->first();
$this->checkAccessPermission('bookshelf-update-all', [ $this->checkAccessPermission('bookshelf-update-all', [
$otherShelf->getUrl('/edit'), $otherShelf->getUrl('/edit'),
], [ ], [
@ -329,7 +333,8 @@ class RolesTest extends BrowserKitTest
public function test_bookshelves_delete_own_permission() public function test_bookshelves_delete_own_permission()
{ {
$this->giveUserPermissions($this->user, ['bookshelf-update-all']); $this->giveUserPermissions($this->user, ['bookshelf-update-all']);
$otherShelf = Bookshelf::first(); /** @var Bookshelf $otherShelf */
$otherShelf = Bookshelf::query()->first();
$ownShelf = $this->newShelf(['name' => 'test-shelf', 'slug' => 'test-shelf']); $ownShelf = $this->newShelf(['name' => 'test-shelf', 'slug' => 'test-shelf']);
$ownShelf->forceFill(['owned_by' => $this->user->id, 'updated_by' => $this->user->id])->save(); $ownShelf->forceFill(['owned_by' => $this->user->id, 'updated_by' => $this->user->id])->save();
$this->regenEntityPermissions($ownShelf); $this->regenEntityPermissions($ownShelf);
@ -340,30 +345,31 @@ class RolesTest extends BrowserKitTest
$ownShelf->getUrl() => 'Delete', $ownShelf->getUrl() => 'Delete',
]); ]);
$this->visit($otherShelf->getUrl()) $this->get($otherShelf->getUrl())
->dontSeeInElement('.action-buttons', 'Delete') ->assertElementNotContains('.action-buttons', 'Delete')
->visit($otherShelf->getUrl('/delete')) ->get($otherShelf->getUrl('/delete'))
->seePageIs('/'); ->seePageIs('/');
$this->visit($ownShelf->getUrl())->visit($ownShelf->getUrl('/delete')) $this->get($ownShelf->getUrl())->get($ownShelf->getUrl('/delete'))
->press('Confirm') ->press('Confirm')
->seePageIs('/shelves') ->seePageIs('/shelves')
->dontSee($ownShelf->name); ->assertDontSee($ownShelf->name);
} }
public function test_bookshelves_delete_all_permission() public function test_bookshelves_delete_all_permission()
{ {
$this->giveUserPermissions($this->user, ['bookshelf-update-all']); $this->giveUserPermissions($this->user, ['bookshelf-update-all']);
$otherShelf = Bookshelf::first(); /** @var Bookshelf $otherShelf */
$otherShelf = Bookshelf::query()->first();
$this->checkAccessPermission('bookshelf-delete-all', [ $this->checkAccessPermission('bookshelf-delete-all', [
$otherShelf->getUrl('/delete'), $otherShelf->getUrl('/delete'),
], [ ], [
$otherShelf->getUrl() => 'Delete', $otherShelf->getUrl() => 'Delete',
]); ]);
$this->visit($otherShelf->getUrl())->visit($otherShelf->getUrl('/delete')) $this->get($otherShelf->getUrl())->get($otherShelf->getUrl('/delete'))
->press('Confirm') ->press('Confirm')
->seePageIs('/shelves') ->seePageIs('/shelves')
->dontSee($otherShelf->name); ->assertDontSee($otherShelf->name);
} }
public function test_books_create_all_permissions() public function test_books_create_all_permissions()
@ -374,7 +380,7 @@ class RolesTest extends BrowserKitTest
'/books' => 'Create New Book', '/books' => 'Create New Book',
]); ]);
$this->visit('/create-book') $this->get('/create-book')
->type('test book', 'name') ->type('test book', 'name')
->type('book desc', 'description') ->type('book desc', 'description')
->press('Save Book') ->press('Save Book')
@ -383,7 +389,8 @@ class RolesTest extends BrowserKitTest
public function test_books_edit_own_permission() public function test_books_edit_own_permission()
{ {
$otherBook = Book::take(1)->get()->first(); /** @var Book $otherBook */
$otherBook = Book::query()->take(1)->get()->first();
$ownBook = $this->createEntityChainBelongingToUser($this->user)['book']; $ownBook = $this->createEntityChainBelongingToUser($this->user)['book'];
$this->checkAccessPermission('book-update-own', [ $this->checkAccessPermission('book-update-own', [
$ownBook->getUrl() . '/edit', $ownBook->getUrl() . '/edit',
@ -391,15 +398,16 @@ class RolesTest extends BrowserKitTest
$ownBook->getUrl() => 'Edit', $ownBook->getUrl() => 'Edit',
]); ]);
$this->visit($otherBook->getUrl()) $this->get($otherBook->getUrl())
->dontSeeInElement('.action-buttons', 'Edit') ->assertElementNotContains('.action-buttons', 'Edit')
->visit($otherBook->getUrl() . '/edit') ->get($otherBook->getUrl() . '/edit')
->seePageIs('/'); ->seePageIs('/');
} }
public function test_books_edit_all_permission() public function test_books_edit_all_permission()
{ {
$otherBook = Book::take(1)->get()->first(); /** @var Book $otherBook */
$otherBook = Book::query()->take(1)->get()->first();
$this->checkAccessPermission('book-update-all', [ $this->checkAccessPermission('book-update-all', [
$otherBook->getUrl() . '/edit', $otherBook->getUrl() . '/edit',
], [ ], [
@ -410,7 +418,8 @@ class RolesTest extends BrowserKitTest
public function test_books_delete_own_permission() public function test_books_delete_own_permission()
{ {
$this->giveUserPermissions($this->user, ['book-update-all']); $this->giveUserPermissions($this->user, ['book-update-all']);
$otherBook = Book::take(1)->get()->first(); /** @var Book $otherBook */
$otherBook = Book::query()->take(1)->get()->first();
$ownBook = $this->createEntityChainBelongingToUser($this->user)['book']; $ownBook = $this->createEntityChainBelongingToUser($this->user)['book'];
$this->checkAccessPermission('book-delete-own', [ $this->checkAccessPermission('book-delete-own', [
$ownBook->getUrl() . '/delete', $ownBook->getUrl() . '/delete',
@ -418,35 +427,37 @@ class RolesTest extends BrowserKitTest
$ownBook->getUrl() => 'Delete', $ownBook->getUrl() => 'Delete',
]); ]);
$this->visit($otherBook->getUrl()) $this->get($otherBook->getUrl())
->dontSeeInElement('.action-buttons', 'Delete') ->assertElementNotContains('.action-buttons', 'Delete')
->visit($otherBook->getUrl() . '/delete') ->get($otherBook->getUrl() . '/delete')
->seePageIs('/'); ->seePageIs('/');
$this->visit($ownBook->getUrl())->visit($ownBook->getUrl() . '/delete') $this->get($ownBook->getUrl())->get($ownBook->getUrl() . '/delete')
->press('Confirm') ->press('Confirm')
->seePageIs('/books') ->seePageIs('/books')
->dontSee($ownBook->name); ->assertDontSee($ownBook->name);
} }
public function test_books_delete_all_permission() public function test_books_delete_all_permission()
{ {
$this->giveUserPermissions($this->user, ['book-update-all']); $this->giveUserPermissions($this->user, ['book-update-all']);
$otherBook = Book::take(1)->get()->first(); /** @var Book $otherBook */
$otherBook = Book::query()->take(1)->get()->first();
$this->checkAccessPermission('book-delete-all', [ $this->checkAccessPermission('book-delete-all', [
$otherBook->getUrl() . '/delete', $otherBook->getUrl() . '/delete',
], [ ], [
$otherBook->getUrl() => 'Delete', $otherBook->getUrl() => 'Delete',
]); ]);
$this->visit($otherBook->getUrl())->visit($otherBook->getUrl() . '/delete') $this->get($otherBook->getUrl())->get($otherBook->getUrl() . '/delete')
->press('Confirm') ->press('Confirm')
->seePageIs('/books') ->seePageIs('/books')
->dontSee($otherBook->name); ->assertDontSee($otherBook->name);
} }
public function test_chapter_create_own_permissions() public function test_chapter_create_own_permissions()
{ {
$book = Book::take(1)->get()->first(); /** @var Book $book */
$book = Book::query()->take(1)->get()->first();
$ownBook = $this->createEntityChainBelongingToUser($this->user)['book']; $ownBook = $this->createEntityChainBelongingToUser($this->user)['book'];
$this->checkAccessPermission('chapter-create-own', [ $this->checkAccessPermission('chapter-create-own', [
$ownBook->getUrl('/create-chapter'), $ownBook->getUrl('/create-chapter'),
@ -454,15 +465,15 @@ class RolesTest extends BrowserKitTest
$ownBook->getUrl() => 'New Chapter', $ownBook->getUrl() => 'New Chapter',
]); ]);
$this->visit($ownBook->getUrl('/create-chapter')) $this->get($ownBook->getUrl('/create-chapter'))
->type('test chapter', 'name') ->type('test chapter', 'name')
->type('chapter desc', 'description') ->type('chapter desc', 'description')
->press('Save Chapter') ->press('Save Chapter')
->seePageIs($ownBook->getUrl('/chapter/test-chapter')); ->seePageIs($ownBook->getUrl('/chapter/test-chapter'));
$this->visit($book->getUrl()) $this->get($book->getUrl())
->dontSeeInElement('.action-buttons', 'New Chapter') ->assertElementNotContains('.action-buttons', 'New Chapter')
->visit($book->getUrl('/create-chapter')) ->get($book->getUrl('/create-chapter'))
->seePageIs('/'); ->seePageIs('/');
} }
@ -475,7 +486,7 @@ class RolesTest extends BrowserKitTest
$book->getUrl() => 'New Chapter', $book->getUrl() => 'New Chapter',
]); ]);
$this->visit($book->getUrl('/create-chapter')) $this->get($book->getUrl('/create-chapter'))
->type('test chapter', 'name') ->type('test chapter', 'name')
->type('chapter desc', 'description') ->type('chapter desc', 'description')
->press('Save Chapter') ->press('Save Chapter')
@ -492,15 +503,16 @@ class RolesTest extends BrowserKitTest
$ownChapter->getUrl() => 'Edit', $ownChapter->getUrl() => 'Edit',
]); ]);
$this->visit($otherChapter->getUrl()) $this->get($otherChapter->getUrl())
->dontSeeInElement('.action-buttons', 'Edit') ->assertElementNotContains('.action-buttons', 'Edit')
->visit($otherChapter->getUrl() . '/edit') ->get($otherChapter->getUrl() . '/edit')
->seePageIs('/'); ->seePageIs('/');
} }
public function test_chapter_edit_all_permission() public function test_chapter_edit_all_permission()
{ {
$otherChapter = Chapter::take(1)->get()->first(); /** @var Chapter $otherChapter */
$otherChapter = Chapter::query()->take(1)->get()->first();
$this->checkAccessPermission('chapter-update-all', [ $this->checkAccessPermission('chapter-update-all', [
$otherChapter->getUrl() . '/edit', $otherChapter->getUrl() . '/edit',
], [ ], [
@ -511,7 +523,8 @@ class RolesTest extends BrowserKitTest
public function test_chapter_delete_own_permission() public function test_chapter_delete_own_permission()
{ {
$this->giveUserPermissions($this->user, ['chapter-update-all']); $this->giveUserPermissions($this->user, ['chapter-update-all']);
$otherChapter = Chapter::take(1)->get()->first(); /** @var Chapter $otherChapter */
$otherChapter = Chapter::query()->take(1)->get()->first();
$ownChapter = $this->createEntityChainBelongingToUser($this->user)['chapter']; $ownChapter = $this->createEntityChainBelongingToUser($this->user)['chapter'];
$this->checkAccessPermission('chapter-delete-own', [ $this->checkAccessPermission('chapter-delete-own', [
$ownChapter->getUrl() . '/delete', $ownChapter->getUrl() . '/delete',
@ -520,14 +533,14 @@ class RolesTest extends BrowserKitTest
]); ]);
$bookUrl = $ownChapter->book->getUrl(); $bookUrl = $ownChapter->book->getUrl();
$this->visit($otherChapter->getUrl()) $this->get($otherChapter->getUrl())
->dontSeeInElement('.action-buttons', 'Delete') ->assertElementNotContains('.action-buttons', 'Delete')
->visit($otherChapter->getUrl() . '/delete') ->get($otherChapter->getUrl() . '/delete')
->seePageIs('/'); ->seePageIs('/');
$this->visit($ownChapter->getUrl())->visit($ownChapter->getUrl() . '/delete') $this->get($ownChapter->getUrl())->get($ownChapter->getUrl() . '/delete')
->press('Confirm') ->press('Confirm')
->seePageIs($bookUrl) ->seePageIs($bookUrl)
->dontSeeInElement('.book-content', $ownChapter->name); ->assertElementNotContains('.book-content', $ownChapter->name);
} }
public function test_chapter_delete_all_permission() public function test_chapter_delete_all_permission()
@ -541,16 +554,18 @@ class RolesTest extends BrowserKitTest
]); ]);
$bookUrl = $otherChapter->book->getUrl(); $bookUrl = $otherChapter->book->getUrl();
$this->visit($otherChapter->getUrl())->visit($otherChapter->getUrl() . '/delete') $this->get($otherChapter->getUrl())->get($otherChapter->getUrl() . '/delete')
->press('Confirm') ->press('Confirm')
->seePageIs($bookUrl) ->seePageIs($bookUrl)
->dontSeeInElement('.book-content', $otherChapter->name); ->assertElementNotContains('.book-content', $otherChapter->name);
} }
public function test_page_create_own_permissions() public function test_page_create_own_permissions()
{ {
$book = Book::first(); /** @var Book $book */
$chapter = Chapter::first(); $book = Book::query()->first();
/** @var Chapter $chapter */
$chapter = Chapter::query()->first();
$entities = $this->createEntityChainBelongingToUser($this->user); $entities = $this->createEntityChainBelongingToUser($this->user);
$ownBook = $entities['book']; $ownBook = $entities['book'];
@ -561,7 +576,7 @@ class RolesTest extends BrowserKitTest
$accessUrls = [$createUrl, $createUrlChapter]; $accessUrls = [$createUrl, $createUrlChapter];
foreach ($accessUrls as $url) { foreach ($accessUrls as $url) {
$this->actingAs($this->user)->visit($url) $this->actingAs($this->user)->get($url)
->seePageIs('/'); ->seePageIs('/');
} }
@ -573,24 +588,24 @@ class RolesTest extends BrowserKitTest
$this->giveUserPermissions($this->user, ['page-create-own']); $this->giveUserPermissions($this->user, ['page-create-own']);
foreach ($accessUrls as $index => $url) { foreach ($accessUrls as $index => $url) {
$this->actingAs($this->user)->visit($url); $this->actingAs($this->user)->get($url);
$expectedUrl = Page::where('draft', '=', true)->orderBy('id', 'desc')->first()->getUrl(); $expectedUrl = Page::where('draft', '=', true)->orderBy('id', 'desc')->first()->getUrl();
$this->seePageIs($expectedUrl); $this->seePageIs($expectedUrl);
} }
$this->visit($createUrl) $this->get($createUrl)
->type('test page', 'name') ->type('test page', 'name')
->type('page desc', 'html') ->type('page desc', 'html')
->press('Save Page') ->press('Save Page')
->seePageIs($ownBook->getUrl('/page/test-page')); ->seePageIs($ownBook->getUrl('/page/test-page'));
$this->visit($book->getUrl()) $this->get($book->getUrl())
->dontSeeInElement('.action-buttons', 'New Page') ->assertElementNotContains('.action-buttons', 'New Page')
->visit($book->getUrl() . '/create-page') ->get($book->getUrl() . '/create-page')
->seePageIs('/'); ->seePageIs('/');
$this->visit($chapter->getUrl()) $this->get($chapter->getUrl())
->dontSeeInElement('.action-buttons', 'New Page') ->assertElementNotContains('.action-buttons', 'New Page')
->visit($chapter->getUrl() . '/create-page') ->get($chapter->getUrl() . '/create-page')
->seePageIs('/'); ->seePageIs('/');
} }
@ -598,14 +613,13 @@ class RolesTest extends BrowserKitTest
{ {
$book = Book::take(1)->get()->first(); $book = Book::take(1)->get()->first();
$chapter = Chapter::take(1)->get()->first(); $chapter = Chapter::take(1)->get()->first();
$baseUrl = $book->getUrl() . '/page';
$createUrl = $book->getUrl('/create-page'); $createUrl = $book->getUrl('/create-page');
$createUrlChapter = $chapter->getUrl('/create-page'); $createUrlChapter = $chapter->getUrl('/create-page');
$accessUrls = [$createUrl, $createUrlChapter]; $accessUrls = [$createUrl, $createUrlChapter];
foreach ($accessUrls as $url) { foreach ($accessUrls as $url) {
$this->actingAs($this->user)->visit($url) $this->actingAs($this->user)->get($url)
->seePageIs('/'); ->seePageIs('/');
} }
@ -617,18 +631,18 @@ class RolesTest extends BrowserKitTest
$this->giveUserPermissions($this->user, ['page-create-all']); $this->giveUserPermissions($this->user, ['page-create-all']);
foreach ($accessUrls as $index => $url) { foreach ($accessUrls as $index => $url) {
$this->actingAs($this->user)->visit($url); $this->actingAs($this->user)->get($url);
$expectedUrl = Page::where('draft', '=', true)->orderBy('id', 'desc')->first()->getUrl(); $expectedUrl = Page::where('draft', '=', true)->orderBy('id', 'desc')->first()->getUrl();
$this->seePageIs($expectedUrl); $this->seePageIs($expectedUrl);
} }
$this->visit($createUrl) $this->get($createUrl)
->type('test page', 'name') ->type('test page', 'name')
->type('page desc', 'html') ->type('page desc', 'html')
->press('Save Page') ->press('Save Page')
->seePageIs($book->getUrl('/page/test-page')); ->seePageIs($book->getUrl('/page/test-page'));
$this->visit($chapter->getUrl('/create-page')) $this->get($chapter->getUrl('/create-page'))
->type('new test page', 'name') ->type('new test page', 'name')
->type('page desc', 'html') ->type('page desc', 'html')
->press('Save Page') ->press('Save Page')
@ -645,9 +659,9 @@ class RolesTest extends BrowserKitTest
$ownPage->getUrl() => 'Edit', $ownPage->getUrl() => 'Edit',
]); ]);
$this->visit($otherPage->getUrl()) $this->get($otherPage->getUrl())
->dontSeeInElement('.action-buttons', 'Edit') ->assertElementNotContains('.action-buttons', 'Edit')
->visit($otherPage->getUrl() . '/edit') ->get($otherPage->getUrl() . '/edit')
->seePageIs('/'); ->seePageIs('/');
} }
@ -673,14 +687,14 @@ class RolesTest extends BrowserKitTest
]); ]);
$parent = $ownPage->chapter ?? $ownPage->book; $parent = $ownPage->chapter ?? $ownPage->book;
$this->visit($otherPage->getUrl()) $this->get($otherPage->getUrl())
->dontSeeInElement('.action-buttons', 'Delete') ->assertElementNotContains('.action-buttons', 'Delete')
->visit($otherPage->getUrl() . '/delete') ->get($otherPage->getUrl() . '/delete')
->seePageIs('/'); ->seePageIs('/');
$this->visit($ownPage->getUrl())->visit($ownPage->getUrl() . '/delete') $this->get($ownPage->getUrl())->get($ownPage->getUrl() . '/delete')
->press('Confirm') ->press('Confirm')
->seePageIs($parent->getUrl()) ->seePageIs($parent->getUrl())
->dontSeeInElement('.book-content', $ownPage->name); ->assertElementNotContains('.book-content', $ownPage->name);
} }
public function test_page_delete_all_permission() public function test_page_delete_all_permission()
@ -694,10 +708,10 @@ class RolesTest extends BrowserKitTest
]); ]);
$parent = $otherPage->chapter ?? $otherPage->book; $parent = $otherPage->chapter ?? $otherPage->book;
$this->visit($otherPage->getUrl())->visit($otherPage->getUrl() . '/delete') $this->get($otherPage->getUrl())->get($otherPage->getUrl() . '/delete')
->press('Confirm') ->press('Confirm')
->seePageIs($parent->getUrl()) ->seePageIs($parent->getUrl())
->dontSeeInElement('.book-content', $otherPage->name); ->assertElementNotContains('.book-content', $otherPage->name);
} }
public function test_public_role_visible_in_user_edit_screen() public function test_public_role_visible_in_user_edit_screen()
@ -705,34 +719,34 @@ class RolesTest extends BrowserKitTest
$user = User::first(); $user = User::first();
$adminRole = Role::getSystemRole('admin'); $adminRole = Role::getSystemRole('admin');
$publicRole = Role::getSystemRole('public'); $publicRole = Role::getSystemRole('public');
$this->asAdmin()->visit('/settings/users/' . $user->id) $this->asAdmin()->get('/settings/users/' . $user->id)
->seeElement('[name="roles[' . $adminRole->id . ']"]') ->assertElementExists('[name="roles[' . $adminRole->id . ']"]')
->seeElement('[name="roles[' . $publicRole->id . ']"]'); ->assertElementExists('[name="roles[' . $publicRole->id . ']"]');
} }
public function test_public_role_visible_in_role_listing() public function test_public_role_visible_in_role_listing()
{ {
$this->asAdmin()->visit('/settings/roles') $this->asAdmin()->get('/settings/roles')
->see('Admin') ->assertSee('Admin')
->see('Public'); ->assertSee('Public');
} }
public function test_public_role_visible_in_default_role_setting() public function test_public_role_visible_in_default_role_setting()
{ {
$this->asAdmin()->visit('/settings') $this->asAdmin()->get('/settings')
->seeElement('[data-system-role-name="admin"]') ->assertElementExists('[data-system-role-name="admin"]')
->seeElement('[data-system-role-name="public"]'); ->assertElementExists('[data-system-role-name="public"]');
} }
public function test_public_role_not_deleteable() public function test_public_role_not_deleteable()
{ {
$this->asAdmin()->visit('/settings/roles') $this->asAdmin()->get('/settings/roles')
->click('Public') ->click('Public')
->see('Edit Role') ->assertSee('Edit Role')
->click('Delete Role') ->click('Delete Role')
->press('Confirm') ->press('Confirm')
->see('Delete Role') ->assertSee('Delete Role')
->see('Cannot be deleted'); ->assertSee('Cannot be deleted');
} }
public function test_image_delete_own_permission() public function test_image_delete_own_permission()
@ -742,13 +756,13 @@ class RolesTest extends BrowserKitTest
$image = factory(Image::class)->create(['uploaded_to' => $page->id, 'created_by' => $this->user->id, 'updated_by' => $this->user->id]); $image = factory(Image::class)->create(['uploaded_to' => $page->id, 'created_by' => $this->user->id, 'updated_by' => $this->user->id]);
$this->actingAs($this->user)->json('delete', '/images/' . $image->id) $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
->seeStatusCode(403); ->assertStatus(403);
$this->giveUserPermissions($this->user, ['image-delete-own']); $this->giveUserPermissions($this->user, ['image-delete-own']);
$this->actingAs($this->user)->json('delete', '/images/' . $image->id) $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
->seeStatusCode(200) ->assertOk()
->dontSeeInDatabase('images', ['id' => $image->id]); ->assertDatabaseMissing('images', ['id' => $image->id]);
} }
public function test_image_delete_all_permission() public function test_image_delete_all_permission()
@ -759,36 +773,36 @@ class RolesTest extends BrowserKitTest
$image = factory(Image::class)->create(['uploaded_to' => $page->id, 'created_by' => $admin->id, 'updated_by' => $admin->id]); $image = factory(Image::class)->create(['uploaded_to' => $page->id, 'created_by' => $admin->id, 'updated_by' => $admin->id]);
$this->actingAs($this->user)->json('delete', '/images/' . $image->id) $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
->seeStatusCode(403); ->assertStatus(403);
$this->giveUserPermissions($this->user, ['image-delete-own']); $this->giveUserPermissions($this->user, ['image-delete-own']);
$this->actingAs($this->user)->json('delete', '/images/' . $image->id) $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
->seeStatusCode(403); ->assertStatus(403);
$this->giveUserPermissions($this->user, ['image-delete-all']); $this->giveUserPermissions($this->user, ['image-delete-all']);
$this->actingAs($this->user)->json('delete', '/images/' . $image->id) $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
->seeStatusCode(200) ->assertOk()
->dontSeeInDatabase('images', ['id' => $image->id]); ->assertDatabaseMissing('images', ['id' => $image->id]);
} }
public function test_role_permission_removal() public function test_role_permission_removal()
{ {
// To cover issue fixed in f99c8ff99aee9beb8c692f36d4b84dc6e651e50a. // To cover issue fixed in f99c8ff99aee9beb8c692f36d4b84dc6e651e50a.
$page = Page::first(); /** @var Page $page */
$page = Page::query()->first();
$viewerRole = Role::getRole('viewer'); $viewerRole = Role::getRole('viewer');
$viewer = $this->getViewer(); $viewer = $this->getViewer();
$this->actingAs($viewer)->visit($page->getUrl())->assertResponseStatus(200); $this->actingAs($viewer)->get($page->getUrl())->assertOk();
$this->asAdmin()->put('/settings/roles/' . $viewerRole->id, [ $this->asAdmin()->put('/settings/roles/' . $viewerRole->id, [
'display_name' => $viewerRole->display_name, 'display_name' => $viewerRole->display_name,
'description' => $viewerRole->description, 'description' => $viewerRole->description,
'permission' => [], 'permission' => [],
])->assertResponseStatus(302); ])->assertStatus(302);
$this->expectException(HttpException::class); $this->actingAs($viewer)->get($page->getUrl())->assertStatus(404);
$this->actingAs($viewer)->visit($page->getUrl())->assertResponseStatus(404);
} }
public function test_empty_state_actions_not_visible_without_permission() public function test_empty_state_actions_not_visible_without_permission()
@ -796,130 +810,118 @@ class RolesTest extends BrowserKitTest
$admin = $this->getAdmin(); $admin = $this->getAdmin();
// Book links // Book links
$book = factory(Book::class)->create(['created_by' => $admin->id, 'updated_by' => $admin->id]); $book = factory(Book::class)->create(['created_by' => $admin->id, 'updated_by' => $admin->id]);
$this->updateEntityPermissions($book); $this->regenEntityPermissions($book);
$this->actingAs($this->getViewer())->visit($book->getUrl()) $this->actingAs($this->getViewer())->get($book->getUrl())
->dontSee('Create a new page') ->assertDontSee('Create a new page')
->dontSee('Add a chapter'); ->assertDontSee('Add a chapter');
// Chapter links // Chapter links
$chapter = factory(Chapter::class)->create(['created_by' => $admin->id, 'updated_by' => $admin->id, 'book_id' => $book->id]); $chapter = factory(Chapter::class)->create(['created_by' => $admin->id, 'updated_by' => $admin->id, 'book_id' => $book->id]);
$this->updateEntityPermissions($chapter); $this->regenEntityPermissions($chapter);
$this->actingAs($this->getViewer())->visit($chapter->getUrl()) $this->actingAs($this->getViewer())->get($chapter->getUrl())
->dontSee('Create a new page') ->assertDontSee('Create a new page')
->dontSee('Sort the current book'); ->assertDontSee('Sort the current book');
} }
public function test_comment_create_permission() public function test_comment_create_permission()
{ {
$ownPage = $this->createEntityChainBelongingToUser($this->user)['page']; $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
$this->actingAs($this->user)->addComment($ownPage); $this->actingAs($this->user)
->addComment($ownPage)
$this->assertResponseStatus(403); ->assertStatus(403);
$this->giveUserPermissions($this->user, ['comment-create-all']); $this->giveUserPermissions($this->user, ['comment-create-all']);
$this->actingAs($this->user)->addComment($ownPage); $this->actingAs($this->user)
$this->assertResponseStatus(200); ->addComment($ownPage)
->assertOk();
} }
public function test_comment_update_own_permission() public function test_comment_update_own_permission()
{ {
$ownPage = $this->createEntityChainBelongingToUser($this->user)['page']; $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
$this->giveUserPermissions($this->user, ['comment-create-all']); $this->giveUserPermissions($this->user, ['comment-create-all']);
$commentId = $this->actingAs($this->user)->addComment($ownPage); $this->actingAs($this->user)->addComment($ownPage);
/** @var Comment $comment */
$comment = $ownPage->comments()->latest()->first();
// no comment-update-own // no comment-update-own
$this->actingAs($this->user)->updateComment($commentId); $this->actingAs($this->user)->updateComment($comment)->assertStatus(403);
$this->assertResponseStatus(403);
$this->giveUserPermissions($this->user, ['comment-update-own']); $this->giveUserPermissions($this->user, ['comment-update-own']);
// now has comment-update-own // now has comment-update-own
$this->actingAs($this->user)->updateComment($commentId); $this->actingAs($this->user)->updateComment($comment)->assertOk();
$this->assertResponseStatus(200);
} }
public function test_comment_update_all_permission() public function test_comment_update_all_permission()
{ {
/** @var Page $ownPage */
$ownPage = $this->createEntityChainBelongingToUser($this->user)['page']; $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
$commentId = $this->asAdmin()->addComment($ownPage); $this->asAdmin()->addComment($ownPage);
/** @var Comment $comment */
$comment = $ownPage->comments()->latest()->first();
// no comment-update-all // no comment-update-all
$this->actingAs($this->user)->updateComment($commentId); $this->actingAs($this->user)->updateComment($comment)->assertStatus(403);
$this->assertResponseStatus(403);
$this->giveUserPermissions($this->user, ['comment-update-all']); $this->giveUserPermissions($this->user, ['comment-update-all']);
// now has comment-update-all // now has comment-update-all
$this->actingAs($this->user)->updateComment($commentId); $this->actingAs($this->user)->updateComment($comment)->assertOk();
$this->assertResponseStatus(200);
} }
public function test_comment_delete_own_permission() public function test_comment_delete_own_permission()
{ {
/** @var Page $ownPage */
$ownPage = $this->createEntityChainBelongingToUser($this->user)['page']; $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
$this->giveUserPermissions($this->user, ['comment-create-all']); $this->giveUserPermissions($this->user, ['comment-create-all']);
$commentId = $this->actingAs($this->user)->addComment($ownPage); $this->actingAs($this->user)->addComment($ownPage);
/** @var Comment $comment */
$comment = $ownPage->comments()->latest()->first();
// no comment-delete-own // no comment-delete-own
$this->actingAs($this->user)->deleteComment($commentId); $this->actingAs($this->user)->deleteComment($comment)->assertStatus(403);
$this->assertResponseStatus(403);
$this->giveUserPermissions($this->user, ['comment-delete-own']); $this->giveUserPermissions($this->user, ['comment-delete-own']);
// now has comment-update-own // now has comment-update-own
$this->actingAs($this->user)->deleteComment($commentId); $this->actingAs($this->user)->deleteComment($comment)->assertOk();
$this->assertResponseStatus(200);
} }
public function test_comment_delete_all_permission() public function test_comment_delete_all_permission()
{ {
/** @var Page $ownPage */
$ownPage = $this->createEntityChainBelongingToUser($this->user)['page']; $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
$commentId = $this->asAdmin()->addComment($ownPage); $this->asAdmin()->addComment($ownPage);
/** @var Comment $comment */
$comment = $ownPage->comments()->latest()->first();
// no comment-delete-all // no comment-delete-all
$this->actingAs($this->user)->deleteComment($commentId); $this->actingAs($this->user)->deleteComment($comment)->assertStatus(403);
$this->assertResponseStatus(403);
$this->giveUserPermissions($this->user, ['comment-delete-all']); $this->giveUserPermissions($this->user, ['comment-delete-all']);
// now has comment-delete-all // now has comment-delete-all
$this->actingAs($this->user)->deleteComment($commentId); $this->actingAs($this->user)->deleteComment($comment)->assertOk();
$this->assertResponseStatus(200);
} }
private function addComment($page) private function addComment(Page $page): TestResponse
{ {
$comment = factory(Comment::class)->make(); $comment = factory(Comment::class)->make();
$url = "/comment/$page->id"; return $this->postJson("/comment/$page->id", $comment->only('text', 'html'));
$request = [
'text' => $comment->text,
'html' => $comment->html,
];
$this->postJson($url, $request);
$comment = $page->comments()->first();
return $comment === null ? null : $comment->id;
} }
private function updateComment($commentId) private function updateComment(Comment $comment): TestResponse
{ {
$comment = factory(Comment::class)->make(); $commentData = factory(Comment::class)->make();
$url = "/comment/$commentId"; return $this->putJson("/comment/{$comment->id}", $commentData->only('text', 'html'));
$request = [
'text' => $comment->text,
'html' => $comment->html,
];
return $this->putJson($url, $request);
} }
private function deleteComment($commentId) private function deleteComment(Comment $comment): TestResponse
{ {
$url = '/comment/' . $commentId; return $this->json('DELETE', '/comment/' . $comment->id);
return $this->json('DELETE', $url);
} }
} }