tags_registry = new Personalization_Tags_Registry(); $this->personalizer = new Personalizer( $this->tags_registry ); } /** * Test personalizing content with a single tag. */ public function testPersonalizeContentWithSingleTag(): void { // Register a tag in the registry. $this->tags_registry->register( 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' ) ); $html_content = '
Hello, !
'; $this->assertSame( 'Hello, John!
', $this->personalizer->personalize_content( $html_content ) ); } /** * Test personalizing content with multiple tags. */ public function testPersonalizeContentWithMultipleTags(): void { // Register multiple tags in the registry. $this->tags_registry->register( 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( 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. $this->personalizer->set_context( array( 'subscriber_name' => 'John', 'subscriber_email' => 'john@example.com', ) ); $html_content = 'Your email is .
Your email is john@example.com.
Hello, !
'; $personalized_content = $this->personalizer->personalize_content( $html_content ); $this->assertSame( 'Hello, !
', $personalized_content ); } /** * Test a callback arguments. */ public function testTagWithArguments(): void { $this->tags_registry->register( new Personalization_Tag( 'default_name', 'user/firstname', 'Subscriber Info', function ( $context, $args ) { return $args['default'] ?? 'Default Name'; } ) ); $html_content = 'Hello, !
'; $this->assertSame( 'Hello, Guest!
', $this->personalizer->personalize_content( $html_content ) ); } /** * Test a callback arguments. */ public function testPersonalizationInTitle(): void { $this->tags_registry->register( 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 = 'Hello, !
'; $this->personalizer->set_context( array( 'user_name' => 'John' ) ); $this->assertSame( 'Hello, John!
', $this->personalizer->personalize_content( $html_content ) ); } }