|
7 | 7 |
|
8 | 8 | use Newspack\RSS; |
9 | 9 | use Newspack\Optional_Modules; |
| 10 | +use Newspack\RSS_Add_Image; |
10 | 11 |
|
| 12 | +require_once __DIR__ . '/../mocks/filter-input-mock.php'; |
11 | 13 | /** |
12 | 14 | * Tests the RSS core functionality. |
13 | 15 | */ |
@@ -123,6 +125,37 @@ private function set_test_settings( $settings ) { |
123 | 125 | $this->current_test_settings = $settings; |
124 | 126 | } |
125 | 127 |
|
| 128 | + /** |
| 129 | + * Render the extra RSS tags for a post with the provided settings overrides. |
| 130 | + * |
| 131 | + * @param WP_Post $post Post object. |
| 132 | + * @param array $settings Settings overrides for the feed. |
| 133 | + * @return string Captured markup from RSS::add_extra_tags(). |
| 134 | + */ |
| 135 | + private function render_extra_tags_for_post( $post, $settings ) { |
| 136 | + $_GET['partner-feed'] = 'test-rss-feed'; |
| 137 | + $this->set_test_settings( |
| 138 | + array_merge( |
| 139 | + [ |
| 140 | + 'num_items_in_feed' => 10, |
| 141 | + ], |
| 142 | + $settings |
| 143 | + ) |
| 144 | + ); |
| 145 | + |
| 146 | + $GLOBALS['post'] = $post; |
| 147 | + setup_postdata( $post ); |
| 148 | + |
| 149 | + ob_start(); |
| 150 | + RSS::add_extra_tags(); |
| 151 | + $output = ob_get_clean(); |
| 152 | + |
| 153 | + wp_reset_postdata(); |
| 154 | + unset( $_GET['partner-feed'] ); |
| 155 | + |
| 156 | + return $output; |
| 157 | + } |
| 158 | + |
126 | 159 | /** |
127 | 160 | * Test default taxonomy_filters_relation setting. |
128 | 161 | */ |
@@ -1001,4 +1034,185 @@ public function test_rss_complex_taxonomy_filtering() { |
1001 | 1034 | $this->assertEquals( 1, $query->found_posts, 'Should find exactly one post' ); |
1002 | 1035 | $this->assertEquals( $post1, $query->posts[0]->ID, 'Should return the post with all three taxonomy terms' ); |
1003 | 1036 | } |
| 1037 | + |
| 1038 | + /** |
| 1039 | + * Test that RSS::add_extra_tags() applies the newspack_rss_image_size filter for <image> markup. |
| 1040 | + */ |
| 1041 | + public function test_add_extra_tags_applies_image_size_filter() { |
| 1042 | + $post_id = $this->factory()->post->create(); |
| 1043 | + $attachment_id = $this->factory()->attachment->create_object( 'image.jpg', $post_id, [ 'post_mime_type' => 'image/jpeg' ] ); |
| 1044 | + set_post_thumbnail( $post_id, $attachment_id ); |
| 1045 | + |
| 1046 | + $post = get_post( $post_id ); |
| 1047 | + |
| 1048 | + $filtered = false; |
| 1049 | + $size_used = null; |
| 1050 | + |
| 1051 | + $image_size_filter = function ( $size, $settings, $filter_post ) use ( &$filtered ) { |
| 1052 | + $filtered = true; |
| 1053 | + $this->assertEquals( RSS_Add_Image::RSS_IMAGE_SIZE, $size, 'Expected image size filter to be applied for media tags.' ); |
| 1054 | + $this->assertIsArray( $settings, 'Expected settings to be an array.' ); |
| 1055 | + $this->assertInstanceOf( 'WP_Post', $filter_post, 'Expected filter post to be a WP_Post object.' ); |
| 1056 | + return 'custom-rss-size'; |
| 1057 | + }; |
| 1058 | + |
| 1059 | + $image_src_tracker = function ( $image, $attachment_id_param, $size ) use ( &$size_used, $attachment_id ) { |
| 1060 | + if ( $attachment_id === $attachment_id_param && null === $size_used ) { |
| 1061 | + $size_used = $size; |
| 1062 | + } |
| 1063 | + return $image; |
| 1064 | + }; |
| 1065 | + |
| 1066 | + add_filter( 'newspack_rss_image_size', $image_size_filter, 10, 3 ); |
| 1067 | + add_filter( 'wp_get_attachment_image_src', $image_src_tracker, 10, 3 ); |
| 1068 | + |
| 1069 | + $output = $this->render_extra_tags_for_post( |
| 1070 | + $post, |
| 1071 | + [ |
| 1072 | + 'use_image_tags' => true, |
| 1073 | + ] |
| 1074 | + ); |
| 1075 | + |
| 1076 | + remove_filter( 'newspack_rss_image_size', $image_size_filter, 10 ); |
| 1077 | + remove_filter( 'wp_get_attachment_image_src', $image_src_tracker, 10 ); |
| 1078 | + |
| 1079 | + $this->assertTrue( $filtered, 'Expected newspack_rss_image_size filter to run.' ); |
| 1080 | + $this->assertSame( 'custom-rss-size', $size_used, 'Expected image size filter value to be used.' ); |
| 1081 | + $this->assertStringContainsString( '<image>', $output, 'Expected image tag markup in output.' ); |
| 1082 | + } |
| 1083 | + |
| 1084 | + /** |
| 1085 | + * Test that RSS::add_extra_tags() allows replacing <tags> markup via newspack_rss_tags_output. |
| 1086 | + */ |
| 1087 | + public function test_add_extra_tags_supports_custom_tags_output() { |
| 1088 | + $category_id = $this->factory()->term->create( |
| 1089 | + [ |
| 1090 | + 'taxonomy' => 'category', |
| 1091 | + 'name' => 'TestCat', |
| 1092 | + ] |
| 1093 | + ); |
| 1094 | + $tag_id = $this->factory()->term->create( |
| 1095 | + [ |
| 1096 | + 'taxonomy' => 'post_tag', |
| 1097 | + 'name' => 'TestTag', |
| 1098 | + ] |
| 1099 | + ); |
| 1100 | + |
| 1101 | + $post_id = $this->factory()->post->create( |
| 1102 | + [ |
| 1103 | + 'post_category' => [ $category_id ], |
| 1104 | + ] |
| 1105 | + ); |
| 1106 | + wp_set_object_terms( $post_id, [ $tag_id ], 'post_tag' ); |
| 1107 | + $post = get_post( $post_id ); |
| 1108 | + |
| 1109 | + $filtered = false; |
| 1110 | + $tags_filter = function ( $output, $all_terms, $settings, $filter_post ) use ( &$filtered ) { |
| 1111 | + $filtered = true; |
| 1112 | + $this->assertIsString( $output, 'Expected output to be a string.' ); |
| 1113 | + $this->assertIsArray( $all_terms, 'Expected all terms to be an array.' ); |
| 1114 | + $this->assertIsArray( $settings, 'Expected settings to be an array.' ); |
| 1115 | + $this->assertInstanceOf( 'WP_Post', $filter_post, 'Expected filter post to be a WP_Post object.' ); |
| 1116 | + |
| 1117 | + $nested = ''; |
| 1118 | + foreach ( $all_terms as $term ) { |
| 1119 | + $nested .= '<tag>' . esc_html( $term->name ) . '</tag>'; |
| 1120 | + } |
| 1121 | + return $nested; |
| 1122 | + }; |
| 1123 | + |
| 1124 | + add_filter( 'newspack_rss_tags_output', $tags_filter, 10, 4 ); |
| 1125 | + |
| 1126 | + $output = $this->render_extra_tags_for_post( |
| 1127 | + $post, |
| 1128 | + [ |
| 1129 | + 'use_tags_tags' => true, |
| 1130 | + ] |
| 1131 | + ); |
| 1132 | + $normalized_output = preg_replace( '/\s+/', '', $output ); |
| 1133 | + |
| 1134 | + remove_filter( 'newspack_rss_tags_output', $tags_filter, 10 ); |
| 1135 | + |
| 1136 | + $this->assertTrue( $filtered, 'Expected newspack_rss_tags_output filter to run.' ); |
| 1137 | + $this->assertStringContainsString( '<tags><tag>TestCat</tag><tag>TestTag</tag></tags>', $normalized_output, 'Expected tags output to be normalized.' ); |
| 1138 | + } |
| 1139 | + |
| 1140 | + /** |
| 1141 | + * Test that RSS::add_extra_tags() applies media filters and fires the related actions. |
| 1142 | + */ |
| 1143 | + public function test_add_extra_tags_applies_media_filters_and_actions() { |
| 1144 | + $post_id = $this->factory()->post->create(); |
| 1145 | + $attachment_id = $this->factory()->attachment->create_object( 'image.jpg', $post_id, [ 'post_mime_type' => 'image/jpeg' ] ); |
| 1146 | + set_post_thumbnail( $post_id, $attachment_id ); |
| 1147 | + wp_update_post( |
| 1148 | + [ |
| 1149 | + 'ID' => $attachment_id, |
| 1150 | + 'post_excerpt' => 'Media caption', |
| 1151 | + ] |
| 1152 | + ); |
| 1153 | + |
| 1154 | + $post = get_post( $post_id ); |
| 1155 | + |
| 1156 | + $image_size_filtered = false; |
| 1157 | + $size_used = null; |
| 1158 | + $media_url_filtered = false; |
| 1159 | + $media_url = 'https://example.com/media-filtered.jpg'; |
| 1160 | + $media_action = new \MockAction(); |
| 1161 | + $after_action = new \MockAction(); |
| 1162 | + |
| 1163 | + $image_size_filter = function ( $size, $settings, $filter_post ) use ( &$image_size_filtered ) { |
| 1164 | + $image_size_filtered = true; |
| 1165 | + $this->assertIsArray( $settings, 'Expected settings to be an array.' ); |
| 1166 | + $this->assertInstanceOf( 'WP_Post', $filter_post, 'Expected filter post to be a WP_Post object.' ); |
| 1167 | + return 'media-custom-size'; |
| 1168 | + }; |
| 1169 | + |
| 1170 | + $image_src_tracker = function ( $image, $attachment_id_param, $size ) use ( &$size_used, $attachment_id ) { |
| 1171 | + if ( $attachment_id === $attachment_id_param && null === $size_used ) { |
| 1172 | + $size_used = $size; |
| 1173 | + } |
| 1174 | + return $image; |
| 1175 | + }; |
| 1176 | + |
| 1177 | + $media_url_filter = function ( $url, $thumbnail_id, $settings, $filter_post ) use ( &$media_url_filtered, $media_url, $attachment_id ) { |
| 1178 | + $media_url_filtered = true; |
| 1179 | + $this->assertEquals( $attachment_id, $thumbnail_id, 'Expected attachment ID to match.' ); |
| 1180 | + $this->assertIsArray( $settings, 'Expected settings to be an array.' ); |
| 1181 | + $this->assertInstanceOf( 'WP_Post', $filter_post, 'Expected filter post to be a WP_Post object.' ); |
| 1182 | + return $media_url; |
| 1183 | + }; |
| 1184 | + |
| 1185 | + add_filter( 'newspack_rss_image_size', $image_size_filter, 10, 3 ); |
| 1186 | + add_filter( 'wp_get_attachment_image_src', $image_src_tracker, 10, 3 ); |
| 1187 | + add_filter( 'newspack_rss_media_content_url', $media_url_filter, 10, 4 ); |
| 1188 | + |
| 1189 | + add_action( 'newspack_rss_after_media_content', [ $media_action, 'action' ], 10, 5 ); |
| 1190 | + add_action( 'newspack_rss_after_extra_tags', [ $after_action, 'action' ], 10, 2 ); |
| 1191 | + |
| 1192 | + $output = $this->render_extra_tags_for_post( |
| 1193 | + $post, |
| 1194 | + [ |
| 1195 | + 'use_media_tags' => true, |
| 1196 | + ] |
| 1197 | + ); |
| 1198 | + |
| 1199 | + remove_filter( 'newspack_rss_image_size', $image_size_filter, 10 ); |
| 1200 | + remove_filter( 'wp_get_attachment_image_src', $image_src_tracker, 10 ); |
| 1201 | + remove_filter( 'newspack_rss_media_content_url', $media_url_filter, 10 ); |
| 1202 | + remove_action( 'newspack_rss_after_media_content', [ $media_action, 'action' ], 10 ); |
| 1203 | + remove_action( 'newspack_rss_after_extra_tags', [ $after_action, 'action' ], 10 ); |
| 1204 | + |
| 1205 | + $this->assertTrue( $image_size_filtered, 'Expected image size filter to be applied for media tags.' ); |
| 1206 | + $this->assertSame( 'media-custom-size', $size_used, 'Expected size used to be the custom size.' ); |
| 1207 | + $this->assertTrue( $media_url_filtered, 'Expected media content URL filter to run.' ); |
| 1208 | + $this->assertSame( 1, $media_action->get_call_count(), 'Expected after media content action to fire once.' ); |
| 1209 | + $media_args = $media_action->get_args()[0]; |
| 1210 | + $this->assertEquals( $attachment_id, $media_args[0], 'Expected attachment ID to match.' ); |
| 1211 | + $this->assertIsArray( $media_args[1], 'Expected media args to be an array.' ); |
| 1212 | + $this->assertIsString( $media_args[2], 'Expected media args to be a string.' ); |
| 1213 | + $this->assertIsArray( $media_args[3], 'Expected media args to be an array.' ); |
| 1214 | + $this->assertInstanceOf( 'WP_Post', $media_args[4], 'Expected media args to be a WP_Post object.' ); |
| 1215 | + $this->assertSame( 1, $after_action->get_call_count(), 'Expected after extra tags action to fire once.' ); |
| 1216 | + $this->assertStringContainsString( 'url="' . esc_url( $media_url ) . '"', $output, 'Expected media URL to be in output.' ); |
| 1217 | + } |
1004 | 1218 | } |
0 commit comments