<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[51122] trunk: Media: Add new functions to return the previous/next attachment links.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/51122">51122</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/51122","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>whyisjake</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-06-08 23:12:22 +0000 (Tue, 08 Jun 2021)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/45708">#45708</a>.
Props flixos90, DrewAPicture, mor10, antpb, hellofromTonya, whyisjake.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkphpcsxmldist">trunk/phpcs.xml.dist</a></li>
<li><a href="#trunksrcwpincludesmediaphp">trunk/src/wp-includes/media.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsmediagetAdjacentImageLinkphp">trunk/tests/phpunit/tests/media/getAdjacentImageLink.php</a></li>
<li><a href="#trunktestsphpunittestsmediagetNextImageLinkphp">trunk/tests/phpunit/tests/media/getNextImageLink.php</a></li>
<li><a href="#trunktestsphpunittestsmediagetPreviousImageLinkphp">trunk/tests/phpunit/tests/media/getPreviousImageLink.php</a></li>
<li><a href="#trunktestsphpunittestsmedianextImageLinkphp">trunk/tests/phpunit/tests/media/nextImageLink.php</a></li>
<li><a href="#trunktestsphpunittestsmediapreviousImageLinkphp">trunk/tests/phpunit/tests/media/previousImageLink.php</a></li>
<li><a href="#trunktestsphpunittestsmediatestcaseadjacentimagelinkphp">trunk/tests/phpunit/tests/media/testcase-adjacent-image-link.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkphpcsxmldist"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/phpcs.xml.dist</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/phpcs.xml.dist 2021-06-08 22:59:19 UTC (rev 51121)
+++ trunk/phpcs.xml.dist 2021-06-08 23:12:22 UTC (rev 51122)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -249,6 +249,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <element value="WP_Import_UnitTestCase"/>
</span><span class="cx" style="display: block; padding: 0 10px"> <element value="Tests_Query_Conditionals"/>
</span><span class="cx" style="display: block; padding: 0 10px"> <element value="WP_Test_XML_TestCase"/>
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ <element value="WP_Test_Adjacent_Image_Link_TestCase"/>
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> <!-- Mock classes. -->
</span><span class="cx" style="display: block; padding: 0 10px"> <element value="Spy_REST_Server"/>
</span></span></pre></div>
<a id="trunksrcwpincludesmediaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/media.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/media.php 2021-06-08 22:59:19 UTC (rev 51121)
+++ trunk/src/wp-includes/media.php 2021-06-08 23:12:22 UTC (rev 51122)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3377,48 +3377,77 @@
</span><span class="cx" style="display: block; padding: 0 10px"> add_shortcode( 'video', 'wp_video_shortcode' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Gets the previous image link that has the same post parent.
+ *
+ * @since 5.8.0
+ *
+ * @see get_adjacent_image_link()
+ *
+ * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array
+ * of width and height values in pixels (in that order). Default 'thumbnail'.
+ * @param string|false $text Optional. Link text. Default false.
+ * @return string Markup for previous image link.
+ */
+function get_previous_image_link( $size = 'thumbnail', $text = false ) {
+ return get_adjacent_image_link( true, $size, $text );
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Displays previous image link that has the same post parent.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 2.5.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @see adjacent_image_link()
- *
</del><span class="cx" style="display: block; padding: 0 10px"> * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array
</span><span class="cx" style="display: block; padding: 0 10px"> * of width and height values in pixels (in that order). Default 'thumbnail'.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string|false $text Optional. Link text. Default false.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function previous_image_link( $size = 'thumbnail', $text = false ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- adjacent_image_link( true, $size, $text );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ echo get_previous_image_link( $size, $text );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Gets the next image link that has the same post parent.
+ *
+ * @since 5.8.0
+ *
+ * @see get_adjacent_image_link()
+ *
+ * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array
+ * of width and height values in pixels (in that order). Default 'thumbnail'.
+ * @param string|false $text Optional. Link text. Default false.
+ * @return string Markup for next image link.
+ */
+function get_next_image_link( $size = 'thumbnail', $text = false ) {
+ return get_adjacent_image_link( false, $size, $text );
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Displays next image link that has the same post parent.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 2.5.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @see adjacent_image_link()
- *
</del><span class="cx" style="display: block; padding: 0 10px"> * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array
</span><span class="cx" style="display: block; padding: 0 10px"> * of width and height values in pixels (in that order). Default 'thumbnail'.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string|false $text Optional. Link text. Default false.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function next_image_link( $size = 'thumbnail', $text = false ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- adjacent_image_link( false, $size, $text );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ echo get_next_image_link( $size, $text );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Displays next or previous image link that has the same post parent.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Gets the next or previous image link that has the same post parent.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * Retrieves the current attachment object from the $post global.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @since 2.5.0
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.8.0
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param bool $prev Optional. Whether to display the next (false) or previous (true) link. Default true.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array
</span><span class="cx" style="display: block; padding: 0 10px"> * of width and height values in pixels (in that order). Default 'thumbnail'.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param bool $text Optional. Link text. Default false.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return string Markup for image link.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function adjacent_image_link( $prev = true, $size = 'thumbnail', $text = false ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function get_adjacent_image_link( $prev = true, $size = 'thumbnail', $text = false ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $post = get_post();
</span><span class="cx" style="display: block; padding: 0 10px"> $attachments = array_values(
</span><span class="cx" style="display: block; padding: 0 10px"> get_children(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3473,10 +3502,26 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * an array of width and height values in pixels (in that order).
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $text Link text.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- echo apply_filters( "{$adjacent}_image_link", $output, $attachment_id, $size, $text );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return apply_filters( "{$adjacent}_image_link", $output, $attachment_id, $size, $text );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Displays next or previous image link that has the same post parent.
+ *
+ * Retrieves the current attachment object from the $post global.
+ *
+ * @since 2.5.0
+ *
+ * @param bool $prev Optional. Whether to display the next (false) or previous (true) link. Default true.
+ * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array
+ * of width and height values in pixels (in that order). Default 'thumbnail'.
+ * @param bool $text Optional. Link text. Default false.
+ */
+function adjacent_image_link( $prev = true, $size = 'thumbnail', $text = false ) {
+ echo get_adjacent_image_link( $prev, $size, $text );
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Retrieves taxonomies attached to given the attachment.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 2.5.0
</span></span></pre></div>
<a id="trunktestsphpunittestsmediagetAdjacentImageLinkphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/media/getAdjacentImageLink.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/media/getAdjacentImageLink.php (rev 0)
+++ trunk/tests/phpunit/tests/media/getAdjacentImageLink.php 2021-06-08 23:12:22 UTC (rev 51122)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,88 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?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',
+ ),
+ ),
+ );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/media/getAdjacentImageLink.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestsmediagetNextImageLinkphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/media/getNextImageLink.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/media/getNextImageLink.php (rev 0)
+++ trunk/tests/phpunit/tests/media/getNextImageLink.php 2021-06-08 23:12:22 UTC (rev 51122)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,57 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?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' ),
+ ),
+ );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/media/getNextImageLink.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestsmediagetPreviousImageLinkphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/media/getPreviousImageLink.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/media/getPreviousImageLink.php (rev 0)
+++ trunk/tests/phpunit/tests/media/getPreviousImageLink.php 2021-06-08 23:12:22 UTC (rev 51122)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,57 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?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' ),
+ ),
+ );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/media/getPreviousImageLink.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestsmedianextImageLinkphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/media/nextImageLink.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/media/nextImageLink.php (rev 0)
+++ trunk/tests/phpunit/tests/media/nextImageLink.php 2021-06-08 23:12:22 UTC (rev 51122)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,56 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?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' ),
+ ),
+ );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/media/nextImageLink.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestsmediapreviousImageLinkphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/media/previousImageLink.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/media/previousImageLink.php (rev 0)
+++ trunk/tests/phpunit/tests/media/previousImageLink.php 2021-06-08 23:12:22 UTC (rev 51122)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,56 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?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' ),
+ ),
+ );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/media/previousImageLink.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestsmediatestcaseadjacentimagelinkphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/media/testcase-adjacent-image-link.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/media/testcase-adjacent-image-link.php (rev 0)
+++ trunk/tests/phpunit/tests/media/testcase-adjacent-image-link.php 2021-06-08 23:12:22 UTC (rev 51122)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,70 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?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 );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/media/testcase-adjacent-image-link.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span></div>
</body>
</html>