forked from timber/timber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-timber-comment-avatar.php
168 lines (145 loc) · 6.1 KB
/
test-timber-comment-avatar.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* @group comments-api
*/
class TestTimberCommentAvatar extends Timber_UnitTestCase
{
public function testAvatarSize()
{
if (!TestTimberImage::is_connected()) {
$this->markTestSkipped('Cannot test avatar images when not connected to internet');
}
$post_id = $this->factory->post->create();
$comment_id = $this->factory->comment->create([
'comment_post_ID' => $post_id,
]);
$comment = Timber\Timber::get_comment($comment_id);
# test default gravatr holding image
$avatar = $comment->avatar("mystery");
$this->assertTrue(substr($avatar, 0, 5) == "http:");
}
public function testAvatarFalse()
{
update_option('show_avatars', false);
$post_id = $this->factory->post->create();
$comment_id = $this->factory->comment->create([
'comment_post_ID' => $post_id,
]);
$comment = Timber\Timber::get_comment($comment_id);
# test default gravatr holding image
$avatar = $comment->avatar();
$this->assertFalse($avatar);
}
public function testAvatarBlank()
{
if (!TestTimberImage::is_connected()) {
$this->markTestSkipped('Cannot test avatar images when not connected to internet');
}
$post_id = $this->factory->post->create();
$comment_id = $this->factory->comment->create([
'comment_post_ID' => $post_id,
]);
$comment = Timber\Timber::get_comment($comment_id);
# test default gravatr holding image
$avatar = $comment->avatar(92, "blank");
$this->assertTrue(substr($avatar, 0, 5) == "http:");
}
public function testAvatarGravatarDefault()
{
if (!TestTimberImage::is_connected()) {
$this->markTestSkipped('Cannot test avatar images when not connected to internet');
}
$post_id = $this->factory->post->create();
$comment_id = $this->factory->comment->create([
'comment_post_ID' => $post_id,
]);
$comment = Timber\Timber::get_comment($comment_id);
# test default gravatr holding image
$avatar = $comment->avatar(92, "gravatar_default");
$this->assertTrue(substr($avatar, 0, 5) == "http:");
}
public function testGravatar()
{
if (!TestTimberImage::is_connected()) {
$this->markTestSkipped('Cannot test avatar images when not connected to internet');
}
$post_id = $this->factory->post->create();
$comment_id = $this->factory->comment->create([
'comment_post_ID' => $post_id,
'comment_author' => 'jarednova',
'comment_author_email' => 'jarednova@upstatement.com',
]);
$comment = Timber\Timber::get_comment($comment_id);
$gravatar = md5(file_get_contents($comment->avatar()));
/* this keeps changing b/c of compression tweaks on WP.org, disabling the test */
//$this->assertEquals($gravatar, md5(file_get_contents(dirname(__FILE__).'/assets/jarednova.jpeg')));
$comment_id = $this->factory->comment->create([
'comment_post_ID' => $post_id,
'comment_author' => 'jarednova',
'comment_author_email' => 'notjared@upstatement.com',
]);
$comment = Timber\Timber::get_comment($comment_id);
$not_gravatar = md5(file_get_contents($comment->avatar()));
$this->assertNotEquals($not_gravatar, md5(file_get_contents(dirname(__FILE__) . '/assets/jarednova.jpeg')));
}
public function testAvatarSimple()
{
if (!TestTimberImage::is_connected()) {
$this->markTestSkipped('Cannot test avatar images when not connected to internet');
}
$theme_url = get_theme_root_uri() . '/' . get_stylesheet();
$post_id = $this->factory->post->create();
$comment_id = $this->factory->comment->create([
'comment_post_ID' => $post_id,
]);
$comment = Timber\Timber::get_comment($comment_id);
# test default gravatr holding image
$avatar = $comment->avatar(32, "mystery");
$this->assertTrue(substr($avatar, 0, 5) == "http:");
# does it work if its SSL?
$_SERVER['HTTPS'] = 'on';
$avatar = $comment->avatar(32, "mystery");
$this->assertTrue(200 === $this->crawl($avatar));
$this->assertTrue(substr($avatar, 0, 6) == "https:");
$_SERVER['HTTPS'] = 'off';
# pass custom url on different domain. can't check by crawling as
# i get a 302 regardless of default url
# so just check it comes back with it in the url
$this->valid_avatar($comment, "http://upload.wikimedia.org/wikipedia/en/b/bc/Wiki.png");
# same domain.
$this->valid_avatar($comment, $theme_url . "/images/default.png");
#relative
$default_url = "/images/default.png";
$avatar = $comment->avatar(32, $default_url);
if (strstr($avatar, '?')) {
list($url, $params) = explode('?', $avatar);
$default_url = $theme_url . $default_url;
# you get back the absoulte url to default in the avatar url?
$this->assertEquals($params, "d=$default_url&s=32");
}
# you get back url?
$this->assertTrue(substr($theme_url . $avatar, 0, 5) == "http:");
}
public function valid_avatar($comment, $default_url)
{
$avatar = $comment->avatar(32, $default_url);
if (strstr($avatar, '?')) {
list($url, $params) = explode('?', $avatar);
# you get back the default in the avatar url?
$this->assertEquals($params, "d=$default_url&s=32");
}
# you get back url?
$this->assertTrue(substr($avatar, 0, 5) == "http:");
}
public function crawl($url)
{
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
/* Get the HTML or whatever is linked in $url. */
$response = curl_exec($handle);
/* Check for 404 (file not found). */
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
curl_close($handle);
return $httpCode;
}
}