View source
<?php
namespace Drupal\Tests\system\Functional\Theme;
use Drupal\Component\Serialization\Json;
use Drupal\Tests\BrowserTestBase;
use Drupal\Core\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
class ThemeTest extends BrowserTestBase {
protected static $modules = [
'theme_test',
'node',
];
protected $defaultTheme = 'classy';
protected function setUp() : void {
parent::setUp();
\Drupal::service('theme_installer')
->install([
'test_theme',
]);
}
public function testPreprocessForSuggestions() {
drupal_theme_rebuild();
for ($i = 0; $i < 2; $i++) {
$this
->drupalGet('theme-test/suggestion');
$this
->assertSession()
->pageTextContains('Theme hook implementor=theme-test--suggestion.html.twig. Foo=template_preprocess_theme_test');
}
}
public function testNegotiatorPriorities() {
$this
->drupalGet('theme-test/priority');
$this
->assertSession()
->pageTextNotContains('Theme hook implementor=theme-test--suggestion.html.twig. Foo=template_preprocess_theme_test');
}
public function testThemeOnNonHtmlRequest() {
$this
->drupalGet('theme-test/non-html');
$json = Json::decode($this
->getSession()
->getPage()
->getContent());
$this
->assertFalse($json['theme_initialized']);
}
public function testFrontPageThemeSuggestion() {
$request = Request::create('/user/login');
$request->attributes
->set(RouteObjectInterface::ROUTE_NAME, 'user.login');
$request->attributes
->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/user/login'));
\Drupal::requestStack()
->push($request);
$this
->config('system.site')
->set('page.front', '/user/login')
->save();
$suggestions = theme_get_suggestions([
'user',
'login',
], 'page');
\Drupal::requestStack()
->pop();
$this
->assertContains('page__front', $suggestions, 'Front page template was suggested.');
}
public function testClassLoading() {
$this
->config('system.theme')
->set('default', 'test_theme')
->save();
$this
->resetAll();
$this
->drupalGet('/theme-test/test-theme-class');
$this
->assertSession()
->pageTextContains('Loading ThemeClass was successful.');
}
public function testCSSOverride() {
$config = $this
->config('system.performance');
$config
->set('css.preprocess', 0);
$config
->save();
$this
->drupalGet('theme-test/suggestion');
$this
->assertSession()
->responseNotContains('js.module.css?');
$config
->set('css.preprocess', 1);
$config
->save();
$this
->drupalGet('theme-test/suggestion');
$config
->set('css.preprocess', 0);
$config
->save();
}
public function testTemplateOverride() {
$this
->config('system.theme')
->set('default', 'test_theme')
->save();
$this
->drupalGet('theme-test/template-test');
$this
->assertSession()
->pageTextContains('Success: Template overridden.');
}
public function testPreprocessHtml() {
$this
->drupalGet('');
$attributes = $this
->xpath('/body[@theme_test_page_variable="Page variable is an array."]');
$this
->assertCount(1, $attributes, 'In template_preprocess_html(), the page variable is still an array (not rendered yet).');
$this
->assertSession()
->pageTextContains('theme test page bottom markup');
}
public function testRegionClass() {
\Drupal::service('module_installer')
->install([
'block',
'theme_region_test',
]);
$this
->drupalPlaceBlock('system_main_block', [
'region' => 'sidebar_first',
]);
$this
->drupalGet('');
$elements = $this
->cssSelect(".region-sidebar-first.new_class");
$this
->assertCount(1, $elements, 'New class found.');
}
public function testSuggestionPreprocessForDefaults() {
$this
->config('system.theme')
->set('default', 'test_theme')
->save();
drupal_theme_rebuild();
for ($i = 0; $i < 2; $i++) {
$this
->drupalGet('theme-test/preprocess-suggestions');
$items = $this
->cssSelect('.suggestion');
$expected_values = [
'Suggestion',
'Kitten',
'Monkey',
'Kitten',
'Flamingo',
];
foreach ($expected_values as $key => $value) {
$this
->assertEquals((string) $value, $items[$key]
->getText());
}
}
}
public function testPreprocessCallback() {
$this
->drupalGet('theme-test/preprocess-callback');
$this
->assertSession()
->pageTextContains('Make Drupal full of kittens again!');
}
}