<!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>[53033] trunk/tests/phpunit/tests/general/feedLinksExtra.php: Build/Test Tools: Add unit tests for `feed_links_extra()`.</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/53033">53033</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/53033","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>peterwilsoncc</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2022-03-31 00:55:16 +0000 (Thu, 31 Mar 2022)</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'>Build/Test Tools: Add unit tests for `feed_links_extra()`.

Props costdev, hellofromTonya.
Fixes <a href="https://core.trac.wordpress.org/ticket/54713">#54713</a>.
See <a href="https://core.trac.wordpress.org/ticket/54725">#54725</a>.</pre>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsgeneralfeedLinksExtraphp">trunk/tests/phpunit/tests/general/feedLinksExtra.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunktestsphpunittestsgeneralfeedLinksExtraphp"></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/general/feedLinksExtra.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/general/feedLinksExtra.php                              (rev 0)
+++ trunk/tests/phpunit/tests/general/feedLinksExtra.php        2022-03-31 00:55:16 UTC (rev 53033)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,513 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Test feed_links_extra().
+ *
+ * @ticket 54713
+ *
+ * @group general
+ * @group template
+ *
+ * @covers ::feed_links_extra
+ */
+class Tests_General_FeedLinksExtra extends WP_UnitTestCase {
+       /**
+        * Author ID.
+        *
+        * @var int
+        */
+       protected static $author_id;
+
+       /**
+        * Category ID.
+        *
+        * @var int
+        */
+       protected static $category_id;
+
+       /**
+        * Tag ID.
+        *
+        * @var int
+        */
+       protected static $tag_id;
+
+       /**
+        * Taxonomy ID.
+        *
+        * @var int
+        */
+       protected static $tax_id;
+
+       /**
+        * Post Type.
+        *
+        * @var string
+        */
+       protected static $post_type;
+
+       /**
+        * The ID of a post with no comment.
+        *
+        * @var int
+        */
+       protected static $post_no_comment_id;
+
+       /**
+        * The ID of a post with a comment.
+        *
+        * @var int
+        */
+       protected static $post_with_comment_id;
+
+       /**
+        * The ID of a post with a custom post type.
+        *
+        * @var int
+        */
+       protected static $post_with_cpt_id;
+
+       public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+               // Author.
+               self::$author_id = $factory->user->create(
+                       array(
+                               'user_login' => 'author_feed_links_extra',
+                               'role'       => 'administrator',
+                       )
+               );
+
+               // Category.
+               self::$category_id = $factory->category->create(
+                       array( 'name' => 'cat_feed_links_extra' )
+               );
+
+               // Tag.
+               self::$tag_id = $factory->tag->create(
+                       array( 'name' => 'tag_feed_links_extra' )
+               );
+
+               // Taxonomy.
+               self::$tax_id = 'tax_feed_links_extra';
+
+               // Post type.
+               self::$post_type = 'cpt_feed_links_extra';
+
+               register_taxonomy(
+                       self::$tax_id,
+                       self::$post_type,
+                       array(
+                               'labels' => array(
+                                       'name'          => 'Taxonomy Terms',
+                                       'singular_name' => 'Taxonomy Term',
+                               ),
+                       )
+               );
+
+               register_post_type(
+                       self::$post_type,
+                       array(
+                               'public'      => true,
+                               'has_archive' => true,
+                               'taxonomies'  => array( self::$tax_id ),
+                               'labels'      => array( 'name' => 'CPT for feed_links_extra()' ),
+                       )
+               );
+
+               // Posts.
+               self::$post_no_comment_id = $factory->post->create(
+                       array( 'post_title' => 'Post with no comments' )
+               );
+
+               self::$post_with_comment_id = $factory->post->create(
+                       array( 'post_title' => 'Post with a comment' )
+               );
+
+               $factory->comment->create(
+                       array(
+                               'comment_author'  => self::$author_id,
+                               'comment_post_ID' => self::$post_with_comment_id,
+                       )
+               );
+
+               self::$post_with_cpt_id = $factory->post->create(
+                       array(
+                               'post_title' => 'Post with a custom post type',
+                               'post_type'  => self::$post_type,
+                       )
+               );
+
+               wp_set_object_terms( self::$post_with_cpt_id, 'tax_term', self::$tax_id );
+       }
+
+       public function set_up() {
+               parent::set_up();
+
+               register_taxonomy(
+                       self::$tax_id,
+                       self::$post_type,
+                       array(
+                               'labels' => array(
+                                       'name'          => 'Taxonomy Terms',
+                                       'singular_name' => 'Taxonomy Term',
+                               ),
+                       )
+               );
+
+               register_post_type(
+                       self::$post_type,
+                       array(
+                               'public'      => true,
+                               'has_archive' => true,
+                               'taxonomies'  => array( self::$tax_id ),
+                               'labels'      => array( 'name' => 'CPT for feed_links_extra()' ),
+                       )
+               );
+       }
+
+       /**
+        * @dataProvider data_feed_links_extra
+        * @ticket 54713
+        *
+        * @param string $title     The expected title.
+        * @param string $type      The name of the test class property containing the object ID.
+        * @param array  $args {
+        *        Optional arguments. Default empty.
+        *
+        *        @type string $separator     The separator between blog name and feed type.
+        *        @type string $singletitle   The title of the comments feed.
+        *        @type string $cattitle      The title of the category feed.
+        *        @type string $tagtitle      The title of the tag feed.
+        *        @type string $taxtitle      The title of the taxonomy feed.
+        *        @type string $authortitle   The title of the author feed.
+        *        @type string $searchtitle   The title of the search feed.
+        *        @type string $posttypetitle The title of the post type feed.
+        * }
+        */
+       public function test_feed_links_extra( $title, $type, array $args = array() ) {
+               $permalink = $this->helper_get_the_permalink( $type );
+               $this->go_to( $permalink );
+
+               $expected = '';
+
+               if ( '' !== $title ) {
+                       if ( 'post_type' === $type || 'search' === $type ) {
+                               $feed_link = $permalink . '&#038;feed=rss2';
+                       } else {
+                               $feed_link = str_replace( '?', '?feed=rss2&#038;', $permalink );
+                       }
+
+                       $expected = sprintf(
+                               '<link rel="alternate" type="application/rss+xml" title="%s" href="%s" />' . "\n",
+                               esc_attr( $title ),
+                               esc_url( $feed_link )
+                       );
+               }
+
+               $this->assertSame( $expected, get_echo( 'feed_links_extra', array( $args ) ) );
+       }
+
+       /**
+        * Data provider.
+        *
+        * @return array
+        */
+       public function data_feed_links_extra() {
+               return array(
+                       'a post with a comment'                        => array(
+                               'title' => 'Test Blog &raquo; Post with a comment Comments Feed',
+                               'type'  => 'post_with_comment',
+                       ),
+                       'a post with a comment and a custom separator' => array(
+                               'title' => 'Test Blog // Post with a comment Comments Feed',
+                               'type'  => 'post_with_comment',
+                               'args'  => array(
+                                       'separator' => '//',
+                               ),
+                       ),
+                       'a post with a comment and a custom title'     => array(
+                               'title' => 'Custom Title for Singular Feed',
+                               'type'  => 'post_with_comment',
+                               'args'  => array(
+                                       'singletitle' => 'Custom Title for Singular Feed',
+                               ),
+                       ),
+                       'a post with a comment, a custom separator and a custom title' => array(
+                               'title' => 'Test Blog // Custom Title for Singular Feed',
+                               'type'  => 'post_with_comment',
+                               'args'  => array(
+                                       'separator'   => '//',
+                                       'singletitle' => '%1$s %2$s Custom Title for Singular Feed',
+                               ),
+                       ),
+                       'a custom post type'                           => array(
+                               'title' => 'Test Blog &raquo; CPT for feed_links_extra() Feed',
+                               'type'  => 'post_type',
+                       ),
+                       'a custom post type and a custom separator'    => array(
+                               'title' => 'Test Blog // CPT for feed_links_extra() Feed',
+                               'type'  => 'post_type',
+                               'args'  => array(
+                                       'separator' => '//',
+                               ),
+                       ),
+                       'a custom post type and a custom title'        => array(
+                               'title' => 'Custom Title for CPT Feed',
+                               'type'  => 'post_type',
+                               'args'  => array(
+                                       'posttypetitle' => 'Custom Title for CPT Feed',
+                               ),
+                       ),
+                       'a custom post type, a custom separator and a custom title' => array(
+                               'title' => 'Test Blog // Custom Title for CPT Feed',
+                               'type'  => 'post_type',
+                               'args'  => array(
+                                       'separator'     => '//',
+                                       'posttypetitle' => '%1$s %2$s Custom Title for CPT Feed',
+                               ),
+                       ),
+                       'a category'                                   => array(
+                               'title' => 'Test Blog &raquo; cat_feed_links_extra Category Feed',
+                               'type'  => 'category',
+                       ),
+                       'a category and a custom separator'            => array(
+                               'title' => 'Test Blog // cat_feed_links_extra Category Feed',
+                               'type'  => 'category',
+                               'args'  => array(
+                                       'separator' => '//',
+                               ),
+                       ),
+                       'a category and a custom title'                => array(
+                               'title' => 'Custom Title for Category Feed',
+                               'type'  => 'category',
+                               'args'  => array(
+                                       'cattitle' => 'Custom Title for Category Feed',
+                               ),
+                       ),
+                       'a category, a custom separator and a custom title' => array(
+                               'title' => 'Test Blog // Custom Title for Category Feed',
+                               'type'  => 'category',
+                               'args'  => array(
+                                       'separator' => '//',
+                                       'cattitle'  => '%1$s %2$s Custom Title for Category Feed',
+                               ),
+                       ),
+                       'a tag'                                        => array(
+                               'title' => 'Test Blog &raquo; tag_feed_links_extra Tag Feed',
+                               'type'  => 'tag',
+                       ),
+                       'a tag and a custom separator'                 => array(
+                               'title' => 'Test Blog // tag_feed_links_extra Tag Feed',
+                               'type'  => 'tag',
+                               'args'  => array(
+                                       'separator' => '//',
+                               ),
+                       ),
+                       'a tag and a custom title'                     => array(
+                               'title' => 'Custom Title for Tag Feed',
+                               'type'  => 'tag',
+                               'args'  => array(
+                                       'tagtitle' => 'Custom Title for Tag Feed',
+                               ),
+                       ),
+                       'a tag, a custom separator and a custom title' => array(
+                               'title' => 'Test Blog // Custom Title for Tag Feed',
+                               'type'  => 'tag',
+                               'args'  => array(
+                                       'separator' => '//',
+                                       'tagtitle'  => '%1$s %2$s Custom Title for Tag Feed',
+                               ),
+                       ),
+                       'a taxonomy'                                   => array(
+                               'title' => 'Test Blog &raquo; tax_term Taxonomy Term Feed',
+                               'type'  => 'tax',
+                       ),
+                       'a taxonomy and a custom separator'            => array(
+                               'title' => 'Test Blog // tax_term Taxonomy Term Feed',
+                               'type'  => 'tax',
+                               'args'  => array(
+                                       'separator' => '//',
+                               ),
+                       ),
+                       'a taxonomy and a custom title'                => array(
+                               'title' => 'Custom Title for Taxonomy Feed',
+                               'type'  => 'tax',
+                               'args'  => array(
+                                       'taxtitle' => 'Custom Title for Taxonomy Feed',
+                               ),
+                       ),
+                       'a taxonomy, a custom separator and a custom title' => array(
+                               'title' => 'Test Blog // Custom Title for Taxonomy Feed',
+                               'type'  => 'tax',
+                               'args'  => array(
+                                       'separator' => '//',
+                                       'taxtitle'  => '%1$s %2$s Custom Title for Taxonomy Feed',
+                               ),
+                       ),
+                       'an author'                                    => array(
+                               'title' => 'Test Blog &raquo; Posts by author_feed_links_extra Feed',
+                               'type'  => 'author',
+                       ),
+                       'an author and a custom separator'             => array(
+                               'title' => 'Test Blog // Posts by author_feed_links_extra Feed',
+                               'type'  => 'author',
+                               'args'  => array(
+                                       'separator' => '//',
+                               ),
+                       ),
+                       'an author and a custom title'                 => array(
+                               'title' => 'Custom Title for Author Feed',
+                               'type'  => 'author',
+                               'args'  => array(
+                                       'authortitle' => 'Custom Title for Author Feed',
+                               ),
+                       ),
+                       'an author, a custom separator and a custom title' => array(
+                               'title' => 'Test Blog // Custom Title for Author Feed',
+                               'type'  => 'author',
+                               'args'  => array(
+                                       'separator'   => '//',
+                                       'authortitle' => '%1$s %2$s Custom Title for Author Feed',
+                               ),
+                       ),
+                       'search results'                               => array(
+                               'title' => 'Test Blog &raquo; Search Results for &#8220;Search&#8221; Feed',
+                               'type'  => 'search',
+                       ),
+                       'search results and a custom separator'        => array(
+                               'title' => 'Test Blog // Search Results for &#8220;Search&#8221; Feed',
+                               'type'  => 'search',
+                               'args'  => array(
+                                       'separator' => '//',
+                               ),
+                       ),
+                       'search results and a custom title'            => array(
+                               'title' => 'Custom Title for Search Feed',
+                               'type'  => 'search',
+                               'args'  => array(
+                                       'searchtitle' => 'Custom Title for Search Feed',
+                               ),
+                       ),
+                       'search results, a custom separator and a custom title' => array(
+                               'title' => 'Test Blog // Custom Title for Search Feed',
+                               'type'  => 'search',
+                               'args'  => array(
+                                       'separator'   => '//',
+                                       'searchtitle' => '%1$s %2$s Custom Title for Search Feed',
+                               ),
+                       ),
+               );
+       }
+
+       /**
+        * Helper function to get the permalink based on type.
+        *
+        * @ticket 54713
+        *
+        * @param string $type The name of the test class property containing the object ID.
+        * @return string The permalink.
+        */
+       private function helper_get_the_permalink( $type ) {
+               if ( 'category' === $type || 'tag' === $type ) {
+                       return get_term_link( self::${$type . '_id'} );
+               }
+
+               if ( 'tax' === $type ) {
+                       return get_term_link( 'tax_term', self::$tax_id );
+               }
+
+               if ( 'post_type' === $type ) {
+                       return get_post_type_archive_link( self::$post_type );
+               }
+
+               if ( 'author' === $type ) {
+                       return get_author_posts_url( self::$author_id );
+               }
+
+               if ( 'search' === $type ) {
+                       return home_url( '?s=Search' );
+               }
+
+               return get_the_permalink( self::${$type . '_id'} );
+       }
+
+       /**
+        * @ticket 54713
+        */
+       public function test_feed_links_extra_should_respect_comments_open() {
+               add_filter( 'comments_open', '__return_true' );
+               add_filter( 'pings_open', '__return_false' );
+
+               $this->go_to( get_the_permalink( self::$post_no_comment_id ) );
+
+               $expected  = '<link rel="alternate" type="application/rss+xml"';
+               $expected .= ' title="Test Blog &raquo; Post with no comments Comments Feed"';
+               $expected .= ' href="http://example.org/?feed=rss2&#038;p=' . self::$post_no_comment_id . '" />' . "\n";
+               $this->assertSame( $expected, get_echo( 'feed_links_extra' ) );
+       }
+
+       /**
+        * @ticket 54713
+        */
+       public function test_feed_links_extra_should_respect_pings_open() {
+               add_filter( 'pings_open', '__return_true' );
+               add_filter( 'comments_open', '__return_false' );
+
+               $this->go_to( get_the_permalink( self::$post_no_comment_id ) );
+
+               $expected  = '<link rel="alternate" type="application/rss+xml"';
+               $expected .= ' title="Test Blog &raquo; Post with no comments Comments Feed"';
+               $expected .= ' href="http://example.org/?feed=rss2&#038;p=' . self::$post_no_comment_id . '" />' . "\n";
+               $this->assertSame( $expected, get_echo( 'feed_links_extra' ) );
+       }
+
+       /**
+        * @ticket 54713
+        */
+       public function test_feed_links_extra_should_respect_post_comment_count() {
+               add_filter( 'pings_open', '__return_false' );
+               add_filter( 'comments_open', '__return_false' );
+
+               $this->go_to( get_the_permalink( self::$post_with_comment_id ) );
+
+               $expected  = '<link rel="alternate" type="application/rss+xml"';
+               $expected .= ' title="Test Blog &raquo; Post with a comment Comments Feed"';
+               $expected .= ' href="http://example.org/?feed=rss2&#038;p=' . self::$post_with_comment_id . '" />' . "\n";
+               $this->assertSame( $expected, get_echo( 'feed_links_extra' ) );
+       }
+
+       /**
+        * @ticket 54713
+        */
+       public function test_feed_links_extra_should_return_empty_when_comments_and_pings_are_closed_and_post_has_no_comments() {
+               add_filter( 'comments_open', '__return_false' );
+               add_filter( 'pings_open', '__return_false' );
+
+               $this->go_to( get_the_permalink( self::$post_no_comment_id ) );
+               $this->assertEmpty( get_echo( 'feed_links_extra' ) );
+       }
+
+       /**
+        * @ticket 54713
+        */
+       public function test_feed_links_extra_should_respect_feed_type() {
+               add_filter(
+                       'default_feed',
+                       static function() {
+                               return 'foo';
+                       }
+               );
+
+               add_filter(
+                       'feed_content_type',
+                       static function() {
+                               return 'testing/foo';
+                       }
+               );
+
+               $this->go_to( get_the_permalink( self::$post_with_comment_id ) );
+
+               $expected  = '<link rel="alternate" type="testing/foo"';
+               $expected .= ' title="Test Blog &raquo; Post with a comment Comments Feed"';
+               $expected .= ' href="http://example.org/?feed=foo&#038;p=' . self::$post_with_comment_id . '" />' . "\n";
+               $this->assertSame( $expected, get_echo( 'feed_links_extra' ) );
+       }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/general/feedLinksExtra.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>