Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\views\Kernel;
|
Chris@0
|
4
|
Chris@0
|
5 /**
|
Chris@0
|
6 * Tests basic functions from the Views module.
|
Chris@0
|
7 *
|
Chris@0
|
8 * @group views
|
Chris@0
|
9 */
|
Chris@0
|
10 use Drupal\views\Plugin\views\filter\Standard;
|
Chris@0
|
11 use Drupal\views\Views;
|
Chris@0
|
12 use Drupal\Component\Render\FormattableMarkup;
|
Chris@0
|
13
|
Chris@0
|
14 class ModuleTest extends ViewsKernelTestBase {
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * Views used by this test.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @var array
|
Chris@0
|
20 */
|
Chris@0
|
21 public static $testViews = ['test_view_status', 'test_view', 'test_argument'];
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * Modules to enable.
|
Chris@0
|
25 *
|
Chris@0
|
26 * @var array
|
Chris@0
|
27 */
|
Chris@0
|
28 public static $modules = ['field', 'user', 'block'];
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@17
|
31 * Stores the last triggered error.
|
Chris@0
|
32 *
|
Chris@0
|
33 * @var string
|
Chris@0
|
34 *
|
Chris@0
|
35 * @see \Drupal\views\Tests\ModuleTest::errorHandler()
|
Chris@0
|
36 */
|
Chris@0
|
37 protected $lastErrorMessage;
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@17
|
40 * Tests the ViewsHandlerManager::getHandler() method.
|
Chris@0
|
41 *
|
Chris@17
|
42 * @see \Drupal\views\Plugin\ViewsHandlerManager::getHandler()
|
Chris@0
|
43 */
|
Chris@0
|
44 public function testViewsGetHandler() {
|
Chris@0
|
45 $types = ['field', 'area', 'filter'];
|
Chris@0
|
46 foreach ($types as $type) {
|
Chris@0
|
47 $item = [
|
Chris@0
|
48 'table' => $this->randomMachineName(),
|
Chris@0
|
49 'field' => $this->randomMachineName(),
|
Chris@0
|
50 ];
|
Chris@0
|
51 $handler = $this->container->get('plugin.manager.views.' . $type)->getHandler($item);
|
Chris@0
|
52 $this->assertEqual('Drupal\views\Plugin\views\\' . $type . '\Broken', get_class($handler), new FormattableMarkup('Make sure that a broken handler of type: @type is created.', ['@type' => $type]));
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@0
|
55 $views_data = $this->viewsData();
|
Chris@0
|
56 $test_tables = ['views_test_data' => ['id', 'name']];
|
Chris@0
|
57 foreach ($test_tables as $table => $fields) {
|
Chris@0
|
58 foreach ($fields as $field) {
|
Chris@0
|
59 $data = $views_data[$table][$field];
|
Chris@0
|
60 $item = [
|
Chris@0
|
61 'table' => $table,
|
Chris@0
|
62 'field' => $field,
|
Chris@0
|
63 ];
|
Chris@0
|
64 foreach ($data as $id => $field_data) {
|
Chris@0
|
65 if (!in_array($id, ['title', 'help'])) {
|
Chris@0
|
66 $handler = $this->container->get('plugin.manager.views.' . $id)->getHandler($item);
|
Chris@0
|
67 $this->assertInstanceHandler($handler, $table, $field, $id);
|
Chris@0
|
68 }
|
Chris@0
|
69 }
|
Chris@0
|
70 }
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 // Test the override handler feature.
|
Chris@0
|
74 $item = [
|
Chris@0
|
75 'table' => 'views_test_data',
|
Chris@0
|
76 'field' => 'job',
|
Chris@0
|
77 ];
|
Chris@0
|
78 $handler = $this->container->get('plugin.manager.views.filter')->getHandler($item, 'standard');
|
Chris@0
|
79 $this->assertTrue($handler instanceof Standard);
|
Chris@0
|
80
|
Chris@0
|
81 // @todo Reinstate these tests when the debug() in views_get_handler() is
|
Chris@0
|
82 // restored.
|
Chris@0
|
83 return;
|
Chris@0
|
84
|
Chris@0
|
85 // Test non-existent tables/fields.
|
Chris@0
|
86 set_error_handler([$this, 'customErrorHandler']);
|
Chris@0
|
87 $item = [
|
Chris@0
|
88 'table' => 'views_test_data',
|
Chris@0
|
89 'field' => 'field_invalid',
|
Chris@0
|
90 ];
|
Chris@0
|
91 $this->container->get('plugin.manager.views.field')->getHandler($item);
|
Chris@0
|
92 $this->assertTrue(strpos($this->lastErrorMessage, format_string("Missing handler: @table @field @type", ['@table' => 'views_test_data', '@field' => 'field_invalid', '@type' => 'field'])) !== FALSE, 'An invalid field name throws a debug message.');
|
Chris@0
|
93 unset($this->lastErrorMessage);
|
Chris@0
|
94
|
Chris@0
|
95 $item = [
|
Chris@0
|
96 'table' => 'table_invalid',
|
Chris@0
|
97 'field' => 'id',
|
Chris@0
|
98 ];
|
Chris@0
|
99 $this->container->get('plugin.manager.views.filter')->getHandler($item);
|
Chris@0
|
100 $this->assertEqual(strpos($this->lastErrorMessage, format_string("Missing handler: @table @field @type", ['@table' => 'table_invalid', '@field' => 'id', '@type' => 'filter'])) !== FALSE, 'An invalid table name throws a debug message.');
|
Chris@0
|
101 unset($this->lastErrorMessage);
|
Chris@0
|
102
|
Chris@0
|
103 $item = [
|
Chris@0
|
104 'table' => 'table_invalid',
|
Chris@0
|
105 'field' => 'id',
|
Chris@0
|
106 ];
|
Chris@0
|
107 $this->container->get('plugin.manager.views.filter')->getHandler($item);
|
Chris@0
|
108 $this->assertEqual(strpos($this->lastErrorMessage, format_string("Missing handler: @table @field @type", ['@table' => 'table_invalid', '@field' => 'id', '@type' => 'filter'])) !== FALSE, 'An invalid table name throws a debug message.');
|
Chris@0
|
109 unset($this->lastErrorMessage);
|
Chris@0
|
110
|
Chris@0
|
111 restore_error_handler();
|
Chris@0
|
112 }
|
Chris@0
|
113
|
Chris@0
|
114 /**
|
Chris@0
|
115 * Defines an error handler which is used in the test.
|
Chris@0
|
116 *
|
Chris@0
|
117 * Because this is registered in set_error_handler(), it has to be public.
|
Chris@0
|
118 *
|
Chris@0
|
119 * @param int $error_level
|
Chris@0
|
120 * The level of the error raised.
|
Chris@0
|
121 * @param string $message
|
Chris@0
|
122 * The error message.
|
Chris@0
|
123 * @param string $filename
|
Chris@0
|
124 * The filename that the error was raised in.
|
Chris@0
|
125 * @param int $line
|
Chris@0
|
126 * The line number the error was raised at.
|
Chris@0
|
127 * @param array $context
|
Chris@0
|
128 * An array that points to the active symbol table at the point the error
|
Chris@0
|
129 * occurred.
|
Chris@0
|
130 *
|
Chris@0
|
131 * @see set_error_handler()
|
Chris@0
|
132 */
|
Chris@0
|
133 public function customErrorHandler($error_level, $message, $filename, $line, $context) {
|
Chris@0
|
134 $this->lastErrorMessage = $message;
|
Chris@0
|
135 }
|
Chris@0
|
136
|
Chris@0
|
137 /**
|
Chris@0
|
138 * Tests the load wrapper/helper functions.
|
Chris@0
|
139 */
|
Chris@0
|
140 public function testLoadFunctions() {
|
Chris@0
|
141 $this->enableModules(['text', 'node']);
|
Chris@18
|
142 $this->installEntitySchema('node');
|
Chris@0
|
143 $this->installConfig(['node']);
|
Chris@0
|
144 $storage = $this->container->get('entity.manager')->getStorage('view');
|
Chris@0
|
145
|
Chris@0
|
146 // Test views_view_is_enabled/disabled.
|
Chris@0
|
147 $archive = $storage->load('archive');
|
Chris@0
|
148 $this->assertTrue(views_view_is_disabled($archive), 'views_view_is_disabled works as expected.');
|
Chris@0
|
149 // Enable the view and check this.
|
Chris@0
|
150 $archive->enable();
|
Chris@0
|
151 $this->assertTrue(views_view_is_enabled($archive), ' views_view_is_enabled works as expected.');
|
Chris@0
|
152
|
Chris@0
|
153 // We can store this now, as we have enabled/disabled above.
|
Chris@0
|
154 $all_views = $storage->loadMultiple();
|
Chris@0
|
155
|
Chris@0
|
156 // Test Views::getAllViews().
|
Chris@0
|
157 ksort($all_views);
|
Chris@0
|
158 $this->assertEquals(array_keys($all_views), array_keys(Views::getAllViews()), 'Views::getAllViews works as expected.');
|
Chris@0
|
159
|
Chris@0
|
160 // Test Views::getEnabledViews().
|
Chris@0
|
161 $expected_enabled = array_filter($all_views, function ($view) {
|
Chris@0
|
162 return views_view_is_enabled($view);
|
Chris@0
|
163 });
|
Chris@0
|
164 $this->assertEquals(array_keys($expected_enabled), array_keys(Views::getEnabledViews()), 'Expected enabled views returned.');
|
Chris@0
|
165
|
Chris@0
|
166 // Test Views::getDisabledViews().
|
Chris@0
|
167 $expected_disabled = array_filter($all_views, function ($view) {
|
Chris@0
|
168 return views_view_is_disabled($view);
|
Chris@0
|
169 });
|
Chris@0
|
170 $this->assertEquals(array_keys($expected_disabled), array_keys(Views::getDisabledViews()), 'Expected disabled views returned.');
|
Chris@0
|
171
|
Chris@0
|
172 // Test Views::getViewsAsOptions().
|
Chris@0
|
173 // Test the $views_only parameter.
|
Chris@0
|
174 $this->assertIdentical(array_keys($all_views), array_keys(Views::getViewsAsOptions(TRUE)), 'Expected option keys for all views were returned.');
|
Chris@0
|
175 $expected_options = [];
|
Chris@0
|
176 foreach ($all_views as $id => $view) {
|
Chris@0
|
177 $expected_options[$id] = $view->label();
|
Chris@0
|
178 }
|
Chris@0
|
179 $this->assertIdentical($expected_options, $this->castSafeStrings(Views::getViewsAsOptions(TRUE)), 'Expected options array was returned.');
|
Chris@0
|
180
|
Chris@0
|
181 // Test the default.
|
Chris@0
|
182 $this->assertIdentical($this->formatViewOptions($all_views), $this->castSafeStrings(Views::getViewsAsOptions()), 'Expected options array for all views was returned.');
|
Chris@0
|
183 // Test enabled views.
|
Chris@0
|
184 $this->assertIdentical($this->formatViewOptions($expected_enabled), $this->castSafeStrings(Views::getViewsAsOptions(FALSE, 'enabled')), 'Expected enabled options array was returned.');
|
Chris@0
|
185 // Test disabled views.
|
Chris@0
|
186 $this->assertIdentical($this->formatViewOptions($expected_disabled), $this->castSafeStrings(Views::getViewsAsOptions(FALSE, 'disabled')), 'Expected disabled options array was returned.');
|
Chris@0
|
187
|
Chris@0
|
188 // Test the sort parameter.
|
Chris@0
|
189 $all_views_sorted = $all_views;
|
Chris@0
|
190 ksort($all_views_sorted);
|
Chris@0
|
191 $this->assertIdentical(array_keys($all_views_sorted), array_keys(Views::getViewsAsOptions(TRUE, 'all', NULL, FALSE, TRUE)), 'All view id keys returned in expected sort order');
|
Chris@0
|
192
|
Chris@0
|
193 // Test $exclude_view parameter.
|
Chris@0
|
194 $this->assertFalse(array_key_exists('archive', Views::getViewsAsOptions(TRUE, 'all', 'archive')), 'View excluded from options based on name');
|
Chris@0
|
195 $this->assertFalse(array_key_exists('archive:default', Views::getViewsAsOptions(FALSE, 'all', 'archive:default')), 'View display excluded from options based on name');
|
Chris@0
|
196 $this->assertFalse(array_key_exists('archive', Views::getViewsAsOptions(TRUE, 'all', $archive->getExecutable())), 'View excluded from options based on object');
|
Chris@0
|
197
|
Chris@0
|
198 // Test the $opt_group parameter.
|
Chris@0
|
199 $expected_opt_groups = [];
|
Chris@0
|
200 foreach ($all_views as $view) {
|
Chris@0
|
201 foreach ($view->get('display') as $display) {
|
Chris@0
|
202 $expected_opt_groups[$view->id()][$view->id() . ':' . $display['id']] = (string) t('@view : @display', ['@view' => $view->id(), '@display' => $display['id']]);
|
Chris@0
|
203 }
|
Chris@0
|
204 }
|
Chris@0
|
205 $this->assertIdentical($expected_opt_groups, $this->castSafeStrings(Views::getViewsAsOptions(FALSE, 'all', NULL, TRUE)), 'Expected option array for an option group returned.');
|
Chris@0
|
206 }
|
Chris@0
|
207
|
Chris@0
|
208 /**
|
Chris@0
|
209 * Tests view enable and disable procedural wrapper functions.
|
Chris@0
|
210 */
|
Chris@0
|
211 public function testStatusFunctions() {
|
Chris@0
|
212 $view = Views::getView('test_view_status')->storage;
|
Chris@0
|
213
|
Chris@0
|
214 $this->assertFalse($view->status(), 'The view status is disabled.');
|
Chris@0
|
215
|
Chris@0
|
216 views_enable_view($view);
|
Chris@0
|
217 $this->assertTrue($view->status(), 'A view has been enabled.');
|
Chris@0
|
218 $this->assertEqual($view->status(), views_view_is_enabled($view), 'views_view_is_enabled is correct.');
|
Chris@0
|
219
|
Chris@0
|
220 views_disable_view($view);
|
Chris@0
|
221 $this->assertFalse($view->status(), 'A view has been disabled.');
|
Chris@0
|
222 $this->assertEqual(!$view->status(), views_view_is_disabled($view), 'views_view_is_disabled is correct.');
|
Chris@0
|
223 }
|
Chris@0
|
224
|
Chris@0
|
225 /**
|
Chris@0
|
226 * Tests the \Drupal\views\Views::fetchPluginNames() method.
|
Chris@0
|
227 */
|
Chris@0
|
228 public function testViewsFetchPluginNames() {
|
Chris@0
|
229 // All style plugins should be returned, as we have not specified a type.
|
Chris@0
|
230 $plugins = Views::fetchPluginNames('style');
|
Chris@0
|
231 $definitions = $this->container->get('plugin.manager.views.style')->getDefinitions();
|
Chris@0
|
232 $expected = [];
|
Chris@0
|
233 foreach ($definitions as $id => $definition) {
|
Chris@0
|
234 $expected[$id] = $definition['title'];
|
Chris@0
|
235 }
|
Chris@0
|
236 asort($expected);
|
Chris@0
|
237 $this->assertIdentical(array_keys($plugins), array_keys($expected));
|
Chris@0
|
238
|
Chris@0
|
239 // Test using the 'test' style plugin type only returns the test_style and
|
Chris@0
|
240 // mapping_test plugins.
|
Chris@0
|
241 $plugins = Views::fetchPluginNames('style', 'test');
|
Chris@0
|
242 $this->assertIdentical(array_keys($plugins), ['mapping_test', 'test_style', 'test_template_style']);
|
Chris@0
|
243
|
Chris@0
|
244 // Test a non existent style plugin type returns no plugins.
|
Chris@0
|
245 $plugins = Views::fetchPluginNames('style', $this->randomString());
|
Chris@0
|
246 $this->assertIdentical($plugins, []);
|
Chris@0
|
247 }
|
Chris@0
|
248
|
Chris@0
|
249 /**
|
Chris@0
|
250 * Tests the \Drupal\views\Views::pluginList() method.
|
Chris@0
|
251 */
|
Chris@0
|
252 public function testViewsPluginList() {
|
Chris@0
|
253 $plugin_list = Views::pluginList();
|
Chris@0
|
254 // Only plugins used by 'test_view' should be in the plugin list.
|
Chris@0
|
255 foreach (['display:default', 'pager:none'] as $key) {
|
Chris@0
|
256 list($plugin_type, $plugin_id) = explode(':', $key);
|
Chris@0
|
257 $plugin_def = $this->container->get("plugin.manager.views.$plugin_type")->getDefinition($plugin_id);
|
Chris@0
|
258
|
Chris@17
|
259 $this->assertTrue(isset($plugin_list[$key]), new FormattableMarkup('The expected @key plugin list key was found.', ['@key' => $key]));
|
Chris@0
|
260 $plugin_details = $plugin_list[$key];
|
Chris@0
|
261
|
Chris@0
|
262 $this->assertEqual($plugin_details['type'], $plugin_type, 'The expected plugin type was found.');
|
Chris@0
|
263 $this->assertEqual($plugin_details['title'], $plugin_def['title'], 'The expected plugin title was found.');
|
Chris@0
|
264 $this->assertEqual($plugin_details['provider'], $plugin_def['provider'], 'The expected plugin provider was found.');
|
Chris@0
|
265 $this->assertTrue(in_array('test_view', $plugin_details['views']), 'The test_view View was found in the list of views using this plugin.');
|
Chris@0
|
266 }
|
Chris@0
|
267 }
|
Chris@0
|
268
|
Chris@0
|
269 /**
|
Chris@0
|
270 * Tests views.module: views_embed_view().
|
Chris@0
|
271 */
|
Chris@0
|
272 public function testViewsEmbedView() {
|
Chris@0
|
273 /** @var \Drupal\Core\Render\RendererInterface $renderer */
|
Chris@0
|
274 $renderer = \Drupal::service('renderer');
|
Chris@0
|
275
|
Chris@0
|
276 $result = views_embed_view('test_argument');
|
Chris@0
|
277 $renderer->renderPlain($result);
|
Chris@0
|
278 $this->assertEqual(count($result['view_build']['#view']->result), 5);
|
Chris@0
|
279
|
Chris@0
|
280 $result = views_embed_view('test_argument', 'default', 1);
|
Chris@0
|
281 $renderer->renderPlain($result);
|
Chris@0
|
282 $this->assertEqual(count($result['view_build']['#view']->result), 1);
|
Chris@0
|
283
|
Chris@0
|
284 $result = views_embed_view('test_argument', 'default', '1,2');
|
Chris@0
|
285 $renderer->renderPlain($result);
|
Chris@0
|
286 $this->assertEqual(count($result['view_build']['#view']->result), 2);
|
Chris@0
|
287
|
Chris@0
|
288 $result = views_embed_view('test_argument', 'default', '1,2', 'John');
|
Chris@0
|
289 $renderer->renderPlain($result);
|
Chris@0
|
290 $this->assertEqual(count($result['view_build']['#view']->result), 1);
|
Chris@0
|
291
|
Chris@0
|
292 $result = views_embed_view('test_argument', 'default', '1,2', 'John,George');
|
Chris@0
|
293 $renderer->renderPlain($result);
|
Chris@0
|
294 $this->assertEqual(count($result['view_build']['#view']->result), 2);
|
Chris@0
|
295 }
|
Chris@0
|
296
|
Chris@0
|
297 /**
|
Chris@0
|
298 * Tests the \Drupal\views\ViewsExecutable::preview() method.
|
Chris@0
|
299 */
|
Chris@0
|
300 public function testViewsPreview() {
|
Chris@0
|
301 $view = Views::getView('test_argument');
|
Chris@0
|
302 $result = $view->preview('default');
|
Chris@0
|
303 $this->assertEqual(count($result['#view']->result), 5);
|
Chris@0
|
304
|
Chris@0
|
305 $view = Views::getView('test_argument');
|
Chris@0
|
306 $result = $view->preview('default', ['0' => 1]);
|
Chris@0
|
307 $this->assertEqual(count($result['#view']->result), 1);
|
Chris@0
|
308
|
Chris@0
|
309 $view = Views::getView('test_argument');
|
Chris@0
|
310 $result = $view->preview('default', ['3' => 1]);
|
Chris@0
|
311 $this->assertEqual(count($result['#view']->result), 1);
|
Chris@0
|
312
|
Chris@0
|
313 $view = Views::getView('test_argument');
|
Chris@0
|
314 $result = $view->preview('default', ['0' => '1,2']);
|
Chris@0
|
315 $this->assertEqual(count($result['#view']->result), 2);
|
Chris@0
|
316
|
Chris@0
|
317 $view = Views::getView('test_argument');
|
Chris@0
|
318 $result = $view->preview('default', ['3' => '1,2']);
|
Chris@0
|
319 $this->assertEqual(count($result['#view']->result), 2);
|
Chris@0
|
320
|
Chris@0
|
321 $view = Views::getView('test_argument');
|
Chris@0
|
322 $result = $view->preview('default', ['0' => '1,2', '1' => 'John']);
|
Chris@0
|
323 $this->assertEqual(count($result['#view']->result), 1);
|
Chris@0
|
324
|
Chris@0
|
325 $view = Views::getView('test_argument');
|
Chris@0
|
326 $result = $view->preview('default', ['3' => '1,2', '4' => 'John']);
|
Chris@0
|
327 $this->assertEqual(count($result['#view']->result), 1);
|
Chris@0
|
328
|
Chris@0
|
329 $view = Views::getView('test_argument');
|
Chris@0
|
330 $result = $view->preview('default', ['0' => '1,2', '1' => 'John,George']);
|
Chris@0
|
331 $this->assertEqual(count($result['#view']->result), 2);
|
Chris@0
|
332
|
Chris@0
|
333 $view = Views::getView('test_argument');
|
Chris@0
|
334 $result = $view->preview('default', ['3' => '1,2', '4' => 'John,George']);
|
Chris@0
|
335 $this->assertEqual(count($result['#view']->result), 2);
|
Chris@0
|
336 }
|
Chris@0
|
337
|
Chris@0
|
338 /**
|
Chris@0
|
339 * Helper to return an expected views option array.
|
Chris@0
|
340 *
|
Chris@0
|
341 * @param array $views
|
Chris@0
|
342 * An array of Drupal\views\Entity\View objects for which to
|
Chris@0
|
343 * create an options array.
|
Chris@0
|
344 *
|
Chris@0
|
345 * @return array
|
Chris@0
|
346 * A formatted options array that matches the expected output.
|
Chris@0
|
347 */
|
Chris@0
|
348 protected function formatViewOptions(array $views = []) {
|
Chris@0
|
349 $expected_options = [];
|
Chris@0
|
350 foreach ($views as $view) {
|
Chris@0
|
351 foreach ($view->get('display') as $display) {
|
Chris@0
|
352 $expected_options[$view->id() . ':' . $display['id']] = (string) t('View: @view - Display: @display',
|
Chris@0
|
353 ['@view' => $view->id(), '@display' => $display['id']]);
|
Chris@0
|
354 }
|
Chris@0
|
355 }
|
Chris@0
|
356
|
Chris@0
|
357 return $expected_options;
|
Chris@0
|
358 }
|
Chris@0
|
359
|
Chris@0
|
360 /**
|
Chris@0
|
361 * Ensure that a certain handler is a instance of a certain table/field.
|
Chris@0
|
362 */
|
Chris@0
|
363 public function assertInstanceHandler($handler, $table, $field, $id) {
|
Chris@0
|
364 $table_data = $this->container->get('views.views_data')->get($table);
|
Chris@0
|
365 $field_data = $table_data[$field][$id];
|
Chris@0
|
366
|
Chris@0
|
367 $this->assertEqual($field_data['id'], $handler->getPluginId());
|
Chris@0
|
368 }
|
Chris@0
|
369
|
Chris@0
|
370 }
|