Exemplo n.º 1
0
 public function testSetLocale()
 {
     Session::start();
     $currentLocale = I18n::getCurrentLocale();
     $_REQUEST['_locale'] = 'en';
     I18n::staticReset();
     $this->assertEquals('en', I18n::getCurrentLocale(), 'Unable to set locale via request');
     $_REQUEST['_locale'] = $currentLocale;
     I18n::staticReset();
     unset($_REQUEST['_locale']);
     I18n::setLocale('en');
     $this->assertEquals('en', I18n::getCurrentLocale(), 'Unable to set locale via setLocale()');
     I18n::setLocale($currentLocale);
     $this->assertEquals($currentLocale, I18n::getCurrentLocale(), 'Unable to set locale via setLocale()');
     I18n::setLocale('non-existing');
     $this->assertEquals($currentLocale, I18n::getCurrentLocale(), 'Unable to restore default locale via setLocale()');
     Session::end();
     I18n::clearCache(true);
     Cookies::set('locale', 'en');
     Session::start();
     I18n::staticReset();
     $this->assertEquals('en', I18n::getCurrentLocale(), 'Unable to set locale via cookie');
     $_REQUEST['_locale'] = $currentLocale;
     I18n::staticReset();
     unset($_REQUEST['_locale']);
     Session::set('current_locale', 'en');
     I18n::staticReset();
     $this->assertEquals('en', I18n::getCurrentLocale(), 'Unable to set locale via session');
     Session::set('current_locale', null);
     I18n::staticReset();
     $this->assertEquals($currentLocale, I18n::getCurrentLocale(), 'Unable to restore default locale');
     Session::end();
 }
Exemplo n.º 2
0
 public static function dispatch($uri = null)
 {
     try {
         static::$router === null and static::$router = static::$di->getShared('router');
         $router = static::$router;
         // @codeCoverageIgnoreStart
         if (!$uri && $router->_sitePathLength && $router->_uriSource == self::URI_SOURCE_GET_URL) {
             list($uri) = explode('?', $_SERVER['REQUEST_URI']);
             $uri = str_replace(basename($_SERVER['SCRIPT_FILENAME']), '', $uri);
             if (substr($uri, 0, $router->_sitePathLength) == $router->_sitePathPrefix) {
                 $uri = substr($uri, $router->_sitePathLength);
             }
         }
         // @codeCoverageIgnoreEnd
         Events::fire('router:before_dispatch', $router, ['uri' => $uri]);
         $router->handle($uri);
         $route = $router->getMatchedRoute() or static::throw404Exception();
         static::$disableSession or Session::start();
         static::$disableCsrfCheck or static::checkCsrfToken();
         if (($controllerClass = $router->getControllerName()) instanceof Closure) {
             $response = $controllerClass();
             if (!$response instanceof Response) {
                 /* @var Response $realResponse */
                 $realResponse = static::$di->getShared('response');
                 $realResponse->setContent($response);
                 $response = $realResponse;
             }
         } else {
             /* @var Controller $controller */
             $controller = new $controllerClass();
             method_exists($controller, 'initialize') and $controller->initialize();
             method_exists($controller, $method = $router->getActionName()) or static::throw404Exception();
             $controller->{$method}();
             $response = $controller->response;
         }
         Events::fire('router:after_dispatch', $router, ['response' => $response]);
         Session::end();
         return $response;
     } catch (HttpException $e) {
         Log::exception($e);
         return static::$runningUnitTest ? $e : $e->toResponse();
     }
 }
Exemplo n.º 3
0
 public function testSessionCsrfToken()
 {
     Config::set('session.default', 'native');
     Session::register($this->di);
     Session::start();
     $this->assertNotEmpty($csrf = Session::generateCsrfToken(), 'Unable to generate CSRF token');
     $this->assertEquals($csrf, Session::getCsrfToken(), 'Unable to check CSRF token');
     Session::clear();
     $this->assertNotEmpty($newCsrf = Session::getCsrfToken(), 'Unable to regenerate CSRF token');
     $this->assertNotEquals($csrf, $newCsrf, 'Unable to regenerate unique CSRF token');
     Session::end();
 }