EngineTwigTest.php 5.89 KB
Newer Older
1
2
3
4
<?php

namespace Drupal\system\Tests\Theme;

5
use Drupal\Core\Render\Markup;
6
use Drupal\Core\Url;
7
8
9
use Drupal\simpletest\WebTestBase;

/**
10
11
12
 * Tests Twig-specific theme functionality.
 *
 * @group Theme
13
 */
14
class EngineTwigTest extends WebTestBase {
15
16
17
18
19
20

  /**
   * Modules to enable.
   *
   * @var array
   */
21
  public static $modules = array('theme_test', 'twig_theme_test');
22

23
  protected function setUp() {
24
    parent::setUp();
25
    \Drupal::service('theme_handler')->install(array('test_theme'));
26
  }
27
28
29
30
31

  /**
   * Tests that the Twig engine handles PHP data correctly.
   */
  function testTwigVariableDataTypes() {
32
    $this->config('system.theme')
33
      ->set('default', 'test_theme')
34
35
36
37
38
39
40
      ->save();
    $this->drupalGet('twig-theme-test/php-variables');
    foreach (_test_theme_twig_php_values() as $type => $value) {
      $this->assertRaw('<li>' . $type . ': ' . $value['expected'] . '</li>');
    }
  }

41
42
43
44
45
46
47
48
49
50
51
52
53
54
  /**
   * Tests the url and url_generate Twig functions.
   */
  public function testTwigUrlGenerator() {
    $this->drupalGet('twig-theme-test/url-generator');
    // Find the absolute URL of the current site.
    $url_generator = $this->container->get('url_generator');
    $expected = array(
      'path (as route) not absolute: ' . $url_generator->generateFromRoute('user.register'),
      'url (as route) absolute: ' . $url_generator->generateFromRoute('user.register', array(), array('absolute' => TRUE)),
      'path (as route) not absolute with fragment: ' . $url_generator->generateFromRoute('user.register', array(), array('fragment' => 'bottom')),
      'url (as route) absolute despite option: ' . $url_generator->generateFromRoute('user.register', array(), array('absolute' => TRUE)),
      'url (as route) absolute with fragment: ' . $url_generator->generateFromRoute('user.register', array(), array('absolute' => TRUE, 'fragment' => 'bottom')),
    );
55
56
57
58
59
60
61

    // Verify that url() has the ability to bubble cacheability metadata:
    // absolute URLs should bubble the 'url.site' cache context. (This only
    // needs to test that cacheability metadata is bubbled *at all*; detailed
    // tests for *which* cacheability metadata is bubbled live elsewhere.)
    $this->assertCacheContext('url.site');

62
    // Make sure we got something.
63
    $content = $this->getRawContent();
64
65
66
67
68
69
    $this->assertFalse(empty($content), 'Page content is not empty');
    foreach ($expected as $string) {
      $this->assertRaw('<div>' . $string . '</div>');
    }
  }

70
  /**
71
   * Tests the link_generator Twig functions.
72
73
74
75
   */
  public function testTwigLinkGenerator() {
    $this->drupalGet('twig-theme-test/link-generator');

76
    /** @var \Drupal\Core\Utility\LinkGenerator $link_generator */
77
78
    $link_generator = $this->container->get('link_generator');

79

80
    $generated_url = Url::fromRoute('user.register', [], ['absolute' => TRUE])->toString(TRUE)->getGeneratedUrl();
81
    $expected = [
82
      'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['absolute' => TRUE])),
83
84
85
      'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['absolute' => TRUE, 'attributes' => ['foo' => 'bar']])),
      'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['attributes' => ['foo' => 'bar', 'id' => 'kitten']])),
      'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['attributes' => ['id' => 'kitten']])),
86
      'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['attributes' => ['class' => ['llama', 'kitten', 'panda']]])),
87
      'link via the linkgenerator: ' . $link_generator->generate(Markup::create('<span>register</span>'), new Url('user.register', [], ['absolute' => TRUE])),
88
      'link via the linkgenerator: <a href="' . $generated_url . '"><span>register</span><svg></svg></a>',
89
      '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']])),
90
91
    ];

92
93
94
95
96
97
    // Verify that link() has the ability to bubble cacheability metadata:
    // absolute URLs should bubble the 'url.site' cache context. (This only
    // needs to test that cacheability metadata is bubbled *at all*; detailed
    // tests for *which* cacheability metadata is bubbled live elsewhere.)
    $this->assertCacheContext('url.site');

98
99
100
101
102
103
104
    $content = $this->getRawContent();
    $this->assertFalse(empty($content), 'Page content is not empty');
    foreach ($expected as $string) {
      $this->assertRaw('<div>' . $string . '</div>');
    }
  }

105
  /**
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
   * Tests the magic url to string Twig functions.
   *
   * @see \Drupal\Core\Url
   */
  public function testTwigUrlToString() {
    $this->drupalGet('twig-theme-test/url-to-string');

    $expected = [
      'rendered url: ' . Url::fromRoute('user.register')->toString(),
    ];

    $content = $this->getRawContent();
    $this->assertFalse(empty($content), 'Page content is not empty');
    foreach ($expected as $string) {
      $this->assertRaw('<div>' . $string . '</div>');
    }
  }

  /**
   * Tests the automatic/magic calling of toString() on objects, if exists.
126
127
128
   */
  public function testTwigFileUrls() {
    $this->drupalGet('/twig-theme-test/file-url');
129
    $filepath = file_url_transform_relative(file_create_url('core/modules/system/tests/modules/twig_theme_test/twig_theme_test.js'));
130
131
132
    $this->assertRaw('<div>file_url: ' . $filepath . '</div>');
  }

133
134
135
136
137
138
139
140
  /**
   * Tests the attach of asset libraries.
   */
  public function testTwigAttachLibrary() {
    $this->drupalGet('/twig-theme-test/attach-library');
    $this->assertRaw('ckeditor.js');
  }

141
142
143
144
145
146
147
148
  /**
   * Tests the rendering of renderables.
   */
  public function testRenderable() {
    $this->drupalGet('/twig-theme-test/renderable');
    $this->assertRaw('<div>Example markup</div>');
  }

149
}