diff --git a/core/modules/field/tests/src/Functional/EntityReference/Views/SelectionTest.php b/core/modules/field/tests/src/Functional/EntityReference/Views/SelectionTest.php deleted file mode 100644 index 6e12a8e519d251ac3efc9131429e9774b470f97a..0000000000000000000000000000000000000000 --- a/core/modules/field/tests/src/Functional/EntityReference/Views/SelectionTest.php +++ /dev/null @@ -1,145 +0,0 @@ -<?php - -namespace Drupal\Tests\field\Functional\EntityReference\Views; - -use Drupal\field\Entity\FieldConfig; -use Drupal\Tests\BrowserTestBase; -use Drupal\views\Views; -use Drupal\field\Entity\FieldStorageConfig; - -/** - * Tests entity reference selection handler. - * - * @group entity_reference - */ -class SelectionTest extends BrowserTestBase { - - public static $modules = ['node', 'views', 'entity_reference_test', 'entity_test']; - - /** - * Nodes for testing. - * - * @var array - */ - protected $nodes = []; - - /** - * The entity reference field to test. - * - * @var \Drupal\Core\Field\FieldDefinitionInterface - */ - protected $field; - - /** - * {@inheritdoc} - */ - protected function setUp() { - parent::setUp(); - - // Create nodes. - $type = $this->drupalCreateContentType()->id(); - $node1 = $this->drupalCreateNode(['type' => $type]); - $node2 = $this->drupalCreateNode(['type' => $type]); - $node3 = $this->drupalCreateNode(); - - foreach ([$node1, $node2, $node3] as $node) { - $this->nodes[$node->getType()][$node->id()] = $node->label(); - } - - // Create a field. - $field_storage = FieldStorageConfig::create([ - 'field_name' => 'test_field', - 'entity_type' => 'entity_test', - 'translatable' => FALSE, - 'settings' => [ - 'target_type' => 'node', - ], - 'type' => 'entity_reference', - 'cardinality' => '1', - ]); - $field_storage->save(); - $field = FieldConfig::create([ - 'field_storage' => $field_storage, - 'bundle' => 'test_bundle', - 'settings' => [ - 'handler' => 'views', - 'handler_settings' => [ - 'view' => [ - 'view_name' => 'test_entity_reference', - 'display_name' => 'entity_reference_1', - 'arguments' => [], - ], - ], - ], - ]); - $field->save(); - $this->field = $field; - } - - /** - * Confirm the expected results are returned. - * - * @param array $result - * Query results keyed by node type and nid. - */ - protected function assertResults(array $result) { - $success = FALSE; - foreach ($result as $node_type => $values) { - foreach ($values as $nid => $label) { - if (!$success = $this->nodes[$node_type][$nid] == trim(strip_tags($label))) { - // There was some error, so break. - break; - } - } - } - $this->assertTrue($success, 'Views selection handler returned expected values.'); - } - - /** - * Tests the selection handler. - */ - public function testSelectionHandler() { - // Get values from selection handler. - $handler = $this->container->get('plugin.manager.entity_reference_selection')->getSelectionHandler($this->field); - $result = $handler->getReferenceableEntities(); - $this->assertResults($result); - } - - /** - * Tests the selection handler with a relationship. - */ - public function testSelectionHandlerRelationship() { - // Add a relationship to the view. - $view = Views::getView('test_entity_reference'); - $view->setDisplay(); - $view->displayHandlers->get('default')->setOption('relationships', [ - 'test_relationship' => [ - 'id' => 'uid', - 'table' => 'node_field_data', - 'field' => 'uid', - ], - ]); - - // Add a filter depending on the relationship to the test view. - $view->displayHandlers->get('default')->setOption('filters', [ - 'uid' => [ - 'id' => 'uid', - 'table' => 'users_field_data', - 'field' => 'uid', - 'relationship' => 'test_relationship', - ], - ]); - - // Set view to distinct so only one row per node is returned. - $query_options = $view->display_handler->getOption('query'); - $query_options['options']['distinct'] = TRUE; - $view->display_handler->setOption('query', $query_options); - $view->save(); - - // Get values from the selection handler. - $handler = $this->container->get('plugin.manager.entity_reference_selection')->getSelectionHandler($this->field); - $result = $handler->getReferenceableEntities(); - $this->assertResults($result); - } - -} diff --git a/core/modules/field/tests/src/Kernel/EntityReference/Views/SelectionTest.php b/core/modules/field/tests/src/Kernel/EntityReference/Views/SelectionTest.php new file mode 100644 index 0000000000000000000000000000000000000000..5a41448201ef38eb2513b55361a92dcb3d9b8695 --- /dev/null +++ b/core/modules/field/tests/src/Kernel/EntityReference/Views/SelectionTest.php @@ -0,0 +1,136 @@ +<?php + +namespace Drupal\Tests\field\Kernel\EntityReference\Views; + +use Drupal\field\Entity\FieldConfig; +use Drupal\KernelTests\KernelTestBase; +use Drupal\node\Entity\NodeType; +use Drupal\Tests\field\Traits\EntityReferenceTestTrait; +use Drupal\Tests\node\Traits\NodeCreationTrait; +use Drupal\views\Views; + +/** + * Tests entity reference selection handler. + * + * @group entity_reference + */ +class SelectionTest extends KernelTestBase { + + use EntityReferenceTestTrait; + use NodeCreationTrait; + + /** + * {@inheritdoc} + */ + protected static $modules = [ + 'entity_reference_test', + 'entity_test', + 'field', + 'filter', + 'node', + 'system', + 'user', + 'views', + ]; + + /** + * Nodes for testing. + * + * @var string[][] + */ + protected $nodes = []; + + /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + + $this->installConfig(['entity_reference_test', 'filter']); + $this->installEntitySchema('user'); + $this->installEntitySchema('node'); + + // Create test nodes. + $type = strtolower($this->randomMachineName()); + NodeType::create(['type' => $type])->save(); + $node1 = $this->createNode(['type' => $type]); + $node2 = $this->createNode(['type' => $type]); + $node3 = $this->createNode(); + + foreach ([$node1, $node2, $node3] as $node) { + $this->nodes[$node->bundle()][$node->id()] = $node->label(); + } + + // Create an entity reference field. + $handler_settings = [ + 'view' => [ + 'view_name' => 'test_entity_reference', + 'display_name' => 'entity_reference_1', + ], + ]; + $this->createEntityReferenceField('entity_test', 'test_bundle', 'test_field', $this->randomString(), 'node', 'views', $handler_settings); + } + + /** + * Tests the selection handler. + */ + public function testSelectionHandler() { + $field_config = FieldConfig::loadByName('entity_test', 'test_bundle', 'test_field'); + $selection_handler = $this->container->get('plugin.manager.entity_reference_selection')->getSelectionHandler($field_config); + + // Tests the selection handler. + $result = $selection_handler->getReferenceableEntities(); + $this->assertResults($result); + + // Add a relationship to the view. + $view = Views::getView('test_entity_reference'); + $view->setDisplay(); + $view->displayHandlers->get('default')->setOption('relationships', [ + 'test_relationship' => [ + 'id' => 'uid', + 'table' => 'node_field_data', + 'field' => 'uid', + ], + ]); + + // Add a filter depending on the relationship to the test view. + $view->displayHandlers->get('default')->setOption('filters', [ + 'uid' => [ + 'id' => 'uid', + 'table' => 'users_field_data', + 'field' => 'uid', + 'relationship' => 'test_relationship', + ], + ]); + + // Set view to distinct so only one row per node is returned. + $query_options = $view->display_handler->getOption('query'); + $query_options['options']['distinct'] = TRUE; + $view->display_handler->setOption('query', $query_options); + $view->save(); + + // Tests the selection handler with a relationship. + $result = $selection_handler->getReferenceableEntities(); + $this->assertResults($result); + } + + /** + * Confirm the expected results are returned. + * + * @param array $result + * Query results keyed by node type and nid. + */ + protected function assertResults(array $result) { + $success = FALSE; + foreach ($result as $node_type => $values) { + foreach ($values as $nid => $label) { + if (!$success = $this->nodes[$node_type][$nid] == trim(strip_tags($label))) { + // There was some error, so break. + break; + } + } + } + $this->assertTrue($success, 'Views selection handler returned expected values.'); + } + +} diff --git a/core/modules/system/tests/modules/entity_reference_test/config/install/views.view.test_entity_reference.yml b/core/modules/system/tests/modules/entity_reference_test/config/install/views.view.test_entity_reference.yml index 3e78c51080f5f7ffc7dd365922e1acb50be71f82..1bcb19c455bdbadae8cc9b952454eb72fe48998d 100644 --- a/core/modules/system/tests/modules/entity_reference_test/config/install/views.view.test_entity_reference.yml +++ b/core/modules/system/tests/modules/entity_reference_test/config/install/views.view.test_entity_reference.yml @@ -21,7 +21,7 @@ display: position: null display_options: access: - type: perm + type: none cache: type: tag query: