Class luya\testsuite\scopes\PermissionScope

Inheritanceluya\testsuite\scopes\PermissionScope » luya\testsuite\scopes\BaseScope
Uses Traitsluya\testsuite\traits\AdminDatabaseTableTrait
Available since version1.0.20
Source Code https://github.com/luyadev/luya-testsuite/blob/master/src/scopes/PermissionScope.php

Generate a permission Scope for a Rest Call.

Its recommend to have the application with the following components config:

'modules' => [
     'admin' => ['class' => 'luya\admin\Module']
]

An example of how to test Rest Controllers with route permissions:

class MyUnitTest extends WebApplicationTestCase
{
     public function testRestController()
     {
         $controller = new RestController('id', $this->app);

         $assert = PermissionScope::run($this->app, function(PermissionScope $scope) use ($controller) {
             // there is now a permission for this route
             $scope->createRoute('module/controller/action');

             // now you can either allow this route or deny:
             $scope->allowRoute('module/controller/action');

             // which is equals to: $scope->createAndAllowRoute('module/controller/action');

             // now the user has permission to this route.
             return $scope->runControllerAction($controller, 'action');
         });

         $this->assertSame('foobar', $assert);
     }
}

An example in how to test an RestActive Controllers with api permissions:

public function testRestActiveController()
{
     $api = new RestActiveController('id', $this->app);

     PermissionScope::run($this->app, function(PermissionScope $scope) use ($api) {
         $scope->createApiAndAllow('api-test-action', true, true, false); // create the route and also allocate the permission create and update but not delete.

         $this->expectException('yii\web\ForbiddenHttpRequest');
         $scope->runControllerAction($api, 'delete', ['id' => 1], 'DELETE');
     });
}

In order to configure the scope use the second argument in run():

PermissionScop::run($this->app, function(PermissionScope $scope) {
    // do stuff
}, function(PermissionScope $setupScope) {
    // the setup scope allows you to configre scoped details before the call scope runs.
    $setupScope->userId = 1000;
    $scopeScope->userFixture = [
         'firstname' => 'Jane',
         'is_api_user' => false,
    ]
});

An example of how to test a POST (save) action of an {{luya\admin\ngrest\base\Api}} controller with JWT enabled:

class MyNgRestTest extends NgRestTestCase
{
    public $modelClass = MyTestModel::class;

    public $apiClass = MyTestModelController::class;

    public $controllerClass = MyTestModelController::class;

    public function getConfigArray()
    {
        return [
            'id' => 'MyTestModel',
            'basePath' => dirname(__FILE__),
            'modules' => [
                'admin' => [
                    'class' => 'luya\admin\Module',
                    'cors' => true
                ],
                'myadminmodel' => 'myadminmodel\admin\Module',
            ],
            'components' => [
                'jwt' => [
                    'class' => 'luya\admin\components\Jwt',
                    'key' => '123',
                    'apiUserEmail' => 'unknown',
                    'identityClass' => 'myadminmodel\models\User',
                ],
            ]
        ];
    }

    public function testSaveEntry()
    {
        $user = new NgRestModelFixture([
            'modelClass' => User::class,
        ]);
        $model = $user->newModel;
        $model->id = 1;
        // set the jwt identity model
        $this->app->jwt->identity = $model;

        PermissionScope::run($this->app, function(PermissionScope $scope) {
            $fixture = new NgRestModelFixture([
                'modelClass' => MyTestModel::class,
            ]);
            $scope->createAndAllowApi('MyTestModel');
            $ctrl = new MyTestModelController('MyTestModel', $this->app);
            $this->app->request->setBodyParams([
                'field' => 'value',
                // ... other post vlaues
            ]);
            $response = $scope->runControllerAction($ctrl, 'create', [], 'POST');
            // assert response content
        });
    }
}

Public Properties

Hide inherited properties

Property Type Description Defined By
$groupId integer The value which should be taken to generate the group. luya\testsuite\scopes\PermissionScope
$userFixtureData array An array which allows you to override user data for example to make user deleted: `php 'userFixtureData' => [ 'is_deleted' => 1, ] ` > the id attribute should never be provided, as its determed by {{$userId}} property. luya\testsuite\scopes\PermissionScope
$userId integer The value which should be taken to generate the user. luya\testsuite\scopes\PermissionScope

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Permission Scope contstructor. luya\testsuite\scopes\BaseScope
addPermissionApi() Add permission api entry luya\testsuite\traits\AdminDatabaseTableTrait
addPermissionRoute() Add permission route. luya\testsuite\traits\AdminDatabaseTableTrait
allowApi() Assign the Api permission to the current user and group with given permissions. luya\testsuite\scopes\PermissionScope
allowRoute() Allow the route for the current user and group. luya\testsuite\scopes\PermissionScope
assignGroupAuth() Assigne a group to an auth entry. luya\testsuite\traits\AdminDatabaseTableTrait
cleanup() Clean up tables and fixtures. luya\testsuite\scopes\PermissionScope
cleanupFixture() A helper method to cleanup fixtures. luya\testsuite\scopes\BaseScope
configure() Configured is used before prepare which allows you to configure the current scope. luya\testsuite\scopes\BaseScope
createAdminAuthTable() Create the admin auth table. luya\testsuite\traits\AdminDatabaseTableTrait
createAdminGroupAuthTable() Create the admin group auth table. luya\testsuite\traits\AdminDatabaseTableTrait
createAdminGroupFixture() Create the Group Fixture with given ID. luya\testsuite\traits\AdminDatabaseTableTrait
createAdminLangFixture() Create admin language fixture luya\testsuite\traits\AdminDatabaseTableTrait
createAdminNgRestLogFixture() Create the NgRest Log Fixture. luya\testsuite\traits\AdminDatabaseTableTrait
createAdminQueueLogErrorFixture() Queue Log Error luya\testsuite\traits\AdminDatabaseTableTrait
createAdminQueueLogFixture() Queue Log luya\testsuite\traits\AdminDatabaseTableTrait
createAdminQueueTable() Create the Admin Queue Table luya\testsuite\traits\AdminDatabaseTableTrait
createAdminStorageFileFixture() Storage File luya\testsuite\traits\AdminDatabaseTableTrait
createAdminStorageFilterChainFixture() Storage Filter Chain luya\testsuite\traits\AdminDatabaseTableTrait
createAdminStorageFilterFixture() Storage Filter luya\testsuite\traits\AdminDatabaseTableTrait
createAdminStorageImageFixture() Storage Image luya\testsuite\traits\AdminDatabaseTableTrait
createAdminTagFixture() Create admin tag fixture luya\testsuite\traits\AdminDatabaseTableTrait
createAdminTagRelationFixture() Create admin tag relation fixture luya\testsuite\traits\AdminDatabaseTableTrait
createAdminUserAuthNotificationTable() Create the admin user auth notification table. luya\testsuite\traits\AdminDatabaseTableTrait
createAdminUserDeviceFixture() User Device luya\testsuite\traits\AdminDatabaseTableTrait
createAdminUserFixture() Create the User Fixture with given fixture Data. luya\testsuite\traits\AdminDatabaseTableTrait
createAdminUserGroupTable() Create the admin user group table. luya\testsuite\traits\AdminDatabaseTableTrait
createAdminUserLoginFixture() Admin User Fixture luya\testsuite\traits\AdminDatabaseTableTrait
createAdminUserLoginLockoutFixture() User Login Lockout luya\testsuite\traits\AdminDatabaseTableTrait
createAdminUserOnlineFixture() Create the User Online Fixture. luya\testsuite\traits\AdminDatabaseTableTrait
createAndAllowApi() Create the Api in the permission system and directly allow the api with given permissions. luya\testsuite\scopes\PermissionScope
createAndAllowRoute() Create a route in permission system and directly allow the route. luya\testsuite\scopes\PermissionScope
createApi() Create an Api in permission system. (ActiveRestController). luya\testsuite\scopes\PermissionScope
createGroupFixture() Create the Group Fixture with given ID. luya\testsuite\traits\AdminDatabaseTableTrait
createNgRestLogFixture() Create the NgRest Log Fixture. luya\testsuite\traits\AdminDatabaseTableTrait
createRoute() Create a route in permission system. luya\testsuite\scopes\PermissionScope
createUserFixture() Create the User Fixture with given fixture Data. luya\testsuite\traits\AdminDatabaseTableTrait
createUserOnlineFixture() Create the User Online Fixture. luya\testsuite\traits\AdminDatabaseTableTrait
denyApi() Deny the Api, which deletes the api permission entry. luya\testsuite\scopes\PermissionScope
denyRoute() Deny the route for the given user and group (removes the group assigment). luya\testsuite\scopes\PermissionScope
dropAdminAuthTable() Drop the admin auth table. luya\testsuite\traits\AdminDatabaseTableTrait
dropAdminGroupAuthTable() Drop the admin group auth table. luya\testsuite\traits\AdminDatabaseTableTrait
dropAdminUserAuthNotificationTable() Drop the admin user auth notification table. luya\testsuite\traits\AdminDatabaseTableTrait
dropAdminUserGroupTable() Drop the admin user group table. luya\testsuite\traits\AdminDatabaseTableTrait
getApp() Returns the Application instance. luya\testsuite\scopes\BaseScope
getDatabaseComponent() Returns the application database componenet. luya\testsuite\scopes\PermissionScope
loginUser() Login the the given user into the admin user system. luya\testsuite\scopes\PermissionScope
prepare() This method is called before the callback runs in order to prepare and setup the permission scope. luya\testsuite\scopes\PermissionScope
removeApi() Remove the api from permission system. luya\testsuite\scopes\PermissionScope
removePermissionApi() Remove permission api entry. luya\testsuite\traits\AdminDatabaseTableTrait
removePermissionRoute() Remove permission route luya\testsuite\traits\AdminDatabaseTableTrait
removeRoute() Remove the route from the permission system. luya\testsuite\scopes\PermissionScope
run() Run a given function inside a permission scope. luya\testsuite\scopes\BaseScope
runCallable() Run the provided callable function luya\testsuite\scopes\BaseScope
runControllerAction() Make a call to a controllers action with params and request method defintion. luya\testsuite\scopes\PermissionScope
setQueryAuthToken() Set authentification query. luya\testsuite\scopes\PermissionScope
unAssignGroupAuth() Unassigne a group from auth luya\testsuite\traits\AdminDatabaseTableTrait
updateApplicationConfig() Method to update the application config with requrired componenets. luya\testsuite\scopes\PermissionScope

Property Details

Hide inherited properties

$groupFixture protected property
protected $groupFixture null
$groupId public property

The value which should be taken to generate the group.

public integer $groupId 1
$langFixture protected property
protected $langFixture null
$ngRestLogFixture protected property
protected $ngRestLogFixture null
$userFixture protected property
protected $userFixture null
$userFixtureData public property

An array which allows you to override user data for example to make user deleted:

'userFixtureData' => [
    'is_deleted' => 1,
]

the id attribute should never be provided, as its determed by {{$userId}} property.

public array $userFixtureData = []
$userGroupId protected property
protected $userGroupId null
$userId public property

The value which should be taken to generate the user.

public integer $userId 1
$userOnlineFixture protected property
protected $userOnlineFixture null

Method Details

Hide inherited methods

__construct() public method

Defined in: luya\testsuite\scopes\BaseScope::__construct()

Permission Scope contstructor.

public void __construct ( yii\base\Application $app, callable $fn, callable $invoke null )
$app yii\base\Application
$fn callable
$invoke callable

                public function __construct(Application $app, callable $fn, callable $invoke = null)
{
    $this->_app = $app;
    $this->_invoke = $invoke;
    $this->_fn = $fn;    
}

            
addPermissionApi() public method
public integer addPermissionApi ( $id, $api, $isCrud true )
$id
$api string
$isCrud boolean
return integer

Returns the "fake" id fro the given api

                public function addPermissionApi($id, $api, $isCrud = true)
{
    $this->insertRow('admin_auth', [
        'id' => $id,
        'module_name' => '@app',
        'alias_name' => $api,
        'is_crud' => (int) $isCrud,
        'api' => $api,
    ]);
    return $id;
}

            
addPermissionRoute() public method
public integer addPermissionRoute ( $id, $route, $moduleName '@app' )
$id
$route string
$moduleName
return integer

Returns the fake id for the auth entry.

                public function addPermissionRoute($id, $route, $moduleName = '@app')
{
    $this->insertRow('admin_auth', [
        'id' => $id,
        'module_name' => $moduleName,
        'alias_name' => $route,
        'route' => $route,
    ]);
    return $id;
}

            
allowApi() public method

Assign the Api permission to the current user and group with given permissions.

public void allowApi ( $api, $canCreate true, $canUpdate true, $canDelete true )
$api string
$canCreate boolean
$canUpdate boolean
$canDelete boolean

                public function allowApi($api, $canCreate = true, $canUpdate = true, $canDelete = true)
{
    return $this->assignGroupAuth($this->groupId, strlen($api), $canCreate, $canUpdate, $canDelete);
}

            
allowRoute() public method

Allow the route for the current user and group.

public void allowRoute ( $route )
$route string

                public function allowRoute($route)
{
    return $this->assignGroupAuth($this->groupId, strlen($route));
}

            
assignGroupAuth() public method

Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::assignGroupAuth()

Assigne a group to an auth entry.

public integer assignGroupAuth ( $groupId, $authId, $canCreate true, $canUpdate true, $canDelete true )
$groupId integer
$authId integer
$canCreate boolean
$canUpdate boolean
$canDelete boolean
return integer

The number of affected rows.

                public function assignGroupAuth($groupId, $authId, $canCreate = true, $canUpdate = true, $canDelete = true)
{
    return $this->insertRow('admin_group_auth', [
        'group_id' => $groupId,
        'auth_id' => $authId,
        'crud_create' => (int) $canCreate,
        'crud_update' => (int) $canUpdate,
        'crud_delete' => (int) $canDelete,
    ]);
}

            
cleanup() public method

Clean up tables and fixtures.

public void cleanup ( )

                public function cleanup()
{
    $this->userFixture->cleanup();
    $this->groupFixture->cleanup();
    $this->userOnlineFixture->cleanup();
    $this->ngRestLogFixture->cleanup();
    $this->langFixture->cleanup();
    $this->dropAdminAuthTable();
    $this->dropAdminGroupAuthTable();
    $this->dropAdminUserGroupTable();
    $this->dropAdminUserAuthNotificationTable();
}

            
cleanupFixture() public method

Defined in: luya\testsuite\scopes\BaseScope::cleanupFixture()

A helper method to cleanup fixtures.

If the given fixture property is not an instance of ActiveRecordFixture nothing happens, otherwise the cleanup() will be run.

public void cleanupFixture ( $fixture )
$fixture mixed

                public function cleanupFixture($fixture)
{
    if ($fixture instanceof ActiveRecordFixture) {
        $fixture->cleanup();
    }
}

            
configure() public method

Defined in: luya\testsuite\scopes\BaseScope::configure()

Configured is used before prepare which allows you to configure the current scope.

public void configure ( )

                public function configure()
{
    if ($this->_invoke) {
        call_user_func_array($this->_invoke, [$this]);
    }
}

            
createAdminAuthTable() public method
public void createAdminAuthTable ( )

                public function createAdminAuthTable()
{
    $this->createTableIfNotExists('admin_auth', [
        'id' => 'INT(11) PRIMARY KEY',
        'alias_name' => 'text',
        'module_name' => 'text',
        'is_crud' => 'int(11)',
        'route' => 'text',
        'api' => 'text',
        'pool' => 'text',
    ]);
}

            
createAdminGroupAuthTable() public method
public void createAdminGroupAuthTable ( )

                public function createAdminGroupAuthTable()
{
    $this->createTableIfNotExists('admin_group_auth', [
        'id' => 'INT(11) PRIMARY KEY',
        'group_id' => 'int(11)',
        'auth_id' => 'int(11)',
        'crud_create' => 'int(11)',
        'crud_update' => 'int(11)',
        'crud_delete' => 'int(11)',
    ]);
}

            
createAdminGroupFixture() public method

Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminGroupFixture()

Create the Group Fixture with given ID.

public luya\testsuite\fixtures\NgRestModelFixture createAdminGroupFixture ( $id )
$id integer

                public function createAdminGroupFixture($id)
{
    return new NgRestModelFixture([
        'modelClass' => Group::class,
        'fixtureData' => [
            'tester' => [
                'id' => $id,
                'name' => 'Test Group',
            ],
        ],
    ]);
}

            
createAdminLangFixture() public method (available since version 1.0.21)
public luya\testsuite\fixtures\NgRestModelFixture createAdminLangFixture ( array $fixtureData = [] )
$fixtureData array

                public function createAdminLangFixture(array $fixtureData = [])
{
    return new NgRestModelFixture([
        'modelClass' => Lang::class,
        'fixtureData' => $fixtureData,
    ]);
}

            
createAdminNgRestLogFixture() public method
public luya\testsuite\fixtures\ActiveRecordFixture createAdminNgRestLogFixture ( array $fixtureData = [] )
$fixtureData

                public function createAdminNgRestLogFixture(array $fixtureData = [])
{
    return new ActiveRecordFixture([
        'modelClass' => NgrestLog::class,
        'fixtureData' => $fixtureData,
    ]);
}

            
createAdminQueueLogErrorFixture() public method
public luya\testsuite\fixtures\ActiveRecordFixture createAdminQueueLogErrorFixture ( array $fixtureData = [] )
$fixtureData array

                public function createAdminQueueLogErrorFixture(array $fixtureData = [])
{
    return new ActiveRecordFixture([
        'modelClass' => QueueLogError::class,
        'fixtureData' => $fixtureData,
    ]);
}

            
createAdminQueueLogFixture() public method
public luya\testsuite\fixtures\ActiveRecordFixture createAdminQueueLogFixture ( array $fixtureData = [] )
$fixtureData array

                public function createAdminQueueLogFixture(array $fixtureData = [])
{
    return new ActiveRecordFixture([
        'modelClass' => QueueLog::class,
        'fixtureData' => $fixtureData,
    ]);
}

            
createAdminQueueTable() public method (available since version 1.1.1)
public void createAdminQueueTable ( )

                public function createAdminQueueTable()
{
    return $this->createTableIfNotExists('admin_queue', [
        'id' => 'INT(11) PRIMARY KEY',
        'channel' => 'text',
        'job' => 'blog',
        'pushed_at' => 'int(11)',
        'ttr' => 'int(11)',
        'delay' => 'int(11)',
        'priority' => 'int(11)',
        'reserved_at' => 'int(11)',
        'attempt' => 'int(11)',
        'done_at' => 'int(11)',
    ]);
}

            
createAdminStorageFileFixture() public method (available since version 1.2.0)
public luya\testsuite\fixtures\ActiveRecordFixture createAdminStorageFileFixture ( array $fixtureData = [] )
$fixtureData array

                public function createAdminStorageFileFixture(array $fixtureData = [])
{
    return new ActiveRecordFixture([
        'modelClass' => StorageFile::class,
        'fixtureData' => $fixtureData,
    ]);
}

            
createAdminStorageFilterChainFixture() public method (available since version 1.2.0)
public luya\testsuite\fixtures\ActiveRecordFixture createAdminStorageFilterChainFixture ( array $fixtureData = [] )
$fixtureData array

                public function createAdminStorageFilterChainFixture(array $fixtureData = [])
{
    return new ActiveRecordFixture([
        'modelClass' => StorageFilterChain::class,
        'fixtureData' => $fixtureData,
    ]);
}

            
createAdminStorageFilterFixture() public method (available since version 1.2.0)
public luya\testsuite\fixtures\ActiveRecordFixture createAdminStorageFilterFixture ( array $fixtureData = [] )
$fixtureData array

                public function createAdminStorageFilterFixture(array $fixtureData = [])
{
    return new ActiveRecordFixture([
        'modelClass' => StorageFilter::class,
        'fixtureData' => $fixtureData,
    ]);
}

            
createAdminStorageImageFixture() public method (available since version 1.2.0)
public luya\testsuite\fixtures\ActiveRecordFixture createAdminStorageImageFixture ( array $fixtureData = [] )
$fixtureData array

                public function createAdminStorageImageFixture(array $fixtureData = [])
{
    return new ActiveRecordFixture([
        'modelClass' => StorageImage::class,
        'fixtureData' => $fixtureData,
    ]);
}

            
createAdminTagFixture() public method (available since version 1.0.22)
public luya\testsuite\fixtures\NgRestModelFixture createAdminTagFixture ( array $fixtureData = [] )
$fixtureData array

                public function createAdminTagFixture(array $fixtureData = [])
{
    return new NgRestModelFixture([
        'modelClass' => Tag::class,
        'fixtureData' => $fixtureData,
    ]);
}

            
createAdminTagRelationFixture() public method (available since version 1.0.22)
public luya\testsuite\fixtures\ActiveRecordFixture createAdminTagRelationFixture ( array $fixtureData = [] )
$fixtureData array

                public function createAdminTagRelationFixture(array $fixtureData = [])
{
    return new ActiveRecordFixture([
        'modelClass' => TagRelation::class,
        'fixtureData' => $fixtureData,
    ]);
}

            
createAdminUserAuthNotificationTable() public method

Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminUserAuthNotificationTable()

Create the admin user auth notification table.

public void createAdminUserAuthNotificationTable ( )

                public function createAdminUserAuthNotificationTable()
{
    $this->createTableIfNotExists('admin_user_auth_notification', [
        'id' => 'INT(11) PRIMARY KEY',
        'user_id' => 'int(11)',
        'auth_id' => 'int(11)',
        'is_muted' => 'int(11)',
        'model_latest_pk_value' => 'text',
        'model_class' => 'text',
        'created_at' => 'int(11)',
        'updated_at' => 'int(11)',
    ]);
}

            
createAdminUserDeviceFixture() public method (available since version 1.0.27)
public luya\testsuite\fixtures\ActiveRecordFixture createAdminUserDeviceFixture ( array $fixtureData = [] )
$fixtureData array

                public function createAdminUserDeviceFixture(array $fixtureData = [])
{
    return new ActiveRecordFixture([
        'modelClass' => UserDevice::class,
        'fixtureData' => $fixtureData,
    ]);
}

            
createAdminUserFixture() public method

Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminUserFixture()

Create the User Fixture with given fixture Data.

public luya\testsuite\fixtures\NgRestModelFixture createAdminUserFixture ( array $fixtureData = [], $defaultSchema true )
$fixtureData array
$defaultSchema

                public function createAdminUserFixture(array $fixtureData = [], $defaultSchema = true)
{
    return new NgRestModelFixture([
        'modelClass' => User::class,
        'schema' => $defaultSchema ? [
            'title' => 'int(11)',
            'firstname' => 'text',
            'lastname' => 'text',
            'email' => 'text',
            'is_deleted' => 'int(11)',
            'is_api_user' => 'boolean',
            'api_last_activity' => 'int(11)',
            'auth_token' => 'text',
            'is_request_logger_enabled' => 'boolean',
            'email_verification_token_timestamp' => 'int(11)',
            'login_attempt_lock_expiration' => 'int(11)',
            'login_attempt' => 'int(11)',
            'email_verification_token' => 'text',
            'api_allowed_ips' => 'text',
            'api_rate_limit' => 'int(11)',
            'cookie_token' => 'text',
            'settings' => 'text',
            'force_reload' => 'int(11)',
            'secure_token_timestamp' => 'int(11)',
            'secure_token' => 'text',
            'password' => 'text',
            'password_salt' => 'text',
            'login_2fa_enabled' => 'int(11)',
            'login_2fa_secret' => 'text',
            'login_2fa_backup_key' => 'text',
            'password_verification_token' => 'text',
            'password_verification_token_timestamp' => 'int(11)',
        ] : null,
        'fixtureData' => $fixtureData,
    ]);
}

            
createAdminUserGroupTable() public method
public void createAdminUserGroupTable ( )

                public function createAdminUserGroupTable()
{
    $this->createTableIfNotExists('admin_user_group', [
        'id' => 'INT(11) PRIMARY KEY',
        'user_id' => 'int(11)',
        'group_id' => 'int(11)',
    ]);
}

            
createAdminUserLoginFixture() public method (available since version 1.0.27)
public luya\testsuite\fixtures\NgRestModelFixture createAdminUserLoginFixture ( array $fixtureData = [] )
$fixtureData array

                public function createAdminUserLoginFixture(array $fixtureData = [])
{
    return new NgRestModelFixture([
        'modelClass' => UserLogin::class,
        'fixtureData' => $fixtureData,
    ]);
}

            
createAdminUserLoginLockoutFixture() public method (available since version 1.0.27)
public luya\testsuite\fixtures\ActiveRecordFixture createAdminUserLoginLockoutFixture ( array $fixtureData = [] )
$fixtureData array

                public function createAdminUserLoginLockoutFixture(array $fixtureData = [])
{
    return new ActiveRecordFixture([
        'modelClass' => UserLoginLockout::class,
        'fixtureData' => $fixtureData,
    ]);
}

            
createAdminUserOnlineFixture() public method
public luya\testsuite\fixtures\ActiveRecordFixture createAdminUserOnlineFixture ( array $fixtureData = [] )
$fixtureData

                public function createAdminUserOnlineFixture(array $fixtureData = [])
{
    return new ActiveRecordFixture([
        'modelClass' => UserOnline::class,
        'fixtureData' => $fixtureData,
    ]);
}

            
createAndAllowApi() public method

Create the Api in the permission system and directly allow the api with given permissions.

public void createAndAllowApi ( $api, $canCreate true, $canUpdate true, $canDelete true )
$api string
$canCreate boolean
$canUpdate boolean
$canDelete boolean

                public function createAndAllowApi($api, $canCreate = true, $canUpdate = true, $canDelete = true)
{
    $this->createApi($api);
    $this->allowApi($api, $canCreate, $canUpdate, $canDelete);
}

            
createAndAllowRoute() public method

Create a route in permission system and directly allow the route.

Read more about details of the route and how its build in {{createRoute()}} method.

public void createAndAllowRoute ( $route )
$route string

                public function createAndAllowRoute($route)
{
    $this->createRoute($route);
    $this->allowRoute($route);
}

            
createApi() public method

Create an Api in permission system. (ActiveRestController).

public integer createApi ( $api )
$api string
return integer

Returns the id of the admin_auth table entry.

                public function createApi($api)
{
    return $this->addPermissionApi(strlen($api), $api, true);
}

            
createGroupFixture() public method
Deprecated since version 1.0.27:

Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createGroupFixture()

Create the Group Fixture with given ID.

public luya\testsuite\fixtures\NgRestModelFixture createGroupFixture ( $id )
$id integer

                public function createGroupFixture($id)
{
    trigger_error('use createAdminGroupFixture() instead', E_USER_DEPRECATED);
    return $this->createAdminGroupFixture($id);
}

            
createNgRestLogFixture() public method
Deprecated since version 1.0.27:
public luya\testsuite\fixtures\ActiveRecordFixture createNgRestLogFixture ( array $fixtureData = [] )
$fixtureData

                public function createNgRestLogFixture(array $fixtureData = [])
{
    trigger_error('use createAdminNgRestLogFixture() instead', E_USER_DEPRECATED);
    return $this->createAdminNgRestLogFixture($fixtureData);
}

            
createRoute() public method

Create a route in permission system.

Assuming the application has id "myapp" (['id' => 'myapp']) and controller defined is mycontroller and the action is helloworld the route would be myapp/mycontroller/helloworld.

$app = new Application(['id' => 'myapp']);

PermissionScope::run($app, function($scope) use ($app) {

   $controller = new MyController('mycontroller', $app);

   $scope->runControllerAction($controller, 'helloworld');
});

The above example would nee to set:

$scope->createRoute('myapp/mycontroller/helloworld');
public integer createRoute ( $route )
$route string
return integer

Returns the id of the admin_auth table entry.

                public function createRoute($route)
{
    return $this->addPermissionRoute(strlen($route), $route);
}

            
createUserFixture() public method
Deprecated since version 1.0.27:

Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createUserFixture()

Create the User Fixture with given fixture Data.

public luya\testsuite\fixtures\NgRestModelFixture createUserFixture ( array $fixtureData = [] )
$fixtureData array

                public function createUserFixture(array $fixtureData = [])
{
    trigger_error('use createAdminUserFixture() instead', E_USER_DEPRECATED);
    return $this->createAdminUserFixture($fixtureData);
}

            
createUserOnlineFixture() public method
Deprecated since version 1.0.27:
public luya\testsuite\fixtures\ActiveRecordFixture createUserOnlineFixture ( array $fixtureData = [] )
$fixtureData

                public function createUserOnlineFixture(array $fixtureData = [])
{
    trigger_error('use createAdminUserOnlineFixture() instead', E_USER_DEPRECATED);
    return $this->createAdminUserOnlineFixture($fixtureData);
}

            
denyApi() public method

Deny the Api, which deletes the api permission entry.

public void denyApi ( $api )
$api string

                public function denyApi($api)
{
    return $this->unAssignGroupAuth($this->groupId, strlen($api));
}

            
denyRoute() public method

Deny the route for the given user and group (removes the group assigment).

public void denyRoute ( $route )
$route string

                public function denyRoute($route)
{
    return $this->unAssignGroupAuth($this->groupId, strlen($route));
}

            
dropAdminAuthTable() public method
public void dropAdminAuthTable ( )

                public function dropAdminAuthTable()
{
    $this->dropTableIfExists('admin_auth');
}

            
dropAdminGroupAuthTable() public method
public void dropAdminGroupAuthTable ( )

                public function dropAdminGroupAuthTable()
{
    $this->dropTableIfExists('admin_group_auth');
}    

            
dropAdminUserAuthNotificationTable() public method

Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::dropAdminUserAuthNotificationTable()

Drop the admin user auth notification table.

public void dropAdminUserAuthNotificationTable ( )

                public function dropAdminUserAuthNotificationTable()
{
    $this->dropTableIfExists('admin_user_auth_notification');
}

            
dropAdminUserGroupTable() public method
public void dropAdminUserGroupTable ( )

                public function dropAdminUserGroupTable()
{
    $this->dropTableIfExists('admin_user_group');   
}

            
getApp() public method

Defined in: luya\testsuite\scopes\BaseScope::getApp()

Returns the Application instance.

Getter method to access the private object.

public yii\base\Application getApp ( )

                public function getApp()
{
    return $this->_app;
}

            
getDatabaseComponent() public method

Returns the application database componenet.

public yii\db\Connection getDatabaseComponent ( )

                public function getDatabaseComponent()
{
    return $this->getApp()->db;
}

            
loginUser() public method

Login the the given user into the admin user system.

this is only used for WEB controller request with session based auth.

public boolean loginUser ( )
return boolean

Whether login was successfull or not.

                public function loginUser()
{
    return $this->getApp()->adminuser->login($this->userFixture->getModel('user'));
}

            
prepare() public method

This method is called before the callback runs in order to prepare and setup the permission scope.

public void prepare ( )

                public function prepare()
{
    // ensure the given and required application components are available.
    $this->updateApplicationConfig();
    $this->userFixture = $this->createAdminUserFixture([
        'user' => array_merge([
            'id' => $this->userId,
            'firstname' => 'John',
            'lastname' => 'Doe',
            'email' => 'john@example.com',
            'is_deleted' => 0,
            'is_api_user' => 1,
            'api_last_activity' => 12345678,
            'auth_token' => 'TestAuthToken',
            'is_request_logger_enabled' => false,
        ], $this->userFixtureData),
    ]);
    $this->groupFixture = $this->createAdminGroupFixture($this->groupId);
    $this->userOnlineFixture = $this->createAdminUserOnlineFixture();
    $this->ngRestLogFixture = $this->createAdminNgRestLogFixture();
    $this->langFixture = $this->createAdminLangFixture([
        1 => [
            'id' => 1,
            'short_code' => 'en',
            'name' => 'English',
            'is_default' => 1,
            'is_deleted' => 0,
        ]
    ]);
    $this->createAdminUserGroupTable();
    $this->createAdminGroupAuthTable();
    $this->createAdminAuthTable();
    $this->createAdminUserAuthNotificationTable();
    $this->createAdminUserLoginLockoutFixture();
    $this->userGroupId = $this->insertRow('admin_user_group', [
        'user_id' => $this->userId,
        'group_id' => $this->groupId,
    ]);
}

            
removeApi() public method

Remove the api from permission system.

public void removeApi ( $api )
$api string

                public function removeApi($api)
{
    return $this->removePermissionApi($api);
}

            
removePermissionApi() public method
public integer removePermissionApi ( $api )
$api string
return integer

The number of affected rows.

                public function removePermissionApi($api)
{
    return $this->deleteRow('admin_auth', [
        'api' => $api,
    ]);
}

            
removePermissionRoute() public method
public integer removePermissionRoute ( $route )
$route string
return integer

The number of affected rows.

                public function removePermissionRoute($route)
{
    return $this->deleteRow('admin_auth', [
        'route' => $route,
    ]);
}

            
removeRoute() public method

Remove the route from the permission system.

public void removeRoute ( $route )
$route string

                public function removeRoute($route)
{
    return $this->removePermissionRoute($route);
}

            
run() public static method

Defined in: luya\testsuite\scopes\BaseScope::run()

Run a given function inside a permission scope.

public static mixed run ( yii\base\Application $app, callable $fn, callable $invoke null )
$app \luya\testsuite\scopes\yii\base\Application
$fn callable

The function to run.

$invoke callable

The function to configure the scope.

                public static function run(Application $app, callable $fn, callable $invoke = null)
{
    $scope = new static($app, $fn, $invoke);
    $scope->configure();
    $scope->prepare();
    $response = $scope->runCallable($scope);
    $scope->cleanup();
    return $response;
}

            
runCallable() public method

Defined in: luya\testsuite\scopes\BaseScope::runCallable()

Run the provided callable function

public mixed runCallable ( luya\testsuite\scopes\BaseScope $scope )
$scope luya\testsuite\scopes\PermissionScope

                public function runCallable(BaseScope $scope)
{
    return call_user_func_array($this->_fn, [$scope]);
}

            
runControllerAction() public method

Make a call to a controllers action with params and request method defintion.

public mixed runControllerAction ( yii\base\Controller $controller, $action, array $params = [], $method 'GET' )
$controller yii\base\Controller
$action string
$params array
$method string

GET, POST, HEAD, PUT, PATCH, DELETE

                public function runControllerAction(Controller $controller, $action, array $params = [], $method = 'GET')
{
    $_SERVER['REQUEST_METHOD'] = strtoupper($method);
    $this->getApp()->controller = $controller;
    if ($controller instanceof RestController) {
        $this->setQueryAuthToken();
    } else {
        $this->loginUser();
        $controller->enableCsrfValidation = false;
    }
    
    return $controller->runAction($action, $params);
}

            
setQueryAuthToken() public method

Set authentification query.

public void setQueryAuthToken ( $value true, $token null )
$value boolean
$token string

                public function setQueryAuthToken($value = true, $token = null)
{
    if ($value) {
        $accessToken = $token ? $token : $this->userFixture->getModel('user')->auth_token;
        $this->getApp()->request->setQueryParams(array_merge(['access-token' => $accessToken], $this->getApp()->request->getQueryParams()));
    } else {
        $this->getApp()->request->setQueryParams(array_merge(['access-token' => null], $this->getApp()->request->getQueryParams()));
    }
}

            
unAssignGroupAuth() public method
public integer unAssignGroupAuth ( $groupId, $authId )
$groupId integer
$authId integer
return integer

The number of affected rows.

                public function unAssignGroupAuth($groupId, $authId)
{
    return $this->deleteRow('admin_group_auth', [
        'group_id' => $groupId,
        'auth_id' => $authId,
    ]);
}

            
updateApplicationConfig() public method

Method to update the application config with requrired componenets.

public void updateApplicationConfig ( )

                public function updateApplicationConfig()
{
    $this->getApp()->set('session',['class' => 'luya\testsuite\components\DummySession']);
    $this->getApp()->set('cache', ['class' => 'yii\caching\DummyCache']);
    $this->getApp()->set('adminuser', ['class' => 'luya\admin\components\AdminUser', 'enableSession' => false]);
    $this->getApp()->set('db', ['class' => 'yii\db\Connection', 'dsn' => 'sqlite::memory:']);
}