Completed
Pull Request — master (#157)
by
unknown
12:36
created

UserTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Drupal\Tests\Driver\Kernel\Drupal8\Entity;
4
5
use Drupal\user\Entity\Role;
6
use Drupal\Driver\Wrapper\Entity\DriverEntityDrupal8;
7
8
/**
9
 * Tests the driver's handling of user entities.
10
 *
11
 * @group driver
12
 */
13
class UserTest extends DriverEntityKernelTestBase {
14
15
  /**
16
   * Machine name of the entity type being tested.
17
   *
18
   * @var string
19
   */
20
  protected $entityType = 'user';
21
22
  /**
23
   * {@inheritdoc}
24
   */
25
  public function setUp() {
26
    parent::setUp();
27
    $this->installSchema('user', 'users_data');
28
  }
29
30
  /**
31
   * Test that a user can be created and deleted.
32
   */
33 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...
34
    $name = $this->randomString();
35
    $user = (object) [
36
      'name' => $name,
37
    ];
38
    $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...
39
40
    $entities = $this->storage->loadByProperties(['name' => $name]);
41
    $this->assertEquals(1, count($entities));
42
43
    // Status should be set to 1 by default.
44
    $entity = reset($entities);
45
    $this->assertEquals(1, $entity->status->value);
46
47
    // Looks like we forget to return the user object from userCreate,
48
    // so none of the code below works. But then how does userDelete ever work?
49
    /*    // Check the id of the new user has been added to the returned object.
50
    $entity = reset($entities);
51
    $this->assertEquals($entity->id(), $user->uid);
52
53
    // Check the node can be deleted.
54
    $this->driver->userDelete($user);
55
    $entities = $this->storage->loadByProperties(['name' => $name]);
56
    $this->assertEquals(0, count($entities));*/
57
  }
58
59
  /**
60
   * Test that a blocked user can be created.
61
   */
62 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...
63
    $name = $this->randomString();
64
    $user = (object) [
65
      'name' => $name,
66
      'status' => 0,
67
    ];
68
    $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...
69
70
    $entities = $this->storage->loadByProperties(['name' => $name]);
71
    $this->assertEquals(1, count($entities));
72
73
    // Status should be set to 0 as explicitly specified.
74
    $entity = reset($entities);
75
    $this->assertEquals(0, $entity->status->value);
76
  }
77
78
  /**
79
   * Test that a user can be given a role, using role label or machine name.
80
   */
81
  public function testUserAddRole() {
82
    $role1Id = $this->randomMachineName();
83
    $role1Label = $this->randomString();
84
    $role2Id = $this->randomMachineName();
85
    $role2Label = $this->randomString();
86
    $role3Id = $this->randomMachineName();
87
    $role3Label = $this->randomString();
88
89
    $role1 = Role::create(['id' => $role1Id, 'label' => $role1Label]);
90
    $role2 = Role::create(['id' => $role2Id, 'label' => $role2Label]);
91
    $role3 = Role::create(['id' => $role3Id, 'label' => $role3Label]);
92
    $role1->save();
93
    $role2->save();
94
    $role3->save();
95
96
    $user = $this->createUser();
97
    $userSimplified = (object) [
98
      'uid' => $user->id(),
99
    ];
100
101
    $this->driver->userAddRole($userSimplified, $role1Id);
102
    $this->driver->userAddRole($userSimplified, $role2Label);
103
    $user = $this->reloadEntity($user);
104
105
    // Check role detection is working.
106
    $this->assertFalse($user->hasRole($role3Id));
107
108
    // Check user roles whether specified by machine name or label.
109
    $this->assertTrue($user->hasRole($role1Id));
110
    $this->assertTrue($user->hasRole($role2Id));
111
  }
112
113
  /**
114
   * Test that a user can be created and deleted.
115
   */
116
  public function testUserCreateDeleteByWrapper() {
117
    $name = $this->randomString();
118
    $fields = [
119
      'name' => $name,
120
    ];
121
    $user = DriverEntityDrupal8::create($fields, $this->entityType)->save();
122
123
    $entities = $this->storage->loadByProperties(['name' => $name]);
124
    $this->assertEquals(1, count($entities));
125
126
    // Status should be set to 1 by default.
127
    $entity = reset($entities);
128
    $this->assertEquals(1, $entity->status->value);
129
130
    // Check the id of the new user has been added to the returned object.
131
    $entity = reset($entities);
132
    $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...
133
134
    // Check the node can be deleted.
135
    $user->delete();
136
    $entities = $this->storage->loadByProperties(['name' => $name]);
137
    $this->assertEquals(0, count($entities));
138
  }
139
140
  /**
141
   * Test that a blocked user can be created.
142
   */
143 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...
144
    $name = $this->randomString();
145
    $fields = [
146
      'name' => $name,
147
      'status' => 0,
148
    ];
149
    $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...
150
151
    $entities = $this->storage->loadByProperties(['name' => $name]);
152
    $this->assertEquals(1, count($entities));
153
154
    // Status should be set to 0 as explicitly specified.
155
    $entity = reset($entities);
156
    $this->assertEquals(0, $entity->status->value);
157
  }
158
159
  /**
160
   * Test that a user can be given a role, using role label or machine name.
161
   */
162
  public function testUserAddRoleByWrapper() {
163
    $role1Id = $this->randomMachineName();
164
    $role1Label = $this->randomString();
165
    $role2Id = $this->randomMachineName();
166
    $role2Label = $this->randomString();
167
    $role3Id = $this->randomMachineName();
168
    $role3Label = $this->randomString();
169
170
    $role1 = Role::create(['id' => $role1Id, 'label' => $role1Label]);
171
    $role2 = Role::create(['id' => $role2Id, 'label' => $role2Label]);
172
    $role3 = Role::create(['id' => $role3Id, 'label' => $role3Label]);
173
    $role1->save();
174
    $role2->save();
175
    $role3->save();
176
177
    $user = $this->createUser();
178
    $userWrapped = new DriverEntityDrupal8('user');
179
    $userWrapped->load($user->id());
180
    $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...
181
    $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...
182
    $userWrapped->save();
183
    $user = $this->reloadEntity($user);
184
185
    // Check role detection is working.
186
    $this->assertFalse($user->hasRole($role3Id));
187
188
    // Check user roles whether specified by machine name or label.
189
    $this->assertTrue($user->hasRole($role1Id));
190
    $this->assertTrue($user->hasRole($role2Id));
191
  }
192
193
}
194