forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetUser.php
More file actions
150 lines (124 loc) · 5.62 KB
/
getUser.php
File metadata and controls
150 lines (124 loc) · 5.62 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
<?php
/**
* @group xmlrpc
* @group user
*/
class Tests_XMLRPC_wp_getUser extends WP_XMLRPC_UnitTestCase {
protected $administrator_id;
function setUp() {
parent::setUp();
// create a super-admin
$this->administrator_id = $this->make_user_by_role( 'administrator' );
if ( is_multisite() )
grant_super_admin( $this->administrator_id );
}
function tearDown() {
if ( is_multisite() )
revoke_super_admin( $this->administrator_id );
parent::tearDown();
}
function test_invalid_username_password() {
$result = $this->myxmlrpcserver->wp_getUser( array( 1, 'username', 'password', 1 ) );
$this->assertInstanceOf( 'IXR_Error', $result );
$this->assertEquals( 403, $result->code );
}
function test_invalid_user() {
$result = $this->myxmlrpcserver->wp_getUser( array( 1, 'administrator', 'administrator', 34902348908234 ) );
$this->assertInstanceOf( 'IXR_Error', $result );
$this->assertEquals( 404, $result->code );
}
function test_incapable_user() {
$this->make_user_by_role( 'subscriber' );
$editor_id = $this->make_user_by_role( 'editor' );
$result = $this->myxmlrpcserver->wp_getUser( array( 1, 'subscriber', 'subscriber', $editor_id ) );
$this->assertInstanceOf( 'IXR_Error', $result );
$this->assertEquals( 401, $result->code );
}
function test_subscriber_self() {
$subscriber_id = $this->make_user_by_role( 'subscriber' );
$result = $this->myxmlrpcserver->wp_getUser( array( 1, 'subscriber', 'subscriber', $subscriber_id ) );
$this->assertNotInstanceOf( 'IXR_Error', $result );
$this->assertEquals( $subscriber_id, $result['user_id'] );
}
function test_valid_user() {
$registered_date = strtotime( '-1 day' );
$user_data = array(
'user_login' => 'getusertestuser',
'user_pass' => rand_str(),
'first_name' => rand_str(),
'last_name' => rand_str(),
'description' => rand_str( 100 ),
'user_email' => 'getUserTestUser@example.com',
'nickname' => rand_str(),
'user_nicename' => rand_str(),
'display_name' => rand_str(),
'user_url' => 'http://www.example.com/testuser',
'role' => 'author',
'aim' => rand_str(),
'user_registered' => strftime( "%Y-%m-%d %H:%M:%S", $registered_date )
);
$user_id = wp_insert_user( $user_data );
$result = $this->myxmlrpcserver->wp_getUser( array( 1, 'administrator', 'administrator', $user_id ) );
$this->assertNotInstanceOf( 'IXR_Error', $result );
// check data types
$this->assertInternalType( 'string', $result['user_id'] );
$this->assertStringMatchesFormat( '%d', $result['user_id'] );
$this->assertInternalType( 'string', $result['username'] );
$this->assertInternalType( 'string', $result['first_name'] );
$this->assertInternalType( 'string', $result['last_name'] );
$this->assertInstanceOf( 'IXR_Date', $result['registered'] );
$this->assertInternalType( 'string', $result['bio'] );
$this->assertInternalType( 'string', $result['email'] );
$this->assertInternalType( 'string', $result['nickname'] );
$this->assertInternalType( 'string', $result['nicename'] );
$this->assertInternalType( 'string', $result['url'] );
$this->assertInternalType( 'string', $result['display_name'] );
$this->assertInternalType( 'array', $result['roles'] );
// check expected values
$this->assertEquals( $user_id, $result['user_id'] );
$this->assertEquals( $user_data['user_login'], $result['username'] );
$this->assertEquals( $user_data['first_name'], $result['first_name'] );
$this->assertEquals( $user_data['last_name'], $result['last_name'] );
$this->assertEquals( $registered_date, $result['registered']->getTimestamp() );
$this->assertEquals( $user_data['description'], $result['bio'] );
$this->assertEquals( $user_data['user_email'], $result['email'] );
$this->assertEquals( $user_data['nickname'], $result['nickname'] );
$this->assertEquals( $user_data['user_nicename'], $result['nicename'] );
$this->assertEquals( $user_data['user_url'], $result['url'] );
$this->assertEquals( $user_data['display_name'], $result['display_name'] );
$this->assertEquals( $user_data['user_login'], $result['username'] );
$this->assertContains( $user_data['role'], $result['roles'] );
wp_delete_user( $user_id );
}
function test_no_fields() {
$editor_id = $this->make_user_by_role( 'editor' );
$result = $this->myxmlrpcserver->wp_getUser( array( 1, 'administrator', 'administrator', $editor_id, array() ) );
$this->assertNotInstanceOf( 'IXR_Error', $result );
$this->assertEquals( $editor_id, $result['user_id'] );
$expected_fields = array( 'user_id' );
$this->assertEquals( $expected_fields, array_keys( $result ) );
}
function test_basic_fields() {
$editor_id = $this->make_user_by_role( 'editor' );
$result = $this->myxmlrpcserver->wp_getUser( array( 1, 'administrator', 'administrator', $editor_id, array( 'basic' ) ) );
$this->assertNotInstanceOf( 'IXR_Error', $result );
$this->assertEquals( $editor_id, $result['user_id'] );
$expected_fields = array( 'user_id', 'username', 'email', 'registered', 'display_name', 'nicename' );
$keys = array_keys( $result );
sort( $expected_fields );
sort( $keys );
$this->assertEqualSets( $expected_fields, $keys );
}
function test_arbitrary_fields() {
$editor_id = $this->make_user_by_role( 'editor' );
$fields = array( 'email', 'bio', 'user_contacts' );
$result = $this->myxmlrpcserver->wp_getUser( array( 1, 'administrator', 'administrator', $editor_id, $fields ) );
$this->assertNotInstanceOf( 'IXR_Error', $result );
$this->assertEquals( $editor_id, $result['user_id'] );
$expected_fields = array( 'user_id', 'email', 'bio' );
$keys = array_keys( $result );
sort( $expected_fields );
sort( $keys );
$this->assertEqualSets( $expected_fields, $keys );
}
}