ConfigEntityQueryTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. <?php
  2. namespace Drupal\KernelTests\Core\Entity;
  3. use Drupal\Core\Config\Entity\Query\QueryFactory;
  4. use Drupal\config_test\Entity\ConfigQueryTest;
  5. use Drupal\KernelTests\KernelTestBase;
  6. /**
  7. * Tests Config Entity Query functionality.
  8. *
  9. * @group Entity
  10. * @see \Drupal\Core\Config\Entity\Query
  11. */
  12. class ConfigEntityQueryTest extends KernelTestBase {
  13. /**
  14. * Modules to enable.
  15. *
  16. * @var array
  17. */
  18. public static $modules = ['config_test'];
  19. /**
  20. * Stores the search results for alter comparison.
  21. *
  22. * @var array
  23. */
  24. protected $queryResults;
  25. /**
  26. * The query factory used to construct all queries in the test.
  27. *
  28. * @var \Drupal\Core\Config\Entity\Query\QueryFactory
  29. */
  30. protected $factory;
  31. /**
  32. * The entity storage used for testing.
  33. *
  34. * @var \Drupal\Core\Entity\EntityStorageInterface
  35. */
  36. protected $entityStorage;
  37. /**
  38. * Stores all config entities created for the test.
  39. *
  40. * @var array
  41. */
  42. protected $entities;
  43. protected function setUp() {
  44. parent::setUp();
  45. $this->entities = [];
  46. $this->entityStorage = $this->container->get('entity_type.manager')->getStorage('config_query_test');
  47. // These two are here to make sure that matchArray needs to go over several
  48. // non-matches on every levels.
  49. $array['level1']['level2a'] = 9;
  50. $array['level1a']['level2'] = 9;
  51. // The tests match array.level1.level2.
  52. $array['level1']['level2'] = 1;
  53. $entity = ConfigQueryTest::create([
  54. 'label' => 'entity_1',
  55. 'id' => '1',
  56. 'number' => 31,
  57. 'array' => $array,
  58. ]);
  59. $this->entities[] = $entity;
  60. $entity->enforceIsNew();
  61. $entity->save();
  62. $array['level1']['level2'] = 2;
  63. $entity = ConfigQueryTest::create([
  64. 'label' => 'entity_2',
  65. 'id' => '2',
  66. 'number' => 41,
  67. 'array' => $array,
  68. ]);
  69. $this->entities[] = $entity;
  70. $entity->enforceIsNew();
  71. $entity->save();
  72. $array['level1']['level2'] = 1;
  73. $entity = ConfigQueryTest::create([
  74. 'label' => 'test_prefix_entity_3',
  75. 'id' => '3',
  76. 'number' => 59,
  77. 'array' => $array,
  78. ]);
  79. $this->entities[] = $entity;
  80. $entity->enforceIsNew();
  81. $entity->save();
  82. $array['level1']['level2'] = 2;
  83. $entity = ConfigQueryTest::create([
  84. 'label' => 'entity_4_test_suffix',
  85. 'id' => '4',
  86. 'number' => 26,
  87. 'array' => $array,
  88. ]);
  89. $this->entities[] = $entity;
  90. $entity->enforceIsNew();
  91. $entity->save();
  92. $array['level1']['level2'] = 3;
  93. $entity = ConfigQueryTest::create([
  94. 'label' => 'entity_5_TEST_contains_entity_5',
  95. 'id' => '5',
  96. 'number' => 53,
  97. 'array' => $array,
  98. ]);
  99. $this->entities[] = $entity;
  100. $entity->enforceIsNew();
  101. $entity->save();
  102. $array['level1'] = [];
  103. $entity = ConfigQueryTest::create([
  104. 'label' => $this->randomMachineName(),
  105. 'id' => '6',
  106. 'array' => $array,
  107. ]);
  108. $this->entities[] = $entity;
  109. $entity->enforceIsNew();
  110. $entity->save();
  111. $array['level1']['level2'] = 4;
  112. $entity = ConfigQueryTest::create([
  113. 'label' => $this->randomMachineName(),
  114. 'id' => '7',
  115. 'number' => 70,
  116. 'array' => $array,
  117. ]);
  118. $this->entities[] = $entity;
  119. $entity->enforceIsNew();
  120. $entity->save();
  121. }
  122. /**
  123. * Tests basic functionality.
  124. */
  125. public function testConfigEntityQuery() {
  126. // Run a test without any condition.
  127. $this->queryResults = $this->entityStorage->getQuery()
  128. ->execute();
  129. $this->assertResults(['1', '2', '3', '4', '5', '6', '7']);
  130. // No conditions, OR.
  131. $this->queryResults = $this->entityStorage->getQuery('OR')
  132. ->execute();
  133. $this->assertResults(['1', '2', '3', '4', '5', '6', '7']);
  134. // Filter by ID with equality.
  135. $this->queryResults = $this->entityStorage->getQuery()
  136. ->condition('id', '3')
  137. ->execute();
  138. $this->assertResults(['3']);
  139. // Filter by label with a known prefix.
  140. $this->queryResults = $this->entityStorage->getQuery()
  141. ->condition('label', 'test_prefix', 'STARTS_WITH')
  142. ->execute();
  143. $this->assertResults(['3']);
  144. // Filter by label with a known suffix.
  145. $this->queryResults = $this->entityStorage->getQuery()
  146. ->condition('label', 'test_suffix', 'ENDS_WITH')
  147. ->execute();
  148. $this->assertResults(['4']);
  149. // Filter by label with a known containing word.
  150. $this->queryResults = $this->entityStorage->getQuery()
  151. ->condition('label', 'test_contains', 'CONTAINS')
  152. ->execute();
  153. $this->assertResults(['5']);
  154. // Filter by ID with the IN operator.
  155. $this->queryResults = $this->entityStorage->getQuery()
  156. ->condition('id', ['2', '3'], 'IN')
  157. ->execute();
  158. $this->assertResults(['2', '3']);
  159. // Filter by ID with the implicit IN operator.
  160. $this->queryResults = $this->entityStorage->getQuery()
  161. ->condition('id', ['2', '3'])
  162. ->execute();
  163. $this->assertResults(['2', '3']);
  164. // Filter by ID with the > operator.
  165. $this->queryResults = $this->entityStorage->getQuery()
  166. ->condition('id', '3', '>')
  167. ->execute();
  168. $this->assertResults(['4', '5', '6', '7']);
  169. // Filter by ID with the >= operator.
  170. $this->queryResults = $this->entityStorage->getQuery()
  171. ->condition('id', '3', '>=')
  172. ->execute();
  173. $this->assertResults(['3', '4', '5', '6', '7']);
  174. // Filter by ID with the <> operator.
  175. $this->queryResults = $this->entityStorage->getQuery()
  176. ->condition('id', '3', '<>')
  177. ->execute();
  178. $this->assertResults(['1', '2', '4', '5', '6', '7']);
  179. // Filter by ID with the < operator.
  180. $this->queryResults = $this->entityStorage->getQuery()
  181. ->condition('id', '3', '<')
  182. ->execute();
  183. $this->assertResults(['1', '2']);
  184. // Filter by ID with the <= operator.
  185. $this->queryResults = $this->entityStorage->getQuery()
  186. ->condition('id', '3', '<=')
  187. ->execute();
  188. $this->assertResults(['1', '2', '3']);
  189. // Filter by two conditions on the same field.
  190. $this->queryResults = $this->entityStorage->getQuery()
  191. ->condition('label', 'test_pref', 'STARTS_WITH')
  192. ->condition('label', 'test_prefix', 'STARTS_WITH')
  193. ->execute();
  194. $this->assertResults(['3']);
  195. // Filter by two conditions on different fields. The first query matches for
  196. // a different ID, so the result is empty.
  197. $this->queryResults = $this->entityStorage->getQuery()
  198. ->condition('label', 'test_prefix', 'STARTS_WITH')
  199. ->condition('id', '5')
  200. ->execute();
  201. $this->assertResults([]);
  202. // Filter by two different conditions on different fields. This time the
  203. // first condition matches on one item, but the second one does as well.
  204. $this->queryResults = $this->entityStorage->getQuery()
  205. ->condition('label', 'test_prefix', 'STARTS_WITH')
  206. ->condition('id', '3')
  207. ->execute();
  208. $this->assertResults(['3']);
  209. // Filter by two different conditions, of which the first one matches for
  210. // every entry, the second one as well, but just the third one filters so
  211. // that just two are left.
  212. $this->queryResults = $this->entityStorage->getQuery()
  213. ->condition('id', '1', '>=')
  214. ->condition('number', 10, '>=')
  215. ->condition('number', 50, '>=')
  216. ->execute();
  217. $this->assertResults(['3', '5', '7']);
  218. // Filter with an OR condition group.
  219. $this->queryResults = $this->entityStorage->getQuery('OR')
  220. ->condition('id', 1)
  221. ->condition('id', '2')
  222. ->execute();
  223. $this->assertResults(['1', '2']);
  224. // Simplify it with IN.
  225. $this->queryResults = $this->entityStorage->getQuery()
  226. ->condition('id', ['1', '2'])
  227. ->execute();
  228. $this->assertResults(['1', '2']);
  229. // Try explicit IN.
  230. $this->queryResults = $this->entityStorage->getQuery()
  231. ->condition('id', ['1', '2'], 'IN')
  232. ->execute();
  233. $this->assertResults(['1', '2']);
  234. // Try not IN.
  235. $this->queryResults = $this->entityStorage->getQuery()
  236. ->condition('id', ['1', '2'], 'NOT IN')
  237. ->execute();
  238. $this->assertResults(['3', '4', '5', '6', '7']);
  239. // Filter with an OR condition group on different fields.
  240. $this->queryResults = $this->entityStorage->getQuery('OR')
  241. ->condition('id', 1)
  242. ->condition('number', 41)
  243. ->execute();
  244. $this->assertResults(['1', '2']);
  245. // Filter with an OR condition group on different fields but matching on the
  246. // same entity.
  247. $this->queryResults = $this->entityStorage->getQuery('OR')
  248. ->condition('id', 1)
  249. ->condition('number', 31)
  250. ->execute();
  251. $this->assertResults(['1']);
  252. // NO simple conditions, YES complex conditions, 'AND'.
  253. $query = $this->entityStorage->getQuery('AND');
  254. $and_condition_1 = $query->orConditionGroup()
  255. ->condition('id', '2')
  256. ->condition('label', $this->entities[0]->label);
  257. $and_condition_2 = $query->orConditionGroup()
  258. ->condition('id', 1)
  259. ->condition('label', $this->entities[3]->label);
  260. $this->queryResults = $query
  261. ->condition($and_condition_1)
  262. ->condition($and_condition_2)
  263. ->execute();
  264. $this->assertResults(['1']);
  265. // NO simple conditions, YES complex conditions, 'OR'.
  266. $query = $this->entityStorage->getQuery('OR');
  267. $and_condition_1 = $query->andConditionGroup()
  268. ->condition('id', 1)
  269. ->condition('label', $this->entities[0]->label);
  270. $and_condition_2 = $query->andConditionGroup()
  271. ->condition('id', '2')
  272. ->condition('label', $this->entities[1]->label);
  273. $this->queryResults = $query
  274. ->condition($and_condition_1)
  275. ->condition($and_condition_2)
  276. ->execute();
  277. $this->assertResults(['1', '2']);
  278. // YES simple conditions, YES complex conditions, 'AND'.
  279. $query = $this->entityStorage->getQuery('AND');
  280. $and_condition_1 = $query->orConditionGroup()
  281. ->condition('id', '2')
  282. ->condition('label', $this->entities[0]->label);
  283. $and_condition_2 = $query->orConditionGroup()
  284. ->condition('id', 1)
  285. ->condition('label', $this->entities[3]->label);
  286. $this->queryResults = $query
  287. ->condition('number', 31)
  288. ->condition($and_condition_1)
  289. ->condition($and_condition_2)
  290. ->execute();
  291. $this->assertResults(['1']);
  292. // YES simple conditions, YES complex conditions, 'OR'.
  293. $query = $this->entityStorage->getQuery('OR');
  294. $and_condition_1 = $query->orConditionGroup()
  295. ->condition('id', '2')
  296. ->condition('label', $this->entities[0]->label);
  297. $and_condition_2 = $query->orConditionGroup()
  298. ->condition('id', 1)
  299. ->condition('label', $this->entities[3]->label);
  300. $this->queryResults = $query
  301. ->condition('number', 53)
  302. ->condition($and_condition_1)
  303. ->condition($and_condition_2)
  304. ->execute();
  305. $this->assertResults(['1', '2', '4', '5']);
  306. // Test the exists and notExists conditions.
  307. $this->queryResults = $this->entityStorage->getQuery()
  308. ->exists('id')
  309. ->execute();
  310. $this->assertResults(['1', '2', '3', '4', '5', '6', '7']);
  311. $this->queryResults = $this->entityStorage->getQuery()
  312. ->exists('non-existent')
  313. ->execute();
  314. $this->assertResults([]);
  315. $this->queryResults = $this->entityStorage->getQuery()
  316. ->notExists('id')
  317. ->execute();
  318. $this->assertResults([]);
  319. $this->queryResults = $this->entityStorage->getQuery()
  320. ->notExists('non-existent')
  321. ->execute();
  322. $this->assertResults(['1', '2', '3', '4', '5', '6', '7']);
  323. }
  324. /**
  325. * Tests ID conditions.
  326. */
  327. public function testStringIdConditions() {
  328. // We need an entity with a non-numeric ID.
  329. $entity = ConfigQueryTest::create([
  330. 'label' => 'entity_1',
  331. 'id' => 'foo.bar',
  332. ]);
  333. $this->entities[] = $entity;
  334. $entity->enforceIsNew();
  335. $entity->save();
  336. // Test 'STARTS_WITH' condition.
  337. $this->queryResults = $this->entityStorage->getQuery()
  338. ->condition('id', 'foo.bar', 'STARTS_WITH')
  339. ->execute();
  340. $this->assertResults(['foo.bar']);
  341. $this->queryResults = $this->entityStorage->getQuery()
  342. ->condition('id', 'f', 'STARTS_WITH')
  343. ->execute();
  344. $this->assertResults(['foo.bar']);
  345. $this->queryResults = $this->entityStorage->getQuery()
  346. ->condition('id', 'miss', 'STARTS_WITH')
  347. ->execute();
  348. $this->assertResults([]);
  349. // Test 'CONTAINS' condition.
  350. $this->queryResults = $this->entityStorage->getQuery()
  351. ->condition('id', 'foo.bar', 'CONTAINS')
  352. ->execute();
  353. $this->assertResults(['foo.bar']);
  354. $this->queryResults = $this->entityStorage->getQuery()
  355. ->condition('id', 'oo.ba', 'CONTAINS')
  356. ->execute();
  357. $this->assertResults(['foo.bar']);
  358. $this->queryResults = $this->entityStorage->getQuery()
  359. ->condition('id', 'miss', 'CONTAINS')
  360. ->execute();
  361. $this->assertResults([]);
  362. // Test 'ENDS_WITH' condition.
  363. $this->queryResults = $this->entityStorage->getQuery()
  364. ->condition('id', 'foo.bar', 'ENDS_WITH')
  365. ->execute();
  366. $this->assertResults(['foo.bar']);
  367. $this->queryResults = $this->entityStorage->getQuery()
  368. ->condition('id', 'r', 'ENDS_WITH')
  369. ->execute();
  370. $this->assertResults(['foo.bar']);
  371. $this->queryResults = $this->entityStorage->getQuery()
  372. ->condition('id', 'miss', 'ENDS_WITH')
  373. ->execute();
  374. $this->assertResults([]);
  375. }
  376. /**
  377. * Tests count query.
  378. */
  379. public function testCount() {
  380. // Test count on no conditions.
  381. $count = $this->entityStorage->getQuery()
  382. ->count()
  383. ->execute();
  384. $this->assertIdentical($count, count($this->entities));
  385. // Test count on a complex query.
  386. $query = $this->entityStorage->getQuery('OR');
  387. $and_condition_1 = $query->andConditionGroup()
  388. ->condition('id', 1)
  389. ->condition('label', $this->entities[0]->label);
  390. $and_condition_2 = $query->andConditionGroup()
  391. ->condition('id', '2')
  392. ->condition('label', $this->entities[1]->label);
  393. $count = $query
  394. ->condition($and_condition_1)
  395. ->condition($and_condition_2)
  396. ->count()
  397. ->execute();
  398. $this->assertIdentical($count, 2);
  399. }
  400. /**
  401. * Tests sorting and range on config entity queries.
  402. */
  403. public function testSortRange() {
  404. // Sort by simple ascending/descending.
  405. $this->queryResults = $this->entityStorage->getQuery()
  406. ->sort('number', 'DESC')
  407. ->execute();
  408. $this->assertIdentical(array_values($this->queryResults), ['7', '3', '5', '2', '1', '4', '6']);
  409. $this->queryResults = $this->entityStorage->getQuery()
  410. ->sort('number', 'ASC')
  411. ->execute();
  412. $this->assertIdentical(array_values($this->queryResults), ['6', '4', '1', '2', '5', '3', '7']);
  413. // Apply some filters and sort.
  414. $this->queryResults = $this->entityStorage->getQuery()
  415. ->condition('id', '3', '>')
  416. ->sort('number', 'DESC')
  417. ->execute();
  418. $this->assertIdentical(array_values($this->queryResults), ['7', '5', '4', '6']);
  419. $this->queryResults = $this->entityStorage->getQuery()
  420. ->condition('id', '3', '>')
  421. ->sort('number', 'ASC')
  422. ->execute();
  423. $this->assertIdentical(array_values($this->queryResults), ['6', '4', '5', '7']);
  424. // Apply a pager and sort.
  425. $this->queryResults = $this->entityStorage->getQuery()
  426. ->sort('number', 'DESC')
  427. ->range('2', '2')
  428. ->execute();
  429. $this->assertIdentical(array_values($this->queryResults), ['5', '2']);
  430. $this->queryResults = $this->entityStorage->getQuery()
  431. ->sort('number', 'ASC')
  432. ->range('2', '2')
  433. ->execute();
  434. $this->assertIdentical(array_values($this->queryResults), ['1', '2']);
  435. // Add a range to a query without a start parameter.
  436. $this->queryResults = $this->entityStorage->getQuery()
  437. ->range(0, '3')
  438. ->sort('id', 'ASC')
  439. ->execute();
  440. $this->assertIdentical(array_values($this->queryResults), ['1', '2', '3']);
  441. // Apply a pager with limit 4.
  442. $this->queryResults = $this->entityStorage->getQuery()
  443. ->pager('4', 0)
  444. ->sort('id', 'ASC')
  445. ->execute();
  446. $this->assertIdentical(array_values($this->queryResults), ['1', '2', '3', '4']);
  447. }
  448. /**
  449. * Tests sorting with tableSort on config entity queries.
  450. */
  451. public function testTableSort() {
  452. $header = [
  453. ['data' => t('ID'), 'specifier' => 'id'],
  454. ['data' => t('Number'), 'specifier' => 'number'],
  455. ];
  456. // Sort key: id
  457. // Sorting with 'DESC' upper case
  458. $this->queryResults = $this->entityStorage->getQuery()
  459. ->tableSort($header)
  460. ->sort('id', 'DESC')
  461. ->execute();
  462. $this->assertIdentical(array_values($this->queryResults), ['7', '6', '5', '4', '3', '2', '1']);
  463. // Sorting with 'ASC' upper case
  464. $this->queryResults = $this->entityStorage->getQuery()
  465. ->tableSort($header)
  466. ->sort('id', 'ASC')
  467. ->execute();
  468. $this->assertIdentical(array_values($this->queryResults), ['1', '2', '3', '4', '5', '6', '7']);
  469. // Sorting with 'desc' lower case
  470. $this->queryResults = $this->entityStorage->getQuery()
  471. ->tableSort($header)
  472. ->sort('id', 'desc')
  473. ->execute();
  474. $this->assertIdentical(array_values($this->queryResults), ['7', '6', '5', '4', '3', '2', '1']);
  475. // Sorting with 'asc' lower case
  476. $this->queryResults = $this->entityStorage->getQuery()
  477. ->tableSort($header)
  478. ->sort('id', 'asc')
  479. ->execute();
  480. $this->assertIdentical(array_values($this->queryResults), ['1', '2', '3', '4', '5', '6', '7']);
  481. // Sort key: number
  482. // Sorting with 'DeSc' mixed upper and lower case
  483. $this->queryResults = $this->entityStorage->getQuery()
  484. ->tableSort($header)
  485. ->sort('number', 'DeSc')
  486. ->execute();
  487. $this->assertIdentical(array_values($this->queryResults), ['7', '3', '5', '2', '1', '4', '6']);
  488. // Sorting with 'AsC' mixed upper and lower case
  489. $this->queryResults = $this->entityStorage->getQuery()
  490. ->tableSort($header)
  491. ->sort('number', 'AsC')
  492. ->execute();
  493. $this->assertIdentical(array_values($this->queryResults), ['6', '4', '1', '2', '5', '3', '7']);
  494. // Sorting with 'dEsC' mixed upper and lower case
  495. $this->queryResults = $this->entityStorage->getQuery()
  496. ->tableSort($header)
  497. ->sort('number', 'dEsC')
  498. ->execute();
  499. $this->assertIdentical(array_values($this->queryResults), ['7', '3', '5', '2', '1', '4', '6']);
  500. // Sorting with 'aSc' mixed upper and lower case
  501. $this->queryResults = $this->entityStorage->getQuery()
  502. ->tableSort($header)
  503. ->sort('number', 'aSc')
  504. ->execute();
  505. $this->assertIdentical(array_values($this->queryResults), ['6', '4', '1', '2', '5', '3', '7']);
  506. }
  507. /**
  508. * Tests dotted path matching.
  509. */
  510. public function testDotted() {
  511. $this->queryResults = $this->entityStorage->getQuery()
  512. ->condition('array.level1.*', 1)
  513. ->execute();
  514. $this->assertResults(['1', '3']);
  515. $this->queryResults = $this->entityStorage->getQuery()
  516. ->condition('*.level1.level2', 2)
  517. ->execute();
  518. $this->assertResults(['2', '4']);
  519. $this->queryResults = $this->entityStorage->getQuery()
  520. ->condition('array.level1.*', 3)
  521. ->execute();
  522. $this->assertResults(['5']);
  523. $this->queryResults = $this->entityStorage->getQuery()
  524. ->condition('array.level1.level2', 3)
  525. ->execute();
  526. $this->assertResults(['5']);
  527. // Test dotted sorting.
  528. $this->queryResults = $this->entityStorage->getQuery()
  529. ->sort('array.level1.level2')
  530. ->execute();
  531. $this->assertResults(['6', '1', '3', '2', '4', '5', '7']);
  532. $this->queryResults = $this->entityStorage->getQuery()
  533. ->sort('array.level1.level2', 'DESC')
  534. ->execute();
  535. $this->assertResults(['7', '5', '2', '4', '1', '3', '6']);
  536. // Make sure that values on the wildcard level do not match if there are
  537. // sub-keys defined. This must not find anything even if entity 2 has a
  538. // top-level key number with value 41.
  539. $this->queryResults = $this->entityStorage->getQuery()
  540. ->condition('*.level1.level2', 41)
  541. ->execute();
  542. $this->assertResults([]);
  543. // Make sure that "IS NULL" and "IS NOT NULL" work correctly with
  544. // array-valued fields/keys.
  545. $this->queryResults = $this->entityStorage->getQuery()
  546. ->exists('array.level1.level2')
  547. ->execute();
  548. $this->assertResults(['1', '2', '3', '4', '5', '7']);
  549. $this->queryResults = $this->entityStorage->getQuery()
  550. ->exists('array.level1')
  551. ->execute();
  552. $this->assertResults(['1', '2', '3', '4', '5', '6', '7']);
  553. $this->queryResults = $this->entityStorage->getQuery()
  554. ->exists('array')
  555. ->execute();
  556. $this->assertResults(['1', '2', '3', '4', '5', '6', '7']);
  557. $this->queryResults = $this->entityStorage->getQuery()
  558. ->notExists('array.level1.level2')
  559. ->execute();
  560. $this->assertResults(['6']);
  561. $this->queryResults = $this->entityStorage->getQuery()
  562. ->notExists('array.level1')
  563. ->execute();
  564. $this->assertResults([]);
  565. $this->queryResults = $this->entityStorage->getQuery()
  566. ->notExists('array')
  567. ->execute();
  568. $this->assertResults([]);
  569. // Make sure that "IS NULL" and "IS NOT NULL" work correctly when the dotted
  570. // path cannot be fully followed.
  571. $this->queryResults = $this->entityStorage->getQuery()
  572. ->exists('does.not.exist')
  573. ->execute();
  574. $this->assertResults([]);
  575. $this->queryResults = $this->entityStorage->getQuery()
  576. ->notExists('does.not.exist')
  577. ->execute();
  578. $this->assertResults(['1', '2', '3', '4', '5', '6', '7']);
  579. }
  580. /**
  581. * Tests case sensitivity.
  582. */
  583. public function testCaseSensitivity() {
  584. // Filter by label with a known containing case-sensitive word.
  585. $this->queryResults = $this->entityStorage->getQuery()
  586. ->condition('label', 'TEST', 'CONTAINS')
  587. ->execute();
  588. $this->assertResults(['3', '4', '5']);
  589. $this->queryResults = $this->entityStorage->getQuery()
  590. ->condition('label', 'test', 'CONTAINS')
  591. ->execute();
  592. $this->assertResults(['3', '4', '5']);
  593. }
  594. /**
  595. * Tests lookup keys are added to the key value store.
  596. */
  597. public function testLookupKeys() {
  598. \Drupal::service('state')->set('config_test.lookup_keys', TRUE);
  599. \Drupal::entityTypeManager()->clearCachedDefinitions();
  600. $key_value = $this->container->get('keyvalue')->get(QueryFactory::CONFIG_LOOKUP_PREFIX . 'config_test');
  601. $test_entities = [];
  602. $storage = \Drupal::entityTypeManager()->getStorage('config_test');
  603. $entity = $storage->create([
  604. 'label' => 'entity_1',
  605. 'id' => '1',
  606. 'style' => 'test',
  607. ]);
  608. $test_entities[$entity->getConfigDependencyName()] = $entity;
  609. $entity->enforceIsNew();
  610. $entity->save();
  611. $expected[] = $entity->getConfigDependencyName();
  612. $this->assertEqual($expected, $key_value->get('style:test'));
  613. $entity = $storage->create([
  614. 'label' => 'entity_2',
  615. 'id' => '2',
  616. 'style' => 'test',
  617. ]);
  618. $test_entities[$entity->getConfigDependencyName()] = $entity;
  619. $entity->enforceIsNew();
  620. $entity->save();
  621. $expected[] = $entity->getConfigDependencyName();
  622. $this->assertEqual($expected, $key_value->get('style:test'));
  623. $entity = $storage->create([
  624. 'label' => 'entity_3',
  625. 'id' => '3',
  626. 'style' => 'blah',
  627. ]);
  628. $entity->enforceIsNew();
  629. $entity->save();
  630. // Do not add this entity to the list of expected result as it has a
  631. // different value.
  632. $this->assertEqual($expected, $key_value->get('style:test'));
  633. $this->assertEqual([$entity->getConfigDependencyName()], $key_value->get('style:blah'));
  634. // Ensure that a delete clears a key.
  635. $entity->delete();
  636. $this->assertEqual(NULL, $key_value->get('style:blah'));
  637. // Ensure that delete only clears one key.
  638. $entity_id = array_pop($expected);
  639. $test_entities[$entity_id]->delete();
  640. $this->assertEqual($expected, $key_value->get('style:test'));
  641. $entity_id = array_pop($expected);
  642. $test_entities[$entity_id]->delete();
  643. $this->assertEqual(NULL, $key_value->get('style:test'));
  644. }
  645. /**
  646. * Asserts the results as expected regardless of order.
  647. *
  648. * @param array $expected
  649. * Array of expected entity IDs.
  650. */
  651. protected function assertResults($expected) {
  652. $this->assertIdentical(count($this->queryResults), count($expected));
  653. foreach ($expected as $value) {
  654. // This also tests whether $this->queryResults[$value] is even set at all.
  655. $this->assertIdentical($this->queryResults[$value], $value);
  656. }
  657. }
  658. }