Skip to content
Snippets Groups Projects
Verified Commit e39882dd authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3046018 by claudiu.cristea: Convert SelectionTest into a Kernel test

(cherry picked from commit 2da5fecc)
parent 5e427993
No related branches found
No related tags found
No related merge requests found
<?php <?php
namespace Drupal\Tests\field\Functional\EntityReference\Views; namespace Drupal\Tests\field\Kernel\EntityReference\Views;
use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\BrowserTestBase; 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; use Drupal\views\Views;
use Drupal\field\Entity\FieldStorageConfig;
/** /**
* Tests entity reference selection handler. * Tests entity reference selection handler.
* *
* @group entity_reference * @group entity_reference
*/ */
class SelectionTest extends BrowserTestBase { class SelectionTest extends KernelTestBase {
public static $modules = ['node', 'views', 'entity_reference_test', 'entity_test']; use EntityReferenceTestTrait;
use NodeCreationTrait;
/** /**
* Nodes for testing. * {@inheritdoc}
*
* @var array
*/ */
protected $nodes = []; protected static $modules = [
'entity_reference_test',
'entity_test',
'field',
'filter',
'node',
'system',
'user',
'views',
];
/** /**
* The entity reference field to test. * Nodes for testing.
* *
* @var \Drupal\Core\Field\FieldDefinitionInterface * @var string[][]
*/ */
protected $field; protected $nodes = [];
/** /**
* {@inheritdoc} * {@inheritdoc}
...@@ -36,79 +46,42 @@ class SelectionTest extends BrowserTestBase { ...@@ -36,79 +46,42 @@ class SelectionTest extends BrowserTestBase {
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
// Create nodes. $this->installConfig(['entity_reference_test', 'filter']);
$type = $this->drupalCreateContentType()->id(); $this->installEntitySchema('user');
$node1 = $this->drupalCreateNode(['type' => $type]); $this->installEntitySchema('node');
$node2 = $this->drupalCreateNode(['type' => $type]);
$node3 = $this->drupalCreateNode(); // 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) { foreach ([$node1, $node2, $node3] as $node) {
$this->nodes[$node->getType()][$node->id()] = $node->label(); $this->nodes[$node->bundle()][$node->id()] = $node->label();
} }
// Create a field. // Create an entity reference field.
$field_storage = FieldStorageConfig::create([ $handler_settings = [
'field_name' => 'test_field', 'view' => [
'entity_type' => 'entity_test', 'view_name' => 'test_entity_reference',
'translatable' => FALSE, 'display_name' => 'entity_reference_1',
'settings' => [
'target_type' => 'node',
], ],
'type' => 'entity_reference', ];
'cardinality' => '1', $this->createEntityReferenceField('entity_test', 'test_bundle', 'test_field', $this->randomString(), 'node', 'views', $handler_settings);
]);
$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. * Tests the selection handler.
*/ */
public function testSelectionHandler() { public function testSelectionHandler() {
// Get values from selection handler. $field_config = FieldConfig::loadByName('entity_test', 'test_bundle', 'test_field');
$handler = $this->container->get('plugin.manager.entity_reference_selection')->getSelectionHandler($this->field); $selection_handler = $this->container->get('plugin.manager.entity_reference_selection')->getSelectionHandler($field_config);
$result = $handler->getReferenceableEntities();
// Tests the selection handler.
$result = $selection_handler->getReferenceableEntities();
$this->assertResults($result); $this->assertResults($result);
}
/**
* Tests the selection handler with a relationship.
*/
public function testSelectionHandlerRelationship() {
// Add a relationship to the view. // Add a relationship to the view.
$view = Views::getView('test_entity_reference'); $view = Views::getView('test_entity_reference');
$view->setDisplay(); $view->setDisplay();
...@@ -136,10 +109,28 @@ public function testSelectionHandlerRelationship() { ...@@ -136,10 +109,28 @@ public function testSelectionHandlerRelationship() {
$view->display_handler->setOption('query', $query_options); $view->display_handler->setOption('query', $query_options);
$view->save(); $view->save();
// Get values from the selection handler. // Tests the selection handler with a relationship.
$handler = $this->container->get('plugin.manager.entity_reference_selection')->getSelectionHandler($this->field); $result = $selection_handler->getReferenceableEntities();
$result = $handler->getReferenceableEntities();
$this->assertResults($result); $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.');
}
} }
...@@ -21,7 +21,7 @@ display: ...@@ -21,7 +21,7 @@ display:
position: null position: null
display_options: display_options:
access: access:
type: perm type: none
cache: cache:
type: tag type: tag
query: query:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment