GravatarHelperTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. * 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 testImage() {
  49. $is = $this->Gravatar->image($this->garageEmail);
  50. $this->assertTrue(!empty($is));
  51. $is = $this->Gravatar->image($this->testEmail);
  52. $this->assertTextContains('.gravatar.com/avatar/', $is);
  53. $is = $this->Gravatar->image($this->testEmail, ['size' => '200']);
  54. $this->assertTextContains('?size=200"', $is);
  55. $is = $this->Gravatar->image($this->testEmail, ['size' => '20']);
  56. $this->assertTextContains('?size=20"', $is);
  57. $is = $this->Gravatar->image($this->testEmail, ['rating' => 'X']); # note the capit. x
  58. $this->assertTextContains('?rating=x"', $is);
  59. $is = $this->Gravatar->image($this->testEmail, ['ext' => true]);
  60. $this->assertTextContains('.jpg"', $is);
  61. $is = $this->Gravatar->image($this->testEmail, ['default' => 'none']);
  62. $this->assertTrue(!empty($is));
  63. $is = $this->Gravatar->image($this->garageEmail, ['default' => 'none']);
  64. $this->assertTrue(!empty($is));
  65. $is = $this->Gravatar->image($this->garageEmail, ['default' => 'http://2.gravatar.com/avatar/8379aabc84ecee06f48d8ca48e09eef4?d=identicon']);
  66. $this->assertTrue(!empty($is));
  67. $is = $this->Gravatar->image($this->testEmail, ['size' => '20']);
  68. $this->assertTextContains('?size=20"', $is);
  69. $is = $this->Gravatar->image($this->testEmail, ['rating' => 'X', 'size' => 20, 'default' => 'none']);
  70. $this->assertTextContains('?rating=x&amp;size=20&amp;default=none"', $is);
  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. [$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->assertMatchesRegularExpression('/\.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->assertMatchesRegularExpression('/\.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. [$url, $params] = explode('?', $result);
  109. $this->assertMatchesRegularExpression('/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->assertMatchesRegularExpression('/[^\?]+/', $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. [$url, $params] = explode('?', $result);
  128. $this->assertMatchesRegularExpression('/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. [$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. }