Wrap personalization Tag token with square brackets
Because Gutenberg RichText component does not render square brackets around the HTML comment. We need to append those brackets to tag tokens and expected them when we replace tags by their values. [MAILPOET-6354]
This commit is contained in:
@@ -55,7 +55,7 @@ class Personalizer_Test extends \MailPoetTest {
|
||||
);
|
||||
|
||||
$this->personalizer->set_context( array( 'subscriber_name' => 'John' ) );
|
||||
$html_content = '<p>Hello, <!--user-firstname-->!</p>';
|
||||
$html_content = '<p>Hello, <!--[user-firstname]-->!</p>';
|
||||
$this->assertSame( '<p>Hello, John!</p>', $this->personalizer->personalize_content( $html_content ) );
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ class Personalizer_Test extends \MailPoetTest {
|
||||
$this->tags_registry->register(
|
||||
new Personalization_Tag(
|
||||
'first_name',
|
||||
'user/firstname',
|
||||
'[user/firstname]',
|
||||
'Subscriber Info',
|
||||
function ( $context, $args ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- The $args parameter is not used in this test.
|
||||
return $context['subscriber_name'] ?? 'Default Name';
|
||||
@@ -96,8 +96,8 @@ class Personalizer_Test extends \MailPoetTest {
|
||||
|
||||
$html_content = '
|
||||
<div>
|
||||
<h1>Hello, <!--user/firstname-->!</h1>
|
||||
<p>Your email is <!--user/email-->.</p>
|
||||
<h1>Hello, <!--[user/firstname]-->!</h1>
|
||||
<p>Your email is <!--[user/email]-->.</p>
|
||||
</div>
|
||||
';
|
||||
|
||||
@@ -117,9 +117,9 @@ class Personalizer_Test extends \MailPoetTest {
|
||||
* Test a missing tag in the registry.
|
||||
*/
|
||||
public function testMissingTagInRegistry(): void {
|
||||
$html_content = '<p>Hello, <!--mailpoet/unknown-tag-->!</p>';
|
||||
$html_content = '<p>Hello, <!--[mailpoet/unknown-tag]-->!</p>';
|
||||
$personalized_content = $this->personalizer->personalize_content( $html_content );
|
||||
$this->assertSame( '<p>Hello, <!--mailpoet/unknown-tag-->!</p>', $personalized_content );
|
||||
$this->assertSame( '<p>Hello, <!--[mailpoet/unknown-tag]-->!</p>', $personalized_content );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,7 +129,7 @@ class Personalizer_Test extends \MailPoetTest {
|
||||
$this->tags_registry->register(
|
||||
new Personalization_Tag(
|
||||
'default_name',
|
||||
'user/firstname',
|
||||
'[user/firstname]',
|
||||
'Subscriber Info',
|
||||
function ( $context, $args ) {
|
||||
return $args['default'] ?? 'Default Name';
|
||||
@@ -137,7 +137,7 @@ class Personalizer_Test extends \MailPoetTest {
|
||||
)
|
||||
);
|
||||
|
||||
$html_content = '<p>Hello, <!--user/firstname default="Guest"-->!</p>';
|
||||
$html_content = '<p>Hello, <!--[user/firstname default="Guest"]-->!</p>';
|
||||
$this->assertSame( '<p>Hello, Guest!</p>', $this->personalizer->personalize_content( $html_content ) );
|
||||
}
|
||||
|
||||
@@ -148,7 +148,7 @@ class Personalizer_Test extends \MailPoetTest {
|
||||
$this->tags_registry->register(
|
||||
new Personalization_Tag(
|
||||
'default_name',
|
||||
'user/firstname',
|
||||
'[user/firstname]',
|
||||
'Subscriber Info',
|
||||
function ( $context, $args ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- The $args parameter is not used in this test.
|
||||
return $context['user_name'] ?? 'Default Name';
|
||||
@@ -159,10 +159,10 @@ class Personalizer_Test extends \MailPoetTest {
|
||||
$html_content = '
|
||||
<html>
|
||||
<head>
|
||||
<title>Welcome, <!--user/firstname default="Guest"-->!</title>
|
||||
<title>Welcome, <!--[user/firstname default="Guest"]-->!</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>Hello, <!--user/firstname default="Guest"-->!</p>
|
||||
<p>Hello, <!--[user/firstname default="Guest"]-->!</p>
|
||||
</html>
|
||||
';
|
||||
$this->personalizer->set_context( array( 'user_name' => 'John' ) );
|
||||
|
@@ -49,17 +49,46 @@ class PersonalizationTagsRegistryTest extends TestCase {
|
||||
);
|
||||
|
||||
// Retrieve the tag.
|
||||
$tag = $this->registry->get_by_token( 'first_name' );
|
||||
$tag = $this->registry->get_by_token( '[first_name]' );
|
||||
|
||||
// Assert that the tag is registered correctly.
|
||||
$this->assertNotNull( $tag );
|
||||
$this->assertSame( 'first_name_tag', $tag->get_name() );
|
||||
$this->assertSame( 'first_name', $tag->get_token() );
|
||||
$this->assertSame( '[first_name]', $tag->get_token() );
|
||||
$this->assertSame( 'Subscriber Info', $tag->get_category() );
|
||||
$this->assertSame( 'Personalized Value', $tag->execute_callback( array(), array() ) );
|
||||
$this->assertSame( array( 'description' => 'First name of the subscriber' ), $tag->get_attributes() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register tag and retrieve it.
|
||||
*/
|
||||
public function testRegisterAndGetTagWithBrackets(): void {
|
||||
$callback = function ( $context, $args ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- Callback parameters are required.
|
||||
return 'Personalized Value';
|
||||
};
|
||||
|
||||
// Register a tag.
|
||||
$this->registry->register(
|
||||
new Personalization_Tag(
|
||||
'Last Name',
|
||||
'[last_name]',
|
||||
'Subscriber Info',
|
||||
$callback
|
||||
)
|
||||
);
|
||||
|
||||
// Retrieve the tag.
|
||||
$tag = $this->registry->get_by_token( '[last_name]' );
|
||||
|
||||
// Assert that the tag is registered correctly.
|
||||
$this->assertNotNull( $tag );
|
||||
$this->assertSame( 'Last Name', $tag->get_name() );
|
||||
$this->assertSame( '[last_name]', $tag->get_token() );
|
||||
$this->assertSame( 'Subscriber Info', $tag->get_category() );
|
||||
$this->assertSame( 'Personalized Value', $tag->execute_callback( array(), array() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to retrieve a tag that hasn't been registered.
|
||||
*/
|
||||
@@ -80,14 +109,14 @@ class PersonalizationTagsRegistryTest extends TestCase {
|
||||
};
|
||||
|
||||
// Register a tag.
|
||||
$this->registry->register( new Personalization_Tag( 'tag1', 'tag-1', 'Category 1', $callback1 ) );
|
||||
$this->registry->register( new Personalization_Tag( 'tag1', '[tag-1]', 'Category 1', $callback1 ) );
|
||||
|
||||
// Attempt to register the same tag again.
|
||||
$this->registry->register( new Personalization_Tag( 'tag2', 'tag-2', 'Category 2', $callback2 ) );
|
||||
$this->registry->register( new Personalization_Tag( 'tag2', '[tag-2]', 'Category 2', $callback2 ) );
|
||||
|
||||
// Retrieve the tag and ensure the first registration is preserved.
|
||||
/** @var Personalization_Tag $tag */ // phpcs:ignore Generic.Commenting.DocComment.MissingShort -- used for phpstan
|
||||
$tag = $this->registry->get_by_token( 'tag-1' );
|
||||
$tag = $this->registry->get_by_token( '[tag-1]' );
|
||||
$this->assertSame( 'tag1', $tag->get_name() );
|
||||
$this->assertSame( 'Category 1', $tag->get_category() );
|
||||
$this->assertSame( 'Value 1', $tag->execute_callback( array(), array() ) );
|
||||
@@ -102,16 +131,16 @@ class PersonalizationTagsRegistryTest extends TestCase {
|
||||
};
|
||||
|
||||
// Register multiple tags.
|
||||
$this->registry->register( new Personalization_Tag( 'tag1', 'tag-1', 'Category 1', $callback ) );
|
||||
$this->registry->register( new Personalization_Tag( 'tag2', 'tag-2', 'Category 2', $callback ) );
|
||||
$this->registry->register( new Personalization_Tag( 'tag1', '[tag-1]', 'Category 1', $callback ) );
|
||||
$this->registry->register( new Personalization_Tag( 'tag2', '[tag-2]', 'Category 2', $callback ) );
|
||||
|
||||
// Retrieve all tags.
|
||||
$all_tags = $this->registry->get_all();
|
||||
|
||||
// Assert the number of registered tags.
|
||||
$this->assertCount( 2, $all_tags );
|
||||
$this->assertArrayHasKey( 'tag-1', $all_tags );
|
||||
$this->assertArrayHasKey( 'tag-2', $all_tags );
|
||||
$this->assertArrayHasKey( '[tag-1]', $all_tags );
|
||||
$this->assertArrayHasKey( '[tag-2]', $all_tags );
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user