GravatarHelperTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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->out($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->out($is);
  45. $this->assertTrue(!empty($is));
  46. $is = $this->Gravatar->image(Configure::read('Config.adminEmail'));
  47. //$this->out($is);
  48. $this->assertTrue(!empty($is));
  49. $is = $this->Gravatar->image($this->testEmail);
  50. //$this->out($is);
  51. $this->assertTrue(!empty($is));
  52. $is = $this->Gravatar->image($this->testEmail, array('size' => '200'));
  53. //$this->out($is);
  54. $this->assertTrue(!empty($is));
  55. $is = $this->Gravatar->image($this->testEmail, array('size' => '20'));
  56. //$this->out($is);
  57. $this->assertTrue(!empty($is));
  58. $is = $this->Gravatar->image($this->testEmail, array('rating' => 'X')); # note the capit. x
  59. //$this->out($is);
  60. $this->assertTrue(!empty($is));
  61. $is = $this->Gravatar->image($this->testEmail, array('ext' => true));
  62. //$this->out($is);
  63. $this->assertTrue(!empty($is));
  64. $is = $this->Gravatar->image($this->testEmail, array('default' => 'none'));
  65. //$this->out($is);
  66. $this->assertTrue(!empty($is));
  67. $is = $this->Gravatar->image($this->garageEmail, array('default' => 'none'));
  68. //$this->out($is);
  69. $this->assertTrue(!empty($is));
  70. $is = $this->Gravatar->image($this->garageEmail, array('default' => 'http://2.gravatar.com/avatar/8379aabc84ecee06f48d8ca48e09eef4?d=identicon'));
  71. //$this->out($is);
  72. $this->assertTrue(!empty($is));
  73. }
  74. /** BASE TEST CASES **/
  75. /**
  76. * TestBaseUrlGeneration
  77. *
  78. * @return void
  79. */
  80. public function testBaseUrlGeneration() {
  81. $expected = 'http://www.gravatar.com/avatar/' . md5('example@gravatar.com');
  82. $result = $this->Gravatar->url('example@gravatar.com', array('ext' => false, 'default' => 'wavatar'));
  83. list($url, $params) = explode('?', $result);
  84. $this->assertEquals($expected, $url);
  85. }
  86. /**
  87. * TestExtensions
  88. *
  89. * @return void
  90. */
  91. public function testExtensions() {
  92. $result = $this->Gravatar->url('example@gravatar.com', array('ext' => true, 'default' => 'wavatar'));
  93. $this->assertRegExp('/\.jpg(?:$|\?)/', $result);
  94. }
  95. /**
  96. * TestRating
  97. *
  98. * @return void
  99. */
  100. public function testRating() {
  101. $result = $this->Gravatar->url('example@gravatar.com', array('ext' => true, 'default' => 'wavatar'));
  102. $this->assertRegExp('/\.jpg(?:$|\?)/', $result);
  103. }
  104. /**
  105. * TestAlternateDefaultIcon
  106. *
  107. * @return void
  108. */
  109. public function testAlternateDefaultIcon() {
  110. $result = $this->Gravatar->url('example@gravatar.com', array('ext' => false, 'default' => 'wavatar'));
  111. list($url, $params) = explode('?', $result);
  112. $this->assertRegExp('/default=wavatar/', $params);
  113. }
  114. /**
  115. * TestAlternateDefaultIconCorrection
  116. *
  117. * @return void
  118. */
  119. public function testAlternateDefaultIconCorrection() {
  120. $result = $this->Gravatar->url('example@gravatar.com', array('ext' => false, 'default' => '12345'));
  121. $this->assertRegExp('/[^\?]+/', $result);
  122. }
  123. /**
  124. * TestSize
  125. *
  126. * @return void
  127. */
  128. public function testSize() {
  129. $result = $this->Gravatar->url('example@gravatar.com', array('size' => '120'));
  130. list($url, $params) = explode('?', $result);
  131. $this->assertRegExp('/size=120/', $params);
  132. }
  133. /**
  134. * TestImageTag
  135. *
  136. * @return void
  137. */
  138. public function testImageTag() {
  139. $expected = '<img src="http://www.gravatar.com/avatar/' . md5('example@gravatar.com') . '" alt=""/>';
  140. $result = $this->Gravatar->image('example@gravatar.com', array('ext' => false));
  141. $this->assertEquals($expected, $result);
  142. $expected = '<img src="http://www.gravatar.com/avatar/' . md5('example@gravatar.com') . '" alt="Gravatar"/>';
  143. $result = $this->Gravatar->image('example@gravatar.com', array('ext' => false, 'alt' => 'Gravatar'));
  144. $this->assertEquals($expected, $result);
  145. }
  146. /**
  147. * TestDefaulting
  148. *
  149. * @return void
  150. */
  151. public function testDefaulting() {
  152. $result = $this->Gravatar->url('example@gravatar.com', array('default' => 'wavatar', 'size' => 'default'));
  153. list($url, $params) = explode('?', $result);
  154. $this->assertEquals($params, 'default=wavatar');
  155. }
  156. /**
  157. * TestNonSecureUrl
  158. *
  159. * @return void
  160. */
  161. public function testNonSecureUrl() {
  162. $_SERVER['HTTPS'] = false;
  163. $expected = 'http://www.gravatar.com/avatar/' . md5('example@gravatar.com');
  164. $result = $this->Gravatar->url('example@gravatar.com', array('ext' => false));
  165. $this->assertEquals($expected, $result);
  166. $expected = 'http://www.gravatar.com/avatar/' . md5('example@gravatar.com');
  167. $result = $this->Gravatar->url('example@gravatar.com', array('ext' => false, 'secure' => false));
  168. $this->assertEquals($expected, $result);
  169. $_SERVER['HTTPS'] = true;
  170. $expected = 'http://www.gravatar.com/avatar/' . md5('example@gravatar.com');
  171. $result = $this->Gravatar->url('example@gravatar.com', array('ext' => false, 'secure' => false));
  172. $this->assertEquals($expected, $result);
  173. }
  174. /**
  175. * TestSecureUrl
  176. *
  177. * @return void
  178. */
  179. public function testSecureUrl() {
  180. $expected = 'https://secure.gravatar.com/avatar/' . md5('example@gravatar.com');
  181. $result = $this->Gravatar->url('example@gravatar.com', array('ext' => false, 'secure' => true));
  182. $this->assertEquals($expected, $result);
  183. $_SERVER['HTTPS'] = true;
  184. $this->Gravatar = new GravatarHelper(new View(null));
  185. $expected = 'https://secure.gravatar.com/avatar/' . md5('example@gravatar.com');
  186. $result = $this->Gravatar->url('example@gravatar.com', array('ext' => false));
  187. $this->assertEquals($expected, $result);
  188. $expected = 'https://secure.gravatar.com/avatar/' . md5('example@gravatar.com');
  189. $result = $this->Gravatar->url('example@gravatar.com', array('ext' => false, 'secure' => true));
  190. $this->assertEquals($expected, $result);
  191. }
  192. }