Loading core/modules/text/src/TextProcessed.php +14 −1 Changes for core/modules/text/src/TextProcessed.php: 14 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -3,6 +3,8 @@ namespace Drupal\text; use Drupal\Core\Cache\CacheableDependencyInterface; use Drupal\Core\Render\AttachmentsInterface; use Drupal\Core\Render\AttachmentsTrait; use Drupal\Core\Serialization\Attribute\JsonSchema; use Drupal\Core\TypedData\DataDefinitionInterface; use Drupal\Core\TypedData\TypedDataInterface; Loading @@ -16,7 +18,9 @@ * Required settings (below the definition's 'settings' key) are: * - text source: The text property containing the to be processed text. */ class TextProcessed extends TypedData implements CacheableDependencyInterface { class TextProcessed extends TypedData implements CacheableDependencyInterface, AttachmentsInterface { use AttachmentsTrait; /** * Cached processed text. Loading Loading @@ -112,4 +116,13 @@ protected function getRenderer() { return \Drupal::service('renderer'); } /** * {@inheritdoc} */ public function getAttachments() { $this->getValue(); $this->addAttachments($this->processed->getAttachments()); return $this->attachments; } } core/modules/text/tests/src/Kernel/TextProcessedAttachmentsTest.php 0 → 100644 +78 −0 Changes for core/modules/text/tests/src/Kernel/TextProcessedAttachmentsTest.php: 78 added lines, 0 removed lines. Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\Tests\text\Kernel; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; use Drupal\filter\Entity\FilterFormat; use Drupal\KernelTests\Core\Entity\EntityKernelTestBase; use Drupal\text\TextProcessed; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses; /** * Tests that TextProcessed::getAttachments() bubbles filter attachments. */ #[CoversClass(TextProcessed::class)] #[Group('text')] #[RunTestsInSeparateProcesses] class TextProcessedAttachmentsTest extends EntityKernelTestBase { /** * {@inheritdoc} */ protected static $modules = ['filter_test']; /** * {@inheritdoc} */ protected function setUp(): void { parent::setUp(); FilterFormat::create([ 'format' => 'test_assets_format', 'name' => 'Test assets format', 'filters' => [ 'filter_test_assets' => [ 'status' => TRUE, ], ], ])->save(); FieldStorageConfig::create([ 'field_name' => 'formatted_text', 'entity_type' => 'entity_test', 'type' => 'text', ])->save(); FieldConfig::create([ 'entity_type' => 'entity_test', 'bundle' => 'entity_test', 'field_name' => 'formatted_text', 'label' => 'Formatted text', ])->save(); } /** * Tests that filter library attachments are available via getAttachments(). */ public function testGetAttachmentsContainsFilterLibrary(): void { $entity = $this->entityTypeManager ->getStorage('entity_test') ->create(['name' => $this->randomMachineName()]); $entity->formatted_text = [ 'value' => 'Hello, world!', 'format' => 'test_assets_format', ]; /** @var \Drupal\text\TextProcessed $processed */ $processed = $entity->get('formatted_text')->get(0)->get('processed'); $attachments = $processed->getAttachments(); $this->assertContains('filter/caption', $attachments['library']); } } core/modules/text/tests/src/Unit/TextProcessedTest.php 0 → 100644 +60 −0 Changes for core/modules/text/tests/src/Unit/TextProcessedTest.php: 60 added lines, 0 removed lines. Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\Tests\text\Unit; use Drupal\Core\TypedData\DataDefinitionInterface; use Drupal\Tests\UnitTestCase; use Drupal\text\Plugin\Field\FieldType\TextItem; use Drupal\text\TextProcessed; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Group; /** * Tests TextProcessed with AttachmentsInterface. */ #[CoversClass(TextProcessed::class)] #[Group('text')] 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']); } } Loading
core/modules/text/src/TextProcessed.php +14 −1 Changes for core/modules/text/src/TextProcessed.php: 14 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -3,6 +3,8 @@ namespace Drupal\text; use Drupal\Core\Cache\CacheableDependencyInterface; use Drupal\Core\Render\AttachmentsInterface; use Drupal\Core\Render\AttachmentsTrait; use Drupal\Core\Serialization\Attribute\JsonSchema; use Drupal\Core\TypedData\DataDefinitionInterface; use Drupal\Core\TypedData\TypedDataInterface; Loading @@ -16,7 +18,9 @@ * Required settings (below the definition's 'settings' key) are: * - text source: The text property containing the to be processed text. */ class TextProcessed extends TypedData implements CacheableDependencyInterface { class TextProcessed extends TypedData implements CacheableDependencyInterface, AttachmentsInterface { use AttachmentsTrait; /** * Cached processed text. Loading Loading @@ -112,4 +116,13 @@ protected function getRenderer() { return \Drupal::service('renderer'); } /** * {@inheritdoc} */ public function getAttachments() { $this->getValue(); $this->addAttachments($this->processed->getAttachments()); return $this->attachments; } }
core/modules/text/tests/src/Kernel/TextProcessedAttachmentsTest.php 0 → 100644 +78 −0 Changes for core/modules/text/tests/src/Kernel/TextProcessedAttachmentsTest.php: 78 added lines, 0 removed lines. Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\Tests\text\Kernel; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; use Drupal\filter\Entity\FilterFormat; use Drupal\KernelTests\Core\Entity\EntityKernelTestBase; use Drupal\text\TextProcessed; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses; /** * Tests that TextProcessed::getAttachments() bubbles filter attachments. */ #[CoversClass(TextProcessed::class)] #[Group('text')] #[RunTestsInSeparateProcesses] class TextProcessedAttachmentsTest extends EntityKernelTestBase { /** * {@inheritdoc} */ protected static $modules = ['filter_test']; /** * {@inheritdoc} */ protected function setUp(): void { parent::setUp(); FilterFormat::create([ 'format' => 'test_assets_format', 'name' => 'Test assets format', 'filters' => [ 'filter_test_assets' => [ 'status' => TRUE, ], ], ])->save(); FieldStorageConfig::create([ 'field_name' => 'formatted_text', 'entity_type' => 'entity_test', 'type' => 'text', ])->save(); FieldConfig::create([ 'entity_type' => 'entity_test', 'bundle' => 'entity_test', 'field_name' => 'formatted_text', 'label' => 'Formatted text', ])->save(); } /** * Tests that filter library attachments are available via getAttachments(). */ public function testGetAttachmentsContainsFilterLibrary(): void { $entity = $this->entityTypeManager ->getStorage('entity_test') ->create(['name' => $this->randomMachineName()]); $entity->formatted_text = [ 'value' => 'Hello, world!', 'format' => 'test_assets_format', ]; /** @var \Drupal\text\TextProcessed $processed */ $processed = $entity->get('formatted_text')->get(0)->get('processed'); $attachments = $processed->getAttachments(); $this->assertContains('filter/caption', $attachments['library']); } }
core/modules/text/tests/src/Unit/TextProcessedTest.php 0 → 100644 +60 −0 Changes for core/modules/text/tests/src/Unit/TextProcessedTest.php: 60 added lines, 0 removed lines. Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\Tests\text\Unit; use Drupal\Core\TypedData\DataDefinitionInterface; use Drupal\Tests\UnitTestCase; use Drupal\text\Plugin\Field\FieldType\TextItem; use Drupal\text\TextProcessed; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Group; /** * Tests TextProcessed with AttachmentsInterface. */ #[CoversClass(TextProcessed::class)] #[Group('text')] 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']); } }