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