GravatarHelperTest.php 6.4 KB

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