annotate core/modules/system/src/Tests/Theme/EngineTwigTest.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 7a779792577d
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\system\Tests\Theme;
Chris@0 4
Chris@0 5 use Drupal\Core\Render\Markup;
Chris@0 6 use Drupal\Core\Url;
Chris@0 7 use Drupal\simpletest\WebTestBase;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * Tests Twig-specific theme functionality.
Chris@0 11 *
Chris@0 12 * @group Theme
Chris@0 13 */
Chris@0 14 class EngineTwigTest extends WebTestBase {
Chris@0 15
Chris@0 16 /**
Chris@0 17 * Modules to enable.
Chris@0 18 *
Chris@0 19 * @var array
Chris@0 20 */
Chris@0 21 public static $modules = ['theme_test', 'twig_theme_test'];
Chris@0 22
Chris@0 23 protected function setUp() {
Chris@0 24 parent::setUp();
Chris@0 25 \Drupal::service('theme_handler')->install(['test_theme']);
Chris@0 26 }
Chris@0 27
Chris@0 28 /**
Chris@0 29 * Tests that the Twig engine handles PHP data correctly.
Chris@0 30 */
Chris@0 31 public function testTwigVariableDataTypes() {
Chris@0 32 $this->config('system.theme')
Chris@0 33 ->set('default', 'test_theme')
Chris@0 34 ->save();
Chris@0 35 $this->drupalGet('twig-theme-test/php-variables');
Chris@0 36 foreach (_test_theme_twig_php_values() as $type => $value) {
Chris@0 37 $this->assertRaw('<li>' . $type . ': ' . $value['expected'] . '</li>');
Chris@0 38 }
Chris@0 39 }
Chris@0 40
Chris@0 41 /**
Chris@0 42 * Tests the url and url_generate Twig functions.
Chris@0 43 */
Chris@0 44 public function testTwigUrlGenerator() {
Chris@0 45 $this->drupalGet('twig-theme-test/url-generator');
Chris@0 46 // Find the absolute URL of the current site.
Chris@0 47 $url_generator = $this->container->get('url_generator');
Chris@0 48 $expected = [
Chris@0 49 'path (as route) not absolute: ' . $url_generator->generateFromRoute('user.register'),
Chris@0 50 'url (as route) absolute: ' . $url_generator->generateFromRoute('user.register', [], ['absolute' => TRUE]),
Chris@0 51 'path (as route) not absolute with fragment: ' . $url_generator->generateFromRoute('user.register', [], ['fragment' => 'bottom']),
Chris@0 52 'url (as route) absolute despite option: ' . $url_generator->generateFromRoute('user.register', [], ['absolute' => TRUE]),
Chris@0 53 'url (as route) absolute with fragment: ' . $url_generator->generateFromRoute('user.register', [], ['absolute' => TRUE, 'fragment' => 'bottom']),
Chris@0 54 ];
Chris@0 55
Chris@0 56 // Verify that url() has the ability to bubble cacheability metadata:
Chris@0 57 // absolute URLs should bubble the 'url.site' cache context. (This only
Chris@0 58 // needs to test that cacheability metadata is bubbled *at all*; detailed
Chris@0 59 // tests for *which* cacheability metadata is bubbled live elsewhere.)
Chris@0 60 $this->assertCacheContext('url.site');
Chris@0 61
Chris@0 62 // Make sure we got something.
Chris@0 63 $content = $this->getRawContent();
Chris@0 64 $this->assertFalse(empty($content), 'Page content is not empty');
Chris@0 65 foreach ($expected as $string) {
Chris@0 66 $this->assertRaw('<div>' . $string . '</div>');
Chris@0 67 }
Chris@0 68 }
Chris@0 69
Chris@0 70 /**
Chris@0 71 * Tests the link_generator Twig functions.
Chris@0 72 */
Chris@0 73 public function testTwigLinkGenerator() {
Chris@0 74 $this->drupalGet('twig-theme-test/link-generator');
Chris@0 75
Chris@0 76 /** @var \Drupal\Core\Utility\LinkGenerator $link_generator */
Chris@0 77 $link_generator = $this->container->get('link_generator');
Chris@0 78
Chris@0 79 $generated_url = Url::fromRoute('user.register', [], ['absolute' => TRUE])->toString(TRUE)->getGeneratedUrl();
Chris@0 80 $expected = [
Chris@0 81 'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['absolute' => TRUE])),
Chris@0 82 'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['absolute' => TRUE, 'attributes' => ['foo' => 'bar']])),
Chris@0 83 'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['attributes' => ['foo' => 'bar', 'id' => 'kitten']])),
Chris@0 84 'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['attributes' => ['id' => 'kitten']])),
Chris@0 85 'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['attributes' => ['class' => ['llama', 'kitten', 'panda']]])),
Chris@0 86 'link via the linkgenerator: ' . $link_generator->generate(Markup::create('<span>register</span>'), new Url('user.register', [], ['absolute' => TRUE])),
Chris@0 87 'link via the linkgenerator: <a href="' . $generated_url . '"><span>register</span><svg></svg></a>',
Chris@0 88 'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['attributes' => ['foo' => 'bar']])) . ' ' . $link_generator->generate('register', new Url('user.register', [], ['attributes' => ['foo' => 'bar']])),
Chris@0 89 ];
Chris@0 90
Chris@0 91 // Verify that link() has the ability to bubble cacheability metadata:
Chris@0 92 // absolute URLs should bubble the 'url.site' cache context. (This only
Chris@0 93 // needs to test that cacheability metadata is bubbled *at all*; detailed
Chris@0 94 // tests for *which* cacheability metadata is bubbled live elsewhere.)
Chris@0 95 $this->assertCacheContext('url.site');
Chris@0 96
Chris@0 97 $content = $this->getRawContent();
Chris@0 98 $this->assertFalse(empty($content), 'Page content is not empty');
Chris@0 99 foreach ($expected as $string) {
Chris@0 100 $this->assertRaw('<div>' . $string . '</div>');
Chris@0 101 }
Chris@0 102 }
Chris@0 103
Chris@0 104 /**
Chris@0 105 * Tests the magic url to string Twig functions.
Chris@0 106 *
Chris@0 107 * @see \Drupal\Core\Url
Chris@0 108 */
Chris@0 109 public function testTwigUrlToString() {
Chris@0 110 $this->drupalGet('twig-theme-test/url-to-string');
Chris@0 111
Chris@0 112 $expected = [
Chris@0 113 'rendered url: ' . Url::fromRoute('user.register')->toString(),
Chris@0 114 ];
Chris@0 115
Chris@0 116 $content = $this->getRawContent();
Chris@0 117 $this->assertFalse(empty($content), 'Page content is not empty');
Chris@0 118 foreach ($expected as $string) {
Chris@0 119 $this->assertRaw('<div>' . $string . '</div>');
Chris@0 120 }
Chris@0 121 }
Chris@0 122
Chris@0 123 /**
Chris@0 124 * Tests the automatic/magic calling of toString() on objects, if exists.
Chris@0 125 */
Chris@0 126 public function testTwigFileUrls() {
Chris@0 127 $this->drupalGet('/twig-theme-test/file-url');
Chris@0 128 $filepath = file_url_transform_relative(file_create_url('core/modules/system/tests/modules/twig_theme_test/twig_theme_test.js'));
Chris@0 129 $this->assertRaw('<div>file_url: ' . $filepath . '</div>');
Chris@0 130 }
Chris@0 131
Chris@0 132 /**
Chris@0 133 * Tests the attach of asset libraries.
Chris@0 134 */
Chris@0 135 public function testTwigAttachLibrary() {
Chris@0 136 $this->drupalGet('/twig-theme-test/attach-library');
Chris@0 137 $this->assertRaw('ckeditor.js');
Chris@0 138 }
Chris@0 139
Chris@0 140 /**
Chris@0 141 * Tests the rendering of renderables.
Chris@0 142 */
Chris@0 143 public function testRenderable() {
Chris@0 144 $this->drupalGet('/twig-theme-test/renderable');
Chris@0 145 $this->assertRaw('<div>Example markup</div>');
Chris@0 146 }
Chris@0 147
Chris@0 148 }