TwigExtensionTest.php

Same filename in this branch
  1. 8.9.x core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php
Same filename in other branches
  1. 9 core/modules/system/tests/src/Functional/Theme/TwigExtensionTest.php
  2. 9 core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php
  3. 10 core/modules/system/tests/src/Functional/Theme/TwigExtensionTest.php
  4. 10 core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php
  5. 11.x core/modules/system/tests/src/Functional/Theme/TwigExtensionTest.php
  6. 11.x core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php

Namespace

Drupal\Tests\system\Functional\Theme

File

core/modules/system/tests/src/Functional/Theme/TwigExtensionTest.php

View source
<?php

namespace Drupal\Tests\system\Functional\Theme;

use Drupal\Tests\BrowserTestBase;
use Drupal\twig_extension_test\TwigExtension\TestExtension;

/**
 * Tests Twig extensions.
 *
 * @group Theme
 */
class TwigExtensionTest extends BrowserTestBase {
    
    /**
     * Modules to enable.
     *
     * @var array
     */
    public static $modules = [
        'theme_test',
        'twig_extension_test',
    ];
    
    /**
     * {@inheritdoc}
     */
    protected $defaultTheme = 'stark';
    protected function setUp() {
        parent::setUp();
        \Drupal::service('theme_installer')->install([
            'test_theme',
        ]);
    }
    
    /**
     * Tests that the provided Twig extension loads the service appropriately.
     */
    public function testTwigExtensionLoaded() {
        $twigService = \Drupal::service('twig');
        $ext = $twigService->getExtension(TestExtension::class);
        $this->assertInstanceOf(TestExtension::class, $ext);
    }
    
    /**
     * Tests that the Twig extension's filter produces expected output.
     */
    public function testTwigExtensionFilter() {
        $this->config('system.theme')
            ->set('default', 'test_theme')
            ->save();
        $this->drupalGet('twig-extension-test/filter');
        $this->assertText('Every plant is not a mineral.', 'Success: String filtered.');
        // Test safe_join filter.
        $this->assertRaw('&lt;em&gt;will be escaped&lt;/em&gt;<br/><em>will be markup</em><br/><strong>will be rendered</strong>');
    }
    
    /**
     * Tests that the Twig extension's function produces expected output.
     */
    public function testTwigExtensionFunction() {
        $this->config('system.theme')
            ->set('default', 'test_theme')
            ->save();
        $this->drupalGet('twig-extension-test/function');
        $this->assertText('THE QUICK BROWN BOX JUMPS OVER THE LAZY DOG 123.', 'Success: Text converted to uppercase.');
        $this->assertText('the quick brown box jumps over the lazy dog 123.', 'Success: Text converted to lowercase.');
        $this->assertNoText('The Quick Brown Fox Jumps Over The Lazy Dog 123.', 'Success: No text left behind.');
    }
    
    /**
     * Tests output of integer and double 0 values of TwigExtension::escapeFilter().
     *
     * @see https://www.drupal.org/node/2417733
     */
    public function testsRenderEscapedZeroValue() {
        
        /** @var \Drupal\Core\Template\TwigExtension $extension */
        $extension = \Drupal::service('twig.extension');
        
        /** @var \Drupal\Core\Template\TwigEnvironment $twig */
        $twig = \Drupal::service('twig');
        $this->assertIdentical($extension->escapeFilter($twig, 0), 0, 'TwigExtension::escapeFilter() returns zero correctly when provided as an integer.');
        $this->assertIdentical($extension->escapeFilter($twig, 0.0), 0, 'TwigExtension::escapeFilter() returns zero correctly when provided as a double.');
    }
    
    /**
     * Tests output of integer and double 0 values of TwigExtension->renderVar().
     *
     * @see https://www.drupal.org/node/2417733
     */
    public function testsRenderZeroValue() {
        
        /** @var \Drupal\Core\Template\TwigExtension $extension */
        $extension = \Drupal::service('twig.extension');
        $this->assertIdentical($extension->renderVar(0), 0, 'TwigExtension::renderVar() renders zero correctly when provided as an integer.');
        $this->assertIdentical($extension->renderVar(0.0), 0, 'TwigExtension::renderVar() renders zero correctly when provided as a double.');
    }

}

Classes

Title Deprecated Summary
TwigExtensionTest Tests Twig extensions.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.