PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Test/Case/View/Helper/GravatarTest.php

https://github.com/mendo/utils
PHP | 198 lines | 84 code | 21 blank | 93 comment | 0 complexity | c2c3171693c553d2156a32cc92858b66 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /**
  3. * CakePHP Gravatar Helper Test
  4. *
  5. * @copyright Copyright 2010, Graham Weldon
  6. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  7. * @package goodies
  8. * @subpackage goodies.tests.cases.helpers
  9. *
  10. */
  11. App::import('Helper', array('Html', 'Utils.Gravatar'));
  12. App::uses('View', 'View');
  13. App::uses('String', 'Utility');
  14. App::uses('Security', 'Utility');
  15. App::uses('Validation', 'Utility');
  16. /**
  17. * GravatarHelper Test
  18. *
  19. * @package goodies
  20. * @subpackage goodies.test.cases.views.helpers
  21. */
  22. class GravatarHelperTest extends CakeTestCase {
  23. /**
  24. * Gravatar helper
  25. *
  26. * @var GravatarHelper
  27. * @access public
  28. */
  29. public $Gravatar = null;
  30. /**
  31. * Start Test
  32. *
  33. * @return void
  34. * @access public
  35. */
  36. public function setUp() {
  37. $null = null;
  38. $this->View = new View($null);
  39. $this->Gravatar = new GravatarHelper($this->View);
  40. $this->Gravatar->Html = new HtmlHelper($this->View);
  41. }
  42. /**
  43. * End Test
  44. *
  45. * @return void
  46. * @access public
  47. */
  48. public function tearDown() {
  49. unset($this->Gravatar);
  50. }
  51. /**
  52. * testBaseUrlGeneration
  53. *
  54. * @return void
  55. * @access public
  56. */
  57. public function testBaseUrlGeneration() {
  58. $expected = 'http://www.gravatar.com/avatar/' . Security::hash('example@gravatar.com', 'md5');
  59. $result = $this->Gravatar->url('example@gravatar.com', array('ext' => false, 'default' => 'wavatar'));
  60. list($url, $params) = explode('?', $result);
  61. $this->assertEqual($expected, $url);
  62. }
  63. /**
  64. * testExtensions
  65. *
  66. * @return void
  67. * @access public
  68. */
  69. public function testExtensions() {
  70. $result = $this->Gravatar->url('example@gravatar.com', array('ext' => true, 'default' => 'wavatar'));
  71. $this->assertPattern('/\.jpg(?:$|\?)/', $result);
  72. }
  73. /**
  74. * testRating
  75. *
  76. * @return void
  77. * @access public
  78. */
  79. public function testRating() {
  80. $result = $this->Gravatar->url('example@gravatar.com', array('ext' => true, 'default' => 'wavatar'));
  81. $this->assertPattern('/\.jpg(?:$|\?)/', $result);
  82. }
  83. /**
  84. * testAlternateDefaultIcon
  85. *
  86. * @return void
  87. * @access public
  88. */
  89. public function testAlternateDefaultIcon() {
  90. $result = $this->Gravatar->url('example@gravatar.com', array('ext' => false, 'default' => 'wavatar'));
  91. list($url, $params) = explode('?', $result);
  92. $this->assertPattern('/default=wavatar/', $params);
  93. }
  94. /**
  95. * testAlternateDefaultIconCorrection
  96. *
  97. * @return void
  98. * @access public
  99. */
  100. public function testAlternateDefaultIconCorrection() {
  101. $result = $this->Gravatar->url('example@gravatar.com', array('ext' => false, 'default' => '12345'));
  102. $this->assertPattern('/[^\?]+/', $result);
  103. }
  104. /**
  105. * testSize
  106. *
  107. * @return void
  108. * @access public
  109. */
  110. public function testSize() {
  111. $result = $this->Gravatar->url('example@gravatar.com', array('size' => '120'));
  112. list($url, $params) = explode('?', $result);
  113. $this->assertPattern('/size=120/', $params);
  114. }
  115. /**
  116. * testImageTag
  117. *
  118. * @return void
  119. * @access public
  120. */
  121. public function testImageTag() {
  122. $expected = '<img src="http://www.gravatar.com/avatar/' . Security::hash('example@gravatar.com', 'md5') . '" alt="" />';
  123. $result = $this->Gravatar->image('example@gravatar.com', array('ext' => false));
  124. $this->assertEqual($expected, $result);
  125. $expected = '<img src="http://www.gravatar.com/avatar/' . Security::hash('example@gravatar.com', 'md5') . '" alt="Gravatar" />';
  126. $result = $this->Gravatar->image('example@gravatar.com', array('ext' => false, 'alt' => 'Gravatar'));
  127. $this->assertEqual($expected, $result);
  128. }
  129. /**
  130. * testDefaulting
  131. *
  132. * @return void
  133. * @access public
  134. */
  135. public function testDefaulting() {
  136. $result = $this->Gravatar->url('example@gravatar.com', array('default' => 'wavatar', 'size' => 'default'));
  137. list($url, $params) = explode('?', $result);
  138. $this->assertEqual($params, 'default=wavatar');
  139. }
  140. /**
  141. * testNonSecureUrl
  142. *
  143. * @return void
  144. * @access public
  145. */
  146. public function testNonSecureUrl() {
  147. $_SERVER['HTTPS'] = false;
  148. $expected = 'http://www.gravatar.com/avatar/' . Security::hash('example@gravatar.com', 'md5');
  149. $result = $this->Gravatar->url('example@gravatar.com', array('ext' => false));
  150. $this->assertEqual($expected, $result);
  151. $expected = 'http://www.gravatar.com/avatar/' . Security::hash('example@gravatar.com', 'md5');
  152. $result = $this->Gravatar->url('example@gravatar.com', array('ext' => false, 'secure' => false));
  153. $this->assertEqual($expected, $result);
  154. $_SERVER['HTTPS'] = true;
  155. $expected = 'http://www.gravatar.com/avatar/' . Security::hash('example@gravatar.com', 'md5');
  156. $result = $this->Gravatar->url('example@gravatar.com', array('ext' => false, 'secure' => false));
  157. $this->assertEqual($expected, $result);
  158. }
  159. /**
  160. * testSecureUrl
  161. *
  162. * @return void
  163. * @access public
  164. */
  165. public function testSecureUrl() {
  166. $expected = 'https://secure.gravatar.com/avatar/' . Security::hash('example@gravatar.com', 'md5');
  167. $result = $this->Gravatar->url('example@gravatar.com', array('ext' => false, 'secure' => true));
  168. $this->assertEqual($expected, $result);
  169. $_SERVER['HTTPS'] = true;
  170. $expected = 'http://www.gravatar.com/avatar/' . Security::hash('example@gravatar.com', 'md5');
  171. $result = $this->Gravatar->url('example@gravatar.com', array('ext' => false));
  172. $this->assertEqual($expected, $result);
  173. $expected = 'https://secure.gravatar.com/avatar/' . Security::hash('example@gravatar.com', 'md5');
  174. $result = $this->Gravatar->url('example@gravatar.com', array('ext' => false, 'secure' => true));
  175. $this->assertEqual($expected, $result);
  176. }
  177. }