PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/Test/Case/View/Helper/GravatarHelperTest.php

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