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( '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( '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'; } ); // Set the context for personalization. $this->personalizer->set_context( array( 'subscriber_name' => 'John', 'subscriber_email' => 'john@example.com', ) ); $html_content = '

Hello, !

Your email is .

'; $personalized_content = $this->personalizer->personalize_content( $html_content ); $this->assertSame( '

Hello, John!

Your email is john@example.com.

', $personalized_content ); } /** * Test a missing tag in the registry. */ public function testMissingTagInRegistry(): void { $html_content = '

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( '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( '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 = ' Welcome, <!--user/firstname default="Guest"-->!

Hello, !

'; $this->personalizer->set_context( array( 'user_name' => 'John' ) ); $this->assertSame( ' Welcome, John!

Hello, John!

', $this->personalizer->personalize_content( $html_content ) ); } }