|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Subscriptions navigation placement tests. |
| 4 | + * |
| 5 | + * @package automattic/jetpack |
| 6 | + */ |
| 7 | + |
| 8 | +use Automattic\Jetpack\Extensions\Subscriptions\Jetpack_Subscription_Site; |
| 9 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 10 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 11 | + |
| 12 | +require_once JETPACK__PLUGIN_DIR . 'extensions/blocks/subscriptions/class-jetpack-subscription-site.php'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Subscribe blocks already placed in navigation. |
| 16 | + * |
| 17 | + * @covers \Automattic\Jetpack\Extensions\Subscriptions\Jetpack_Subscription_Site |
| 18 | + */ |
| 19 | +#[CoversClass( Jetpack_Subscription_Site::class )] |
| 20 | +class Jetpack_Subscription_Site_Test extends WP_UnitTestCase { |
| 21 | + |
| 22 | + use \Automattic\Jetpack\PHPUnit\WP_UnitTestCase_Fix; |
| 23 | + |
| 24 | + /** |
| 25 | + * Theme directories to restore after each test. |
| 26 | + * |
| 27 | + * @var string[] |
| 28 | + */ |
| 29 | + private $theme_directories; |
| 30 | + |
| 31 | + /** |
| 32 | + * Enable navigation placement in a block theme. |
| 33 | + */ |
| 34 | + public function set_up() { |
| 35 | + parent::set_up(); |
| 36 | + |
| 37 | + $this->theme_directories = $GLOBALS['wp_theme_directories']; |
| 38 | + register_theme_directory( DIR_TESTDATA . '/themedir1' ); |
| 39 | + switch_theme( 'block-theme' ); |
| 40 | + update_option( 'jetpack_subscriptions_subscribe_navigation_enabled', true ); |
| 41 | + update_option( 'jetpack_subscriptions_subscribe_post_end_enabled', false ); |
| 42 | + Jetpack_Subscription_Site::init()->handle_subscribe_block_placements(); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Restore the theme directories. |
| 47 | + */ |
| 48 | + public function tear_down() { |
| 49 | + $GLOBALS['wp_theme_directories'] = $this->theme_directories; |
| 50 | + parent::tear_down(); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Keep one Subscribe block when navigation hooks run repeatedly. |
| 55 | + * |
| 56 | + * @dataProvider data_navigation_placement |
| 57 | + * |
| 58 | + * @param string $subscribe_markup Existing Subscribe block markup. |
| 59 | + * @param array $expected_attrs Expected Subscribe block attributes. |
| 60 | + * @param string $callback Block hooks callback. |
| 61 | + */ |
| 62 | + #[DataProvider( 'data_navigation_placement' )] |
| 63 | + public function test_navigation_placement_does_not_duplicate_existing_subscribe_block( $subscribe_markup, $expected_attrs, $callback ) { |
| 64 | + $this->assertTrue( wp_is_block_theme() ); |
| 65 | + $post = self::factory()->post->create_and_get( |
| 66 | + array( |
| 67 | + 'post_type' => 'wp_navigation', |
| 68 | + 'post_status' => 'publish', |
| 69 | + 'post_content' => '<!-- wp:navigation-link {"label":"Home","url":"/"} /-->' . $subscribe_markup, |
| 70 | + ) |
| 71 | + ); |
| 72 | + |
| 73 | + $content = apply_block_hooks_to_content_from_post_object( $post->post_content, $post, $callback ); |
| 74 | + $this->assertSame( 1, substr_count( $content, '<!-- wp:jetpack/subscriptions ' ) ); |
| 75 | + $this->assertStringContainsString( get_comment_delimited_block_content( 'jetpack/subscriptions', $expected_attrs, '' ), $content ); |
| 76 | + $this->assertSame( $content, apply_block_hooks_to_content_from_post_object( $content, $post, $callback ) ); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * An anchor block without innerBlocks must not break the filter. |
| 81 | + */ |
| 82 | + public function test_navigation_anchor_without_inner_blocks_does_not_error() { |
| 83 | + $block_type = 'jetpack/subscriptions'; |
| 84 | + $hooked_block = array( |
| 85 | + 'blockName' => $block_type, |
| 86 | + 'attrs' => array(), |
| 87 | + ); |
| 88 | + $result = apply_filters( |
| 89 | + "hooked_block_{$block_type}", |
| 90 | + $hooked_block, |
| 91 | + $block_type, |
| 92 | + 'last_child', |
| 93 | + array( 'blockName' => 'core/navigation' ), |
| 94 | + null |
| 95 | + ); |
| 96 | + $this->assertSame( $block_type, $result['blockName'] ); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @dataProvider data_navigation_post_types |
| 101 | + * |
| 102 | + * @param string $post_type Post type containing the navigation. |
| 103 | + */ |
| 104 | + #[DataProvider( 'data_navigation_post_types' )] |
| 105 | + public function test_saved_navigation_keeps_subscribe_removal_after_non_rest_update( $post_type ) { |
| 106 | + $subscribe = '<!-- wp:jetpack/subscriptions /-->'; |
| 107 | + $content = '<!-- wp:navigation-link {"label":"Home","url":"/"} /-->' . $subscribe; |
| 108 | + if ( 'wp_template_part' === $post_type ) { |
| 109 | + $content = '<!-- wp:navigation -->' . $content . '<!-- /wp:navigation -->'; |
| 110 | + } |
| 111 | + $post = self::factory()->post->create_and_get( |
| 112 | + array( |
| 113 | + 'post_type' => $post_type, |
| 114 | + 'post_name' => 'header', |
| 115 | + 'post_status' => 'publish', |
| 116 | + 'post_content' => $content, |
| 117 | + ) |
| 118 | + ); |
| 119 | + |
| 120 | + if ( 'wp_navigation' === $post_type ) { |
| 121 | + $changes = update_ignored_hooked_blocks_postmeta( (object) (array) $post ); |
| 122 | + } else { |
| 123 | + wp_set_object_terms( $post->ID, get_stylesheet(), 'wp_theme' ); |
| 124 | + wp_set_object_terms( $post->ID, 'header', 'wp_template_part_area' ); |
| 125 | + $changes = inject_ignored_hooked_blocks_metadata_attributes( (object) (array) $post ); |
| 126 | + } |
| 127 | + $this->assertNotWPError( $changes ); |
| 128 | + $this->assertSame( $post->ID, wp_update_post( wp_slash( (array) $changes ) ) ); |
| 129 | + |
| 130 | + $content = get_post( $post->ID )->post_content; |
| 131 | + $this->assertStringContainsString( $subscribe, $content ); |
| 132 | + if ( 'wp_navigation' === $post_type ) { |
| 133 | + $this->assertContains( 'jetpack/subscriptions', (array) json_decode( get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true ), true ) ); |
| 134 | + } else { |
| 135 | + $this->assertStringContainsString( '"ignoredHookedBlocks":["jetpack/subscriptions"]', $content ); |
| 136 | + } |
| 137 | + wp_update_post( |
| 138 | + wp_slash( |
| 139 | + array( |
| 140 | + 'ID' => $post->ID, |
| 141 | + 'post_content' => str_replace( $subscribe, '', $content ), |
| 142 | + ) |
| 143 | + ) |
| 144 | + ); |
| 145 | + |
| 146 | + $post = get_post( $post->ID ); |
| 147 | + if ( 'wp_navigation' === $post_type ) { |
| 148 | + $content = apply_block_hooks_to_content_from_post_object( $post->post_content, $post ); |
| 149 | + } else { |
| 150 | + $template = new WP_Block_Template(); |
| 151 | + $template->area = 'header'; |
| 152 | + $content = apply_block_hooks_to_content( $post->post_content, $template ); |
| 153 | + } |
| 154 | + $this->assertFalse( has_block( 'jetpack/subscriptions', $content ) ); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * @return array |
| 159 | + */ |
| 160 | + public static function data_navigation_post_types() { |
| 161 | + return array( |
| 162 | + 'referenced menu' => array( 'wp_navigation' ), |
| 163 | + 'inline menu' => array( 'wp_template_part' ), |
| 164 | + ); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Navigation contents for both block hooks callbacks. |
| 169 | + * |
| 170 | + * @return array |
| 171 | + */ |
| 172 | + public static function data_navigation_placement() { |
| 173 | + $manual_attrs = array( |
| 174 | + 'className' => 'is-style-button', |
| 175 | + 'submitButtonText' => 'Join us', |
| 176 | + ); |
| 177 | + $automatic_attrs = array_merge( $manual_attrs, array( 'appSource' => 'subscribe-block-navigation' ) ); |
| 178 | + $manual_block = get_comment_delimited_block_content( 'jetpack/subscriptions', $manual_attrs, '' ); |
| 179 | + $cases = array( |
| 180 | + 'manual' => array( $manual_block, $manual_attrs ), |
| 181 | + 'automatic' => array( get_comment_delimited_block_content( 'jetpack/subscriptions', $automatic_attrs, '' ), $automatic_attrs ), |
| 182 | + 'nested' => array( '<!-- wp:navigation-submenu {"label":"More"} -->' . $manual_block . '<!-- /wp:navigation-submenu -->', $manual_attrs ), |
| 183 | + 'absent' => array( |
| 184 | + '', |
| 185 | + array( |
| 186 | + 'className' => 'is-style-button', |
| 187 | + 'appSource' => 'subscribe-block-navigation', |
| 188 | + ), |
| 189 | + ), |
| 190 | + ); |
| 191 | + |
| 192 | + $data = array(); |
| 193 | + foreach ( array( 'insert_hooked_blocks', 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' ) as $callback ) { |
| 194 | + foreach ( $cases as $name => $case ) { |
| 195 | + $data[ $name . ': ' . $callback ] = array_merge( $case, array( $callback ) ); |
| 196 | + } |
| 197 | + } |
| 198 | + return $data; |
| 199 | + } |
| 200 | +} |
0 commit comments