Add new class Personalization_Tag

The new class contains all necessary data to replace token with personalized value.
[MAILPOET-6328]
This commit is contained in:
Jan Lysý
2024-11-26 18:56:36 +01:00
committed by Pavel Dohnal
parent 829905a0f5
commit f95688ca70
6 changed files with 215 additions and 82 deletions

View File

@@ -8,6 +8,7 @@
declare(strict_types = 1);
namespace MailPoet\EmailEditor\Engine;
use MailPoet\EmailEditor\Engine\PersonalizationTags\Personalization_Tag;
use MailPoet\EmailEditor\Engine\PersonalizationTags\Personalization_Tags_Registry;
/**
@@ -43,12 +44,14 @@ class Personalizer_Test extends \MailPoetTest {
public function testPersonalizeContentWithSingleTag(): void {
// Register a tag in the registry.
$this->tags_registry->register(
'first_name',
'user-firstname',
'User',
function ( $context, $args ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- The $args parameter is not used in this test.
return $context['subscriber_name'] ?? 'Default Name';
}
new Personalization_Tag(
'first_name',
'user-firstname',
'User',
function ( $context, $args ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- The $args parameter is not used in this test.
return $context['subscriber_name'] ?? 'Default Name';
}
)
);
$this->personalizer->set_context( array( 'subscriber_name' => 'John' ) );
@@ -62,21 +65,25 @@ class Personalizer_Test extends \MailPoetTest {
public function testPersonalizeContentWithMultipleTags(): void {
// Register multiple tags in the registry.
$this->tags_registry->register(
'first_name',
'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';
}
new Personalization_Tag(
'first_name',
'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';
}
)
);
$this->tags_registry->register(
'email',
'user/email',
'Subscriber Info',
function ( $context, $args ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- The $args parameter is not used in this test.
return $context['subscriber_email'] ?? 'unknown@example.com';
}
new Personalization_Tag(
'email',
'user/email',
'Subscriber Info',
function ( $context, $args ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- The $args parameter is not used in this test.
return $context['subscriber_email'] ?? 'unknown@example.com';
}
)
);
// Set the context for personalization.
@@ -120,12 +127,14 @@ class Personalizer_Test extends \MailPoetTest {
*/
public function testTagWithArguments(): void {
$this->tags_registry->register(
'default_name',
'user/firstname',
'Subscriber Info',
function ( $context, $args ) {
return $args['default'] ?? 'Default Name';
}
new Personalization_Tag(
'default_name',
'user/firstname',
'Subscriber Info',
function ( $context, $args ) {
return $args['default'] ?? 'Default Name';
}
)
);
$html_content = '<p>Hello, <!--user/firstname default="Guest"-->!</p>';
@@ -137,12 +146,14 @@ class Personalizer_Test extends \MailPoetTest {
*/
public function testPersonalizationInTitle(): void {
$this->tags_registry->register(
'default_name',
'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';
}
new Personalization_Tag(
'default_name',
'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';
}
)
);
$html_content = '

View File

@@ -7,6 +7,7 @@
declare(strict_types = 1);
use MailPoet\EmailEditor\Engine\PersonalizationTags\Personalization_Tag;
use PHPUnit\Framework\TestCase;
use MailPoet\EmailEditor\Engine\PersonalizationTags\Personalization_Tags_Registry;
@@ -32,61 +33,63 @@ class PersonalizationTagsRegistryTest extends TestCase {
* Register tag and retrieve it.
*/
public function testRegisterAndGetTag(): void {
$callback = function () {
$callback = function ( $context, $args ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- Callback parameters are required.
return 'Personalized Value';
};
// Register a tag.
$this->registry->register(
'first_name_tag',
'first_name',
'Subscriber Info',
$callback,
array( 'description' => 'First name of the subscriber' )
new Personalization_Tag(
'first_name_tag',
'first_name',
'Subscriber Info',
$callback,
array( 'description' => 'First name of the subscriber' )
)
);
// Retrieve the tag.
$tag_data = $this->registry->get_by_tag( 'first_name' );
$tag = $this->registry->get_by_token( 'first_name' );
// Assert that the tag is registered correctly.
$this->assertNotNull( $tag_data );
$this->assertSame( 'first_name', $tag_data['tag'] );
$this->assertSame( 'first_name_tag', $tag_data['name'] );
$this->assertSame( 'Subscriber Info', $tag_data['category'] );
$this->assertSame( $callback, $tag_data['callback'] );
$this->assertSame( 'First name of the subscriber', $tag_data['attributes']['description'] );
$this->assertNotNull( $tag );
$this->assertSame( 'first_name_tag', $tag->get_name() );
$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() );
}
/**
* Try to retrieve a tag that hasn't been registered.
*/
public function testRetrieveNonexistentTag(): void {
$this->assertNull( $this->registry->get_by_tag( 'nonexistent' ) );
$this->assertNull( $this->registry->get_by_token( 'nonexistent' ) );
}
/**
* Register multiple tags and retrieve them.
*/
public function testRegisterDuplicateTag(): void {
$callback1 = function () {
$callback1 = function ( $context, $args ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- Callback parameters are required.
return 'Value 1';
};
$callback2 = function () {
$callback2 = function ( $context, $args ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- Callback parameters are required.
return 'Value 2';
};
// Register a tag.
$this->registry->register( '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( '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.
$tag_data = $this->registry->get_by_tag( 'tag-1' );
$this->assertSame( 'tag1', $tag_data['name'] );
$this->assertSame( 'Category 1', $tag_data['category'] );
$this->assertSame( $callback1, $tag_data['callback'] );
$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() ) );
}
/**
@@ -98,8 +101,8 @@ class PersonalizationTagsRegistryTest extends TestCase {
};
// Register multiple tags.
$this->registry->register( 'tag1', 'tag-1', 'Category 1', $callback );
$this->registry->register( '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();