Completed
Pull Request — master (#157)
by
unknown
01:46
created

RoleTest::testRoleCreateDeleteNew()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
dl 22
loc 22
rs 9.2
c 0
b 0
f 0
cc 1
eloc 15
nc 1
nop 0
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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