|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\Tests\Driver\Kernel\Drupal8\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Tests\Driver\Kernel\Drupal8\Entity\DriverEntityKernelTestBase; |
|
6
|
|
|
use Drupal\user\Entity\User; |
|
7
|
|
|
use Drupal\user\Entity\Role; |
|
8
|
|
|
use Drupal\Driver\Wrapper\Entity\DriverEntityDrupal8; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Tests the driver's handling of role entities. |
|
12
|
|
|
* |
|
13
|
|
|
* @group driver |
|
14
|
|
|
*/ |
|
15
|
|
|
class RoleTest extends DriverEntityKernelTestBase |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Machine name of the entity type being tested. |
|
20
|
|
|
* |
|
21
|
|
|
* @string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $entityType = 'user_role'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Our entity is a config entity. |
|
27
|
|
|
* |
|
28
|
|
|
* @boolean |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $config = true; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Test that a role can be created and deleted. |
|
34
|
|
|
*/ |
|
35
|
|
|
public function testRoleCreateDelete() |
|
36
|
|
|
{ |
|
37
|
|
|
|
|
38
|
|
|
$permissions = [ |
|
39
|
|
|
'view the administration theme', |
|
40
|
|
|
]; |
|
41
|
|
|
$roleName = $this->driver->roleCreate($permissions); |
|
42
|
|
|
$role = Role::load($roleName); |
|
43
|
|
|
$this->assertNotNull($role); |
|
44
|
|
|
$this->assertEquals($permissions, $role->getPermissions()); |
|
45
|
|
|
|
|
46
|
|
|
// Check the role can be deleted. |
|
47
|
|
|
$this->driver->roleDelete($roleName); |
|
48
|
|
|
$role = Role::load($roleName); |
|
49
|
|
|
$this->assertNull($role); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Test that a role can be created and deleted. |
|
54
|
|
|
*/ |
|
55
|
|
View Code Duplication |
public function testRoleCreateDeleteNew() |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
|
|
$name = $this->randomMachineName(); |
|
58
|
|
|
$permissions = [ |
|
59
|
|
|
'view the administration theme', |
|
60
|
|
|
]; |
|
61
|
|
|
$entity = new DriverEntityDrupal8( |
|
62
|
|
|
$this->entityType |
|
63
|
|
|
); |
|
64
|
|
|
$entity->set('id', $name); |
|
65
|
|
|
$entity->set('permissions', $permissions); |
|
66
|
|
|
$entity->save(); |
|
67
|
|
|
|
|
68
|
|
|
$role = Role::load($name); |
|
69
|
|
|
$this->assertNotNull($role); |
|
70
|
|
|
$this->assertEquals($permissions, $role->getPermissions()); |
|
71
|
|
|
|
|
72
|
|
|
// Check the role can be deleted. |
|
73
|
|
|
$entity->delete(); |
|
74
|
|
|
$role = Role::load($name); |
|
75
|
|
|
$this->assertNull($role); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Test that an exception is thrown if config property is missing. |
|
80
|
|
|
*/ |
|
81
|
|
View Code Duplication |
public function testMissingConfigProperty() |
|
|
|
|
|
|
82
|
|
|
{ |
|
83
|
|
|
$name = $this->randomString(); |
|
84
|
|
|
$entity = new DriverEntityDrupal8( |
|
85
|
|
|
$this->entityType |
|
86
|
|
|
); |
|
87
|
|
|
$this->setExpectedException(\Exception::class, "Field or property cannot be identified"); |
|
88
|
|
|
$entity->set('nonexistentproperty', $name); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.