function FilterAPITest::testProcessedTextElement

Same name and namespace in other branches
  1. 9 core/modules/filter/tests/src/Kernel/FilterAPITest.php \Drupal\Tests\filter\Kernel\FilterAPITest::testProcessedTextElement()
  2. 10 core/modules/filter/tests/src/Kernel/FilterAPITest.php \Drupal\Tests\filter\Kernel\FilterAPITest::testProcessedTextElement()
  3. 11.x core/modules/filter/tests/src/Kernel/FilterAPITest.php \Drupal\Tests\filter\Kernel\FilterAPITest::testProcessedTextElement()

Tests the 'processed_text' element.

Function check_markup() is a wrapper for the 'processed_text' element, for use in simple scenarios; the 'processed_text' element has more advanced features: it lets filters attach assets, associate cache tags and define #lazy_builder callbacks. This test focuses solely on those advanced features.

File

core/modules/filter/tests/src/Kernel/FilterAPITest.php, line 256

Class

FilterAPITest
Tests the behavior of the API of the Filter module.

Namespace

Drupal\Tests\filter\Kernel

Code

public function testProcessedTextElement() {
    FilterFormat::create([
        'format' => 'element_test',
        'name' => 'processed_text element test format',
        'filters' => [
            'filter_test_assets' => [
                'weight' => -1,
                'status' => TRUE,
            ],
            'filter_test_cache_tags' => [
                'weight' => 0,
                'status' => TRUE,
            ],
            'filter_test_cache_contexts' => [
                'weight' => 0,
                'status' => TRUE,
            ],
            'filter_test_cache_merge' => [
                'weight' => 0,
                'status' => TRUE,
            ],
            'filter_test_placeholders' => [
                'weight' => 1,
                'status' => TRUE,
            ],
            // Run the HTML corrector filter last, because it has the potential to
            // break the placeholders added by the filter_test_placeholders filter.
'filter_htmlcorrector' => [
                'weight' => 10,
                'status' => TRUE,
            ],
        ],
    ])->save();
    $build = [
        '#type' => 'processed_text',
        '#text' => '<p>Hello, world!</p>',
        '#format' => 'element_test',
    ];
    \Drupal::service('renderer')->renderRoot($build);
    // Verify the attachments and cacheability metadata.
    $expected_attachments = [
        // The assets attached by the filter_test_assets filter.
'library' => [
            'filter/caption',
        ],
        // The placeholders attached that still need to be processed.
'placeholders' => [],
    ];
    $this->assertEqual($expected_attachments, $build['#attached'], 'Expected attachments present');
    $expected_cache_tags = [
        // The cache tag set by the processed_text element itself.
'config:filter.format.element_test',
        // The cache tags set by the filter_test_cache_tags filter.
'foo:bar',
        'foo:baz',
        // The cache tags set by the filter_test_cache_merge filter.
'merge:tag',
    ];
    $this->assertEqual($expected_cache_tags, $build['#cache']['tags'], 'Expected cache tags present.');
    $expected_cache_contexts = [
        // The cache context set by the filter_test_cache_contexts filter.
'languages:' . LanguageInterface::TYPE_CONTENT,
        // The default cache contexts for Renderer.
'languages:' . LanguageInterface::TYPE_INTERFACE,
        'theme',
        // The cache tags set by the filter_test_cache_merge filter.
'user.permissions',
    ];
    $this->assertEqual($expected_cache_contexts, $build['#cache']['contexts'], 'Expected cache contexts present.');
    $expected_markup = '<p>Hello, world!</p><p>This is a dynamic llama.</p>';
    $this->assertEqual($expected_markup, $build['#markup'], 'Expected #lazy_builder callback has been applied.');
}

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