Version 1
[yaffs-website] / web / core / modules / views / tests / src / Functional / Handler / HandlerAllTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Functional\Handler;
4
5 use Drupal\comment\Tests\CommentTestTrait;
6 use Drupal\Tests\views\Functional\ViewTestBase;
7 use Drupal\views\ViewExecutable;
8 use Drupal\views\Plugin\views\HandlerBase;
9 use Drupal\views\Plugin\views\filter\InOperator;
10 use Drupal\views\Entity\View;
11
12 /**
13  * Tests instances of all handlers.
14  *
15  * @group views
16  */
17 class HandlerAllTest extends ViewTestBase {
18
19   use CommentTestTrait;
20
21   /**
22    * Modules to enable.
23    *
24    * @var array
25    */
26   public static $modules = [
27     'aggregator',
28     'book',
29     'block',
30     'comment',
31     'contact',
32     'field',
33     'filter',
34     'file',
35     'forum',
36     'history',
37     'language',
38     'locale',
39     'node',
40     'search',
41     'statistics',
42     'taxonomy',
43     'user',
44   ];
45
46   /**
47    * Tests most of the handlers.
48    */
49   public function testHandlers() {
50     $this->drupalCreateContentType(['type' => 'article']);
51     $this->addDefaultCommentField('node', 'article');
52
53     $object_types = array_keys(ViewExecutable::getHandlerTypes());
54     foreach ($this->container->get('views.views_data')->get() as $base_table => $info) {
55       if (!isset($info['table']['base'])) {
56         continue;
57       }
58
59       $view = View::create(['base_table' => $base_table]);
60       $view = $view->getExecutable();
61
62       // @todo The groupwise relationship is currently broken.
63       $exclude[] = 'taxonomy_term_field_data:tid_representative';
64       $exclude[] = 'users_field_data:uid_representative';
65
66       // Go through all fields and there through all handler types.
67       foreach ($info as $field => $field_info) {
68         // Table is a reserved key for the metainformation.
69         if ($field != 'table' && !in_array("$base_table:$field", $exclude)) {
70           $item = [
71             'table' => $base_table,
72             'field' => $field,
73           ];
74           foreach ($object_types as $type) {
75             if (isset($field_info[$type]['id'])) {
76               $options = [];
77               if ($type == 'filter') {
78                 $handler = $this->container->get("plugin.manager.views.$type")->getHandler($item);
79                 // Set the value to use for the filter based on the filter type.
80                 if ($handler instanceof InOperator) {
81                   $options['value'] = [1];
82                 }
83                 else {
84                   $options['value'] = 1;
85                 }
86               }
87               $view->addHandler('default', $type, $base_table, $field, $options);
88             }
89           }
90         }
91       }
92
93       // Go through each step individually to see whether some parts are
94       // failing.
95       $view->build();
96       $view->preExecute();
97       $view->execute();
98       $view->render();
99
100       // Make sure all handlers extend the HandlerBase.
101       foreach ($object_types as $type) {
102         if (isset($view->{$type})) {
103           foreach ($view->{$type} as $handler) {
104             $this->assertTrue($handler instanceof HandlerBase, format_string(
105               '@type handler of class %class is an instance of HandlerBase',
106               [
107                 '@type' => $type,
108                 '%class' => get_class($handler),
109               ]));
110           }
111         }
112       }
113     }
114   }
115
116 }