lowercasing of openid scheme and host
[services_libravatar:sunchasers-services_libravatar.git] / tests / Services / LibravatarTest.php
1 <?php
2 require_once 'Services/Libravatar.php';
3
4 class Services_LibravatarTest extends PHPUnit_Framework_TestCase
5 {
6     public function setUp()
7     {
8         $this->sl = new Services_Libravatar();
9     }
10
11     public function testUrl()
12     {
13         $this->loadSLMock();
14         $this->assertEquals(
15             'http://example.org/avatar/9e263681488308e5e5d5e548b2f9bc99',
16             $this->sl->url('cweiske@cweiske.de')
17         );
18     }
19
20     public function testGetUrl()
21     {
22         $this->loadSLMock();
23         $this->assertEquals(
24             'http://example.org/avatar/9e263681488308e5e5d5e548b2f9bc99',
25             $this->sl->getUrl('cweiske@cweiske.de')
26         );
27     }
28
29     public function testGetUrlHttpsDefault()
30     {
31         $this->loadSLMock();
32         $this->sl->setHttps(true);
33         $this->assertEquals(
34             'https://example.org/avatar/9e263681488308e5e5d5e548b2f9bc99',
35             $this->sl->getUrl('cweiske@cweiske.de')
36         );
37     }
38
39     public function testGetUrlHttpsOptionOff()
40     {
41         $this->loadSLMock();
42         $this->sl->setHttps(true);
43         $this->assertEquals(
44             'http://example.org/avatar/9e263681488308e5e5d5e548b2f9bc99',
45             $this->sl->getUrl('cweiske@cweiske.de', array('https' => false))
46         );
47     }
48
49     public function testGetUrlAlgorithmDefault()
50     {
51         $this->loadSLMock();
52         $this->sl->setAlgorithm('sha256');
53         $this->assertEquals(
54             'http://example.org/avatar/baa4e986ac6bb3f3715de5b08727be61d33afb4b03e792eb6db2f184a61626d6',
55             $this->sl->getUrl('cweiske@cweiske.de')
56         );
57     }
58
59     public function testGetUrlAlgorithmOption()
60     {
61         $this->loadSLMock();
62         $this->assertEquals(
63             'http://example.org/avatar/baa4e986ac6bb3f3715de5b08727be61d33afb4b03e792eb6db2f184a61626d6',
64             $this->sl->getUrl('cweiske@cweiske.de', array('algorithm' => 'sha256'))
65         );
66     }
67
68     public function testGetUrlSizeDefault()
69     {
70         $this->loadSLMock();
71         $this->sl->setSize(128);
72         $this->assertEquals(
73             'http://example.org/avatar/9e263681488308e5e5d5e548b2f9bc99?size=128',
74             $this->sl->getUrl('cweiske@cweiske.de')
75         );
76     }
77
78     public function testGetUrlSizeOption()
79     {
80         $this->loadSLMock();
81         $this->sl->setSize(128);
82         $this->assertEquals(
83             'http://example.org/avatar/9e263681488308e5e5d5e548b2f9bc99?size=256',
84             $this->sl->getUrl('cweiske@cweiske.de', array('size' => 256))
85         );
86     }
87
88     public function testGetUrlDefaultDefault()
89     {
90         $this->loadSLMock();
91         $this->sl->setDefault('identicon');
92         $this->assertEquals(
93             'http://example.org/avatar/9e263681488308e5e5d5e548b2f9bc99?default=identicon',
94             $this->sl->getUrl('cweiske@cweiske.de')
95         );
96     }
97
98     public function testGetUrlDefaultOption()
99     {
100         $this->loadSLMock();
101         $this->assertEquals(
102             'http://example.org/avatar/9e263681488308e5e5d5e548b2f9bc99?default=404',
103             $this->sl->getUrl('cweiske@cweiske.de', array('default' => 404))
104         );
105     }
106
107     public function testGetUrlNoIdentifier()
108     {
109         $this->loadSLMock();
110         $this->assertEquals(
111             'http://example.org/avatar/d41d8cd98f00b204e9800998ecf8427e',
112             $this->sl->getUrl(false)
113         );
114     }
115
116     public function testGetUrlOpenId()
117     {
118         $this->loadSLMock();
119         $this->assertEquals(
120             'http://example.org/avatar/b5bbeb6202fa01ca1deb8809716a1492f013a8896abf6e11b651fdf1cde23380',
121             $this->sl->getUrl('cweiske.de')
122         );
123     }
124
125     public function testNormalizeOpenId()
126     {
127         $this->assertEquals(
128             'https://example.org/',
129             Services_Libravatar::normalizeOpenId('https://example.org/')
130         );
131     }
132
133     public function testNormalizeOpenIdCasing()
134     {
135         $this->assertEquals(
136             'https://example.org/BaR?Foo',
137             Services_Libravatar::normalizeOpenId('Https://examPLe.Org/BaR?Foo#mE')
138         );
139     }
140
141     public function testNormalizeOpenIdUsername()
142     {
143         $this->assertEquals(
144             'https://User@example.org/',
145             Services_Libravatar::normalizeOpenId('Https://User@examPLe.Org/')
146         );
147     }
148
149     public function testNormalizeOpenIdPassword()
150     {
151         $this->assertEquals(
152             'https://:pAss@example.org/',
153             Services_Libravatar::normalizeOpenId('Https://:pAss@examPLe.Org/')
154         );
155     }
156
157     public function testNormalizeOpenIdUserAndPass()
158     {
159         $this->assertEquals(
160             'https://User:Pass@example.org/',
161             Services_Libravatar::normalizeOpenId('Https://User:Pass@examPLe.Org/')
162         );
163     }
164
165     public function testNormalizeOpenIdXRI()
166     {
167         $this->assertEquals(
168             'foo',
169             Services_Libravatar::normalizeOpenId('xri://foo')
170         );
171     }
172
173     public function testNormalizeOpenIdXRIGlobalSymbol()
174     {
175         $this->assertEquals(
176             '=bar',
177             Services_Libravatar::normalizeOpenId('=bar')
178         );
179     }
180
181     public function testNormalizeOpenIdMissingProtocol()
182     {
183         $this->assertEquals(
184             'http://example.org/',
185             Services_Libravatar::normalizeOpenId('example.org/')
186         );
187     }
188
189     public function testNormalizeOpenIdMissingSlash()
190     {
191         $this->assertEquals(
192             'http://example.org/',
193             Services_Libravatar::normalizeOpenId('http://example.org')
194         );
195     }
196
197     public function testNormalizeOpenIdInvalid()
198     {
199         $this->assertEquals(
200             '',
201             Services_Libravatar::normalizeOpenId('http://e=g/')
202         );
203     }
204
205
206
207     public function testSetAlgorithmValid()
208     {
209         $this->sl->setAlgorithm('md5');
210         $this->assertEquals('md5', $this->getProtected('algorithm', $this->sl));
211
212         $this->sl->setAlgorithm('sha256');
213         $this->assertEquals('sha256', $this->getProtected('algorithm', $this->sl));
214     }
215
216     /**
217      * @expectedException InvalidArgumentException
218      * @expectedExceptionMessage Only md5 and sha256 hashing supported
219      */
220     public function testSetAlgorithmInvalid()
221     {
222         $this->sl->setAlgorithm('foo');
223     }
224
225     public function testSetDefaultIdenticon()
226     {
227         $this->sl->setDefault('identicon');
228         $this->assertEquals('identicon', $this->getProtected('default', $this->sl));
229     }
230
231     public function testSetDefaultUrl()
232     {
233         $this->sl->setDefault('http://example.org/default.png');
234         $this->assertEquals(
235             'http://example.org/default.png',
236             $this->getProtected('default', $this->sl)
237         );
238     }
239
240     public function testSetDefaultNull()
241     {
242         $this->sl->setDefault(null);
243         $this->assertNull(
244             $this->getProtected('default', $this->sl)
245         );
246     }
247
248     /**
249      * @expectedException InvalidArgumentException
250      * @expectedExceptionMessage Invalid default avatar URL
251      */
252     public function testSetDefaultInvalidShortcut()
253     {
254         $this->sl->setDefault('foo');
255     }
256
257     /**
258      * @expectedException InvalidArgumentException
259      * @expectedExceptionMessage Invalid default avatar URL
260      */
261     public function testSetDefaultInvalidUrl()
262     {
263         //missing protocol
264         $this->sl->setDefault('example.org/default.png');
265     }
266
267     public function testSetHttps()
268     {
269         $this->sl->setHttps(true);
270         $this->assertEquals(true, $this->getProtected('https', $this->sl));
271     }
272
273     public function testSetSize()
274     {
275         $this->sl->setSize(32);
276         $this->assertEquals(32, $this->getProtected('size', $this->sl));
277     }
278
279     public function testSetSizeNull()
280     {
281         $this->sl->setSize(null);
282         $this->assertNull($this->getProtected('size', $this->sl));
283     }
284
285     /**
286      * @expectedException InvalidArgumentException
287      * @expectedExceptionMessage Size has to be larger than 0
288      */
289     public function testSetSizeInvalid()
290     {
291         $this->sl->setSize(-21);
292     }
293
294     public function testDetectHttpsOn()
295     {
296         $_SERVER['HTTPS'] = 'on';
297         $this->sl->detectHttps();
298         $this->assertEquals(true, $this->getProtected('https', $this->sl));
299     }
300
301     public function testDetectHttpsOff()
302     {
303         unset($_SERVER['HTTPS']);
304         $this->sl->detectHttps();
305         $this->assertEquals(false, $this->getProtected('https', $this->sl));
306     }
307
308
309     /**
310      * Get the value of a protected/private class property
311      */
312     protected function getProtected($variable, $object)
313     {
314         $rc = new ReflectionClass($object);
315         $prop = $rc->getProperty($variable);
316         $prop->setAccessible(true);
317         return $prop->getValue($object);
318     }
319
320     protected function loadSLMock()
321     {
322         $this->sl = $this->getMock('Services_Libravatar', array('srvGet'));
323         $this->sl->expects($this->once())
324             ->method('srvGet')
325             ->will($this->returnValue('example.org'));
326     }
327 }
328
329 ?>