/core/modules/system/tests/src/Kernel/Entity/EntityReferenceSelectionReferenceableTest.php

https://gitlab.com/reasonat/test8 · PHP · 200 lines · 75 code · 21 blank · 104 comment · 2 complexity · 0e6c587ba30c35dc203cd523024ad991 MD5 · raw file

  1. <?php
  2. namespace Drupal\Tests\system\Kernel\Entity;
  3. use Drupal\Component\Utility\Html;
  4. use Drupal\Component\Utility\Unicode;
  5. use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
  6. use Drupal\field\Entity\FieldConfig;
  7. use Drupal\node\Entity\NodeType;
  8. use Drupal\KernelTests\KernelTestBase;
  9. /**
  10. * Tests entity reference selection plugins.
  11. *
  12. * @group entity_reference
  13. */
  14. class EntityReferenceSelectionReferenceableTest extends KernelTestBase {
  15. use EntityReferenceTestTrait;
  16. /**
  17. * Bundle of 'entity_test_no_label' entity.
  18. *
  19. * @var string
  20. */
  21. protected $bundle;
  22. /**
  23. * Labels to be tested.
  24. *
  25. * @var array
  26. */
  27. protected static $labels = ['abc', 'Xyz_', 'xyabz_', 'foo_', 'bar_', 'baz_', 'șz_', NULL, '<strong>'];
  28. /**
  29. * The selection handler.
  30. *
  31. * @var \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface.
  32. */
  33. protected $selectionHandler;
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public static $modules = ['system', 'user', 'field', 'entity_reference', 'node', 'entity_test'];
  38. /**
  39. * {@inheritdoc}
  40. */
  41. protected function setUp() {
  42. parent::setUp();
  43. $this->installEntitySchema('entity_test_no_label');
  44. /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
  45. $storage = $this->container->get('entity.manager')
  46. ->getStorage('entity_test_no_label');
  47. // Create a new node-type.
  48. NodeType::create([
  49. 'type' => $node_type = Unicode::strtolower($this->randomMachineName()),
  50. 'name' => $this->randomString(),
  51. ])->save();
  52. // Create an entity reference field targeting 'entity_test_no_label'
  53. // entities.
  54. $field_name = Unicode::strtolower($this->randomMachineName());
  55. $this->createEntityReferenceField('node', $node_type, $field_name, $this->randomString(), 'entity_test_no_label');
  56. $field_config = FieldConfig::loadByName('node', $node_type, $field_name);
  57. $this->selectionHandler = $this->container->get('plugin.manager.entity_reference_selection')->getSelectionHandler($field_config);
  58. // Generate a bundle name to be used with 'entity_test_no_label'.
  59. $this->bundle = Unicode::strtolower($this->randomMachineName());
  60. // Create 6 entities to be referenced by the field.
  61. foreach (static::$labels as $name) {
  62. $storage->create([
  63. 'id' => Unicode::strtolower($this->randomMachineName()),
  64. 'name' => $name,
  65. 'type' => $this->bundle,
  66. ])->save();
  67. }
  68. }
  69. /**
  70. * Tests values returned by SelectionInterface::getReferenceableEntities()
  71. * when the target entity type has no 'label' key.
  72. *
  73. * @param mixed $match
  74. * The input text to be checked.
  75. * @param string $match_operator
  76. * The matching operator.
  77. * @param int $limit
  78. * The limit of returning records.
  79. * @param int $count_limited
  80. * The expected number of limited entities to be retrieved.
  81. * @param array $items
  82. * Array of entity labels expected to be returned.
  83. * @param int $count_all
  84. * The total number (unlimited) of entities to be retrieved.
  85. *
  86. * @dataProvider providerTestCases
  87. */
  88. public function testReferenceablesWithNoLabelKey($match, $match_operator, $limit, $count_limited, array $items, $count_all) {
  89. // Test ::getReferenceableEntities().
  90. $referenceables = $this->selectionHandler->getReferenceableEntities($match, $match_operator, $limit);
  91. // Number of returned items.
  92. if (empty($count_limited)) {
  93. $this->assertTrue(empty($referenceables[$this->bundle]));
  94. }
  95. else {
  96. $this->assertSame(count($referenceables[$this->bundle]), $count_limited);
  97. }
  98. // Test returned items.
  99. foreach ($items as $item) {
  100. // SelectionInterface::getReferenceableEntities() always return escaped
  101. // entity labels.
  102. // @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface::getReferenceableEntities()
  103. $item = is_string($item) ? Html::escape($item) : $item;
  104. $this->assertTrue(array_search($item, $referenceables[$this->bundle]) !== FALSE);
  105. }
  106. // Test ::countReferenceableEntities().
  107. $count_referenceables = $this->selectionHandler->countReferenceableEntities($match, $match_operator);
  108. $this->assertSame($count_referenceables, $count_all);
  109. }
  110. /**
  111. * Provides test cases for ::testReferenceablesWithNoLabelKey() test.
  112. *
  113. * @return array[]
  114. */
  115. public function providerTestCases() {
  116. return [
  117. // All referenceables, no limit. Expecting 9 items.
  118. [NULL, 'CONTAINS', 0, 9, static::$labels, 9],
  119. // Referenceables containing 'w', no limit. Expecting no item.
  120. ['w', 'CONTAINS', 0, 0, [], 0],
  121. // Referenceables starting with 'w', no limit. Expecting no item.
  122. ['w', 'STARTS_WITH', 0, 0, [], 0],
  123. // Referenceables containing 'ab', no limit. Expecting 2 items ('abc',
  124. // 'xyabz').
  125. ['ab', 'CONTAINS', 0, 2, ['abc', 'xyabz_'], 2],
  126. // Referenceables starting with 'A', no limit. Expecting 1 item ('abc').
  127. ['A', 'STARTS_WITH', 0, 1, ['abc'], 1],
  128. // Referenceables containing '_', limited to 3. Expecting 3 limited items
  129. // ('Xyz_', 'xyabz_', 'foo_') and 5 total.
  130. ['_', 'CONTAINS', 3, 3, ['Xyz_', 'xyabz_', 'foo_'], 6],
  131. // Referenceables ending with 'z_', limited to 3. Expecting 3 limited
  132. // items ('Xyz_', 'xyabz_', 'baz_') and 4 total.
  133. ['z_', 'ENDS_WITH', 3, 3, ['Xyz_', 'xyabz_', 'baz_'], 4],
  134. // Referenceables identical with 'xyabz_', no limit. Expecting 1 item
  135. // ('xyabz_').
  136. ['xyabz_', '=', 0, 1, ['xyabz_'], 1],
  137. // Referenceables greater than 'foo', no limit. Expecting 4 items ('Xyz_',
  138. // 'xyabz_', 'foo_', 'șz_').
  139. ['foo', '>', 0, 4, ['Xyz_', 'xyabz_', 'foo_', 'șz_'], 4],
  140. // Referenceables greater or identical with 'baz_', no limit. Expecting 5
  141. // items ('Xyz_', 'xyabz_', 'foo_', 'baz_', 'șz_').
  142. ['baz_', '>=', 0, 5, ['Xyz_', 'xyabz_', 'foo_', 'baz_', 'șz_'], 5],
  143. // Referenceables less than 'foo', no limit. Expecting 5 items ('abc',
  144. // 'bar_', 'baz_', NULL, '<strong>').
  145. ['foo', '<', 0, 5, ['abc', 'bar_', 'baz_', NULL, '<strong>'], 5],
  146. // Referenceables less or identical with 'baz_', no limit. Expecting 5
  147. // items ('abc', 'bar_', 'baz_', NULL, '<strong>').
  148. ['baz_', '<=', 0, 5, ['abc', 'bar_', 'baz_', NULL, '<strong>'], 5],
  149. // Referenceables not identical with 'baz_', no limit. Expecting 7 items
  150. // ('abc', 'Xyz_', 'xyabz_', 'foo_', 'bar_', 'șz_', NULL, '<strong>').
  151. ['baz_', '<>', 0, 8, ['abc', 'Xyz_', 'xyabz_', 'foo_', 'bar_', 'șz_', NULL, '<strong>'], 8],
  152. // Referenceables in ('bar_', 'baz_'), no limit. Expecting 2 items
  153. // ('bar_', 'baz_')
  154. [['bar_', 'baz_'], 'IN', 0, 2, ['bar_', 'baz_'], 2],
  155. // Referenceables not in ('bar_', 'baz_'), no limit. Expecting 6 items
  156. // ('abc', 'Xyz_', 'xyabz_', 'foo_', 'șz_', NULL, '<strong>')
  157. [['bar_', 'baz_'], 'NOT IN', 0, 7, ['abc', 'Xyz_', 'xyabz_', 'foo_', 'șz_', NULL, '<strong>'], 7],
  158. // Referenceables not null, no limit. Expecting 9 items ('abc', 'Xyz_',
  159. // 'xyabz_', 'foo_', 'bar_', 'baz_', 'șz_', NULL, '<strong>').
  160. //
  161. // Note: Even we set the name as NULL, when retrieving the label from the
  162. // entity we'll get an empty string, meaning that this match operator
  163. // will return TRUE every time.
  164. [NULL, 'IS NOT NULL', 0, 9, static::$labels, 9],
  165. // Referenceables null, no limit. Expecting 9 items ('abc', 'Xyz_',
  166. // 'xyabz_', 'foo_', 'bar_', 'baz_', 'șz_', NULL, '<strong>').
  167. //
  168. // Note: Even we set the name as NULL, when retrieving the label from the
  169. // entity we'll get an empty string, meaning that this match operator
  170. // will return FALSE every time.
  171. [NULL, 'IS NULL', 0, 9, static::$labels, 9],
  172. // Referenceables containing '<strong>' markup, no limit. Expecting 1 item
  173. // ('<strong>').
  174. ['<strong>', 'CONTAINS', 0, 1, ['<strong>'], 1],
  175. // Test an unsupported operator. We expect no items.
  176. ['abc', '*unsupported*', 0, 0, [], 0],
  177. ];
  178. }
  179. }