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@0
|
142 $this->installConfig(['node']);
|
Chris@0
|
143 $storage = $this->container->get('entity.manager')->getStorage('view');
|
Chris@0
|
144
|
Chris@0
|
145 // Test views_view_is_enabled/disabled.
|
Chris@0
|
146 $archive = $storage->load('archive');
|
Chris@0
|
147 $this->assertTrue(views_view_is_disabled($archive), 'views_view_is_disabled works as expected.');
|
Chris@0
|
148 // Enable the view and check this.
|
Chris@0
|
149 $archive->enable();
|
Chris@0
|
150 $this->assertTrue(views_view_is_enabled($archive), ' views_view_is_enabled works as expected.');
|
Chris@0
|
151
|
Chris@0
|
152 // We can store this now, as we have enabled/disabled above.
|
Chris@0
|
153 $all_views = $storage->loadMultiple();
|
Chris@0
|
154
|
Chris@0
|
155 // Test Views::getAllViews().
|
Chris@0
|
156 ksort($all_views);
|
Chris@0
|
157 $this->assertEquals(array_keys($all_views), array_keys(Views::getAllViews()), 'Views::getAllViews works as expected.');
|
Chris@0
|
158
|
Chris@0
|
159 // Test Views::getEnabledViews().
|
Chris@0
|
160 $expected_enabled = array_filter($all_views, function ($view) {
|
Chris@0
|
161 return views_view_is_enabled($view);
|
Chris@0
|
162 });
|
Chris@0
|
163 $this->assertEquals(array_keys($expected_enabled), array_keys(Views::getEnabledViews()), 'Expected enabled views returned.');
|
Chris@0
|
164
|
Chris@0
|
165 // Test Views::getDisabledViews().
|
Chris@0
|
166 $expected_disabled = array_filter($all_views, function ($view) {
|
Chris@0
|
167 return views_view_is_disabled($view);
|
Chris@0
|
168 });
|
Chris@0
|
169 $this->assertEquals(array_keys($expected_disabled), array_keys(Views::getDisabledViews()), 'Expected disabled views returned.');
|
Chris@0
|
170
|
Chris@0
|
171 // Test Views::getViewsAsOptions().
|
Chris@0
|
172 // Test the $views_only parameter.
|
Chris@0
|
173 $this->assertIdentical(array_keys($all_views), array_keys(Views::getViewsAsOptions(TRUE)), 'Expected option keys for all views were returned.');
|
Chris@0
|
174 $expected_options = [];
|
Chris@0
|
175 foreach ($all_views as $id => $view) {
|
Chris@0
|
176 $expected_options[$id] = $view->label();
|
Chris@0
|
177 }
|
Chris@0
|
178 $this->assertIdentical($expected_options, $this->castSafeStrings(Views::getViewsAsOptions(TRUE)), 'Expected options array was returned.');
|
Chris@0
|
179
|
Chris@0
|
180 // Test the default.
|
Chris@0
|
181 $this->assertIdentical($this->formatViewOptions($all_views), $this->castSafeStrings(Views::getViewsAsOptions()), 'Expected options array for all views was returned.');
|
Chris@0
|
182 // Test enabled views.
|
Chris@0
|
183 $this->assertIdentical($this->formatViewOptions($expected_enabled), $this->castSafeStrings(Views::getViewsAsOptions(FALSE, 'enabled')), 'Expected enabled options array was returned.');
|
Chris@0
|
184 // Test disabled views.
|
Chris@0
|
185 $this->assertIdentical($this->formatViewOptions($expected_disabled), $this->castSafeStrings(Views::getViewsAsOptions(FALSE, 'disabled')), 'Expected disabled options array was returned.');
|
Chris@0
|
186
|
Chris@0
|
187 // Test the sort parameter.
|
Chris@0
|
188 $all_views_sorted = $all_views;
|
Chris@0
|
189 ksort($all_views_sorted);
|
Chris@0
|
190 $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
|
191
|
Chris@0
|
192 // Test $exclude_view parameter.
|
Chris@0
|
193 $this->assertFalse(array_key_exists('archive', Views::getViewsAsOptions(TRUE, 'all', 'archive')), 'View excluded from options based on name');
|
Chris@0
|
194 $this->assertFalse(array_key_exists('archive:default', Views::getViewsAsOptions(FALSE, 'all', 'archive:default')), 'View display excluded from options based on name');
|
Chris@0
|
195 $this->assertFalse(array_key_exists('archive', Views::getViewsAsOptions(TRUE, 'all', $archive->getExecutable())), 'View excluded from options based on object');
|
Chris@0
|
196
|
Chris@0
|
197 // Test the $opt_group parameter.
|
Chris@0
|
198 $expected_opt_groups = [];
|
Chris@0
|
199 foreach ($all_views as $view) {
|
Chris@0
|
200 foreach ($view->get('display') as $display) {
|
Chris@0
|
201 $expected_opt_groups[$view->id()][$view->id() . ':' . $display['id']] = (string) t('@view : @display', ['@view' => $view->id(), '@display' => $display['id']]);
|
Chris@0
|
202 }
|
Chris@0
|
203 }
|
Chris@0
|
204 $this->assertIdentical($expected_opt_groups, $this->castSafeStrings(Views::getViewsAsOptions(FALSE, 'all', NULL, TRUE)), 'Expected option array for an option group returned.');
|
Chris@0
|
205 }
|
Chris@0
|
206
|
Chris@0
|
207 /**
|
Chris@0
|
208 * Tests view enable and disable procedural wrapper functions.
|
Chris@0
|
209 */
|
Chris@0
|
210 public function testStatusFunctions() {
|
Chris@0
|
211 $view = Views::getView('test_view_status')->storage;
|
Chris@0
|
212
|
Chris@0
|
213 $this->assertFalse($view->status(), 'The view status is disabled.');
|
Chris@0
|
214
|
Chris@0
|
215 views_enable_view($view);
|
Chris@0
|
216 $this->assertTrue($view->status(), 'A view has been enabled.');
|
Chris@0
|
217 $this->assertEqual($view->status(), views_view_is_enabled($view), 'views_view_is_enabled is correct.');
|
Chris@0
|
218
|
Chris@0
|
219 views_disable_view($view);
|
Chris@0
|
220 $this->assertFalse($view->status(), 'A view has been disabled.');
|
Chris@0
|
221 $this->assertEqual(!$view->status(), views_view_is_disabled($view), 'views_view_is_disabled is correct.');
|
Chris@0
|
222 }
|
Chris@0
|
223
|
Chris@0
|
224 /**
|
Chris@0
|
225 * Tests the \Drupal\views\Views::fetchPluginNames() method.
|
Chris@0
|
226 */
|
Chris@0
|
227 public function testViewsFetchPluginNames() {
|
Chris@0
|
228 // All style plugins should be returned, as we have not specified a type.
|
Chris@0
|
229 $plugins = Views::fetchPluginNames('style');
|
Chris@0
|
230 $definitions = $this->container->get('plugin.manager.views.style')->getDefinitions();
|
Chris@0
|
231 $expected = [];
|
Chris@0
|
232 foreach ($definitions as $id => $definition) {
|
Chris@0
|
233 $expected[$id] = $definition['title'];
|
Chris@0
|
234 }
|
Chris@0
|
235 asort($expected);
|
Chris@0
|
236 $this->assertIdentical(array_keys($plugins), array_keys($expected));
|
Chris@0
|
237
|
Chris@0
|
238 // Test using the 'test' style plugin type only returns the test_style and
|
Chris@0
|
239 // mapping_test plugins.
|
Chris@0
|
240 $plugins = Views::fetchPluginNames('style', 'test');
|
Chris@0
|
241 $this->assertIdentical(array_keys($plugins), ['mapping_test', 'test_style', 'test_template_style']);
|
Chris@0
|
242
|
Chris@0
|
243 // Test a non existent style plugin type returns no plugins.
|
Chris@0
|
244 $plugins = Views::fetchPluginNames('style', $this->randomString());
|
Chris@0
|
245 $this->assertIdentical($plugins, []);
|
Chris@0
|
246 }
|
Chris@0
|
247
|
Chris@0
|
248 /**
|
Chris@0
|
249 * Tests the \Drupal\views\Views::pluginList() method.
|
Chris@0
|
250 */
|
Chris@0
|
251 public function testViewsPluginList() {
|
Chris@0
|
252 $plugin_list = Views::pluginList();
|
Chris@0
|
253 // Only plugins used by 'test_view' should be in the plugin list.
|
Chris@0
|
254 foreach (['display:default', 'pager:none'] as $key) {
|
Chris@0
|
255 list($plugin_type, $plugin_id) = explode(':', $key);
|
Chris@0
|
256 $plugin_def = $this->container->get("plugin.manager.views.$plugin_type")->getDefinition($plugin_id);
|
Chris@0
|
257
|
Chris@17
|
258 $this->assertTrue(isset($plugin_list[$key]), new FormattableMarkup('The expected @key plugin list key was found.', ['@key' => $key]));
|
Chris@0
|
259 $plugin_details = $plugin_list[$key];
|
Chris@0
|
260
|
Chris@0
|
261 $this->assertEqual($plugin_details['type'], $plugin_type, 'The expected plugin type was found.');
|
Chris@0
|
262 $this->assertEqual($plugin_details['title'], $plugin_def['title'], 'The expected plugin title was found.');
|
Chris@0
|
263 $this->assertEqual($plugin_details['provider'], $plugin_def['provider'], 'The expected plugin provider was found.');
|
Chris@0
|
264 $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
|
265 }
|
Chris@0
|
266 }
|
Chris@0
|
267
|
Chris@0
|
268 /**
|
Chris@0
|
269 * Tests views.module: views_embed_view().
|
Chris@0
|
270 */
|
Chris@0
|
271 public function testViewsEmbedView() {
|
Chris@0
|
272 /** @var \Drupal\Core\Render\RendererInterface $renderer */
|
Chris@0
|
273 $renderer = \Drupal::service('renderer');
|
Chris@0
|
274
|
Chris@0
|
275 $result = views_embed_view('test_argument');
|
Chris@0
|
276 $renderer->renderPlain($result);
|
Chris@0
|
277 $this->assertEqual(count($result['view_build']['#view']->result), 5);
|
Chris@0
|
278
|
Chris@0
|
279 $result = views_embed_view('test_argument', 'default', 1);
|
Chris@0
|
280 $renderer->renderPlain($result);
|
Chris@0
|
281 $this->assertEqual(count($result['view_build']['#view']->result), 1);
|
Chris@0
|
282
|
Chris@0
|
283 $result = views_embed_view('test_argument', 'default', '1,2');
|
Chris@0
|
284 $renderer->renderPlain($result);
|
Chris@0
|
285 $this->assertEqual(count($result['view_build']['#view']->result), 2);
|
Chris@0
|
286
|
Chris@0
|
287 $result = views_embed_view('test_argument', 'default', '1,2', 'John');
|
Chris@0
|
288 $renderer->renderPlain($result);
|
Chris@0
|
289 $this->assertEqual(count($result['view_build']['#view']->result), 1);
|
Chris@0
|
290
|
Chris@0
|
291 $result = views_embed_view('test_argument', 'default', '1,2', 'John,George');
|
Chris@0
|
292 $renderer->renderPlain($result);
|
Chris@0
|
293 $this->assertEqual(count($result['view_build']['#view']->result), 2);
|
Chris@0
|
294 }
|
Chris@0
|
295
|
Chris@0
|
296 /**
|
Chris@0
|
297 * Tests the \Drupal\views\ViewsExecutable::preview() method.
|
Chris@0
|
298 */
|
Chris@0
|
299 public function testViewsPreview() {
|
Chris@0
|
300 $view = Views::getView('test_argument');
|
Chris@0
|
301 $result = $view->preview('default');
|
Chris@0
|
302 $this->assertEqual(count($result['#view']->result), 5);
|
Chris@0
|
303
|
Chris@0
|
304 $view = Views::getView('test_argument');
|
Chris@0
|
305 $result = $view->preview('default', ['0' => 1]);
|
Chris@0
|
306 $this->assertEqual(count($result['#view']->result), 1);
|
Chris@0
|
307
|
Chris@0
|
308 $view = Views::getView('test_argument');
|
Chris@0
|
309 $result = $view->preview('default', ['3' => 1]);
|
Chris@0
|
310 $this->assertEqual(count($result['#view']->result), 1);
|
Chris@0
|
311
|
Chris@0
|
312 $view = Views::getView('test_argument');
|
Chris@0
|
313 $result = $view->preview('default', ['0' => '1,2']);
|
Chris@0
|
314 $this->assertEqual(count($result['#view']->result), 2);
|
Chris@0
|
315
|
Chris@0
|
316 $view = Views::getView('test_argument');
|
Chris@0
|
317 $result = $view->preview('default', ['3' => '1,2']);
|
Chris@0
|
318 $this->assertEqual(count($result['#view']->result), 2);
|
Chris@0
|
319
|
Chris@0
|
320 $view = Views::getView('test_argument');
|
Chris@0
|
321 $result = $view->preview('default', ['0' => '1,2', '1' => 'John']);
|
Chris@0
|
322 $this->assertEqual(count($result['#view']->result), 1);
|
Chris@0
|
323
|
Chris@0
|
324 $view = Views::getView('test_argument');
|
Chris@0
|
325 $result = $view->preview('default', ['3' => '1,2', '4' => 'John']);
|
Chris@0
|
326 $this->assertEqual(count($result['#view']->result), 1);
|
Chris@0
|
327
|
Chris@0
|
328 $view = Views::getView('test_argument');
|
Chris@0
|
329 $result = $view->preview('default', ['0' => '1,2', '1' => 'John,George']);
|
Chris@0
|
330 $this->assertEqual(count($result['#view']->result), 2);
|
Chris@0
|
331
|
Chris@0
|
332 $view = Views::getView('test_argument');
|
Chris@0
|
333 $result = $view->preview('default', ['3' => '1,2', '4' => 'John,George']);
|
Chris@0
|
334 $this->assertEqual(count($result['#view']->result), 2);
|
Chris@0
|
335 }
|
Chris@0
|
336
|
Chris@0
|
337 /**
|
Chris@0
|
338 * Helper to return an expected views option array.
|
Chris@0
|
339 *
|
Chris@0
|
340 * @param array $views
|
Chris@0
|
341 * An array of Drupal\views\Entity\View objects for which to
|
Chris@0
|
342 * create an options array.
|
Chris@0
|
343 *
|
Chris@0
|
344 * @return array
|
Chris@0
|
345 * A formatted options array that matches the expected output.
|
Chris@0
|
346 */
|
Chris@0
|
347 protected function formatViewOptions(array $views = []) {
|
Chris@0
|
348 $expected_options = [];
|
Chris@0
|
349 foreach ($views as $view) {
|
Chris@0
|
350 foreach ($view->get('display') as $display) {
|
Chris@0
|
351 $expected_options[$view->id() . ':' . $display['id']] = (string) t('View: @view - Display: @display',
|
Chris@0
|
352 ['@view' => $view->id(), '@display' => $display['id']]);
|
Chris@0
|
353 }
|
Chris@0
|
354 }
|
Chris@0
|
355
|
Chris@0
|
356 return $expected_options;
|
Chris@0
|
357 }
|
Chris@0
|
358
|
Chris@0
|
359 /**
|
Chris@0
|
360 * Ensure that a certain handler is a instance of a certain table/field.
|
Chris@0
|
361 */
|
Chris@0
|
362 public function assertInstanceHandler($handler, $table, $field, $id) {
|
Chris@0
|
363 $table_data = $this->container->get('views.views_data')->get($table);
|
Chris@0
|
364 $field_data = $table_data[$field][$id];
|
Chris@0
|
365
|
Chris@0
|
366 $this->assertEqual($field_data['id'], $handler->getPluginId());
|
Chris@0
|
367 }
|
Chris@0
|
368
|
Chris@0
|
369 }
|