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

EntityReferenceTest::testNodeReferenceSingle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace Drupal\Tests\Driver\Kernel\Drupal8\Field;
4
5
use Drupal\Tests\Driver\Kernel\Drupal8\Field\DriverFieldKernelTestBase;
6
use Drupal\node\Entity\NodeType;
7
use Drupal\node\Entity\Node;
8
use Drupal\user\Entity\User;
9
use Drupal\user\Entity\Role;
10
11
/**
12
 * Tests the driver's handling of entity reference fields.
13
 *
14
 * @group driver
15
 */
16
class EntityReferenceTest extends DriverFieldKernelTestBase
17
{
18
19
  /**
20
   * {@inheritdoc}
21
   */
22
    public static $modules = ['entity_test', 'field', 'user', 'node'];
23
24
  /**
25
   * Machine name of the field type being tested.
26
   *
27
   * @string
28
   */
29
    protected $fieldType = 'entity_reference';
30
31
  /**
32
   * Entities available to reference.
33
   *
34
   * @array
35
   */
36
    protected $entities = [];
37
38
    protected function setUp()
39
    {
40
        parent::setUp();
41
        $nodeType = NodeType::create(['type' => 'article', 'name' => 'article'])
0 ignored issues
show
Unused Code introduced by
$nodeType 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...
42
        ->save();
43
        $this->entities['node1'] = Node::Create([
44
        'title' => $this->randomMachineName(),
45
        'type' => 'article',
46
        ]);
47
        $this->entities['node1']->save();
48
        $this->entities['node2'] = Node::Create([
49
        'title' => $this->randomMachineName(),
50
        'type' => 'article',
51
        ]);
52
        $this->entities['node2']->save();
53
        $this->entities['node3'] = Node::Create([
54
        'title' => $this->randomMachineName(),
55
        'type' => 'article',
56
        ]);
57
        $this->entities['node3']->save();
58
        $this->entities['user1'] = User::Create(['name' => $this->randomMachineName()]);
59
        $this->entities['user1']->save();
60
    }
61
62
  /**
63
   * Test referencing a node using its title.
64
   */
65
    public function testNodeReferenceSingle()
66
    {
67
        $this->fieldStorageSettings = ['target_type' => 'node'];
68
        $this->fieldSettings = [
69
        'handler' => 'default',
70
        'handler_settings' => ['target_bundles' => ['article']],
71
        ];
72
        $field = [$this->entities['node1']->label()];
73
        $fieldExpected = [['target_id' => $this->entities['node1']->id()]];
74
        $this->assertCreatedWithField($field, $fieldExpected);
75
    }
76
77
  /**
78
   * Test referencing multiple nodes using their title.
79
   */
80
    public function testNodeReferenceMultiple()
81
    {
82
        $this->fieldStorageSettings = ['target_type' => 'node'];
83
        $this->fieldSettings = [
84
        'handler' => 'default',
85
        'handler_settings' => ['target_bundles' => ['article']],
86
        ];
87
        $field = [
88
        $this->entities['node3']->label(),
89
        $this->entities['node1']->label(),
90
        $this->entities['node2']->label(),
91
        ];
92
        $fieldExpected = [
93
        ['target_id' => $this->entities['node3']->id()],
94
        ['target_id' => $this->entities['node1']->id()],
95
        ['target_id' => $this->entities['node2']->id()],
96
        ];
97
        $this->assertCreatedWithField($field, $fieldExpected);
98
    }
99
100
  /**
101
   * Test referencing a user by name (they don't have a label key or bundles,
102
   * so their driver entity plugin has to say what field to reference by).
103
   */
104
    public function testUserReferenceByName()
105
    {
106
        $this->fieldStorageSettings = ['target_type' => 'user'];
107
        $field = [$this->entities['user1']->name->value];
108
        $fieldExpected = [['target_id' => $this->entities['user1']->id()]];
109
        $this->assertCreatedWithField($field, $fieldExpected);
110
    }
111
112
  /**
113
   * Test referencing a user by mail (they don't have a label key or bundles,
114
   * so their driver entity plugin has to say what field to reference by).
115
   */
116
    public function testUserReferenceByMail()
117
    {
118
        $this->fieldStorageSettings = ['target_type' => 'user'];
119
        $mail = $this->randomMachineName() . '@' . $this->randomMachineName() . '.com';
120
        $this->entities['user1']->set('mail', $mail)->save();
121
        $field = [$mail];
122
        $fieldExpected = [['target_id' => $this->entities['user1']->id()]];
123
        $this->assertCreatedWithField($field, $fieldExpected);
124
    }
125
126
  /**
127
   * Test referencing a role by label.
128
   * Roles have string id's so can be referenced by label or id.
129
   */
130 View Code Duplication
    public function testRoleReferenceByLabel()
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...
131
    {
132
        $this->installEntitySchema('user_role');
133
        $role = Role::create(['id' => 'test_role', 'label' => 'Test role label'])->save();
0 ignored issues
show
Unused Code introduced by
$role 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...
134
        $this->fieldStorageSettings = ['target_type' => 'user_role'];
135
        $field = ['Test role label'];
136
        $fieldExpected = [['target_id' => 'test_role']];
137
        $this->assertCreatedWithField($field, $fieldExpected);
138
    }
139
140
  /**
141
   * Test referencing a role by id.
142
   * Roles have string id's so can be referenced by label or id.
143
   */
144 View Code Duplication
    public function testRoleReferenceById()
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...
145
    {
146
        $this->installEntitySchema('user_role');
147
        $role = Role::create(['id' => 'test_role', 'label' => 'Test role label'])->save();
0 ignored issues
show
Unused Code introduced by
$role 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...
148
        $this->fieldStorageSettings = ['target_type' => 'user_role'];
149
        $field = ['test_role'];
150
        $fieldExpected = [['target_id' => 'test_role']];
151
        $this->assertCreatedWithField($field, $fieldExpected);
152
    }
153
154
  /**
155
   * Test referencing a role by id without underscores.
156
   * Roles have string id's so can be referenced by label or id.
157
   */
158 View Code Duplication
    public function testRoleReferenceByIdWithoutUnderscores()
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...
159
    {
160
        $this->installEntitySchema('user_role');
161
        $role = Role::create(['id' => 'test_role', 'label' => 'Test role label'])->save();
0 ignored issues
show
Unused Code introduced by
$role 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...
162
        $this->fieldStorageSettings = ['target_type' => 'user_role'];
163
        $field = ['test role'];
164
        $fieldExpected = [['target_id' => 'test_role']];
165
        $this->assertCreatedWithField($field, $fieldExpected);
166
    }
167
168
  /**
169
   * Test referencing a role by id case insensitively.
170
   * Roles have string id's so can be referenced by label or id.
171
   */
172
173
  /*
174
   * It would be good to have case insensitive references, but it seems that
175
     config entityqueries are intrinsically case sensitive. Test commented out
176
     until issue is resolved.
177
178
  public function testRoleReferenceCaseInsensitive() {
179
    $this->installEntitySchema('user_role');
180
    $role = Role::create(['id' => 'test_role', 'label' => 'Test role label'])->save();
181
    $this->fieldStorageSettings = ['target_type' => 'user_role'];
182
    $field = ['TEST_role'];
183
    $fieldExpected = [['target_id' => 'test_role']];
184
    $this->assertCreatedWithField($field, $fieldExpected);
185
  }
186
  */
187
}
188