Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Entity / EntityReferenceSelection / EntityReferenceSelectionSortTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Entity\EntityReferenceSelection;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
8 use Drupal\node\Entity\Node;
9 use Drupal\node\Entity\NodeType;
10 use Drupal\field\Entity\FieldStorageConfig;
11
12 /**
13  * Tests sorting referenced items.
14  *
15  * @group entity_reference
16  */
17 class EntityReferenceSelectionSortTest extends EntityKernelTestBase {
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = ['node'];
25
26   protected function setUp() {
27     parent::setUp();
28
29     // Create an Article node type.
30     $article = NodeType::create([
31       'type' => 'article',
32     ]);
33     $article->save();
34
35     // Test as a non-admin.
36     $normal_user = $this->createUser([], ['access content']);
37     \Drupal::currentUser()->setAccount($normal_user);
38   }
39
40   /**
41    * Assert sorting by field and property.
42    */
43   public function testSort() {
44     // Add text field to entity, to sort by.
45     FieldStorageConfig::create([
46       'field_name' => 'field_text',
47       'entity_type' => 'node',
48       'type' => 'text',
49       'entity_types' => ['node'],
50     ])->save();
51
52     FieldConfig::create([
53       'label' => 'Text Field',
54       'field_name' => 'field_text',
55       'entity_type' => 'node',
56       'bundle' => 'article',
57       'settings' => [],
58       'required' => FALSE,
59     ])->save();
60
61     // Build a set of test data.
62     $node_values = [
63       'published1' => [
64         'type' => 'article',
65         'status' => 1,
66         'title' => 'Node published1 (<&>)',
67         'uid' => 1,
68         'field_text' => [
69           [
70             'value' => 1,
71           ],
72         ],
73       ],
74       'published2' => [
75         'type' => 'article',
76         'status' => 1,
77         'title' => 'Node published2 (<&>)',
78         'uid' => 1,
79         'field_text' => [
80           [
81             'value' => 2,
82           ],
83         ],
84       ],
85     ];
86
87     $nodes = [];
88     $node_labels = [];
89     foreach ($node_values as $key => $values) {
90       $node = Node::create($values);
91       $node->save();
92       $nodes[$key] = $node;
93       $node_labels[$key] = Html::escape($node->label());
94     }
95
96     $selection_options = [
97       'target_type' => 'node',
98       'handler' => 'default',
99       'handler_settings' => [
100         'target_bundles' => NULL,
101         // Add sorting.
102         'sort' => [
103           'field' => 'field_text.value',
104           'direction' => 'DESC',
105         ],
106       ],
107     ];
108     $handler = $this->container->get('plugin.manager.entity_reference_selection')->getInstance($selection_options);
109
110     // Not only assert the result, but make sure the keys are sorted as
111     // expected.
112     $result = $handler->getReferenceableEntities();
113     $expected_result = [
114       $nodes['published2']->id() => $node_labels['published2'],
115       $nodes['published1']->id() => $node_labels['published1'],
116     ];
117     $this->assertIdentical($result['article'], $expected_result, 'Query sorted by field returned expected values.');
118
119     // Assert sort by base field.
120     $selection_options['handler_settings']['sort'] = [
121       'field' => 'nid',
122       'direction' => 'ASC',
123     ];
124     $handler = $this->container->get('plugin.manager.entity_reference_selection')->getInstance($selection_options);
125     $result = $handler->getReferenceableEntities();
126     $expected_result = [
127       $nodes['published1']->id() => $node_labels['published1'],
128       $nodes['published2']->id() => $node_labels['published2'],
129     ];
130     $this->assertIdentical($result['article'], $expected_result, 'Query sorted by property returned expected values.');
131   }
132
133 }