forked from WordPress/wordpress-develop
/
getRecentPosts.php
144 lines (118 loc) · 5.3 KB
/
getRecentPosts.php
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
<?php
/**
* @group xmlrpc
*/
class Tests_XMLRPC_mw_getRecentPosts extends WP_XMLRPC_UnitTestCase {
protected static $post_id;
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$post_id = $factory->post->create(
array(
'post_type' => 'page',
'post_author' => $factory->user->create(
array(
'user_login' => 'author',
'user_pass' => 'author',
'role' => 'author',
)
),
'post_date' => strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 day' ) ),
)
);
}
function test_invalid_username_password() {
$result = $this->myxmlrpcserver->mw_getRecentPosts( array( 1, 'username', 'password' ) );
$this->assertIXRError( $result );
$this->assertEquals( 403, $result->code );
}
/**
* @ticket 22320
*/
function test_no_editing_privileges() {
$this->make_user_by_role( 'subscriber' );
$result = $this->myxmlrpcserver->mw_getRecentPosts( array( 1, 'subscriber', 'subscriber' ) );
$this->assertIXRError( $result );
$this->assertEquals( 401, $result->code );
}
function test_no_editable_posts() {
wp_delete_post( self::$post_id, true );
$result = $this->myxmlrpcserver->mw_getRecentPosts( array( 1, 'author', 'author' ) );
$this->assertNotIXRError( $result );
$this->assertEquals( 0, count( $result ) );
}
function test_valid_post() {
add_theme_support( 'post-thumbnails' );
$fields = array( 'post' );
$results = $this->myxmlrpcserver->mw_getRecentPosts( array( 1, 'author', 'author' ) );
$this->assertNotIXRError( $results );
foreach ( $results as $result ) {
$post = get_post( $result['postid'] );
// Check data types.
$this->assertIsString( $result['userid'] );
$this->assertIsString( $result['postid'] );
$this->assertIsString( $result['description'] );
$this->assertIsString( $result['title'] );
$this->assertIsString( $result['link'] );
$this->assertIsString( $result['permaLink'] );
$this->assertIsArray( $result['categories'] );
$this->assertIsString( $result['mt_excerpt'] );
$this->assertIsString( $result['mt_text_more'] );
$this->assertIsString( $result['wp_more_text'] );
$this->assertIsInt( $result['mt_allow_comments'] );
$this->assertIsInt( $result['mt_allow_pings'] );
$this->assertIsString( $result['mt_keywords'] );
$this->assertIsString( $result['wp_slug'] );
$this->assertIsString( $result['wp_password'] );
$this->assertIsString( $result['wp_author_id'] );
$this->assertIsString( $result['wp_author_display_name'] );
$this->assertIsString( $result['post_status'] );
$this->assertIsArray( $result['custom_fields'] );
$this->assertIsString( $result['wp_post_format'] );
// Check expected values.
$this->assertStringMatchesFormat( '%d', $result['userid'] );
$this->assertStringMatchesFormat( '%d', $result['postid'] );
$this->assertEquals( $post->post_title, $result['title'] );
$this->assertEquals( 'draft', $result['post_status'] );
$this->assertStringMatchesFormat( '%d', $result['wp_author_id'] );
$this->assertEquals( $post->post_excerpt, $result['mt_excerpt'] );
$this->assertEquals( url_to_postid( $result['link'] ), $post->ID );
$this->assertEquals( '', $result['wp_post_thumbnail'] );
}
remove_theme_support( 'post-thumbnails' );
}
function test_post_thumbnail() {
add_theme_support( 'post-thumbnails' );
// Create attachment.
$filename = ( DIR_TESTDATA . '/images/a2-small.jpg' );
$attachment_id = self::factory()->attachment->create_upload_object( $filename, self::$post_id );
set_post_thumbnail( self::$post_id, $attachment_id );
$results = $this->myxmlrpcserver->mw_getRecentPosts( array( self::$post_id, 'author', 'author' ) );
$this->assertNotIXRError( $results );
foreach ( $results as $result ) {
$this->assertIsString( $result['wp_post_thumbnail'] );
$this->assertStringMatchesFormat( '%d', $result['wp_post_thumbnail'] );
if ( ! empty( $result['wp_post_thumbnail'] ) || $result['postid'] === self::$post_id ) {
$attachment_id = get_post_meta( $result['postid'], '_thumbnail_id', true );
$this->assertEquals( $attachment_id, $result['wp_post_thumbnail'] );
}
}
remove_theme_support( 'post-thumbnails' );
}
function test_date() {
$this->make_user_by_role( 'editor' );
$results = $this->myxmlrpcserver->mw_getRecentPosts( array( 1, 'editor', 'editor' ) );
$this->assertNotIXRError( $results );
foreach ( $results as $result ) {
$post = get_post( $result['postid'] );
$date_gmt = strtotime( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $post->post_date, false ), 'Ymd\TH:i:s' ) );
$date_modified_gmt = strtotime( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $post->post_modified, false ), 'Ymd\TH:i:s' ) );
$this->assertInstanceOf( 'IXR_Date', $result['dateCreated'] );
$this->assertInstanceOf( 'IXR_Date', $result['date_created_gmt'] );
$this->assertInstanceOf( 'IXR_Date', $result['date_modified'] );
$this->assertInstanceOf( 'IXR_Date', $result['date_modified_gmt'] );
$this->assertEquals( strtotime( $post->post_date ), $result['dateCreated']->getTimestamp() );
$this->assertEquals( $date_gmt, $result['date_created_gmt']->getTimestamp() );
$this->assertEquals( strtotime( $post->post_date ), $result['date_modified']->getTimestamp() );
$this->assertEquals( $date_modified_gmt, $result['date_modified_gmt']->getTimestamp() );
}
}
}