mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 14:20:15 +00:00
Media: Add new functions to return the previous/next attachment links.
New functions: * get_adjacent_image_link() * get_next_image_link() * get_previous_image_link() Fixes #45708. Props flixos90, DrewAPicture, mor10, antpb, hellofromTonya, whyisjake. git-svn-id: https://develop.svn.wordpress.org/trunk@51122 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
88
tests/phpunit/tests/media/getAdjacentImageLink.php
Normal file
88
tests/phpunit/tests/media/getAdjacentImageLink.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/testcase-adjacent-image-link.php';
|
||||
|
||||
/**
|
||||
* @group media
|
||||
* @covers ::get_adjacent_image_link
|
||||
*/
|
||||
class Tests_Media_GetAdjacentImageLink extends WP_Test_Adjacent_Image_Link_TestCase {
|
||||
protected $default_args = array(
|
||||
'prev' => true,
|
||||
'size' => 'thumbnail',
|
||||
'text' => false,
|
||||
);
|
||||
|
||||
/**
|
||||
* @ticket 45708
|
||||
*
|
||||
* @dataProvider data_get_adjacent_image_link
|
||||
*/
|
||||
function test_get_adjacent_image_link( $current_attachment_index, $expected_attachment_index, $expected, array $args = array() ) {
|
||||
list( $expected, $args ) = $this->setup_test_scenario( $current_attachment_index, $expected_attachment_index, $expected, $args );
|
||||
|
||||
$actual = get_adjacent_image_link( ...$args );
|
||||
|
||||
$this->assertSame( $expected, $actual );
|
||||
}
|
||||
|
||||
public function data_get_adjacent_image_link() {
|
||||
return array(
|
||||
// Happy paths.
|
||||
'when has previous link' => array(
|
||||
'current_attachment_index' => 3,
|
||||
'expected_attachment_index' => 2,
|
||||
'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>',
|
||||
),
|
||||
'with text when has previous link' => array(
|
||||
'current_attachment_index' => 3,
|
||||
'expected_attachment_index' => 2,
|
||||
'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'>Some text</a>',
|
||||
'args' => array( 'text' => 'Some text' ),
|
||||
),
|
||||
'when has next link' => array(
|
||||
'current_attachment_index' => 4,
|
||||
'expected_attachment_index' => 5,
|
||||
'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>',
|
||||
'args' => array( 'prev' => false ),
|
||||
),
|
||||
'with text when has next link' => array(
|
||||
'current_attachment_index' => 4,
|
||||
'expected_attachment_index' => 5,
|
||||
'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'>Some text</a>',
|
||||
'args' => array(
|
||||
'prev' => false,
|
||||
'text' => 'Some text',
|
||||
),
|
||||
),
|
||||
|
||||
// Unhappy paths.
|
||||
'when no previous link' => array(
|
||||
'current_attachment_index' => 1,
|
||||
'expected_attachment_index' => 0,
|
||||
'expected' => '',
|
||||
),
|
||||
'with text when no previous link' => array(
|
||||
'current_attachment_index' => 1,
|
||||
'expected_attachment_index' => 0,
|
||||
'expected' => '',
|
||||
'args' => array( 'text' => 'Some text' ),
|
||||
),
|
||||
'when no next link' => array(
|
||||
'current_attachment_index' => 5,
|
||||
'expected_attachment_index' => 0,
|
||||
'expected' => '',
|
||||
'args' => array( 'prev' => false ),
|
||||
),
|
||||
'with text when no next link' => array(
|
||||
'current_attachment_index' => 5,
|
||||
'expected_attachment_index' => 0,
|
||||
'expected' => '',
|
||||
'args' => array(
|
||||
'prev' => false,
|
||||
'text' => 'Some text',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
57
tests/phpunit/tests/media/getNextImageLink.php
Normal file
57
tests/phpunit/tests/media/getNextImageLink.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/testcase-adjacent-image-link.php';
|
||||
|
||||
/**
|
||||
* @group media
|
||||
* @covers ::get_next_image_link
|
||||
*/
|
||||
class Tests_Media_GetNextImageLink extends WP_Test_Adjacent_Image_Link_TestCase {
|
||||
protected $default_args = array(
|
||||
'size' => 'thumbnail',
|
||||
'text' => false,
|
||||
);
|
||||
|
||||
/**
|
||||
* @ticket 45708
|
||||
*
|
||||
* @dataProvider data_get_next_image_link
|
||||
*/
|
||||
function test_get_next_image_link( $current_attachment_index, $expected_attachment_index = 0, $expected, array $args = array() ) {
|
||||
list( $expected, $args ) = $this->setup_test_scenario( $current_attachment_index, $expected_attachment_index, $expected, $args );
|
||||
|
||||
$actual = get_next_image_link( ...$args );
|
||||
|
||||
$this->assertSame( $expected, $actual );
|
||||
}
|
||||
|
||||
public function data_get_next_image_link() {
|
||||
return array(
|
||||
// Happy paths.
|
||||
'when has next link' => array(
|
||||
'current_attachment_index' => 4,
|
||||
'expected_attachment_index' => 5,
|
||||
'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>',
|
||||
),
|
||||
'with text when has next link' => array(
|
||||
'current_attachment_index' => 4,
|
||||
'expected_attachment_index' => 5,
|
||||
'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'>Some text</a>',
|
||||
'args' => array( 'text' => 'Some text' ),
|
||||
),
|
||||
|
||||
// Unhappy paths.
|
||||
'when no next link' => array(
|
||||
'current_attachment_index' => 5,
|
||||
'expected_attachment_index' => 0,
|
||||
'expected' => '',
|
||||
),
|
||||
'with text when no next link' => array(
|
||||
'current_attachment_index' => 5,
|
||||
'expected_attachment_index' => 0,
|
||||
'expected' => '',
|
||||
'args' => array( 'text' => 'Some text' ),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
57
tests/phpunit/tests/media/getPreviousImageLink.php
Normal file
57
tests/phpunit/tests/media/getPreviousImageLink.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/testcase-adjacent-image-link.php';
|
||||
|
||||
/**
|
||||
* @group media
|
||||
* @covers ::get_previous_image_link
|
||||
*/
|
||||
class Tests_Media_GetPreviousImageLink extends WP_Test_Adjacent_Image_Link_TestCase {
|
||||
protected $default_args = array(
|
||||
'size' => 'thumbnail',
|
||||
'text' => false,
|
||||
);
|
||||
|
||||
/**
|
||||
* @ticket 45708
|
||||
*
|
||||
* @dataProvider data_get_previous_image_link
|
||||
*/
|
||||
function test_get_previous_image_link( $current_attachment_index, $expected_attachment_index = 0, $expected, array $args = array() ) {
|
||||
list( $expected, $args ) = $this->setup_test_scenario( $current_attachment_index, $expected_attachment_index, $expected, $args );
|
||||
|
||||
$actual = get_previous_image_link( ...$args );
|
||||
|
||||
$this->assertSame( $expected, $actual );
|
||||
}
|
||||
|
||||
public function data_get_previous_image_link() {
|
||||
return array(
|
||||
// Happy paths.
|
||||
'when has previous link' => array(
|
||||
'current_attachment_index' => 3,
|
||||
'expected_attachment_index' => 2,
|
||||
'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>',
|
||||
),
|
||||
'with text when has previous link' => array(
|
||||
'current_attachment_index' => 3,
|
||||
'expected_attachment_index' => 2,
|
||||
'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'>Some text</a>',
|
||||
'args' => array( 'text' => 'Some text' ),
|
||||
),
|
||||
|
||||
// Unhappy paths.
|
||||
'when no previous link' => array(
|
||||
'current_attachment_index' => 1,
|
||||
'expected_attachment_index' => 0,
|
||||
'expected' => '',
|
||||
),
|
||||
'with text when no previous link' => array(
|
||||
'current_attachment_index' => 1,
|
||||
'expected_attachment_index' => 0,
|
||||
'expected' => '',
|
||||
'args' => array( 'text' => 'Some text' ),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
56
tests/phpunit/tests/media/nextImageLink.php
Normal file
56
tests/phpunit/tests/media/nextImageLink.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/testcase-adjacent-image-link.php';
|
||||
|
||||
/**
|
||||
* @group media
|
||||
* @covers ::next_image_link
|
||||
*/
|
||||
class Tests_Media_NextImageLink extends WP_Test_Adjacent_Image_Link_TestCase {
|
||||
protected $default_args = array(
|
||||
'size' => 'thumbnail',
|
||||
'text' => false,
|
||||
);
|
||||
|
||||
/**
|
||||
* @ticket 45708
|
||||
*
|
||||
* @dataProvider data_next_image_link
|
||||
*/
|
||||
function test_next_image_link( $current_attachment_index, $expected_attachment_index = 0, $expected, array $args = array() ) {
|
||||
list( $expected, $args ) = $this->setup_test_scenario( $current_attachment_index, $expected_attachment_index, $expected, $args );
|
||||
|
||||
$this->expectOutputString( $expected );
|
||||
$this->assertNull( next_image_link( ...$args ) );
|
||||
}
|
||||
|
||||
public function data_next_image_link() {
|
||||
return array(
|
||||
// Happy paths.
|
||||
'when has next link' => array(
|
||||
'current_attachment_index' => 4,
|
||||
'expected_attachment_index' => 5,
|
||||
'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>',
|
||||
),
|
||||
'with text when has next link' => array(
|
||||
'current_attachment_index' => 4,
|
||||
'expected_attachment_index' => 5,
|
||||
'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'>Some text</a>',
|
||||
'args' => array( 'text' => 'Some text' ),
|
||||
),
|
||||
|
||||
// Unhappy paths.
|
||||
'when no next link' => array(
|
||||
'current_attachment_index' => 5,
|
||||
'expected_attachment_index' => 0,
|
||||
'expected' => '',
|
||||
),
|
||||
'with text when no next link' => array(
|
||||
'current_attachment_index' => 5,
|
||||
'expected_attachment_index' => 0,
|
||||
'expected' => '',
|
||||
'args' => array( 'text' => 'Some text' ),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
56
tests/phpunit/tests/media/previousImageLink.php
Normal file
56
tests/phpunit/tests/media/previousImageLink.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/testcase-adjacent-image-link.php';
|
||||
|
||||
/**
|
||||
* @group media
|
||||
* @covers ::previous_image_link
|
||||
*/
|
||||
class Tests_Media_PreviousImageLink extends WP_Test_Adjacent_Image_Link_TestCase {
|
||||
protected $default_args = array(
|
||||
'size' => 'thumbnail',
|
||||
'text' => false,
|
||||
);
|
||||
|
||||
/**
|
||||
* @ticket 45708
|
||||
*
|
||||
* @dataProvider data_previous_image_link
|
||||
*/
|
||||
function test_previous_image_link( $current_attachment_index, $expected_attachment_index = 0, $expected, array $args = array() ) {
|
||||
list( $expected, $args ) = $this->setup_test_scenario( $current_attachment_index, $expected_attachment_index, $expected, $args );
|
||||
|
||||
$this->expectOutputString( $expected );
|
||||
$this->assertNull( previous_image_link( ...$args ) );
|
||||
}
|
||||
|
||||
public function data_previous_image_link() {
|
||||
return array(
|
||||
// Happy paths.
|
||||
'when has previous link' => array(
|
||||
'current_attachment_index' => 3,
|
||||
'expected_attachment_index' => 2,
|
||||
'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>',
|
||||
),
|
||||
'with text when has previous link' => array(
|
||||
'current_attachment_index' => 3,
|
||||
'expected_attachment_index' => 2,
|
||||
'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'>Some text</a>',
|
||||
'args' => array( 'text' => 'Some text' ),
|
||||
),
|
||||
|
||||
// Unhappy paths.
|
||||
'when no previous link' => array(
|
||||
'current_attachment_index' => 1,
|
||||
'expected_attachment_index' => 0,
|
||||
'expected' => '',
|
||||
),
|
||||
'with text when no previous link' => array(
|
||||
'current_attachment_index' => 1,
|
||||
'expected_attachment_index' => 0,
|
||||
'expected' => '',
|
||||
'args' => array( 'text' => 'Some text' ),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
70
tests/phpunit/tests/media/testcase-adjacent-image-link.php
Normal file
70
tests/phpunit/tests/media/testcase-adjacent-image-link.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
abstract class WP_Test_Adjacent_Image_Link_TestCase extends WP_UnitTestCase {
|
||||
/**
|
||||
* Array of 5 attachments for use in the tests.
|
||||
*
|
||||
* @var init{}|WP_Error[]
|
||||
*/
|
||||
protected static $attachments;
|
||||
|
||||
/**
|
||||
* Default args for the function being tested.
|
||||
*
|
||||
* Defined in each test class.
|
||||
*
|
||||
* @var int[]|WP_Error[] Array of attachment IDs.
|
||||
*/
|
||||
protected $default_args = array();
|
||||
|
||||
/**
|
||||
* Setup the tests after the data provider but before the tests start.
|
||||
*
|
||||
* @param WP_UnitTest_Factory $factory Instance of the factory.
|
||||
*/
|
||||
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
|
||||
$parent_id = $factory->post->create();
|
||||
|
||||
for ( $index = 1; $index <= 5; $index++ ) {
|
||||
self::$attachments[ $index ] = $factory->attachment->create_object(
|
||||
"image{$index}.jpg",
|
||||
$parent_id,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
'post_type' => 'attachment',
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the test scenario.
|
||||
*
|
||||
* @param integer $current_attachment_index Current attachment's index number in the self::$attachments array.
|
||||
* @param integer $expected_attachment_index Expected attachment's index number in the self::$attachments array.
|
||||
* @param string $expected The expected output string.
|
||||
* @param array $args Array of arguments to pass to the function being tested.
|
||||
* @return array {
|
||||
* Array of the prepared test parameters.
|
||||
*
|
||||
* @var string $expected Expected output string.
|
||||
* @var array $args All of the arguments to pass to the function being tested.
|
||||
* }
|
||||
*/
|
||||
protected function setup_test_scenario( $current_attachment_index, $expected_attachment_index, $expected, array $args = array() ) {
|
||||
// This prep code allows the data provider to specify the different arguments needed for the test scenario.
|
||||
$args = array_merge( $this->default_args, $args );
|
||||
$args = array_values( $args );
|
||||
|
||||
// Replace the attachment ID placeholder.
|
||||
if ( isset( self::$attachments[ $expected_attachment_index ] ) ) {
|
||||
$expected = str_replace( '%%ID%%', self::$attachments[ $expected_attachment_index ], $expected );
|
||||
}
|
||||
|
||||
// Go to the current attachment to set the state for the tests.
|
||||
$this->go_to( get_permalink( self::$attachments[ $current_attachment_index ] ) );
|
||||
|
||||
// Return the changed parameters.
|
||||
return array( $expected, $args );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user