SitemapTest.php 8.5 KB

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