3 namespace Drupal\Tests\views\Kernel;
6 * Tests basic functions from the Views module.
10 use Drupal\views\Plugin\views\filter\Standard;
11 use Drupal\views\Views;
12 use Drupal\Component\Utility\SafeMarkup;
13 use Drupal\Component\Render\FormattableMarkup;
15 class ModuleTest extends ViewsKernelTestBase {
18 * Views used by this test.
22 public static $testViews = ['test_view_status', 'test_view', 'test_argument'];
29 public static $modules = ['field', 'user', 'block'];
32 * Stores the last triggered error, for example via debug().
36 * @see \Drupal\views\Tests\ModuleTest::errorHandler()
38 protected $lastErrorMessage;
41 * Tests the views_get_handler method.
43 * @see views_get_handler()
45 public function testViewsGetHandler() {
46 $types = ['field', 'area', 'filter'];
47 foreach ($types as $type) {
49 'table' => $this->randomMachineName(),
50 'field' => $this->randomMachineName(),
52 $handler = $this->container->get('plugin.manager.views.' . $type)->getHandler($item);
53 $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]));
56 $views_data = $this->viewsData();
57 $test_tables = ['views_test_data' => ['id', 'name']];
58 foreach ($test_tables as $table => $fields) {
59 foreach ($fields as $field) {
60 $data = $views_data[$table][$field];
65 foreach ($data as $id => $field_data) {
66 if (!in_array($id, ['title', 'help'])) {
67 $handler = $this->container->get('plugin.manager.views.' . $id)->getHandler($item);
68 $this->assertInstanceHandler($handler, $table, $field, $id);
74 // Test the override handler feature.
76 'table' => 'views_test_data',
79 $handler = $this->container->get('plugin.manager.views.filter')->getHandler($item, 'standard');
80 $this->assertTrue($handler instanceof Standard);
82 // @todo Reinstate these tests when the debug() in views_get_handler() is
86 // Test non-existent tables/fields.
87 set_error_handler([$this, 'customErrorHandler']);
89 'table' => 'views_test_data',
90 'field' => 'field_invalid',
92 $this->container->get('plugin.manager.views.field')->getHandler($item);
93 $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.');
94 unset($this->lastErrorMessage);
97 'table' => 'table_invalid',
100 $this->container->get('plugin.manager.views.filter')->getHandler($item);
101 $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.');
102 unset($this->lastErrorMessage);
105 'table' => 'table_invalid',
108 $this->container->get('plugin.manager.views.filter')->getHandler($item);
109 $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.');
110 unset($this->lastErrorMessage);
112 restore_error_handler();
116 * Defines an error handler which is used in the test.
118 * Because this is registered in set_error_handler(), it has to be public.
120 * @param int $error_level
121 * The level of the error raised.
122 * @param string $message
124 * @param string $filename
125 * The filename that the error was raised in.
127 * The line number the error was raised at.
128 * @param array $context
129 * An array that points to the active symbol table at the point the error
132 * @see set_error_handler()
134 public function customErrorHandler($error_level, $message, $filename, $line, $context) {
135 $this->lastErrorMessage = $message;
139 * Tests the load wrapper/helper functions.
141 public function testLoadFunctions() {
142 $this->enableModules(['text', 'node']);
143 $this->installConfig(['node']);
144 $storage = $this->container->get('entity.manager')->getStorage('view');
146 // Test views_view_is_enabled/disabled.
147 $archive = $storage->load('archive');
148 $this->assertTrue(views_view_is_disabled($archive), 'views_view_is_disabled works as expected.');
149 // Enable the view and check this.
151 $this->assertTrue(views_view_is_enabled($archive), ' views_view_is_enabled works as expected.');
153 // We can store this now, as we have enabled/disabled above.
154 $all_views = $storage->loadMultiple();
156 // Test Views::getAllViews().
158 $this->assertEquals(array_keys($all_views), array_keys(Views::getAllViews()), 'Views::getAllViews works as expected.');
160 // Test Views::getEnabledViews().
161 $expected_enabled = array_filter($all_views, function ($view) {
162 return views_view_is_enabled($view);
164 $this->assertEquals(array_keys($expected_enabled), array_keys(Views::getEnabledViews()), 'Expected enabled views returned.');
166 // Test Views::getDisabledViews().
167 $expected_disabled = array_filter($all_views, function ($view) {
168 return views_view_is_disabled($view);
170 $this->assertEquals(array_keys($expected_disabled), array_keys(Views::getDisabledViews()), 'Expected disabled views returned.');
172 // Test Views::getViewsAsOptions().
173 // Test the $views_only parameter.
174 $this->assertIdentical(array_keys($all_views), array_keys(Views::getViewsAsOptions(TRUE)), 'Expected option keys for all views were returned.');
175 $expected_options = [];
176 foreach ($all_views as $id => $view) {
177 $expected_options[$id] = $view->label();
179 $this->assertIdentical($expected_options, $this->castSafeStrings(Views::getViewsAsOptions(TRUE)), 'Expected options array was returned.');
182 $this->assertIdentical($this->formatViewOptions($all_views), $this->castSafeStrings(Views::getViewsAsOptions()), 'Expected options array for all views was returned.');
183 // Test enabled views.
184 $this->assertIdentical($this->formatViewOptions($expected_enabled), $this->castSafeStrings(Views::getViewsAsOptions(FALSE, 'enabled')), 'Expected enabled options array was returned.');
185 // Test disabled views.
186 $this->assertIdentical($this->formatViewOptions($expected_disabled), $this->castSafeStrings(Views::getViewsAsOptions(FALSE, 'disabled')), 'Expected disabled options array was returned.');
188 // Test the sort parameter.
189 $all_views_sorted = $all_views;
190 ksort($all_views_sorted);
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');
193 // Test $exclude_view parameter.
194 $this->assertFalse(array_key_exists('archive', Views::getViewsAsOptions(TRUE, 'all', 'archive')), 'View excluded from options based on name');
195 $this->assertFalse(array_key_exists('archive:default', Views::getViewsAsOptions(FALSE, 'all', 'archive:default')), 'View display excluded from options based on name');
196 $this->assertFalse(array_key_exists('archive', Views::getViewsAsOptions(TRUE, 'all', $archive->getExecutable())), 'View excluded from options based on object');
198 // Test the $opt_group parameter.
199 $expected_opt_groups = [];
200 foreach ($all_views as $view) {
201 foreach ($view->get('display') as $display) {
202 $expected_opt_groups[$view->id()][$view->id() . ':' . $display['id']] = (string) t('@view : @display', ['@view' => $view->id(), '@display' => $display['id']]);
205 $this->assertIdentical($expected_opt_groups, $this->castSafeStrings(Views::getViewsAsOptions(FALSE, 'all', NULL, TRUE)), 'Expected option array for an option group returned.');
209 * Tests view enable and disable procedural wrapper functions.
211 public function testStatusFunctions() {
212 $view = Views::getView('test_view_status')->storage;
214 $this->assertFalse($view->status(), 'The view status is disabled.');
216 views_enable_view($view);
217 $this->assertTrue($view->status(), 'A view has been enabled.');
218 $this->assertEqual($view->status(), views_view_is_enabled($view), 'views_view_is_enabled is correct.');
220 views_disable_view($view);
221 $this->assertFalse($view->status(), 'A view has been disabled.');
222 $this->assertEqual(!$view->status(), views_view_is_disabled($view), 'views_view_is_disabled is correct.');
226 * Tests the \Drupal\views\Views::fetchPluginNames() method.
228 public function testViewsFetchPluginNames() {
229 // All style plugins should be returned, as we have not specified a type.
230 $plugins = Views::fetchPluginNames('style');
231 $definitions = $this->container->get('plugin.manager.views.style')->getDefinitions();
233 foreach ($definitions as $id => $definition) {
234 $expected[$id] = $definition['title'];
237 $this->assertIdentical(array_keys($plugins), array_keys($expected));
239 // Test using the 'test' style plugin type only returns the test_style and
240 // mapping_test plugins.
241 $plugins = Views::fetchPluginNames('style', 'test');
242 $this->assertIdentical(array_keys($plugins), ['mapping_test', 'test_style', 'test_template_style']);
244 // Test a non existent style plugin type returns no plugins.
245 $plugins = Views::fetchPluginNames('style', $this->randomString());
246 $this->assertIdentical($plugins, []);
250 * Tests the \Drupal\views\Views::pluginList() method.
252 public function testViewsPluginList() {
253 $plugin_list = Views::pluginList();
254 // Only plugins used by 'test_view' should be in the plugin list.
255 foreach (['display:default', 'pager:none'] as $key) {
256 list($plugin_type, $plugin_id) = explode(':', $key);
257 $plugin_def = $this->container->get("plugin.manager.views.$plugin_type")->getDefinition($plugin_id);
259 $this->assertTrue(isset($plugin_list[$key]), SafeMarkup::format('The expected @key plugin list key was found.', ['@key' => $key]));
260 $plugin_details = $plugin_list[$key];
262 $this->assertEqual($plugin_details['type'], $plugin_type, 'The expected plugin type was found.');
263 $this->assertEqual($plugin_details['title'], $plugin_def['title'], 'The expected plugin title was found.');
264 $this->assertEqual($plugin_details['provider'], $plugin_def['provider'], 'The expected plugin provider was found.');
265 $this->assertTrue(in_array('test_view', $plugin_details['views']), 'The test_view View was found in the list of views using this plugin.');
270 * Tests views.module: views_embed_view().
272 public function testViewsEmbedView() {
273 /** @var \Drupal\Core\Render\RendererInterface $renderer */
274 $renderer = \Drupal::service('renderer');
276 $result = views_embed_view('test_argument');
277 $renderer->renderPlain($result);
278 $this->assertEqual(count($result['view_build']['#view']->result), 5);
280 $result = views_embed_view('test_argument', 'default', 1);
281 $renderer->renderPlain($result);
282 $this->assertEqual(count($result['view_build']['#view']->result), 1);
284 $result = views_embed_view('test_argument', 'default', '1,2');
285 $renderer->renderPlain($result);
286 $this->assertEqual(count($result['view_build']['#view']->result), 2);
288 $result = views_embed_view('test_argument', 'default', '1,2', 'John');
289 $renderer->renderPlain($result);
290 $this->assertEqual(count($result['view_build']['#view']->result), 1);
292 $result = views_embed_view('test_argument', 'default', '1,2', 'John,George');
293 $renderer->renderPlain($result);
294 $this->assertEqual(count($result['view_build']['#view']->result), 2);
298 * Tests the \Drupal\views\ViewsExecutable::preview() method.
300 public function testViewsPreview() {
301 $view = Views::getView('test_argument');
302 $result = $view->preview('default');
303 $this->assertEqual(count($result['#view']->result), 5);
305 $view = Views::getView('test_argument');
306 $result = $view->preview('default', ['0' => 1]);
307 $this->assertEqual(count($result['#view']->result), 1);
309 $view = Views::getView('test_argument');
310 $result = $view->preview('default', ['3' => 1]);
311 $this->assertEqual(count($result['#view']->result), 1);
313 $view = Views::getView('test_argument');
314 $result = $view->preview('default', ['0' => '1,2']);
315 $this->assertEqual(count($result['#view']->result), 2);
317 $view = Views::getView('test_argument');
318 $result = $view->preview('default', ['3' => '1,2']);
319 $this->assertEqual(count($result['#view']->result), 2);
321 $view = Views::getView('test_argument');
322 $result = $view->preview('default', ['0' => '1,2', '1' => 'John']);
323 $this->assertEqual(count($result['#view']->result), 1);
325 $view = Views::getView('test_argument');
326 $result = $view->preview('default', ['3' => '1,2', '4' => 'John']);
327 $this->assertEqual(count($result['#view']->result), 1);
329 $view = Views::getView('test_argument');
330 $result = $view->preview('default', ['0' => '1,2', '1' => 'John,George']);
331 $this->assertEqual(count($result['#view']->result), 2);
333 $view = Views::getView('test_argument');
334 $result = $view->preview('default', ['3' => '1,2', '4' => 'John,George']);
335 $this->assertEqual(count($result['#view']->result), 2);
339 * Helper to return an expected views option array.
341 * @param array $views
342 * An array of Drupal\views\Entity\View objects for which to
343 * create an options array.
346 * A formatted options array that matches the expected output.
348 protected function formatViewOptions(array $views = []) {
349 $expected_options = [];
350 foreach ($views as $view) {
351 foreach ($view->get('display') as $display) {
352 $expected_options[$view->id() . ':' . $display['id']] = (string) t('View: @view - Display: @display',
353 ['@view' => $view->id(), '@display' => $display['id']]);
357 return $expected_options;
361 * Ensure that a certain handler is a instance of a certain table/field.
363 public function assertInstanceHandler($handler, $table, $field, $id) {
364 $table_data = $this->container->get('views.views_data')->get($table);
365 $field_data = $table_data[$field][$id];
367 $this->assertEqual($field_data['id'], $handler->getPluginId());