FilterEntityBundleTest.php in Zircon Profile 8.0
File
core/modules/views/src/Tests/Entity/FilterEntityBundleTest.php
View source
<?php
namespace Drupal\views\Tests\Entity;
use Drupal\views\Tests\ViewTestBase;
use Drupal\views\Tests\ViewTestData;
use Drupal\views\Views;
class FilterEntityBundleTest extends ViewTestBase {
public static $testViews = array(
'test_entity_type_filter',
);
public static $modules = array(
'node',
);
protected $entityBundles;
protected $entities = array();
protected function setUp() {
parent::setUp(FALSE);
$this
->drupalCreateContentType(array(
'type' => 'test_bundle',
));
$this
->drupalCreateContentType(array(
'type' => 'test_bundle_2',
));
ViewTestData::createTestViews(get_class($this), array(
'views_test_config',
));
$this->entityBundles = entity_get_bundles('node');
$this->entities['count'] = 0;
foreach ($this->entityBundles as $key => $info) {
for ($i = 0; $i < 5; $i++) {
$entity = entity_create('node', array(
'title' => $this
->randomString(),
'uid' => 1,
'type' => $key,
));
$entity
->save();
$this->entities[$key][$entity
->id()] = $entity;
$this->entities['count']++;
}
}
}
public function testFilterEntity() {
$view = Views::getView('test_entity_type_filter');
$expected = [
'config' => [
'node.type.test_bundle',
'node.type.test_bundle_2',
],
'module' => [
'node',
],
];
$this
->assertIdentical($expected, $view
->getDependencies());
$this
->executeView($view);
$this
->assertEqual(count($view->result), $this->entities['count']);
$expected = array();
foreach ($this->entityBundles as $key => $info) {
$expected[$key] = $info['label'];
}
$this
->assertIdentical($view->filter['type']
->getValueOptions(), $expected);
$view
->destroy();
foreach ($this->entityBundles as $key => $info) {
$view
->initDisplay();
$filters = $view->display_handler
->getOption('filters');
$filters['type']['value'] = array(
$key => $key,
);
$view->display_handler
->setOption('filters', $filters);
$this
->executeView($view);
$this
->assertEqual(count($view->result), count($this->entities[$key]));
$view
->destroy();
}
$view
->initDisplay();
$filters = $view->display_handler
->getOption('filters');
$filters['type']['value'] = array(
'type_3' => 'type_3',
);
$view->display_handler
->setOption('filters', $filters);
$this
->executeView($view);
$this
->assertEqual(count($view->result), 0);
}
}