Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / system / tests / src / Kernel / Entity / EntityReferenceSelectionReferenceableTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Kernel\Entity;
4
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;
10
11 /**
12  * Tests entity reference selection plugins.
13  *
14  * @group entity_reference
15  */
16 class EntityReferenceSelectionReferenceableTest extends KernelTestBase {
17
18   use EntityReferenceTestTrait;
19
20   /**
21    * Bundle of 'entity_test_no_label' entity.
22    *
23    * @var string
24    */
25   protected $bundle;
26
27   /**
28    * Labels to be tested.
29    *
30    * @var array
31    */
32   protected static $labels = ['abc', 'Xyz_', 'xyabz_', 'foo_', 'bar_', 'baz_', 'șz_', NULL, '<strong>'];
33
34   /**
35    * The selection handler.
36    *
37    * @var \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface
38    */
39   protected $selectionHandler;
40
41   /**
42    * {@inheritdoc}
43    */
44   public static $modules = ['system', 'user', 'field', 'entity_reference', 'node', 'entity_test'];
45
46   /**
47    * {@inheritdoc}
48    */
49   protected function setUp() {
50     parent::setUp();
51
52     $this->installEntitySchema('entity_test_no_label');
53
54     /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
55     $storage = $this->container->get('entity.manager')
56       ->getStorage('entity_test_no_label');
57
58     // Create a new node-type.
59     NodeType::create([
60       'type' => $node_type = mb_strtolower($this->randomMachineName()),
61       'name' => $this->randomString(),
62     ])->save();
63
64     // Create an entity reference field targeting 'entity_test_no_label'
65     // entities.
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);
70
71     // Generate a bundle name to be used with 'entity_test_no_label'.
72     $this->bundle = mb_strtolower($this->randomMachineName());
73
74     // Create 6 entities to be referenced by the field.
75     foreach (static::$labels as $name) {
76       $storage->create([
77         'id' => mb_strtolower($this->randomMachineName()),
78         'name' => $name,
79         'type' => $this->bundle,
80       ])->save();
81     }
82   }
83
84   /**
85    * Tests values returned by SelectionInterface::getReferenceableEntities()
86    * when the target entity type has no 'label' key.
87    *
88    * @param mixed $match
89    *   The input text to be checked.
90    * @param string $match_operator
91    *   The matching operator.
92    * @param int $limit
93    *   The limit of returning records.
94    * @param int $count_limited
95    *   The expected number of limited entities to be retrieved.
96    * @param array $items
97    *   Array of entity labels expected to be returned.
98    * @param int $count_all
99    *   The total number (unlimited) of entities to be retrieved.
100    *
101    * @dataProvider providerTestCases
102    */
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);
106
107     // Number of returned items.
108     if (empty($count_limited)) {
109       $this->assertTrue(empty($referenceables[$this->bundle]));
110     }
111     else {
112       $this->assertSame(count($referenceables[$this->bundle]), $count_limited);
113     }
114
115     // Test returned items.
116     foreach ($items as $item) {
117       // SelectionInterface::getReferenceableEntities() always return escaped
118       // entity labels.
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);
122     }
123
124     // Test ::countReferenceableEntities().
125     $count_referenceables = $this->selectionHandler->countReferenceableEntities($match, $match_operator);
126     $this->assertSame($count_referenceables, $count_all);
127   }
128
129   /**
130    * Provides test cases for ::testReferenceablesWithNoLabelKey() test.
131    *
132    * @return array[]
133    */
134   public function providerTestCases() {
135     return [
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',
143       // 'xyabz').
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
154       // ('xyabz_').
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
172       // ('bar_', 'baz_')
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>').
179       //
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>').
186       //
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
192       // ('<strong>').
193       ['<strong>', 'CONTAINS', 0, 1, ['<strong>'], 1],
194       // Test an unsupported operator. We expect no items.
195       ['abc', '*unsupported*', 0, 0, [], 0],
196     ];
197   }
198
199 }