Commit d005153a authored by Viral Solani's avatar Viral Solani

remove old test cases

parent 0d3df30c
......@@ -30,14 +30,14 @@
"bvipul/generator": "v0.9.1",
"filp/whoops": "~2.0",
"fzaninotto/faker": "~1.4",
"laravel/browser-kit-testing": "^1.0",
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "~6.0",
"xethron/migrations-generator": "2.0.2"
},
"autoload": {
"classmap": [
"database"
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
......
This diff is collapsed.
<?php
use Tests\BrowserKitTestCase;
/**
* Class AccessHelperTest.
*/
class AccessHelperTest extends BrowserKitTestCase
{
public function testAccessUser()
{
$this->actingAs($this->user);
$this->assertEquals(auth()->user(), access()->user());
}
public function testAccessGuest()
{
$this->assertEquals(auth()->guest(), access()->guest());
}
public function testAccessLogout()
{
$this->actingAs($this->user);
$this->assertFalse(access()->guest());
access()->logout();
$this->assertTrue(access()->guest());
}
public function testAccessGetsUserId()
{
$this->actingAs($this->user);
$this->assertEquals(auth()->id(), access()->id());
}
public function testAccessLogsUserInById()
{
access()->loginUsingId(3);
$this->assertEquals(auth()->user(), access()->user());
}
public function testAccessHasRole()
{
$this->actingAs($this->executive);
$this->assertTrue(access()->hasRole('Executive'));
$this->assertFalse(access()->hasRole('Administrator'));
$this->assertTrue(access()->hasRole(2));
$this->assertFalse(access()->hasRole(1));
}
public function testAdminHasAllRoles()
{
$this->actingAs($this->admin);
$this->assertTrue(access()->hasRole('Administrator'));
$this->assertTrue(access()->hasRole('Non-Existing Role'));
$this->assertTrue(access()->hasRole(1));
$this->assertTrue(access()->hasRole(123));
}
public function testAccessHasRoles()
{
$this->actingAs($this->executive);
$this->assertTrue(access()->hasRoles(['Executive', 'User']));
$this->assertTrue(access()->hasRoles([2, 3]));
}
public function testAccessHasRolesNeedsAll()
{
$this->actingAs($this->executive);
$this->assertFalse(access()->hasRoles(['Executive', 'User'], true));
$this->assertFalse(access()->hasRoles([2, 3], true));
}
public function testAccessAllow()
{
$this->actingAs($this->executive);
$this->assertTrue(access()->allow('view-backend'));
$this->assertTrue(access()->allow(1));
}
public function testAdminHasAllAccess()
{
$this->actingAs($this->admin);
$this->assertTrue(access()->allow('view-backend'));
$this->assertTrue(access()->allow('view-something-that-doesnt-exist'));
$this->assertTrue(access()->allow(1));
$this->assertTrue(access()->allow(123));
}
public function testAccessAllowMultiple()
{
$this->actingAs($this->executive);
$this->assertTrue(access()->allowMultiple(['view-backend']));
$this->assertTrue(access()->allowMultiple([1]));
}
public function testAccessAllowMultipleNeedsAll()
{
$this->actingAs($this->executive);
$this->assertTrue(access()->allowMultiple(['view-backend'], true));
$this->assertTrue(access()->allowMultiple([1], true));
}
public function testAccessHasPermission()
{
$this->actingAs($this->executive);
$this->assertTrue(access()->hasPermission('view-backend'));
$this->assertTrue(access()->hasPermission(1));
}
public function testAccessHasPermissions()
{
$this->actingAs($this->executive);
$this->assertTrue(access()->hasPermissions(['view-backend']));
$this->assertTrue(access()->hasPermissions([1]));
}
public function testAccessHasPermissionsNeedsAll()
{
$this->actingAs($this->executive);
$this->assertTrue(access()->hasPermissions(['view-backend'], true));
$this->assertTrue(access()->hasPermissions([1], true));
}
}
<?php
use Tests\BrowserKitTestCase;
/**
* Class AccessRepositoryTest.
*/
class AccessRepositoryTest extends BrowserKitTestCase
{
public function testGetUsersByPermissionUsingName()
{
$results = app()->make(\App\Repositories\Backend\Access\User\UserRepository::class)
->getByPermission('view-backend')
->toArray();
$this->assertCount(1, $results);
$this->assertArraySubset(['name' => $this->executive->name], $results[0]);
}
public function testGetUsersByPermissionsUsingNames()
{
$this->userRole->permissions()->sync([1]);
$results = app()->make(\App\Repositories\Backend\Access\User\UserRepository::class)
->getByPermission(['view-backend'])
->toArray();
$this->assertCount(2, $results);
$this->assertArraySubset(['name' => $this->executive->name], $results[0]);
$this->assertArraySubset(['name' => $this->user->name], $results[1]);
}
public function testGetUsersByPermissionUsingId()
{
$results = app()->make(\App\Repositories\Backend\Access\User\UserRepository::class)
->getByPermission(1, 'id')
->toArray();
$this->assertCount(1, $results);
$this->assertArraySubset(['name' => $this->executive->name], $results[0]);
}
public function testGetUsersByPermissionsUsingIds()
{
$this->userRole->permissions()->sync([1]);
$results = app()->make(\App\Repositories\Backend\Access\User\UserRepository::class)
->getByPermission([1], 'id')
->toArray();
$this->assertCount(2, $results);
$this->assertArraySubset(['name' => $this->executive->name], $results[0]);
$this->assertArraySubset(['name' => $this->user->name], $results[1]);
}
public function testGetUsersByRoleUsingName()
{
$results = app()->make(\App\Repositories\Backend\Access\User\UserRepository::class)
->getByRole('User')
->toArray();
$this->assertCount(1, $results);
$this->assertArraySubset(['name' => $this->user->name], $results[0]);
}
public function testGetUsersByRolesUsingNames()
{
$results = app()->make(\App\Repositories\Backend\Access\User\UserRepository::class)
->getByRole(['User', 'Executive'])
->toArray();
$this->assertCount(2, $results);
$this->assertArraySubset(['name' => $this->executive->name], $results[0]);
$this->assertArraySubset(['name' => $this->user->name], $results[1]);
}
public function testGetUsersByRoleUsingId()
{
$results = app()->make(\App\Repositories\Backend\Access\User\UserRepository::class)
->getByRole(1, 'id')
->toArray();
$this->assertCount(1, $results);
$this->assertArraySubset(['name' => $this->admin->name], $results[0]);
}
public function testGetUsersByRolesUsingIds()
{
$results = app()->make(\App\Repositories\Backend\Access\User\UserRepository::class)
->getByRole([1, 3], 'id')
->toArray();
$this->assertCount(2, $results);
$this->assertArraySubset(['name' => $this->admin->name], $results[0]);
$this->assertArraySubset(['name' => $this->user->name], $results[1]);
}
}
<?php
use App\Models\Access\Permission\Permission;
use Tests\BrowserKitTestCase;
class RolePermissionTest extends BrowserKitTestCase
{
public function testSavePermissionsToRole()
{
$this->notSeeInDatabase(config('access.permission_role_table'), ['permission_id' => 1, 'role_id' => $this->userRole->id]);
$this->userRole->permissions()->sync([1]);
$this->seeInDatabase(config('access.permission_role_table'), ['permission_id' => 1, 'role_id' => $this->userRole->id]);
}
public function testEmptyPermissionsFromRole()
{
$this->notSeeInDatabase(config('access.permission_role_table'), ['permission_id' => 1, 'role_id' => $this->userRole->id]);
$this->userRole->permissions()->sync([1]);
$this->seeInDatabase(config('access.permission_role_table'), ['permission_id' => 1, 'role_id' => $this->userRole->id]);
$this->userRole->permissions()->sync([]);
$this->notSeeInDatabase(config('access.permission_role_table'), ['permission_id' => 1, 'role_id' => $this->userRole->id]);
}
public function testAttachPermissionToRoleById()
{
$this->notSeeInDatabase(config('access.permission_role_table'), ['permission_id' => 1, 'role_id' => $this->userRole->id]);
$this->userRole->attachPermission(1);
$this->seeInDatabase(config('access.permission_role_table'), ['permission_id' => 1, 'role_id' => $this->userRole->id]);
}
public function testAttachPermissionToRoleByObject()
{
$this->notSeeInDatabase(config('access.permission_role_table'), ['permission_id' => 1, 'role_id' => $this->userRole->id]);
$this->userRole->attachPermission(Permission::findOrFail(1));
$this->seeInDatabase(config('access.permission_role_table'), ['permission_id' => 1, 'role_id' => $this->userRole->id]);
}
public function testDetachPermissionFromRoleById()
{
$this->notSeeInDatabase(config('access.permission_role_table'), ['permission_id' => 1, 'role_id' => $this->userRole->id]);
$this->userRole->attachPermission(1);
$this->seeInDatabase(config('access.permission_role_table'), ['permission_id' => 1, 'role_id' => $this->userRole->id]);
$this->userRole->detachPermission(1);
$this->notSeeInDatabase(config('access.permission_role_table'), ['permission_id' => 1, 'role_id' => $this->userRole->id]);
}
public function testDetachPermissionFromRoleByObject()
{
$this->notSeeInDatabase(config('access.permission_role_table'), ['permission_id' => 1, 'role_id' => $this->userRole->id]);
$this->userRole->attachPermission(Permission::findOrFail(1));
$this->seeInDatabase(config('access.permission_role_table'), ['permission_id' => 1, 'role_id' => $this->userRole->id]);
$this->userRole->detachPermission(Permission::findOrFail(1));
$this->notSeeInDatabase(config('access.permission_role_table'), ['permission_id' => 1, 'role_id' => $this->userRole->id]);
}
public function testAttachPermissionsToRoleById()
{
$this->notSeeInDatabase(config('access.permission_role_table'), ['permission_id' => 1, 'role_id' => $this->userRole->id]);
$this->userRole->attachPermissions([1]);
$this->seeInDatabase(config('access.permission_role_table'), ['permission_id' => 1, 'role_id' => $this->userRole->id]);
}
public function testAttachPermissionsToRoleByObject()
{
$this->notSeeInDatabase(config('access.permission_role_table'), ['permission_id' => 1, 'role_id' => $this->userRole->id]);
$this->userRole->attachPermissions([Permission::findOrFail(1)]);
$this->seeInDatabase(config('access.permission_role_table'), ['permission_id' => 1, 'role_id' => $this->userRole->id]);
}
public function testDetachPermissionsFromRoleById()
{
$this->notSeeInDatabase(config('access.permission_role_table'), ['permission_id' => 1, 'role_id' => $this->userRole->id]);
$this->userRole->attachPermissions([1]);
$this->seeInDatabase(config('access.permission_role_table'), ['permission_id' => 1, 'role_id' => $this->userRole->id]);
$this->userRole->detachPermissions([1]);
$this->notSeeInDatabase(config('access.permission_role_table'), ['permission_id' => 1, 'role_id' => $this->userRole->id]);
}
public function testDetachPermissionsFromRoleByObject()
{
$this->notSeeInDatabase(config('access.permission_role_table'), ['permission_id' => 1, 'role_id' => $this->userRole->id]);
$this->userRole->attachPermissions([Permission::findOrFail(1)]);
$this->seeInDatabase(config('access.permission_role_table'), ['permission_id' => 1, 'role_id' => $this->userRole->id]);
$this->userRole->detachPermissions([Permission::findOrFail(1)]);
$this->notSeeInDatabase(config('access.permission_role_table'), ['permission_id' => 1, 'role_id' => $this->userRole->id]);
}
}
<?php
use Tests\BrowserKitTestCase;
/**
* Class UserAccessTest.
*/
class UserAccessTest extends BrowserKitTestCase
{
public function testUserCantAccessAdminDashboard()
{
$this->visit('/')
->actingAs($this->user)
->visit('/admin/dashboard')
->seePageIs('/')
->see('You do not have access to do that.');
}
public function testExecutiveCanAccessAdminDashboard()
{
$this->visit('/')
->actingAs($this->executive)
->visit('/admin/dashboard')
->seePageIs('/admin/dashboard')
->see($this->executive->name);
}
public function testExecutiveCantAccessManageRoles()
{
$this->visit('/')
->actingAs($this->executive)
->visit('/admin/dashboard')
->seePageIs('/admin/dashboard')
->visit('/admin/access/role')
->seePageIs('/')
->see('You do not have access to do that.');
}
}
<?php
use Tests\BrowserKitTestCase;
/**
* Class UserRoleTest.
*/
class UserRoleTest extends BrowserKitTestCase
{
public function testAttachRoleToUserById()
{
$this->notSeeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->adminRole->id]);
$this->user->attachRole($this->adminRole->id);
$this->seeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->adminRole->id]);
}
public function testAttachRoleToUserByObject()
{
$this->notSeeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->adminRole->id]);
$this->user->attachRole($this->adminRole);
$this->seeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->adminRole->id]);
}
public function testDetachRoleByIdFromUser()
{
$this->notSeeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->adminRole->id]);
$this->user->attachRole($this->adminRole->id);
$this->seeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->adminRole->id]);
$this->user->detachRole($this->adminRole->id);
$this->notSeeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->adminRole->id]);
}
public function testDetachRoleByObjectFromUser()
{
$this->notSeeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->adminRole->id]);
$this->user->attachRole($this->adminRole);
$this->seeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->adminRole->id]);
$this->user->detachRole($this->adminRole);
$this->notSeeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->adminRole->id]);
}
public function testAttachRolesByIdToUser()
{
$this->notSeeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->adminRole->id]);
$this->notSeeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->executiveRole->id]);
$this->user->attachRoles([$this->adminRole->id, $this->executiveRole->id]);
$this->seeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->adminRole->id]);
$this->seeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->executiveRole->id]);
}
public function testAttachRolesByObjectToUser()
{
$this->notSeeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->adminRole->id]);
$this->notSeeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->executiveRole->id]);
$this->user->attachRoles([$this->adminRole, $this->executiveRole]);
$this->seeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->adminRole->id]);
$this->seeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->executiveRole->id]);
}
public function testDetachRolesByIdFromUser()
{
$this->notSeeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->adminRole->id]);
$this->notSeeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->executiveRole->id]);
$this->user->attachRoles([$this->adminRole->id, $this->executiveRole->id]);
$this->seeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->adminRole->id]);
$this->seeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->executiveRole->id]);
$this->user->detachRoles([$this->adminRole->id, $this->executiveRole->id]);
$this->notSeeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->adminRole->id]);
$this->notSeeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->executiveRole->id]);
}
public function testDetachRolesByObjectFromUser()
{
$this->notSeeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->adminRole->id]);
$this->notSeeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->executiveRole->id]);
$this->user->attachRoles([$this->adminRole, $this->executiveRole]);
$this->seeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->adminRole->id]);
$this->seeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->executiveRole->id]);
$this->user->detachRoles([$this->adminRole, $this->executiveRole]);
$this->notSeeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->adminRole->id]);
$this->notSeeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => $this->executiveRole->id]);
}
}
<?php
use App\Events\Backend\Access\Role\RoleCreated;
use App\Events\Backend\Access\Role\RoleDeleted;
use App\Events\Backend\Access\Role\RoleUpdated;
use App\Models\Access\Role\Role;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Tests\BrowserKitTestCase;
/**
* Class RoleFormTest.
*/
class RoleFormTest extends BrowserKitTestCase
{
public function testCreateRoleRequiredFieldsAll()
{
// All Permissions
$this->actingAs($this->admin)
->visit('/admin/access/role/create')
->type('', 'name')
->press('Create')
->seePageIs('/admin/access/role/create')
->see('The name field is required.');
}
public function testCreateRoleRequiredFieldsSpecificPermissions()
{
// Custom Permissions
$this->actingAs($this->admin)
->visit('/admin/access/role/create')
->type('Test Role', 'name')
->select('custom', 'associated-permissions')
->press('Create')
->seePageIs('/admin/access/role/create')
->see('You must select at least one permission for this role.');
}
public function testCreateRoleFormAll()
{
// Make sure our events are fired
Event::fake();
// Test create with all permissions
$this->actingAs($this->admin)
->visit('/admin/access/role/create')
->type('Test Role', 'name')
->type('999', 'sort')
->press('Create')
->seePageIs('/admin/access/role')
->see('The role was successfully created.')
->seeInDatabase(config('access.roles_table'), ['name' => 'Test Role', 'all' => 1, 'sort' => 999]);
Event::assertDispatched(RoleCreated::class);
}
public function testCreateRoleFormSpecificPermissions()
{
// Make sure our events are fired
Event::fake();
// Test create with some permissions
$this->actingAs($this->admin)
->visit('/admin/access/role/create')
->type('Test Role', 'name')
->select('custom', 'associated-permissions')
->check('permissions[1]')
->press('Create')
->seePageIs('/admin/access/role')
->see('The role was successfully created.')
->seeInDatabase(config('access.roles_table'), ['name' => 'Test Role', 'all' => 0])
->seeInDatabase(config('access.permission_role_table'), ['permission_id' => 1, 'role_id' => 4]);
Event::assertDispatched(RoleCreated::class);
}
public function testRoleAlreadyExists()
{
$this->actingAs($this->admin)
->visit('/admin/access/role/create')
->type('Administrator', 'name')
->press('Create')
->seePageIs('/admin/access/role/create')
->see('That role already exists. Please choose a different name.');
}
public function testRoleRequiresPermission()
{
if (config('access.roles.role_must_contain_permission')) {
$this->actingAs($this->admin)
->visit('/admin/access/role/create')
->type('Test Role', 'name')
->select('custom', 'associated-permissions')
->press('Create')
->seePageIs('/admin/access/role/create')
->see('You must select at least one permission for this role.');
}
}
public function testUpdateRoleRequiredFields()
{
$this->actingAs($this->admin)
->visit('/admin/access/role/1/edit')
->type('', 'name')
->press('Update')
->seePageIs('/admin/access/role/1/edit')
->see('The name field is required.');
}
public function testUpdateRoleFormAll()
{
// Make sure our events are fired
Event::fake();
$this->actingAs($this->admin)
->visit('/admin/access/role/1/edit')
->type('Administrator Edited', 'name')
->type('123', 'sort')
->press('Update')
->seePageIs('/admin/access/role')
->see('The role was successfully updated.')
->seeInDatabase(config('access.roles_table'), ['id' => 1, 'name' => 'Administrator Edited', 'sort' => 123]);
Event::assertDispatched(RoleUpdated::class);
}
public function testUpdateRoleFormSpecificPermissions()
{
// Make sure our events are fired
Event::fake();
$this->actingAs($this->admin)
->notSeeInDatabase(config('access.permission_role_table'), ['permission_id' => 1, 'role_id' => 3])
->visit('/admin/access/role/3/edit')
->check('permissions[1]')
->press('Update')
->seePageIs('/admin/access/role')
->see('The role was successfully updated.')
->seeInDatabase(config('access.permission_role_table'), ['permission_id' => 1, 'role_id' => 3]);
Event::assertDispatched(RoleUpdated::class);
}
public function testUpdateRoleRequiresPermission()
{
$this->actingAs($this->admin)
->visit('/admin/access/role/3/edit')
->press('Update')
->seePageIs('/admin/access/role/3/edit')
->see('You must select at least one permission for this role.');
}
public function testDeleteRoleForm()
{
// Make sure our events are fired
Event::fake();
$role = factory(Role::class)->create();
$this->actingAs($this->admin)
->seeInDatabase(config('access.roles_table'), ['id' => $role->id])
->delete('/admin/access/role/'.$role->id)
->assertRedirectedTo('/admin/access/role')
->notSeeInDatabase(config('access.roles_table'), ['id' => $role->id])
->seeInSession(['flash_success' => 'The role was successfully deleted.']);
Event::assertDispatched(RoleDeleted::class);
}
public function testDeleteRoleWithPermissions()
{
// Make sure our events are fired
Event::fake();
// Remove users from role first because it will error on that first
DB::table(config('access.role_user_table'))->where('role_id', 2)->delete();
$this->actingAs($this->admin)
->visit('/admin/access/role')
->delete('/admin/access/role/2')
->assertRedirectedTo('/admin/access/role')
->notSeeInDatabase(config('access.roles_table'), ['id' => 2])
->notSeeInDatabase(config('access.permission_role_table'), ['permission_id' => 1, 'role_id' => 2])
->seeInSession(['flash_success' => 'The role was successfully deleted.']);
Event::assertDispatched(RoleDeleted::class);
}
public function testCanNotDeleteAdministratorRole()
{
$this->actingAs($this->admin)
->visit('/admin/access/role')
->delete('/admin/access/role/1')
->assertRedirectedTo('/admin/access/role')
->seeInDatabase(config('access.roles_table'), ['id' => 1, 'name' => 'Administrator'])
->seeInSession(['flash_danger' => 'You can not delete the Administrator role.']);
}
public function testCanNotDeleteRoleWithUsers()
{
$this->actingAs($this->admin)
->visit('/admin/access/role')
->delete('/admin/access/role/2')
->assertRedirectedTo('/admin/access/role')
->seeInDatabase(config('access.roles_table'), ['id' => 2])
->seeInSession(['flash_danger' => 'You can not delete a role with associated users.']);
}
}
<?php
use App\Events\Backend\Access\User\UserCreated;
use App\Events\Backend\Access\User\UserDeleted;
use App\Events\Backend\Access\User\UserPasswordChanged;
use App\Events\Backend\Access\User\UserUpdated;
use App\Models\Access\User\User;
use App\Notifications\Frontend\Auth\UserNeedsConfirmation;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Notification;
use Tests\BrowserKitTestCase;
/**
* Class UserFormTest.
*/
class UserFormTest extends BrowserKitTestCase
{
public function testCreateUserRequiredFields()
{
$this->actingAs($this->admin)
->visit('/admin/access/user/create')
->type('', 'name')
->type('', 'email')
->type('', 'password')
->type('', 'password_confirmation')
->press('Create')
->see('The name field is required.')
->see('The email field is required.')
->see('The password field is required.');
}
public function testCreateUserPasswordsDoNotMatch()
{
$this->actingAs($this->admin)
->visit('/admin/access/user/create')
->type('Test User', 'name')
->type('test@test.com', 'email')
->type('123456', 'password')
->type('1234567', 'password_confirmation')
->press('Create')
->see('The password confirmation does not match.');
}
public function testCreateUserConfirmedForm()
{
// Make sure our events are fired
Event::fake();
// Create any needed resources
$faker = Faker\Factory::create();
$name = $faker->name;
$email = $faker->safeEmail;
$password = $faker->password(8);
$this->actingAs($this->admin)
->visit('/admin/access/user/create')
->type($name, 'name')
->type($email, 'email')
->type($password, 'password')
->type($password, 'password_confirmation')
->seeIsChecked('status')
->seeIsChecked('confirmed')
->dontSeeIsChecked('confirmation_email')
->check('assignees_roles[2]')
->check('assignees_roles[3]')
->press('Create')
->seePageIs('/admin/access/user')
->see('The user was successfully created.')
->seeInDatabase(config('access.users_table'), ['name' => $name, 'email' => $email, 'status' => 1, 'confirmed' => 1])
->seeInDatabase(config('access.role_user_table'), ['user_id' => 4, 'role_id' => 2])
->seeInDatabase(config('access.role_user_table'), ['user_id' => 4, 'role_id' => 3]);
Event::assertDispatched(UserCreated::class);
}
public function testCreateUserUnconfirmedForm()
{
// Make sure our events are fired
Event::fake();
// Make sure our notifications are sent
Notification::fake();
// Create any needed resources
$faker = Faker\Factory::create();
$name = $faker->name;
$email = $faker->safeEmail;
$password = $faker->password(8);
$this->actingAs($this->admin)
->visit('/admin/access/user/create')
->type($name, 'name')
->type($email, 'email')
->type($password, 'password')
->type($password, 'password_confirmation')
->seeIsChecked('status')
->uncheck('confirmed')
->check('confirmation_email')
->check('assignees_roles[2]')
->check('assignees_roles[3]')
->press('Create')
->seePageIs('/admin/access/user')
->see('The user was successfully created.')
->seeInDatabase(config('access.users_table'), ['name' => $name, 'email' => $email, 'status' => 1, 'confirmed' => 0])
->seeInDatabase(config('access.role_user_table'), ['user_id' => 4, 'role_id' => 2])
->seeInDatabase(config('access.role_user_table'), ['user_id' => 4, 'role_id' => 3]);
// Get the user that was inserted into the database
$user = User::where('email', $email)->first();
// Check that the user was sent the confirmation email
Notification::assertSentTo([$user],
UserNeedsConfirmation::class);
Event::assertDispatched(UserCreated::class);
}
public function testCreateUserFailsIfEmailExists()
{
$this->actingAs($this->admin)
->visit('/admin/access/user/create')
->type('User', 'name')
->type('user@user.com', 'email')
->type('123456', 'password')
->type('123456', 'password_confirmation')
->press('Create')
->seePageIs('/admin/access/user/create')
->see('The email has already been taken.');
}
public function testUpdateUserRequiredFields()
{
$this->actingAs($this->admin)
->visit('/admin/access/user/3/edit')
->type('', 'name')
->type('', 'email')
->press('Update')
->see('The name field is required.')
->see('The email field is required.');
}
public function testUpdateUserForm()
{
// Make sure our events are fired
Event::fake();
$this->actingAs($this->admin)
->visit('/admin/access/user/'.$this->user->id.'/edit')
->see($this->user->name)
->see($this->user->email)
->type('User New', 'name')
->type('user2@user.com', 'email')
->uncheck('status')
->uncheck('confirmed')
->check('assignees_roles[2]')
->uncheck('assignees_roles[3]')
->press('Update')
->seePageIs('/admin/access/user')
->see('The user was successfully updated.')
->seeInDatabase(config('access.users_table'),
[
'id' => $this->user->id,
'name' => 'User New',
'email' => 'user2@user.com',
'status' => 0,
'confirmed' => 0,
])
->seeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => 2])
->notSeeInDatabase(config('access.role_user_table'), ['user_id' => $this->user->id, 'role_id' => 3]);
Event::assertDispatched(UserUpdated::class);
}
public function testDeleteUserForm()
{
// Make sure our events are fired
Event::fake();
$this->actingAs($this->admin)
->delete('/admin/access/user/'.$this->user->id)
->assertRedirectedTo('/admin/access/user/deleted')
->notSeeInDatabase(config('access.users_table'), ['id' => $this->user->id, 'deleted_at' => null]);
Event::assertDispatched(UserDeleted::class);
}
public function testUserCanNotDeleteSelf()
{
$this->actingAs($this->admin)
->visit('/admin/access/user')
->delete('/admin/access/user/'.$this->admin->id)
->assertRedirectedTo('/admin/access/user')
->seeInDatabase(config('access.users_table'), ['id' => $this->admin->id, 'deleted_at' => null])
->seeInSession(['flash_danger' => 'You can not delete yourself.']);
}
public function testChangeUserPasswordRequiredFields()
{
$this->actingAs($this->admin)
->visit('/admin/access/user/'.$this->user->id.'/password/change')
->see('Change Password for '.$this->user->name)
->type('', 'password')
->type('', 'password_confirmation')
->press('Update')
->seePageIs('/admin/access/user/'.$this->user->id.'/password/change')
->see('The password field is required.');
}
public function testChangeUserPasswordForm()
{
// Make sure our events are fired
Event::fake();
$password = '87654321';
$this->actingAs($this->admin)
->visit('/admin/access/user/'.$this->user->id.'/password/change')
->see('Change Password for '.$this->user->name)
->type($password, 'password')
->type($password, 'password_confirmation')
->press('Update')
->seePageIs('/admin/access/user')
->see('The user\'s password was successfully updated.');
Event::assertDispatched(UserPasswordChanged::class);
}
public function testChangeUserPasswordDoNotMatch()
{
$this->actingAs($this->admin)
->visit('/admin/access/user/'.$this->user->id.'/password/change')
->see('Change Password for '.$this->user->name)
->type('123456', 'password')
->type('1234567', 'password_confirmation')
->press('Update')
->seePageIs('/admin/access/user/'.$this->user->id.'/password/change')
->see('The password confirmation does not match.');
}
}
<?php
use Tests\BrowserKitTestCase;
/**
* Class SearchFormTest.
*/
class SearchFormTest extends BrowserKitTestCase
{
public function testSearchPageWithNoQuery()
{
$this->actingAs($this->admin)
->visit('/admin/search')
->seePageIs('/admin/dashboard')
->see('Please enter a search term.');
}
public function testSearchFormRedirectsToResults()
{
$this->actingAs($this->admin)
->visit('/admin/dashboard')
->type('Test Query', 'q')
->press('search-btn')
->seePageIs('/admin/search?q=Test%20Query')
->see('Search Results for Test Query');
}
}
<?php
use Tests\BrowserKitTestCase;
/**
* Class HistoryLogTest.
*/
class HistoryLogTest extends BrowserKitTestCase
{
public function testHistoryLogByTypeNameFunction()
{
$this->actingAs($this->admin);
history()
->withType('User')
->withText(trans('history.backend.users.created').$this->user->name)
->withEntity($this->user->id)
->withIcon('plus')
->withClass('bg-green')
->log();
$this->seeInDatabase('history',
[
'type_id' => 1,
'user_id' => $this->admin->id,
'entity_id' => $this->user->id,
'icon' => 'plus',
'class' => 'bg-green',
'text' => trans('history.backend.users.created').$this->user->name,
])
->visit('/admin/dashboard')
->see('<strong>'.$this->admin->name.'</strong> '.trans('history.backend.users.created').$this->user->name);
}
public function testHistoryLogByTypeIdFunction()
{
$this->actingAs($this->admin);
history()
->withType(1)
->withText(trans('history.backend.users.created').$this->user->name)
->withEntity($this->user->id)
->withIcon('plus')
->withClass('bg-green')
->log();
$this->seeInDatabase('history',
[
'type_id' => 1,
'user_id' => $this->admin->id,
'entity_id' => $this->user->id,
'icon' => 'plus',
'class' => 'bg-green',
'text' => trans('history.backend.users.created').$this->user->name,
])
->visit('/admin/dashboard')
->see('<strong>'.$this->admin->name.'</strong> '.trans('history.backend.users.created').$this->user->name);
}
}
<?php
use App\Models\Access\User\User;
use Tests\BrowserKitTestCase;
/**
* Class HistoryRenderEntityTest.
*/
class HistoryRenderEntityTest extends BrowserKitTestCase
{
public function testViewOnlyHasHistoryOfEntity()
{
$this->actingAs($this->admin);
$test_user = factory(User::class)->create();
history()
->withType('User')
->withText('trans("history.backend.users.created") '.$this->user->name)
->withEntity($this->user->id)
->withIcon('plus')
->withClass('bg-green')
->log();
history()
->withType('User')
->withText('trans("history.backend.users.updated") '.$this->user->name)
->withEntity($this->user->id)
->withIcon('pencil')
->withClass('bg-blue')
->log();
history()
->withType('User')
->withText('trans("history.backend.users.deleted") '.$this->user->name)
->withEntity($this->user->id)
->withIcon('trash')
->withClass('bg-red')
->log();
history()
->withType('User')
->withText('trans("history.backend.roles.created") '.$test_user->name)
->withEntity($test_user->id)
->withIcon('plus')
->withClass('bg-red')
->log();
history()
->withType('User')
->withText('trans("history.backend.roles.updated") '.$test_user->name)
->withEntity($test_user->id)
->withIcon('pencil')
->withClass('bg-red')
->log();
history()
->withType('User')
->withText('trans("history.backend.roles.deleted") '.$test_user->name)
->withEntity($test_user->id)
->withIcon('trash')
->withClass('bg-red')
->log();
$this->visit('/admin/access/user/'.$this->user->id)
->see('<strong>'.$this->admin->name.'</strong> created user '.$this->user->name)
->see('<strong>'.$this->admin->name.'</strong> updated user '.$this->user->name)
->see('<strong>'.$this->admin->name.'</strong> deleted user '.$this->user->name)
->dontSee('<strong>'.$this->admin->name.'</strong> created user '.$test_user->name)
->dontSee('<strong>'.$this->admin->name.'</strong> updated user '.$test_user->name)
->dontSee('<strong>'.$this->admin->name.'</strong> deleted user '.$test_user->name);
}
}
<?php
use Tests\BrowserKitTestCase;
/**
* Class HistoryRenderTest.
*/
class HistoryRenderTest extends BrowserKitTestCase
{
public function testDashboardDisplaysHistory()
{
$this->actingAs($this->admin);
history()
->withType('User')
->withText(trans('history.backend.users.created').$this->user->name)
->withEntity($this->user->id)
->withIcon('plus')
->withClass('bg-green')
->log();
$this->visit('/admin/dashboard')
->see('<strong>'.$this->admin->name.'</strong> '.trans('history.backend.users.created').$this->user->name);
}
public function testTypeDisplaysHistory()
{
$this->actingAs($this->admin);
history()
->withType('User')
->withText(trans('history.backend.users.created').$this->user->name)
->withEntity($this->user->id)
->withIcon('plus')
->withClass('bg-green')
->log();
$this->visit('/admin/access/user')
->see('<strong>'.$this->admin->name.'</strong> '.trans('history.backend.users.created').$this->user->name);
}
public function testEntityDisplaysHistory()
{
$this->actingAs($this->admin);
history()
->withType('User')
->withText(trans('history.backend.users.created').$this->user->name)
->withEntity($this->user->id)
->withIcon('plus')
->withClass('bg-green')
->log();
$this->visit('/admin/access/user/3')
->see('<strong>'.$this->admin->name.'</strong> '.trans('history.backend.users.created').$this->user->name);
}
}
<?php
use Tests\BrowserKitTestCase;
/**
* Class HistoryRenderTypeTest.
*/
class HistoryRenderTypeTest extends BrowserKitTestCase
{
public function testViewOnlyHasHistoryOfType()
{
$this->actingAs($this->admin);
history()
->withType('User')
->withText('trans("history.backend.users.created") '.$this->user->name)
->withEntity($this->user->id)
->withIcon('plus')
->withClass('bg-green')
->log();
history()
->withType('User')
->withText('trans("history.backend.users.updated") '.$this->user->name)
->withEntity($this->user->id)
->withIcon('pencil')
->withClass('bg-blue')
->log();
history()
->withType('User')
->withText('trans("history.backend.users.deleted") '.$this->user->name)
->withEntity($this->user->id)
->withIcon('trash')
->withClass('bg-red')
->log();
history()
->withType('Role')
->withText('trans("history.backend.roles.created") '.$this->adminRole->name)
->withEntity($this->adminRole->id)
->withIcon('plus')
->withClass('bg-red')
->log();
history()
->withType('Role')
->withText('trans("history.backend.roles.updated") '.$this->adminRole->name)
->withEntity($this->adminRole->id)
->withIcon('pencil')
->withClass('bg-red')
->log();
history()
->withType('Role')
->withText('trans("history.backend.roles.deleted") ')
->withEntity($this->adminRole->id)
->withIcon('trash')
->withClass('bg-red')
->log();
$this->visit('/admin/access/user')
->see('<strong>'.$this->admin->name.'</strong> created user '.$this->user->name)
->see('<strong>'.$this->admin->name.'</strong> updated user '.$this->user->name)
->see('<strong>'.$this->admin->name.'</strong> deleted user '.$this->user->name)
->dontSee('<strong>'.$this->admin->name.'</strong> created role '.$this->adminRole->name)
->dontSee('<strong>'.$this->admin->name.'</strong> updated role '.$this->adminRole->name)
->dontSee('<strong>'.$this->admin->name.'</strong> deleted role '.$this->adminRole->name);
}
}
<?php
use Tests\BrowserKitTestCase;
/**
* Class RoleRouteTest.
*/
class RoleRouteTest extends BrowserKitTestCase
{
public function testRolesIndex()
{
$this->actingAs($this->admin)->visit('/admin/access/role')->see('Role Management');
}
public function testCreateRole()
{
$this->actingAs($this->admin)->visit('/admin/access/role/create')->see('Create Role');
}
public function testEditRole()
{
$this->actingAs($this->admin)
->visit('/admin/access/role/'.$this->adminRole->id.'/edit')
->see('Edit Role')
->see($this->adminRole->name);
}
}
<?php
use App\Events\Backend\Access\User\UserDeactivated;
use App\Events\Backend\Access\User\UserPermanentlyDeleted;
use App\Events\Backend\Access\User\UserReactivated;
use App\Events\Backend\Access\User\UserRestored;
use App\Notifications\Frontend\Auth\UserNeedsConfirmation;
use Carbon\Carbon;
use Illuminate\Support\Facades\Event;
use Tests\BrowserKitTestCase;
/**
* Class UserRouteTest.
*/
class UserRouteTest extends BrowserKitTestCase
{
public function testActiveUsers()
{
$this->actingAs($this->admin)->visit('/admin/access/user')->see('Active Users');
}
public function testDeactivatedUsers()
{
$this->actingAs($this->admin)->visit('/admin/access/user/deactivated')->see('Deactivated Users');
}
public function testDeletedUsers()
{
$this->actingAs($this->admin)->visit('/admin/access/user/deleted')->see('Deleted Users');
}
public function testCreateUser()
{
$this->actingAs($this->admin)->visit('/admin/access/user/create')->see('Create User');
}
public function testViewUser()
{
$this->actingAs($this->admin)
->visit('/admin/access/user/'.$this->user->id)
->see('View User')
->see('Overview')
->see('History')
->see($this->user->name)
->see($this->user->email);
}
public function testEditUser()
{
$this->actingAs($this->admin)
->visit('/admin/access/user/'.$this->user->id.'/edit')
->see('Edit User')
->see($this->user->name)
->see($this->user->email);
}
public function testChangeUserPassword()
{
$this->actingAs($this->admin)
->visit('/admin/access/user/'.$this->user->id.'/password/change')
->see('Change Password for '.$this->user->name);
}
public function testResendUserConfirmationEmail()
{
Notification::fake();
$this->actingAs($this->admin)
->visit('/admin/access/user')
->visit('/admin/access/user/'.$this->user->id.'/account/confirm/resend')
->seePageIs('/admin/access/user')
->see('A new confirmation e-mail has been sent to the address on file.');
Notification::assertSentTo($this->user, UserNeedsConfirmation::class);
}
public function testLoginAsUser()
{
$this->actingAs($this->admin)
->visit('/admin/access/user/'.$this->user->id.'/login-as')
->seePageIs('/')
->see('You are currently logged in as '.$this->user->name.'.')
->see($this->admin->name)
->assertTrue(access()->id() == $this->user->id);
}
public function testCantLoginAsSelf()
{
$this->actingAs($this->admin)
->visit('/admin/access/user/'.$this->admin->id.'/login-as')
->see('Do not try to login as yourself.');
}
public function testLogoutAsUser()
{
$this->actingAs($this->admin)
->visit('/admin/access/user/'.$this->user->id.'/login-as')
->seePageIs('/')
->see('You are currently logged in as '.$this->user->name.'.')
->click('Re-Login as '.$this->admin->name)
->seePageIs('/admin/access/user')
->assertTrue(access()->id() == $this->admin->id);
}
public function testDeactivateReactivateUser()
{
// Make sure our events are fired
Event::fake();
$this->actingAs($this->admin)
->visit('/admin/access/user/'.$this->user->id.'/mark/0')
->seePageIs('/admin/access/user/deactivated')
->see('The user was successfully updated.')
->seeInDatabase(config('access.users_table'), ['id' => $this->user->id, 'status' => 0])
->visit('/admin/access/user/'.$this->user->id.'/mark/1')
->seePageIs('/admin/access/user')
->see('The user was successfully updated.')
->seeInDatabase(config('access.users_table'), ['id' => $this->user->id, 'status' => 1]);
Event::assertDispatched(UserDeactivated::class);
Event::assertDispatched(UserReactivated::class);
}
public function testRestoreUser()
{
// Make sure our events are fired
Event::fake();
$this->user->deleted_at = Carbon::now();
$this->user->save();
$this->actingAs($this->admin)
->notSeeInDatabase(config('access.users_table'), ['id' => $this->user->id, 'deleted_at' => null])
->visit('/admin/access/user/'.$this->user->id.'/restore')
->seePageIs('/admin/access/user')
->see('The user was successfully restored.')
->seeInDatabase(config('access.users_table'), ['id' => $this->user->id, 'deleted_at' => null]);
Event::assertDispatched(UserRestored::class);
}
public function testUserIsDeletedBeforeBeingRestored()
{
$this->actingAs($this->admin)
->seeInDatabase(config('access.users_table'), ['id' => $this->user->id, 'deleted_at' => null])
->visit('/admin/access/user')
->visit('/admin/access/user/'.$this->user->id.'/restore')
->seePageIs('/admin/access/user')
->see('This user is not deleted so it can not be restored.')
->seeInDatabase(config('access.users_table'), ['id' => $this->user->id, 'deleted_at' => null]);
}
public function testPermanentlyDeleteUser()
{
// Make sure our events are fired
Event::fake();
$this->actingAs($this->admin)
->delete('/admin/access/user/'.$this->user->id)
->notSeeInDatabase(config('access.users_table'), ['id' => $this->user->id, 'deleted_at' => null])
->visit('/admin/access/user/'.$this->user->id.'/delete')
->seePageIs('/admin/access/user/deleted')
->see('The user was deleted permanently.')
->notSeeInDatabase(config('access.users_table'), ['id' => $this->user->id]);
Event::assertDispatched(UserPermanentlyDeleted::class);
}
public function testUserIsDeletedBeforeBeingPermanentlyDeleted()
{
$this->actingAs($this->admin)
->seeInDatabase(config('access.users_table'), ['id' => $this->user->id, 'deleted_at' => null])
->visit('/admin/access/user')
->visit('/admin/access/user/'.$this->user->id.'/delete')
->seePageIs('/admin/access/user')
->see('This user must be deleted first before it can be destroyed permanently.')
->seeInDatabase(config('access.users_table'), ['id' => $this->user->id, 'deleted_at' => null]);
}
public function testCantNotDeactivateSelf()
{
$this->actingAs($this->admin)
->visit('/admin/access/user')
->visit('/admin/access/user/'.$this->admin->id.'/mark/0')
->seePageIs('/admin/access/user')
->see('You can not do that to yourself.');
}
}
<?php
use Tests\BrowserKitTestCase;
/**
* Class DashboardRouteTest.
*/
class DashboardRouteTest extends BrowserKitTestCase
{
public function testAdminDashboard()
{
$this->actingAs($this->admin)->visit('/admin/dashboard')->see('Access Management')->see($this->admin->name);
}
}
<?php
use Tests\BrowserKitTestCase;
/**
* Class LogViewerRouteTest.
*/
class LogViewerRouteTest extends BrowserKitTestCase
{
public function testLogViewerDashboard()
{
$this->actingAs($this->admin)
->visit('/admin/log-viewer')
->see('Log Viewer');
}
public function testLogViewerList()
{
$this->actingAs($this->admin)
->visit('/admin/log-viewer/logs')
->see('Logs');
}
public function testLogViewerSingle()
{
$this->actingAs($this->admin)
->visit('/admin/log-viewer/logs/'.date('Y-m-d'))
->see('Log ['.date('Y-m-d').']');
}
public function testLogViewerSingleType()
{
$this->actingAs($this->admin)
->visit('/admin/log-viewer/logs/'.date('Y-m-d').'/error')
->see('Log ['.date('Y-m-d').']');
}
}
<?php
namespace Tests;
use App\Models\Access\Role\Role;
use App\Models\Access\User\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Laravel\BrowserKitTesting\TestCase as BaseTestCase;
/**
* Class TestCase.
*/
abstract class BrowserKitTestCase extends BaseTestCase
{
use CreatesApplication;
use DatabaseTransactions;
/**
* The base URL to use while testing the application.
*
* @var string
*/
protected $baseUrl = ' */
*/
http://laraveladminpanel.dev';
/**
* @var
*/
protected $admin;
/**
* @var
*/
protected $executive;
/**
* @var
*/
protected $user;
/**
* @var
*/
protected $adminRole;
/**
* @var
*/
protected $executiveRole;
/**
* @var
*/
protected $userRole;
/**
* Set up tests.
*/
public function setUp()
{
parent::setUp();
$this->baseUrl = config('app.url', ' */
*/
http://laraveladminpanel.dev');
// Set up the database
Artisan::call('migrate:refresh');
Artisan::call('db:seed');
// Run the tests in English
App::setLocale('en');
/*
* Create class properties to be used in tests
*/
$this->admin = User::find(1);
$this->executive = User::find(2);
$this->user = User::find(3);
$this->adminRole = Role::find(1);
$this->executiveRole = Role::find(2);
$this->userRole = Role::find(3);
}
public function tearDown()
{
$this->beforeApplicationDestroyed(function () {
DB::disconnect();
});
parent::tearDown();
}
}
......@@ -15,10 +15,6 @@ trait CreatesApplication
{
$app = require __DIR__.'/../bootstrap/app.php';
if (file_exists(dirname(__DIR__).'/.env.testing')) {
( new \Dotenv\Dotenv(dirname(__DIR__), '.env.testing') )->load();
}
$app->make(Kernel::class)->bootstrap();
return $app;
......
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testBasicTest()
{
$response = $this->get('/');
$response->assertStatus(200);
}
}
<?php
use Tests\BrowserKitTestCase;
/**
* Class LoggedInFormTest.
*/
class LoggedInFormTest extends BrowserKitTestCase
{
/**
* Test that the errors work if nothing is filled in the update account form.
*/
public function testUpdateProfileRequiredFields()
{
if (config('access.users.change_email')) {
$this->actingAs($this->user)
->visit('/account')
->type('', 'name')
->type('', 'email')
->press('update-profile')
->seePageIs('/account')
->see('The name field is required.')
->see('The email field is required.');
} else {
$this->actingAs($this->user)
->visit('/account')
->type('', 'name')
->press('update-profile')
->seePageIs('/account')
->see('The name field is required.');
}
}
/**
* Test that we can target the update profile form and update the profile
* Based on whether the user is allowed to alter their email address or not.
*/
public function testUpdateProfileForm()
{
$rand = rand();
if (config('access.users.change_email')) {
$this->actingAs($this->user)
->visit('/account')
->see('My Account')
->type($this->user->name.'_'.$rand, 'name')
->type('2_'.$this->user->email, 'email')
->press('update-profile')
->seePageIs('/account')
->see('Profile successfully updated.')
->seeInDatabase(config('access.users_table'),
['email' => '2_'.$this->user->email, 'name' => $this->user->name.'_'.$rand]);
} else {
$this->actingAs($this->user)
->visit('/account')
->see('My Account')
->type($this->user->name.'_'.$rand, 'name')
->press('update-profile')
->seePageIs('/account')
->see('Profile successfully updated.')
->seeInDatabase(config('access.users_table'), ['name' => $this->user->name.'_'.$rand]);
}
}
/**
* Test that the errors work if nothing is filled in the change password form.
*/
public function testChangePasswordRequiredFields()
{
$this->actingAs($this->user)
->visit('/account')
->type('', 'old_password')
->type('', 'password')
->type('', 'password_confirmation')
->press('change-password')
->seePageIs('/account')
->see('The old password field is required.')
->see('The password field is required.');
}
/**
* Test that the frontend change password form works.
*/
public function testChangePasswordForm()
{
$password = '87654321';
$this->actingAs($this->user)
->visit('/account')
->see('My Account')
->type('1234', 'old_password')
->type($password, 'password')
->type($password, 'password_confirmation')
->press('change-password')
->seePageIs('/account')
->see('Password successfully updated.');
}
}
<?php
use App\Events\Frontend\Auth\UserLoggedIn;
use App\Events\Frontend\Auth\UserRegistered;
use App\Models\Access\User\User;
use App\Notifications\Frontend\Auth\UserNeedsConfirmation;
use App\Notifications\Frontend\Auth\UserNeedsPasswordReset;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Notification;
use Tests\BrowserKitTestCase;
/**
* Class LoggedOutFormTest.
*/
class LoggedOutFormTest extends BrowserKitTestCase
{
/**
* Test that the errors work if nothing is filled in the registration form.
*/
public function testRegistrationRequiredFields()
{
$this->visit('/register')
->type('', 'name')
->type('', 'email')
->type('', 'password')
->press('Register')
->seePageIs('/register')
->see('The name field is required.')
->see('The email field is required.')
->see('The password field is required.');
}
/**
* Test the registration form
* Test it works with confirming email on or off, and that the confirm email notification is sent
* Note: Captcha is disabled by default in phpunit.xml.
*/
public function testRegistrationForm()
{
// Make sure our events are fired
Event::fake();
// Create any needed resources
$faker = Faker\Factory::create();
$name = $faker->name;
$email = $faker->safeEmail;
$password = $faker->password(8);
// Check if confirmation required is on or off
if (config('access.users.confirm_email')) {
Notification::fake();
$this->visit('/register')
->type($name, 'name')
->type($email, 'email')
->type($password, 'password')
->type($password, 'password_confirmation')
->press('Register')
->see('Your account was successfully created. We have sent you an e-mail to confirm your account.')
->see('Login')
->seePageIs('/')
->seeInDatabase(config('access.users_table'), ['email' => $email, 'name' => $name]);
// Get the user that was inserted into the database
$user = User::where('email', $email)->first();
// Check that the user was sent the confirmation email
Notification::assertSentTo([$user],
UserNeedsConfirmation::class);
} else {
$this->visit('/register')
->type($name, 'name')
->type($email, 'email')
->type($password, 'password')
->type($password, 'password_confirmation')
->press('Register')
->see('Dashboard')
->seePageIs('/')
->seeInDatabase(config('access.users_table'), ['email' => $email, 'name' => $name]);
}
Event::assertDispatched(UserRegistered::class);
}
/**
* Test that the errors work if nothing is filled in the login form.
*/
public function testLoginRequiredFields()
{
$this->visit('/login')
->type('', 'email')
->type('', 'password')
->press('Login')
->seePageIs('/login')
->see('The email field is required.')
->see('The password field is required.');
}
/**
* Test that the user is logged in and redirected to the dashboard
* Test that the admin is logged in and redirected to the backend.
*/
public function testLoginForm()
{
// Make sure our events are fired
Event::fake();
Auth::logout();
//User Test
$this->visit('/login')
->type($this->user->email, 'email')
->type('1234', 'password')
->press('Login')
->seePageIs('/dashboard')
->see($this->user->email);
Auth::logout();
//Admin Test
$this->visit('/login')
->type($this->admin->email, 'email')
->type('1234', 'password')
->press('Login')
->seePageIs('/admin/dashboard')
->see($this->admin->name)
->see('Access Management');
Event::assertDispatched(UserLoggedIn::class);
}
/**
* Test that the errors work if nothing is filled in the forgot password form.
*/
public function testForgotPasswordRequiredFields()
{
$this->visit('/password/reset')
->type('', 'email')
->press('Send Password Reset Link')
->seePageIs('/password/reset')
->see('The email field is required.');
}
/**
* Test that the forgot password form sends the user the notification and places the
* row in the password_resets table.
*/
public function testForgotPasswordForm()
{
Notification::fake();
$this->visit('password/reset')
->type($this->user->email, 'email')
->press('Send Password Reset Link')
->seePageIs('password/reset')
->see('We have e-mailed your password reset link!')
->seeInDatabase('password_resets', ['email' => $this->user->email]);
Notification::assertSentTo([$this->user],
UserNeedsPasswordReset::class);
}
/**
* Test that the errors work if nothing is filled in the reset password form.
*/
public function testResetPasswordRequiredFields()
{
$token = $this->app->make('auth.password.broker')->createToken($this->user);
$this->visit('password/reset/'.$token)
->see($this->user->email)
->type('', 'password')
->type('', 'password_confirmation')
->press('Reset Password')
->see('The password field is required.');
}
/**
* Test that the password reset form works and logs the user back in.
*/
public function testResetPasswordForm()
{
$token = $this->app->make('auth.password.broker')->createToken($this->user);
$this->visit('password/reset/'.$token)
->see($this->user->email)
->type('12345678', 'password')
->type('12345678', 'password_confirmation')
->press('Reset Password')
->seePageIs('/')
->see($this->user->name);
}
/**
* Test that an unconfirmed user can not login.
*/
public function testUnconfirmedUserCanNotLogIn()
{
// Create default user to test with
$unconfirmed = factory(User::class)->states('unconfirmed')->create();
$unconfirmed->attachRole(3); //User
$this->visit('/login')
->type($unconfirmed->email, 'email')
->type('secret', 'password')
->press('Login')
->seePageIs('/login')
->see('Your account is not confirmed.');
}
/**
* Test that an inactive user can not login.
*/
public function testInactiveUserCanNotLogIn()
{
// Create default user to test with
$inactive = factory(User::class)->states('confirmed', 'inactive')->create();
$inactive->attachRole(3); //User
$this->visit('/login')
->type($inactive->email, 'email')
->type('secret', 'password')
->press('Login')
->seePageIs('/login')
->see('Your account has been deactivated.');
}
/**
* Test that a user with invalid credentials get kicked back.
*/
public function testInvalidLoginCredentials()
{
$this->visit('/login')
->type($this->user->email, 'email')
->type('9s8gy8s9diguh4iev', 'password')
->press('Login')
->seePageIs('/login')
->see('These credentials do not match our records.');
}
/**
* Adds a password reset row to the database to play with.
*
* @param $token
*
* @return mixed
*/
private function createPasswordResetToken($token)
{
DB::table('password_resets')->insert([
'email' => $this->user->email,
'token' => $token,
'created_at' => \Carbon\Carbon::now(),
]);
return $token;
}
}
<?php
use App\Events\Frontend\Auth\UserLoggedOut;
use Illuminate\Support\Facades\Event;
use Tests\BrowserKitTestCase;
/**
* Class LoggedInRouteTest.
*/
class LoggedInRouteTest extends BrowserKitTestCase
{
/**
* Test the homepage works and the dashboard button appears.
*/
public function testHomePageLoggedIn()
{
$this->actingAs($this->user)->visit('/')->see('Dashboard')->see($this->user->name)->dontSee('Administration');
}
/**
* Test the dashboard page works and displays the users information.
*/
public function testDashboardPage()
{
$this->actingAs($this->user)
->visit('/dashboard')
->see($this->user->email)
->see('Joined')
->dontSee('Administration');
}
/**
* Test the account page works and displays the users information.
*/
public function testAccountPage()
{
$this->actingAs($this->user)
->visit('/account')
->see('My Account')
->see('Profile')
->see('Update Information')
->see('Change Password')
->dontSee('Administration');
}
/**
* Test the account page works and displays the users information.
*/
public function testLoggedInAdmin()
{
$this->actingAs($this->admin)->visit('/')->see('Administration')->see($this->admin->name);
}
/**
* Test the logout button redirects the user back to home and the login button is again visible.
*/
public function testLogoutRoute()
{
// Make sure our events are fired
Event::fake();
$this->actingAs($this->user)->visit('/logout')->see('Login')->see('Register');
Event::assertDispatched(UserLoggedOut::class);
}
}
<?php
use App\Events\Frontend\Auth\UserConfirmed;
use App\Models\Access\User\User;
use App\Notifications\Frontend\Auth\UserNeedsConfirmation;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Notification;
use Tests\BrowserKitTestCase;
/**
* Class LoggedOutRouteTest.
*/
class LoggedOutRouteTest extends BrowserKitTestCase
{
/**
* User Logged Out Frontend.
*/
/**
* Test the homepage works.
*/
public function testHomePage()
{
$this->visit('/')->assertResponseOk();
}
/**
* Test the macro page works.
*/
public function testMacroPage()
{
$this->visit('/macros')->see('Macro Examples');
}
/**
* Test the login page works.
*/
public function testLoginPage()
{
$this->visit('/login')->see('Login');
}
/**
* Test the register page works.
*/
public function testRegisterPage()
{
$this->visit('/register')->see('Register');
}
/**
* Test the forgot password page works.
*/
public function testForgotPasswordPage()
{
$this->visit('password/reset')->see('Reset Password');
}
/**
* Test the dashboard page redirects to login.
*/
public function testDashboardPageLoggedOut()
{
$this->visit('/dashboard')->seePageIs('/login');
}
/**
* Test the account page redirects to login.
*/
public function testAccountPageLoggedOut()
{
$this->visit('/account')->seePageIs('/login');
}
/**
* Create an unconfirmed user and assure the user gets
* confirmed when hitting the confirmation route.
*/
public function testConfirmAccountRoute()
{
Event::fake();
// Create default user to test with
$unconfirmed = factory(User::class)->states('unconfirmed')->create();
$unconfirmed->attachRole(3); //User
$this->visit('/account/confirm/'.$unconfirmed->confirmation_code)
->seePageIs('/login')
->see('Your account has been successfully confirmed!')
->seeInDatabase(config('access.users_table'), ['email' => $unconfirmed->email, 'confirmed' => 1]);
Event::assertDispatched(UserConfirmed::class);
}
/**
* Assure the user gets resent a confirmation email
* after hitting the resend confirmation route.
*/
public function testResendConfirmAccountRoute()
{
Notification::fake();
$this->visit('/account/confirm/resend/'.$this->user->id)
->seePageIs('/login')
->see('A new confirmation e-mail has been sent to the address on file.');
Notification::assertSentTo([$this->user],
UserNeedsConfirmation::class);
}
/**
* Test the language switcher changes the desired language in the session.
*/
public function testLanguageSwitcher()
{
$this->visit('lang/es')->see('Registrarse')->assertSessionHas('locale', 'es');
App::setLocale('en');
}
/**
* Test the generic 404 page.
*/
public function test404Page()
{
$this->get('7g48hwbfw9eufj')->seeStatusCode(404)->see('Page Not Found');
}
}
<?php
use App\Models\Access\Role\Role;
use App\Models\Access\User\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
namespace Tests;
/**
* Class TestCase.
*/
abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
{
use DatabaseTransactions;
/**
* The base URL to use while testing the application.
*
* @var string
*/
protected $baseUrl = ' */
*/
http://laraveladminpanel.dev';
/**
* @var
*/
protected $admin;
/**
* @var
*/
protected $executive;
/**
* @var
*/
protected $user;
/**
* @var
*/
protected $adminRole;
/**
* @var
*/
protected $executiveRole;
/**
* @var
*/
protected $userRole;
/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
return $app;
}
/**
* Set up tests.
*/
public function setUp()
{
parent::setUp();
// Set up the database
Artisan::call('migrate:refresh');
Artisan::call('db:seed');
// Run the tests in English
App::setLocale('en');
/*
* Create class properties to be used in tests
*/
$this->admin = User::find(1);
$this->executive = User::find(2);
$this->user = User::find(3);
$this->adminRole = Role::find(1);
$this->executiveRole = Role::find(2);
$this->userRole = Role::find(3);
}
public function tearDown()
{
$this->beforeApplicationDestroyed(function () {
DB::disconnect();
});
parent::tearDown();
}
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
}
<?php
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testBasicTest()
{
$this->assertTrue(true);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment