SitemapTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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-2014 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. require_once dirname(__FILE__) . '/TestAbstract.php';
  23. require_once 'Zend/Controller/Front.php';
  24. require_once 'Zend/Controller/Request/Http.php';
  25. require_once 'Zend/View/Helper/Navigation/Sitemap.php';
  26. /**
  27. * Tests Zend_View_Helper_Navigation_Sitemap
  28. *
  29. * @category Zend
  30. * @package Zend_View
  31. * @subpackage UnitTests
  32. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. * @group Zend_View
  35. * @group Zend_View_Helper
  36. */
  37. class Zend_View_Helper_Navigation_SitemapTest
  38. extends Zend_View_Helper_Navigation_TestAbstract
  39. {
  40. protected $_front;
  41. protected $_oldRequest;
  42. protected $_oldRouter;
  43. protected $_oldServer = array();
  44. /**
  45. * Class name for view helper to test
  46. *
  47. * @var string
  48. */
  49. protected $_helperName = 'Zend_View_Helper_Navigation_Sitemap';
  50. /**
  51. * View helper
  52. *
  53. * @var Zend_View_Helper_Navigation_Sitemap
  54. */
  55. protected $_helper;
  56. protected function setUp()
  57. {
  58. date_default_timezone_set('Europe/Berlin');
  59. if (isset($_SERVER['SERVER_NAME'])) {
  60. $this->_oldServer['SERVER_NAME'] = $_SERVER['SERVER_NAME'];
  61. }
  62. if (isset($_SERVER['SERVER_PORT'])) {
  63. $this->_oldServer['SERVER_PORT'] = $_SERVER['SERVER_PORT'];
  64. }
  65. if (isset($_SERVER['REQUEST_URI'])) {
  66. $this->_oldServer['REQUEST_URI'] = $_SERVER['REQUEST_URI'];
  67. }
  68. $_SERVER['SERVER_NAME'] = 'localhost';
  69. $_SERVER['SERVER_PORT'] = 80;
  70. $_SERVER['REQUEST_URI'] = '/';
  71. $this->_front = Zend_Controller_Front::getInstance();
  72. $this->_oldRequest = $this->_front->getRequest();
  73. $this->_oldRouter = $this->_front->getRouter();
  74. $this->_front->resetInstance();
  75. $this->_front->setRequest(new Zend_Controller_Request_Http());
  76. $this->_front->getRouter()->addDefaultRoutes();
  77. parent::setUp();
  78. $this->_helper->setFormatOutput(true);
  79. }
  80. protected function tearDown()
  81. {
  82. if (null !== $this->_oldRequest) {
  83. $this->_front->setRequest($this->_oldRequest);
  84. } else {
  85. $this->_front->setRequest(new Zend_Controller_Request_Http());
  86. }
  87. $this->_front->setRouter($this->_oldRouter);
  88. foreach ($this->_oldServer as $key => $value) {
  89. $_SERVER[$key] = $value;
  90. }
  91. }
  92. public function testHelperEntryPointWithoutAnyParams()
  93. {
  94. $returned = $this->_helper->sitemap();
  95. $this->assertEquals($this->_helper, $returned);
  96. $this->assertEquals($this->_nav1, $returned->getContainer());
  97. }
  98. public function testHelperEntryPointWithContainerParam()
  99. {
  100. $returned = $this->_helper->sitemap($this->_nav2);
  101. $this->assertEquals($this->_helper, $returned);
  102. $this->assertEquals($this->_nav2, $returned->getContainer());
  103. }
  104. public function testNullingOutNavigation()
  105. {
  106. $this->_helper->setContainer();
  107. $this->assertEquals(0, count($this->_helper->getContainer()));
  108. }
  109. public function testAutoloadContainerFromRegistry()
  110. {
  111. $oldReg = null;
  112. if (Zend_Registry::isRegistered(self::REGISTRY_KEY)) {
  113. $oldReg = Zend_Registry::get(self::REGISTRY_KEY);
  114. }
  115. Zend_Registry::set(self::REGISTRY_KEY, $this->_nav1);
  116. $this->_helper->setContainer(null);
  117. $expected = $this->_getExpected('sitemap/default1.xml');
  118. $actual = $this->_helper->render();
  119. Zend_Registry::set(self::REGISTRY_KEY, $oldReg);
  120. $this->assertEquals($expected, $expected);
  121. }
  122. public function testRenderSuppliedContainerWithoutInterfering()
  123. {
  124. $rendered1 = $this->_getExpected('sitemap/default1.xml');
  125. $rendered2 = $this->_getExpected('sitemap/default2.xml');
  126. $expected = array(
  127. 'registered' => $rendered1,
  128. 'supplied' => $rendered2,
  129. 'registered_again' => $rendered1
  130. );
  131. $actual = array(
  132. 'registered' => $this->_helper->render(),
  133. 'supplied' => $this->_helper->render($this->_nav2),
  134. 'registered_again' => $this->_helper->render()
  135. );
  136. $this->assertEquals($expected, $actual);
  137. }
  138. public function testUseAclRoles()
  139. {
  140. $acl = $this->_getAcl();
  141. $this->_helper->setAcl($acl['acl']);
  142. $this->_helper->setRole($acl['role']);
  143. $expected = $this->_getExpected('sitemap/acl.xml');
  144. $this->assertEquals($expected, $this->_helper->render());
  145. }
  146. public function testUseAclButNoRole()
  147. {
  148. $acl = $this->_getAcl();
  149. $this->_helper->setAcl($acl['acl']);
  150. $this->_helper->setRole(null);
  151. $expected = $this->_getExpected('sitemap/acl2.xml');
  152. $this->assertEquals($expected, $this->_helper->render());
  153. }
  154. public function testSettingMaxDepth()
  155. {
  156. $this->_helper->setMaxDepth(0);
  157. $expected = $this->_getExpected('sitemap/depth1.xml');
  158. $this->assertEquals($expected, $this->_helper->render());
  159. }
  160. public function testSettingMinDepth()
  161. {
  162. $this->_helper->setMinDepth(1);
  163. $expected = $this->_getExpected('sitemap/depth2.xml');
  164. $this->assertEquals($expected, $this->_helper->render());
  165. }
  166. public function testSettingBothDepths()
  167. {
  168. $this->_helper->setMinDepth(1)->setMaxDepth(2);
  169. $expected = $this->_getExpected('sitemap/depth3.xml');
  170. $this->assertEquals($expected, $this->_helper->render());
  171. }
  172. public function testDropXmlDeclaration()
  173. {
  174. $this->_helper->setUseXmlDeclaration(false);
  175. $expected = $this->_getExpected('sitemap/nodecl.xml');
  176. $this->assertEquals($expected, $this->_helper->render($this->_nav2));
  177. }
  178. public function testThrowExceptionOnInvalidLoc()
  179. {
  180. $nav = clone $this->_nav2;
  181. $nav->addPage(array('label' => 'Invalid', 'uri' => 'http://w..'));
  182. try {
  183. $this->_helper->render($nav);
  184. } catch (Zend_View_Exception $e) {
  185. $expected = sprintf(
  186. 'Encountered an invalid URL for Sitemap XML: "%s"',
  187. 'http://w..');
  188. $actual = $e->getMessage();
  189. $this->assertEquals($expected, $actual);
  190. return;
  191. }
  192. $this->fail('A Zend_View_Exception was not thrown on invalid <loc />');
  193. }
  194. public function testDisablingValidators()
  195. {
  196. $nav = clone $this->_nav2;
  197. $nav->addPage(array('label' => 'Invalid', 'uri' => 'http://w.'));
  198. $this->_helper->setUseSitemapValidators(false);
  199. $expected = $this->_getExpected('sitemap/invalid.xml');
  200. $this->assertXmlStringEqualsXmlString($expected, $this->_helper->render($nav));
  201. }
  202. public function testSetServerUrlRequiresValidUri()
  203. {
  204. try {
  205. $this->_helper->setServerUrl('site.example.org');
  206. $this->fail('An invalid server URL was given, but a ' .
  207. 'Zend_Uri_Exception was not thrown');
  208. } catch (Zend_Uri_Exception $e) {
  209. $this->assertContains('Illegal scheme', $e->getMessage());
  210. }
  211. }
  212. public function testSetServerUrlWithSchemeAndHost()
  213. {
  214. $this->_helper->setServerUrl('http://sub.example.org');
  215. $expected = $this->_getExpected('sitemap/serverurl1.xml');
  216. $this->assertEquals($expected, $this->_helper->render());
  217. }
  218. public function testSetServerUrlWithSchemeAndPortAndHostAndPath()
  219. {
  220. $this->_helper->setServerUrl('http://sub.example.org:8080/foo/');
  221. $expected = $this->_getExpected('sitemap/serverurl2.xml');
  222. $this->assertEquals($expected, $this->_helper->render());
  223. }
  224. public function testGetUserSchemaValidation()
  225. {
  226. $this->_helper->setUseSchemaValidation(true);
  227. $this->assertTrue($this->_helper->getUseSchemaValidation());
  228. $this->_helper->setUseSchemaValidation(false);
  229. $this->assertFalse($this->_helper->getUseSchemaValidation());
  230. }
  231. public function testUseSchemaValidation()
  232. {
  233. $this->markTestSkipped('Skipped because it fetches XSD from web');
  234. return;
  235. $nav = clone $this->_nav2;
  236. $this->_helper->setUseSitemapValidators(false);
  237. $this->_helper->setUseSchemaValidation(true);
  238. $nav->addPage(array('label' => 'Invalid', 'uri' => 'http://w.'));
  239. try {
  240. $this->_helper->render($nav);
  241. } catch (Zend_View_Exception $e) {
  242. $expected = sprintf(
  243. 'Sitemap is invalid according to XML Schema at "%s"',
  244. Zend_View_Helper_Navigation_Sitemap::SITEMAP_XSD);
  245. $actual = $e->getMessage();
  246. $this->assertEquals($expected, $actual);
  247. return;
  248. }
  249. $this->fail('A Zend_View_Exception was not thrown when using Schema validation');
  250. }
  251. /**
  252. * @group ZF-8874
  253. */
  254. public function testRenderingWithoutWhitespace()
  255. {
  256. // Reset format output option
  257. $this->_helper->setFormatOutput(false);
  258. $expected = $this->_helper->render();
  259. $actual = $this->_getExpected('sitemap/without_whitespace.xml');
  260. $this->assertEquals($expected, $actual);
  261. }
  262. }