GravatarHelperTest.php 6.3 KB

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