GravatarHelperTest.php 6.6 KB

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