Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create integration test for AdminRoles component and update README wi… #31

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Model/Component/AdminRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function __construct(
/**
* @param $data
*/
protected function processData($data = null)
public function processData($data = null)
{

if (isset($data['adminroles'])) {
Expand Down Expand Up @@ -85,7 +85,7 @@ private function createAdminRole($roleName, $resources)
sprintf('Admin Role "%s" creation skipped: Already exists in database', $roleName)
);

//Get exisiting Role
//Get existing Role
$role = $role->getCollection()->addFieldToFilter('role_name', $roleName)->getFirstItem();
$this->setResourceIds($role, $resources);

Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ php vendor/bin/phpcpd vendor/ctidigital/magento2-configurator/Model/ vendor/ctid
php vendor/bin/phpunit --coverage-clover build/logs/clover.xml vendor/ctidigital/magento2-configurator/Test/Unit/
```

## Integration tests
- Configure your [Magento integration test environment](http://devdocs.magento.com/guides/v2.0/test/integration/integration_test_setup.html).
- Add the XML below to dev/tests/integration/phpunit.xml.dist

````
<testsuite name="magento2-configurator">
<directory>../../../vendor/ctidigital/magento2-configurator/Test/Integration</directory>
</testsuite>
````

- You can run the tests from the correct place on the command line

````
/dev/tests/integration$ ../../../vendor/bin/phpunit --testsuite "magento2-configurator"
````

- You can also add the magento PHP developer tools to your path, so that you do not have to specify location of phpunit
````
export PATH=$PATH:/var/www/magento2/vendor/bin
````
## Unit tests
If you're developing a new component, please ensure you have your corresponding unit test which extends `ComponentAbstractTestCase` as that will test that your component has the required functions.
Do also include sample files with your component that works

Expand Down
1 change: 1 addition & 0 deletions Samples/Components/AdminRoles/adminroles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ adminroles:
- name: "Tax Manager"
resources:
- 'Magento_Backend::dashboard'
- 'Magento_Backend::admin'
- 'Magento_Sales::sales'
- 'Magento_Sales::sales_operation'
- 'Magento_Sales::sales_order'
Expand Down
143 changes: 143 additions & 0 deletions Test/Integration/AdminRolesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

namespace CtiDigital\Configurator\Model\Component;

use Magento\Authorization\Model\Role;
use Magento\TestFramework\Helper\Bootstrap;
use Symfony\Component\Yaml\Parser;

class AdminRolesTest extends \PHPUnit_Framework_TestCase
{
const ROLE_NAME_KEY = "role_name";
const TAX_MANAGER_TEST_ROLE_NAME = "Tax Manager";
const ROLE_ID_KEY = "role_id";
const ROLE_NAMES_DIFFERENT_MESSAGE = "Actual role name was different from expected role name";
const RESOURCES_DIFFERENT_MESSAGE = "Actual resources were different to expected resources";
const SALES_USERS_TEST_ROLE_NAME = "Sales Users";

private $adminRolesYamlPath;

private $expectedAllowedTaxManagerResources = array(
'Magento_Backend::dashboard',
'Magento_Backend::admin',
'Magento_Sales::sales',
'Magento_Sales::sales_operation',
'Magento_Sales::sales_order',
'Magento_Sales::actions',
'Magento_Sales::create',
'Magento_Sales::actions_view',
'Magento_Sales::email',
'Magento_Sales::reorder'
);

private $expectedAllowedSalesUsersResources = array(
'Magento_Backend::dashboard',
'Magento_Backend::admin',
'Magento_Sales::sales',
'Magento_Sales::create',
'Magento_Sales::actions_view',
'Magento_Sales::actions_edit',
'Magento_Sales::cancel',
'Magento_Sales::hold',
'Magento_Sales::unhold'
);

/**
* AdminRoles is the class under test
*
* @var AdminRoles
*/
private $adminRolesComponent;

/**
* Role resource model
*
* @var Role
*/
private $roleResourceModel;

/**
* Rules resource model
*
* @var
*/
private $rulesResourceModel;

public function setUp()
{
$this->adminRolesYamlPath = sprintf("%s/../../Samples/Components/AdminRoles/adminroles.yaml", __DIR__);
$this->adminRolesComponent = Bootstrap::getObjectManager()
->get('CtiDigital\Configurator\Model\Component\AdminRoles');

$roleFactory = Bootstrap::getObjectManager()
->get('Magento\Authorization\Model\RoleFactory');

$rulesFactory = Bootstrap::getObjectManager()
->get('Magento\Authorization\Model\RulesFactory');

$this->rulesResourceModel = $rulesFactory->create();
$this->roleResourceModel = $roleFactory->create();
}

public function testShouldCreateNewAdminRolesFromYamlFile()
{
// given a yaml file containing AdminRoles
$yamlParser = new Parser();
$testAdminRoles = $yamlParser->parse(file_get_contents($this->adminRolesYamlPath), true);

// when we run the AdminRoles component
$this->adminRolesComponent->processData($testAdminRoles);

// then it should generate new admin roles in the database, with associated resources
$actualTaxManagerRole = $this->getRoleByName(self::TAX_MANAGER_TEST_ROLE_NAME);
$this->assertNotEmpty($actualTaxManagerRole);

$actualSalesUsersRole = $this->getRoleByName(self::SALES_USERS_TEST_ROLE_NAME);
$this->assertNotEmpty($actualSalesUsersRole);

$actualTaxManagerResources = $this->getResourceNamesFromRoleId($actualTaxManagerRole[self::ROLE_ID_KEY]);
$this->assertResourceNamesArePresent(
$this->expectedAllowedTaxManagerResources,
$actualTaxManagerResources
);

$actualSalesUsersResources = $this->getResourceNamesFromRoleId($actualSalesUsersRole[self::ROLE_ID_KEY]);
$this->assertResourceNamesArePresent(
$this->expectedAllowedSalesUsersResources,
$actualSalesUsersResources
);
}

public function assertResourceNamesArePresent($expectedAllowedResources, $actualAllowedResources)
{
$this->assertEquals(
sort($expectedAllowedResources),
sort($actualAllowedResources),
self::RESOURCES_DIFFERENT_MESSAGE
);
}
private function getRoleByName($roleName)
{
$actualTaxManagerRole = $this->roleResourceModel->getCollection()
->addFieldToFilter(self::ROLE_NAME_KEY, $roleName)
->getFirstItem();
return $actualTaxManagerRole;
}

private function getResourceNamesFromRoleId($roleId)
{
$resources = $this->rulesResourceModel->getCollection()
->addFieldToFilter(self::ROLE_ID_KEY, $roleId)
->addFieldToFilter("permission", "allow");
return $this->extractResourceNames($resources->getData());
}

private function extractResourceNames(array $resources)
{
$resourceNames = array();
foreach ($resources as $resource) {
$resourceNames[] = $resource['resource_id'];
}
return $resourceNames;
}
}