22bb6435c87df83f94a8510e5ea01feeb35c09f6
[yaffs-website] / web / core / modules / views / tests / src / Functional / Entity / FilterEntityBundleTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Functional\Entity;
4
5 use Drupal\node\Entity\Node;
6 use Drupal\Tests\views\Functional\ViewTestBase;
7 use Drupal\views\Tests\ViewTestData;
8 use Drupal\views\Views;
9
10 /**
11  * Tests the generic entity bundle filter.
12  *
13  * @group views
14  */
15 class FilterEntityBundleTest extends ViewTestBase {
16
17   /**
18    * Views used by this test.
19    *
20    * @var array
21    */
22   public static $testViews = ['test_entity_type_filter'];
23
24   /**
25    * Modules to enable.
26    *
27    * @var array
28    */
29   public static $modules = ['node'];
30
31   /**
32    * Entity bundle data.
33    *
34    * @var array
35    */
36   protected $entityBundles;
37
38   /**
39    * An array of entities.
40    *
41    * @var array
42    */
43   protected $entities = [];
44
45   protected function setUp($import_test_views = TRUE) {
46     parent::setUp(FALSE);
47
48     $this->drupalCreateContentType(['type' => 'test_bundle']);
49     $this->drupalCreateContentType(['type' => 'test_bundle_2']);
50
51     ViewTestData::createTestViews(get_class($this), ['views_test_config']);
52
53     $this->entityBundles = $this->container->get('entity_type.bundle.info')->getBundleInfo('node');
54
55     $this->entities['count'] = 0;
56
57     foreach ($this->entityBundles as $key => $info) {
58       for ($i = 0; $i < 5; $i++) {
59         $entity = Node::create([
60           'title' => $this->randomString(),
61           'uid' => 1,
62           'type' => $key,
63         ]);
64         $entity->save();
65         $this->entities[$key][$entity->id()] = $entity;
66         $this->entities['count']++;
67       }
68     }
69   }
70
71   /**
72    * Tests the generic bundle filter.
73    */
74   public function testFilterEntity() {
75     $view = Views::getView('test_entity_type_filter');
76
77     // Tests \Drupal\views\Plugin\views\filter\Bundle::calculateDependencies().
78     $expected = [
79       'config' => [
80         'node.type.test_bundle',
81         'node.type.test_bundle_2',
82       ],
83       'module' => [
84         'node',
85       ],
86     ];
87     $this->assertIdentical($expected, $view->getDependencies());
88
89     $this->executeView($view);
90
91     // Test we have all the results, with all types selected.
92     $this->assertEqual(count($view->result), $this->entities['count']);
93
94     // Test the valueOptions of the filter handler.
95     $expected = [];
96
97     foreach ($this->entityBundles as $key => $info) {
98       $expected[$key] = $info['label'];
99     }
100     $this->assertIdentical($view->filter['type']->getValueOptions(), $expected);
101
102     $view->destroy();
103
104     // Test each bundle type.
105     foreach ($this->entityBundles as $key => $info) {
106       // Test each bundle type.
107       $view->initDisplay();
108       $filters = $view->display_handler->getOption('filters');
109       $filters['type']['value'] = [$key => $key];
110       $view->display_handler->setOption('filters', $filters);
111       $this->executeView($view);
112
113       $this->assertEqual(count($view->result), count($this->entities[$key]));
114
115       $view->destroy();
116     }
117
118     // Test an invalid bundle type to make sure we have no results.
119     $view->initDisplay();
120     $filters = $view->display_handler->getOption('filters');
121     $filters['type']['value'] = ['type_3' => 'type_3'];
122     $view->display_handler->setOption('filters', $filters);
123     $this->executeView($view);
124
125     $this->assertEqual(count($view->result), 0);
126   }
127
128 }