Chris@0: $this->randomMachineName(), Chris@0: 'field' => $this->randomMachineName(), Chris@0: ]; Chris@0: $handler = $this->container->get('plugin.manager.views.' . $type)->getHandler($item); Chris@0: $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: } Chris@0: Chris@0: $views_data = $this->viewsData(); Chris@0: $test_tables = ['views_test_data' => ['id', 'name']]; Chris@0: foreach ($test_tables as $table => $fields) { Chris@0: foreach ($fields as $field) { Chris@0: $data = $views_data[$table][$field]; Chris@0: $item = [ Chris@0: 'table' => $table, Chris@0: 'field' => $field, Chris@0: ]; Chris@0: foreach ($data as $id => $field_data) { Chris@0: if (!in_array($id, ['title', 'help'])) { Chris@0: $handler = $this->container->get('plugin.manager.views.' . $id)->getHandler($item); Chris@0: $this->assertInstanceHandler($handler, $table, $field, $id); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: // Test the override handler feature. Chris@0: $item = [ Chris@0: 'table' => 'views_test_data', Chris@0: 'field' => 'job', Chris@0: ]; Chris@0: $handler = $this->container->get('plugin.manager.views.filter')->getHandler($item, 'standard'); Chris@0: $this->assertTrue($handler instanceof Standard); Chris@0: Chris@0: // @todo Reinstate these tests when the debug() in views_get_handler() is Chris@0: // restored. Chris@0: return; Chris@0: Chris@0: // Test non-existent tables/fields. Chris@0: set_error_handler([$this, 'customErrorHandler']); Chris@0: $item = [ Chris@0: 'table' => 'views_test_data', Chris@0: 'field' => 'field_invalid', Chris@0: ]; Chris@0: $this->container->get('plugin.manager.views.field')->getHandler($item); Chris@0: $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: unset($this->lastErrorMessage); Chris@0: Chris@0: $item = [ Chris@0: 'table' => 'table_invalid', Chris@0: 'field' => 'id', Chris@0: ]; Chris@0: $this->container->get('plugin.manager.views.filter')->getHandler($item); Chris@0: $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: unset($this->lastErrorMessage); Chris@0: Chris@0: $item = [ Chris@0: 'table' => 'table_invalid', Chris@0: 'field' => 'id', Chris@0: ]; Chris@0: $this->container->get('plugin.manager.views.filter')->getHandler($item); Chris@0: $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: unset($this->lastErrorMessage); Chris@0: Chris@0: restore_error_handler(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Defines an error handler which is used in the test. Chris@0: * Chris@0: * Because this is registered in set_error_handler(), it has to be public. Chris@0: * Chris@0: * @param int $error_level Chris@0: * The level of the error raised. Chris@0: * @param string $message Chris@0: * The error message. Chris@0: * @param string $filename Chris@0: * The filename that the error was raised in. Chris@0: * @param int $line Chris@0: * The line number the error was raised at. Chris@0: * @param array $context Chris@0: * An array that points to the active symbol table at the point the error Chris@0: * occurred. Chris@0: * Chris@0: * @see set_error_handler() Chris@0: */ Chris@0: public function customErrorHandler($error_level, $message, $filename, $line, $context) { Chris@0: $this->lastErrorMessage = $message; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the load wrapper/helper functions. Chris@0: */ Chris@0: public function testLoadFunctions() { Chris@0: $this->enableModules(['text', 'node']); Chris@18: $this->installEntitySchema('node'); Chris@0: $this->installConfig(['node']); Chris@0: $storage = $this->container->get('entity.manager')->getStorage('view'); Chris@0: Chris@0: // Test views_view_is_enabled/disabled. Chris@0: $archive = $storage->load('archive'); Chris@0: $this->assertTrue(views_view_is_disabled($archive), 'views_view_is_disabled works as expected.'); Chris@0: // Enable the view and check this. Chris@0: $archive->enable(); Chris@0: $this->assertTrue(views_view_is_enabled($archive), ' views_view_is_enabled works as expected.'); Chris@0: Chris@0: // We can store this now, as we have enabled/disabled above. Chris@0: $all_views = $storage->loadMultiple(); Chris@0: Chris@0: // Test Views::getAllViews(). Chris@0: ksort($all_views); Chris@0: $this->assertEquals(array_keys($all_views), array_keys(Views::getAllViews()), 'Views::getAllViews works as expected.'); Chris@0: Chris@0: // Test Views::getEnabledViews(). Chris@0: $expected_enabled = array_filter($all_views, function ($view) { Chris@0: return views_view_is_enabled($view); Chris@0: }); Chris@0: $this->assertEquals(array_keys($expected_enabled), array_keys(Views::getEnabledViews()), 'Expected enabled views returned.'); Chris@0: Chris@0: // Test Views::getDisabledViews(). Chris@0: $expected_disabled = array_filter($all_views, function ($view) { Chris@0: return views_view_is_disabled($view); Chris@0: }); Chris@0: $this->assertEquals(array_keys($expected_disabled), array_keys(Views::getDisabledViews()), 'Expected disabled views returned.'); Chris@0: Chris@0: // Test Views::getViewsAsOptions(). Chris@0: // Test the $views_only parameter. Chris@0: $this->assertIdentical(array_keys($all_views), array_keys(Views::getViewsAsOptions(TRUE)), 'Expected option keys for all views were returned.'); Chris@0: $expected_options = []; Chris@0: foreach ($all_views as $id => $view) { Chris@0: $expected_options[$id] = $view->label(); Chris@0: } Chris@0: $this->assertIdentical($expected_options, $this->castSafeStrings(Views::getViewsAsOptions(TRUE)), 'Expected options array was returned.'); Chris@0: Chris@0: // Test the default. Chris@0: $this->assertIdentical($this->formatViewOptions($all_views), $this->castSafeStrings(Views::getViewsAsOptions()), 'Expected options array for all views was returned.'); Chris@0: // Test enabled views. Chris@0: $this->assertIdentical($this->formatViewOptions($expected_enabled), $this->castSafeStrings(Views::getViewsAsOptions(FALSE, 'enabled')), 'Expected enabled options array was returned.'); Chris@0: // Test disabled views. Chris@0: $this->assertIdentical($this->formatViewOptions($expected_disabled), $this->castSafeStrings(Views::getViewsAsOptions(FALSE, 'disabled')), 'Expected disabled options array was returned.'); Chris@0: Chris@0: // Test the sort parameter. Chris@0: $all_views_sorted = $all_views; Chris@0: ksort($all_views_sorted); Chris@0: $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: Chris@0: // Test $exclude_view parameter. Chris@0: $this->assertFalse(array_key_exists('archive', Views::getViewsAsOptions(TRUE, 'all', 'archive')), 'View excluded from options based on name'); Chris@0: $this->assertFalse(array_key_exists('archive:default', Views::getViewsAsOptions(FALSE, 'all', 'archive:default')), 'View display excluded from options based on name'); Chris@0: $this->assertFalse(array_key_exists('archive', Views::getViewsAsOptions(TRUE, 'all', $archive->getExecutable())), 'View excluded from options based on object'); Chris@0: Chris@0: // Test the $opt_group parameter. Chris@0: $expected_opt_groups = []; Chris@0: foreach ($all_views as $view) { Chris@0: foreach ($view->get('display') as $display) { Chris@0: $expected_opt_groups[$view->id()][$view->id() . ':' . $display['id']] = (string) t('@view : @display', ['@view' => $view->id(), '@display' => $display['id']]); Chris@0: } Chris@0: } Chris@0: $this->assertIdentical($expected_opt_groups, $this->castSafeStrings(Views::getViewsAsOptions(FALSE, 'all', NULL, TRUE)), 'Expected option array for an option group returned.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests view enable and disable procedural wrapper functions. Chris@0: */ Chris@0: public function testStatusFunctions() { Chris@0: $view = Views::getView('test_view_status')->storage; Chris@0: Chris@0: $this->assertFalse($view->status(), 'The view status is disabled.'); Chris@0: Chris@0: views_enable_view($view); Chris@0: $this->assertTrue($view->status(), 'A view has been enabled.'); Chris@0: $this->assertEqual($view->status(), views_view_is_enabled($view), 'views_view_is_enabled is correct.'); Chris@0: Chris@0: views_disable_view($view); Chris@0: $this->assertFalse($view->status(), 'A view has been disabled.'); Chris@0: $this->assertEqual(!$view->status(), views_view_is_disabled($view), 'views_view_is_disabled is correct.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the \Drupal\views\Views::fetchPluginNames() method. Chris@0: */ Chris@0: public function testViewsFetchPluginNames() { Chris@0: // All style plugins should be returned, as we have not specified a type. Chris@0: $plugins = Views::fetchPluginNames('style'); Chris@0: $definitions = $this->container->get('plugin.manager.views.style')->getDefinitions(); Chris@0: $expected = []; Chris@0: foreach ($definitions as $id => $definition) { Chris@0: $expected[$id] = $definition['title']; Chris@0: } Chris@0: asort($expected); Chris@0: $this->assertIdentical(array_keys($plugins), array_keys($expected)); Chris@0: Chris@0: // Test using the 'test' style plugin type only returns the test_style and Chris@0: // mapping_test plugins. Chris@0: $plugins = Views::fetchPluginNames('style', 'test'); Chris@0: $this->assertIdentical(array_keys($plugins), ['mapping_test', 'test_style', 'test_template_style']); Chris@0: Chris@0: // Test a non existent style plugin type returns no plugins. Chris@0: $plugins = Views::fetchPluginNames('style', $this->randomString()); Chris@0: $this->assertIdentical($plugins, []); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the \Drupal\views\Views::pluginList() method. Chris@0: */ Chris@0: public function testViewsPluginList() { Chris@0: $plugin_list = Views::pluginList(); Chris@0: // Only plugins used by 'test_view' should be in the plugin list. Chris@0: foreach (['display:default', 'pager:none'] as $key) { Chris@0: list($plugin_type, $plugin_id) = explode(':', $key); Chris@0: $plugin_def = $this->container->get("plugin.manager.views.$plugin_type")->getDefinition($plugin_id); Chris@0: Chris@17: $this->assertTrue(isset($plugin_list[$key]), new FormattableMarkup('The expected @key plugin list key was found.', ['@key' => $key])); Chris@0: $plugin_details = $plugin_list[$key]; Chris@0: Chris@0: $this->assertEqual($plugin_details['type'], $plugin_type, 'The expected plugin type was found.'); Chris@0: $this->assertEqual($plugin_details['title'], $plugin_def['title'], 'The expected plugin title was found.'); Chris@0: $this->assertEqual($plugin_details['provider'], $plugin_def['provider'], 'The expected plugin provider was found.'); Chris@0: $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: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests views.module: views_embed_view(). Chris@0: */ Chris@0: public function testViewsEmbedView() { Chris@0: /** @var \Drupal\Core\Render\RendererInterface $renderer */ Chris@0: $renderer = \Drupal::service('renderer'); Chris@0: Chris@0: $result = views_embed_view('test_argument'); Chris@0: $renderer->renderPlain($result); Chris@0: $this->assertEqual(count($result['view_build']['#view']->result), 5); Chris@0: Chris@0: $result = views_embed_view('test_argument', 'default', 1); Chris@0: $renderer->renderPlain($result); Chris@0: $this->assertEqual(count($result['view_build']['#view']->result), 1); Chris@0: Chris@0: $result = views_embed_view('test_argument', 'default', '1,2'); Chris@0: $renderer->renderPlain($result); Chris@0: $this->assertEqual(count($result['view_build']['#view']->result), 2); Chris@0: Chris@0: $result = views_embed_view('test_argument', 'default', '1,2', 'John'); Chris@0: $renderer->renderPlain($result); Chris@0: $this->assertEqual(count($result['view_build']['#view']->result), 1); Chris@0: Chris@0: $result = views_embed_view('test_argument', 'default', '1,2', 'John,George'); Chris@0: $renderer->renderPlain($result); Chris@0: $this->assertEqual(count($result['view_build']['#view']->result), 2); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the \Drupal\views\ViewsExecutable::preview() method. Chris@0: */ Chris@0: public function testViewsPreview() { Chris@0: $view = Views::getView('test_argument'); Chris@0: $result = $view->preview('default'); Chris@0: $this->assertEqual(count($result['#view']->result), 5); Chris@0: Chris@0: $view = Views::getView('test_argument'); Chris@0: $result = $view->preview('default', ['0' => 1]); Chris@0: $this->assertEqual(count($result['#view']->result), 1); Chris@0: Chris@0: $view = Views::getView('test_argument'); Chris@0: $result = $view->preview('default', ['3' => 1]); Chris@0: $this->assertEqual(count($result['#view']->result), 1); Chris@0: Chris@0: $view = Views::getView('test_argument'); Chris@0: $result = $view->preview('default', ['0' => '1,2']); Chris@0: $this->assertEqual(count($result['#view']->result), 2); Chris@0: Chris@0: $view = Views::getView('test_argument'); Chris@0: $result = $view->preview('default', ['3' => '1,2']); Chris@0: $this->assertEqual(count($result['#view']->result), 2); Chris@0: Chris@0: $view = Views::getView('test_argument'); Chris@0: $result = $view->preview('default', ['0' => '1,2', '1' => 'John']); Chris@0: $this->assertEqual(count($result['#view']->result), 1); Chris@0: Chris@0: $view = Views::getView('test_argument'); Chris@0: $result = $view->preview('default', ['3' => '1,2', '4' => 'John']); Chris@0: $this->assertEqual(count($result['#view']->result), 1); Chris@0: Chris@0: $view = Views::getView('test_argument'); Chris@0: $result = $view->preview('default', ['0' => '1,2', '1' => 'John,George']); Chris@0: $this->assertEqual(count($result['#view']->result), 2); Chris@0: Chris@0: $view = Views::getView('test_argument'); Chris@0: $result = $view->preview('default', ['3' => '1,2', '4' => 'John,George']); Chris@0: $this->assertEqual(count($result['#view']->result), 2); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Helper to return an expected views option array. Chris@0: * Chris@0: * @param array $views Chris@0: * An array of Drupal\views\Entity\View objects for which to Chris@0: * create an options array. Chris@0: * Chris@0: * @return array Chris@0: * A formatted options array that matches the expected output. Chris@0: */ Chris@0: protected function formatViewOptions(array $views = []) { Chris@0: $expected_options = []; Chris@0: foreach ($views as $view) { Chris@0: foreach ($view->get('display') as $display) { Chris@0: $expected_options[$view->id() . ':' . $display['id']] = (string) t('View: @view - Display: @display', Chris@0: ['@view' => $view->id(), '@display' => $display['id']]); Chris@0: } Chris@0: } Chris@0: Chris@0: return $expected_options; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Ensure that a certain handler is a instance of a certain table/field. Chris@0: */ Chris@0: public function assertInstanceHandler($handler, $table, $field, $id) { Chris@0: $table_data = $this->container->get('views.views_data')->get($table); Chris@0: $field_data = $table_data[$field][$id]; Chris@0: Chris@0: $this->assertEqual($field_data['id'], $handler->getPluginId()); Chris@0: } Chris@0: Chris@0: }