You are here

public function MessageTemplateDependenciesTest::testFilterFormatDependency in Message 8

Tests filter format dependencies.

@covers ::calculateDependencies @covers ::onDependencyRemoval

File

tests/src/Kernel/MessageTemplateDependenciesTest.php, line 32

Class

MessageTemplateDependenciesTest
Tests message templates dependencies.

Namespace

Drupal\Tests\message\Kernel

Code

public function testFilterFormatDependency() {
  $config_factory = \Drupal::configFactory();

  // Create a fallback text format.
  FilterFormat::create([
    'format' => 'fallback',
  ])
    ->save();
  $config_factory
    ->getEditable('filter.settings')
    ->set('fallback_format', 'fallback')
    ->save();
  FilterFormat::create([
    'format' => 'test_format1',
  ])
    ->save();
  FilterFormat::create([
    'format' => 'test_format2',
  ])
    ->save();
  MessageTemplate::create([
    'template' => 'foo',
    'text' => [
      [
        'value' => 'text...',
        'format' => 'test_format1',
      ],
      [
        'value' => 'other text',
        'format' => 'test_format2',
      ],
    ],
  ])
    ->save();

  /** @var \Drupal\message\MessageTemplateInterface $template */
  $template = MessageTemplate::load('foo');
  $dependencies = $template
    ->getDependencies() + [
    'config' => [],
  ];

  // Check that proper dependencies were calculated.
  $this
    ->assertSame([
    'filter.format.test_format1',
    'filter.format.test_format2',
  ], $dependencies['config']);

  // Remove the 2nd filter format.
  FilterFormat::load('test_format2')
    ->delete();
  $template = MessageTemplate::load('foo');
  $dependencies = $template
    ->getDependencies() + [
    'config' => [],
  ];

  // Check that 'test_format2' has been replaced with 'fallback'.
  $this
    ->assertSame('fallback', $template
    ->get('text')[1]['format']);
  $this
    ->assertSame([
    'filter.format.fallback',
    'filter.format.test_format1',
  ], $dependencies['config']);
}