/web/core/modules/field/tests/src/Functional/EntityReference/Views/SelectionTest.php

https://gitlab.com/mohamed_hussein/prodt · PHP · 124 lines · 70 code · 19 blank · 35 comment · 0 complexity · 01bfd9436a46685747c9019424321e13 MD5 · raw file

  1. <?php
  2. namespace Drupal\Tests\field\Functional\EntityReference\Views;
  3. use Drupal\Component\Serialization\Json;
  4. use Drupal\Component\Utility\Crypt;
  5. use Drupal\Component\Utility\Html;
  6. use Drupal\Core\Site\Settings;
  7. use Drupal\Tests\BrowserTestBase;
  8. use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
  9. use Drupal\views\Views;
  10. /**
  11. * Tests entity reference selection handler.
  12. *
  13. * @group entity_reference
  14. */
  15. class SelectionTest extends BrowserTestBase {
  16. use EntityReferenceTestTrait;
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected static $modules = [
  21. 'node',
  22. 'views',
  23. 'entity_reference_test',
  24. 'entity_test',
  25. ];
  26. /**
  27. * {@inheritdoc}
  28. */
  29. protected $defaultTheme = 'stark';
  30. /**
  31. * An array of node titles, keyed by content type and node ID.
  32. *
  33. * @var \Drupal\node\NodeInterface[]
  34. */
  35. protected $nodes = [];
  36. /**
  37. * The handler settings for the entity reference field.
  38. *
  39. * @var array
  40. */
  41. protected $handlerSettings;
  42. /**
  43. * {@inheritdoc}
  44. */
  45. protected function setUp(): void {
  46. parent::setUp();
  47. // Create content types and nodes.
  48. $type1 = $this->drupalCreateContentType()->id();
  49. $type2 = $this->drupalCreateContentType()->id();
  50. // Add some characters that should be escaped but not double escaped.
  51. $node1 = $this->drupalCreateNode(['type' => $type1, 'title' => 'Test first node &<>']);
  52. $node2 = $this->drupalCreateNode(['type' => $type1, 'title' => 'Test second node &&&']);
  53. $node3 = $this->drupalCreateNode(['type' => $type2, 'title' => 'Test third node <span />']);
  54. foreach ([$node1, $node2, $node3] as $node) {
  55. $this->nodes[$node->id()] = $node;
  56. }
  57. // Create an entity reference field.
  58. $handler_settings = [
  59. 'view' => [
  60. 'view_name' => 'test_entity_reference',
  61. 'display_name' => 'entity_reference_1',
  62. ],
  63. ];
  64. $this->handlerSettings = $handler_settings;
  65. $this->createEntityReferenceField('entity_test', 'test_bundle', 'test_field', $this->randomString(), 'node', 'views', $handler_settings);
  66. }
  67. /**
  68. * Tests that the Views selection handles the views output properly.
  69. */
  70. public function testAutocompleteOutput() {
  71. // Reset any internal static caching.
  72. \Drupal::service('entity_type.manager')->getStorage('node')->resetCache();
  73. $view = Views::getView('test_entity_reference');
  74. $view->setDisplay();
  75. // Enable the display of the 'type' field so we can test that the output
  76. // does not contain only the entity label.
  77. $fields = $view->displayHandlers->get('entity_reference_1')->getOption('fields');
  78. $fields['type']['exclude'] = FALSE;
  79. $view->displayHandlers->get('entity_reference_1')->setOption('fields', $fields);
  80. $view->save();
  81. // Prepare the selection settings key needed by the entity reference
  82. // autocomplete route.
  83. $target_type = 'node';
  84. $selection_handler = 'views';
  85. $selection_settings = $this->handlerSettings;
  86. $selection_settings_key = Crypt::hmacBase64(serialize($selection_settings) . $target_type . $selection_handler, Settings::getHashSalt());
  87. \Drupal::keyValue('entity_autocomplete')->set($selection_settings_key, $selection_settings);
  88. $result = Json::decode($this->drupalGet('entity_reference_autocomplete/' . $target_type . '/' . $selection_handler . '/' . $selection_settings_key, ['query' => ['q' => 't']]));
  89. $expected = [
  90. 0 => [
  91. 'value' => $this->nodes[1]->bundle() . ': ' . $this->nodes[1]->label() . ' (' . $this->nodes[1]->id() . ')',
  92. 'label' => '<span class="views-field views-field-type"><span class="field-content">' . $this->nodes[1]->bundle() . '</span></span>: <span class="views-field views-field-title"><span class="field-content">' . Html::escape($this->nodes[1]->label()) . '</span></span>',
  93. ],
  94. 1 => [
  95. 'value' => $this->nodes[2]->bundle() . ': ' . $this->nodes[2]->label() . ' (' . $this->nodes[2]->id() . ')',
  96. 'label' => '<span class="views-field views-field-type"><span class="field-content">' . $this->nodes[2]->bundle() . '</span></span>: <span class="views-field views-field-title"><span class="field-content">' . Html::escape($this->nodes[2]->label()) . '</span></span>',
  97. ],
  98. 2 => [
  99. 'value' => $this->nodes[3]->bundle() . ': ' . $this->nodes[3]->label() . ' (' . $this->nodes[3]->id() . ')',
  100. 'label' => '<span class="views-field views-field-type"><span class="field-content">' . $this->nodes[3]->bundle() . '</span></span>: <span class="views-field views-field-title"><span class="field-content">' . Html::escape($this->nodes[3]->label()) . '</span></span>',
  101. ],
  102. ];
  103. $this->assertEquals($expected, $result, 'The autocomplete result of the Views entity reference selection handler contains the proper output.');
  104. }
  105. }