gravatar.test.php 6.5 KB

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