You are here

function FilterAPITest::testProcessedTextElement in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/filter/src/Tests/FilterAPITest.php \Drupal\filter\Tests\FilterAPITest::testProcessedTextElement()

Tests the 'processed_text' element.

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/src/Tests/FilterAPITest.php, line 256
Contains \Drupal\filter\Tests\FilterAPITest.

Class

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

Namespace

Drupal\filter\Tests

Code

function testProcessedTextElement() {
  entity_create('filter_format', array(
    'format' => 'element_test',
    'name' => 'processed_text element test format',
    'filters' => array(
      'filter_test_assets' => array(
        'weight' => -1,
        'status' => TRUE,
      ),
      'filter_test_cache_tags' => array(
        'weight' => 0,
        'status' => TRUE,
      ),
      'filter_test_cache_contexts' => array(
        'weight' => 0,
        'status' => TRUE,
      ),
      'filter_test_cache_merge' => array(
        'weight' => 0,
        'status' => TRUE,
      ),
      'filter_test_placeholders' => array(
        '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' => array(
        'weight' => 10,
        'status' => TRUE,
      ),
    ),
  ))
    ->save();
  $build = array(
    '#type' => 'processed_text',
    '#text' => '<p>Hello, world!</p>',
    '#format' => 'element_test',
  );
  drupal_render_root($build);

  // Verify the attachments and cacheability metadata.
  $expected_attachments = array(
    // The assets attached by the filter_test_assets filter.
    'library' => array(
      'filter/caption',
    ),
    // The placeholders attached that still need to be processed.
    'placeholders' => [],
  );
  $this
    ->assertEqual($expected_attachments, $build['#attached'], 'Expected attachments present');
  $expected_cache_tags = array(
    // 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.');
}