Chris@0: install(['test_theme']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test attribute merging. Chris@0: * Chris@0: * Render arrays that use a render element and templates (and hence call Chris@0: * template_preprocess()) must ensure the attributes at different occasions Chris@0: * are all merged correctly: Chris@0: * - $variables['attributes'] as passed in to the theme hook implementation. Chris@0: * - the render element's #attributes Chris@0: * - any attributes set in the template's preprocessing function Chris@0: */ Chris@0: public function testAttributeMerging() { Chris@0: $theme_test_render_element = [ Chris@0: 'elements' => [ Chris@0: '#attributes' => ['data-foo' => 'bar'], Chris@0: ], Chris@0: 'attributes' => [ Chris@0: 'id' => 'bazinga', Chris@0: ], Chris@0: ]; Chris@0: $this->assertThemeOutput('theme_test_render_element', $theme_test_render_element, '
' . "\n"); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test that ThemeManager renders the expected data types. Chris@0: */ Chris@0: public function testThemeDataTypes() { Chris@0: // theme_test_false is an implemented theme hook so \Drupal::theme() service Chris@0: // should return a string or an object that implements MarkupInterface, Chris@0: // even though the theme function itself can return anything. Chris@0: $foos = ['null' => NULL, 'false' => FALSE, 'integer' => 1, 'string' => 'foo', 'empty_string' => '']; Chris@0: foreach ($foos as $type => $example) { Chris@0: $output = \Drupal::theme()->render('theme_test_foo', ['foo' => $example]); Chris@0: $this->assertTrue($output instanceof MarkupInterface || is_string($output), format_string('\Drupal::theme() returns an object that implements MarkupInterface or a string for data type @type.', ['@type' => $type])); Chris@0: if ($output instanceof MarkupInterface) { Chris@0: $this->assertIdentical((string) $example, $output->__toString()); Chris@0: } Chris@0: elseif (is_string($output)) { Chris@0: $this->assertIdentical($output, '', 'A string will be return when the theme returns an empty string.'); Chris@0: } Chris@0: } Chris@0: Chris@0: // suggestionnotimplemented is not an implemented theme hook so \Drupal::theme() service Chris@0: // should return FALSE instead of a string. Chris@0: $output = \Drupal::theme()->render(['suggestionnotimplemented'], []); Chris@0: $this->assertIdentical($output, FALSE, '\Drupal::theme() returns FALSE when a hook suggestion is not implemented.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test function theme_get_suggestions() for SA-CORE-2009-003. Chris@0: */ Chris@0: public function testThemeSuggestions() { Chris@0: // Set the front page as something random otherwise the CLI Chris@0: // test runner fails. Chris@0: $this->config('system.site')->set('page.front', '/nobody-home')->save(); Chris@0: $args = ['node', '1', 'edit']; Chris@0: $suggestions = theme_get_suggestions($args, 'page'); Chris@0: $this->assertEqual($suggestions, ['page__node', 'page__node__%', 'page__node__1', 'page__node__edit'], 'Found expected node edit page suggestions'); Chris@0: // Check attack vectors. Chris@0: $args = ['node', '\\1']; Chris@0: $suggestions = theme_get_suggestions($args, 'page'); Chris@0: $this->assertEqual($suggestions, ['page__node', 'page__node__%', 'page__node__1'], 'Removed invalid \\ from suggestions'); Chris@0: $args = ['node', '1/']; Chris@0: $suggestions = theme_get_suggestions($args, 'page'); Chris@0: $this->assertEqual($suggestions, ['page__node', 'page__node__%', 'page__node__1'], 'Removed invalid / from suggestions'); Chris@0: $args = ['node', "1\0"]; Chris@0: $suggestions = theme_get_suggestions($args, 'page'); Chris@0: $this->assertEqual($suggestions, ['page__node', 'page__node__%', 'page__node__1'], 'Removed invalid \\0 from suggestions'); Chris@0: // Define path with hyphens to be used to generate suggestions. Chris@0: $args = ['node', '1', 'hyphen-path']; Chris@0: $result = ['page__node', 'page__node__%', 'page__node__1', 'page__node__hyphen_path']; Chris@0: $suggestions = theme_get_suggestions($args, 'page'); Chris@0: $this->assertEqual($suggestions, $result, 'Found expected page suggestions for paths containing hyphens.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Ensures preprocess functions run even for suggestion implementations. Chris@0: * Chris@0: * The theme hook used by this test has its base preprocess function in a Chris@0: * separate file, so this test also ensures that that file is correctly loaded Chris@0: * when needed. Chris@0: */ Chris@0: public function testPreprocessForSuggestions() { Chris@0: // Test with both an unprimed and primed theme registry. Chris@0: drupal_theme_rebuild(); Chris@0: for ($i = 0; $i < 2; $i++) { Chris@0: $this->drupalGet('theme-test/suggestion'); Chris@0: $this->assertText('Theme hook implementor=test_theme_theme_test__suggestion(). Foo=template_preprocess_theme_test', 'Theme hook suggestion ran with data available from a preprocess function for the base hook.'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the priority of some theme negotiators. Chris@0: */ Chris@0: public function testNegotiatorPriorities() { Chris@0: $this->drupalGet('theme-test/priority'); Chris@0: Chris@0: // Ensure that the custom theme negotiator was not able to set the theme. Chris@0: Chris@0: $this->assertNoText('Theme hook implementor=test_theme_theme_test__suggestion(). Foo=template_preprocess_theme_test', 'Theme hook suggestion ran with data available from a preprocess function for the base hook.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Ensures that non-HTML requests never initialize themes. Chris@0: */ Chris@0: public function testThemeOnNonHtmlRequest() { Chris@0: $this->drupalGet('theme-test/non-html'); Chris@0: $json = Json::decode($this->getRawContent()); Chris@0: $this->assertFalse($json['theme_initialized']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Ensure page-front template suggestion is added when on front page. Chris@0: */ Chris@0: public function testFrontPageThemeSuggestion() { Chris@0: // Set the current route to user.login because theme_get_suggestions() will Chris@0: // query it to see if we are on the front page. Chris@0: $request = Request::create('/user/login'); Chris@0: $request->attributes->set(RouteObjectInterface::ROUTE_NAME, 'user.login'); Chris@0: $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/user/login')); Chris@0: \Drupal::requestStack()->push($request); Chris@0: $this->config('system.site')->set('page.front', '/user/login')->save(); Chris@0: $suggestions = theme_get_suggestions(['user', 'login'], 'page'); Chris@0: // Set it back to not annoy the batch runner. Chris@0: \Drupal::requestStack()->pop(); Chris@0: $this->assertTrue(in_array('page__front', $suggestions), 'Front page template was suggested.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Ensures a theme's .info.yml file is able to override a module CSS file from being added to the page. Chris@0: * Chris@0: * @see test_theme.info.yml Chris@0: */ Chris@0: public function testCSSOverride() { Chris@0: // Reuse the same page as in testPreprocessForSuggestions(). We're testing Chris@0: // what is output to the HTML HEAD based on what is in a theme's .info.yml Chris@0: // file, so it doesn't matter what page we get, as long as it is themed with Chris@0: // the test theme. First we test with CSS aggregation disabled. Chris@0: $config = $this->config('system.performance'); Chris@0: $config->set('css.preprocess', 0); Chris@0: $config->save(); Chris@0: $this->drupalGet('theme-test/suggestion'); Chris@0: $this->assertNoText('js.module.css', 'The theme\'s .info.yml file is able to override a module CSS file from being added to the page.'); Chris@0: Chris@0: // Also test with aggregation enabled, simply ensuring no PHP errors are Chris@0: // triggered during drupal_build_css_cache() when a source file doesn't Chris@0: // exist. Then allow remaining tests to continue with aggregation disabled Chris@0: // by default. Chris@0: $config->set('css.preprocess', 1); Chris@0: $config->save(); Chris@0: $this->drupalGet('theme-test/suggestion'); Chris@0: $config->set('css.preprocess', 0); Chris@0: $config->save(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Ensures a themes template is overridable based on the 'template' filename. Chris@0: */ Chris@0: public function testTemplateOverride() { Chris@0: $this->config('system.theme') Chris@0: ->set('default', 'test_theme') Chris@0: ->save(); Chris@0: $this->drupalGet('theme-test/template-test'); Chris@0: $this->assertText('Success: Template overridden.', 'Template overridden by defined \'template\' filename.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Ensures a theme template can override a theme function. Chris@0: */ Chris@0: public function testFunctionOverride() { Chris@0: $this->drupalGet('theme-test/function-template-overridden'); Chris@0: $this->assertText('Success: Template overrides theme function.', 'Theme function overridden by test_theme template.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test the listInfo() function. Chris@0: */ Chris@0: public function testListThemes() { Chris@0: $theme_handler = $this->container->get('theme_handler'); Chris@0: $theme_handler->install(['test_subtheme']); Chris@0: $themes = $theme_handler->listInfo(); Chris@0: Chris@0: // Check if ThemeHandlerInterface::listInfo() retrieves enabled themes. Chris@0: $this->assertIdentical(1, $themes['test_theme']->status, 'Installed theme detected'); Chris@0: Chris@0: // Check if ThemeHandlerInterface::listInfo() returns disabled themes. Chris@0: // Check for base theme and subtheme lists. Chris@0: $base_theme_list = ['test_basetheme' => 'Theme test base theme']; Chris@0: $sub_theme_list = ['test_subsubtheme' => 'Theme test subsubtheme', 'test_subtheme' => 'Theme test subtheme']; Chris@0: Chris@0: $this->assertIdentical($themes['test_basetheme']->sub_themes, $sub_theme_list, 'Base theme\'s object includes list of subthemes.'); Chris@0: $this->assertIdentical($themes['test_subtheme']->base_themes, $base_theme_list, 'Subtheme\'s object includes list of base themes.'); Chris@0: // Check for theme engine in subtheme. Chris@0: $this->assertIdentical($themes['test_subtheme']->engine, 'twig', 'Subtheme\'s object includes the theme engine.'); Chris@0: // Check for theme engine prefix. Chris@0: $this->assertIdentical($themes['test_basetheme']->prefix, 'twig', 'Base theme\'s object includes the theme engine prefix.'); Chris@0: $this->assertIdentical($themes['test_subtheme']->prefix, 'twig', 'Subtheme\'s object includes the theme engine prefix.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests child element rendering for 'render element' theme hooks. Chris@0: */ Chris@0: public function testDrupalRenderChildren() { Chris@0: $element = [ Chris@0: '#theme' => 'theme_test_render_element_children', Chris@0: 'child' => [ Chris@0: '#markup' => 'Foo', Chris@0: ], Chris@0: ]; Chris@0: $this->assertThemeOutput('theme_test_render_element_children', $element, 'Foo', 'drupal_render() avoids #theme recursion loop when rendering a render element.'); Chris@0: Chris@0: $element = [ Chris@0: '#theme_wrappers' => ['theme_test_render_element_children'], Chris@0: 'child' => [ Chris@0: '#markup' => 'Foo', Chris@0: ], Chris@0: ]; Chris@0: $this->assertThemeOutput('theme_test_render_element_children', $element, 'Foo', 'drupal_render() avoids #theme_wrappers recursion loop when rendering a render element.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests theme can provide classes. Chris@0: */ Chris@0: public function testClassLoading() { Chris@0: new ThemeClass(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests drupal_find_theme_templates(). Chris@0: */ Chris@0: public function testFindThemeTemplates() { Chris@0: $registry = $this->container->get('theme.registry')->get(); Chris@0: $templates = drupal_find_theme_templates($registry, '.html.twig', drupal_get_path('theme', 'test_theme')); Chris@0: $this->assertEqual($templates['node__1']['template'], 'node--1', 'Template node--1.tpl.twig was found in test_theme.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that the page variable is not prematurely flattened. Chris@0: * Chris@0: * Some modules check the page array in template_preprocess_html(), so we Chris@0: * ensure that it has not been rendered prematurely. Chris@0: */ Chris@0: public function testPreprocessHtml() { Chris@0: $this->drupalGet(''); Chris@0: $attributes = $this->xpath('/html/body[@theme_test_page_variable="Page variable is an array."]'); Chris@0: $this->assertTrue(count($attributes) == 1, 'In template_preprocess_html(), the page variable is still an array (not rendered yet).'); Chris@0: $this->assertText('theme test page bottom markup', 'Modules are able to set the page bottom region.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that region attributes can be manipulated via preprocess functions. Chris@0: */ Chris@0: public function testRegionClass() { Chris@0: \Drupal::service('module_installer')->install(['block', 'theme_region_test']); Chris@0: Chris@0: // Place a block. Chris@0: $this->drupalPlaceBlock('system_main_block'); Chris@0: $this->drupalGet(''); Chris@0: $elements = $this->cssSelect(".region-sidebar-first.new_class"); Chris@0: $this->assertEqual(count($elements), 1, 'New class found.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Ensures suggestion preprocess functions run for default implementations. Chris@0: * Chris@0: * The theme hook used by this test has its base preprocess function in a Chris@0: * separate file, so this test also ensures that that file is correctly loaded Chris@0: * when needed. Chris@0: */ Chris@0: public function testSuggestionPreprocessForDefaults() { Chris@0: $this->config('system.theme')->set('default', 'test_theme')->save(); Chris@0: // Test with both an unprimed and primed theme registry. Chris@0: drupal_theme_rebuild(); Chris@0: for ($i = 0; $i < 2; $i++) { Chris@0: $this->drupalGet('theme-test/preprocess-suggestions'); Chris@0: $items = $this->cssSelect('.suggestion'); Chris@0: $expected_values = [ Chris@0: 'Suggestion', Chris@0: 'Kitten', Chris@0: 'Monkey', Chris@0: 'Kitten', Chris@0: 'Flamingo', Chris@0: ]; Chris@0: foreach ($expected_values as $key => $value) { Chris@0: $this->assertEqual((string) $value, $items[$key]); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: }