|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\Tests\Driver\Kernel\Drupal8\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Driver\Wrapper\Entity\DriverEntityDrupal8; |
|
6
|
|
|
use Drupal\taxonomy\Entity\Vocabulary; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Tests the driver's handling of term entities. |
|
10
|
|
|
* |
|
11
|
|
|
* @group driver |
|
12
|
|
|
*/ |
|
13
|
|
|
class TaxonomyTermTest extends DriverEntityKernelTestBase { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* {@inheritdoc} |
|
17
|
|
|
*/ |
|
18
|
|
|
public static $modules = ['taxonomy']; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Machine name of the entity type being tested. |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $entityType = 'taxonomy_term'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* {@inheritdoc} |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function setUp() { |
|
31
|
|
|
parent::setUp(); |
|
32
|
|
|
$this->installEntitySchema('taxonomy_term'); |
|
33
|
|
|
$vocabulary = Vocabulary::create(['vid' => 'testvocab', 'name' => 'test vocabulary']); |
|
34
|
|
|
$vocabulary->save(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Test that a term can be created and deleted. |
|
39
|
|
|
*/ |
|
40
|
|
View Code Duplication |
public function testTermCreateDelete() { |
|
|
|
|
|
|
41
|
|
|
$name = $this->randomString(); |
|
42
|
|
|
$term = (object) [ |
|
43
|
|
|
'name' => $name, |
|
44
|
|
|
'vocabulary_machine_name' => 'testvocab', |
|
45
|
|
|
]; |
|
46
|
|
|
$term = $this->driver->createTerm($term); |
|
47
|
|
|
|
|
48
|
|
|
$entities = $this->storage->loadByProperties(['name' => $name]); |
|
49
|
|
|
$this->assertEquals(1, count($entities)); |
|
50
|
|
|
|
|
51
|
|
|
// Check the id of the new term has been added to the returned object. |
|
52
|
|
|
$entity = reset($entities); |
|
53
|
|
|
$this->assertEquals($entity->id(), $term->tid); |
|
54
|
|
|
|
|
55
|
|
|
// Check the term can be deleted. |
|
56
|
|
|
$this->driver->termDelete($term); |
|
57
|
|
|
$entities = $this->storage->loadByProperties(['name' => $name]); |
|
58
|
|
|
$this->assertEquals(0, count($entities)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Test that a term can be created with a parent term. |
|
63
|
|
|
*/ |
|
64
|
|
|
public function testTermCreateWithParent() { |
|
65
|
|
|
$parentName = $this->randomString(); |
|
66
|
|
|
$parent = (object) [ |
|
67
|
|
|
'name' => $parentName, |
|
68
|
|
|
'vocabulary_machine_name' => 'testvocab', |
|
69
|
|
|
]; |
|
70
|
|
|
$parent = $this->driver->createTerm($parent); |
|
71
|
|
|
|
|
72
|
|
|
$childName = $this->randomString(); |
|
73
|
|
|
$child = (object) [ |
|
74
|
|
|
'name' => $childName, |
|
75
|
|
|
'vocabulary_machine_name' => 'testvocab', |
|
76
|
|
|
'parent' => $parentName, |
|
77
|
|
|
]; |
|
78
|
|
|
$child = $this->driver->createTerm($child); |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
$entities = $this->storage->loadByProperties(['name' => $childName]); |
|
81
|
|
|
$this->assertEquals(1, count($entities)); |
|
82
|
|
|
|
|
83
|
|
|
// Check the parent is set on the child term. |
|
84
|
|
|
$entity = reset($entities); |
|
85
|
|
|
$parentEntities = $this->storage->loadParents($entity->id()); |
|
86
|
|
|
$parentEntity = reset($parentEntities); |
|
87
|
|
|
$this->assertEquals($parent->tid, $parentEntity->id()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Test that a term can be created and deleted. |
|
92
|
|
|
*/ |
|
93
|
|
View Code Duplication |
public function testTermCreateDeleteByWrapper() { |
|
|
|
|
|
|
94
|
|
|
$name = $this->randomString(); |
|
95
|
|
|
$fields = [ |
|
96
|
|
|
'name' => $name, |
|
97
|
|
|
'vocabulary' => 'testvocab', |
|
98
|
|
|
]; |
|
99
|
|
|
$term = DriverEntityDrupal8::create($fields, $this->entityType)->save(); |
|
100
|
|
|
|
|
101
|
|
|
$entities = $this->storage->loadByProperties(['name' => $name]); |
|
102
|
|
|
$this->assertEquals(1, count($entities)); |
|
103
|
|
|
|
|
104
|
|
|
// Check the id of the new term has been added to the returned object. |
|
105
|
|
|
$entity = reset($entities); |
|
106
|
|
|
$this->assertEquals($entity->id(), $term->tid); |
|
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
// Check the term can be deleted. |
|
109
|
|
|
$term->delete(); |
|
110
|
|
|
$entities = $this->storage->loadByProperties(['name' => $name]); |
|
111
|
|
|
$this->assertEquals(0, count($entities)); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Test that a term can be created with a parent term. |
|
116
|
|
|
* |
|
117
|
|
|
* Also tests that a vocabulary can be referred to by it label. |
|
118
|
|
|
*/ |
|
119
|
|
|
public function testTermCreateWithParentByWrapper() { |
|
120
|
|
|
$parentName = $this->randomString(); |
|
121
|
|
|
$parentFields = [ |
|
122
|
|
|
'name' => $parentName, |
|
123
|
|
|
// Test using label not machine name for vocab reference. |
|
124
|
|
|
'vocabulary' => 'test vocabulary', |
|
125
|
|
|
]; |
|
126
|
|
|
$parent = DriverEntityDrupal8::create($parentFields, $this->entityType)->save(); |
|
127
|
|
|
|
|
128
|
|
|
$childName = $this->randomString(); |
|
129
|
|
|
$childFields = [ |
|
130
|
|
|
'name' => $childName, |
|
131
|
|
|
'vocabulary' => 'testvocab', |
|
132
|
|
|
'parent' => $parentName, |
|
133
|
|
|
]; |
|
134
|
|
|
$child = DriverEntityDrupal8::create($childFields, $this->entityType)->save(); |
|
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
$entities = $this->storage->loadByProperties(['name' => $childName]); |
|
137
|
|
|
$this->assertEquals(1, count($entities)); |
|
138
|
|
|
|
|
139
|
|
|
// Check the parent is set on the child term. |
|
140
|
|
|
$entity = reset($entities); |
|
141
|
|
|
$parentEntities = $this->storage->loadParents($entity->id()); |
|
142
|
|
|
$parentEntity = reset($parentEntities); |
|
143
|
|
|
$this->assertEquals($parent->tid, $parentEntity->id()); |
|
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Test 'vocabulary_machine_name' as BC support for old human-friendly name. |
|
148
|
|
|
*/ |
|
149
|
|
|
public function testVocabularyBcBundleName() { |
|
150
|
|
|
$name = $this->randomString(); |
|
151
|
|
|
$fields = [ |
|
152
|
|
|
'name' => $name, |
|
153
|
|
|
'vocabulary_machine_name' => 'testvocab', |
|
154
|
|
|
]; |
|
155
|
|
|
$term = DriverEntityDrupal8::create($fields, $this->entityType)->save(); |
|
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
$entities = $this->storage->loadByProperties(['name' => $name, 'vid' => 'testvocab']); |
|
158
|
|
|
$this->assertEquals(1, count($entities)); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
} |
|
162
|
|
|
|
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.