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

UserTest::testUserCreateBlockedByWrapper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
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 user entities.
12
 *
13
 * @group driver
14
 */
15
class UserTest extends DriverEntityKernelTestBase
16
{
17
18
  /**
19
   * Machine name of the entity type being tested.
20
   *
21
   * @string
22
   */
23
    protected $entityType = 'user';
24
25
  /**
26
   * @inheritdoc
27
   */
28
    public function setUp()
29
    {
30
        parent::setUp();
31
        $this->installSchema('user', 'users_data');
32
    }
33
34
  /**
35
   * Test that a user can be created and deleted.
36
   */
37 View Code Duplication
    public function testUserCreateDelete()
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...
38
    {
39
        $name = $this->randomString();
40
        $user = (object) [
41
        'name' => $name,
42
        ];
43
        $user = $this->driver->userCreate($user);
0 ignored issues
show
Unused Code introduced by
$user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
44
45
        $entities = $this->storage->loadByProperties(['name' => $name]);
46
        $this->assertEquals(1, count($entities));
47
48
        // Status should be set to 1 by default.
49
        $entity = reset($entities);
50
        $this->assertEquals(1, $entity->status->value);
51
52
        // Looks like we forget to return the user object from userCreate,
53
        // so none of the code below works. But then how does userDelete ever work?
54
55
    /*    // Check the id of the new user has been added to the returned object.
56
        $entity = reset($entities);
57
        $this->assertEquals($entity->id(), $user->uid);
58
59
        // Check the node can be deleted.
60
        $this->driver->userDelete($user);
61
        $entities = $this->storage->loadByProperties(['name' => $name]);
62
        $this->assertEquals(0, count($entities));*/
63
    }
64
65
  /**
66
   * Test that a blocked user can be created.
67
   */
68 View Code Duplication
    public function testUserCreateBlocked()
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...
69
    {
70
        $name = $this->randomString();
71
        $user = (object) [
72
        'name' => $name,
73
        'status' => 0,
74
        ];
75
        $user = $this->driver->userCreate($user);
0 ignored issues
show
Unused Code introduced by
$user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
76
77
        $entities = $this->storage->loadByProperties(['name' => $name]);
78
        $this->assertEquals(1, count($entities));
79
80
        // Status should be set to 0 as explicitly specified.
81
        $entity = reset($entities);
82
        $this->assertEquals(0, $entity->status->value);
83
    }
84
85
  /**
86
   * Test that a user can be given a role, using role label or machine name.
87
   */
88
    public function testUserAddRole()
89
    {
90
        $role1Id = $this->randomMachineName();
91
        $role1Label = $this->randomString();
92
        $role2Id = $this->randomMachineName();
93
        $role2Label = $this->randomString();
94
        $role3Id = $this->randomMachineName();
95
        $role3Label = $this->randomString();
96
97
        $role1 = Role::create(['id' => $role1Id, 'label' => $role1Label]);
98
        $role2 = Role::create(['id' => $role2Id, 'label' => $role2Label]);
99
        $role3 = Role::create(['id' => $role3Id, 'label' => $role3Label]);
100
        $role1->save();
101
        $role2->save();
102
        $role3->save();
103
104
        $user = $this->createUser();
105
        $userSimplified = (object) [
106
        'uid' => $user->id(),
107
        ];
108
109
        $this->driver->userAddRole($userSimplified, $role1Id);
110
        $this->driver->userAddRole($userSimplified, $role2Label);
111
        $user = $this->reloadEntity($user);
112
113
        // Check role detection is working.
114
        $this->assertFalse($user->hasRole($role3Id));
115
116
        // Check user roles whether specified by machine name or label.
117
        $this->assertTrue($user->hasRole($role1Id));
118
        $this->assertTrue($user->hasRole($role2Id));
119
    }
120
121
  /**
122
   * Test that a user can be created and deleted.
123
   */
124
    public function testUserCreateDeleteByWrapper()
125
    {
126
        $name = $this->randomString();
127
        $fields = [
128
        'name' => $name,
129
        ];
130
        $user = DriverEntityDrupal8::create($fields, $this->entityType)->save();
131
132
        $entities = $this->storage->loadByProperties(['name' => $name]);
133
        $this->assertEquals(1, count($entities));
134
135
        // Status should be set to 1 by default.
136
        $entity = reset($entities);
137
        $this->assertEquals(1, $entity->status->value);
138
139
        // Check the id of the new user has been added to the returned object.
140
        $entity = reset($entities);
141
        $this->assertEquals($entity->id(), $user->uid);
0 ignored issues
show
Documentation introduced by
The property uid does not exist on object<Drupal\Driver\Wra...ntity\DriverEntityBase>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
142
143
        // Check the node can be deleted.
144
        $user->delete();
145
        $entities = $this->storage->loadByProperties(['name' => $name]);
146
        $this->assertEquals(0, count($entities));
147
    }
148
149
  /**
150
   * Test that a blocked user can be created.
151
   */
152 View Code Duplication
    public function testUserCreateBlockedByWrapper()
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...
153
    {
154
        $name = $this->randomString();
155
        $fields = [
156
        'name' => $name,
157
        'status' => 0,
158
        ];
159
        $user = DriverEntityDrupal8::create($fields, $this->entityType)->save();
0 ignored issues
show
Unused Code introduced by
$user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
160
161
        $entities = $this->storage->loadByProperties(['name' => $name]);
162
        $this->assertEquals(1, count($entities));
163
164
        // Status should be set to 0 as explicitly specified.
165
        $entity = reset($entities);
166
        $this->assertEquals(0, $entity->status->value);
167
    }
168
169
  /**
170
   * Test that a user can be given a role, using role label or machine name.
171
   */
172
    public function testUserAddRoleByWrapper()
173
    {
174
        $role1Id = $this->randomMachineName();
175
        $role1Label = $this->randomString();
176
        $role2Id = $this->randomMachineName();
177
        $role2Label = $this->randomString();
178
        $role3Id = $this->randomMachineName();
179
        $role3Label = $this->randomString();
180
181
        $role1 = Role::create(['id' => $role1Id, 'label' => $role1Label]);
182
        $role2 = Role::create(['id' => $role2Id, 'label' => $role2Label]);
183
        $role3 = Role::create(['id' => $role3Id, 'label' => $role3Label]);
184
        $role1->save();
185
        $role2->save();
186
        $role3->save();
187
188
        $user = $this->createUser();
189
        $userWrapped = new DriverEntityDrupal8('user');
190
        $userWrapped->load($user->id());
191
        $userWrapped->addRole($role1Id);
0 ignored issues
show
Documentation Bug introduced by
The method addRole does not exist on object<Drupal\Driver\Wra...ty\DriverEntityDrupal8>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
192
        $userWrapped->addRole($role2Label);
0 ignored issues
show
Documentation Bug introduced by
The method addRole does not exist on object<Drupal\Driver\Wra...ty\DriverEntityDrupal8>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
193
        $userWrapped->save();
194
        $user = $this->reloadEntity($user);
195
196
        // Check role detection is working.
197
        $this->assertFalse($user->hasRole($role3Id));
198
199
        // Check user roles whether specified by machine name or label.
200
        $this->assertTrue($user->hasRole($role1Id));
201
        $this->assertTrue($user->hasRole($role2Id));
202
    }
203
}
204