Commit e8c9956d authored by catch's avatar catch
Browse files

Issue #2863429 by Lendude, ApacheEx, Jo Fitzgerald, borisson_: Theme: Convert...

Issue #2863429 by Lendude, ApacheEx, Jo Fitzgerald, borisson_: Theme: Convert system functional tests to phpunit
parent d67d9f32
......@@ -178,7 +178,7 @@ function theme_test_theme_suggestions_theme_test_suggestion_provided(array $vari
* Implements hook_theme_suggestions_alter().
*/
function theme_test_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
\Drupal::messenger()->addStatus(__FUNCTION__ . '() executed.');
\Drupal::messenger()->addStatus(__FUNCTION__ . '() executed for ' . $hook . '.');
}
/**
......
<?php
namespace Drupal\system\Tests\Theme;
namespace Drupal\Tests\system\Functional\Theme;
use Drupal\Core\Render\Markup;
use Drupal\Core\Url;
use Drupal\simpletest\WebTestBase;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
/**
* Tests Twig-specific theme functionality.
*
* @group Theme
*/
class EngineTwigTest extends WebTestBase {
class EngineTwigTest extends BrowserTestBase {
use AssertPageCacheContextsAndTagsTrait;
/**
* Modules to enable.
......
<?php
namespace Drupal\system\Tests\Theme;
namespace Drupal\Tests\system\Functional\Theme;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\Extension\ExtensionDiscovery;
use Drupal\comment\CommentInterface;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\node\NodeInterface;
use Drupal\simpletest\WebTestBase;
use Drupal\comment\Entity\Comment;
use Drupal\taxonomy\Entity\Term;
use Drupal\Tests\BrowserTestBase;
/**
* Tests themed output for each entity type in all available themes to ensure
......@@ -17,7 +17,7 @@
*
* @group Theme
*/
class EntityFilteringThemeTest extends WebTestBase {
class EntityFilteringThemeTest extends BrowserTestBase {
use CommentTestTrait;
......
<?php
namespace Drupal\system\Tests\Theme;
namespace Drupal\Tests\system\Functional\Theme;
use Drupal\simpletest\WebTestBase;
use Drupal\Tests\BrowserTestBase;
/**
* Tests attributes inserted in the 'html' and 'body' elements on the page.
*
* @group Theme
*/
class HtmlAttributesTest extends WebTestBase {
class HtmlAttributesTest extends BrowserTestBase {
/**
* Modules to enable.
......@@ -23,9 +23,8 @@ class HtmlAttributesTest extends WebTestBase {
*/
public function testThemeHtmlAttributes() {
$this->drupalGet('');
$attributes = $this->xpath('/html[@theme_test_html_attribute="theme test html attribute value"]');
$this->assertTrue(count($attributes) == 1, "Attribute set in the 'html' element via hook_preprocess_HOOK() found.");
$attributes = $this->xpath('/html/body[@theme_test_body_attribute="theme test body attribute value"]');
$this->assertSession()->responseContains('<html lang="en" dir="ltr" theme_test_html_attribute="theme test html attribute value">');
$attributes = $this->xpath('/body[@theme_test_body_attribute="theme test body attribute value"]');
$this->assertTrue(count($attributes) == 1, "Attribute set in the 'body' element via hook_preprocess_HOOK() found.");
}
......
<?php
namespace Drupal\system\Tests\Theme;
namespace Drupal\Tests\system\Functional\Theme;
use Drupal\Component\Utility\Xss;
use Drupal\simpletest\WebTestBase;
use Drupal\Tests\BrowserTestBase;
/**
* Tests theme suggestion alter hooks.
*
* @group Theme
*/
class ThemeSuggestionsAlterTest extends WebTestBase {
class ThemeSuggestionsAlterTest extends BrowserTestBase {
/**
* Modules to enable.
......@@ -170,16 +170,20 @@ public function testExecutionOrder() {
$this->drupalGet('theme-test/suggestion-alter');
// Ensure that the order is first by extension, then for a given extension,
// the hook-specific one after the generic one.
$expected = [
$expected_order = [
'theme_suggestions_test_theme_suggestions_alter() executed.',
'theme_suggestions_test_theme_suggestions_theme_test_suggestions_alter() executed.',
'theme_test_theme_suggestions_alter() executed.',
'theme_test_theme_suggestions_alter() executed for theme_test_suggestions.',
'theme_test_theme_suggestions_theme_test_suggestions_alter() executed.',
'test_theme_theme_suggestions_alter() executed.',
'test_theme_theme_suggestions_theme_test_suggestions_alter() executed.',
];
$content = preg_replace('/\s+/', ' ', Xss::filter($this->content, []));
$this->assert(strpos($content, implode(' ', $expected)) !== FALSE, 'Suggestion alter hooks executed in the expected order.');
$content = preg_replace('/\s+/', ' ', Xss::filter($this->getSession()->getPage()->getContent(), []));
$order = 0;
foreach ($expected_order as $expected_string) {
$this->assertGreaterThan($order, strpos($content, $expected_string));
$order = strpos($content, $expected_string);
}
}
}
<?php
namespace Drupal\Tests\system\Functional\Theme;
use Drupal\Component\Serialization\Json;
use Drupal\Tests\BrowserTestBase;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
/**
* Tests low-level theme functions.
*
* @group Theme
*/
class ThemeTest extends BrowserTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['theme_test', 'node'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
\Drupal::service('theme_handler')->install(['test_theme']);
}
/**
* Ensures preprocess functions run even for suggestion implementations.
*
* The theme hook used by this test has its base preprocess function in a
* separate file, so this test also ensures that that file is correctly loaded
* when needed.
*/
public function testPreprocessForSuggestions() {
// Test with both an unprimed and primed theme registry.
drupal_theme_rebuild();
for ($i = 0; $i < 2; $i++) {
$this->drupalGet('theme-test/suggestion');
$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.');
}
}
/**
* Tests the priority of some theme negotiators.
*/
public function testNegotiatorPriorities() {
$this->drupalGet('theme-test/priority');
// Ensure that the custom theme negotiator was not able to set the theme.
$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.');
}
/**
* Ensures that non-HTML requests never initialize themes.
*/
public function testThemeOnNonHtmlRequest() {
$this->drupalGet('theme-test/non-html');
$json = Json::decode($this->getRawContent());
$this->assertFalse($json['theme_initialized']);
}
/**
* Ensure page-front template suggestion is added when on front page.
*/
public function testFrontPageThemeSuggestion() {
// Set the current route to user.login because theme_get_suggestions() will
// query it to see if we are on the front page.
$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');
// Set it back to not annoy the batch runner.
\Drupal::requestStack()->pop();
$this->assertTrue(in_array('page__front', $suggestions), 'Front page template was suggested.');
}
/**
* Ensures a theme's .info.yml file is able to override a module CSS file from being added to the page.
*
* @see test_theme.info.yml
*/
public function testCSSOverride() {
// Reuse the same page as in testPreprocessForSuggestions(). We're testing
// what is output to the HTML HEAD based on what is in a theme's .info.yml
// file, so it doesn't matter what page we get, as long as it is themed with
// the test theme. First we test with CSS aggregation disabled.
$config = $this->config('system.performance');
$config->set('css.preprocess', 0);
$config->save();
$this->drupalGet('theme-test/suggestion');
$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.');
// Also test with aggregation enabled, simply ensuring no PHP errors are
// triggered during drupal_build_css_cache() when a source file doesn't
// exist. Then allow remaining tests to continue with aggregation disabled
// by default.
$config->set('css.preprocess', 1);
$config->save();
$this->drupalGet('theme-test/suggestion');
$config->set('css.preprocess', 0);
$config->save();
}
/**
* Ensures a themes template is overridable based on the 'template' filename.
*/
public function testTemplateOverride() {
$this->config('system.theme')
->set('default', 'test_theme')
->save();
$this->drupalGet('theme-test/template-test');
$this->assertText('Success: Template overridden.', 'Template overridden by defined \'template\' filename.');
}
/**
* Ensures a theme template can override a theme function.
*/
public function testFunctionOverride() {
$this->drupalGet('theme-test/function-template-overridden');
$this->assertText('Success: Template overrides theme function.', 'Theme function overridden by test_theme template.');
}
/**
* Tests that the page variable is not prematurely flattened.
*
* Some modules check the page array in template_preprocess_html(), so we
* ensure that it has not been rendered prematurely.
*/
public function testPreprocessHtml() {
$this->drupalGet('');
$attributes = $this->xpath('/body[@theme_test_page_variable="Page variable is an array."]');
$this->assertTrue(count($attributes) == 1, 'In template_preprocess_html(), the page variable is still an array (not rendered yet).');
$this->assertText('theme test page bottom markup', 'Modules are able to set the page bottom region.');
}
/**
* Tests that region attributes can be manipulated via preprocess functions.
*/
public function testRegionClass() {
\Drupal::service('module_installer')->install(['block', 'theme_region_test']);
// Place a block.
$this->drupalPlaceBlock('system_main_block');
$this->drupalGet('');
$elements = $this->cssSelect(".region-sidebar-first.new_class");
$this->assertEqual(count($elements), 1, 'New class found.');
}
/**
* Ensures suggestion preprocess functions run for default implementations.
*
* The theme hook used by this test has its base preprocess function in a
* separate file, so this test also ensures that that file is correctly loaded
* when needed.
*/
public function testSuggestionPreprocessForDefaults() {
$this->config('system.theme')->set('default', 'test_theme')->save();
// Test with both an unprimed and primed theme registry.
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->assertEqual((string) $value, $items[$key]->getText());
}
}
}
}
<?php
namespace Drupal\system\Tests\Theme;
namespace Drupal\Tests\system\Functional\Theme;
use Drupal\simpletest\WebTestBase;
use Drupal\Component\Utility\Html;
use Drupal\Tests\BrowserTestBase;
/**
* Tests for Twig debug markup.
*
* @group Theme
*/
class TwigDebugMarkupTest extends WebTestBase {
class TwigDebugMarkupTest extends BrowserTestBase {
/**
* Modules to enable.
......@@ -45,10 +46,9 @@ public function testTwigDebugMarkup() {
$build = node_view($node);
$output = $renderer->renderRoot($build);
$this->assertTrue(strpos($output, '<!-- THEME DEBUG -->') !== FALSE, 'Twig debug markup found in theme output when debug is enabled.');
$this->setRawContent($output);
$this->assertTrue(strpos($output, "THEME HOOK: 'node'") !== FALSE, 'Theme call information found.');
$this->assertTrue(strpos($output, '* node--1--full' . $extension . PHP_EOL . ' x node--1' . $extension . PHP_EOL . ' * node--page--full' . $extension . PHP_EOL . ' * node--page' . $extension . PHP_EOL . ' * node--full' . $extension . PHP_EOL . ' * node' . $extension) !== FALSE, 'Suggested template files found in order and node ID specific template shown as current template.');
$this->assertEscaped('node--<script type="text/javascript">alert(\'yo\');</script>');
$this->assertContains(Html::escape('node--<script type="text/javascript">alert(\'yo\');</script>'), (string) $output);
$template_filename = $templates['node__1']['path'] . '/' . $templates['node__1']['template'] . $extension;
$this->assertTrue(strpos($output, "BEGIN OUTPUT from '$template_filename'") !== FALSE, 'Full path to current template file found.');
......
<?php
namespace Drupal\system\Tests\Theme;
namespace Drupal\Tests\system\Functional\Theme;
use Drupal\Core\Language\LanguageInterface;
use Drupal\simpletest\WebTestBase;
use Drupal\Tests\BrowserTestBase;
/**
* Tests Twig "trans" tags.
*
* @group Theme
*/
class TwigTransTest extends WebTestBase {
class TwigTransTest extends BrowserTestBase {
/**
* Modules to enable.
......
<?php
namespace Drupal\system\Tests\Theme;
namespace Drupal\Tests\system\Kernel\Theme;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\Html;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Session\UserSession;
use Drupal\Core\Url;
use Drupal\simpletest\WebTestBase;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests for common theme functions.
*
* @group Theme
*/
class FunctionsTest extends WebTestBase {
class FunctionsTest extends KernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['router_test'];
public static $modules = ['router_test', 'system'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// Enable the Classy theme.
$this->container->get('theme_handler')->install(['classy']);
$this->config('system.theme')->set('default', 'classy')->save();
}
/**
* Tests item-list.html.twig.
......
<?php
namespace Drupal\system\Tests\Theme;
namespace Drupal\Tests\system\Kernel\Theme;
use Drupal\Component\Serialization\Json;
use Drupal\simpletest\WebTestBase;
use Drupal\test_theme\ThemeClass;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Component\Render\MarkupInterface;
use Drupal\test_theme\ThemeClass;
/**
* Tests low-level theme functions.
*
* @group Theme
*/
class ThemeTest extends WebTestBase {
class ThemeTest extends KernelTestBase {
/**
* Modules to enable.
*
* @var array
* {@inheritdoc}
*/
public static $modules = ['theme_test', 'node'];
public static $modules = ['theme_test', 'node', 'system'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
\Drupal::service('theme_handler')->install(['test_theme']);
......@@ -103,105 +100,6 @@ public function testThemeSuggestions() {
$this->assertEqual($suggestions, $result, 'Found expected page suggestions for paths containing hyphens.');
}
/**
* Ensures preprocess functions run even for suggestion implementations.
*
* The theme hook used by this test has its base preprocess function in a
* separate file, so this test also ensures that that file is correctly loaded
* when needed.
*/
public function testPreprocessForSuggestions() {
// Test with both an unprimed and primed theme registry.
drupal_theme_rebuild();
for ($i = 0; $i < 2; $i++) {
$this->drupalGet('theme-test/suggestion');
$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.');
}
}
/**
* Tests the priority of some theme negotiators.
*/
public function testNegotiatorPriorities() {
$this->drupalGet('theme-test/priority');
// Ensure that the custom theme negotiator was not able to set the theme.
$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.');
}
/**
* Ensures that non-HTML requests never initialize themes.
*/
public function testThemeOnNonHtmlRequest() {
$this->drupalGet('theme-test/non-html');
$json = Json::decode($this->getRawContent());
$this->assertFalse($json['theme_initialized']);
}
/**
* Ensure page-front template suggestion is added when on front page.
*/
public function testFrontPageThemeSuggestion() {
// Set the current route to user.login because theme_get_suggestions() will
// query it to see if we are on the front page.
$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');
// Set it back to not annoy the batch runner.
\Drupal::requestStack()->pop();
$this->assertTrue(in_array('page__front', $suggestions), 'Front page template was suggested.');
}
/**
* Ensures a theme's .info.yml file is able to override a module CSS file from being added to the page.
*
* @see test_theme.info.yml
*/
public function testCSSOverride() {
// Reuse the same page as in testPreprocessForSuggestions(). We're testing
// what is output to the HTML HEAD based on what is in a theme's .info.yml
// file, so it doesn't matter what page we get, as long as it is themed with
// the test theme. First we test with CSS aggregation disabled.
$config = $this->config('system.performance');
$config->set('css.preprocess', 0);
$config->save();
$this->drupalGet('theme-test/suggestion');
$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.');
// Also test with aggregation enabled, simply ensuring no PHP errors are
// triggered during drupal_build_css_cache() when a source file doesn't
// exist. Then allow remaining tests to continue with aggregation disabled
// by default.
$config->set('css.preprocess', 1);
$config->save();
$this->drupalGet('theme-test/suggestion');
$config->set('css.preprocess', 0);
$config->save();
}
/**
* Ensures a themes template is overridable based on the 'template' filename.
*/
public function testTemplateOverride() {
$this->config('system.theme')
->set('default', 'test_theme')
->save();
$this->drupalGet('theme-test/template-test');
$this->assertText('Success: Template overridden.', 'Template overridden by defined \'template\' filename.');
}
/**
* Ensures a theme template can override a theme function.
*/
public function testFunctionOverride() {
$this->drupalGet('theme-test/function-template-overridden');
$this->assertText('Success: Template overrides theme function.', 'Theme function overridden by test_theme template.');
}
/**
* Test the listInfo() function.
*/
......@@ -264,57 +162,4 @@ public function testFindThemeTemplates() {
$this->assertEqual($templates['node__1']['template'], 'node--1', 'Template node--1.tpl.twig was found in test_theme.');
}
/**
* Tests that the page variable is not prematurely flattened.
*
* Some modules check the page array in template_preprocess_html(), so we
* ensure that it has not been rendered prematurely.
*/
public function testPreprocessHtml() {
$this->drupalGet('');
$attributes = $this->xpath('/html/body[@theme_test_page_variable="Page variable is an array."]');
$this->assertTrue(count($attributes) == 1, 'In template_preprocess_html(), the page variable is still an array (not rendered yet).');
$this->assertText('theme test page bottom markup', 'Modules are able to set the page bottom region.');
}
/**
* Tests that region attributes can be manipulated via preprocess functions.
*/
public function testRegionClass() {
\Drupal::service('module_installer')->install(['block', 'theme_region_test']);
// Place a block.
$this->drupalPlaceBlock('system_main_block');
$this->drupalGet('');
$elements = $this->cssSelect(".region-sidebar-first.new_class");
$this->assertEqual(count($elements), 1, 'New class found.');
}
/**
* Ensures suggestion preprocess functions run for default implementations.
*
* The theme hook used by this test has its base preprocess function in a