Newer
Older
Dries Buytaert
committed
<?php
/**
* @file
Alex Pott
committed
* Contains \Drupal\field\Tests\EntityReference\Views\SelectionTest.
Dries Buytaert
committed
*/
Alex Pott
committed
namespace Drupal\field\Tests\EntityReference\Views;
Dries Buytaert
committed
use Drupal\simpletest\WebTestBase;
Alex Pott
committed
use Drupal\views\Views;
Dries Buytaert
committed
/**
* Tests entity reference selection handler.
*
* @group entity_reference
Dries Buytaert
committed
*/
class SelectionTest extends WebTestBase {
Alex Pott
committed
public static $modules = ['node', 'views', 'entity_reference_test', 'entity_test'];
Dries Buytaert
committed
/**
Alex Pott
committed
* Nodes for testing.
*
* @var array
Alex Pott
committed
protected $nodes = array();
/**
* The entity reference field to test.
*
* @var \Drupal\Core\Field\FieldDefinitionInterface
*/
protected $field;
/**
* {@inheritdoc}
*/
Alex Pott
committed
protected function setUp() {
Alex Pott
committed
parent::setUp();
Dries Buytaert
committed
// Create nodes.
$type = $this->drupalCreateContentType()->id();
Dries Buytaert
committed
$node1 = $this->drupalCreateNode(array('type' => $type));
$node2 = $this->drupalCreateNode(array('type' => $type));
$node3 = $this->drupalCreateNode();
foreach (array($node1, $node2, $node3) as $node) {
Alex Pott
committed
$this->nodes[$node->getType()][$node->id()] = $node->label();
Dries Buytaert
committed
}
// Create a field.
$field_storage = entity_create('field_storage_config', array(
Alex Pott
committed
'field_name' => 'test_field',
'entity_type' => 'entity_test',
Dries Buytaert
committed
'translatable' => FALSE,
'settings' => array(
'target_type' => 'node',
),
'type' => 'entity_reference',
'cardinality' => '1',
$field_storage->save();
$field = entity_create('field_config', array(
'field_storage' => $field_storage,
'bundle' => 'test_bundle',
Dries Buytaert
committed
'settings' => array(
'handler' => 'views',
'handler_settings' => array(
'view' => array(
'view_name' => 'test_entity_reference',
'display_name' => 'entity_reference_1',
'arguments' => array(),
),
),
),
$field->save();
Alex Pott
committed
$this->field = $field;
}
Dries Buytaert
committed
Alex Pott
committed
/**
* Confirm the expected results are returned.
*
* @param array $result
* Query results keyed by node type and nid.
*/
protected function assertResults(array $result) {
Dries Buytaert
committed
$success = FALSE;
foreach ($result as $node_type => $values) {
foreach ($values as $nid => $label) {
Alex Pott
committed
if (!$success = $this->nodes[$node_type][$nid] == trim(strip_tags($label))) {
Dries Buytaert
committed
// There was some error, so break.
break;
}
}
}
Alex Pott
committed
$this->assertTrue($success, 'Views selection handler returned expected values.');
Alex Pott
committed
/**
* Tests the selection handler.
*/
public function testSelectionHandler() {
// Get values from selection handler.
Alex Pott
committed
$handler = $this->container->get('plugin.manager.entity_reference_selection')->getSelectionHandler($this->field);
Alex Pott
committed
$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', array(
'test_relationship' => array(
'id' => 'uid',
Angie Byron
committed
'table' => 'node_field_data',
Alex Pott
committed
'field' => 'uid',
),
));
// Add a filter depending on the relationship to the test view.
$view->displayHandlers->get('default')->setOption('filters', array(
'uid' => array(
'id' => 'uid',
Angie Byron
committed
'table' => 'users_field_data',
Alex Pott
committed
'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.
Alex Pott
committed
$handler = $this->container->get('plugin.manager.entity_reference_selection')->getSelectionHandler($this->field);
Alex Pott
committed
$result = $handler->getReferenceableEntities();
$this->assertResults($result);
}