Make WordPress Core

Changeset 30283


Ignore:
Timestamp:
11/08/2014 08:54:06 PM (12 years ago)
Author:
boonebgorges
Message:

Improve Tests_Feed_RSS2::test_items().

  • Better reference to post author, to avoid PHP notices.
  • Code styling.
  • More reliable checking of tags and categories.

Props kurtpayne.
Fixes ​#UT32. See #30284.

File:
1 edited

Legend:

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

    r25002 r30283  
    9292
    9393                // check each of the items against the known post data
    94                 for ($i=0; $i < $this->post_count; $i++) {
     94                foreach ( $items as $key => $item ) {
     95
     96                        // Get post for comparison
     97                        $guid = xml_find( $items[$key]['child'], 'guid' );
     98                        preg_match( '/\?p=(\d+)/', $guid[0]['content'], $matches );
     99                        $post = get_post( $matches[1] );
    95100
    96101                        // title
    97                         $title = xml_find($items[$i]['child'], 'title');
    98                         $this->assertEquals($posts[$i]->post_title, $title[0]['content']);
     102                        $title = xml_find( $items[$key]['child'], 'title' );
     103                        $this->assertEquals( $post->post_title, $title[0]['content'] );
    99104
    100105                        // link
    101                         $link = xml_find($items[$i]['child'], 'link');
    102                         $this->assertEquals(get_permalink($posts[$i]->ID), $link[0]['content']);
     106                        $link = xml_find( $items[$key]['child'], 'link' );
     107                        $this->assertEquals( get_permalink( $post ), $link[0]['content'] );
    103108
    104109                        // comment link
    105                         $comments_link = xml_find($items[$i]['child'], 'comments');
    106                         $this->assertEquals(get_permalink($posts[$i]->ID) . '#comments', $comments_link[0]['content']);
     110                        $comments_link = xml_find( $items[$key]['child'], 'comments' );
     111                        $this->assertEquals( get_permalink( $post) . '#comments', $comments_link[0]['content'] );
    107112
    108113                        // pub date
    109                         $pubdate = xml_find($items[$i]['child'], 'pubDate');
    110                         $this->assertEquals(strtotime($posts[$i]->post_date), strtotime($pubdate[0]['content']));
     114                        $pubdate = xml_find( $items[$key]['child'], 'pubDate' );
     115                        $this->assertEquals( strtotime( $post->post_date_gmt ), strtotime( $pubdate[0]['content'] ) );
    111116
    112117                        // author
    113                         $creator = xml_find($items[$i]['child'], 'dc:creator');
    114                         $this->assertEquals($this->author->user_nicename, $creator[0]['content']);
     118                        $creator = xml_find( $items[$key]['child'], 'dc:creator' );
     119                        $user = new WP_User( $post->post_author );
     120                        $this->assertEquals( $user->user_login, $creator[0]['content'] );
    115121
    116122                        // categories (perhaps multiple)
    117                         $categories = xml_find($items[$i]['child'], 'category');
    118                         $cat_ids = wp_get_post_categories($post->ID);
    119                         if (empty($cat_ids))    $cat_ids = array(1);
     123                        $categories = xml_find( $items[$key]['child'], 'category' );
     124                        $cats = array();
     125                        foreach ( get_the_category( $post->ID ) as $term ) {
     126                                $cats[] = $term->name;
     127                        }
     128                        foreach ( get_the_tags( $post->ID ) as $term ) {
     129                                $cats[] = $term->name;
     130                        }
     131                        $cats = array_filter( $cats );
    120132                        // should be the same number of categories
    121                         $this->assertEquals(count($cat_ids), count($categories));
     133                        $this->assertEquals( count( $cats ), count( $categories ) );
     134
    122135                        // ..with the same names
    123                         for ($j=0; $j < count($cat_ids); $j++)
    124                                 $this->assertEquals(get_cat_name($cat_ids[$j]), $categories[$j]['content']);
     136                        foreach ( $cats as $id => $cat ) {
     137                                $this->assertEquals( $cat, $categories[$id]['content']);
     138                        }
    125139
    126140                        // GUID
    127                         $guid = xml_find($items[$i]['child'], 'guid');
    128                         $this->assertEquals('false', $guid[0]['attributes']['isPermaLink']);
    129                         $this->assertEquals($posts[$i]->guid, $guid[0]['content']);
     141                        $guid = xml_find( $items[$key]['child'], 'guid' );
     142                        $this->assertEquals('false', $guid[0]['attributes']['isPermaLink'] );
     143                        $this->assertEquals( $post->guid, $guid[0]['content'] );
    130144
    131145                        // description/excerpt
    132                         $description = xml_find($items[$i]['child'], 'description');
    133                         $this->assertEquals(trim($posts[$i]->post_excerpt), trim($description[0]['content']));
     146                        if ( !empty( $post->post_excerpt ) ) {
     147                                $description = xml_find( $items[$key]['child'], 'description' );
     148                                $this->assertEquals( trim( $post->post_excerpt ), trim( $description[0]['content'] ) );
     149                        }
    134150
    135151                        // post content
    136                         if (!$this->excerpt_only) {
    137                                 $content = xml_find($items[$i]['child'], 'content:encoded');
    138                                 $this->assertEquals(trim(apply_filters('the_content', $posts[$i]->post_content)), trim($content[0]['content']));
     152                        if ( !$this->excerpt_only ) {
     153                                $content = xml_find( $items[$key]['child'], 'content:encoded' );
     154                                $this->assertEquals( trim( apply_filters( 'the_content', $post->post_content ) ), trim( $content[0]['content'] ) );
    139155                        }
    140156
    141157                        // comment rss
    142                         $comment_rss = xml_find($items[$i]['child'], 'wfw:commentRss');
    143                         $this->assertEquals(html_entity_decode(get_post_comments_feed_link($posts[$i]->ID)), $comment_rss[0]['content']);
     158                        $comment_rss = xml_find( $items[$key]['child'], 'wfw:commentRss' );
     159                        $this->assertEquals( html_entity_decode( get_post_comments_feed_link( $post->ID) ), $comment_rss[0]['content'] );
    144160                }
    145 
    146161        }
    147162}
Note: See TracChangeset for help on using the changeset viewer.