|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Podcast Player Block Email Rendering tests |
| 4 | + * |
| 5 | + * @package automattic/jetpack |
| 6 | + */ |
| 7 | + |
| 8 | +require_once JETPACK__PLUGIN_DIR . 'extensions/blocks/podcast-player/podcast-player.php'; |
| 9 | + |
| 10 | +// Include mock classes for WooCommerce Email Editor helpers |
| 11 | +require_once __DIR__ . '/class-mock-styles-helper.php'; |
| 12 | +require_once __DIR__ . '/class-mock-table-wrapper-helper.php'; |
| 13 | + |
| 14 | +use PHPUnit\Framework\Attributes\CoversFunction; |
| 15 | + |
| 16 | +/** |
| 17 | + * Podcast Player Block Email Rendering tests. |
| 18 | + * |
| 19 | + * These tests verify the render_email function works correctly for various scenarios |
| 20 | + * including valid inputs, security validation, and email rendering. |
| 21 | + * |
| 22 | + * @covers ::Automattic\Jetpack\Extensions\Podcast_Player\render_email |
| 23 | + */ |
| 24 | +#[CoversFunction( 'Automattic\Jetpack\Extensions\Podcast_Player\render_email' )] |
| 25 | +class Podcast_Player_Block_Email_Test extends WP_UnitTestCase { |
| 26 | + use \Automattic\Jetpack\PHPUnit\WP_UnitTestCase_Fix; |
| 27 | + |
| 28 | + /** |
| 29 | + * Helper to create a parsed block with test podcast URL. |
| 30 | + * |
| 31 | + * @param array $attrs Optional custom attributes. |
| 32 | + * @return array Parsed block structure. |
| 33 | + */ |
| 34 | + private function create_parsed_block_with_attrs( $attrs = array() ) { |
| 35 | + $default_attrs = array( |
| 36 | + 'url' => 'https://feeds.acast.com/public/shows/test-podcast', |
| 37 | + ); |
| 38 | + |
| 39 | + return array( |
| 40 | + 'attrs' => array_merge( $default_attrs, $attrs ), |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Helper to create a rendering context mock. |
| 46 | + * |
| 47 | + * @param string $width The width to return from get_layout_width_without_padding. |
| 48 | + * @return object Mock rendering context. |
| 49 | + */ |
| 50 | + private function create_rendering_context_mock( $width = '600px' ) { |
| 51 | + return new class( $width ) { |
| 52 | + private $width; |
| 53 | + |
| 54 | + public function __construct( $width ) { |
| 55 | + $this->width = $width; |
| 56 | + } |
| 57 | + |
| 58 | + public function get_layout_width_without_padding() { |
| 59 | + return $this->width; |
| 60 | + } |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Test render_email with valid podcast URL. |
| 66 | + */ |
| 67 | + public function test_render_email_with_valid_podcast_url() { |
| 68 | + $parsed_block = $this->create_parsed_block_with_attrs(); |
| 69 | + $mock_context = $this->create_rendering_context_mock(); |
| 70 | + $result = \Automattic\Jetpack\Extensions\Podcast_Player\render_email( '', $parsed_block, $mock_context ); |
| 71 | + |
| 72 | + // Should return HTML content |
| 73 | + $this->assertNotEmpty( $result ); |
| 74 | + $this->assertStringContainsString( '<table', $result ); |
| 75 | + $this->assertStringContainsString( 'href=', $result ); |
| 76 | + $this->assertStringContainsString( 'Listen to the podcast', $result ); |
| 77 | + |
| 78 | + // Should contain table-based layout for email compatibility |
| 79 | + $this->assertStringContainsString( 'border-collapse: collapse', $result ); |
| 80 | + |
| 81 | + // Should contain margin styling for email spacing |
| 82 | + $this->assertStringContainsString( 'margin: 16px 0', $result ); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Test render_email with missing attrs. |
| 87 | + */ |
| 88 | + public function test_render_email_with_missing_attrs() { |
| 89 | + $mock_context = $this->create_rendering_context_mock(); |
| 90 | + |
| 91 | + // Test with missing attrs |
| 92 | + $result = \Automattic\Jetpack\Extensions\Podcast_Player\render_email( '', array( 'not-attrs' => array() ), $mock_context ); |
| 93 | + $this->assertSame( '', $result ); |
| 94 | + |
| 95 | + // Test with empty attrs |
| 96 | + $result = \Automattic\Jetpack\Extensions\Podcast_Player\render_email( '', array( 'attrs' => array() ), $mock_context ); |
| 97 | + $this->assertSame( '', $result ); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Test render_email with empty URL. |
| 102 | + */ |
| 103 | + public function test_render_email_with_empty_url() { |
| 104 | + $parsed_block = $this->create_parsed_block_with_attrs( |
| 105 | + array( |
| 106 | + 'url' => '', |
| 107 | + ) |
| 108 | + ); |
| 109 | + $mock_context = $this->create_rendering_context_mock(); |
| 110 | + $result = \Automattic\Jetpack\Extensions\Podcast_Player\render_email( '', $parsed_block, $mock_context ); |
| 111 | + |
| 112 | + // Should return empty string when no valid URL |
| 113 | + $this->assertSame( '', $result ); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Test render_email with invalid URL. |
| 118 | + */ |
| 119 | + public function test_render_email_with_invalid_url() { |
| 120 | + $parsed_block = $this->create_parsed_block_with_attrs( |
| 121 | + array( |
| 122 | + 'url' => 'not-a-valid-url', |
| 123 | + ) |
| 124 | + ); |
| 125 | + $mock_context = $this->create_rendering_context_mock(); |
| 126 | + $result = \Automattic\Jetpack\Extensions\Podcast_Player\render_email( '', $parsed_block, $mock_context ); |
| 127 | + |
| 128 | + // Should return empty string when URL is invalid |
| 129 | + $this->assertSame( '', $result ); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Test render_email returns empty when WooCommerce Email Editor helper classes are missing. |
| 134 | + */ |
| 135 | + public function test_render_email_returns_empty_when_helpers_missing() { |
| 136 | + $parsed_block = $this->create_parsed_block_with_attrs(); |
| 137 | + $mock_context = $this->create_rendering_context_mock(); |
| 138 | + |
| 139 | + // Verify that with mocked classes, the function works |
| 140 | + $result_with_mocks = \Automattic\Jetpack\Extensions\Podcast_Player\render_email( '', $parsed_block, $mock_context ); |
| 141 | + $this->assertNotEmpty( $result_with_mocks ); |
| 142 | + |
| 143 | + // Test the class existence check logic directly |
| 144 | + $table_helper_exists = class_exists( '\Automattic\WooCommerce\EmailEditor\Integrations\Utils\Table_Wrapper_Helper' ); |
| 145 | + |
| 146 | + // Class should exist due to our mock |
| 147 | + $this->assertTrue( $table_helper_exists, 'Table_Wrapper_Helper class should be mocked and available' ); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Test render_email security - URL validation. |
| 152 | + */ |
| 153 | + public function test_render_email_security_url_validation() { |
| 154 | + $parsed_block = $this->create_parsed_block_with_attrs( |
| 155 | + array( |
| 156 | + 'url' => 'https://feeds.acast.com/public/shows/test-podcast', |
| 157 | + ) |
| 158 | + ); |
| 159 | + $mock_context = $this->create_rendering_context_mock(); |
| 160 | + $result = \Automattic\Jetpack\Extensions\Podcast_Player\render_email( '', $parsed_block, $mock_context ); |
| 161 | + |
| 162 | + // Should render with valid URL |
| 163 | + $this->assertNotEmpty( $result ); |
| 164 | + $this->assertStringContainsString( 'href="https://feeds.acast.com/public/shows/test-podcast"', $result ); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Test render_email contains proper button styling. |
| 169 | + */ |
| 170 | + public function test_render_email_contains_button_styling() { |
| 171 | + $parsed_block = $this->create_parsed_block_with_attrs(); |
| 172 | + $mock_context = $this->create_rendering_context_mock(); |
| 173 | + $result = \Automattic\Jetpack\Extensions\Podcast_Player\render_email( '', $parsed_block, $mock_context ); |
| 174 | + |
| 175 | + // Should contain button styling |
| 176 | + $this->assertNotEmpty( $result ); |
| 177 | + $this->assertStringContainsString( 'background-color: #f6f7f7', $result ); |
| 178 | + $this->assertStringContainsString( 'border: 1px solid #AAA', $result ); |
| 179 | + $this->assertStringContainsString( 'border-radius: 9999px', $result ); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Test render_email contains play icon. |
| 184 | + */ |
| 185 | + public function test_render_email_contains_play_icon() { |
| 186 | + $parsed_block = $this->create_parsed_block_with_attrs(); |
| 187 | + $mock_context = $this->create_rendering_context_mock(); |
| 188 | + $result = \Automattic\Jetpack\Extensions\Podcast_Player\render_email( '', $parsed_block, $mock_context ); |
| 189 | + |
| 190 | + // Should contain play icon |
| 191 | + $this->assertNotEmpty( $result ); |
| 192 | + $this->assertStringContainsString( 'audio-play.png', $result ); |
| 193 | + $this->assertStringContainsString( '<img', $result ); |
| 194 | + } |
| 195 | +} |
0 commit comments