Exemple #1
0
 public function testMissingViewScriptDoesDoubleRender()
 {
     \Zend\Controller\Action\HelperBroker\HelperBroker::getStack()->offsetSet(-91, new \Zend\Controller\Action\Helper\ViewRenderer());
     // go to the test controller for this funcitonal test
     $this->dispatch('/zend-layout-functional-test-test/missing-view-script');
     $this->assertEquals(trim($this->response->getBody()), "[DEFAULT_LAYOUT_START]\n[DEFAULT_LAYOUT_START]\n[DEFAULT_LAYOUT_END]\n(ErrorController::errorAction output)[DEFAULT_LAYOUT_END]");
 }
Exemple #2
0
 /**
  * Create JSON response
  *
  * Encodes and returns data to JSON. Content-Type header set to
  * 'application/json', and disables layouts and viewRenderer (if being
  * used).
  *
  * @param  mixed   $data
  * @param  boolean $keepLayouts
  * @param  boolean|array $keepLayouts
  * NOTE:   if boolean, establish $keepLayouts to true|false
  *         if array, admit params for Zend_Json::encode as enableJsonExprFinder=>true|false
  *         if $keepLayouts and parmas for Zend_Json::encode are required
  *         then, the array can contains a 'keepLayout'=>true|false
  *         that will not be passed to Zend_Json::encode method but will be passed
  *         to Zend_View_Helper_Json
  * @throws \Zend\Controller\Action\Helper\JSON
  * @return string
  */
 public function encodeJson($data, $keepLayouts = false)
 {
     $jsonHelper = new \Zend\View\Helper\JSON();
     $data = $jsonHelper->direct($data, $keepLayouts);
     if (!$keepLayouts) {
         \Zend\Controller\Action\HelperBroker\HelperBroker::getStaticHelper('viewRenderer')->setNoRender(true);
     }
     return $data;
 }
Exemple #3
0
 public function setUp()
 {
     if (isset($this->error)) {
         unset($this->error);
     }
     $front = Controller\Front::getInstance();
     $front->resetInstance();
     \Zend\Controller\Action\HelperBroker\HelperBroker::removeHelper('viewRenderer');
     $this->_dispatcher = new \Zend\Controller\Dispatcher\Standard();
     $this->_dispatcher->setControllerDirectory(array('default' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files', 'admin' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'Admin'));
 }
Exemple #4
0
 /**
  * Sets up the fixture, for example, open a network connection.
  * This method is called before a test is executed.
  *
  * @return void
  */
 public function setUp()
 {
     \Zend\Controller\Front::getInstance()->resetInstance();
     if (HelperBroker\HelperBroker::hasHelper('Layout')) {
         HelperBroker\HelperBroker::removeHelper('Layout');
     }
     if (HelperBroker\HelperBroker::hasHelper('viewRenderer')) {
         HelperBroker\HelperBroker::removeHelper('viewRenderer');
     }
     \Zend\Layout\Layout::resetMvcInstance();
 }
Exemple #5
0
 /**
  * Sets up the fixture, for example, open a network connection.
  * This method is called before a test is executed.
  *
  * @return void
  */
 public function setUp()
 {
     \Zend\Layout\Layout::resetMvcInstance();
     $this->response = new \Zend\Controller\Response\HTTP();
     $this->response->headersSentThrowsException = false;
     $front = \Zend\Controller\Front::getInstance();
     $front->resetInstance();
     $front->setResponse($this->response);
     $this->viewRenderer = new \Zend\Controller\Action\Helper\ViewRenderer();
     \Zend\Controller\Action\HelperBroker\HelperBroker::addHelper($this->viewRenderer);
     $this->helper = new \Zend\Controller\Action\Helper\JSON();
     $this->helper->suppressExit = true;
 }
Exemple #6
0
 /**
  * Sets up the fixture, for example, open a network connection.
  * This method is called before a test is executed.
  *
  * @return void
  */
 public function setUp()
 {
     $this->markTestSkipped('Skipped until DOJO is converted to Zend\\Dojo');
     \Zend\Layout\Layout::resetMvcInstance();
     HelperBroker\HelperBroker::resetHelpers();
     HelperBroker\HelperBroker::setPluginLoader(null);
     $this->request = new \Zend\Controller\Request\HTTP();
     $this->response = new \Zend\Controller\Response\Cli();
     $this->front = \Zend\Controller\Front::getInstance();
     $this->front->resetInstance();
     $this->front->setRequest($this->request)->setResponse($this->response);
     $this->viewRenderer = HelperBroker\HelperBroker::getStaticHelper('viewRenderer');
     $this->layout = Layout\Layout::startMvc();
 }
Exemple #7
0
 /**
  * Set up redirector
  *
  * Creates request, response, and action controller objects; sets action
  * controller in redirector, and sets exit to false.
  *
  * Also resets the front controller instance.
  */
 public function setUp()
 {
     $front = Controller\Front::getInstance();
     $front->resetInstance();
     \Zend\Controller\Action\HelperBroker\HelperBroker::removeHelper('viewRenderer');
     $this->redirector = new \Zend\Controller\Action\Helper\Redirector();
     $this->router = $front->getRouter();
     $this->request = new \Zend\Controller\Request\HTTP();
     $this->response = new \Zend\Controller\Response\HTTP();
     $this->controller = new TestController($this->request, $this->response, array());
     // Add default routes
     $this->router->addDefaultRoutes();
     // do this so setting headers does not throw exceptions
     $this->response->headersSentThrowsException = false;
     $this->redirector->setExit(false)->setActionController($this->controller);
     $this->_server = $_SERVER;
 }
Exemple #8
0
 /**
  * Sets up the fixture, for example, open a network connection.
  * This method is called before a test is executed.
  *
  * @return void
  */
 public function setUp()
 {
     \Zend\Layout\Layout::resetMvcInstance();
     HelperBroker\HelperBroker::resetHelpers();
     $this->front = Controller\Front::getInstance();
     $this->front->resetInstance();
     $this->front->addModuleDirectory(dirname(__FILE__) . '/../../_files/modules');
     $this->layout = Layout\Layout::startMvc();
     $this->helper = new Helper\ContextSwitch();
     HelperBroker\HelperBroker::addHelper($this->helper);
     $this->request = new \Zend\Controller\Request\HTTP();
     $this->response = new \Zend\Controller\Response\Cli();
     $this->front->setRequest($this->request)->setResponse($this->response)->addControllerDirectory(dirname(__FILE__));
     $this->view = new \Zend\View\View();
     $this->viewRenderer = HelperBroker\HelperBroker::getStaticHelper('viewRenderer');
     $this->viewRenderer->setView($this->view);
     $this->controller = new ContextSwitchTestController($this->request, $this->response, array());
     $this->controller->setupContexts();
     $this->helper->setActionController($this->controller);
 }
Exemple #9
0
 /**
  * Sets up the fixture, for example, open a network connection.
  * This method is called before a test is executed.
  *
  * @return void
  */
 public function setUp()
 {
     if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
         unset($_SERVER['HTTP_X_REQUESTED_WITH']);
     }
     \Zend\Layout\Layout::resetMvcInstance();
     HelperBroker\HelperBroker::resetHelpers();
     HelperBroker\HelperBroker::addPrefix('Zend\\Controller\\Action\\Helper');
     $this->front = \Zend\Controller\Front::getInstance();
     $this->front->resetInstance();
     $this->front->addModuleDirectory(dirname(__FILE__) . '/../../_files/modules');
     $this->layout = Layout\Layout::startMvc();
     $this->helper = new \Zend\Controller\Action\Helper\AjaxContext();
     $this->request = new \Zend\Controller\Request\HTTP();
     $this->response = new \Zend\Controller\Response\Cli();
     $this->front->setRequest($this->request)->setResponse($this->response);
     $this->view = new \Zend\View\View();
     $this->view->addHelperPath(dirname(__FILE__) . '/../../../../../library/Zend/View/Helper/');
     $this->viewRenderer = HelperBroker\HelperBroker::getStaticHelper('viewRenderer');
     $this->viewRenderer->setView($this->view);
     $this->controller = new AjaxContextTestController($this->request, $this->response, array());
     $this->helper->setActionController($this->controller);
 }
Exemple #10
0
 /**
  * @group ZF-4704
  */
 public function testBrokerShouldAcceptCustomPluginLoaderInstance()
 {
     $loader = HelperBroker\HelperBroker::getPluginLoader();
     $custom = new \Zend\Loader\PluginLoader\PluginLoader();
     HelperBroker\HelperBroker::setPluginLoader($custom);
     $test = HelperBroker\HelperBroker::getPluginLoader();
     $this->assertNotSame($loader, $test);
     $this->assertSame($custom, $test);
 }
Exemple #11
0
 /**
  * Retrieve rendered contents of a controller action
  *
  * If the action results in a forward or redirect, returns empty string.
  *
  * @param  string $action
  * @param  string $controller
  * @param  string $module Defaults to default module
  * @param  array $params
  * @return string
  */
 public function direct($action = null, $controller = null, $module = null, array $params = array())
 {
     if ($action == null || $controller == null) {
         throw new \InvalidArgumentException('Action: missing argument. $action and $controller are required in action($action, $controller, $module = null, array $params = array())');
     }
     $this->resetObjects();
     if (null === $module) {
         $module = $this->defaultModule;
     }
     // clone the view object to prevent over-writing of view variables
     $viewRendererObj = HelperBroker\HelperBroker::getStaticHelper('viewRenderer');
     HelperBroker\HelperBroker::addHelper(clone $viewRendererObj);
     $this->request->setParams($params)->setModuleName($module)->setControllerName($controller)->setActionName($action)->setDispatched(true);
     $this->dispatcher->dispatch($this->request, $this->response);
     // reset the viewRenderer object to it's original state
     HelperBroker\HelperBroker::addHelper($viewRendererObj);
     if (!$this->request->isDispatched() || $this->response->isRedirect()) {
         // forwards and redirects render nothing
         return '';
     }
     $return = $this->response->getBody();
     $this->resetObjects();
     return $return;
 }
Exemple #12
0
 /**
  * Get current view object
  *
  * If no view object currently set, retrieves it from the ViewRenderer.
  *
  * @todo Set inflector from view renderer at same time
  * @return \Zend\View\ViewInterface
  */
 public function getView()
 {
     if (null === $this->_view) {
         $viewRenderer = HelperBroker\HelperBroker::getStaticHelper('viewRenderer');
         if (null === $viewRenderer->view) {
             $viewRenderer->initView();
         }
         $this->setView($viewRenderer->view);
     }
     return $this->_view;
 }
Exemple #13
0
 public function testResettingMvcInstanceUnregistersHelperAndPlugin()
 {
     $this->testGetMvcInstanceReturnsLayoutInstanceWhenStartMvcHasBeenCalled();
     Layout\Layout::resetMvcInstance();
     $front = Controller\Front::getInstance();
     $this->assertFalse($front->hasPlugin('Zend_Layout_Controller_Plugin_Layout'), 'Plugin not unregistered');
     $this->assertFalse(HelperBroker\HelperBroker::hasHelper('Layout'), 'Helper not unregistered');
 }
Exemple #14
0
 public function testSpecifyingAnotherUrlHelperToGenerateHrefs()
 {
     $newHelper = new \ZendTest\Navigation\TestAsset\UrlHelper();
     Page\Mvc::setUrlHelper($newHelper);
     $page = new Page\Mvc();
     $expected = \ZendTest\Navigation\TestAsset\UrlHelper::RETURN_URL;
     $actual = $page->getHref();
     $old = \Zend\Controller\Action\HelperBroker\HelperBroker::getStaticHelper('URL');
     Page\Mvc::setUrlHelper($old);
     $this->assertEquals($expected, $actual);
 }
Exemple #15
0
 /**
  * Returns href for this page
  *
  * This method uses {@link Zend_Controller_Action_Helper_Url} to assemble
  * the href based on the page's properties.
  *
  * @return string  page href
  */
 public function getHref()
 {
     if ($this->_hrefCache) {
         return $this->_hrefCache;
     }
     if (null === self::$_urlHelper) {
         self::$_urlHelper = \Zend\Controller\Action\HelperBroker\HelperBroker::getStaticHelper('URL');
     }
     $params = $this->getParams();
     if (($param = $this->getModule()) != null) {
         $params['module'] = $param;
     }
     if (($param = $this->getController()) != null) {
         $params['controller'] = $param;
     }
     if (($param = $this->getAction()) != null) {
         $params['action'] = $param;
     }
     $url = self::$_urlHelper->__invoke($params, $this->getRoute(), $this->getResetParams());
     //var_dump($url);
     return $this->_hrefCache = $url;
 }
Exemple #16
0
 /**
  * Future ViewRenderer State issues should be included in this test.
  *
  * @issue ZF-2846
  */
 public function testActionReturnsViewRendererToOriginalState()
 {
     /* Setup the VR as if we were inside an action controller */
     $viewRenderer = new \Zend\Controller\Action\Helper\ViewRenderer();
     $viewRenderer->init();
     HelperBroker\HelperBroker::addHelper($viewRenderer);
     // make sure noRender is false
     $this->assertFalse($viewRenderer->getNoRender());
     $value = $this->helper->direct('bar', 'action-foo');
     $viewRendererPostAction = HelperBroker\HelperBroker::getStaticHelper('viewRenderer');
     // ViewRenderer noRender should still be false
     $this->assertFalse($viewRendererPostAction->getNoRender());
     $this->assertSame($viewRenderer, $viewRendererPostAction);
 }
Exemple #17
0
 public function testViewRendererHelperNotRegisteredIfNoViewRendererSet()
 {
     $this->assertFalse(HelperBroker\HelperBroker::hasHelper('viewRenderer'));
     $this->_controller->setParam('noViewRenderer', true);
     $request = new Request\HTTP('http://example.com/index/index');
     $this->_controller->setResponse(new Response\Cli());
     $response = $this->_controller->dispatch($request);
     $this->assertFalse(HelperBroker\HelperBroker::hasHelper('viewRenderer'));
 }
Exemple #18
0
 /**
  * Dispatch the requested action
  *
  * @param string $action Method name of action
  * @return void
  */
 public function dispatch($action)
 {
     // Notify helpers of action preDispatch state
     $this->_helper->notifyPreDispatch();
     $this->preDispatch();
     if ($this->getRequest()->isDispatched()) {
         if (null === $this->_classMethods) {
             $this->_classMethods = get_class_methods($this);
         }
         // preDispatch() didn't change the action, so we can continue
         if ($this->getInvokeArg('useCaseSensitiveActions') || in_array($action, $this->_classMethods)) {
             if ($this->getInvokeArg('useCaseSensitiveActions')) {
                 trigger_error('Using case sensitive actions without word separators is deprecated; please do not rely on this "feature"');
             }
             $this->{$action}();
         } else {
             $this->__call($action, array());
         }
         $this->postDispatch();
     }
     // whats actually important here is that this action controller is
     // shutting down, regardless of dispatching; notify the helpers of this
     // state
     $this->_helper->notifyPostDispatch();
 }
Exemple #19
0
 /**
  * Retrieves the view instance.  If none registered, attempts to pull f
  * rom ViewRenderer.
  *
  * @return \Zend\View\ViewInterface|null
  */
 public function getView()
 {
     if ($this->_view === null) {
         $viewRenderer = \Zend\Controller\Action\HelperBroker\HelperBroker::getStaticHelper('viewRenderer');
         if ($viewRenderer->view === null) {
             $viewRenderer->initView();
         }
         $this->_view = $viewRenderer->view;
     }
     return $this->_view;
 }
Exemple #20
0
 /**
  * Dispatch an HTTP request to a controller/action.
  *
  * @param \Zend\Controller\Request\AbstractRequest|null $request
  * @param \Zend\Controller\Response\AbstractResponse|null $response
  * @return void|\Zend\Controller\Response\AbstractResponse Returns response object if returnResponse() is true
  */
 public function dispatch(Request\AbstractRequest $request = null, Response\AbstractResponse $response = null)
 {
     if (!$this->getParam('noErrorHandler') && !$this->_plugins->hasPlugin('\\Zend\\Controller\\Plugin\\ErrorHandler')) {
         // Register with stack index of 100
         $this->_plugins->registerPlugin(new Plugin\ErrorHandler(), 100);
     }
     if (!$this->getParam('noViewRenderer') && !Action\HelperBroker\HelperBroker::hasHelper('viewRenderer')) {
         Action\HelperBroker\HelperBroker::getStack()->offsetSet(-80, new Action\Helper\ViewRenderer());
     }
     /**
      * Instantiate default request object (HTTP version) if none provided
      */
     if (null !== $request) {
         $this->setRequest($request);
     } elseif (null === $request && null === ($request = $this->getRequest())) {
         $request = new Request\HTTP();
         $this->setRequest($request);
     }
     /**
      * Set base URL of request object, if available
      */
     if (is_callable(array($this->_request, 'setBaseUrl'))) {
         if (null !== $this->_baseUrl) {
             $this->_request->setBaseUrl($this->_baseUrl);
         }
     }
     /**
      * Instantiate default response object (HTTP version) if none provided
      */
     if (null !== $response) {
         $this->setResponse($response);
     } elseif (null === $this->_response && null === ($this->_response = $this->getResponse())) {
         $response = new Response\HTTP();
         $this->setResponse($response);
     }
     /**
      * Register request and response objects with plugin broker
      */
     $this->_plugins->setRequest($this->_request)->setResponse($this->_response);
     /**
      * Initialize router
      */
     $router = $this->getRouter();
     $router->setParams($this->getParams());
     /**
      * Initialize dispatcher
      */
     $dispatcher = $this->getDispatcher();
     $dispatcher->setParams($this->getParams())->setResponse($this->_response);
     // Begin dispatch
     try {
         /**
          * Route request to controller/action, if a router is provided
          */
         /**
          * Notify plugins of router startup
          */
         $this->_plugins->routeStartup($this->_request);
         try {
             $router->route($this->_request);
         } catch (\Exception $e) {
             if ($this->throwExceptions()) {
                 throw $e;
             }
             $this->_response->setException($e);
         }
         /**
          * Notify plugins of router completion
          */
         $this->_plugins->routeShutdown($this->_request);
         /**
          * Notify plugins of dispatch loop startup
          */
         $this->_plugins->dispatchLoopStartup($this->_request);
         /**
          *  Attempt to dispatch the controller/action. If the $this->_request
          *  indicates that it needs to be dispatched, move to the next
          *  action in the request.
          */
         do {
             $this->_request->setDispatched(true);
             /**
              * Notify plugins of dispatch startup
              */
             $this->_plugins->preDispatch($this->_request);
             /**
              * Skip requested action if preDispatch() has reset it
              */
             if (!$this->_request->isDispatched()) {
                 continue;
             }
             /**
              * Dispatch request
              */
             try {
                 $dispatcher->dispatch($this->_request, $this->_response);
             } catch (\Exception $e) {
                 if ($this->throwExceptions()) {
                     throw $e;
                 }
                 $this->_response->setException($e);
             }
             /**
              * Notify plugins of dispatch completion
              */
             $this->_plugins->postDispatch($this->_request);
         } while (!$this->_request->isDispatched());
     } catch (\Exception $e) {
         if ($this->throwExceptions()) {
             throw $e;
         }
         $this->_response->setException($e);
     }
     /**
      * Notify plugins of dispatch loop completion
      */
     try {
         $this->_plugins->dispatchLoopShutdown();
     } catch (\Exception $e) {
         if ($this->throwExceptions()) {
             throw $e;
         }
         $this->_response->setException($e);
     }
     if ($this->returnResponse()) {
         return $this->_response;
     }
     $this->_response->sendResponse();
 }
 /**
  * Encode data to JSON
  *
  * @param  mixed $data
  * @param  bool  $keepLayouts
  * @throws \Zend\Controller\Action\Exception
  * @return string
  */
 public function encodeJson($data, $keepLayouts = false)
 {
     if ($this->validateData($data)) {
         return HelperBroker\HelperBroker::getStaticHelper('Json')->encodeJson($data, $keepLayouts);
     }
     throw new \Zend\Controller\Action\Exception('Invalid data passed for autocompletion');
 }
Exemple #22
0
 public function testRenderUsingViewRenderer()
 {
     HelperBroker\HelperBroker::addHelper(new Helper\ViewRenderer());
     $request = new Request\HTTP();
     $request->setControllerName('view')->setActionName('script');
     $response = new Response\Cli();
     Controller\Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
     $controller = new \ViewController($request, $response);
     $controller->scriptAction();
     $this->assertContains('Inside custom/renderScript.php', $response->getBody());
 }
Exemple #23
0
 /**
  * Retrieve ViewRenderer
  *
  * @return \Zend\Controller\Action\Helper\ViewRenderer Provides a fluent interface
  */
 protected function _getViewRenderer()
 {
     if (null === $this->_viewRenderer) {
         $this->_viewRenderer = HelperBroker\HelperBroker::getStaticHelper('viewRenderer');
     }
     return $this->_viewRenderer;
 }
Exemple #24
0
 /**
  * Tears down the fixture, for example, close a network connection.
  * This method is called after a test is executed.
  *
  * @access protected
  */
 protected function tearDown()
 {
     HelperBroker\HelperBroker::resetHelpers();
 }
Exemple #25
0
 public function testPostDispatchRendersLayout()
 {
     $front = Controller\Front::getInstance();
     $request = new Request\Simple();
     $response = new Response\Cli();
     $request->setDispatched(true);
     $response->setBody('Application content');
     $front->setRequest($request)->setResponse($response);
     $layout = Layout\Layout::startMvc();
     $layout->setLayoutPath(dirname(__FILE__) . '/_files/layouts')->setLayout('plugin.phtml')->disableInflector();
     $helper = HelperBroker\HelperBroker::getStaticHelper('layout');
     $plugin = $front->getPlugin('Zend\\Layout\\Controller\\Plugin\\Layout');
     $plugin->setResponse($response);
     $helper->postDispatch();
     $plugin->postDispatch($request);
     $body = $response->getBody();
     $this->assertContains('Application content', $body, $body);
     $this->assertContains('Site Layout', $body, $body);
 }
Exemple #26
0
 public function testGetsViewFromViewRenderer()
 {
     $viewRenderer = HelperBroker\HelperBroker::getStaticHelper('viewRenderer');
     $viewRenderer->setView(new View\View());
     $this->assertType('Zend\\View\\ViewInterface', $this->_paginator->getView());
 }