View source
<?php
namespace Drupal\views\Tests\Handler;
use Drupal\block\Entity\Block;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormState;
use Drupal\views\Entity\View;
use Drupal\views\Tests\ViewKernelTestBase;
use Drupal\views\Views;
class AreaEntityTest extends ViewKernelTestBase {
public static $modules = [
'entity_test',
'user',
'block',
];
public static $testViews = array(
'test_entity_area',
);
protected function setUp() {
parent::setUp();
}
protected function setUpFixtures() {
$this
->installEntitySchema('user');
$this
->installEntitySchema('entity_test');
$this
->installConfig([
'entity_test',
]);
Block::create([
'id' => 'test_block',
'plugin' => 'system_main_block',
])
->save();
parent::setUpFixtures();
}
public function testEntityAreaData() {
$data = $this->container
->get('views.views_data')
->get('views');
$entity_types = $this->container
->get('entity.manager')
->getDefinitions();
$expected_entities = array_filter($entity_types, function (EntityTypeInterface $entity_type) {
return $entity_type
->hasViewBuilderClass();
});
foreach (array_keys($expected_entities) as $entity) {
$this
->assertTrue(!empty($data['entity_' . $entity]), format_string('Views entity area data found for @entity', array(
'@entity' => $entity,
)));
$this
->assertEqual($entity, $data['entity_' . $entity]['area']['entity_type'], format_string('Correct entity_type set for @entity', array(
'@entity' => $entity,
)));
}
$expected_entities = array_filter($entity_types, function (EntityTypeInterface $type) {
return !$type
->hasViewBuilderClass();
});
foreach (array_keys($expected_entities) as $entity) {
$this
->assertTrue(empty($data['entity_' . $entity]), format_string('Views config entity area data not found for @entity', array(
'@entity' => $entity,
)));
}
}
public function testEntityArea() {
$entities = array();
for ($i = 0; $i < 3; $i++) {
$random_label = $this
->randomMachineName();
$data = array(
'bundle' => 'entity_test',
'name' => $random_label,
);
$entity_test = $this->container
->get('entity.manager')
->getStorage('entity_test')
->create($data);
$uuid_map[0] = 'aa0c61cb-b7bb-4795-972a-493dabcf529c';
$uuid_map[1] = '62cef0ff-6f30-4f7a-b9d6-a8ed5a3a6bf3';
$uuid_map[2] = '3161d6e9-3326-4719-b513-8fa68a731ba2';
$entity_test->uuid->value = $uuid_map[$i];
$entity_test
->save();
$entities[] = $entity_test;
\Drupal::state()
->set('entity_test_entity_access.view.' . $entity_test
->id(), $i != 2);
}
$this
->doTestCalculateDependencies();
$this
->doTestRender($entities);
}
public function doTestRender($entities) {
$renderer = $this->container
->get('renderer');
$view = Views::getView('test_entity_area');
$preview = $view
->preview('default', [
$entities[1]
->id(),
]);
$this
->setRawContent(\Drupal::service('renderer')
->renderRoot($preview));
$view_class = 'js-view-dom-id-' . $view->dom_id;
$header_xpath = '//div[@class = "' . $view_class . '"]/header[1]';
$footer_xpath = '//div[@class = "' . $view_class . '"]/footer[1]';
$result = $this
->xpath($header_xpath);
$this
->assertTrue(strpos(trim((string) $result[0]), $entities[0]
->label()) !== FALSE, 'The rendered entity appears in the header of the view.');
$this
->assertTrue(strpos(trim((string) $result[0]), 'full') !== FALSE, 'The rendered entity appeared in the right view mode.');
$result = $this
->xpath($footer_xpath);
$this
->assertTrue(strpos(trim((string) $result[0]), $entities[1]
->label()) !== FALSE, 'The rendered entity appears in the footer of the view.');
$this
->assertTrue(strpos(trim((string) $result[0]), 'full') !== FALSE, 'The rendered entity appeared in the right view mode.');
$preview = $view
->preview('default', array(
$entities[1]
->id(),
));
$this
->setRawContent($renderer
->renderRoot($preview));
$result = $this
->xpath($header_xpath);
$this
->assertTrue(strpos(trim((string) $result[0]), $entities[0]
->label()) !== FALSE, 'The rendered entity appears in the header of the view.');
$this
->assertTrue(strpos(trim((string) $result[0]), 'full') !== FALSE, 'The rendered entity appeared in the right view mode.');
$result = $this
->xpath($footer_xpath);
$this
->assertTrue(strpos(trim((string) $result[0]), $entities[1]
->label()) !== FALSE, 'The rendered entity appears in the footer of the view.');
$this
->assertTrue(strpos(trim((string) $result[0]), 'full') !== FALSE, 'The rendered entity appeared in the right view mode.');
$entity_view_mode = \Drupal::entityManager()
->getStorage('entity_view_mode')
->load('entity_test.test');
$entity_view_mode
->enable();
$entity_view_mode
->save();
$view = Views::getView('test_entity_area');
$item = $view
->getHandler('default', 'header', 'entity_entity_test');
$item['view_mode'] = 'test';
$view
->setHandler('default', 'header', 'entity_entity_test', $item);
$preview = $view
->preview('default', array(
$entities[1]
->id(),
));
$this
->setRawContent($renderer
->renderRoot($preview));
$view_class = 'js-view-dom-id-' . $view->dom_id;
$result = $this
->xpath('//div[@class = "' . $view_class . '"]/header[1]');
$this
->assertTrue(strpos(trim((string) $result[0]), $entities[0]
->label()) !== FALSE, 'The rendered entity appears in the header of the view.');
$this
->assertTrue(strpos(trim((string) $result[0]), 'test') !== FALSE, 'The rendered entity appeared in the right view mode.');
$view = Views::getView('test_entity_area');
$preview = $view
->preview('default', array(
$entities[2]
->id(),
));
$this
->setRawContent($renderer
->renderRoot($preview));
$view_class = 'js-view-dom-id-' . $view->dom_id;
$result = $this
->xpath('//div[@class = "' . $view_class . '"]/footer[1]');
$this
->assertTrue(strpos($result[0], $entities[2]
->label()) === FALSE, 'The rendered entity does not appear in the footer of the view.');
$form = array();
$form_state = (new FormState())
->set('type', 'header');
$view->display_handler
->getHandler('header', 'entity_entity_test')
->buildOptionsForm($form, $form_state);
$this
->assertTrue(isset($form['view_mode']['#options']['test']), 'Ensure that the test view mode is available.');
$this
->assertTrue(isset($form['view_mode']['#options']['default']), 'Ensure that the default view mode is available.');
}
public function doTestCalculateDependencies() {
$view = View::load('test_entity_area');
$dependencies = $view
->calculateDependencies()
->getDependencies();
$this
->assertEqual([
'config' => [
'block.block.test_block',
],
'content' => [
'entity_test:entity_test:aa0c61cb-b7bb-4795-972a-493dabcf529c',
],
], $dependencies);
}
}