You are here

public function TextimageFieldFormatterTest::testTextimageCaching in Textimage 8.3

Same name and namespace in other branches
  1. 8.4 tests/src/Functional/TextimageFieldFormatterTest.php \Drupal\Tests\textimage\Functional\TextimageFieldFormatterTest::testTextimageCaching()

Test Textimage caching.

File

tests/src/Functional/TextimageFieldFormatterTest.php, line 313

Class

TextimageFieldFormatterTest
Test Textimage formatters on node display.

Namespace

Drupal\Tests\textimage\Functional

Code

public function testTextimageCaching() {

  // Create a text field for Textimage test.
  $field_name = 'test_caching';
  $this
    ->createTextField($field_name, 'article');

  // Create a new node.
  $field_value = 'test for caching';
  $nid = $this
    ->createTextimageNode('text', $field_name, $field_value, 'article', 'test');
  $node = Node::load($nid);

  // Set textimage formatter - no link.
  $display = $this->entityDisplayRepository
    ->getViewDisplay('node', $node
    ->getType(), 'default');
  $display_options['type'] = 'textimage_text_field_formatter';
  $display_options['settings']['image_style'] = 'textimage_test';
  $display_options['settings']['image_link'] = '';
  $display_options['settings']['image_build_deferred'] = FALSE;
  $display
    ->setComponent($field_name, $display_options)
    ->save();
  $this
    ->drupalGet('node/' . $nid);

  // From previous get, Textimage was built.
  $this
    ->assertText('Built Textimage');

  // Invalidate the rendered objects cache. Textimage should find the image
  // in its cache.
  Cache::invalidateTags([
    'rendered',
  ]);
  $this
    ->drupalGet('node/' . $nid);
  $this
    ->assertText('Cached Textimage');

  // Invalidate the rendered objects cache, and delete the Textimage cache.
  // Textimage should still find a built image in the store.
  Cache::invalidateTags([
    'rendered',
  ]);
  \Drupal::cache('textimage')
    ->deleteAll();
  $this
    ->drupalGet('node/' . $nid);
  $this
    ->assertText('Stored Textimage');

  // Invalidate 'rendered' again, Textimage should find the image in its
  // cache.
  Cache::invalidateTags([
    'rendered',
  ]);
  $this
    ->drupalGet('node/' . $nid);
  $this
    ->assertText('Cached Textimage');
}