EntityReferenceSelectionSortTest.php 3.5 KB

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