|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Test the Atom feed by generating a feed, parsing it, and checking that the |
|
5
|
|
|
* parsed contents match the contents of the posts stored in the database. Since |
|
6
|
|
|
* we're using a real XML parser, this confirms that the feed is valid, well formed, |
|
7
|
|
|
* and contains the right stuff. |
|
8
|
|
|
* |
|
9
|
|
|
* @group feed |
|
10
|
|
|
*/ |
|
11
|
|
|
class Tests_Feeds_Atom extends WP_UnitTestCase |
|
12
|
|
|
{ |
|
13
|
|
|
static $user_id; |
|
14
|
|
|
static $posts; |
|
15
|
|
|
static $category; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Setup a new user and attribute some posts. |
|
19
|
|
|
*/ |
|
20
|
|
|
public static function wpSetUpBeforeClass( $factory ) |
|
|
|
|
|
|
21
|
|
|
{ |
|
22
|
|
|
// Create a user |
|
23
|
|
|
self::$user_id = $factory->user->create( |
|
24
|
|
|
array( |
|
25
|
|
|
'role' => 'author', |
|
26
|
|
|
'user_login' => 'test_author', |
|
27
|
|
|
'display_name' => 'Test A. Uthor', |
|
28
|
|
|
) |
|
29
|
|
|
); |
|
30
|
|
|
|
|
31
|
|
|
// Create a taxonomy |
|
32
|
|
|
self::$category = self::factory()->category->create_and_get( |
|
33
|
|
|
array( |
|
34
|
|
|
'name' => 'Test Category', |
|
35
|
|
|
'slug' => 'test-cat', |
|
36
|
|
|
) |
|
37
|
|
|
); |
|
38
|
|
|
|
|
39
|
|
|
$count = get_option('posts_per_rss') + 1; |
|
40
|
|
|
|
|
41
|
|
|
// Create a few posts |
|
42
|
|
|
self::$posts = $factory->post->create_many( |
|
43
|
|
|
$count, array( |
|
44
|
|
|
'post_author' => self::$user_id, |
|
45
|
|
|
'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.', |
|
46
|
|
|
'post_excerpt' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', |
|
47
|
|
|
) |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
|
|
// Assign a category to those posts |
|
51
|
|
|
foreach ( self::$posts as $post ) { |
|
52
|
|
|
wp_set_object_terms($post, self::$category->slug, 'category'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Setup. |
|
59
|
|
|
*/ |
|
60
|
|
|
public function setUp() |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
parent::setUp(); |
|
63
|
|
|
|
|
64
|
|
|
$this->post_count = (int) get_option('posts_per_rss'); |
|
65
|
|
|
$this->excerpt_only = get_option('rss_use_excerpt'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* This is a bit of a hack used to buffer feed content. |
|
70
|
|
|
*/ |
|
71
|
|
View Code Duplication |
function do_atom() |
|
72
|
|
|
{ |
|
73
|
|
|
ob_start(); |
|
74
|
|
|
// Nasty hack! In the future it would better to leverage do_feed( 'atom' ). |
|
75
|
|
|
global $post; |
|
76
|
|
|
try { |
|
77
|
|
|
@include ABSPATH . 'wp-includes/feed-atom.php'; |
|
|
|
|
|
|
78
|
|
|
$out = ob_get_clean(); |
|
79
|
|
|
} catch ( Exception $e ) { |
|
80
|
|
|
$out = ob_get_clean(); |
|
81
|
|
|
throw( $e ); |
|
82
|
|
|
} |
|
83
|
|
|
return $out; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Test the <feed> element to make sure its present and populated |
|
88
|
|
|
* with the expected child elements and attributes. |
|
89
|
|
|
*/ |
|
90
|
|
|
function test_feed_element() |
|
91
|
|
|
{ |
|
92
|
|
|
$this->go_to('/?feed=atom'); |
|
93
|
|
|
$feed = $this->do_atom(); |
|
94
|
|
|
$xml = xml_to_array($feed); |
|
95
|
|
|
|
|
96
|
|
|
// Get the <feed> child element of <xml>. |
|
97
|
|
|
$atom = xml_find($xml, 'feed'); |
|
98
|
|
|
|
|
99
|
|
|
// There should only be one <feed> child element. |
|
100
|
|
|
$this->assertCount(1, $atom); |
|
101
|
|
|
|
|
102
|
|
|
// Verify attributes. |
|
103
|
|
|
$this->assertEquals('http://www.w3.org/2005/Atom', $atom[0]['attributes']['xmlns']); |
|
104
|
|
|
$this->assertEquals('http://purl.org/syndication/thread/1.0', $atom[0]['attributes']['xmlns:thr']); |
|
105
|
|
|
$this->assertEquals(site_url('/wp-atom.php'), $atom[0]['attributes']['xml:base']); |
|
106
|
|
|
|
|
107
|
|
|
// Verify the <feed> element is present and contains a <title> child element. |
|
108
|
|
|
$title = xml_find($xml, 'feed', 'title'); |
|
109
|
|
|
$this->assertEquals(get_option('blogname'), $title[0]['content']); |
|
110
|
|
|
|
|
111
|
|
|
// Verify the <feed> element is present and contains a <updated> child element. |
|
112
|
|
|
$updated = xml_find($xml, 'feed', 'updated'); |
|
113
|
|
|
$this->assertEquals(strtotime(get_lastpostmodified()), strtotime($updated[0]['content'])); |
|
114
|
|
|
|
|
115
|
|
|
// Verify the <feed> element is present and contains a <subtitle> child element. |
|
116
|
|
|
$subtitle = xml_find($xml, 'feed', 'subtitle'); |
|
117
|
|
|
$this->assertEquals(get_option('blogdescription'), $subtitle[0]['content']); |
|
118
|
|
|
|
|
119
|
|
|
// Verify the <feed> element is present and contains two <link> child elements. |
|
120
|
|
|
$link = xml_find($xml, 'feed', 'link'); |
|
121
|
|
|
$this->assertCount(2, $link); |
|
122
|
|
|
|
|
123
|
|
|
// Verify the <feed> element is present and contains a <link rel="alternate"> child element. |
|
124
|
|
|
$this->assertEquals('alternate', $link[0]['attributes']['rel']); |
|
125
|
|
|
$this->assertEquals(home_url(), $link[0]['attributes']['href']); |
|
126
|
|
|
|
|
127
|
|
|
// Verify the <feed> element is present and contains a <link rel="href"> child element. |
|
128
|
|
|
$this->assertEquals('self', $link[1]['attributes']['rel']); |
|
129
|
|
|
$this->assertEquals(home_url('/?feed=atom'), $link[1]['attributes']['href']); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Validate <entry> child elements. |
|
134
|
|
|
*/ |
|
135
|
|
|
function test_entry_elements() |
|
136
|
|
|
{ |
|
137
|
|
|
$this->go_to('/?feed=atom'); |
|
138
|
|
|
$feed = $this->do_atom(); |
|
139
|
|
|
$xml = xml_to_array($feed); |
|
140
|
|
|
|
|
141
|
|
|
// Get all the <entry> child elements of the <feed> element. |
|
142
|
|
|
$entries = xml_find($xml, 'feed', 'entry'); |
|
143
|
|
|
|
|
144
|
|
|
// Verify we are displaying the correct number of posts. |
|
145
|
|
|
$this->assertCount($this->post_count, $entries); |
|
146
|
|
|
|
|
147
|
|
|
// We Really only need to test X number of entries unless the content is different |
|
148
|
|
|
$entries = array_slice($entries, 1); |
|
149
|
|
|
|
|
150
|
|
|
// Check each of the desired entries against the known post data. |
|
151
|
|
|
foreach ( $entries as $key => $entry ) { |
|
152
|
|
|
|
|
153
|
|
|
// Get post for comparison |
|
154
|
|
|
$id = xml_find($entries[$key]['child'], 'id'); |
|
|
|
|
|
|
155
|
|
|
preg_match('/\?p=(\d+)/', $id[0]['content'], $matches); |
|
156
|
|
|
$post = get_post($matches[1]); |
|
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
// Author |
|
159
|
|
|
$author = xml_find($entries[$key]['child'], 'author', 'name'); |
|
|
|
|
|
|
160
|
|
|
$user = new WP_User($post->post_author); |
|
161
|
|
|
$this->assertEquals($user->display_name, $author[0]['content']); |
|
162
|
|
|
|
|
163
|
|
|
// Title |
|
164
|
|
|
$title = xml_find($entries[$key]['child'], 'title'); |
|
|
|
|
|
|
165
|
|
|
$this->assertEquals($post->post_title, $title[0]['content']); |
|
166
|
|
|
|
|
167
|
|
|
// Link rel="alternate" |
|
168
|
|
|
$link_alts = xml_find($entries[$key]['child'], 'link'); |
|
|
|
|
|
|
169
|
|
|
foreach ( $link_alts as $link_alt ) { |
|
170
|
|
|
if ('alternate' == $link_alt['attributes']['rel'] ) { |
|
171
|
|
|
$this->assertEquals(get_permalink($post), $link_alt['attributes']['href']); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
// Id |
|
176
|
|
|
$guid = xml_find($entries[$key]['child'], 'id'); |
|
|
|
|
|
|
177
|
|
|
$this->assertEquals($post->guid, $id[0]['content']); |
|
178
|
|
|
|
|
179
|
|
|
// Updated |
|
180
|
|
|
$updated = xml_find($entries[$key]['child'], 'updated'); |
|
|
|
|
|
|
181
|
|
|
$this->assertEquals(strtotime($post->post_modified_gmt), strtotime($updated[0]['content'])); |
|
182
|
|
|
|
|
183
|
|
|
// Published |
|
184
|
|
|
$published = xml_find($entries[$key]['child'], 'published'); |
|
|
|
|
|
|
185
|
|
|
$this->assertEquals(strtotime($post->post_date_gmt), strtotime($published[0]['content'])); |
|
186
|
|
|
|
|
187
|
|
|
// Category |
|
188
|
|
|
foreach ( get_the_category($post->ID) as $term ) { |
|
189
|
|
|
$terms[] = $term->name; |
|
|
|
|
|
|
190
|
|
|
} |
|
191
|
|
|
$categories = xml_find($entries[$key]['child'], 'category'); |
|
|
|
|
|
|
192
|
|
|
foreach ( $categories as $category ) { |
|
193
|
|
|
$this->assertTrue(in_array($category['attributes']['term'], $terms)); |
|
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
unset($terms); |
|
196
|
|
|
|
|
197
|
|
|
// Content |
|
198
|
|
View Code Duplication |
if (! $this->excerpt_only ) { |
|
|
|
|
|
|
199
|
|
|
$content = xml_find($entries[$key]['child'], 'content'); |
|
|
|
|
|
|
200
|
|
|
$this->assertEquals(trim(apply_filters('the_content', $post->post_content)), trim($content[0]['content'])); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
// Link rel="replies" |
|
204
|
|
|
$link_replies = xml_find($entries[$key]['child'], 'link'); |
|
|
|
|
|
|
205
|
|
|
foreach ( $link_replies as $link_reply ) { |
|
206
|
|
|
if ('replies' == $link_reply['attributes']['rel'] && 'application/atom+xml' == $link_reply['attributes']['type'] ) { |
|
207
|
|
|
$this->assertEquals(get_post_comments_feed_link($post->ID, 'atom'), $link_reply['attributes']['href']); |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|