Changeset 36519
- Timestamp:
- 02/12/2016 07:02:25 PM (9 years ago)
- Location:
- trunk/tests/phpunit/tests/feed
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/feed/rss2.php
r35506 r36519 2 2 3 3 /** 4 * test the RSS 2.0 feed by generating a feed, parsing it, and checking that the4 * Test the RSS 2.0 feed by generating a feed, parsing it, and checking that the 5 5 * parsed contents match the contents of the posts stored in the database. Since 6 6 * we're using a real XML parser, this confirms that the feed is valid, well formed, … … 9 9 * @group feed 10 10 */ 11 class Tests_Feed _RSS2 extends WP_UnitTestCase {11 class Tests_Feeds_RSS2 extends WP_UnitTestCase { 12 12 static $user_id; 13 13 static $posts; 14 14 static $category; 15 16 /** 17 * Setup a new user and attribute some posts. 18 */ 15 19 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', 19 25 ) ); 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 */ 22 49 public static function wpTearDownAfterClass() { 50 // Delete our user 23 51 self::delete_user( self::$user_id ); 24 52 53 // Delete all of our posts 25 54 foreach ( self::$posts as $post ) { 26 55 wp_delete_post( $post, true ); 27 56 } 28 } 29 57 58 // Delete our taxonomy 59 wp_delete_category( self::$category->term_id ); 60 } 61 62 /** 63 * Setup. 64 */ 30 65 public function setUp() { 31 66 parent::setUp(); 32 67 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' ); 35 70 // 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 */ 39 77 function do_rss2() { 40 78 ob_start(); 41 // nasty hack79 // Nasty hack! In the future it would better to leverage do_feed( 'rss2' ). 42 80 global $post; 43 81 try { … … 51 89 } 52 90 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'] ); 68 110 69 111 // 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 ); 77 123 78 124 // 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'] ) ); 94 142 } 95 143 … … 97 145 * @ticket UT32 98 146 */ 99 function test_item s() {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 elements147 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 105 153 $items = xml_find( $xml, 'rss', 'channel', 'item' ); 106 154 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 108 162 foreach ( $items as $key => $item ) { 109 163 … … 113 167 $post = get_post( $matches[1] ); 114 168 115 // title169 // Title 116 170 $title = xml_find( $items[$key]['child'], 'title' ); 117 171 $this->assertEquals( $post->post_title, $title[0]['content'] ); 118 172 119 // link173 // Link 120 174 $link = xml_find( $items[$key]['child'], 'link' ); 121 175 $this->assertEquals( get_permalink( $post ), $link[0]['content'] ); 122 176 123 // comment link177 // Comment link 124 178 $comments_link = xml_find( $items[$key]['child'], 'comments' ); 125 $this->assertEquals( get_permalink( $post ) . '#respond', $comments_link[0]['content'] );126 127 // pub date179 $this->assertEquals( get_permalink( $post ) . '#respond', $comments_link[0]['content'] ); 180 181 // Pub date 128 182 $pubdate = xml_find( $items[$key]['child'], 'pubDate' ); 129 183 $this->assertEquals( strtotime( $post->post_date_gmt ), strtotime( $pubdate[0]['content'] ) ); 130 184 131 // author185 // Author 132 186 $creator = xml_find( $items[$key]['child'], 'dc:creator' ); 133 187 $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) 137 191 $categories = xml_find( $items[$key]['child'], 'category' ); 138 192 $cats = array(); … … 148 202 } 149 203 $cats = array_filter( $cats ); 150 // should be the same number of categories204 // Should be the same number of categories 151 205 $this->assertEquals( count( $cats ), count( $categories ) ); 152 206 153 207 // ..with the same names 154 208 foreach ( $cats as $id => $cat ) { 155 $this->assertEquals( $cat, $categories[$id]['content'] );209 $this->assertEquals( $cat, $categories[$id]['content'] ); 156 210 } 157 211 158 212 // GUID 159 213 $guid = xml_find( $items[$key]['child'], 'guid' ); 160 $this->assertEquals( 'false', $guid[0]['attributes']['isPermaLink'] );214 $this->assertEquals( 'false', $guid[0]['attributes']['isPermaLink'] ); 161 215 $this->assertEquals( $post->guid, $guid[0]['content'] ); 162 216 163 // description/excerpt164 if ( ! empty( $post->post_excerpt ) ) {217 // Description / Excerpt 218 if ( ! empty( $post->post_excerpt ) ) { 165 219 $description = xml_find( $items[$key]['child'], 'description' ); 166 220 $this->assertEquals( trim( $post->post_excerpt ), trim( $description[0]['content'] ) ); 167 221 } 168 222 169 // post content170 if ( ! $this->excerpt_only ) {223 // Post content 224 if ( ! $this->excerpt_only ) { 171 225 $content = xml_find( $items[$key]['child'], 'content:encoded' ); 172 226 $this->assertEquals( trim( apply_filters( 'the_content', $post->post_content ) ), trim( $content[0]['content'] ) ); 173 227 } 174 228 175 // comment rss229 // Comment rss 176 230 $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'] ); 178 232 } 179 233 } … … 187 241 $this->go_to( '/?feed=rss2' ); 188 242 $feed = $this->do_rss2(); 189 $xml = xml_to_array( $feed);243 $xml = xml_to_array( $feed ); 190 244 191 245 // get all the rss -> channel -> item elements … … 210 264 remove_filter( 'comments_open', '__return_false' ); 211 265 } 266 212 267 }
Note: See TracChangeset
for help on using the changeset viewer.