Ticket #45708: 45708.1.diff
| File 45708.1.diff, 19.9 KB (added by , 4 years ago) |
|---|
-
phpcs.xml.dist
diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 0f15b16849..d09abc947c 100644
a b 249 249 <element value="WP_Import_UnitTestCase"/> 250 250 <element value="Tests_Query_Conditionals"/> 251 251 <element value="WP_Test_XML_TestCase"/> 252 <element value="WP_Test_Adjacent_Image_Link_TestCase"/> 252 253 253 254 <!-- Mock classes. --> 254 255 <element value="Spy_REST_Server"/> -
src/wp-includes/media.php
diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 19fa2b8b9d..4a9849d3ca 100644
a b function wp_video_shortcode( $attr, $content = '' ) { 3376 3376 } 3377 3377 add_shortcode( 'video', 'wp_video_shortcode' ); 3378 3378 3379 /** 3380 * Gets the previous image link that has the same post parent. 3381 * 3382 * @since 5.8.0 3383 * 3384 * @see get_adjacent_image_link() 3385 * 3386 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array 3387 * of width and height values in pixels (in that order). Default 'thumbnail'. 3388 * @param string|false $text Optional. Link text. Default false. 3389 * @return string Markup for previous image link. 3390 */ 3391 function get_previous_image_link( $size = 'thumbnail', $text = false ) { 3392 return get_adjacent_image_link( true, $size, $text ); 3393 } 3394 3379 3395 /** 3380 3396 * Displays previous image link that has the same post parent. 3381 3397 * 3382 3398 * @since 2.5.0 3383 3399 * 3384 * @see adjacent_image_link()3385 *3386 3400 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array 3387 3401 * of width and height values in pixels (in that order). Default 'thumbnail'. 3388 3402 * @param string|false $text Optional. Link text. Default false. 3389 3403 */ 3390 3404 function previous_image_link( $size = 'thumbnail', $text = false ) { 3391 adjacent_image_link( true, $size, $text ); 3405 echo get_previous_image_link( $size, $text ); 3406 } 3407 3408 /** 3409 * Gets the next image link that has the same post parent. 3410 * 3411 * @since 5.8.0 3412 * 3413 * @see get_adjacent_image_link() 3414 * 3415 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array 3416 * of width and height values in pixels (in that order). Default 'thumbnail'. 3417 * @param string|false $text Optional. Link text. Default false. 3418 * @return string Markup for next image link. 3419 */ 3420 function get_next_image_link( $size = 'thumbnail', $text = false ) { 3421 return get_adjacent_image_link( false, $size, $text ); 3392 3422 } 3393 3423 3394 3424 /** … … function previous_image_link( $size = 'thumbnail', $text = false ) { 3396 3426 * 3397 3427 * @since 2.5.0 3398 3428 * 3399 * @see adjacent_image_link()3400 *3401 3429 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array 3402 3430 * of width and height values in pixels (in that order). Default 'thumbnail'. 3403 3431 * @param string|false $text Optional. Link text. Default false. 3404 3432 */ 3405 3433 function next_image_link( $size = 'thumbnail', $text = false ) { 3406 adjacent_image_link( false,$size, $text );3434 echo get_next_image_link( $size, $text ); 3407 3435 } 3408 3436 3409 3437 /** 3410 * Displaysnext or previous image link that has the same post parent.3438 * Gets the next or previous image link that has the same post parent. 3411 3439 * 3412 3440 * Retrieves the current attachment object from the $post global. 3413 3441 * 3414 * @since 2.5.03442 * @since 5.8.0 3415 3443 * 3416 3444 * @param bool $prev Optional. Whether to display the next (false) or previous (true) link. Default true. 3417 3445 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array 3418 3446 * of width and height values in pixels (in that order). Default 'thumbnail'. 3419 3447 * @param bool $text Optional. Link text. Default false. 3448 * @return string Markup for image link. 3420 3449 */ 3421 function adjacent_image_link( $prev = true, $size = 'thumbnail', $text = false ) {3450 function get_adjacent_image_link( $prev = true, $size = 'thumbnail', $text = false ) { 3422 3451 $post = get_post(); 3423 3452 $attachments = array_values( 3424 3453 get_children( … … function adjacent_image_link( $prev = true, $size = 'thumbnail', $text = false ) 3473 3502 * an array of width and height values in pixels (in that order). 3474 3503 * @param string $text Link text. 3475 3504 */ 3476 echo apply_filters( "{$adjacent}_image_link", $output, $attachment_id, $size, $text ); 3505 return apply_filters( "{$adjacent}_image_link", $output, $attachment_id, $size, $text ); 3506 } 3507 3508 /** 3509 * Displays next or previous image link that has the same post parent. 3510 * 3511 * Retrieves the current attachment object from the $post global. 3512 * 3513 * @since 2.5.0 3514 * 3515 * @param bool $prev Optional. Whether to display the next (false) or previous (true) link. Default true. 3516 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array 3517 * of width and height values in pixels (in that order). Default 'thumbnail'. 3518 * @param bool $text Optional. Link text. Default false. 3519 */ 3520 function adjacent_image_link( $prev = true, $size = 'thumbnail', $text = false ) { 3521 echo get_adjacent_image_link( $prev, $size, $text ); 3477 3522 } 3478 3523 3479 3524 /** -
new file tests/phpunit/tests/media/getAdjacentImageLink.php
diff --git a/tests/phpunit/tests/media/getAdjacentImageLink.php b/tests/phpunit/tests/media/getAdjacentImageLink.php new file mode 100644 index 0000000000..3f3d6c3c28
- + 1 <?php 2 3 require_once __DIR__ . '/testcase-adjacent-image-link.php'; 4 5 /** 6 * @group media 7 * @covers ::get_adjacent_image_link 8 */ 9 class Tests_Media_GetAdjacentImageLink extends WP_Test_Adjacent_Image_Link_TestCase { 10 protected $default_args = array( 11 'prev' => true, 12 'size' => 'thumbnail', 13 'text' => false, 14 ); 15 16 /** 17 * @ticket 45708 18 * 19 * @dataProvider data_get_adjacent_image_link 20 */ 21 function test_get_adjacent_image_link( $current_attachment_index, $expected_attachment_index, $expected, array $args = array() ) { 22 list( $expected, $args ) = $this->setup_test_scenario( $current_attachment_index, $expected_attachment_index, $expected, $args ); 23 24 $actual = get_adjacent_image_link( ...$args ); 25 26 $this->assertSame( $expected, $actual ); 27 } 28 29 public function data_get_adjacent_image_link() { 30 return array( 31 // Happy paths. 32 'when has previous link' => array( 33 'current_attachment_index' => 3, 34 'expected_attachment_index' => 2, 35 'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src="http://example.org/wp-content/uploads/image2.jpg" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" /></a>', 36 ), 37 'with text when has previous link' => array( 38 'current_attachment_index' => 3, 39 'expected_attachment_index' => 2, 40 'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'>Some text</a>', 41 'args' => array( 'text' => 'Some text' ), 42 ), 43 'when has next link' => array( 44 'current_attachment_index' => 4, 45 'expected_attachment_index' => 5, 46 'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src="http://example.org/wp-content/uploads/image5.jpg" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" /></a>', 47 'args' => array( 'prev' => false ), 48 ), 49 'with text when has next link' => array( 50 'current_attachment_index' => 4, 51 'expected_attachment_index' => 5, 52 'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'>Some text</a>', 53 'args' => array( 54 'prev' => false, 55 'text' => 'Some text', 56 ), 57 ), 58 59 // Unhappy paths. 60 'when no previous link' => array( 61 'current_attachment_index' => 1, 62 'expected_attachment_index' => 0, 63 'expected' => '', 64 ), 65 'with text when no previous link' => array( 66 'current_attachment_index' => 1, 67 'expected_attachment_index' => 0, 68 'expected' => '', 69 'args' => array( 'text' => 'Some text' ), 70 ), 71 'when no next link' => array( 72 'current_attachment_index' => 5, 73 'expected_attachment_index' => 0, 74 'expected' => '', 75 'args' => array( 'prev' => false ), 76 ), 77 'with text when no next link' => array( 78 'current_attachment_index' => 5, 79 'expected_attachment_index' => 0, 80 'expected' => '', 81 'args' => array( 82 'prev' => false, 83 'text' => 'Some text', 84 ), 85 ), 86 ); 87 } 88 } -
new file tests/phpunit/tests/media/getNextImageLink.php
diff --git a/tests/phpunit/tests/media/getNextImageLink.php b/tests/phpunit/tests/media/getNextImageLink.php new file mode 100644 index 0000000000..484f13ddf9
- + 1 <?php 2 3 require_once __DIR__ . '/testcase-adjacent-image-link.php'; 4 5 /** 6 * @group media 7 * @covers ::get_next_image_link 8 */ 9 class Tests_Media_GetNextImageLink extends WP_Test_Adjacent_Image_Link_TestCase { 10 protected $default_args = array( 11 'size' => 'thumbnail', 12 'text' => false, 13 ); 14 15 /** 16 * @ticket 45708 17 * 18 * @dataProvider data_get_next_image_link 19 */ 20 function test_get_next_image_link( $current_attachment_index, $expected_attachment_index = 0, $expected, array $args = array() ) { 21 list( $expected, $args ) = $this->setup_test_scenario( $current_attachment_index, $expected_attachment_index, $expected, $args ); 22 23 $actual = get_next_image_link( ...$args ); 24 25 $this->assertSame( $expected, $actual ); 26 } 27 28 public function data_get_next_image_link() { 29 return array( 30 // Happy paths. 31 'when has next link' => array( 32 'current_attachment_index' => 4, 33 'expected_attachment_index' => 5, 34 'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src="http://example.org/wp-content/uploads/image5.jpg" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" /></a>', 35 ), 36 'with text when has next link' => array( 37 'current_attachment_index' => 4, 38 'expected_attachment_index' => 5, 39 'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'>Some text</a>', 40 'args' => array( 'text' => 'Some text' ), 41 ), 42 43 // Unhappy paths. 44 'when no next link' => array( 45 'current_attachment_index' => 5, 46 'expected_attachment_index' => 0, 47 'expected' => '', 48 ), 49 'with text when no next link' => array( 50 'current_attachment_index' => 5, 51 'expected_attachment_index' => 0, 52 'expected' => '', 53 'args' => array( 'text' => 'Some text' ), 54 ), 55 ); 56 } 57 } -
new file tests/phpunit/tests/media/getPreviousImageLink.php
diff --git a/tests/phpunit/tests/media/getPreviousImageLink.php b/tests/phpunit/tests/media/getPreviousImageLink.php new file mode 100644 index 0000000000..9c98c883cc
- + 1 <?php 2 3 require_once __DIR__ . '/testcase-adjacent-image-link.php'; 4 5 /** 6 * @group media 7 * @covers ::get_previous_image_link 8 */ 9 class Tests_Media_GetPreviousImageLink extends WP_Test_Adjacent_Image_Link_TestCase { 10 protected $default_args = array( 11 'size' => 'thumbnail', 12 'text' => false, 13 ); 14 15 /** 16 * @ticket 45708 17 * 18 * @dataProvider data_get_previous_image_link 19 */ 20 function test_get_previous_image_link( $current_attachment_index, $expected_attachment_index = 0, $expected, array $args = array() ) { 21 list( $expected, $args ) = $this->setup_test_scenario( $current_attachment_index, $expected_attachment_index, $expected, $args ); 22 23 $actual = get_previous_image_link( ...$args ); 24 25 $this->assertSame( $expected, $actual ); 26 } 27 28 public function data_get_previous_image_link() { 29 return array( 30 // Happy paths. 31 'when has previous link' => array( 32 'current_attachment_index' => 3, 33 'expected_attachment_index' => 2, 34 'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src="http://example.org/wp-content/uploads/image2.jpg" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" /></a>', 35 ), 36 'with text when has previous link' => array( 37 'current_attachment_index' => 3, 38 'expected_attachment_index' => 2, 39 'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'>Some text</a>', 40 'args' => array( 'text' => 'Some text' ), 41 ), 42 43 // Unhappy paths. 44 'when no previous link' => array( 45 'current_attachment_index' => 1, 46 'expected_attachment_index' => 0, 47 'expected' => '', 48 ), 49 'with text when no previous link' => array( 50 'current_attachment_index' => 1, 51 'expected_attachment_index' => 0, 52 'expected' => '', 53 'args' => array( 'text' => 'Some text' ), 54 ), 55 ); 56 } 57 } -
new file tests/phpunit/tests/media/nextImageLink.php
diff --git a/tests/phpunit/tests/media/nextImageLink.php b/tests/phpunit/tests/media/nextImageLink.php new file mode 100644 index 0000000000..fc4527f49e
- + 1 <?php 2 3 require_once __DIR__ . '/testcase-adjacent-image-link.php'; 4 5 /** 6 * @group media 7 * @covers ::next_image_link 8 */ 9 class Tests_Media_NextImageLink extends WP_Test_Adjacent_Image_Link_TestCase { 10 protected $default_args = array( 11 'size' => 'thumbnail', 12 'text' => false, 13 ); 14 15 /** 16 * @ticket 45708 17 * 18 * @dataProvider data_next_image_link 19 */ 20 function test_next_image_link( $current_attachment_index, $expected_attachment_index = 0, $expected, array $args = array() ) { 21 list( $expected, $args ) = $this->setup_test_scenario( $current_attachment_index, $expected_attachment_index, $expected, $args ); 22 23 $this->expectOutputString( $expected ); 24 $this->assertNull( next_image_link( ...$args ) ); 25 } 26 27 public function data_next_image_link() { 28 return array( 29 // Happy paths. 30 'when has next link' => array( 31 'current_attachment_index' => 4, 32 'expected_attachment_index' => 5, 33 'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src="http://example.org/wp-content/uploads/image5.jpg" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" /></a>', 34 ), 35 'with text when has next link' => array( 36 'current_attachment_index' => 4, 37 'expected_attachment_index' => 5, 38 'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'>Some text</a>', 39 'args' => array( 'text' => 'Some text' ), 40 ), 41 42 // Unhappy paths. 43 'when no next link' => array( 44 'current_attachment_index' => 5, 45 'expected_attachment_index' => 0, 46 'expected' => '', 47 ), 48 'with text when no next link' => array( 49 'current_attachment_index' => 5, 50 'expected_attachment_index' => 0, 51 'expected' => '', 52 'args' => array( 'text' => 'Some text' ), 53 ), 54 ); 55 } 56 } -
new file tests/phpunit/tests/media/previousImageLink.php
diff --git a/tests/phpunit/tests/media/previousImageLink.php b/tests/phpunit/tests/media/previousImageLink.php new file mode 100644 index 0000000000..7cdb24e0bf
- + 1 <?php 2 3 require_once __DIR__ . '/testcase-adjacent-image-link.php'; 4 5 /** 6 * @group media 7 * @covers ::previous_image_link 8 */ 9 class Tests_Media_PreviousImageLink extends WP_Test_Adjacent_Image_Link_TestCase { 10 protected $default_args = array( 11 'size' => 'thumbnail', 12 'text' => false, 13 ); 14 15 /** 16 * @ticket 45708 17 * 18 * @dataProvider data_previous_image_link 19 */ 20 function test_previous_image_link( $current_attachment_index, $expected_attachment_index = 0, $expected, array $args = array() ) { 21 list( $expected, $args ) = $this->setup_test_scenario( $current_attachment_index, $expected_attachment_index, $expected, $args ); 22 23 $this->expectOutputString( $expected ); 24 $this->assertNull( previous_image_link( ...$args ) ); 25 } 26 27 public function data_previous_image_link() { 28 return array( 29 // Happy paths. 30 'when has previous link' => array( 31 'current_attachment_index' => 3, 32 'expected_attachment_index' => 2, 33 'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src="http://example.org/wp-content/uploads/image2.jpg" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" /></a>', 34 ), 35 'with text when has previous link' => array( 36 'current_attachment_index' => 3, 37 'expected_attachment_index' => 2, 38 'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'>Some text</a>', 39 'args' => array( 'text' => 'Some text' ), 40 ), 41 42 // Unhappy paths. 43 'when no previous link' => array( 44 'current_attachment_index' => 1, 45 'expected_attachment_index' => 0, 46 'expected' => '', 47 ), 48 'with text when no previous link' => array( 49 'current_attachment_index' => 1, 50 'expected_attachment_index' => 0, 51 'expected' => '', 52 'args' => array( 'text' => 'Some text' ), 53 ), 54 ); 55 } 56 } -
new file tests/phpunit/tests/media/testcase-adjacent-image-link.php
diff --git a/tests/phpunit/tests/media/testcase-adjacent-image-link.php b/tests/phpunit/tests/media/testcase-adjacent-image-link.php new file mode 100644 index 0000000000..57ba20c08e
- + 1 <?php 2 3 abstract class WP_Test_Adjacent_Image_Link_TestCase extends WP_UnitTestCase { 4 /** 5 * Array of 5 attachments for use in the tests. 6 * 7 * @var init{}|WP_Error[] 8 */ 9 protected static $attachments; 10 11 /** 12 * Default args for the function being tested. 13 * 14 * Defined in each test class. 15 * 16 * @var int[]|WP_Error[] Array of attachment IDs. 17 */ 18 protected $default_args = array(); 19 20 /** 21 * Setup the tests after the data provider but before the tests start. 22 * 23 * @param WP_UnitTest_Factory $factory Instance of the factory. 24 */ 25 public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 26 $parent_id = $factory->post->create(); 27 28 for ( $index = 1; $index <= 5; $index++ ) { 29 self::$attachments[ $index ] = $factory->attachment->create_object( 30 "image{$index}.jpg", 31 $parent_id, 32 array( 33 'post_mime_type' => 'image/jpeg', 34 'post_type' => 'attachment', 35 ) 36 ); 37 } 38 } 39 40 /** 41 * Sets up the test scenario. 42 * 43 * @param integer $current_attachment_index Current attachment's index number in the self::$attachments array. 44 * @param integer $expected_attachment_index Expected attachment's index number in the self::$attachments array. 45 * @param string $expected The expected output string. 46 * @param array $args Array of arguments to pass to the function being tested. 47 * @return array { 48 * Array of the prepared test parameters. 49 * 50 * @var string $expected Expected output string. 51 * @var array $args All of the arguments to pass to the function being tested. 52 * } 53 */ 54 protected function setup_test_scenario( $current_attachment_index, $expected_attachment_index, $expected, array $args = array() ) { 55 // This prep code allows the data provider to specify the different arguments needed for the test scenario. 56 $args = array_merge( $this->default_args, $args ); 57 $args = array_values( $args ); 58 59 // Replace the attachment ID placeholder. 60 if ( isset( self::$attachments[ $expected_attachment_index ] ) ) { 61 $expected = str_replace( '%%ID%%', self::$attachments[ $expected_attachment_index ], $expected ); 62 } 63 64 // Go to the current attachment to set the state for the tests. 65 $this->go_to( get_permalink( self::$attachments[ $current_attachment_index ] ) ); 66 67 // Return the changed parameters. 68 return array( $expected, $args ); 69 } 70 }