2
0

ContextSwitchTest.php 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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_Controller
  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$
  21. */
  22. // Call Zend_Controller_Action_Helper_ContextSwitchTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Controller_Action_Helper_ContextSwitchTest::main");
  25. }
  26. require_once 'Zend/Controller/Action/Helper/ContextSwitch.php';
  27. require_once 'Zend/Config.php';
  28. require_once 'Zend/Controller/Action.php';
  29. require_once 'Zend/Controller/Action/HelperBroker.php';
  30. require_once 'Zend/Controller/Front.php';
  31. require_once 'Zend/Controller/Request/Http.php';
  32. require_once 'Zend/Controller/Response/Cli.php';
  33. require_once 'Zend/Json.php';
  34. require_once 'Zend/Layout.php';
  35. require_once 'Zend/View.php';
  36. require_once 'Zend/View/Interface.php';
  37. /**
  38. * Test class for Zend_Controller_Action_Helper_ContextSwitch.
  39. *
  40. * @category Zend
  41. * @package Zend_Controller
  42. * @subpackage UnitTests
  43. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  44. * @license http://framework.zend.com/license/new-bsd New BSD License
  45. * @group Zend_Controller
  46. * @group Zend_Controller_Action
  47. * @group Zend_Controller_Action_Helper
  48. */
  49. class Zend_Controller_Action_Helper_ContextSwitchTest extends PHPUnit_Framework_TestCase
  50. {
  51. /**
  52. * Runs the test methods of this class.
  53. *
  54. * @access public
  55. * @static
  56. */
  57. public static function main()
  58. {
  59. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_Action_Helper_ContextSwitchTest");
  60. $result = PHPUnit_TextUI_TestRunner::run($suite);
  61. }
  62. /**
  63. * Sets up the fixture, for example, open a network connection.
  64. * This method is called before a test is executed.
  65. *
  66. * @return void
  67. */
  68. public function setUp()
  69. {
  70. Zend_Controller_Action_Helper_ContextSwitchTest_LayoutOverride::resetMvcInstance();
  71. Zend_Controller_Action_HelperBroker::resetHelpers();
  72. $this->front = Zend_Controller_Front::getInstance();
  73. $this->front->resetInstance();
  74. $this->front->addModuleDirectory(dirname(__FILE__) . '/../../_files/modules');
  75. $this->layout = Zend_Layout::startMvc();
  76. $this->helper = new Zend_Controller_Action_Helper_ContextSwitch();
  77. Zend_Controller_Action_HelperBroker::addHelper($this->helper);
  78. $this->request = new Zend_Controller_Request_Http();
  79. $this->response = new Zend_Controller_Response_Cli();
  80. $this->front->setRequest($this->request)
  81. ->setResponse($this->response)
  82. ->addControllerDirectory(dirname(__FILE__));
  83. $this->view = new Zend_View();
  84. $this->viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  85. $this->viewRenderer->setView($this->view);
  86. $this->controller = new Zend_Controller_Action_Helper_ContextSwitchTestController(
  87. $this->request,
  88. $this->response,
  89. array()
  90. );
  91. $this->controller->setupContexts();
  92. $this->helper->setActionController($this->controller);
  93. }
  94. /**
  95. * Tears down the fixture, for example, close a network connection.
  96. * This method is called after a test is executed.
  97. *
  98. * @return void
  99. */
  100. public function tearDown()
  101. {
  102. }
  103. public function testDirectReturnsObjectInstance()
  104. {
  105. $helper = $this->helper->direct();
  106. $this->assertSame($this->helper, $helper);
  107. }
  108. public function testSetSuffixModifiesContextSuffix()
  109. {
  110. $this->helper->setSuffix('xml', 'foobar');
  111. $this->assertContains('foobar', $this->helper->getSuffix('xml'));
  112. }
  113. public function testSetSuffixPrependsToViewRendererSuffixByDefault()
  114. {
  115. $this->helper->setSuffix('xml', 'foobar');
  116. $expected = 'foobar.' . $this->viewRenderer->getViewSuffix();
  117. $this->assertContains($expected, $this->helper->getSuffix('xml'));
  118. }
  119. public function testCanSetSuffixWithoutViewRendererSuffix()
  120. {
  121. $this->helper->setSuffix('xml', 'foobar', false);
  122. $expected = 'foobar';
  123. $this->assertContains($expected, $this->helper->getSuffix('xml'));
  124. }
  125. public function testSuffixAccessorsThrowExceptionOnInvalidContextType()
  126. {
  127. try {
  128. $this->helper->setSuffix('foobar', 'foobar');
  129. $this->fail('setSuffix() should throw exception with invalid context type');
  130. } catch (Zend_Controller_Action_Exception $e) {
  131. $this->assertContains('Cannot set suffix', $e->getMessage());
  132. }
  133. try {
  134. $this->helper->getSuffix('foobar');
  135. $this->fail('getSuffix() should throw exception with invalid context type');
  136. } catch (Zend_Controller_Action_Exception $e) {
  137. $this->assertContains('Cannot retrieve suffix', $e->getMessage());
  138. }
  139. }
  140. public function testCanAddAdditionalHeadersPerContext()
  141. {
  142. $this->helper->addHeader('xml', 'X-Foo', 'Bar');
  143. $headers = $this->helper->getHeaders('xml');
  144. $this->assertTrue(isset($headers['Content-Type']));
  145. $this->assertEquals('application/xml', $headers['Content-Type']);
  146. $this->assertTrue(isset($headers['X-Foo']));
  147. $this->assertEquals('Bar', $headers['X-Foo']);
  148. }
  149. public function testCanAddMultipleHeadersPerContextSimultaneously()
  150. {
  151. $this->helper->addHeaders('xml', array(
  152. 'X-Foo' => 'Bar',
  153. 'X-Bar' => 'Baz'
  154. ));
  155. $headers = $this->helper->getHeaders('xml');
  156. $this->assertTrue(isset($headers['Content-Type']));
  157. $this->assertEquals('application/xml', $headers['Content-Type']);
  158. $this->assertTrue(isset($headers['X-Foo']));
  159. $this->assertEquals('Bar', $headers['X-Foo']);
  160. $this->assertTrue(isset($headers['X-Bar']));
  161. $this->assertEquals('Baz', $headers['X-Bar']);
  162. }
  163. public function testAddHeaderThrowsExceptionWhenReferencingExistingHeader()
  164. {
  165. try {
  166. $this->helper->addHeader('xml', 'Content-Type', 'application/xml');
  167. $this->fail('addHeader() should raise exception for existing headers');
  168. } catch (Zend_Controller_Exception $e) {
  169. $this->assertContains('already exists', $e->getMessage());
  170. }
  171. }
  172. public function testSetHeaderOverwritesHeaderExistingHeader()
  173. {
  174. $this->helper->setHeader('xml', 'Content-Type', 'application/foo-xml');
  175. $this->assertEquals('application/foo-xml', $this->helper->getHeader('xml', 'Content-Type'));
  176. }
  177. public function testSetHeadersOverwritesHeaders()
  178. {
  179. $headers = array(
  180. 'X-Foo' => 'Bar',
  181. 'X-Bar' => 'Baz'
  182. );
  183. $this->helper->setHeaders('xml', $headers);
  184. $this->assertEquals($headers, $this->helper->getHeaders('xml'));
  185. }
  186. public function testCanRemoveSingleHeaders()
  187. {
  188. $this->helper->addHeader('xml', 'X-Foo', 'Bar');
  189. $this->assertEquals('Bar', $this->helper->getHeader('xml', 'X-Foo'));
  190. $this->helper->removeHeader('xml', 'X-Foo');
  191. $this->assertNull($this->helper->getHeader('xml', 'X-Foo'));
  192. }
  193. public function testCanClearAllHeaders()
  194. {
  195. $this->helper->addHeader('xml', 'X-Foo', 'Bar');
  196. $expected = array('Content-Type' => 'application/xml', 'X-Foo' => 'Bar');
  197. $this->assertEquals($expected, $this->helper->getHeaders('xml'));
  198. $this->helper->clearHeaders('xml');
  199. $this->assertEquals(array(), $this->helper->getHeaders('xml'));
  200. }
  201. public function testHeaderAccessorsThrowExceptionOnInvalidContextType()
  202. {
  203. try {
  204. $this->helper->addHeader('foobar', 'foobar', 'baz');
  205. $this->fail('addHeader() should throw exception with invalid context type');
  206. } catch (Zend_Controller_Action_Exception $e) {
  207. $this->assertContains('does not exist', $e->getMessage());
  208. }
  209. try {
  210. $this->helper->setHeader('foobar', 'foobar', 'baz');
  211. $this->fail('setHeader() should throw exception with invalid context type');
  212. } catch (Zend_Controller_Action_Exception $e) {
  213. $this->assertContains('does not exist', $e->getMessage());
  214. }
  215. try {
  216. $this->helper->getHeader('foobar', 'Content-Type');
  217. $this->fail('getHeader() should throw exception with invalid context type');
  218. } catch (Zend_Controller_Action_Exception $e) {
  219. $this->assertContains('does not exist', $e->getMessage());
  220. }
  221. try {
  222. $this->helper->getHeaders('foobar');
  223. $this->fail('getHeaders() should throw exception with invalid context type');
  224. } catch (Zend_Controller_Action_Exception $e) {
  225. $this->assertContains('does not exist', $e->getMessage());
  226. }
  227. try {
  228. $this->helper->addHeaders('foobar', array('X-Foo' => 'Bar'));
  229. $this->fail('addHeaders() should throw exception with invalid context type');
  230. } catch (Zend_Controller_Action_Exception $e) {
  231. $this->assertContains('does not exist', $e->getMessage());
  232. }
  233. try {
  234. $this->helper->setHeaders('foobar', array('X-Foo' => 'Bar'));
  235. $this->fail('setHeaders() should throw exception with invalid context type');
  236. } catch (Zend_Controller_Action_Exception $e) {
  237. $this->assertContains('does not exist', $e->getMessage());
  238. }
  239. try {
  240. $this->helper->removeHeader('foobar', 'X-Foo');
  241. $this->fail('removeHeader() should throw exception with invalid context type');
  242. } catch (Zend_Controller_Action_Exception $e) {
  243. $this->assertContains('does not exist', $e->getMessage());
  244. }
  245. try {
  246. $this->helper->clearHeaders('foobar');
  247. $this->fail('clearHeaders() should throw exception with invalid context type');
  248. } catch (Zend_Controller_Action_Exception $e) {
  249. $this->assertContains('does not exist', $e->getMessage());
  250. }
  251. }
  252. public function testCanSetCallbackByContextAndTrigger()
  253. {
  254. $this->helper->setCallback('xml', 'init', 'htmlentities');
  255. $this->assertEquals('htmlentities', $this->helper->getCallback('xml', 'init'));
  256. $this->helper->setCallback('xml', 'post', array('Zend_Controller_Action_Helper_ContextSwitchTest', 'main'));
  257. $this->assertSame(array('Zend_Controller_Action_Helper_ContextSwitchTest', 'main'), $this->helper->getCallback('xml', 'post'));
  258. }
  259. public function testCanSetAllCallbacksByContext()
  260. {
  261. $callbacks = array(
  262. 'init' => 'htmlentities',
  263. 'post' => array('Zend_Loader', 'registerAutoload')
  264. );
  265. $this->helper->setCallbacks('xml', $callbacks);
  266. $returned = $this->helper->getCallbacks('xml');
  267. $this->assertSame(array_values($callbacks), array_values($returned));
  268. }
  269. public function testCanRemoveCallbackByContextAndTrigger()
  270. {
  271. $this->testCanSetCallbackByContextAndTrigger();
  272. $this->helper->removeCallback('xml', 'init');
  273. $this->assertNull($this->helper->getCallback('xml', 'init'));
  274. }
  275. public function testCanClearAllCallbacksByContext()
  276. {
  277. $this->testCanSetCallbackByContextAndTrigger();
  278. $this->helper->clearCallbacks('xml');
  279. $this->assertSame(array(), $this->helper->getCallbacks('xml'));
  280. }
  281. public function testCanAddContext()
  282. {
  283. $this->helper->addContext('foobar', array(
  284. 'suffix' => 'foo.bar',
  285. 'headers' => array('Content-Type' => 'application/x-foobar', 'X-Foo' => 'Bar'),
  286. ));
  287. $context = $this->helper->getContext('foobar');
  288. $this->assertNotNull($context);
  289. $this->assertTrue(is_array($context));
  290. $this->assertTrue(isset($context['suffix']));
  291. $this->assertTrue(isset($context['headers']));
  292. $this->assertTrue(isset($context['callbacks']));
  293. $this->assertContains('foo.bar', $context['suffix']);
  294. $this->assertEquals('application/x-foobar', $context['headers']['Content-Type']);
  295. $this->assertEquals('Bar', $context['headers']['X-Foo']);
  296. }
  297. public function testAddContextThrowsExceptionIfContextAlreadyExists()
  298. {
  299. try {
  300. $this->helper->addContext('xml', array());
  301. $this->fail('Shold not be able to add context if already exists');
  302. } catch (Zend_Controller_Exception $e) {
  303. $this->assertContains('exists', $e->getMessage());
  304. }
  305. }
  306. public function testSetContextOverwritesExistingContext()
  307. {
  308. $this->helper->setContext('xml', array());
  309. $this->assertNull($this->helper->getHeader('xml', 'Content-Type'));
  310. $this->assertEquals($this->viewRenderer->getViewSuffix(), $this->helper->getSuffix('xml'));
  311. }
  312. public function testCanAddMultipleContextsAtOnce()
  313. {
  314. $this->helper->addContexts(array(
  315. 'foobar' => array(
  316. 'suffix' => 'foo.bar',
  317. 'headers' => array('Content-Type' => 'application/x-foobar', 'X-Foo' => 'Bar'),
  318. ),
  319. 'barbaz' => array(
  320. 'suffix' => 'bar.baz',
  321. 'headers' => array('Content-Type' => 'application/x-barbaz', 'X-Bar' => 'Baz'),
  322. )
  323. ));
  324. $this->assertTrue($this->helper->hasContext('foobar'));
  325. $this->assertTrue($this->helper->hasContext('barbaz'));
  326. }
  327. public function testCanOverwriteManyContextsAtOnce()
  328. {
  329. $this->helper->setContexts(array(
  330. 'xml' => array(
  331. 'suffix' => array('suffix' => 'xml', 'prependViewRendererSuffix' => false),
  332. 'headers' => array('Content-Type' => 'application/xml'),
  333. 'callbacks' => array('TRIGGER_INIT' => 'foobar')
  334. ),
  335. 'foobar' => array(
  336. 'suffix' => 'foo.bar',
  337. 'headers' => array('Content-Type' => 'application/x-foobar', 'X-Foo' => 'Bar'),
  338. ),
  339. 'barbaz' => array(
  340. 'suffix' => 'bar.baz',
  341. 'headers' => array('Content-Type' => 'application/x-barbaz', 'X-Bar' => 'Baz'),
  342. )
  343. ));
  344. $this->assertTrue($this->helper->hasContext('xml'));
  345. $this->assertFalse($this->helper->hasContext('json'));
  346. $this->assertTrue($this->helper->hasContext('foobar'));
  347. $this->assertTrue($this->helper->hasContext('barbaz'));
  348. $this->assertEquals('xml', $this->helper->getSuffix('xml'));
  349. $this->assertNotEquals('foo.bar', $this->helper->getSuffix('foobar'));
  350. $this->assertContains('foo.bar', $this->helper->getSuffix('foobar'));
  351. $this->assertNotEquals('bar.baz', $this->helper->getSuffix('barbaz'));
  352. $this->assertContains('bar.baz', $this->helper->getSuffix('barbaz'));
  353. }
  354. public function testCanRemoveSingleContext()
  355. {
  356. $this->assertTrue($this->helper->hasContext('xml'));
  357. $this->helper->removeContext('xml');
  358. $this->assertFalse($this->helper->hasContext('xml'));
  359. }
  360. public function testCanClearAllContexts()
  361. {
  362. $this->assertTrue($this->helper->hasContext('xml'));
  363. $this->assertTrue($this->helper->hasContext('json'));
  364. $contexts = $this->helper->getContexts();
  365. $this->helper->clearContexts();
  366. $received = $this->helper->getContexts();
  367. $this->assertNotEquals($contexts, $received);
  368. $this->assertTrue(empty($received));
  369. }
  370. public function testDefaultContextParam()
  371. {
  372. $this->assertEquals('format', $this->helper->getContextParam());
  373. }
  374. public function testCanSetContextParam()
  375. {
  376. $this->helper->setContextParam('foobar');
  377. $this->assertEquals('foobar', $this->helper->getContextParam());
  378. }
  379. public function testDefaultContext()
  380. {
  381. $this->assertEquals('xml', $this->helper->getDefaultContext());
  382. }
  383. public function testCanSetDefaultContext()
  384. {
  385. $this->helper->setDefaultContext('json');
  386. $this->assertEquals('json', $this->helper->getDefaultContext());
  387. }
  388. public function testSetDefaultContextThrowsExceptionIfContextDoesNotExist()
  389. {
  390. try {
  391. $this->helper->setDefaultContext('foobar');
  392. $this->fail('setDefaultContext() should raise exception if context does not exist');
  393. } catch (Zend_Controller_Action_Exception $e) {
  394. $this->assertContains('Cannot set default context', $e->getMessage());
  395. }
  396. }
  397. public function testContextSwitchDisablesLayoutsByDefault()
  398. {
  399. $this->assertTrue($this->helper->getAutoDisableLayout());
  400. }
  401. public function testCanChooseWhetherLayoutsAreDisabled()
  402. {
  403. $this->helper->setAutoDisableLayout(false);
  404. $this->assertFalse($this->helper->getAutoDisableLayout());
  405. $this->helper->setAutoDisableLayout(true);
  406. $this->assertTrue($this->helper->getAutoDisableLayout());
  407. }
  408. public function checkNothingIsDone()
  409. {
  410. $this->assertEquals('phtml', $this->viewRenderer->getViewSuffix());
  411. $headers = $this->response->getHeaders();
  412. $this->assertTrue(empty($headers));
  413. }
  414. public function testInitContextDoesNothingIfNoContextsSet()
  415. {
  416. unset($this->controller->contexts);
  417. $this->request->setParam('format', 'xml')
  418. ->setActionName('foo');
  419. $this->helper->initContext();
  420. $this->checkNothingIsDone();
  421. }
  422. public function testInitContextDoesNothingIfActionHasNoContexts()
  423. {
  424. $this->request->setParam('format', 'xml')
  425. ->setActionName('baz');
  426. $this->helper->initContext();
  427. $this->checkNothingIsDone();
  428. $this->request->setParam('format', 'json')
  429. ->setActionName('baz');
  430. $this->helper->initContext();
  431. $this->checkNothingIsDone();
  432. }
  433. public function testInitContextDoesNothingIfActionDoesNotHaveContext()
  434. {
  435. $this->request->setParam('format', 'json')
  436. ->setActionName('foo');
  437. $this->helper->initContext();
  438. $this->checkNothingIsDone();
  439. }
  440. public function testInitContextUsesBooleanTrueActionValueToAssumeAllContexts()
  441. {
  442. $this->request->setParam('format', 'json')
  443. ->setActionName('all');
  444. $this->helper->initContext();
  445. $this->assertEquals('json', $this->helper->getCurrentContext());
  446. $this->assertContains('json', $this->viewRenderer->getViewSuffix());
  447. $this->request->setParam('format', 'xml')
  448. ->setActionName('all');
  449. $this->helper->initContext();
  450. $this->assertEquals('xml', $this->helper->getCurrentContext());
  451. $this->assertContains('xml', $this->viewRenderer->getViewSuffix());
  452. }
  453. public function testInitContextDoesNothingIfActionDoesNotHaveContextAndPassedFormatInvalid()
  454. {
  455. $this->request->setParam('format', 'json')
  456. ->setActionName('foo');
  457. $this->helper->initContext('bogus');
  458. $this->checkNothingIsDone();
  459. }
  460. public function testInitContextSetsViewRendererViewSuffix()
  461. {
  462. $this->request->setParam('format', 'xml')
  463. ->setActionName('foo');
  464. $this->helper->initContext();
  465. $this->assertContains('xml', $this->viewRenderer->getViewSuffix());
  466. }
  467. public function testInitContextSetsAppropriateResponseHeader()
  468. {
  469. $this->request->setParam('format', 'xml')
  470. ->setActionName('foo');
  471. $this->helper->initContext();
  472. $headers = $this->response->getHeaders();
  473. $found = false;
  474. foreach ($headers as $header) {
  475. if ('Content-Type' == $header['name']) {
  476. $found = true;
  477. $value = $header['value'];
  478. }
  479. }
  480. $this->assertTrue($found);
  481. $this->assertEquals('application/xml', $value);
  482. }
  483. public function testInitContextUsesPassedFormatWhenContextParamPresent()
  484. {
  485. $this->request->setParam('format', 'xml')
  486. ->setActionName('foo');
  487. $this->helper->initContext('json');
  488. $this->assertContains('json', $this->viewRenderer->getViewSuffix());
  489. $headers = $this->response->getHeaders();
  490. $found = false;
  491. foreach ($headers as $header) {
  492. if ('Content-Type' == $header['name']) {
  493. $found = true;
  494. $value = $header['value'];
  495. }
  496. }
  497. $this->assertTrue($found);
  498. $this->assertEquals('application/json', $value);
  499. }
  500. public function testInitContextUsesPassedFormatWhenNoContextParamNotPresent()
  501. {
  502. $this->request->setActionName('foo');
  503. $this->helper->initContext('xml');
  504. $this->assertContains('xml', $this->viewRenderer->getViewSuffix());
  505. $headers = $this->response->getHeaders();
  506. $found = false;
  507. foreach ($headers as $header) {
  508. if ('Content-Type' == $header['name']) {
  509. $found = true;
  510. $value = $header['value'];
  511. }
  512. }
  513. $this->assertTrue($found);
  514. $this->assertEquals('application/xml', $value);
  515. }
  516. public function testInitContextDisablesLayoutByDefault()
  517. {
  518. $this->request->setParam('format', 'xml')
  519. ->setActionName('foo');
  520. $this->helper->initContext();
  521. $this->assertFalse($this->layout->isEnabled());
  522. }
  523. public function testInitContextDoesNotDisableLayoutIfDisableLayoutDisabled()
  524. {
  525. $this->helper->setAutoDisableLayout(false);
  526. $this->request->setParam('format', 'xml')
  527. ->setActionName('foo');
  528. $this->helper->initContext();
  529. $this->assertTrue($this->layout->isEnabled());
  530. }
  531. public function testGetCurrentContextInitiallyNull()
  532. {
  533. $this->assertNull($this->helper->getCurrentContext());
  534. }
  535. public function testGetCurrentContextReturnsContextAfterInitContextIsSuccessful()
  536. {
  537. $this->request->setParam('format', 'xml')
  538. ->setActionName('foo');
  539. $this->helper->initContext();
  540. $this->assertEquals('xml', $this->helper->getCurrentContext());
  541. }
  542. public function testGetCurrentContextResetToNullWhenSubsequentInitContextFails()
  543. {
  544. $this->assertNull($this->helper->getCurrentContext());
  545. $this->request->setParam('format', 'xml')
  546. ->setActionName('foo');
  547. $this->helper->initContext();
  548. $this->assertEquals('xml', $this->helper->getCurrentContext());
  549. $this->request->setParam('format', 'foo')
  550. ->setActionName('bogus');
  551. $this->helper->initContext();
  552. $this->assertNull($this->helper->getCurrentContext());
  553. }
  554. public function testGetCurrentContextChangesAfterSubsequentInitContextCalls()
  555. {
  556. $this->assertNull($this->helper->getCurrentContext());
  557. $this->request->setParam('format', 'xml')
  558. ->setActionName('foo');
  559. $this->helper->initContext();
  560. $this->assertEquals('xml', $this->helper->getCurrentContext());
  561. $this->request->setParam('format', 'json')
  562. ->setActionName('bar');
  563. $this->helper->initContext();
  564. $this->assertEquals('json', $this->helper->getCurrentContext());
  565. }
  566. public function testJsonContextShouldEncodeViewVariablesByDefaultAndNotRequireRenderingView()
  567. {
  568. $this->request->setParam('format', 'json')
  569. ->setActionName('bar')
  570. ->setDispatched(true);
  571. $this->controller->dispatch('barAction');
  572. $headers = $this->response->getHeaders();
  573. $found = false;
  574. foreach ($headers as $header) {
  575. if ($header['name'] == 'Content-Type') {
  576. if ($header['value'] == 'application/json') {
  577. $found = true;
  578. }
  579. break;
  580. }
  581. }
  582. $this->assertTrue($found, 'JSON content type header not found');
  583. $body = $this->response->getBody();
  584. $result = Zend_Json::decode($body);
  585. $this->assertTrue(is_array($result), var_export($body, 1));
  586. $this->assertTrue(isset($result['foo']));
  587. $this->assertTrue(isset($result['bar']));
  588. $this->assertEquals('bar', $result['foo']);
  589. $this->assertEquals('baz', $result['bar']);
  590. }
  591. public function testAutoJsonSerializationMayBeDisabled()
  592. {
  593. $this->request->setParam('format', 'json')
  594. ->setActionName('bar')
  595. ->setDispatched(true);
  596. $this->helper->setAutoJsonSerialization(false);
  597. $this->controller->dispatch('barAction');
  598. $headers = $this->response->getHeaders();
  599. $found = false;
  600. foreach ($headers as $header) {
  601. if ($header['name'] == 'Content-Type') {
  602. if ($header['value'] == 'application/json') {
  603. $found = true;
  604. }
  605. break;
  606. }
  607. }
  608. $this->assertTrue($found, 'JSON content type header not found');
  609. $body = $this->response->getBody();
  610. $this->assertTrue(empty($body), $body);
  611. }
  612. public function testCanAddOneOrMoreActionContexts()
  613. {
  614. $this->assertFalse($this->helper->hasActionContext('foo', 'json'));
  615. $this->helper->addActionContext('foo', 'json');
  616. $this->assertTrue($this->helper->hasActionContext('foo', 'json'));
  617. $this->assertFalse($this->helper->hasActionContext('baz', 'xml'));
  618. $this->assertFalse($this->helper->hasActionContext('baz', 'json'), var_export($this->controller->contexts, 1));
  619. $this->helper->addActionContext('baz', array('xml', 'json'));
  620. $this->assertTrue($this->helper->hasActionContext('baz', 'xml'));
  621. $this->assertTrue($this->helper->hasActionContext('baz', 'json'));
  622. }
  623. public function testCanOverwriteAnActionContext()
  624. {
  625. $this->assertTrue($this->helper->hasActionContext('foo', 'xml'));
  626. $this->helper->setActionContext('foo', 'json');
  627. $this->assertFalse($this->helper->hasActionContext('foo', 'xml'));
  628. $this->assertTrue($this->helper->hasActionContext('foo', 'json'));
  629. $this->helper->setActionContext('foo', array('xml', 'json'));
  630. $this->assertTrue($this->helper->hasActionContext('foo', 'json'));
  631. $this->assertTrue($this->helper->hasActionContext('foo', 'xml'));
  632. }
  633. public function testCanAddContextsForMultipleActions()
  634. {
  635. $this->assertFalse($this->helper->hasActionContext('foo', 'json'));
  636. $this->assertFalse($this->helper->hasActionContext('baz', 'json'));
  637. $this->assertFalse($this->helper->hasActionContext('baz', 'xml'));
  638. $this->helper->addActionContexts(array(
  639. 'foo' => 'json',
  640. 'baz' => array('json', 'xml'),
  641. ));
  642. $this->assertTrue($this->helper->hasActionContext('foo', 'json'));
  643. $this->assertTrue($this->helper->hasActionContext('baz', 'json'));
  644. $this->assertTrue($this->helper->hasActionContext('baz', 'xml'));
  645. }
  646. public function testCanOverwriteContextsForMultipleActions()
  647. {
  648. $this->assertTrue($this->helper->hasActionContext('foo', 'xml'));
  649. $this->assertTrue($this->helper->hasActionContext('bar', 'json'));
  650. $this->assertTrue($this->helper->hasActionContext('bar', 'xml'));
  651. $this->helper->setActionContexts(array(
  652. 'foo' => 'json',
  653. 'bar' => 'xml'
  654. ));
  655. $this->assertFalse($this->helper->hasActionContext('foo', 'xml'));
  656. $this->assertTrue($this->helper->hasActionContext('foo', 'json'));
  657. $this->assertFalse($this->helper->hasActionContext('bar', 'json'));
  658. $this->assertTrue($this->helper->hasActionContext('bar', 'xml'));
  659. }
  660. public function testCanRemoveOneOrMoreActionContexts()
  661. {
  662. $this->assertTrue($this->helper->hasActionContext('bar', 'json'));
  663. $this->assertTrue($this->helper->hasActionContext('bar', 'xml'));
  664. $this->helper->removeActionContext('bar', 'xml');
  665. $this->assertTrue($this->helper->hasActionContext('bar', 'json'));
  666. $this->assertFalse($this->helper->hasActionContext('bar', 'xml'));
  667. }
  668. public function testCanClearAllContextsForASingleAction()
  669. {
  670. $this->assertTrue($this->helper->hasActionContext('bar', 'json'));
  671. $this->assertTrue($this->helper->hasActionContext('bar', 'xml'));
  672. $this->helper->clearActionContexts('bar');
  673. $this->assertFalse($this->helper->hasActionContext('bar', 'json'));
  674. $this->assertFalse($this->helper->hasActionContext('bar', 'xml'));
  675. }
  676. public function testCanClearAllActionContexts()
  677. {
  678. $this->helper->clearActionContexts();
  679. $contexts = $this->helper->getActionContexts();
  680. $this->assertTrue(empty($contexts));
  681. }
  682. public function getOptions()
  683. {
  684. $options = array(
  685. 'contexts' => array('ajax' => array('suffix' => 'ajax', 'headers' => array('Content-Type' => 'text/x-html')), 'json' => array('suffix' => 'json', 'headers' => array('Content-Type' => 'application/json'), 'callbacks' => array('init' => 'initJsonCallback', 'post' => 'postJsonCallback'))),
  686. 'autoJsonSerialization' => false,
  687. 'suffix' => array('json' => array('suffix' => 'js', 'prependViewRendererSuffix' => false)),
  688. 'headers' => array('json' => array('Content-Type' => 'text/js')),
  689. 'callbacks' => array('json' => array('init' => 'htmlentities')),
  690. 'contextParam' => 'foobar',
  691. 'defaultContext' => 'json',
  692. 'autoDisableLayout' => false,
  693. );
  694. return $options;
  695. }
  696. public function checkOptionsAreSet()
  697. {
  698. $this->assertFalse($this->helper->getAutoJsonSerialization());
  699. $this->assertEquals('js', $this->helper->getSuffix('json'));
  700. $this->assertEquals('text/js', $this->helper->getHeader('json', 'Content-Type'));
  701. $this->assertEquals('htmlentities', $this->helper->getCallback('json', 'init'));
  702. $this->assertEquals('foobar', $this->helper->getContextParam());
  703. $this->assertEquals('json', $this->helper->getDefaultContext());
  704. $this->assertFalse($this->helper->getAutoDisableLayout());
  705. $this->assertTrue($this->helper->hasContext('ajax'));
  706. }
  707. public function testCanSetOptionsViaArray()
  708. {
  709. $this->helper->setOptions($this->getOptions());
  710. $this->checkOptionsAreSet();
  711. }
  712. public function testCanSetOptionsViaConfig()
  713. {
  714. $config = new Zend_Config($this->getOptions());
  715. $this->helper->setConfig($config);
  716. $this->checkOptionsAreSet();
  717. }
  718. public function testOptionsPassedToConstructorShouldSetInstanceState()
  719. {
  720. $this->helper = new Zend_Controller_Action_Helper_ContextSwitch($this->getOptions());
  721. $this->checkOptionsAreSet();
  722. }
  723. public function testConfigPassedToConstructorShouldSetInstanceState()
  724. {
  725. $config = new Zend_Config($this->getOptions());
  726. $this->helper = new Zend_Controller_Action_Helper_ContextSwitch($config);
  727. $this->checkOptionsAreSet();
  728. }
  729. /**
  730. * @group ZF-3279
  731. */
  732. public function testPostJsonContextDoesntThrowExceptionWhenGetVarsMethodsExists()
  733. {
  734. try {
  735. $this->helper->setAutoJsonSerialization(true);
  736. $this->helper->postJsonContext();
  737. } catch(Zend_Controller_Action_Exception $zcae) {
  738. $this->fail('Exception should be throw when view does not implement getVars() method');
  739. }
  740. }
  741. /**
  742. * @group ZF-3279
  743. */
  744. public function testPostJsonContextThrowsExceptionWhenGetVarsMethodsDoesntExist()
  745. {
  746. $view = new Zend_Controller_Action_Helper_ContextSwitchText_CustomView();
  747. $this->viewRenderer->setView($view);
  748. try {
  749. $this->helper->setAutoJsonSerialization(true);
  750. $this->helper->postJsonContext();
  751. $this->fail('Exception should be throw when view does not implement getVars() method');
  752. } catch(Zend_Controller_Action_Exception $zcae) {
  753. }
  754. }
  755. /**
  756. * @group ZF-4866
  757. */
  758. public function testForwardingShouldNotUseContextSuffixIfNewActionDoesNotDetectValidContext()
  759. {
  760. $this->request->setParam('format', 'xml')
  761. ->setActionName('foo');
  762. $this->helper->setActionContext('bar', 'json');
  763. $this->helper->initContext();
  764. $this->assertEquals('xml', $this->helper->getCurrentContext());
  765. $this->request->setActionName('bar');
  766. $this->helper->init();
  767. $this->helper->initContext();
  768. $suffix = $this->viewRenderer->getViewSuffix();
  769. $this->assertNotContains('xml', $suffix, $suffix);
  770. }
  771. /**
  772. * @group ZF-4866
  773. */
  774. public function testForwardingShouldNotPrependMultipleViewSuffixesForCustomContexts()
  775. {
  776. $this->helper->addContext('foo', array('suffix' => 'foo'));
  777. $this->helper->setActionContext('foo', 'foo');
  778. $this->helper->setActionContext('bar', 'foo');
  779. $this->request->setParam('format', 'foo')
  780. ->setActionName('foo');
  781. $this->helper->initContext();
  782. $this->assertEquals('foo', $this->helper->getCurrentContext());
  783. $suffix = $this->viewRenderer->getViewSuffix();
  784. $this->assertContains('foo', $suffix, $suffix);
  785. $this->request->setActionName('bar');
  786. $this->helper->init();
  787. $this->helper->initContext();
  788. $this->assertEquals('foo', $this->helper->getCurrentContext());
  789. $suffix = $this->viewRenderer->getViewSuffix();
  790. $this->assertContains('foo', $suffix, $suffix);
  791. $this->assertNotContains('foo.foo', $suffix, $suffix);
  792. }
  793. /**
  794. * @group ZF-11793
  795. */
  796. public function testGetActionContextsReturnsFullListWhenArgumentIsNull()
  797. {
  798. $expected = array(
  799. 'foo' => array('xml'),
  800. 'bar' => array('xml', 'json'),
  801. 'all' => array('json','xml')
  802. );
  803. $actual = $this->helper->getActionContexts(null);
  804. $this->assertEquals($expected, $actual);
  805. }
  806. }
  807. class Zend_Controller_Action_Helper_ContextSwitchTestController extends Zend_Controller_Action
  808. {
  809. public $contextSwitch;
  810. /*
  811. public $contexts = array(
  812. 'foo' => array('xml'), // only XML context
  813. 'bar' => array('xml', 'json'), // only XML and JSON contexts
  814. 'baz' => array(), // no contexts
  815. 'all' => true, // all contexts
  816. );
  817. */
  818. public function setupContexts()
  819. {
  820. $this->_helper->contextSwitch()->setActionContexts(array(
  821. 'foo' => 'xml',
  822. 'bar' => array('xml', 'json'),
  823. 'all' => true
  824. ));
  825. }
  826. public function postDispatch()
  827. {
  828. $this->_helper->viewRenderer->setNoRender();
  829. }
  830. public function barAction()
  831. {
  832. $this->_helper->contextSwitch->initContext();
  833. $this->view->foo = 'bar';
  834. $this->view->bar = 'baz';
  835. }
  836. }
  837. class Zend_Controller_Action_Helper_ContextSwitchTest_LayoutOverride extends Zend_Layout
  838. {
  839. public static function resetMvcInstance()
  840. {
  841. self::$_mvcInstance = null;
  842. }
  843. }
  844. class Zend_Controller_Action_Helper_ContextSwitchText_CustomView implements Zend_View_Interface
  845. {
  846. public function getEngine()
  847. {}
  848. public function setScriptPath($path)
  849. {}
  850. public function getScriptPaths()
  851. {}
  852. public function setBasePath($path, $classPrefix = 'Zend_View')
  853. {}
  854. public function addBasePath($path, $classPrefix = 'Zend_View')
  855. {}
  856. public function __set($key, $val)
  857. {}
  858. public function __isset($key)
  859. {}
  860. public function __unset($key)
  861. {}
  862. public function assign($spec, $value = null)
  863. {}
  864. public function clearVars()
  865. {}
  866. public function render($name)
  867. {}
  868. }
  869. // Call Zend_Controller_Action_Helper_ContextSwitchTest::main() if this source file is executed directly.
  870. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Action_Helper_ContextSwitchTest::main") {
  871. Zend_Controller_Action_Helper_ContextSwitchTest::main();
  872. }