PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/standard/tags/release-1.5.0PR/tests/Zend/Controller/Action/Helper/RedirectorTest.php

https://github.com/bhaumik25/zend-framework
PHP | 368 lines | 265 code | 55 blank | 48 comment | 6 complexity | 3a9017c27d56ea5aac55cb0e2f9c19a3 MD5 | raw file
  1. <?php
  2. // Call Zend_Controller_Action_Helper_RedirectorTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_Controller_Action_Helper_RedirectorTest::main");
  5. $ZFPATH = realpath(dirname(__FILE__) . '/../../../../../../');
  6. set_include_path(
  7. $ZFPATH . DIRECTORY_SEPARATOR . 'incubator' . DIRECTORY_SEPARATOR . 'tests'
  8. . PATH_SEPARATOR . $ZFPATH . DIRECTORY_SEPARATOR . 'incubator' . DIRECTORY_SEPARATOR . 'library'
  9. . PATH_SEPARATOR . $ZFPATH . DIRECTORY_SEPARATOR . 'library'
  10. . PATH_SEPARATOR . get_include_path()
  11. );
  12. }
  13. require_once "PHPUnit/Framework/TestCase.php";
  14. require_once "PHPUnit/Framework/TestSuite.php";
  15. require_once 'Zend/Controller/Front.php';
  16. require_once 'Zend/Controller/Action.php';
  17. require_once 'Zend/Controller/Action/HelperBroker.php';
  18. require_once 'Zend/Controller/Action/Helper/Redirector.php';
  19. require_once 'Zend/Controller/Request/Http.php';
  20. require_once 'Zend/Controller/Response/Http.php';
  21. /**
  22. * Test class for Zend_Controller_Action_Helper_Redirector.
  23. * Generated by PHPUnit_Util_Skeleton on 2007-05-04 at 09:28:59.
  24. */
  25. class Zend_Controller_Action_Helper_RedirectorTest extends PHPUnit_Framework_TestCase
  26. {
  27. /**
  28. * @var Zend_Controller_Action_Helper_Redirector
  29. */
  30. public $redirector;
  31. /**
  32. * @var Zend_Controller_Request_Http
  33. */
  34. public $request;
  35. /**
  36. * @var Zend_Controller_Response_Http
  37. */
  38. public $response;
  39. /**
  40. * @var Zend_Controller_Action
  41. */
  42. public $controller;
  43. /**
  44. * Runs the test methods of this class.
  45. */
  46. public static function main()
  47. {
  48. require_once "PHPUnit/TextUI/TestRunner.php";
  49. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_Action_Helper_RedirectorTest");
  50. $result = PHPUnit_TextUI_TestRunner::run($suite);
  51. }
  52. /**
  53. * Set up redirector
  54. *
  55. * Creates request, response, and action controller objects; sets action
  56. * controller in redirector, and sets exit to false.
  57. *
  58. * Also resets the front controller instance.
  59. */
  60. public function setUp()
  61. {
  62. $front = Zend_Controller_Front::getInstance();
  63. $front->resetInstance();
  64. Zend_Controller_Action_HelperBroker::removeHelper('viewRenderer');
  65. $this->redirector = new Zend_Controller_Action_Helper_Redirector();
  66. $this->request = new Zend_Controller_Request_Http();
  67. $this->response = new Zend_Controller_Response_Http();
  68. $this->controller = new Zend_Controller_Action_Helper_Redirector_TestController(
  69. $this->request,
  70. $this->response,
  71. array()
  72. );
  73. // do this so setting headers does not throw exceptions
  74. $this->response->headersSentThrowsException = false;
  75. $this->redirector->setExit(false)
  76. ->setActionController($this->controller);
  77. $this->_server = $_SERVER;
  78. }
  79. /**
  80. * Unset all properties
  81. */
  82. public function tearDown()
  83. {
  84. unset($this->redirector);
  85. unset($this->controller);
  86. unset($this->request);
  87. unset($this->response);
  88. $_SERVER = $this->_server;
  89. }
  90. public function testCode()
  91. {
  92. $this->assertEquals(302, $this->redirector->getCode(), 'Default code should be 302');
  93. $this->redirector->setCode(301);
  94. $this->assertEquals(301, $this->redirector->getCode());
  95. try {
  96. $this->redirector->setCode(251);
  97. $this->fail('Invalid redirect code should throw exception');
  98. } catch (Exception $e) {
  99. }
  100. try {
  101. $this->redirector->setCode(351);
  102. $this->fail('Invalid redirect code should throw exception');
  103. } catch (Exception $e) {
  104. }
  105. }
  106. public function testExit()
  107. {
  108. $this->assertFalse($this->redirector->getExit());
  109. $this->redirector->setExit(true);
  110. $this->assertTrue($this->redirector->getExit());
  111. }
  112. public function testPrependBase()
  113. {
  114. $this->assertTrue($this->redirector->getPrependBase());
  115. $this->redirector->setPrependBase(false);
  116. $this->assertFalse($this->redirector->getPrependBase());
  117. }
  118. public function testGetRedirectUrlNullByDefault()
  119. {
  120. $this->assertNull($this->redirector->getRedirectUrl());
  121. }
  122. public function testSetGotoWithActionOnly()
  123. {
  124. $request = $this->request;
  125. $request->setModuleName('blog')
  126. ->setControllerName('list')
  127. ->setActionName('all');
  128. $this->redirector->setGoto('error');
  129. $this->assertEquals('/blog/list/error', $this->redirector->getRedirectUrl());
  130. }
  131. public function testSetGotoWithActionAndController()
  132. {
  133. $request = $this->request;
  134. $request->setModuleName('blog')
  135. ->setControllerName('list')
  136. ->setActionName('all');
  137. $this->redirector->setGoto('item', 'view');
  138. $this->assertEquals('/blog/view/item', $this->redirector->getRedirectUrl());
  139. }
  140. public function testSetGotoWithActionControllerAndModule()
  141. {
  142. $request = $this->request;
  143. $request->setModuleName('blog')
  144. ->setControllerName('list')
  145. ->setActionName('all');
  146. $this->redirector->setGoto('item', 'view', 'news');
  147. $this->assertEquals('/news/view/item', $this->redirector->getRedirectUrl());
  148. }
  149. public function testSetGotoWithActionControllerModuleAndParams()
  150. {
  151. $request = $this->request;
  152. $request->setModuleName('blog')
  153. ->setControllerName('list')
  154. ->setActionName('all');
  155. $this->redirector->setGoto('item', 'view', 'news', array('id' => 42));
  156. $this->assertEquals('/news/view/item/id/42', $this->redirector->getRedirectUrl());
  157. }
  158. public function testSetGotoRoute()
  159. {
  160. $router = Zend_Controller_Front::getInstance()->getRouter();
  161. $route = new Zend_Controller_Router_Route(
  162. 'blog/archive/:id',
  163. array('controller' => 'blog', 'action' => 'view', 'id' => false),
  164. array('id' => '\d+')
  165. );
  166. $router->addRoute('blogArchive', $route);
  167. $this->redirector->setGotoRoute(
  168. array('id' => 281),
  169. 'blogArchive'
  170. );
  171. $this->assertEquals('/blog/archive/281', $this->redirector->getRedirectUrl());
  172. }
  173. public function testSetGotoUrl()
  174. {
  175. $this->redirector->setGotoUrl('/foo/bar');
  176. $this->assertEquals('/foo/bar', $this->redirector->getRedirectUrl());
  177. }
  178. public function testSetGotoUrlWithBaseUrlUsingPrependBaseProperty()
  179. {
  180. $this->request->setBaseUrl('/my');
  181. $this->redirector->setPrependBase(true);
  182. $this->redirector->setGotoUrl('/foo/bar');
  183. $this->assertEquals('/my/foo/bar', $this->redirector->getRedirectUrl());
  184. }
  185. public function testSetGotoUrlWithBaseUrlUsingPrependBaseOption()
  186. {
  187. $this->request->setBaseUrl('/my');
  188. $this->redirector->setGotoUrl('/foo/bar', array('prependBase' => true));
  189. $this->assertEquals('/my/foo/bar', $this->redirector->getRedirectUrl());
  190. }
  191. public function testSetGotoUrlWithHttpCodeUsingCodeProperty()
  192. {
  193. $this->redirector->setCode(301);
  194. $this->redirector->setGotoUrl('/foo/bar');
  195. $this->assertEquals('/foo/bar', $this->redirector->getRedirectUrl());
  196. $this->assertEquals(301, $this->response->getHttpResponseCode());
  197. }
  198. public function testSetGotoUrlWithHttpCodeUsingCodeOption()
  199. {
  200. $this->redirector->setGotoUrl('/foo/bar', array('code' => 301));
  201. $this->assertEquals('/foo/bar', $this->redirector->getRedirectUrl());
  202. $this->assertEquals(301, $this->response->getHttpResponseCode());
  203. }
  204. /**
  205. * goto() is an alias for setGoto(); just do a single test case
  206. */
  207. public function testGoto()
  208. {
  209. $request = $this->request;
  210. $request->setModuleName('blog')
  211. ->setControllerName('list')
  212. ->setActionName('all');
  213. $this->redirector->goto('error');
  214. $this->assertEquals('/blog/list/error', $this->redirector->getRedirectUrl());
  215. }
  216. public function testGotoAndExit()
  217. {
  218. $this->markTestSkipped(
  219. "Testing Zend_Controller_Action_Helper_Redirector::gotoAndExit() would break the test suite"
  220. );
  221. }
  222. /**
  223. * gotoRoute() is an alias for setGotoRoute()
  224. */
  225. public function testGotoRoute()
  226. {
  227. $router = Zend_Controller_Front::getInstance()->getRouter();
  228. $route = new Zend_Controller_Router_Route(
  229. 'blog/archive/:id',
  230. array('controller' => 'blog', 'action' => 'view', 'id' => false),
  231. array('id' => '\d+')
  232. );
  233. $router->addRoute('blogArchive', $route);
  234. $this->redirector->gotoRoute(
  235. array('id' => 281),
  236. 'blogArchive'
  237. );
  238. $this->assertEquals('/blog/archive/281', $this->redirector->getRedirectUrl());
  239. }
  240. public function testGotoRouteAndExit()
  241. {
  242. $this->markTestSkipped(
  243. "Testing Zend_Controller_Action_Helper_Redirector::gotoRouteAndExit() would break the test suite"
  244. );
  245. }
  246. /**
  247. * gotoUrl() is an alias for setGotoUrl()
  248. */
  249. public function testGotoUrl()
  250. {
  251. $this->redirector->gotoUrl('/foo/bar');
  252. $this->assertEquals('/foo/bar', $this->redirector->getRedirectUrl());
  253. }
  254. public function testGotoUrlAndExit()
  255. {
  256. $this->markTestSkipped(
  257. "Testing Zend_Controller_Action_Helper_Redirector::gotoUrlAndExit() would break the test suite"
  258. );
  259. }
  260. public function testRedirectAndExit()
  261. {
  262. $this->markTestSkipped(
  263. "Testing Zend_Controller_Action_Helper_Redirector::redirectAndExit() would break the test suite"
  264. );
  265. }
  266. /**
  267. * direct() is an alias for goto(), which is an alias for setGoto()
  268. */
  269. public function testDirect()
  270. {
  271. $request = $this->request;
  272. $request->setModuleName('blog')
  273. ->setControllerName('list')
  274. ->setActionName('all');
  275. $this->redirector->direct('error');
  276. $this->assertEquals('/blog/list/error', $this->redirector->getRedirectUrl());
  277. }
  278. public function testUseAbsoluteUriFlag()
  279. {
  280. $this->assertFalse($this->redirector->getUseAbsoluteUri());
  281. $this->redirector->setUseAbsoluteUri(true);
  282. $this->assertTrue($this->redirector->getUseAbsoluteUri());
  283. }
  284. public function testUseAbsoluteUriSetsFullUriInResponse()
  285. {
  286. $_SERVER['HTTP_HOST'] = 'foobar.example.com';
  287. $_SERVER['SERVER_PORT'] = '4443';
  288. $_SERVER['HTTPS'] = 1;
  289. $this->redirector->setUseAbsoluteUri(true);
  290. $this->redirector->gotoUrl('/bar/baz');
  291. $headers = $this->response->getHeaders();
  292. $uri = false;
  293. foreach ($headers as $header) {
  294. if ('Location' == $header['name']) {
  295. $uri = $header['value'];
  296. }
  297. }
  298. if (!$uri) {
  299. $this->fail('No redirect header set in response');
  300. }
  301. $this->assertEquals('https://foobar.example.com:4443/bar/baz', $uri);
  302. }
  303. }
  304. /**
  305. * Test controller for use with redirector tests
  306. */
  307. class Zend_Controller_Action_Helper_Redirector_TestController extends Zend_Controller_Action
  308. {
  309. }
  310. // Call Zend_Controller_Action_Helper_RedirectorTest::main() if this source file is executed directly.
  311. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Action_Helper_RedirectorTest::main") {
  312. Zend_Controller_Action_Helper_RedirectorTest::main();
  313. }