| | 1 | <?php |
| | 2 | /** |
| | 3 | * Test cases for the `do_enclose()` function. |
| | 4 | * |
| | 5 | * @package WordPress\UnitTests |
| | 6 | * |
| | 7 | * @since 4.9.7 |
| | 8 | */ |
| | 9 | |
| | 10 | /** |
| | 11 | * Tests_Functions_DoEnclose class. |
| | 12 | * |
| | 13 | * @group functions.php |
| | 14 | * @covers do_enclose |
| | 15 | * |
| | 16 | * @since 4.9.7 |
| | 17 | */ |
| | 18 | class Tests_Functions_DoEnclose extends WP_UnitTestCase { |
| | 19 | |
| | 20 | /** |
| | 21 | * Setup before each test method. |
| | 22 | * |
| | 23 | * @since 4.9.7 |
| | 24 | */ |
| | 25 | public function setUp() { |
| | 26 | parent::setUp(); |
| | 27 | add_filter( 'pre_http_request', array( $this, 'fake_http_request' ), 10, 3 ); |
| | 28 | } |
| | 29 | |
| | 30 | /** |
| | 31 | * Cleanup after each test method. |
| | 32 | * |
| | 33 | * @since 4.9.7 |
| | 34 | */ |
| | 35 | public function tearDown() { |
| | 36 | parent::tearDown(); |
| | 37 | remove_filter( 'pre_http_request', array( $this, 'fake_http_request' ) ); |
| | 38 | } |
| | 39 | |
| | 40 | /** |
| | 41 | * Test the function with an explicit content input. |
| | 42 | * |
| | 43 | * @since 4.9.7 |
| | 44 | * |
| | 45 | * @dataProvider data_test_do_enclose |
| | 46 | */ |
| | 47 | public function test_function_with_explicit_content_input( $content, $expected ) { |
| | 48 | $post_id = self::factory()->post->create(); |
| | 49 | |
| | 50 | do_enclose( $content, $post_id ); |
| | 51 | |
| | 52 | $actual = $this->get_enclosed_by_post_id( $post_id ); |
| | 53 | $this->assertSame( $expected, $actual ); |
| | 54 | } |
| | 55 | |
| | 56 | /** |
| | 57 | * Test the function with an implicit content input. |
| | 58 | * |
| | 59 | * @since 4.9.7 |
| | 60 | * |
| | 61 | * @dataProvider data_test_do_enclose |
| | 62 | */ |
| | 63 | public function test_function_with_implicit_content_input( $content, $expected ) { |
| | 64 | $post_id = self::factory()->post->create( |
| | 65 | array( |
| | 66 | 'post_content' => $content, |
| | 67 | ) |
| | 68 | ); |
| | 69 | |
| | 70 | do_enclose( null, $post_id ); |
| | 71 | |
| | 72 | $actual = $this->get_enclosed_by_post_id( $post_id ); |
| | 73 | $this->assertSame( $expected, $actual ); |
| | 74 | } |
| | 75 | |
| | 76 | /** |
| | 77 | * Dataprovider for `test_function_with_explicit_content_input()` |
| | 78 | * and `test_function_with_implicit_content_input()`. |
| | 79 | * |
| | 80 | * @since 4.9.7 |
| | 81 | * |
| | 82 | * @return array { |
| | 83 | * @type array { |
| | 84 | * @type string Post content. |
| | 85 | * @type string Expected values. |
| | 86 | * } |
| | 87 | * } |
| | 88 | */ |
| | 89 | public function data_test_do_enclose() { |
| | 90 | return array( |
| | 91 | 'null' => array( |
| | 92 | 'content' => null, |
| | 93 | 'expected' => '', |
| | 94 | ), |
| | 95 | 'empty' => array( |
| | 96 | 'content' => '', |
| | 97 | 'expected' => '', |
| | 98 | ), |
| | 99 | 'single-bare-movie' => array( |
| | 100 | 'content' => 'movie.mp4', |
| | 101 | 'expected' => '', |
| | 102 | ), |
| | 103 | 'single-bare-audio' => array( |
| | 104 | 'content' => 'audio.ogg', |
| | 105 | 'expected' => '', |
| | 106 | ), |
| | 107 | 'single-relative-movie' => array( |
| | 108 | 'content' => '/movie.mp4', |
| | 109 | 'expected' => "/movie.mp4\n123\nvideo/mp4\n", |
| | 110 | ), |
| | 111 | 'single-relative-audio' => array( |
| | 112 | 'content' => '/audio.ogg', |
| | 113 | 'expected' => "/audio.ogg\n321\naudio/ogg\n", |
| | 114 | ), |
| | 115 | 'single-unknown' => array( |
| | 116 | 'content' => 'https://example.com/wp-content/uploads/2018/06/file.unknown', |
| | 117 | 'expected' => '', |
| | 118 | ), |
| | 119 | 'single-movie' => array( |
| | 120 | 'content' => 'https://example.com/wp-content/uploads/2018/06/movie.mp4', |
| | 121 | 'expected' => "https://example.com/wp-content/uploads/2018/06/movie.mp4\n123\nvideo/mp4\n", |
| | 122 | ), |
| | 123 | 'single-audio' => array( |
| | 124 | 'content' => 'https://example.com/wp-content/uploads/2018/06/audio.ogg', |
| | 125 | 'expected' => "https://example.com/wp-content/uploads/2018/06/audio.ogg\n321\naudio/ogg\n", |
| | 126 | ), |
| | 127 | 'single-movie-query' => array( |
| | 128 | 'content' => 'https://example.com/wp-content/uploads/2018/06/movie.mp4?test=1', |
| | 129 | 'expected' => "https://example.com/wp-content/uploads/2018/06/movie.mp4?test=1\n123\nvideo/mp4\n", |
| | 130 | ), |
| | 131 | 'multi' => array( |
| | 132 | 'content' => "https://example.com/wp-content/uploads/2018/06/audio.ogg\n" . |
| | 133 | 'https://example.com/wp-content/uploads/2018/06/movie.mp4', |
| | 134 | 'expected' => "https://example.com/wp-content/uploads/2018/06/audio.ogg\n321\naudio/ogg\n" . |
| | 135 | "https://example.com/wp-content/uploads/2018/06/movie.mp4\n123\nvideo/mp4\n", |
| | 136 | ), |
| | 137 | ); |
| | 138 | } |
| | 139 | |
| | 140 | /** |
| | 141 | * The function should return false when the post ID input is invalid. |
| | 142 | * |
| | 143 | * @since 4.9.7 |
| | 144 | */ |
| | 145 | public function test_function_should_return_false_when_invalid_post_id() { |
| | 146 | $post_id = null; |
| | 147 | $result = do_enclose( null, $post_id ); |
| | 148 | $this->assertFalse( $result ); |
| | 149 | } |
| | 150 | |
| | 151 | /** |
| | 152 | * The function should delete an enclosed link when it's no longer in the post content. |
| | 153 | * |
| | 154 | * @since 4.9.7 |
| | 155 | */ |
| | 156 | public function test_function_should_delete_enclosed_link_when_no_longer_in_post_content() { |
| | 157 | $data = $this->data_test_do_enclose(); |
| | 158 | |
| | 159 | // Create a post with a single movie link. |
| | 160 | $post_id = self::factory()->post->create( |
| | 161 | array( |
| | 162 | 'post_content' => $data['single-movie']['content'], |
| | 163 | ) |
| | 164 | ); |
| | 165 | |
| | 166 | do_enclose( null, $post_id ); |
| | 167 | |
| | 168 | $actual = $this->get_enclosed_by_post_id( $post_id ); |
| | 169 | $this->assertSame( $data['single-movie']['expected'], $actual ); |
| | 170 | |
| | 171 | // Replace the movie link with an audio link. |
| | 172 | wp_update_post( |
| | 173 | array( |
| | 174 | 'ID' => $post_id, |
| | 175 | 'post_content' => $data['single-audio']['content'], |
| | 176 | ) |
| | 177 | ); |
| | 178 | |
| | 179 | do_enclose( null, $post_id ); |
| | 180 | |
| | 181 | $actual = $this->get_enclosed_by_post_id( $post_id ); |
| | 182 | $this->assertSame( $data['single-audio']['expected'], $actual ); |
| | 183 | } |
| | 184 | |
| | 185 | /** |
| | 186 | * The function should support a post object input. |
| | 187 | * |
| | 188 | * @since 4.9.7 |
| | 189 | */ |
| | 190 | public function test_function_should_support_post_object_input() { |
| | 191 | $data = $this->data_test_do_enclose(); |
| | 192 | |
| | 193 | $post_object = self::factory()->post->create_and_get( |
| | 194 | array( |
| | 195 | 'post_content' => $data['multi']['content'], |
| | 196 | ) |
| | 197 | ); |
| | 198 | |
| | 199 | do_enclose( null, $post_object ); |
| | 200 | |
| | 201 | $actual = $this->get_enclosed_by_post_id( $post_object->ID ); |
| | 202 | $this->assertSame( $data['multi']['expected'], $actual ); |
| | 203 | } |
| | 204 | |
| | 205 | /** |
| | 206 | * The enclosure links should be filterable with the `enclosure_links` filter. |
| | 207 | * |
| | 208 | * @since 4.9.7 |
| | 209 | */ |
| | 210 | public function test_function_enclosure_links_should_be_filterable() { |
| | 211 | $data = $this->data_test_do_enclose(); |
| | 212 | |
| | 213 | $post_id = self::factory()->post->create( |
| | 214 | array( |
| | 215 | 'post_content' => $data['multi']['content'], |
| | 216 | ) |
| | 217 | ); |
| | 218 | |
| | 219 | add_filter( 'enclosure_links', array( $this, 'filter_enclosure_links' ), 10, 2 ); |
| | 220 | do_enclose( null, $post_id ); |
| | 221 | remove_filter( 'enclosure_links', array( $this, 'filter_enclosure_links' ) ); |
| | 222 | |
| | 223 | $actual = $this->get_enclosed_by_post_id( $post_id ); |
| | 224 | $expected = str_replace( 'example.org', sprintf( 'example-%d.org', $post_id ), $data['multi']['expected'] ); |
| | 225 | $this->assertSame( $expected, $actual ); |
| | 226 | } |
| | 227 | |
| | 228 | /** |
| | 229 | * A callback to filter the list of enclosure links. |
| | 230 | * |
| | 231 | * @since 4.9.7 |
| | 232 | * |
| | 233 | * @param array $post_links An array of enclosure links. |
| | 234 | * @param int $post_id Post ID. |
| | 235 | * @return array $post_links An array of enclosure links. |
| | 236 | */ |
| | 237 | public function filter_enclosure_links( $enclosure_links, $post_id ) { |
| | 238 | // Replace the link host to contain the post ID, to test both filter input arguments. |
| | 239 | foreach ( $enclosure_links as &$link ) { |
| | 240 | $link = str_replace( 'example.org', sprintf( 'example-%d.org', $post_id ), $link ); |
| | 241 | } |
| | 242 | return $enclosure_links; |
| | 243 | } |
| | 244 | |
| | 245 | /** |
| | 246 | * Helper function to get all enclosure data for a given post. |
| | 247 | * |
| | 248 | * @since 4.9.7 |
| | 249 | * |
| | 250 | * @param int $post_id Post ID. |
| | 251 | * @return string All enclosure data for the given post. |
| | 252 | */ |
| | 253 | protected function get_enclosed_by_post_id( $post_id ) { |
| | 254 | return join( (array) get_post_meta( $post_id, 'enclosure', false ), '' ); |
| | 255 | } |
| | 256 | |
| | 257 | /** |
| | 258 | * Fake the HTTP request response. |
| | 259 | * |
| | 260 | * @since 4.9.7 |
| | 261 | * |
| | 262 | * @param bool $false False. |
| | 263 | * @param array $arguments Request arguments. |
| | 264 | * @param string $url Request URL. |
| | 265 | * |
| | 266 | * @return array Header. |
| | 267 | */ |
| | 268 | public function fake_http_request( $false, $arguments, $url ) { |
| | 269 | |
| | 270 | // Video and audio headers. |
| | 271 | $fake_headers = array( |
| | 272 | 'mp4' => array( |
| | 273 | 'headers' => array( |
| | 274 | 'content-length' => 123, |
| | 275 | 'content-type' => 'video/mp4', |
| | 276 | ), |
| | 277 | ), |
| | 278 | 'ogg' => array( |
| | 279 | 'headers' => array( |
| | 280 | 'content-length' => 321, |
| | 281 | 'content-type' => 'audio/ogg', |
| | 282 | ), |
| | 283 | ), |
| | 284 | ); |
| | 285 | |
| | 286 | $path = parse_url( $url, PHP_URL_PATH ); |
| | 287 | |
| | 288 | if ( false !== $path ) { |
| | 289 | $extension = pathinfo( $path, PATHINFO_EXTENSION ); |
| | 290 | if ( isset( $fake_headers[ $extension ] ) ) { |
| | 291 | return $fake_headers[ $extension ]; |
| | 292 | } |
| | 293 | } |
| | 294 | |
| | 295 | // Fallback header. |
| | 296 | return array( |
| | 297 | 'headers' => array( |
| | 298 | 'content-length' => 0, |
| | 299 | 'content-type' => '', |
| | 300 | ), |
| | 301 | ); |
| | 302 | } |
| | 303 | |
| | 304 | } |