Chris@0: install(['test_theme']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that hooks to provide theme suggestions work. Chris@0: */ Chris@0: public function testTemplateSuggestions() { Chris@0: $this->drupalGet('theme-test/suggestion-provided'); Chris@0: $this->assertText('Template for testing suggestions provided by the module declaring the theme hook.'); Chris@0: Chris@0: // Install test_theme, it contains a template suggested by theme_test.module Chris@0: // in theme_test_theme_suggestions_theme_test_suggestion_provided(). Chris@0: $this->config('system.theme') Chris@0: ->set('default', 'test_theme') Chris@0: ->save(); Chris@0: Chris@0: $this->drupalGet('theme-test/suggestion-provided'); Chris@0: $this->assertText('Template overridden based on suggestion provided by the module declaring the theme hook.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests hook_theme_suggestions_alter(). Chris@0: */ Chris@0: public function testGeneralSuggestionsAlter() { Chris@0: $this->drupalGet('theme-test/general-suggestion-alter'); Chris@0: $this->assertText('Original template for testing hook_theme_suggestions_alter().'); Chris@0: Chris@0: // Install test_theme and test that themes can alter template suggestions. Chris@0: $this->config('system.theme') Chris@0: ->set('default', 'test_theme') Chris@0: ->save(); Chris@0: $this->drupalGet('theme-test/general-suggestion-alter'); Chris@0: $this->assertText('Template overridden based on new theme suggestion provided by the test_theme theme via hook_theme_suggestions_alter().'); Chris@0: Chris@0: // Enable the theme_suggestions_test module to test modules implementing Chris@0: // suggestions alter hooks. Chris@0: \Drupal::service('module_installer')->install(['theme_suggestions_test']); Chris@0: $this->resetAll(); Chris@0: $this->drupalGet('theme-test/general-suggestion-alter'); Chris@0: $this->assertText('Template overridden based on new theme suggestion provided by a module via hook_theme_suggestions_alter().'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that theme suggestion alter hooks work for templates. Chris@0: */ Chris@0: public function testTemplateSuggestionsAlter() { Chris@0: $this->drupalGet('theme-test/suggestion-alter'); Chris@0: $this->assertText('Original template for testing hook_theme_suggestions_HOOK_alter().'); Chris@0: Chris@0: // Install test_theme and test that themes can alter template suggestions. Chris@0: $this->config('system.theme') Chris@0: ->set('default', 'test_theme') Chris@0: ->save(); Chris@0: $this->drupalGet('theme-test/suggestion-alter'); Chris@0: $this->assertText('Template overridden based on new theme suggestion provided by the test_theme theme via hook_theme_suggestions_HOOK_alter().'); Chris@0: Chris@0: // Enable the theme_suggestions_test module to test modules implementing Chris@0: // suggestions alter hooks. Chris@0: \Drupal::service('module_installer')->install(['theme_suggestions_test']); Chris@0: $this->resetAll(); Chris@0: $this->drupalGet('theme-test/suggestion-alter'); Chris@0: $this->assertText('Template overridden based on new theme suggestion provided by a module via hook_theme_suggestions_HOOK_alter().'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that theme suggestion alter hooks work for specific theme calls. Chris@0: */ Chris@0: public function testSpecificSuggestionsAlter() { Chris@0: // Test that the default template is rendered. Chris@0: $this->drupalGet('theme-test/specific-suggestion-alter'); Chris@0: $this->assertText('Template for testing specific theme calls.'); Chris@0: Chris@0: $this->config('system.theme') Chris@0: ->set('default', 'test_theme') Chris@0: ->save(); Chris@0: Chris@0: // Test a specific theme call similar to '#theme' => 'node__article'. Chris@0: $this->drupalGet('theme-test/specific-suggestion-alter'); Chris@0: $this->assertText('Template matching the specific theme call.'); Chris@0: $this->assertText('theme_test_specific_suggestions__variant', 'Specific theme call is added to the suggestions array.'); Chris@0: Chris@0: // Ensure that the base hook is used to determine the suggestion alter hook. Chris@0: \Drupal::service('module_installer')->install(['theme_suggestions_test']); Chris@0: $this->resetAll(); Chris@0: $this->drupalGet('theme-test/specific-suggestion-alter'); Chris@0: $this->assertText('Template overridden based on suggestion alter hook determined by the base hook.'); Chris@0: $this->assertTrue(strpos($this->getRawContent(), 'theme_test_specific_suggestions__variant') < strpos($this->getRawContent(), 'theme_test_specific_suggestions__variant__foo'), 'Specific theme call is added to the suggestions array before the suggestions alter hook.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that theme suggestion alter hooks work for theme functions. Chris@0: */ Chris@0: public function testThemeFunctionSuggestionsAlter() { Chris@0: $this->drupalGet('theme-test/function-suggestion-alter'); Chris@0: $this->assertText('Original theme function.'); Chris@0: Chris@0: // Install test_theme and test that themes can alter theme suggestions. Chris@0: $this->config('system.theme') Chris@0: ->set('default', 'test_theme') Chris@0: ->save(); Chris@0: $this->drupalGet('theme-test/function-suggestion-alter'); Chris@0: $this->assertText('Theme function overridden based on new theme suggestion provided by the test_theme theme.'); Chris@0: Chris@0: // Enable the theme_suggestions_test module to test modules implementing Chris@0: // suggestions alter hooks. Chris@0: \Drupal::service('module_installer')->install(['theme_suggestions_test']); Chris@0: $this->resetAll(); Chris@0: $this->drupalGet('theme-test/function-suggestion-alter'); Chris@0: $this->assertText('Theme function overridden based on new theme suggestion provided by a module.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that theme suggestion alter hooks work with theme hook includes. Chris@0: */ Chris@0: public function testSuggestionsAlterInclude() { Chris@0: // Check the original theme output. Chris@0: $this->drupalGet('theme-test/suggestion-alter-include'); Chris@0: $this->assertText('Original function before altering theme suggestions.'); Chris@0: Chris@0: // Enable theme_suggestions_test module and make two requests to make sure Chris@0: // the include file is always loaded. The file will always be included for Chris@0: // the first request because the theme registry is being rebuilt. Chris@0: \Drupal::service('module_installer')->install(['theme_suggestions_test']); Chris@0: $this->resetAll(); Chris@0: $this->drupalGet('theme-test/suggestion-alter-include'); Chris@0: $this->assertText('Function suggested via suggestion alter hook found in include file.', 'Include file loaded for initial request.'); Chris@0: $this->drupalGet('theme-test/suggestion-alter-include'); Chris@0: $this->assertText('Function suggested via suggestion alter hook found in include file.', 'Include file loaded for second request.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests execution order of theme suggestion alter hooks. Chris@0: * Chris@0: * hook_theme_suggestions_alter() should fire before Chris@0: * hook_theme_suggestions_HOOK_alter() within an extension (module or theme). Chris@0: */ Chris@0: public function testExecutionOrder() { Chris@0: // Install our test theme and module. Chris@0: $this->config('system.theme') Chris@0: ->set('default', 'test_theme') Chris@0: ->save(); Chris@0: \Drupal::service('module_installer')->install(['theme_suggestions_test']); Chris@0: $this->resetAll(); Chris@0: Chris@0: // Send two requests so that we get all the messages we've set via Chris@0: // drupal_set_message(). Chris@0: $this->drupalGet('theme-test/suggestion-alter'); Chris@0: // Ensure that the order is first by extension, then for a given extension, Chris@0: // the hook-specific one after the generic one. Chris@0: $expected = [ Chris@0: 'theme_suggestions_test_theme_suggestions_alter() executed.', Chris@0: 'theme_suggestions_test_theme_suggestions_theme_test_suggestions_alter() executed.', Chris@0: 'theme_test_theme_suggestions_alter() executed.', Chris@0: 'theme_test_theme_suggestions_theme_test_suggestions_alter() executed.', Chris@0: 'test_theme_theme_suggestions_alter() executed.', Chris@0: 'test_theme_theme_suggestions_theme_test_suggestions_alter() executed.', Chris@0: ]; Chris@0: $content = preg_replace('/\s+/', ' ', Xss::filter($this->content, [])); Chris@0: $this->assert(strpos($content, implode(' ', $expected)) !== FALSE, 'Suggestion alter hooks executed in the expected order.'); Chris@0: } Chris@0: Chris@0: }