Make WordPress Core

Changeset 36519


Ignore:
Timestamp:
02/12/2016 07:02:25 PM (9 years ago)
Author:
jorbin
Message:

Improve Automated Feed Tests

Multiple improvements to the RSS2 automated tests along with the addition of Atom tests.

  1. General whitespace cleanup (since the rss2 file serves as the base of the atom file).
  2. Adds an author and category to the tests.
  3. Since the content of the posts is the same, we don't need to test all of the post content.
  4. Adds many posts so that the post count can be checked

Props stevenkword
Fixes #35160.

Location:
trunk/tests/phpunit/tests/feed
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/feed/rss2.php

    r35506 r36519  
    22
    33/**
    4  * test the RSS 2.0 feed by generating a feed, parsing it, and checking that the
     4 * Test the RSS 2.0 feed by generating a feed, parsing it, and checking that the
    55 * parsed contents match the contents of the posts stored in the database.  Since
    66 * we're using a real XML parser, this confirms that the feed is valid, well formed,
     
    99 * @group feed
    1010 */
    11 class Tests_Feed_RSS2 extends WP_UnitTestCase {
     11class Tests_Feeds_RSS2 extends WP_UnitTestCase {
    1212    static $user_id;
    1313    static $posts;
    14 
     14    static $category;
     15
     16    /**
     17     * Setup a new user and attribute some posts.
     18     */
    1519    public static function wpSetUpBeforeClass( $factory ) {
    16         self::$user_id = $factory->user->create();
    17         self::$posts = $factory->post->create_many( 5, array(
    18             'post_author' => self::$user_id,
     20        // Create a user
     21        self::$user_id = $factory->user->create( array(
     22            'role'         => 'author',
     23            'user_login'   => 'test_author',
     24            'display_name' => 'Test A. Uthor',
    1925        ) );
    20     }
    21 
     26
     27        // Create a taxonomy
     28        self::$category = self::factory()->category->create_and_get( array(
     29            'name' => 'Test Category',
     30            'slug' => 'test-cat',
     31        ) );
     32
     33        // Create a few posts
     34        self::$posts = $factory->post->create_many( 42, array(
     35            'post_author'  => self::$user_id,
     36            'post_content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec velit massa, ultrices eu est suscipit, mattis posuere est. Donec vitae purus lacus. Cras vitae odio odio.',
     37            'post_excerpt' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
     38        ) );
     39
     40        // Assign a category to those posts
     41        foreach ( self::$posts as $post ) {
     42            wp_set_object_terms( $post, self::$category->slug, 'category' );
     43        }
     44    }
     45
     46    /**
     47     * Destroy the user we created and related posts.
     48     */
    2249    public static function wpTearDownAfterClass() {
     50        // Delete our user
    2351        self::delete_user( self::$user_id );
    2452
     53        // Delete all of our posts
    2554        foreach ( self::$posts as $post ) {
    2655            wp_delete_post( $post, true );
    2756        }
    28     }
    29 
     57
     58        // Delete our taxonomy
     59        wp_delete_category( self::$category->term_id );
     60    }
     61
     62    /**
     63     * Setup.
     64     */
    3065    public function setUp() {
    3166        parent::setUp();
    3267
    33         $this->post_count = get_option('posts_per_rss');
    34         $this->excerpt_only = get_option('rss_use_excerpt');
     68        $this->post_count = (int) get_option( 'posts_per_rss' );
     69        $this->excerpt_only = get_option( 'rss_use_excerpt' );
    3570        // this seems to break something
    36         update_option('use_smilies', false);
    37     }
    38 
     71        update_option( 'use_smilies', false );
     72    }
     73
     74    /**
     75     * This is a bit of a hack used to buffer feed content.
     76     */
    3977    function do_rss2() {
    4078        ob_start();
    41         // nasty hack
     79        // Nasty hack! In the future it would better to leverage do_feed( 'rss2' ).
    4280        global $post;
    4381        try {
     
    5189    }
    5290
    53     function test_rss() {
    54         $this->go_to( '/?feed=rss2' );
    55         $feed = $this->do_rss2();
    56         $xml = xml_to_array($feed);
    57 
    58         // get the rss element
    59         $rss = xml_find($xml, 'rss');
    60 
    61         // there should only be one rss element
    62         $this->assertEquals(1, count($rss));
    63 
    64         $this->assertEquals('2.0', $rss[0]['attributes']['version']);
    65         $this->assertEquals('http://purl.org/rss/1.0/modules/content/', $rss[0]['attributes']['xmlns:content']);
    66         $this->assertEquals('http://wellformedweb.org/CommentAPI/', $rss[0]['attributes']['xmlns:wfw']);
    67         $this->assertEquals('http://purl.org/dc/elements/1.1/', $rss[0]['attributes']['xmlns:dc']);
     91    /**
     92     * Test the <rss> element to make sure its present and populated
     93     * with the expected child elements and attributes.
     94     */
     95    function test_rss_element() {
     96        $this->go_to( '/?feed=rss2' );
     97        $feed = $this->do_rss2();
     98        $xml = xml_to_array( $feed );
     99
     100        // Get the <rss> child element of <xml>.
     101        $rss = xml_find( $xml, 'rss' );
     102
     103        // There should only be one <rss> child element.
     104        $this->assertEquals( 1, count( $rss ) );
     105
     106        $this->assertEquals( '2.0', $rss[0]['attributes']['version'] );
     107        $this->assertEquals( 'http://purl.org/rss/1.0/modules/content/', $rss[0]['attributes']['xmlns:content'] );
     108        $this->assertEquals( 'http://wellformedweb.org/CommentAPI/', $rss[0]['attributes']['xmlns:wfw'] );
     109        $this->assertEquals( 'http://purl.org/dc/elements/1.1/', $rss[0]['attributes']['xmlns:dc'] );
    68110
    69111        // rss should have exactly one child element (channel)
    70         $this->assertEquals(1, count($rss[0]['child']));
    71     }
    72 
    73     function test_channel() {
    74         $this->go_to( '/?feed=rss2' );
    75         $feed = $this->do_rss2();
    76         $xml = xml_to_array($feed);
     112        $this->assertEquals( 1, count( $rss[0]['child'] ) );
     113    }
     114
     115    /**
     116     * [test_channel_element description]
     117     * @return [type] [description]
     118     */
     119    function test_channel_element() {
     120        $this->go_to( '/?feed=rss2' );
     121        $feed = $this->do_rss2();
     122        $xml = xml_to_array( $feed );
    77123
    78124        // get the rss -> channel element
    79         $channel = xml_find($xml, 'rss', 'channel');
    80 
    81         $this->assertTrue(empty($channel[0]['attributes']));
    82 
    83         $title = xml_find($xml, 'rss', 'channel', 'title');
    84         $this->assertEquals(get_option('blogname'), $title[0]['content']);
    85 
    86         $desc = xml_find($xml, 'rss', 'channel', 'description');
    87         $this->assertEquals(get_option('blogdescription'), $desc[0]['content']);
    88 
    89         $link = xml_find($xml, 'rss', 'channel', 'link');
    90         $this->assertEquals(get_option('siteurl'), $link[0]['content']);
    91 
    92         $pubdate = xml_find($xml, 'rss', 'channel', 'lastBuildDate');
    93         $this->assertEquals(strtotime(get_lastpostmodified()), strtotime($pubdate[0]['content']));
     125        $channel = xml_find( $xml, 'rss', 'channel' );
     126
     127        // The channel should be free of attributes
     128        $this->assertTrue( empty( $channel[0]['attributes'] ) );
     129
     130        // Verify the channel is present and contains a title child element
     131        $title = xml_find( $xml, 'rss', 'channel', 'title' );
     132        $this->assertEquals( get_option( 'blogname' ), $title[0]['content'] );
     133
     134        $desc = xml_find( $xml, 'rss', 'channel', 'description' );
     135        $this->assertEquals( get_option( 'blogdescription' ), $desc[0]['content'] );
     136
     137        $link = xml_find( $xml, 'rss', 'channel', 'link' );
     138        $this->assertEquals( get_option( 'siteurl' ), $link[0]['content'] );
     139
     140        $pubdate = xml_find( $xml, 'rss', 'channel', 'lastBuildDate' );
     141        $this->assertEquals( strtotime( get_lastpostmodified() ), strtotime( $pubdate[0]['content'] ) );
    94142    }
    95143
     
    97145     * @ticket UT32
    98146     */
    99     function test_items() {
    100         $this->go_to( '/?feed=rss2' );
    101         $feed = $this->do_rss2();
    102         $xml = xml_to_array($feed);
    103 
    104         // get all the rss -> channel -> item elements
     147    function test_item_elements() {
     148        $this->go_to( '/?feed=rss2' );
     149        $feed = $this->do_rss2();
     150        $xml = xml_to_array( $feed );
     151
     152        // Get all the <item> child elements of the <channel> element
    105153        $items = xml_find( $xml, 'rss', 'channel', 'item' );
    106154
    107         // check each of the items against the known post data
     155        // Verify we are displaying the correct number of posts.
     156        $this->assertCount( $this->post_count, $items );
     157
     158        // We Really only need to test X number of items unless the content is different
     159        $items = array_slice( $items, 1 );
     160
     161        // Check each of the desired entries against the known post data
    108162        foreach ( $items as $key => $item ) {
    109163
     
    113167            $post = get_post( $matches[1] );
    114168
    115             // title
     169            // Title
    116170            $title = xml_find( $items[$key]['child'], 'title' );
    117171            $this->assertEquals( $post->post_title, $title[0]['content'] );
    118172
    119             // link
     173            // Link
    120174            $link = xml_find( $items[$key]['child'], 'link' );
    121175            $this->assertEquals( get_permalink( $post ), $link[0]['content'] );
    122176
    123             // comment link
     177            // Comment link
    124178            $comments_link = xml_find( $items[$key]['child'], 'comments' );
    125             $this->assertEquals( get_permalink( $post) . '#respond', $comments_link[0]['content'] );
    126 
    127             // pub date
     179            $this->assertEquals( get_permalink( $post ) . '#respond', $comments_link[0]['content'] );
     180
     181            // Pub date
    128182            $pubdate = xml_find( $items[$key]['child'], 'pubDate' );
    129183            $this->assertEquals( strtotime( $post->post_date_gmt ), strtotime( $pubdate[0]['content'] ) );
    130184
    131             // author
     185            // Author
    132186            $creator = xml_find( $items[$key]['child'], 'dc:creator' );
    133187            $user = new WP_User( $post->post_author );
    134             $this->assertEquals( $user->user_login, $creator[0]['content'] );
    135 
    136             // categories (perhaps multiple)
     188            $this->assertEquals( $user->display_name, $creator[0]['content'] );
     189
     190            // Categories (perhaps multiple)
    137191            $categories = xml_find( $items[$key]['child'], 'category' );
    138192            $cats = array();
     
    148202            }
    149203            $cats = array_filter( $cats );
    150             // should be the same number of categories
     204            // Should be the same number of categories
    151205            $this->assertEquals( count( $cats ), count( $categories ) );
    152206
    153207            // ..with the same names
    154208            foreach ( $cats as $id => $cat ) {
    155                 $this->assertEquals( $cat, $categories[$id]['content']);
     209                $this->assertEquals( $cat, $categories[$id]['content'] );
    156210            }
    157211
    158212            // GUID
    159213            $guid = xml_find( $items[$key]['child'], 'guid' );
    160             $this->assertEquals('false', $guid[0]['attributes']['isPermaLink'] );
     214            $this->assertEquals( 'false', $guid[0]['attributes']['isPermaLink'] );
    161215            $this->assertEquals( $post->guid, $guid[0]['content'] );
    162216
    163             // description/excerpt
    164             if ( !empty( $post->post_excerpt ) ) {
     217            // Description / Excerpt
     218            if ( ! empty( $post->post_excerpt ) ) {
    165219                $description = xml_find( $items[$key]['child'], 'description' );
    166220                $this->assertEquals( trim( $post->post_excerpt ), trim( $description[0]['content'] ) );
    167221            }
    168222
    169             // post content
    170             if ( !$this->excerpt_only ) {
     223            // Post content
     224            if ( ! $this->excerpt_only ) {
    171225                $content = xml_find( $items[$key]['child'], 'content:encoded' );
    172226                $this->assertEquals( trim( apply_filters( 'the_content', $post->post_content ) ), trim( $content[0]['content'] ) );
    173227            }
    174228
    175             // comment rss
     229            // Comment rss
    176230            $comment_rss = xml_find( $items[$key]['child'], 'wfw:commentRss' );
    177             $this->assertEquals( html_entity_decode( get_post_comments_feed_link( $post->ID) ), $comment_rss[0]['content'] );
     231            $this->assertEquals( html_entity_decode( get_post_comments_feed_link( $post->ID ) ), $comment_rss[0]['content'] );
    178232        }
    179233    }
     
    187241        $this->go_to( '/?feed=rss2' );
    188242        $feed = $this->do_rss2();
    189         $xml = xml_to_array($feed);
     243        $xml = xml_to_array( $feed );
    190244
    191245        // get all the rss -> channel -> item elements
     
    210264        remove_filter( 'comments_open', '__return_false' );
    211265    }
     266
    212267}
Note: See TracChangeset for help on using the changeset viewer.