Fix bug #19608: E_NOTICE for invalid emails
[services_libravatar: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 testGetUrlCasing()
30     {
31         $this->loadSLMock();
32         $this->assertEquals(
33             'http://example.org/avatar/9e263681488308e5e5d5e548b2f9bc99',
34             $this->sl->getUrl('CWeiske@cWeiske.de')
35         );
36     }
37
38     public function testGetUrlHttpsDefault()
39     {
40         $this->loadSLMock();
41         $this->sl->setHttps(true);
42         $this->assertEquals(
43             'https://example.org/avatar/9e263681488308e5e5d5e548b2f9bc99',
44             $this->sl->getUrl('cweiske@cweiske.de')
45         );
46     }
47
48     public function testGetUrlHttpsOptionOff()
49     {
50         $this->loadSLMock();
51         $this->sl->setHttps(true);
52         $this->assertEquals(
53             'http://example.org/avatar/9e263681488308e5e5d5e548b2f9bc99',
54             $this->sl->getUrl('cweiske@cweiske.de', array('https' => false))
55         );
56     }
57
58     public function testGetUrlAlgorithmDefault()
59     {
60         $this->loadSLMock();
61         $this->sl->setAlgorithm('sha256');
62         $this->assertEquals(
63             'http://example.org/avatar/baa4e986ac6bb3f3715de5b08727be61d33afb4b03e792eb6db2f184a61626d6',
64             $this->sl->getUrl('cweiske@cweiske.de')
65         );
66     }
67
68     public function testGetUrlAlgorithmOption()
69     {
70         $this->loadSLMock();
71         $this->assertEquals(
72             'http://example.org/avatar/baa4e986ac6bb3f3715de5b08727be61d33afb4b03e792eb6db2f184a61626d6',
73             $this->sl->getUrl('cweiske@cweiske.de', array('algorithm' => 'sha256'))
74         );
75     }
76
77     public function testGetUrlSizeDefault()
78     {
79         $this->loadSLMock();
80         $this->sl->setSize(128);
81         $this->assertEquals(
82             'http://example.org/avatar/9e263681488308e5e5d5e548b2f9bc99?size=128',
83             $this->sl->getUrl('cweiske@cweiske.de')
84         );
85     }
86
87     public function testGetUrlSizeOption()
88     {
89         $this->loadSLMock();
90         $this->sl->setSize(128);
91         $this->assertEquals(
92             'http://example.org/avatar/9e263681488308e5e5d5e548b2f9bc99?size=256',
93             $this->sl->getUrl('cweiske@cweiske.de', array('size' => 256))
94         );
95     }
96
97     /**
98      * Note that this short option is deprecated
99      */
100     public function testGetUrlSizeOptionShort()
101     {
102         $this->loadSLMock();
103         $this->sl->setSize(128);
104         $this->assertEquals(
105             'http://example.org/avatar/9e263681488308e5e5d5e548b2f9bc99?size=256',
106             $this->sl->getUrl('cweiske@cweiske.de', array('s' => 256))
107         );
108     }
109
110     public function testGetUrlDefaultDefault()
111     {
112         $this->loadSLMock();
113         $this->sl->setDefault('identicon');
114         $this->assertEquals(
115             'http://example.org/avatar/9e263681488308e5e5d5e548b2f9bc99?default=identicon',
116             $this->sl->getUrl('cweiske@cweiske.de')
117         );
118     }
119
120     public function testGetUrlDefaultOption()
121     {
122         $this->loadSLMock();
123         $this->assertEquals(
124             'http://example.org/avatar/9e263681488308e5e5d5e548b2f9bc99?default=404',
125             $this->sl->getUrl('cweiske@cweiske.de', array('default' => 404))
126         );
127     }
128
129     /**
130      * Note that this short option is deprecated
131      */
132     public function testGetUrlDefaultOptionShort()
133     {
134         $this->loadSLMock();
135         $this->assertEquals(
136             'http://example.org/avatar/9e263681488308e5e5d5e548b2f9bc99?default=404',
137             $this->sl->getUrl('cweiske@cweiske.de', array('d' => 404))
138         );
139     }
140
141     /**
142      * @expectedException InvalidArgumentException
143      * @expectedExceptionMessage Invalid option in array: foo
144      */
145     public function testGetUrlOptionInvalid()
146     {
147         $this->sl->getUrl('cweiske@cweiske.de', array('foo' => 123));
148     }
149
150     public function testGetUrlNoIdentifier()
151     {
152         $this->loadSLMock();
153         $this->assertEquals(
154             'http://example.org/avatar/d41d8cd98f00b204e9800998ecf8427e',
155             $this->sl->getUrl(false)
156         );
157     }
158
159     public function testGetUrlOpenId()
160     {
161         $this->loadSLMock();
162         $this->assertEquals(
163             'http://example.org/avatar/b5bbeb6202fa01ca1deb8809716a1492f013a8896abf6e11b651fdf1cde23380',
164             $this->sl->getUrl('cweiske.de')
165         );
166     }
167
168     public function testGetUrlInvalid()
169     {
170         $this->assertEquals(
171             'http://cdn.libravatar.org/avatar/e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
172             $this->sl->getUrl('adam@adam-laptop.(none)')
173         );
174     }
175
176     public function testNormalizeOpenId()
177     {
178         $this->assertEquals(
179             'https://example.org/',
180             Services_Libravatar::normalizeOpenId('https://example.org/')
181         );
182     }
183
184     public function testNormalizeOpenIdCasing()
185     {
186         $this->assertEquals(
187             'https://example.org/BaR?Foo',
188             Services_Libravatar::normalizeOpenId('Https://examPLe.Org/BaR?Foo#mE')
189         );
190     }
191
192     public function testNormalizeOpenIdPortDefault()
193     {
194         $this->assertEquals(
195             'http://example.org/',
196             Services_Libravatar::normalizeOpenId('http://example.org:80/')
197         );
198         $this->assertEquals(
199             'https://example.org/',
200             Services_Libravatar::normalizeOpenId('https://example.org:443/')
201         );
202     }
203
204     public function testNormalizeOpenIdPortNonDefault()
205     {
206         $this->assertEquals(
207             'http://example.org:123/',
208             Services_Libravatar::normalizeOpenId('http://example.org:123/')
209         );
210         $this->assertEquals(
211             'https://example.org:234/',
212             Services_Libravatar::normalizeOpenId('https://example.org:234/')
213         );
214     }
215
216     public function testNormalizeOpenIdUsername()
217     {
218         $this->assertEquals(
219             'https://User@example.org/',
220             Services_Libravatar::normalizeOpenId('Https://User@examPLe.Org/')
221         );
222     }
223
224     public function testNormalizeOpenIdPassword()
225     {
226         $this->assertEquals(
227             'https://:pAss@example.org/',
228             Services_Libravatar::normalizeOpenId('Https://:pAss@examPLe.Org/')
229         );
230     }
231
232     public function testNormalizeOpenIdUserAndPass()
233     {
234         $this->assertEquals(
235             'https://User:Pass@example.org/',
236             Services_Libravatar::normalizeOpenId('Https://User:Pass@examPLe.Org/')
237         );
238     }
239
240     public function testNormalizeOpenIdXRI()
241     {
242         $this->assertEquals(
243             'foo',
244             Services_Libravatar::normalizeOpenId('xri://foo')
245         );
246     }
247
248     public function testNormalizeOpenIdXRIGlobalSymbol()
249     {
250         $this->assertEquals(
251             '=bar',
252             Services_Libravatar::normalizeOpenId('=bar')
253         );
254     }
255
256     public function testNormalizeOpenIdMissingProtocol()
257     {
258         $this->assertEquals(
259             'http://example.org/',
260             Services_Libravatar::normalizeOpenId('example.org/')
261         );
262     }
263
264     public function testNormalizeOpenIdMissingSlash()
265     {
266         $this->assertEquals(
267             'http://example.org/',
268             Services_Libravatar::normalizeOpenId('http://example.org')
269         );
270     }
271
272     public function testNormalizeOpenIdInvalid()
273     {
274         $this->assertEquals(
275             '',
276             Services_Libravatar::normalizeOpenId('http://e=g/')
277         );
278     }
279
280     public function testDomainGetEmail()
281     {
282         $this->assertEquals(
283             'example.org',
284             $this->callProtected('domainGet', 'user@example.org')
285         );
286     }
287
288     public function testDomainGetNull()
289     {
290         $this->assertEquals(
291             null,
292             $this->callProtected('domainGet', null)
293         );
294     }
295
296     public function testDomainGetOpenId()
297     {
298         $this->assertEquals(
299             'example.org',
300             $this->callProtected('domainGet', 'http://example.org/')
301         );
302     }
303
304     public function testDomainGetOpenIdNonDefaultPort()
305     {
306         $this->assertEquals(
307             'example.org:123',
308             $this->callProtected('domainGet', 'http://example.org:123/')
309         );
310         $this->assertEquals(
311             'example.org:234',
312             $this->callProtected('domainGet', 'https://example.org:234/')
313         );
314     }
315
316
317
318     public function testSetAlgorithmValid()
319     {
320         $this->sl->setAlgorithm('md5');
321         $this->assertEquals('md5', $this->getProtected('algorithm', $this->sl));
322
323         $this->sl->setAlgorithm('sha256');
324         $this->assertEquals('sha256', $this->getProtected('algorithm', $this->sl));
325     }
326
327     /**
328      * @expectedException InvalidArgumentException
329      * @expectedExceptionMessage Only md5 and sha256 hashing supported
330      */
331     public function testSetAlgorithmInvalid()
332     {
333         $this->sl->setAlgorithm('foo');
334     }
335
336     public function testSetDefaultIdenticon()
337     {
338         $this->sl->setDefault('identicon');
339         $this->assertEquals('identicon', $this->getProtected('default', $this->sl));
340     }
341
342     public function testSetDefaultUrl()
343     {
344         $this->sl->setDefault('http://example.org/default.png');
345         $this->assertEquals(
346             'http://example.org/default.png',
347             $this->getProtected('default', $this->sl)
348         );
349     }
350
351     public function testSetDefaultNull()
352     {
353         $this->sl->setDefault(null);
354         $this->assertNull(
355             $this->getProtected('default', $this->sl)
356         );
357     }
358
359     /**
360      * @expectedException InvalidArgumentException
361      * @expectedExceptionMessage Invalid default avatar URL
362      */
363     public function testSetDefaultInvalidShortcut()
364     {
365         $this->sl->setDefault('foo');
366     }
367
368     /**
369      * @expectedException InvalidArgumentException
370      * @expectedExceptionMessage Invalid default avatar URL
371      */
372     public function testSetDefaultInvalidUrl()
373     {
374         //missing protocol
375         $this->sl->setDefault('example.org/default.png');
376     }
377
378     public function testSetHttps()
379     {
380         $this->sl->setHttps(true);
381         $this->assertEquals(true, $this->getProtected('https', $this->sl));
382     }
383
384     public function testSetSize()
385     {
386         $this->sl->setSize(32);
387         $this->assertEquals(32, $this->getProtected('size', $this->sl));
388     }
389
390     public function testSetSizeNull()
391     {
392         $this->sl->setSize(null);
393         $this->assertNull($this->getProtected('size', $this->sl));
394     }
395
396     /**
397      * @expectedException InvalidArgumentException
398      * @expectedExceptionMessage Size has to be larger than 0
399      */
400     public function testSetSizeInvalid()
401     {
402         $this->sl->setSize(-21);
403     }
404
405     public function testDetectHttpsOn()
406     {
407         $_SERVER['HTTPS'] = 'on';
408         $this->sl->detectHttps();
409         $this->assertEquals(true, $this->getProtected('https', $this->sl));
410     }
411
412     public function testDetectHttpsOff()
413     {
414         unset($_SERVER['HTTPS']);
415         $this->sl->detectHttps();
416         $this->assertEquals(false, $this->getProtected('https', $this->sl));
417     }
418
419
420     /**
421      * Get the value of a protected/private class property
422      */
423     protected function getProtected($variable, $object)
424     {
425         $rc = new ReflectionClass($object);
426         $prop = $rc->getProperty($variable);
427         $prop->setAccessible(true);
428         return $prop->getValue($object);
429     }
430
431     /**
432      * Call a protected/private class method
433      *
434      * @param string $method Method name
435      * @param mixed  $arg1   Any number of method arguments
436      */
437     protected function callProtected($method)
438     {
439         $args = func_get_args();
440         array_shift($args);
441
442         $rc = new ReflectionClass($this->sl);
443         $method = $rc->getMethod($method);
444         $method->setAccessible(true);
445         return $method->invokeArgs($this->sl, $args);
446     }
447
448     protected function loadSLMock()
449     {
450         $this->sl = $this->getMock('Services_Libravatar', array('srvGet'));
451         $this->sl->expects($this->once())
452             ->method('srvGet')
453             ->will($this->returnValue('example.org'));
454     }
455 }
456
457 ?>