|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\Tests\Driver\Kernel\Drupal8\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Driver\Wrapper\Entity\DriverEntityDrupal8; |
|
6
|
|
|
use Drupal\Tests\Driver\Kernel\Drupal8\Entity\DriverEntityKernelTestBase; |
|
7
|
|
|
use Drupal\node\Entity\NodeType; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Tests the driver's handling of node entities. |
|
11
|
|
|
* |
|
12
|
|
|
* @group driver |
|
13
|
|
|
*/ |
|
14
|
|
|
class NodeTest extends DriverEntityKernelTestBase |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* {@inheritdoc} |
|
19
|
|
|
*/ |
|
20
|
|
|
public static $modules = ['node',]; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Machine name of the entity type being tested. |
|
24
|
|
|
* |
|
25
|
|
|
* @string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $entityType = 'node'; |
|
28
|
|
|
|
|
29
|
|
|
protected function setUp() |
|
30
|
|
|
{ |
|
31
|
|
|
parent::setUp(); |
|
32
|
|
|
$type = NodeType::create(['type' => 'article', 'name' => 'article']); |
|
33
|
|
|
$type->save(); |
|
34
|
|
|
|
|
35
|
|
|
// Add a body field to articles. |
|
36
|
|
|
$this->installConfig('node'); |
|
37
|
|
|
node_add_body_field($type); |
|
38
|
|
|
|
|
39
|
|
|
// Without node_access an error is thrown on deletion. |
|
40
|
|
|
$this->installSchema('node', 'node_access'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Test that a node can be created and deleted. |
|
46
|
|
|
*/ |
|
47
|
|
|
public function testNodeCreateDelete() |
|
48
|
|
|
{ |
|
49
|
|
|
$title = $this->driver->getRandom()->string(); |
|
50
|
|
|
$node = (object) [ |
|
51
|
|
|
'title' => $title, |
|
52
|
|
|
'type' => 'article', |
|
53
|
|
|
]; |
|
54
|
|
|
$node = $this->driver->createNode($node); |
|
55
|
|
|
|
|
56
|
|
|
$entities = $this->storage->loadByProperties(['title' => $title]); |
|
57
|
|
|
$this->assertEquals(1, count($entities)); |
|
58
|
|
|
|
|
59
|
|
|
// Check the id of the new node has been added to the returned object. |
|
60
|
|
|
$entity = reset($entities); |
|
61
|
|
|
$this->assertEquals($entity->id(), $node->nid); |
|
62
|
|
|
|
|
63
|
|
|
// Check the node can be deleted. |
|
64
|
|
|
$this->driver->nodeDelete($node); |
|
65
|
|
|
$entities = $this->storage->loadByProperties(['title' => $title]); |
|
66
|
|
|
$this->assertEquals(0, count($entities)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Test that a node can be created specifying its author by name. |
|
71
|
|
|
*/ |
|
72
|
|
View Code Duplication |
public function testNodeCreateWithAuthorName() |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
$title = $this->randomString(); |
|
75
|
|
|
$author = $this->createUser(); |
|
76
|
|
|
$authorName = $author->getUsername(); |
|
77
|
|
|
$node = (object) [ |
|
78
|
|
|
'title' => $title, |
|
79
|
|
|
'type' => 'article', |
|
80
|
|
|
'author' => $authorName, |
|
81
|
|
|
]; |
|
82
|
|
|
$node = $this->driver->createNode($node); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
$entities = $this->storage->loadByProperties(['title' => $title]); |
|
85
|
|
|
$this->assertEquals(1, count($entities)); |
|
86
|
|
|
$entity = reset($entities); |
|
87
|
|
|
$this->assertEquals($author->id(), $entity->getOwnerId()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Test that a node can be created specifying its body field. |
|
92
|
|
|
*/ |
|
93
|
|
View Code Duplication |
public function testNodeCreateWithBody() |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
$title = $this->randomString(); |
|
96
|
|
|
$body = $this->randomString(); |
|
97
|
|
|
$node = (object) [ |
|
98
|
|
|
'title' => $title, |
|
99
|
|
|
'type' => 'article', |
|
100
|
|
|
'body' => $body, |
|
101
|
|
|
]; |
|
102
|
|
|
$node = $this->driver->createNode($node); |
|
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
$entities = $this->storage->loadByProperties(['title' => $title]); |
|
105
|
|
|
$this->assertEquals(1, count($entities)); |
|
106
|
|
|
$entity = reset($entities); |
|
107
|
|
|
$this->assertEquals($body, $entity->get('body')->value); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Test that a node can be created and deleted. |
|
112
|
|
|
*/ |
|
113
|
|
View Code Duplication |
public function testNodeCreateDeleteByWrapper() |
|
|
|
|
|
|
114
|
|
|
{ |
|
115
|
|
|
$title = $this->driver->getRandom()->string(); |
|
116
|
|
|
$fields = [ |
|
117
|
|
|
'title' => $title, |
|
118
|
|
|
'type' => 'article', |
|
119
|
|
|
]; |
|
120
|
|
|
$node = DriverEntityDrupal8::create($fields, $this->entityType)->save(); |
|
121
|
|
|
|
|
122
|
|
|
$entities = $this->storage->loadByProperties(['title' => $title]); |
|
123
|
|
|
$this->assertEquals(1, count($entities)); |
|
124
|
|
|
|
|
125
|
|
|
// Check the id of the new node has been added to the returned object. |
|
126
|
|
|
$entity = reset($entities); |
|
127
|
|
|
$this->assertEquals($entity->id(), $node->nid); |
|
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
// Check the node can be deleted. |
|
130
|
|
|
$this->driver->nodeDelete($node); |
|
131
|
|
|
$entities = $this->storage->loadByProperties(['title' => $title]); |
|
132
|
|
|
$this->assertEquals(0, count($entities)); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Test that a node can be created specifying its author by name. |
|
137
|
|
|
*/ |
|
138
|
|
View Code Duplication |
public function testNodeCreateWithAuthorNameByWrapper() |
|
|
|
|
|
|
139
|
|
|
{ |
|
140
|
|
|
$title = $this->randomString(); |
|
141
|
|
|
$author = $this->createUser(); |
|
142
|
|
|
$authorName = $author->getUsername(); |
|
143
|
|
|
$fields = [ |
|
144
|
|
|
'title' => $title, |
|
145
|
|
|
'type' => 'article', |
|
146
|
|
|
'author' => $authorName, |
|
147
|
|
|
]; |
|
148
|
|
|
$node = DriverEntityDrupal8::create($fields, $this->entityType)->save(); |
|
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
$entities = $this->storage->loadByProperties(['title' => $title]); |
|
151
|
|
|
$this->assertEquals(1, count($entities)); |
|
152
|
|
|
$entity = reset($entities); |
|
153
|
|
|
$this->assertEquals($author->id(), $entity->getOwnerId()); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Test that a node can be created specifying its body field. |
|
158
|
|
|
*/ |
|
159
|
|
View Code Duplication |
public function testNodeCreateWithBodyByWrapper() |
|
|
|
|
|
|
160
|
|
|
{ |
|
161
|
|
|
$title = $this->randomString(); |
|
162
|
|
|
$body = $this->randomString(); |
|
163
|
|
|
$fields = [ |
|
164
|
|
|
'title' => $title, |
|
165
|
|
|
'type' => 'article', |
|
166
|
|
|
'body' => $body, |
|
167
|
|
|
]; |
|
168
|
|
|
$node = DriverEntityDrupal8::create($fields, $this->entityType)->save(); |
|
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
$entities = $this->storage->loadByProperties(['title' => $title]); |
|
171
|
|
|
$this->assertEquals(1, count($entities)); |
|
172
|
|
|
$entity = reset($entities); |
|
173
|
|
|
$this->assertEquals($body, $entity->get('body')->value); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Test the created and changed fields on a node. |
|
178
|
|
|
*/ |
|
179
|
|
|
public function testNodeCreatedChanged() |
|
180
|
|
|
{ |
|
181
|
|
|
$title = $this->randomString(); |
|
182
|
|
|
$fields = [ |
|
183
|
|
|
'title' => $title, |
|
184
|
|
|
'type' => 'article', |
|
185
|
|
|
'created' => '04/27/2013 11:11am UTC', |
|
186
|
|
|
'changed' => '07/27/2014 12:03pm UTC', |
|
187
|
|
|
]; |
|
188
|
|
|
$node = DriverEntityDrupal8::create($fields, $this->entityType)->save(); |
|
|
|
|
|
|
189
|
|
|
|
|
190
|
|
|
$entities = $this->storage->loadByProperties(['title' => $title]); |
|
191
|
|
|
$this->assertEquals(1, count($entities)); |
|
192
|
|
|
$entity = reset($entities); |
|
193
|
|
|
$this->assertEquals('1367061060', $entity->get('created')->value); |
|
194
|
|
|
$this->assertEquals('1406462580', $entity->get('changed')->value); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
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.