class TextProcessedTest

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

Tests TextProcessed with AttachmentsInterface.

Attributes

#[CoversClass(TextProcessed::class)] #[Group('text')]

Hierarchy

Expanded class hierarchy of TextProcessedTest

File

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

Namespace

Drupal\Tests\text\Unit
View source
class TextProcessedTest extends UnitTestCase {
  
  /**
   * Tests addAttachments() merges attachments correctly.
   */
  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.