function TextProcessedTest::testAttachments

Same name and namespace in other branches
  1. 11.x core/modules/text/tests/src/Unit/TextProcessedTest.php \Drupal\Tests\text\Unit\TextProcessedTest::testAttachments()

Tests addAttachments() merges attachments correctly.

File

core/modules/text/tests/src/Unit/TextProcessedTest.php, line 24

Class

TextProcessedTest
Tests TextProcessed with AttachmentsInterface.

Namespace

Drupal\Tests\text\Unit

Code

public function testAttachments() : void {
  $definition = $this->createMock(DataDefinitionInterface::class);
  $definition->expects($this->atLeastOnce())
    ->method('getSetting')
    ->with('text source')
    ->willReturn('value');
  $parent = $this->createStub(TextItem::class);
  $parent->method('__get')
    ->willReturnMap([
    [
      'value',
      '',
    ],
    [
      'format',
      'basic_html',
    ],
  ]);
  $parent->method('getLangcode')
    ->willReturn('en');
  $processed = new TextProcessed($definition, 'formatted_text', $parent);
  // Set initial attachments.
  $processed->setAttachments([
    'library' => [
      'core/drupal',
    ],
  ]);
  // Add more attachments.
  $result = $processed->addAttachments([
    'library' => [
      'core/jquery',
    ],
    'drupalSettings' => [
      'foo' => 'bar',
    ],
  ]);
  $this->assertSame($processed, $result);
  $attachments = $processed->getAttachments();
  $this->assertContains('core/drupal', $attachments['library']);
  $this->assertContains('core/jquery', $attachments['library']);
  $this->assertSame([
    'foo' => 'bar',
  ], $attachments['drupalSettings']);
}

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