GravatarHelperTest.php 6.3 KB

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