View source
<?php
namespace Drupal\Tests\views\Functional\Plugin;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\views\Views;
class DisplayEntityReferenceTest extends ViewTestBase {
use EntityReferenceTestTrait;
public static $testViews = [
'test_display_entity_reference',
];
protected static $modules = [
'entity_test',
'field',
'views_ui',
];
protected $defaultTheme = 'stark';
protected $fieldName;
protected $entityRefFieldName;
protected $fieldStorage;
protected $field;
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
$this
->drupalLogin($this
->drupalCreateUser([
'administer views',
]));
$this->fieldName = 'field_test_entity_ref_display';
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => $this->fieldName,
'entity_type' => 'entity_test',
'type' => 'text',
]);
$this->fieldStorage
->save();
$this->field = FieldConfig::create([
'field_storage' => $this->fieldStorage,
'bundle' => 'entity_test',
]);
$this->field
->save();
$this->entityRefFieldName = 'field_test_entity_ref_entity_ref';
$this
->createEntityReferenceField('entity_test', 'entity_test', $this->entityRefFieldName, NULL, 'entity_test');
for ($i = 0; $i < 5; $i++) {
EntityTest::create([
'bundle' => 'entity_test',
'name' => 'name' . $i,
$this->fieldName => 'text',
])
->save();
EntityTest::create([
'bundle' => 'entity_test',
'name' => 'name',
$this->fieldName => 'text' . $i,
])
->save();
}
EntityTest::create([
'bundle' => 'entity_test',
'name' => 'name',
$this->fieldName => 'tex',
])
->save();
EntityTest::create([
'bundle' => 'entity_test',
'name' => 'name',
$this->fieldName => 'TEX',
])
->save();
EntityTest::create([
'bundle' => 'entity_test',
'name' => 'name',
$this->fieldName => 'sometext',
])
->save();
}
public function testEntityReferenceDisplay() {
$this
->drupalGet('admin/structure/views/view/test_display_entity_reference/edit/entity_reference_1');
$this
->assertSession()
->linkByHrefNotExists('admin/structure/views/nojs/display/test_display_entity_reference/entity_reference_1/title');
$this
->drupalGet('admin/structure/views/nojs/add-handler/test_display_entity_reference/default/field');
$this
->submitForm([
'name[entity_test__' . $this->fieldName . '.' . $this->fieldName . ']' => TRUE,
], 'Add and configure fields');
$this
->submitForm([], 'Apply');
$this
->drupalGet('admin/structure/views/nojs/display/test_display_entity_reference/entity_reference_1/style_options');
$this
->assertSession()
->pageTextContains('Test entity: Name');
$this
->assertSession()
->pageTextContains('Test entity: ' . $this->field
->label());
$this
->submitForm([
'style_options[search_fields][' . $this->fieldName . ']' => $this->fieldName,
], 'Apply');
$this
->submitForm([], 'Save');
$view = Views::getView('test_display_entity_reference');
$view
->setDisplay('entity_reference_1');
$options = [
'match' => '1',
'match_operator' => 'CONTAINS',
'limit' => 0,
'ids' => NULL,
];
$view->display_handler
->setOption('entity_reference_options', $options);
$this
->executeView($view);
$this
->assertCount(2, $view->result, 'Search returned two rows');
$view
->destroy();
$view = Views::getView('test_display_entity_reference');
$view
->setDisplay('entity_reference_1');
$options = [
'match' => 'tex',
'match_operator' => 'CONTAINS',
'limit' => 0,
'ids' => NULL,
];
$view->display_handler
->setOption('entity_reference_options', $options);
$this
->executeView($view);
$this
->assertCount(13, $view->result, 'Search returned thirteen rows');
$view
->destroy();
$view = Views::getView('test_display_entity_reference');
$view
->setDisplay('entity_reference_1');
$options = [
'match' => 'tex',
'match_operator' => 'STARTS_WITH',
'limit' => 0,
'ids' => NULL,
];
$view->display_handler
->setOption('entity_reference_options', $options);
$this
->executeView($view);
$this
->assertCount(12, $view->result, 'Search returned twelve rows');
$view
->destroy();
$view = Views::getView('test_display_entity_reference');
$view
->setDisplay('entity_reference_1');
$options = [
'match' => 'tex',
'match_operator' => '=',
'limit' => 0,
'ids' => NULL,
];
$view->display_handler
->setOption('entity_reference_options', $options);
$this
->executeView($view);
$this
->assertCount(2, $view->result, 'Search returned two rows');
$view
->destroy();
$this
->drupalGet('admin/structure/views/nojs/add-handler/test_display_entity_reference/default/relationship');
$this
->submitForm([
'name[entity_test.user_id]' => TRUE,
], 'Add and configure relationships');
$this
->submitForm([], 'Apply');
$this
->drupalGet('admin/structure/views/nojs/add-handler/test_display_entity_reference/default/field');
$this
->submitForm([
'name[users_field_data.uid]' => TRUE,
], 'Add and configure fields');
$this
->submitForm([], 'Apply');
$this
->drupalGet('admin/structure/views/nojs/display/test_display_entity_reference/entity_reference_1/style_options');
$this
->submitForm([
'style_options[search_fields][uid]' => 'uid',
], 'Apply');
$this
->submitForm([], 'Save');
$view = Views::getView('test_display_entity_reference');
$view
->setDisplay('entity_reference_1');
$options = [
'match' => '2',
'match_operator' => 'CONTAINS',
'limit' => 0,
'ids' => NULL,
];
$view->display_handler
->setOption('entity_reference_options', $options);
$this
->executeView($view);
$this
->assertCount(2, $view->result, 'Search returned two rows');
$view
->destroy();
$this
->drupalGet('admin/structure/views/nojs/add-handler/test_display_entity_reference/default/relationship');
$this
->submitForm([
'name[entity_test__field_test_entity_ref_entity_ref.field_test_entity_ref_entity_ref]' => TRUE,
], 'Add and configure relationships');
$this
->submitForm([], 'Apply');
$this
->submitForm([], 'Save');
$view = Views::getView('test_display_entity_reference');
$view
->setDisplay('entity_reference_1');
$options = [
'match' => '1',
'match_operator' => 'CONTAINS',
'limit' => 0,
'ids' => [
1,
2,
],
];
$view->display_handler
->setOption('entity_reference_options', $options);
$this
->executeView($view);
$this
->assertCount(2, $view->result, 'Search returned two rows');
$view = Views::getView('test_display_entity_reference');
$view
->setDisplay('entity_reference_1');
$render = $view->display_handler
->render();
$this
->assertSame([], $render, 'Render returned empty array');
}
}