diff --git a/src/Plugin/PatternFieldProcessor/TokenProcessor.php b/src/Plugin/PatternFieldProcessor/TokenProcessor.php index 5e1c84e..3002830 100644 --- a/src/Plugin/PatternFieldProcessor/TokenProcessor.php +++ b/src/Plugin/PatternFieldProcessor/TokenProcessor.php @@ -83,7 +83,12 @@ class TokenProcessor extends PatternFieldProcessorBase { * {@inheritdoc} */ public function apply(SchemaContract $propertySchema, $value, $context, BubbleableMetadata $bubbleableMetadata): string { - return $this->token->replace($value, $context, [], $bubbleableMetadata); + if (property_exists($propertySchema, 'format') && $propertySchema->format == 'html') { + return $this->token->replace($value, $context, ['clear' => TRUE], $bubbleableMetadata); + } + else { + return $this->token->replacePlain($value, $context, ['clear' => TRUE], $bubbleableMetadata); + } } } diff --git a/tests/src/Kernel/PatternFieldProcessorPluginManagerTest.php b/tests/src/Kernel/PatternFieldProcessorPluginManagerTest.php index 4c4ec89..46681df 100644 --- a/tests/src/Kernel/PatternFieldProcessorPluginManagerTest.php +++ b/tests/src/Kernel/PatternFieldProcessorPluginManagerTest.php @@ -240,6 +240,7 @@ class PatternFieldProcessorPluginManagerTest extends KernelTestBase { // Prepare test values for identification in rendered output. $title = $this->getRandomGenerator()->sentences(3); $title_token = '[node:title]'; + $optional_field_token = '[node:field_url]'; $invalid_token = '[node:invalid_token]'; $body_text = 'My body text with an invalid token: '; @@ -247,6 +248,7 @@ class PatternFieldProcessorPluginManagerTest extends KernelTestBase { $node = Node::create([ 'type' => 'article', 'title' => $title, + 'field_url' => '', ]); $node->save(); @@ -254,6 +256,7 @@ class PatternFieldProcessorPluginManagerTest extends KernelTestBase { $config = [ 'text' => $title_token, 'formatted_text' => $body_text . $invalid_token, + 'image_url' => $optional_field_token ]; // Assemble the pattern element for rendering. @@ -274,8 +277,10 @@ class PatternFieldProcessorPluginManagerTest extends KernelTestBase { // Confirm tokens with replacement values are not visible. $this->assertStringNotContainsString($title_token, $output); - // Confirm the invalid token is unchanged. - $this->assertStringContainsString($invalid_token, $output); + // Confirm the invalid token and token without a replacement value are both + // removed from the output. + $this->assertStringNotContainsString($invalid_token, $output); + $this->assertStringNotContainsString($optional_field_token, $output); // Confirm token replacement text is included as expected. $this->assertStringContainsString($title, $output, 'Title text was not replaced into token placement.'); diff --git a/tests/src/Unit/Plugin/PatternFieldProcessor/TokenProcessorTest.php b/tests/src/Unit/Plugin/PatternFieldProcessor/TokenProcessorTest.php index 205b3b5..a9c3b7c 100644 --- a/tests/src/Unit/Plugin/PatternFieldProcessor/TokenProcessorTest.php +++ b/tests/src/Unit/Plugin/PatternFieldProcessor/TokenProcessorTest.php @@ -160,39 +160,73 @@ JSON; * @covers ::apply * @covers ::__construct */ - public function testApply() { + public function testApplyReplace() { $schema_json = <<getRandomGenerator()->name(); $expected = str_replace('[node:title]', $randomTitle, $value); $node = $this->createMock(NodeInterface::class); $node->title = $randomTitle; - // Mock the token replacement. + // Ensure the replacePlain method is not invoked for html formatted strings. + $this->token->expects($this->never()) + ->method('replacePlain'); + + // Mock the token replacement with expected arguments. $this->token->expects($this->once()) ->method('replace') + ->with($value, ['node' => $node], ['clear' => TRUE], $this->isInstanceOf(BubbleableMetadata::class)) + ->willReturn($expected); + + $result = $this->plugin->apply($schema, $value, ['node' => $node], new BubbleableMetadata()); + $this->assertIsString($result); + $this->assertEquals($expected, $result); + } + + /** + * @covers ::apply + * @covers ::__construct + */ + public function testApplyReplacePlain() { + $schema_json = <<getRandomGenerator()->name(); + $expected = str_replace('[node:title]', $randomTitle, $value); + $node = $this->createMock(NodeInterface::class); + $node->title = $randomTitle; + + // Ensure the replace method is not invoked for plain text strings. + $this->token->expects($this->never()) + ->method('replace'); + + // Mock the token replacement with expected arguments. + $this->token->expects($this->once()) + ->method('replacePlain') + ->with($value, ['node' => $node], ['clear' => TRUE], $this->isInstanceOf(BubbleableMetadata::class)) ->willReturn($expected); $result = $this->plugin->apply($schema, $value, ['node' => $node], new BubbleableMetadata()); $this->assertIsString($result); - $this->assertEquals("My $randomTitle string", $result); + $this->assertEquals($expected, $result); } }