/
PermissionsContext.php
97 lines (84 loc) · 2.71 KB
/
PermissionsContext.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
namespace Pim\Behat\Context\Domain\System;
use Context\Spin\SpinCapableTrait;
use PHPUnit\Framework\Assert;
use Pim\Behat\Context\PimContext;
/**
* This context regroups all methods related to a permission section
*/
class PermissionsContext extends PimContext
{
use SpinCapableTrait;
/**
* @param string $action
* @param string $api
* @param string $type
* @param string $acls
*
* @throws \InvalidArgumentException If $action is not a defined method
*
* @When /^I (grant|revoke) rights to( API)? (resources?|groups?) (.*)$/
*/
public function iSetRightsToACL($action, $api, $type, $acls)
{
if (false !== strpos($type, 'resource')) {
$type = 'Resource';
} elseif (false !== strpos($type, 'group')) {
$type = 'Group';
}
$element = $api ? 'API permission' : 'Permission';
$permissionElement = $this->getElementOnCurrentPage($element);
switch ($action) {
case 'grant':
$method = 'grant' . $type;
break;
case 'revoke':
$method = 'revoke' . $type;
break;
default:
throw new \InvalidArgumentException(sprintf('Action "%s" does not exist.', $action));
break;
}
foreach ($this->listToArray($acls) as $acl) {
$permissionElement->$method($acl);
}
}
/**
* @When /^I grant all rights$/
*/
public function iGrantAllRightsToACLResources()
{
$iconSelector = '.acl-permission .acl-permission-toggle.non-granted';
$this->getSession()->executeScript(
sprintf('$("%s").each(function () { $(this).click(); });', $iconSelector)
);
}
/**
* @param string $api
* @param string $acls
* @param string $action
*
* @throws \InvalidArgumentException If $action is not a defined method
*
* @When /^I should see( API)? resources? (.*) (granted|revoked)$/
*/
public function iShouldSeeRightsOnACL($api, $acls, $action)
{
$element = $api ? 'API permission' : 'Permission';
$permissionElement = $this->getElementOnCurrentPage($element);
switch ($action) {
case 'granted':
$method = 'isGrantedResource';
break;
case 'revoked':
$method = 'isRevokedResource';
break;
default:
throw new \InvalidArgumentException(sprintf('Action "%s" does not exist.', $action));
break;
}
foreach ($this->listToArray($acls) as $acl) {
Assert::assertTrue($permissionElement->$method($acl));
}
}
}