Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Entity / EntityQueryRelationshipTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Entity;
4
5 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
6 use Drupal\Component\Utility\Unicode;
7 use Drupal\entity_test\Entity\EntityTest;
8 use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
9 use Drupal\taxonomy\Entity\Vocabulary;
10 use Drupal\taxonomy\Entity\Term;
11
12 /**
13  * Tests the Entity Query relationship API.
14  *
15  * @group Entity
16  */
17 class EntityQueryRelationshipTest extends EntityKernelTestBase {
18
19   use EntityReferenceTestTrait;
20
21   /**
22    * Modules to enable.
23    *
24    * @var array
25    */
26   public static $modules = ['taxonomy'];
27
28   /**
29    * @var \Drupal\Core\Entity\Query\QueryFactory
30    */
31   protected $factory;
32
33   /**
34    * Term entities.
35    *
36    * @var array
37    */
38   protected $terms;
39
40   /**
41    * User entities.
42    *
43    * @var array
44    */
45   public $accounts;
46
47   /**
48    * entity_test entities.
49    *
50    * @var array
51    */
52   protected $entities;
53
54   /**
55    * The name of the taxonomy field used for test.
56    *
57    * @var string
58    */
59   protected $fieldName;
60
61   /**
62    * The results returned by EntityQuery.
63    *
64    * @var array
65    */
66   protected $queryResults;
67
68   protected function setUp() {
69     parent::setUp();
70
71     $this->installEntitySchema('taxonomy_term');
72
73     // We want an entity reference field. It needs a vocabulary, terms, a field
74     // storage and a field. First, create the vocabulary.
75     $vocabulary = Vocabulary::create([
76       'vid' => Unicode::strtolower($this->randomMachineName()),
77     ]);
78     $vocabulary->save();
79
80     // Second, create the field.
81     entity_test_create_bundle('test_bundle');
82     $this->fieldName = strtolower($this->randomMachineName());
83     $handler_settings = [
84       'target_bundles' => [
85         $vocabulary->id() => $vocabulary->id(),
86        ],
87       'auto_create' => TRUE,
88     ];
89     $this->createEntityReferenceField('entity_test', 'test_bundle', $this->fieldName, NULL, 'taxonomy_term', 'default', $handler_settings);
90
91     // Create two terms and also two accounts.
92     for ($i = 0; $i <= 1; $i++) {
93       $term = Term::create([
94         'name' => $this->randomMachineName(),
95         'vid' => $vocabulary->id(),
96       ]);
97       $term->save();
98       $this->terms[] = $term;
99       $this->accounts[] = $this->createUser();
100     }
101     // Create three entity_test entities, the 0th entity will point to the
102     // 0th account and 0th term, the 1st and 2nd entity will point to the
103     // 1st account and 1st term.
104     for ($i = 0; $i <= 2; $i++) {
105       $entity = EntityTest::create(['type' => 'test_bundle']);
106       $entity->name->value = $this->randomMachineName();
107       $index = $i ? 1 : 0;
108       $entity->user_id->target_id = $this->accounts[$index]->id();
109       $entity->{$this->fieldName}->target_id = $this->terms[$index]->id();
110       $entity->save();
111       $this->entities[] = $entity;
112     }
113     $this->factory = \Drupal::service('entity.query');
114   }
115
116   /**
117    * Tests querying.
118    */
119   public function testQuery() {
120     // This returns the 0th entity as that's the only one pointing to the 0th
121     // account.
122     $this->queryResults = $this->factory->get('entity_test')
123       ->condition("user_id.entity.name", $this->accounts[0]->getUsername())
124       ->execute();
125     $this->assertResults([0]);
126     // This returns the 1st and 2nd entity as those point to the 1st account.
127     $this->queryResults = $this->factory->get('entity_test')
128       ->condition("user_id.entity.name", $this->accounts[0]->getUsername(), '<>')
129       ->execute();
130     $this->assertResults([1, 2]);
131     // This returns all three entities because all of them point to an
132     // account.
133     $this->queryResults = $this->factory->get('entity_test')
134       ->exists("user_id.entity.name")
135       ->execute();
136     $this->assertResults([0, 1, 2]);
137     // This returns no entities because all of them point to an account.
138     $this->queryResults = $this->factory->get('entity_test')
139       ->notExists("user_id.entity.name")
140       ->execute();
141     $this->assertEqual(count($this->queryResults), 0);
142     // This returns the 0th entity as that's only one pointing to the 0th
143     // term (test without specifying the field column).
144     $this->queryResults = $this->factory->get('entity_test')
145       ->condition("$this->fieldName.entity.name", $this->terms[0]->name->value)
146       ->execute();
147     $this->assertResults([0]);
148     // This returns the 0th entity as that's only one pointing to the 0th
149     // term (test with specifying the column name).
150     $this->queryResults = $this->factory->get('entity_test')
151       ->condition("$this->fieldName.target_id.entity.name", $this->terms[0]->name->value)
152       ->execute();
153     $this->assertResults([0]);
154     // This returns the 1st and 2nd entity as those point to the 1st term.
155     $this->queryResults = $this->factory->get('entity_test')
156       ->condition("$this->fieldName.entity.name", $this->terms[0]->name->value, '<>')
157       ->execute();
158     $this->assertResults([1, 2]);
159     // This returns the 0th entity as that's only one pointing to the 0th
160     // account.
161     $this->queryResults = $this->factory->get('entity_test')
162       ->condition("user_id.entity:user.name", $this->accounts[0]->getUsername())
163       ->execute();
164     $this->assertResults([0]);
165     // This returns the 1st and 2nd entity as those point to the 1st account.
166     $this->queryResults = $this->factory->get('entity_test')
167       ->condition("user_id.entity:user.name", $this->accounts[0]->getUsername(), '<>')
168       ->execute();
169     $this->assertResults([1, 2]);
170     // This returns all three entities because all of them point to an
171     // account.
172     $this->queryResults = $this->factory->get('entity_test')
173       ->exists("user_id.entity:user.name")
174       ->execute();
175     $this->assertResults([0, 1, 2]);
176     // This returns no entities because all of them point to an account.
177     $this->queryResults = $this->factory->get('entity_test')
178       ->notExists("user_id.entity:user.name")
179       ->execute();
180     $this->assertEqual(count($this->queryResults), 0);
181     // This returns the 0th entity as that's only one pointing to the 0th
182     // term (test without specifying the field column).
183     $this->queryResults = $this->factory->get('entity_test')
184       ->condition("$this->fieldName.entity:taxonomy_term.name", $this->terms[0]->name->value)
185       ->execute();
186     $this->assertResults([0]);
187     // This returns the 0th entity as that's only one pointing to the 0th
188     // term (test with specifying the column name).
189     $this->queryResults = $this->factory->get('entity_test')
190       ->condition("$this->fieldName.target_id.entity:taxonomy_term.name", $this->terms[0]->name->value)
191       ->execute();
192     $this->assertResults([0]);
193     // This returns the 1st and 2nd entity as those point to the 1st term.
194     $this->queryResults = $this->factory->get('entity_test')
195       ->condition("$this->fieldName.entity:taxonomy_term.name", $this->terms[0]->name->value, '<>')
196       ->execute();
197     $this->assertResults([1, 2]);
198   }
199
200   /**
201    * Tests the invalid specifier in the query relationship.
202    */
203   public function testInvalidSpecifier() {
204     $this->setExpectedException(PluginNotFoundException::class);
205     $this->factory
206       ->get('taxonomy_term')
207       ->condition('langcode.language.foo', 'bar')
208       ->execute();
209   }
210
211   /**
212    * Assert the results.
213    *
214    * @param array $expected
215    *   A list of indexes in the $this->entities array.
216    */
217   protected function assertResults($expected) {
218     $this->assertEqual(count($this->queryResults), count($expected));
219     foreach ($expected as $key) {
220       $id = $this->entities[$key]->id();
221       $this->assertEqual($this->queryResults[$id], $id);
222     }
223   }
224
225 }