GravatarHelperTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace Tools\TestCase\View\Helper;
  3. use Cake\View\View;
  4. use Tools\TestSuite\TestCase;
  5. use Tools\View\Helper\GravatarHelper;
  6. /**
  7. * Gravatar Test Case
  8. */
  9. class GravatarHelperTest extends TestCase {
  10. /**
  11. * SetUp method
  12. */
  13. public function setUp() {
  14. parent::setUp();
  15. $this->testEmail = 'graham@grahamweldon.com'; // For testing normal behavior
  16. $this->garageEmail = 'test@test.de'; // For testing default image behavior
  17. $this->Gravatar = new GravatarHelper(new View(null));
  18. }
  19. /**
  20. * TearDown method
  21. */
  22. public function tearDown() {
  23. parent::tearDown();
  24. unset($this->Gravatar);
  25. }
  26. /**
  27. * @return void
  28. */
  29. public function testDefaultImages() {
  30. $is = $this->Gravatar->defaultImages();
  31. $expectedCount = 7;
  32. foreach ($is as $image) {
  33. //$this->debug($image . ' ');
  34. }
  35. $this->assertTrue(is_array($is) && (count($is) === $expectedCount));
  36. }
  37. /**
  38. * @return void
  39. */
  40. public function testImages() {
  41. $is = $this->Gravatar->image($this->garageEmail);
  42. //$this->debug($is);
  43. $this->assertTrue(!empty($is));
  44. $is = $this->Gravatar->image($this->testEmail);
  45. //$this->debug($is);
  46. $this->assertTrue(!empty($is));
  47. $is = $this->Gravatar->image($this->testEmail, ['size' => '200']);
  48. //$this->debug($is);
  49. $this->assertTrue(!empty($is));
  50. $is = $this->Gravatar->image($this->testEmail, ['size' => '20']);
  51. //$this->debug($is);
  52. $this->assertTrue(!empty($is));
  53. $is = $this->Gravatar->image($this->testEmail, ['rating' => 'X']); # note the capit. x
  54. //$this->debug($is);
  55. $this->assertTrue(!empty($is));
  56. $is = $this->Gravatar->image($this->testEmail, ['ext' => true]);
  57. //$this->debug($is);
  58. $this->assertTrue(!empty($is));
  59. $is = $this->Gravatar->image($this->testEmail, ['default' => 'none']);
  60. //$this->debug($is);
  61. $this->assertTrue(!empty($is));
  62. $is = $this->Gravatar->image($this->garageEmail, ['default' => 'none']);
  63. //$this->debug($is);
  64. $this->assertTrue(!empty($is));
  65. $is = $this->Gravatar->image($this->garageEmail, ['default' => 'http://2.gravatar.com/avatar/8379aabc84ecee06f48d8ca48e09eef4?d=identicon']);
  66. //$this->debug($is);
  67. $this->assertTrue(!empty($is));
  68. }
  69. /**
  70. * BASE TEST CASES
  71. **/
  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', ['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', ['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', ['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', ['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', ['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', ['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', ['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', ['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', ['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', ['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', ['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', ['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', ['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', ['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', ['ext' => false, 'secure' => true]);
  187. $this->assertEquals($expected, $result);
  188. }
  189. }