forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavatar.php
More file actions
283 lines (232 loc) · 7.91 KB
/
avatar.php
File metadata and controls
283 lines (232 loc) · 7.91 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
/**
* Test avatar related functions
*
* @group avatar
*/
class Tests_Avatar extends WP_UnitTestCase {
/**
* @ticket 21195
*/
public function test_get_avatar_url_gravatar_url() {
$url = get_avatar_url( 1 );
$this->assertEquals( preg_match( '|^http?://[0-9]+.gravatar.com/avatar/[0-9a-f]{32}\?|', $url ), 1 );
}
/**
* @ticket 21195
*/
public function test_get_avatar_url_size() {
$url = get_avatar_url( 1 );
$this->assertEquals( preg_match( '|\?.*s=96|', $url ), 1 );
$args = array( 'size' => 100 );
$url = get_avatar_url( 1, $args );
$this->assertEquals( preg_match( '|\?.*s=100|', $url ), 1 );
}
/**
* @ticket 21195
*/
public function test_get_avatar_url_default() {
$url = get_avatar_url( 1 );
$this->assertEquals( preg_match( '|\?.*d=mm|', $url ), 1 );
$args = array( 'default' => 'wavatar' );
$url = get_avatar_url( 1, $args );
$this->assertEquals( preg_match( '|\?.*d=wavatar|', $url ), 1 );
$this->assertEquals( preg_match( '|\?.*f=y|', $url ), 0 );
$args = array( 'force_default' => true );
$url = get_avatar_url( 1, $args );
$this->assertEquals( preg_match( '|\?.*f=y|', $url ), 1 );
}
/**
* @ticket 21195
*/
public function test_get_avatar_url_rating() {
$url = get_avatar_url( 1 );
$this->assertEquals( preg_match( '|\?.*r=g|', $url ), 1 );
$args = array( 'rating' => 'M' );
$url = get_avatar_url( 1, $args );
$this->assertEquals( preg_match( '|\?.*r=m|', $url ), 1 );
}
/**
* @ticket 21195
*/
public function test_get_avatar_url_scheme() {
$url = get_avatar_url( 1 );
$this->assertEquals( preg_match( '|^http://|', $url ), 1 );
$args = array( 'scheme' => 'https' );
$url = get_avatar_url( 1, $args );
$this->assertEquals( preg_match( '|^https://|', $url ), 1 );
$args = array( 'scheme' => 'lolcat' );
$url = get_avatar_url( 1, $args );
$this->assertEquals( preg_match( '|^lolcat://|', $url ), 0 );
}
/**
* @ticket 21195
*/
public function test_get_avatar_url_user() {
$url = get_avatar_url( 1 );
$url2 = get_avatar_url( WP_TESTS_EMAIL );
$this->assertEquals( $url, $url2 );
$url2 = get_avatar_url( md5( WP_TESTS_EMAIL ) . '@md5.gravatar.com' );
$this->assertEquals( $url, $url2 );
$user = get_user_by( 'id', 1 );
$url2 = get_avatar_url( $user );
$this->assertEquals( $url, $url2 );
$post_id = self::factory()->post->create( array( 'post_author' => 1 ) );
$post = get_post( $post_id );
$url2 = get_avatar_url( $post );
$this->assertEquals( $url, $url2 );
$comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'user_id' => 1,
)
);
$comment = get_comment( $comment_id );
$url2 = get_avatar_url( $comment );
$this->assertEquals( $url, $url2 );
}
protected $fake_url;
/**
* @ticket 21195
*/
public function test_pre_get_avatar_url_filter() {
$this->fake_url = 'haha wat';
add_filter( 'pre_get_avatar_data', array( $this, 'pre_get_avatar_url_filter' ), 10, 1 );
$url = get_avatar_url( 1 );
remove_filter( 'pre_get_avatar_data', array( $this, 'pre_get_avatar_url_filter' ), 10 );
$this->assertEquals( $url, $this->fake_url );
}
public function pre_get_avatar_url_filter( $args ) {
$args['url'] = $this->fake_url;
return $args;
}
/**
* @ticket 21195
*/
public function test_get_avatar_url_filter() {
$this->fake_url = 'omg lol';
add_filter( 'get_avatar_url', array( $this, 'get_avatar_url_filter' ), 10, 1 );
$url = get_avatar_url( 1 );
remove_filter( 'get_avatar_url', array( $this, 'get_avatar_url_filter' ), 10 );
$this->assertEquals( $url, $this->fake_url );
}
public function get_avatar_url_filter( $url ) {
return $this->fake_url;
}
/**
* @ticket 21195
*/
public function test_get_avatar_comment_types_filter() {
$url = get_avatar_url( 1 );
$post_id = self::factory()->post->create( array( 'post_author' => 1 ) );
$comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'user_id' => 1,
'comment_type' => 'pingback',
)
);
$comment = get_comment( $comment_id );
$url2 = get_avatar_url( $comment );
$this->assertFalse( $url2 );
add_filter( 'get_avatar_comment_types', array( $this, 'get_avatar_comment_types_filter' ), 10, 1 );
$url2 = get_avatar_url( $comment );
remove_filter( 'get_avatar_comment_types', array( $this, 'get_avatar_comment_types_filter' ), 10 );
$this->assertEquals( $url, $url2 );
}
public function get_avatar_comment_types_filter( $comment_types ) {
$comment_types[] = 'pingback';
return $comment_types;
}
public function test_get_avatar() {
$img = get_avatar( 1 );
$this->assertEquals( preg_match( "|^<img alt='[^']*' src='[^']*' srcset='[^']*' class='[^']*' height='[^']*' width='[^']*' />$|", $img ), 1 );
}
public function test_get_avatar_size() {
$size = '100';
$img = get_avatar( 1, $size );
$this->assertEquals( preg_match( "|^<img .*height='$size'.*width='$size'|", $img ), 1 );
}
public function test_get_avatar_alt() {
$alt = 'Mr Hyde';
$img = get_avatar( 1, 96, '', $alt );
$this->assertEquals( preg_match( "|^<img alt='$alt'|", $img ), 1 );
}
public function test_get_avatar_class() {
$class = 'first';
$img = get_avatar( 1, 96, '', '', array( 'class' => $class ) );
$this->assertEquals( preg_match( "|^<img .*class='[^']*{$class}[^']*'|", $img ), 1 );
}
public function test_get_avatar_default_class() {
$img = get_avatar( 1, 96, '', '', array( 'force_default' => true ) );
$this->assertEquals( preg_match( "|^<img .*class='[^']*avatar-default[^']*'|", $img ), 1 );
}
public function test_get_avatar_force_display() {
$old = get_option( 'show_avatars' );
update_option( 'show_avatars', false );
$this->assertFalse( get_avatar( 1 ) );
$this->assertNotEmpty( get_avatar( 1, 96, '', '', array( 'force_display' => true ) ) );
update_option( 'show_avatars', $old );
}
protected $fake_img;
/**
* @ticket 21195
*/
public function test_pre_get_avatar_filter() {
$this->fake_img = 'YOU TOO?!';
add_filter( 'pre_get_avatar', array( $this, 'pre_get_avatar_filter' ), 10, 1 );
$img = get_avatar( 1 );
remove_filter( 'pre_get_avatar', array( $this, 'pre_get_avatar_filter' ), 10 );
$this->assertEquals( $img, $this->fake_img );
}
public function pre_get_avatar_filter( $img ) {
return $this->fake_img;
}
/**
* @ticket 21195
*/
public function test_get_avatar_filter() {
$this->fake_url = 'YA RLY';
add_filter( 'get_avatar', array( $this, 'get_avatar_filter' ), 10, 1 );
$img = get_avatar( 1 );
remove_filter( 'get_avatar', array( $this, 'get_avatar_filter' ), 10 );
$this->assertEquals( $img, $this->fake_url );
}
public function get_avatar_filter( $img ) {
return $this->fake_url;
}
/**
* The `get_avatar_data()` function should return gravatar url when comment type allowed to retrieve avatars.
*
* @ticket 44033
*/
public function test_get_avatar_data_should_return_gravatar_url_when_input_avatar_comment_type() {
$comment_type = 'comment';
$comment = self::factory()->comment->create_and_get(
array(
'comment_author_email' => 'commenter@example.com',
'comment_type' => $comment_type,
)
);
$actual_data = get_avatar_data( $comment );
$this->assertTrue( is_avatar_comment_type( $comment_type ) );
$this->assertRegexp( '|^http?://[0-9]+.gravatar.com/avatar/[0-9a-f]{32}\?|', $actual_data['url'] );
}
/**
* The `get_avatar_data()` function should return invalid url when comment type not allowed to retrieve avatars.
*
* @ticket 44033
*/
public function test_get_avatar_data_should_return_invalid_url_when_input_not_avatar_comment_type() {
$comment_type = 'review';
$comment = self::factory()->comment->create_and_get(
array(
'comment_author_email' => 'commenter@example.com',
'comment_type' => $comment_type,
)
);
$actual_data = get_avatar_data( $comment );
$this->assertFalse( is_avatar_comment_type( $comment_type ) );
$this->assertFalse( $actual_data['url'] );
}
}