Ejemplo n.º 1
0
 /**
  * Ensures Twig template cache setting can be overridden.
  */
 function testTwigCacheOverride()
 {
     $extension = twig_extension();
     $theme_handler = $this->container->get('theme_handler');
     $theme_handler->install(array('test_theme'));
     $theme_handler->setDefault('test_theme');
     // The registry still works on theme globals, so set them here.
     \Drupal::theme()->setActiveTheme(\Drupal::service('theme.initialization')->getActiveThemeByName('test_theme'));
     // Reset the theme registry, so that the new theme is used.
     $this->container->set('theme.registry', NULL);
     // Load array of Twig templates.
     // reset() is necessary to invalidate caches tagged with 'theme_registry'.
     $registry = $this->container->get('theme.registry');
     $registry->reset();
     $templates = $registry->getRuntime();
     // Get the template filename and the cache filename for
     // theme_test.template_test.html.twig.
     $info = $templates->get('theme_test_template_test');
     $template_filename = $info['path'] . '/' . $info['template'] . $extension;
     $cache_filename = $this->container->get('twig')->getCacheFilename($template_filename);
     // Navigate to the page and make sure the template gets cached.
     $this->drupalGet('theme-test/template-test');
     $this->assertTrue(PhpStorageFactory::get('twig')->exists($cache_filename), 'Cached Twig template found.');
     // Disable the Twig cache and rebuild the service container.
     $parameters = $this->container->getParameter('twig.config');
     $parameters['cache'] = FALSE;
     $this->setContainerParameter('twig.config', $parameters);
     $this->rebuildContainer();
     // This should return false after rebuilding the service container.
     $this->assertFalse($this->container->get('twig')->getCache(), 'Twig environment has caching disabled.');
 }
Ejemplo n.º 2
0
 /**
  * Ensures Twig template cache setting can be overridden.
  */
 function testTwigCacheOverride()
 {
     $extension = twig_extension();
     $theme_handler = $this->container->get('theme_handler');
     $theme_handler->enable(array('test_theme'));
     $theme_handler->setDefault('test_theme');
     // The registry still works on theme globals, so set them here.
     $GLOBALS['theme'] = 'test_theme';
     $GLOBALS['theme_info'] = $theme_handler->listInfo()['test_theme'];
     // Reset the theme registry, so that the new theme is used.
     $this->container->set('theme.registry', NULL);
     // Load array of Twig templates.
     // reset() is necessary to invalidate caches tagged with 'theme_registry'.
     $registry = $this->container->get('theme.registry');
     $registry->reset();
     $templates = $registry->getRuntime();
     // Get the template filename and the cache filename for
     // theme_test.template_test.html.twig.
     $info = $templates->get('theme_test_template_test');
     $template_filename = $info['path'] . '/' . $info['template'] . $extension;
     $cache_filename = $this->container->get('twig')->getCacheFilename($template_filename);
     // Navigate to the page and make sure the template gets cached.
     $this->drupalGet('theme-test/template-test');
     $this->assertTrue(PhpStorageFactory::get('twig')->exists($cache_filename), 'Cached Twig template found.');
     // Disable the Twig cache and rebuild the service container.
     $this->settingsSet('twig_cache', FALSE);
     $this->rebuildContainer();
     // This should return false after rebuilding the service container.
     $new_cache_filename = $this->container->get('twig')->getCacheFilename($template_filename);
     $this->assertFalse($new_cache_filename, 'Twig environment does not return cache filename after caching is disabled.');
 }
Ejemplo n.º 3
0
 public function __construct(Twig_LoaderInterface $loader = NULL, $options = array())
 {
     $this->fileExtension = twig_extension();
     $options = array_merge(array('autorender' => TRUE), $options);
     // Auto render means, overrule default class
     if ($options['autorender']) {
         $this->autoRender = TRUE;
     }
     parent::__construct($loader, $options);
 }
Ejemplo n.º 4
0
 /**
  * Tests debug markup added to Twig template output.
  */
 function testTwigDebugMarkup()
 {
     /** @var \Drupal\Core\Render\RendererInterface $renderer */
     $renderer = $this->container->get('renderer');
     $extension = twig_extension();
     \Drupal::service('theme_handler')->install(array('test_theme'));
     \Drupal::service('theme_handler')->setDefault('test_theme');
     $this->drupalCreateContentType(array('type' => 'page'));
     // Enable debug, rebuild the service container, and clear all caches.
     $parameters = $this->container->getParameter('twig.config');
     $parameters['debug'] = TRUE;
     $this->setContainerParameter('twig.config', $parameters);
     $this->rebuildContainer();
     $this->resetAll();
     $cache = $this->container->get('theme.registry')->get();
     // Create array of Twig templates.
     $templates = drupal_find_theme_templates($cache, $extension, drupal_get_path('theme', 'test_theme'));
     $templates += drupal_find_theme_templates($cache, $extension, drupal_get_path('module', 'node'));
     // Create a node and test different features of the debug markup.
     $node = $this->drupalCreateNode();
     $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>');
     $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.');
     // Create another node and make sure the template suggestions shown in the
     // debug markup are correct.
     $node2 = $this->drupalCreateNode();
     $build = node_view($node2);
     $output = $renderer->renderRoot($build);
     $this->assertTrue(strpos($output, '* node--2--full' . $extension . PHP_EOL . '   * node--2' . $extension . PHP_EOL . '   * node--page--full' . $extension . PHP_EOL . '   * node--page' . $extension . PHP_EOL . '   * node--full' . $extension . PHP_EOL . '   x node' . $extension) !== FALSE, 'Suggested template files found in order and base template shown as current template.');
     // Create another node and make sure the template suggestions shown in the
     // debug markup are correct.
     $node3 = $this->drupalCreateNode();
     $build = array('#theme' => 'node__foo__bar');
     $build += node_view($node3);
     $output = $renderer->renderRoot($build);
     $this->assertTrue(strpos($output, "THEME HOOK: 'node__foo__bar'") !== FALSE, 'Theme call information found.');
     $this->assertTrue(strpos($output, '* node--foo--bar' . $extension . PHP_EOL . '   * node--foo' . $extension . PHP_EOL . '   * node--&lt;script type=&quot;text/javascript&quot;&gt;alert(&#039;yo&#039;);&lt;/script&gt;' . $extension . PHP_EOL . '   * node--3--full' . $extension . PHP_EOL . '   * node--3' . $extension . PHP_EOL . '   * node--page--full' . $extension . PHP_EOL . '   * node--page' . $extension . PHP_EOL . '   * node--full' . $extension . PHP_EOL . '   x node' . $extension) !== FALSE, 'Suggested template files found in order and base template shown as current template.');
     // Disable debug, rebuild the service container, and clear all caches.
     $parameters = $this->container->getParameter('twig.config');
     $parameters['debug'] = FALSE;
     $this->setContainerParameter('twig.config', $parameters);
     $this->rebuildContainer();
     $this->resetAll();
     $build = node_view($node);
     $output = $renderer->renderRoot($build);
     $this->assertFalse(strpos($output, '<!-- THEME DEBUG -->') !== FALSE, 'Twig debug markup not found in theme output when debug is disabled.');
 }
Ejemplo n.º 5
0
 /**
  * Tests debug markup added to Twig template output.
  */
 function testTwigDebugMarkup()
 {
     $extension = twig_extension();
     theme_enable(array('test_theme'));
     \Drupal::config('system.theme')->set('default', 'test_theme')->save();
     // Enable debug, rebuild the service container, and clear all caches.
     $this->settingsSet('twig_debug', TRUE);
     $this->rebuildContainer();
     $this->resetAll();
     $cache = $this->container->get('theme.registry')->get();
     // Create array of Twig templates.
     $templates = drupal_find_theme_templates($cache, $extension, drupal_get_path('theme', 'test_theme'));
     $templates += drupal_find_theme_templates($cache, $extension, drupal_get_path('module', 'node'));
     // Create a node and test different features of the debug markup.
     $node = $this->drupalCreateNode();
     $build = node_view($node);
     $output = drupal_render($build);
     $this->assertTrue(strpos($output, '<!-- THEME DEBUG -->') !== FALSE, 'Twig debug markup found in theme output when debug is enabled.');
     $this->assertTrue(strpos($output, "CALL: _theme('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.');
     $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.');
     // Create another node and make sure the template suggestions shown in the
     // debug markup are correct.
     $node2 = $this->drupalCreateNode();
     $build = node_view($node2);
     $output = drupal_render($build);
     $this->assertTrue(strpos($output, '* node--2--full' . $extension . PHP_EOL . '   * node--2' . $extension . PHP_EOL . '   * node--page--full' . $extension . PHP_EOL . '   * node--page' . $extension . PHP_EOL . '   * node--full' . $extension . PHP_EOL . '   x node' . $extension) !== FALSE, 'Suggested template files found in order and base template shown as current template.');
     // Create another node and make sure the template suggestions shown in the
     // debug markup are correct.
     $node3 = $this->drupalCreateNode();
     $build = array('#theme' => 'node__foo__bar');
     $build += node_view($node3);
     $output = drupal_render($build);
     $this->assertTrue(strpos($output, "CALL: _theme('node__foo__bar')") !== FALSE, 'Theme call information found.');
     $this->assertTrue(strpos($output, '* node--foo--bar' . $extension . PHP_EOL . '   * node--foo' . $extension . PHP_EOL . '   * node--3--full' . $extension . PHP_EOL . '   * node--3' . $extension . PHP_EOL . '   * node--page--full' . $extension . PHP_EOL . '   * node--page' . $extension . PHP_EOL . '   * node--full' . $extension . PHP_EOL . '   x node' . $extension) !== FALSE, 'Suggested template files found in order and base template shown as current template.');
     // Disable debug, rebuild the service container, and clear all caches.
     $this->settingsSet('twig_debug', FALSE);
     $this->rebuildContainer();
     $this->resetAll();
     $build = node_view($node);
     $output = drupal_render($build);
     $this->assertFalse(strpos($output, '<!-- THEME DEBUG -->') !== FALSE, 'Twig debug markup not found in theme output when debug is disabled.');
 }