GravatarHelperTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
  3. define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
  4. }
  5. define('VALID_TEST_EMAIL', 'graham@grahamweldon.com'); # for testing normal behavior
  6. define('GARBIGE_TEST_EMAIL', 'test@test.de'); # for testing default image behavior
  7. App::uses('HtmlHelper', 'View/Helper');
  8. App::uses('GravatarHelper', 'Tools.View/Helper');
  9. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  10. App::uses('View', 'View');
  11. /**
  12. * Gravatar Test Case
  13. *
  14. * 2010-05-27 ms
  15. */
  16. class GravatarHelperTest extends MyCakeTestCase {
  17. /**
  18. * setUp method
  19. */
  20. public function setUp() {
  21. parent::setUp();
  22. $this->Gravatar = new GravatarHelper(new View(null));
  23. $this->Gravatar->Html = new HtmlHelper(new View(null));
  24. }
  25. /**
  26. * tearDown method
  27. */
  28. public function tearDown() {
  29. parent::tearDown();
  30. unset($this->Gravatar);
  31. }
  32. /**
  33. * @return void
  34. * 2009-07-30 ms
  35. */
  36. public function testDefaultImages() {
  37. $is = $this->Gravatar->defaultImages();
  38. $expectedCount = 7;
  39. foreach ($is as $image) {
  40. $this->out($image.' ');
  41. }
  42. $this->assertTrue(is_array($is) && (count($is) === $expectedCount));
  43. }
  44. /**
  45. * @return void
  46. * 2009-07-30 ms
  47. */
  48. public function testImages() {
  49. $is = $this->Gravatar->image(GARBIGE_TEST_EMAIL);
  50. $this->out($is);
  51. $this->assertTrue(!empty($is));
  52. $is = $this->Gravatar->image(Configure::read('Config.admin_email'));
  53. $this->out($is);
  54. $this->assertTrue(!empty($is));
  55. $is = $this->Gravatar->image(VALID_TEST_EMAIL);
  56. $this->out($is);
  57. $this->assertTrue(!empty($is));
  58. $is = $this->Gravatar->image(VALID_TEST_EMAIL, array('size'=>'200'));
  59. $this->out($is);
  60. $this->assertTrue(!empty($is));
  61. $is = $this->Gravatar->image(VALID_TEST_EMAIL, array('size'=>'20'));
  62. $this->out($is);
  63. $this->assertTrue(!empty($is));
  64. $is = $this->Gravatar->image(VALID_TEST_EMAIL, array('rating'=>'X')); # note the capit. x
  65. $this->out($is);
  66. $this->assertTrue(!empty($is));
  67. $is = $this->Gravatar->image(VALID_TEST_EMAIL, array('ext'=>true));
  68. $this->out($is);
  69. $this->assertTrue(!empty($is));
  70. $is = $this->Gravatar->image(VALID_TEST_EMAIL, array('default'=>'none'));
  71. $this->out($is);
  72. $this->assertTrue(!empty($is));
  73. $is = $this->Gravatar->image(GARBIGE_TEST_EMAIL, array('default'=>'none'));
  74. $this->out($is);
  75. $this->assertTrue(!empty($is));
  76. $is = $this->Gravatar->image(GARBIGE_TEST_EMAIL, array('default'=>'http://2.gravatar.com/avatar/8379aabc84ecee06f48d8ca48e09eef4?d=identicon'));
  77. $this->out($is);
  78. $this->assertTrue(!empty($is));
  79. }
  80. /** BASE TEST CASES **/
  81. /**
  82. * testBaseUrlGeneration
  83. *
  84. * @return void
  85. */
  86. public function testBaseUrlGeneration() {
  87. $expected = 'http://www.gravatar.com/avatar/' . md5('example@gravatar.com');
  88. $result = $this->Gravatar->imageUrl('example@gravatar.com', array('ext' => false, 'default' => 'wavatar'));
  89. list($url, $params) = explode('?', $result);
  90. $this->assertEquals($expected, $url);
  91. }
  92. /**
  93. * testExtensions
  94. *
  95. * @return void
  96. */
  97. public function testExtensions() {
  98. $result = $this->Gravatar->imageUrl('example@gravatar.com', array('ext' => true, 'default' => 'wavatar'));
  99. $this->assertRegExp('/\.jpg(?:$|\?)/', $result);
  100. }
  101. /**
  102. * testRating
  103. *
  104. * @return void
  105. */
  106. public function testRating() {
  107. $result = $this->Gravatar->imageUrl('example@gravatar.com', array('ext' => true, 'default' => 'wavatar'));
  108. $this->assertRegExp('/\.jpg(?:$|\?)/', $result);
  109. }
  110. /**
  111. * testAlternateDefaultIcon
  112. *
  113. * @return void
  114. */
  115. public function testAlternateDefaultIcon() {
  116. $result = $this->Gravatar->imageUrl('example@gravatar.com', array('ext' => false, 'default' => 'wavatar'));
  117. list($url, $params) = explode('?', $result);
  118. $this->assertRegExp('/default=wavatar/', $params);
  119. }
  120. /**
  121. * testAlternateDefaultIconCorrection
  122. *
  123. * @return void
  124. */
  125. public function testAlternateDefaultIconCorrection() {
  126. $result = $this->Gravatar->imageUrl('example@gravatar.com', array('ext' => false, 'default' => '12345'));
  127. $this->assertRegExp('/[^\?]+/', $result);
  128. }
  129. /**
  130. * testSize
  131. *
  132. * @return void
  133. */
  134. public function testSize() {
  135. $result = $this->Gravatar->imageUrl('example@gravatar.com', array('size' => '120'));
  136. list($url, $params) = explode('?', $result);
  137. $this->assertRegExp('/size=120/', $params);
  138. }
  139. /**
  140. * testImageTag
  141. *
  142. * @return void
  143. */
  144. public function testImageTag() {
  145. $expected = '<img src="http://www.gravatar.com/avatar/' . md5('example@gravatar.com') . '" alt="" />';
  146. $result = $this->Gravatar->image('example@gravatar.com', array('ext' => false));
  147. $this->assertEquals($expected, $result);
  148. $expected = '<img src="http://www.gravatar.com/avatar/' . md5('example@gravatar.com') . '" alt="Gravatar" />';
  149. $result = $this->Gravatar->image('example@gravatar.com', array('ext' => false, 'alt' => 'Gravatar'));
  150. $this->assertEquals($expected, $result);
  151. }
  152. /**
  153. * testDefaulting
  154. *
  155. * @return void
  156. */
  157. public function testDefaulting() {
  158. $result = $this->Gravatar->imageUrl('example@gravatar.com', array('default' => 'wavatar', 'size' => 'default'));
  159. list($url, $params) = explode('?', $result);
  160. $this->assertEquals($params, 'default=wavatar');
  161. }
  162. /**
  163. * testNonSecureUrl
  164. *
  165. * @return void
  166. */
  167. public function testNonSecureUrl() {
  168. $_SERVER['HTTPS'] = false;
  169. $expected = 'http://www.gravatar.com/avatar/' . md5('example@gravatar.com');
  170. $result = $this->Gravatar->imageUrl('example@gravatar.com', array('ext' => false));
  171. $this->assertEquals($expected, $result);
  172. $expected = 'http://www.gravatar.com/avatar/' . md5('example@gravatar.com');
  173. $result = $this->Gravatar->imageUrl('example@gravatar.com', array('ext' => false, 'secure' => false));
  174. $this->assertEquals($expected, $result);
  175. $_SERVER['HTTPS'] = true;
  176. $expected = 'http://www.gravatar.com/avatar/' . md5('example@gravatar.com');
  177. $result = $this->Gravatar->imageUrl('example@gravatar.com', array('ext' => false, 'secure' => false));
  178. $this->assertEquals($expected, $result);
  179. }
  180. /**
  181. * testSecureUrl
  182. *
  183. * @return void
  184. */
  185. public function testSecureUrl() {
  186. $expected = 'https://secure.gravatar.com/avatar/' . md5('example@gravatar.com');
  187. $result = $this->Gravatar->imageUrl('example@gravatar.com', array('ext' => false, 'secure' => true));
  188. $this->assertEquals($expected, $result);
  189. $_SERVER['HTTPS'] = true;
  190. $this->Gravatar = new GravatarHelper(new View(null));
  191. $this->Gravatar->Html = new HtmlHelper(new View(null));
  192. $expected = 'https://secure.gravatar.com/avatar/' . md5('example@gravatar.com');
  193. $result = $this->Gravatar->imageUrl('example@gravatar.com', array('ext' => false));
  194. $this->assertEquals($expected, $result);
  195. $expected = 'https://secure.gravatar.com/avatar/' . md5('example@gravatar.com');
  196. $result = $this->Gravatar->imageUrl('example@gravatar.com', array('ext' => false, 'secure' => true));
  197. $this->assertEquals($expected, $result);
  198. }
  199. }