Commit 2da5fecc authored by larowlan's avatar larowlan

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

parent e3dea8c6
<?php
namespace Drupal\Tests\field\Functional\EntityReference\Views;
namespace Drupal\Tests\field\Kernel\EntityReference\Views;
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\field\Entity\FieldStorageConfig;
/**
* Tests entity reference selection handler.
*
* @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.
*
* @var array
* {@inheritdoc}
*/
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}
......@@ -36,79 +46,42 @@ class SelectionTest extends BrowserTestBase {
protected function setUp() {
parent::setUp();
// Create nodes.
$type = $this->drupalCreateContentType()->id();
$node1 = $this->drupalCreateNode(['type' => $type]);
$node2 = $this->drupalCreateNode(['type' => $type]);
$node3 = $this->drupalCreateNode();
$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->getType()][$node->id()] = $node->label();
$this->nodes[$node->bundle()][$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',
// Create an entity reference field.
$handler_settings = [
'view' => [
'view_name' => 'test_entity_reference',
'display_name' => 'entity_reference_1',
],
'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.');
];
$this->createEntityReferenceField('entity_test', 'test_bundle', 'test_field', $this->randomString(), 'node', 'views', $handler_settings);
}
/**
* 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();
$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);
}
/**
* Tests the selection handler with a relationship.
*/
public function testSelectionHandlerRelationship() {
// Add a relationship to the view.
$view = Views::getView('test_entity_reference');
$view->setDisplay();
......@@ -136,10 +109,28 @@ public function testSelectionHandlerRelationship() {
$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();
// 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.');
}
}
......@@ -21,7 +21,7 @@ display:
position: null
display_options:
access:
type: perm
type: none
cache:
type: tag
query:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment