GravatarHelperTest.php 6.9 KB

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