forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetPosts.php
More file actions
171 lines (146 loc) · 5.51 KB
/
getPosts.php
File metadata and controls
171 lines (146 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* @group xmlrpc
*/
class Tests_XMLRPC_wp_getPosts extends WP_XMLRPC_UnitTestCase {
function test_invalid_username_password() {
$result = $this->myxmlrpcserver->wp_getPosts( array( 1, 'username', 'password' ) );
$this->assertIXRError( $result );
$this->assertEquals( 403, $result->code );
}
/**
* @ticket 20991
*/
function test_incapable_user() {
$this->make_user_by_role( 'subscriber' );
$result = $this->myxmlrpcserver->wp_getPosts( array( 1, 'subscriber', 'subscriber' ) );
$this->assertIXRError( $result );
$this->assertEquals( 401, $result->code );
$filter = array( 'post_type' => 'page' );
$result = $this->myxmlrpcserver->wp_getPosts( array( 1, 'subscriber', 'subscriber', $filter ) );
$this->assertIXRError( $result );
$this->assertEquals( 401, $result->code );
}
function test_capable_user() {
$this->make_user_by_role( 'editor' );
$result = $this->myxmlrpcserver->wp_getPosts( array( 1, 'editor', 'editor' ) );
$this->assertNotIXRError( $result );
}
function test_invalid_post_type() {
$this->make_user_by_role( 'editor' );
$filter = array( 'post_type' => 'invalid_post_type_name' );
$result = $this->myxmlrpcserver->wp_getPosts( array( 1, 'editor', 'editor', $filter ) );
$this->assertIXRError( $result );
}
function test_filters() {
$this->make_user_by_role( 'editor' );
$cpt_name = 'test_wp_getposts_cpt';
register_post_type(
$cpt_name,
array(
'taxonomies' => array( 'post_tag', 'category' ),
'public' => true,
)
);
$post_ids = array();
$num_posts = 4;
foreach ( range( 1, $num_posts ) as $i ) {
$post_ids[] = self::factory()->post->create(
array(
'post_type' => $cpt_name,
'post_date' => date( 'Y-m-d H:i:s', time() + $i ),
)
);
}
// get them all
$filter = array(
'post_type' => $cpt_name,
'number' => $num_posts + 10,
);
$results = $this->myxmlrpcserver->wp_getPosts( array( 1, 'editor', 'editor', $filter ) );
$this->assertNotIXRError( $results );
$this->assertEquals( $num_posts, count( $results ) );
// page through results
$posts_found = array();
$filter['number'] = 2;
$filter['offset'] = 0;
do {
$presults = $this->myxmlrpcserver->wp_getPosts( array( 1, 'editor', 'editor', $filter ) );
$posts_found = array_merge( $posts_found, wp_list_pluck( $presults, 'post_id' ) );
$filter['offset'] += $filter['number'];
} while ( count( $presults ) > 0 );
// verify that $post_ids matches $posts_found
$this->assertEquals( 0, count( array_diff( $post_ids, $posts_found ) ) );
// add comments to some of the posts
foreach ( $post_ids as $key => $post_id ) {
// Larger post IDs will get more comments.
self::factory()->comment->create_post_comments( $post_id, $key );
}
// get results ordered by comment count
$filter2 = array(
'post_type' => $cpt_name,
'number' => $num_posts,
'orderby' => 'comment_count',
'order' => 'DESC',
);
$results2 = $this->myxmlrpcserver->wp_getPosts( array( 1, 'editor', 'editor', $filter2 ) );
$this->assertNotIXRError( $results2 );
$last_comment_count = 100;
foreach ( $results2 as $post ) {
$comment_count = intval( get_comments_number( $post['post_id'] ) );
$this->assertLessThanOrEqual( $last_comment_count, $comment_count );
$last_comment_count = $comment_count;
}
// set one of the posts to draft and get drafts
$post = get_post( $post_ids[0] );
$post->post_status = 'draft';
wp_update_post( $post );
$filter3 = array(
'post_type' => $cpt_name,
'post_status' => 'draft',
);
$results3 = $this->myxmlrpcserver->wp_getPosts( array( 1, 'editor', 'editor', $filter3 ) );
$this->assertNotIXRError( $results3 );
$this->assertEquals( 1, count( $results3 ) );
$this->assertEquals( $post->ID, $results3[0]['post_id'] );
_unregister_post_type( $cpt_name );
}
function test_fields() {
$this->make_user_by_role( 'editor' );
self::factory()->post->create();
// check default fields
$results = $this->myxmlrpcserver->wp_getPosts( array( 1, 'editor', 'editor' ) );
$this->assertNotIXRError( $results );
$expected_fields = array( 'post_id', 'post_title', 'terms', 'custom_fields', 'link' ); // subset of expected fields
foreach ( $expected_fields as $field ) {
$this->assertArrayHasKey( $field, $results[0] );
}
// request specific fields and verify that only those are returned
$filter = array();
$fields = array( 'post_name', 'post_author', 'enclosure' );
$results2 = $this->myxmlrpcserver->wp_getPosts( array( 1, 'editor', 'editor', $filter, $fields ) );
$this->assertNotIXRError( $results2 );
$expected_fields = array_merge( $fields, array( 'post_id' ) );
foreach ( array_keys( $results2[0] ) as $field ) {
$this->assertContains( $field, $expected_fields );
}
}
/**
* @ticket 21623
*/
function test_search() {
$this->make_user_by_role( 'editor' );
$post_ids[] = self::factory()->post->create( array( 'post_title' => 'First: Hello, World!' ) );
$post_ids[] = self::factory()->post->create( array( 'post_title' => 'Second: Hello, World!' ) );
// Search for none of them
$filter = array( 's' => 'Third' );
$results = $this->myxmlrpcserver->wp_getPosts( array( 1, 'editor', 'editor', $filter ) );
$this->assertNotIXRError( $results );
$this->assertEquals( 0, count( $results ) );
// Search for one of them
$filter = array( 's' => 'First:' );
$results = $this->myxmlrpcserver->wp_getPosts( array( 1, 'editor', 'editor', $filter ) );
$this->assertNotIXRError( $results );
$this->assertEquals( 1, count( $results ) );
}
}