Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / system / tests / src / Functional / Theme / TwigRegistryLoaderTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Theme;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8  * Tests Twig registry loader.
9  *
10  * @group Theme
11  */
12 class TwigRegistryLoaderTest extends BrowserTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['twig_theme_test', 'block'];
20
21   /**
22    * @var \Drupal\Core\Template\TwigEnvironment
23    */
24   protected $twig;
25
26   protected function setUp() {
27     parent::setUp();
28     \Drupal::service('theme_handler')->install(['test_theme_twig_registry_loader', 'test_theme_twig_registry_loader_theme', 'test_theme_twig_registry_loader_subtheme']);
29     $this->twig = \Drupal::service('twig');
30   }
31
32   /**
33    * Checks to see if a value is a Twig template.
34    */
35   public function assertTwigTemplate($value, $message = '', $group = 'Other') {
36     $this->assertTrue($value instanceof \Twig_Template, $message, $group);
37   }
38
39   /**
40    * Tests template discovery using the Drupal theme registry.
41    */
42   public function testTemplateDiscovery() {
43     $this->assertTwigTemplate($this->twig->resolveTemplate('block.html.twig'), 'Found block.html.twig in block module.');
44   }
45
46   /**
47    * Tests template extension and includes using the Drupal theme registry.
48    */
49   public function testTwigNamespaces() {
50     // Test the module-provided extend and insert templates.
51     $this->drupalGet('twig-theme-test/registry-loader');
52     $this->assertText('This line is from twig_theme_test/templates/twig-registry-loader-test-extend.html.twig');
53     $this->assertText('This line is from twig_theme_test/templates/twig-registry-loader-test-include.html.twig');
54
55     // Enable a theme that overrides the extend and insert templates to ensure
56     // they are picked up by the registry loader.
57     $this->config('system.theme')
58       ->set('default', 'test_theme_twig_registry_loader')
59       ->save();
60     $this->drupalGet('twig-theme-test/registry-loader');
61     $this->assertText('This line is from test_theme_twig_registry_loader/templates/twig-registry-loader-test-extend.html.twig');
62     $this->assertText('This line is from test_theme_twig_registry_loader/templates/twig-registry-loader-test-include.html.twig');
63
64     // Enable overriding theme that overrides the extend and insert templates
65     // from the base theme.
66     $this->config('system.theme')
67       ->set('default', 'test_theme_twig_registry_loader_theme')
68       ->save();
69     $this->drupalGet('twig-theme-test/registry-loader');
70     $this->assertText('This line is from test_theme_twig_registry_loader_theme/templates/twig-registry-loader-test-extend.html.twig');
71     $this->assertText('This line is from test_theme_twig_registry_loader_theme/templates/twig-registry-loader-test-include.html.twig');
72
73     // Enable a subtheme for the theme that doesn't have any overrides to make
74     // sure that templates are being loaded from the first parent which has the
75     // templates.
76     $this->config('system.theme')
77       ->set('default', 'test_theme_twig_registry_loader_subtheme')
78       ->save();
79     $this->drupalGet('twig-theme-test/registry-loader');
80     $this->assertText('This line is from test_theme_twig_registry_loader_theme/templates/twig-registry-loader-test-extend.html.twig');
81     $this->assertText('This line is from test_theme_twig_registry_loader_theme/templates/twig-registry-loader-test-include.html.twig');
82   }
83
84 }