3 namespace Drupal\Tests\system\Kernel\Entity;
5 use Drupal\Component\Utility\Html;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\node\Entity\NodeType;
8 use Drupal\KernelTests\KernelTestBase;
9 use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
12 * Tests entity reference selection plugins.
14 * @group entity_reference
16 class EntityReferenceSelectionReferenceableTest extends KernelTestBase {
18 use EntityReferenceTestTrait;
21 * Bundle of 'entity_test_no_label' entity.
28 * Labels to be tested.
32 protected static $labels = ['abc', 'Xyz_', 'xyabz_', 'foo_', 'bar_', 'baz_', 'șz_', NULL, '<strong>'];
35 * The selection handler.
37 * @var \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface
39 protected $selectionHandler;
44 public static $modules = ['system', 'user', 'field', 'entity_reference', 'node', 'entity_test'];
49 protected function setUp() {
52 $this->installEntitySchema('entity_test_no_label');
54 /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
55 $storage = $this->container->get('entity.manager')
56 ->getStorage('entity_test_no_label');
58 // Create a new node-type.
60 'type' => $node_type = mb_strtolower($this->randomMachineName()),
61 'name' => $this->randomString(),
64 // Create an entity reference field targeting 'entity_test_no_label'
66 $field_name = mb_strtolower($this->randomMachineName());
67 $this->createEntityReferenceField('node', $node_type, $field_name, $this->randomString(), 'entity_test_no_label');
68 $field_config = FieldConfig::loadByName('node', $node_type, $field_name);
69 $this->selectionHandler = $this->container->get('plugin.manager.entity_reference_selection')->getSelectionHandler($field_config);
71 // Generate a bundle name to be used with 'entity_test_no_label'.
72 $this->bundle = mb_strtolower($this->randomMachineName());
74 // Create 6 entities to be referenced by the field.
75 foreach (static::$labels as $name) {
77 'id' => mb_strtolower($this->randomMachineName()),
79 'type' => $this->bundle,
85 * Tests values returned by SelectionInterface::getReferenceableEntities()
86 * when the target entity type has no 'label' key.
89 * The input text to be checked.
90 * @param string $match_operator
91 * The matching operator.
93 * The limit of returning records.
94 * @param int $count_limited
95 * The expected number of limited entities to be retrieved.
97 * Array of entity labels expected to be returned.
98 * @param int $count_all
99 * The total number (unlimited) of entities to be retrieved.
101 * @dataProvider providerTestCases
103 public function testReferenceablesWithNoLabelKey($match, $match_operator, $limit, $count_limited, array $items, $count_all) {
104 // Test ::getReferenceableEntities().
105 $referenceables = $this->selectionHandler->getReferenceableEntities($match, $match_operator, $limit);
107 // Number of returned items.
108 if (empty($count_limited)) {
109 $this->assertTrue(empty($referenceables[$this->bundle]));
112 $this->assertSame(count($referenceables[$this->bundle]), $count_limited);
115 // Test returned items.
116 foreach ($items as $item) {
117 // SelectionInterface::getReferenceableEntities() always return escaped
119 // @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface::getReferenceableEntities()
120 $item = is_string($item) ? Html::escape($item) : $item;
121 $this->assertTrue(array_search($item, $referenceables[$this->bundle]) !== FALSE);
124 // Test ::countReferenceableEntities().
125 $count_referenceables = $this->selectionHandler->countReferenceableEntities($match, $match_operator);
126 $this->assertSame($count_referenceables, $count_all);
130 * Provides test cases for ::testReferenceablesWithNoLabelKey() test.
134 public function providerTestCases() {
136 // All referenceables, no limit. Expecting 9 items.
137 [NULL, 'CONTAINS', 0, 9, static::$labels, 9],
138 // Referenceables containing 'w', no limit. Expecting no item.
139 ['w', 'CONTAINS', 0, 0, [], 0],
140 // Referenceables starting with 'w', no limit. Expecting no item.
141 ['w', 'STARTS_WITH', 0, 0, [], 0],
142 // Referenceables containing 'ab', no limit. Expecting 2 items ('abc',
144 ['ab', 'CONTAINS', 0, 2, ['abc', 'xyabz_'], 2],
145 // Referenceables starting with 'A', no limit. Expecting 1 item ('abc').
146 ['A', 'STARTS_WITH', 0, 1, ['abc'], 1],
147 // Referenceables containing '_', limited to 3. Expecting 3 limited items
148 // ('Xyz_', 'xyabz_', 'foo_') and 5 total.
149 ['_', 'CONTAINS', 3, 3, ['Xyz_', 'xyabz_', 'foo_'], 6],
150 // Referenceables ending with 'z_', limited to 3. Expecting 3 limited
151 // items ('Xyz_', 'xyabz_', 'baz_') and 4 total.
152 ['z_', 'ENDS_WITH', 3, 3, ['Xyz_', 'xyabz_', 'baz_'], 4],
153 // Referenceables identical with 'xyabz_', no limit. Expecting 1 item
155 ['xyabz_', '=', 0, 1, ['xyabz_'], 1],
156 // Referenceables greater than 'foo', no limit. Expecting 4 items ('Xyz_',
157 // 'xyabz_', 'foo_', 'șz_').
158 ['foo', '>', 0, 4, ['Xyz_', 'xyabz_', 'foo_', 'șz_'], 4],
159 // Referenceables greater or identical with 'baz_', no limit. Expecting 5
160 // items ('Xyz_', 'xyabz_', 'foo_', 'baz_', 'șz_').
161 ['baz_', '>=', 0, 5, ['Xyz_', 'xyabz_', 'foo_', 'baz_', 'șz_'], 5],
162 // Referenceables less than 'foo', no limit. Expecting 5 items ('abc',
163 // 'bar_', 'baz_', NULL, '<strong>').
164 ['foo', '<', 0, 5, ['abc', 'bar_', 'baz_', NULL, '<strong>'], 5],
165 // Referenceables less or identical with 'baz_', no limit. Expecting 5
166 // items ('abc', 'bar_', 'baz_', NULL, '<strong>').
167 ['baz_', '<=', 0, 5, ['abc', 'bar_', 'baz_', NULL, '<strong>'], 5],
168 // Referenceables not identical with 'baz_', no limit. Expecting 7 items
169 // ('abc', 'Xyz_', 'xyabz_', 'foo_', 'bar_', 'șz_', NULL, '<strong>').
170 ['baz_', '<>', 0, 8, ['abc', 'Xyz_', 'xyabz_', 'foo_', 'bar_', 'șz_', NULL, '<strong>'], 8],
171 // Referenceables in ('bar_', 'baz_'), no limit. Expecting 2 items
173 [['bar_', 'baz_'], 'IN', 0, 2, ['bar_', 'baz_'], 2],
174 // Referenceables not in ('bar_', 'baz_'), no limit. Expecting 6 items
175 // ('abc', 'Xyz_', 'xyabz_', 'foo_', 'șz_', NULL, '<strong>')
176 [['bar_', 'baz_'], 'NOT IN', 0, 7, ['abc', 'Xyz_', 'xyabz_', 'foo_', 'șz_', NULL, '<strong>'], 7],
177 // Referenceables not null, no limit. Expecting 9 items ('abc', 'Xyz_',
178 // 'xyabz_', 'foo_', 'bar_', 'baz_', 'șz_', NULL, '<strong>').
180 // Note: Even we set the name as NULL, when retrieving the label from the
181 // entity we'll get an empty string, meaning that this match operator
182 // will return TRUE every time.
183 [NULL, 'IS NOT NULL', 0, 9, static::$labels, 9],
184 // Referenceables null, no limit. Expecting 9 items ('abc', 'Xyz_',
185 // 'xyabz_', 'foo_', 'bar_', 'baz_', 'șz_', NULL, '<strong>').
187 // Note: Even we set the name as NULL, when retrieving the label from the
188 // entity we'll get an empty string, meaning that this match operator
189 // will return FALSE every time.
190 [NULL, 'IS NULL', 0, 9, static::$labels, 9],
191 // Referenceables containing '<strong>' markup, no limit. Expecting 1 item
193 ['<strong>', 'CONTAINS', 0, 1, ['<strong>'], 1],
194 // Test an unsupported operator. We expect no items.
195 ['abc', '*unsupported*', 0, 0, [], 0],