GravatarHelperTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. /** OWN ONES **/
  33. /**
  34. * @access public
  35. * @return void
  36. * 2009-07-30 ms
  37. */
  38. public function testDefaultImages() {
  39. $is = $this->Gravatar->defaultImages();
  40. $expectedCount = 7;
  41. foreach ($is as $image) {
  42. echo $image.' ';
  43. }
  44. $this->assertTrue(is_array($is) && (count($is) === $expectedCount));
  45. }
  46. /**
  47. * @access public
  48. * @return void
  49. * 2009-07-30 ms
  50. */
  51. public function testImages() {
  52. $is = $this->Gravatar->image(GARBIGE_TEST_EMAIL);
  53. echo $is;
  54. $this->assertTrue(!empty($is));
  55. $is = $this->Gravatar->image(Configure::read('Config.admin_email'));
  56. echo $is;
  57. $this->assertTrue(!empty($is));
  58. $is = $this->Gravatar->image(VALID_TEST_EMAIL);
  59. echo $is;
  60. $this->assertTrue(!empty($is));
  61. $is = $this->Gravatar->image(VALID_TEST_EMAIL, array('size'=>'200'));
  62. echo $is;
  63. $this->assertTrue(!empty($is));
  64. $is = $this->Gravatar->image(VALID_TEST_EMAIL, array('size'=>'20'));
  65. echo $is;
  66. $this->assertTrue(!empty($is));
  67. $is = $this->Gravatar->image(VALID_TEST_EMAIL, array('rating'=>'X')); # note the capit. x
  68. echo $is;
  69. $this->assertTrue(!empty($is));
  70. $is = $this->Gravatar->image(VALID_TEST_EMAIL, array('ext'=>true));
  71. echo $is;
  72. $this->assertTrue(!empty($is));
  73. $is = $this->Gravatar->image(VALID_TEST_EMAIL, array('default'=>'none'));
  74. echo $is;
  75. $this->assertTrue(!empty($is));
  76. $is = $this->Gravatar->image(GARBIGE_TEST_EMAIL, array('default'=>'none'));
  77. echo $is;
  78. $this->assertTrue(!empty($is));
  79. $is = $this->Gravatar->image(GARBIGE_TEST_EMAIL, array('default'=>'http://2.gravatar.com/avatar/8379aabc84ecee06f48d8ca48e09eef4?d=identicon'));
  80. echo $is;
  81. $this->assertTrue(!empty($is));
  82. }
  83. /** BASE TEST CASES **/
  84. /**
  85. * testBaseUrlGeneration
  86. *
  87. * @return void
  88. * @access public
  89. */
  90. public function testBaseUrlGeneration() {
  91. $expected = 'http://www.gravatar.com/avatar/' . md5('example@gravatar.com');
  92. $result = $this->Gravatar->imageUrl('example@gravatar.com', array('ext' => false, 'default' => 'wavatar'));
  93. list($url, $params) = explode('?', $result);
  94. $this->assertEquals($expected, $url);
  95. }
  96. /**
  97. * testExtensions
  98. *
  99. * @return void
  100. * @access public
  101. */
  102. public function testExtensions() {
  103. $result = $this->Gravatar->imageUrl('example@gravatar.com', array('ext' => true, 'default' => 'wavatar'));
  104. $this->assertRegExp('/\.jpg(?:$|\?)/', $result);
  105. }
  106. /**
  107. * testRating
  108. *
  109. * @return void
  110. * @access public
  111. */
  112. public function testRating() {
  113. $result = $this->Gravatar->imageUrl('example@gravatar.com', array('ext' => true, 'default' => 'wavatar'));
  114. $this->assertRegExp('/\.jpg(?:$|\?)/', $result);
  115. }
  116. /**
  117. * testAlternateDefaultIcon
  118. *
  119. * @return void
  120. * @access public
  121. */
  122. public function testAlternateDefaultIcon() {
  123. $result = $this->Gravatar->imageUrl('example@gravatar.com', array('ext' => false, 'default' => 'wavatar'));
  124. list($url, $params) = explode('?', $result);
  125. $this->assertRegExp('/default=wavatar/', $params);
  126. }
  127. /**
  128. * testAlternateDefaultIconCorrection
  129. *
  130. * @return void
  131. * @access public
  132. */
  133. public function testAlternateDefaultIconCorrection() {
  134. $result = $this->Gravatar->imageUrl('example@gravatar.com', array('ext' => false, 'default' => '12345'));
  135. $this->assertRegExp('/[^\?]+/', $result);
  136. }
  137. /**
  138. * testSize
  139. *
  140. * @return void
  141. * @access public
  142. */
  143. public function testSize() {
  144. $result = $this->Gravatar->imageUrl('example@gravatar.com', array('size' => '120'));
  145. list($url, $params) = explode('?', $result);
  146. $this->assertRegExp('/size=120/', $params);
  147. }
  148. /**
  149. * testImageTag
  150. *
  151. * @return void
  152. * @access public
  153. */
  154. public function testImageTag() {
  155. $expected = '<img src="http://www.gravatar.com/avatar/' . md5('example@gravatar.com') . '" alt="" />';
  156. $result = $this->Gravatar->image('example@gravatar.com', array('ext' => false));
  157. $this->assertEquals($expected, $result);
  158. $expected = '<img src="http://www.gravatar.com/avatar/' . md5('example@gravatar.com') . '" alt="Gravatar" />';
  159. $result = $this->Gravatar->image('example@gravatar.com', array('ext' => false, 'alt' => 'Gravatar'));
  160. $this->assertEquals($expected, $result);
  161. }
  162. /**
  163. * testDefaulting
  164. *
  165. * @return void
  166. * @access public
  167. */
  168. public function testDefaulting() {
  169. $result = $this->Gravatar->imageUrl('example@gravatar.com', array('default' => 'wavatar', 'size' => 'default'));
  170. list($url, $params) = explode('?', $result);
  171. $this->assertEquals($params, 'default=wavatar');
  172. }
  173. /**
  174. * testNonSecureUrl
  175. *
  176. * @return void
  177. * @access public
  178. */
  179. public function testNonSecureUrl() {
  180. $_SERVER['HTTPS'] = false;
  181. $expected = 'http://www.gravatar.com/avatar/' . md5('example@gravatar.com');
  182. $result = $this->Gravatar->imageUrl('example@gravatar.com', array('ext' => false));
  183. $this->assertEquals($expected, $result);
  184. $expected = 'http://www.gravatar.com/avatar/' . md5('example@gravatar.com');
  185. $result = $this->Gravatar->imageUrl('example@gravatar.com', array('ext' => false, 'secure' => false));
  186. $this->assertEquals($expected, $result);
  187. $_SERVER['HTTPS'] = true;
  188. $expected = 'http://www.gravatar.com/avatar/' . md5('example@gravatar.com');
  189. $result = $this->Gravatar->imageUrl('example@gravatar.com', array('ext' => false, 'secure' => false));
  190. $this->assertEquals($expected, $result);
  191. }
  192. /**
  193. * testSecureUrl
  194. *
  195. * @return void
  196. * @access public
  197. */
  198. public function testSecureUrl() {
  199. $expected = 'https://secure.gravatar.com/avatar/' . md5('example@gravatar.com');
  200. $result = $this->Gravatar->imageUrl('example@gravatar.com', array('ext' => false, 'secure' => true));
  201. $this->assertEquals($expected, $result);
  202. $_SERVER['HTTPS'] = true;
  203. $this->Gravatar = new GravatarHelper(new View(null));
  204. $this->Gravatar->Html = new HtmlHelper(new View(null));
  205. $expected = 'https://secure.gravatar.com/avatar/' . md5('example@gravatar.com');
  206. $result = $this->Gravatar->imageUrl('example@gravatar.com', array('ext' => false));
  207. $this->assertEquals($expected, $result);
  208. $expected = 'https://secure.gravatar.com/avatar/' . md5('example@gravatar.com');
  209. $result = $this->Gravatar->imageUrl('example@gravatar.com', array('ext' => false, 'secure' => true));
  210. $this->assertEquals($expected, $result);
  211. }
  212. }