| 310 | |
| 311 | /** |
| 312 | * @ticket 23219 |
| 313 | */ |
| 314 | function test_add_enclosure_if_new() { |
| 315 | // Sample enclosure data |
| 316 | $enclosure = array( |
| 317 | 'url' => 'http://example.com/sound.mp3', |
| 318 | 'length' => 12345, |
| 319 | 'type' => 'audio/mpeg', |
| 320 | ); |
| 321 | |
| 322 | // Second sample enclosure data array |
| 323 | $new_enclosure = array( |
| 324 | 'url' => 'http://example.com/sound2.mp3', |
| 325 | 'length' => 12345, |
| 326 | 'type' => 'audio/mpeg', |
| 327 | ); |
| 328 | |
| 329 | // Create a test user |
| 330 | $editor_id = $this->make_user_by_role( 'editor' ); |
| 331 | |
| 332 | // Add a dummy post |
| 333 | $post_id = $this->factory->post->create( array( |
| 334 | 'post_title' => 'Post Enclosure Test', |
| 335 | 'post_content' => 'Fake content', |
| 336 | 'post_author' => $editor_id, |
| 337 | 'post_status' => 'publish', |
| 338 | ) ); |
| 339 | |
| 340 | // Add the enclosure as it is added in "do_enclose()" |
| 341 | $enclosure_string = "{$enclosure['url']}\n{$enclosure['length']}\n{$enclosure['type']}\n"; |
| 342 | add_post_meta( $post_id, 'enclosure', $enclosure_string ); |
| 343 | |
| 344 | // Verify that the correct data is there |
| 345 | $this->assertEquals( $enclosure_string, get_post_meta( $post_id, 'enclosure', true ) ); |
| 346 | |
| 347 | // Attempt to add the enclosure a second time |
| 348 | $this->myxmlrpcserver->add_enclosure_if_new( $post_id, $enclosure ); |
| 349 | |
| 350 | // Verify that there is only a single value in the array and that a duplicate is not present |
| 351 | $this->assertEquals( 1, count( get_post_meta( $post_id, 'enclosure' ) ) ); |
| 352 | |
| 353 | // For good measure, check that the expected value is in the array |
| 354 | $this->assertTrue( in_array( $enclosure_string, get_post_meta( $post_id, 'enclosure' ) ) ); |
| 355 | |
| 356 | // Attempt to add a brand new enclosure via XML-RPC |
| 357 | $this->myxmlrpcserver->add_enclosure_if_new( $post_id, $new_enclosure ); |
| 358 | |
| 359 | // Having added the new enclosure, 2 values are expected in the array |
| 360 | $this->assertEquals( 2, count( get_post_meta( $post_id, 'enclosure' ) ) ); |
| 361 | |
| 362 | // Check that the new enclosure is in the enclosure meta |
| 363 | $new_enclosure_string = "{$new_enclosure['url']}\n{$new_enclosure['length']}\n{$new_enclosure['type']}\n"; |
| 364 | $this->assertTrue( in_array( $new_enclosure_string, get_post_meta( $post_id, 'enclosure' ) ) ); |
| 365 | |
| 366 | // Check that the old enclosure is in the enclosure meta |
| 367 | $this->assertTrue( in_array( $enclosure_string, get_post_meta( $post_id, 'enclosure' ) ) ); |
| 368 | } |