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

/**
 * @file
5
 * Contains \Drupal\system\Tests\Theme\EngineTwigTest.
6 7 8 9
 */

namespace Drupal\system\Tests\Theme;

10
use Drupal\Core\Url;
11 12 13
use Drupal\simpletest\WebTestBase;

/**
14 15 16
 * Tests Twig-specific theme functionality.
 *
 * @group Theme
17
 */
18
class EngineTwigTest extends WebTestBase {
19 20 21 22 23 24

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

27
  protected function setUp() {
28
    parent::setUp();
29
    \Drupal::service('theme_handler')->install(array('test_theme'));
30
  }
31 32 33 34 35

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

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
  /**
   * 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')),
    );
    // Make sure we got something.
    $content = $this->drupalGetContent();
    $this->assertFalse(empty($content), 'Page content is not empty');
    foreach ($expected as $string) {
      $this->assertRaw('<div>' . $string . '</div>');
    }
  }

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

    $link_generator = $this->container->get('link_generator');

    $expected = [
76
      'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register')),
77 78 79 80 81 82 83 84 85
    ];

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

86
  /**
87
   * Tests the file_url Twig functions.
88 89 90 91 92 93 94
   */
  public function testTwigFileUrls() {
    $this->drupalGet('/twig-theme-test/file-url');
    $filepath = file_create_url('core/modules/system/tests/modules/twig_theme_test/twig_theme_test.js');
    $this->assertRaw('<div>file_url: ' . $filepath . '</div>');
  }

95
}