GravatarTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_View
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: BaseUrlTest.php 20096 2010-01-06 02:05:09Z bkarwin $
  21. */
  22. // Call Zend_View_Helper_BaseUrlTest::main() if this source file is executed directly.
  23. if (!defined('PHPUnit_MAIN_METHOD')) {
  24. define('PHPUnit_MAIN_METHOD', 'Zend_View_Helper_GravatarTest::main');
  25. }
  26. /**
  27. * @see Zend_View_Helper_Gravatar
  28. */
  29. require_once 'Zend/View/Helper/Gravatar.php';
  30. /**
  31. * @see Zend_View
  32. */
  33. require_once 'Zend/View.php';
  34. /**
  35. * @category Zend
  36. * @package Zend_View
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @group Zend_View
  41. * @group Zend_View_Helper
  42. */
  43. class Zend_View_Helper_GravatarTest extends PHPUnit_Framework_TestCase
  44. {
  45. /**
  46. * @var Zend_View_Helper_Gravatar
  47. */
  48. protected $_object;
  49. /**
  50. * @var Zend_View
  51. */
  52. protected $_view;
  53. /**
  54. * Main
  55. */
  56. public static function main()
  57. {
  58. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_GravatarTest");
  59. $result = PHPUnit_TextUI_TestRunner::run($suite);
  60. }
  61. /**
  62. * Prepares the environment before running a test.
  63. */
  64. protected function setUp()
  65. {
  66. $this->_object = new Zend_View_Helper_Gravatar();
  67. $this->_view = new Zend_View();
  68. $this->_view->doctype()->setDoctype(strtoupper("XHTML1_STRICT"));
  69. $this->_object->setView($this->_view);
  70. if( isset($_SERVER['HTTPS'])) {
  71. unset ($_SERVER['HTTPS']);
  72. }
  73. }
  74. /**
  75. * Cleans up the environment after running a test.
  76. */
  77. protected function tearDown()
  78. {
  79. unset($this->_object, $this->_view);
  80. }
  81. /**
  82. * Test default options.
  83. */
  84. public function testGravataXHTMLDoctype()
  85. {
  86. $this->assertRegExp('/\/>$/',
  87. $this->_object->gravatar('example@example.com')->__toString());
  88. }
  89. /**
  90. * Test if doctype is HTML
  91. */
  92. public function testGravatarHTMLDoctype()
  93. {
  94. $object = new Zend_View_Helper_Gravatar();
  95. $view = new Zend_View();
  96. $view->doctype()->setDoctype(strtoupper("HTML5"));
  97. $object->setView($view);
  98. $this->assertRegExp('/[^\/]>$/',
  99. $this->_object->gravatar('example@example.com')->__toString());
  100. }
  101. /**
  102. * Test get set methods
  103. */
  104. public function testGetAndSetMethods()
  105. {
  106. $attribs = array('class' => 'gravatar', 'title' => 'avatar', 'id' => 'gravatar-1');
  107. $this->_object->setDefaultImg('monsterid')
  108. ->setImgSize(150)
  109. ->setSecure(true)
  110. ->setEmail("example@example.com")
  111. ->setAttribs($attribs)
  112. ->setRating('pg');
  113. $this->assertEquals("monsterid", $this->_object->getDefaultImg());
  114. $this->assertEquals("pg", $this->_object->getRating());
  115. $this->assertEquals("example@example.com", $this->_object->getEmail());
  116. $this->assertEquals($attribs, $this->_object->getAttribs());
  117. $this->assertEquals(150, $this->_object->getImgSize());
  118. $this->assertTrue($this->_object->getSecure());
  119. }
  120. public function tesSetDefaultImg()
  121. {
  122. $this->_object->gravatar("example@example.com");
  123. $img = array(
  124. "wavatar",
  125. "http://www.example.com/images/avatar/example.png",
  126. Zend_View_Helper_Gravatar::DEFAULT_MONSTERID,
  127. );
  128. foreach ($img as $value) {
  129. $this->_object->setDefaultImg($value);
  130. $this->assertEquals(urlencode($value), $this->_object->getDefaultImg());
  131. }
  132. }
  133. public function testSetImgSize()
  134. {
  135. $imgSizesRight = array(1, 500, "600");
  136. foreach ($imgSizesRight as $value) {
  137. $this->_object->setImgSize($value);
  138. $this->assertTrue(is_int($this->_object->getImgSize()));
  139. }
  140. }
  141. public function testInvalidRatingParametr()
  142. {
  143. $ratingsWrong = array( 'a', 'cs', 456);
  144. $this->setExpectedException('Zend_View_Exception');
  145. foreach ($ratingsWrong as $value) {
  146. $this->_object->setRating($value);
  147. }
  148. }
  149. public function testSetRating()
  150. {
  151. $ratingsRight = array( 'g', 'pg', 'r', 'x', Zend_View_Helper_Gravatar::RATING_R);
  152. foreach ($ratingsRight as $value) {
  153. $this->_object->setRating($value);
  154. $this->assertEquals($value, $this->_object->getRating());
  155. }
  156. }
  157. public function testSetSecure()
  158. {
  159. $values = array("true", "false", "text", $this->_view, 100, true, "", null, 0, false);
  160. foreach ($values as $value) {
  161. $this->_object->setSecure($value);
  162. $this->assertTrue(is_bool($this->_object->getSecure()));
  163. }
  164. }
  165. /**
  166. * Test SSL location
  167. */
  168. public function testHttpsSource()
  169. {
  170. $this->assertRegExp('/src="https:\/\/secure.gravatar.com\/avatar\/[a-z0-9]{32}.+"/',
  171. $this->_object->gravatar("example@example.com", array('secure' => true))->__toString());
  172. }
  173. /**
  174. * Test HTML attribs
  175. */
  176. public function testImgAttribs()
  177. {
  178. $this->assertRegExp('/class="gravatar" title="Gravatar"/',
  179. $this->_object->gravatar("example@example.com", array(),
  180. array('class' => 'gravatar', 'title' => 'Gravatar'))
  181. ->__toString()
  182. );
  183. }
  184. /**
  185. * Test gravatar's options (rating, size, default image and secure)
  186. */
  187. public function testGravatarOptions()
  188. {
  189. $this->assertRegExp('/src="http:\/\/www.gravatar.com\/avatar\/[a-z0-9]{32}\?s=125&amp;d=wavatar&amp;r=pg"/',
  190. $this->_object->gravatar("example@example.com",
  191. array('rating' => 'pg', 'imgSize' => 125, 'defaultImg' => 'wavatar',
  192. 'secure' => false))
  193. ->__toString()
  194. );
  195. }
  196. /**
  197. * Test auto detect location.
  198. * If request was made through the HTTPS protocol use secure location.
  199. */
  200. public function testAutoDetectLocation()
  201. {
  202. $values = array("on", "", 1, true);
  203. foreach ($values as $value) {
  204. $_SERVER['HTTPS'] = $value;
  205. $this->assertRegExp('/src="https:\/\/secure.gravatar.com\/avatar\/[a-z0-9]{32}.+"/',
  206. $this->_object->gravatar("example@example.com")->__toString());
  207. }
  208. }
  209. /**
  210. * @link http://php.net/manual/en/reserved.variables.server.php Section "HTTPS"
  211. */
  212. public function testAutoDetectLocationOnIis()
  213. {
  214. $_SERVER['HTTPS'] = "off";
  215. $this->assertRegExp('/src="http:\/\/www.gravatar.com\/avatar\/[a-z0-9]{32}.+"/',
  216. $this->_object->gravatar("example@example.com")->__toString());
  217. }
  218. public function testSetAttribsWithSrcKey()
  219. {
  220. $email = 'example@example.com';
  221. $this->_object->setEmail($email);
  222. $this->_object->setAttribs(array(
  223. 'class' => 'gravatar',
  224. 'src' => 'http://example.com',
  225. 'id' => 'gravatarID',
  226. ));
  227. $this->assertRegExp('/src="http:\/\/www.gravatar.com\/avatar\/[a-z0-9]{32}.+"/',
  228. $this->_object->getImgTag());
  229. }
  230. public function testForgottenEmailParameter()
  231. {
  232. $this->assertRegExp('/(src="http:\/\/www.gravatar.com\/avatar\/[a-z0-9]{32}.+")/',
  233. $this->_object->getImgTag());
  234. }
  235. public function testReturnImgTag()
  236. {
  237. $this->assertRegExp("/^<img\s.+/",
  238. $this->_object->gravatar("example@example.com")->__toString());
  239. }
  240. public function testReturnThisObject()
  241. {
  242. $this->assertTrue(
  243. $this->_object->gravatar() instanceof Zend_View_Helper_Gravatar
  244. );
  245. }
  246. public function testInvalidKeyPassedToSetOptionsMethod()
  247. {
  248. $options = array(
  249. 'unknown' => array('val' => 1)
  250. );
  251. $this->_object->gravatar()->setOptions($options);
  252. }
  253. }
  254. // Call Zend_View_Helper_BaseUrlTest::main() if this source file is executed directly.
  255. if (PHPUnit_MAIN_METHOD == 'Zend_View_Helper_BaseUrlTest::main') {
  256. Zend_View_Helper_BaseUrlTest::main();
  257. }