function FrontMatterTest::testFrontMatter

Same name in other branches
  1. 10 core/tests/Drupal/KernelTests/Core/Theme/FrontMatterTest.php \Drupal\KernelTests\Core\Theme\FrontMatterTest::testFrontMatter()
  2. 11.x core/tests/Drupal/KernelTests/Core/Theme/FrontMatterTest.php \Drupal\KernelTests\Core\Theme\FrontMatterTest::testFrontMatter()

Test Twig template front matter.

@covers \Drupal\Core\Template\TwigEnvironment::compileSource @covers \Drupal\Core\Template\TwigEnvironment::getTemplateMetadata

@dataProvider \Drupal\Tests\Component\FrontMatter\FrontMatterTest::providerFrontMatterData

Parameters

array|null $yaml: The YAML used for metadata in a Twig template.

int $line: The expected line number where the source code starts.

string $content: The content to use for testing purposes.

File

core/tests/Drupal/KernelTests/Core/Theme/FrontMatterTest.php, line 97

Class

FrontMatterTest
Tests Twig front matter support.

Namespace

Drupal\KernelTests\Core\Theme

Code

public function testFrontMatter($yaml, $line, $content = ComponentFrontMatterTest::SOURCE) {
    // Create a temporary Twig template.
    $source = ComponentFrontMatterTest::createFrontMatterSource($yaml, $content);
    $file = $this->createTwigTemplate($source);
    $name = basename($file);
    // Ensure the proper metadata is returned.
    $metadata = $this->twig
        ->getTemplateMetadata($name);
    $this->assertEquals($yaml === NULL ? [] : $yaml, $metadata);
    // Ensure the metadata is never rendered.
    $output = $this->twig
        ->load($name)
        ->render();
    $this->assertEquals($content, $output);
    // Create a temporary Twig template.
    $source = ComponentFrontMatterTest::createFrontMatterSource($yaml, static::BROKEN_SOURCE);
    $file = $this->createTwigTemplate($source);
    $name = basename($file);
    try {
        $this->twig
            ->load($name);
    } catch (Error $error) {
        $this->assertEquals($line, $error->getTemplateLine());
    }
    // Ensure string based templates work too.
    try {
        $this->twig
            ->createTemplate($source)
            ->render();
    } catch (Error $error) {
        $this->assertEquals($line, $error->getTemplateLine());
    }
}

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