public function __destruct()
 {
     // Shift off anything else that's on the stack.  This can happen if something throws
     // an exception that causes a premature TestSession::__destruct() call
     while (Controller::has_curr() && Controller::curr() !== $this->controller) {
         Controller::curr()->popCurrent();
     }
     if (Controller::has_curr()) {
         $this->controller->popCurrent();
     }
 }
 /**
  * @param array $record
  * @return bool
  */
 protected function write(array $record)
 {
     ini_set('display_errors', 0);
     // TODO: This coupling isn't ideal
     // See https://github.com/silverstripe/silverstripe-framework/issues/4484
     if (Controller::has_curr()) {
         $response = Controller::curr()->getResponse();
     } else {
         $response = new HTTPResponse();
     }
     // If headers have been sent then these won't be used, and may throw errors that we wont' want to see.
     if (!headers_sent()) {
         $response->setStatusCode($this->statusCode);
         $response->addHeader("Content-Type", $this->contentType);
     } else {
         // To supress errors aboot errors
         $response->setStatusCode(200);
     }
     $response->setBody($record['formatted']);
     $response->output();
     return false === $this->bubble;
 }
 /**
  * Checks if we're on a controller where we should filter. ie. Are we loading the SiteTree?
  *
  * @return bool
  */
 public function showingCMSTree()
 {
     if (!Controller::has_curr()) {
         return false;
     }
     $controller = Controller::curr();
     return $controller instanceof LeftAndMain && in_array($controller->getAction(), array("treeview", "listview", "getsubtree"));
 }
 public function tearDown()
 {
     // Preserve memory settings
     ini_set('memory_limit', $this->originalMemoryLimit ? $this->originalMemoryLimit : -1);
     // Restore email configuration
     $this->mailer = null;
     // Restore password validation
     if ($this->originalMemberPasswordValidator) {
         Member::set_password_validator($this->originalMemberPasswordValidator);
     }
     // Restore requirements
     if ($this->originalRequirements) {
         Requirements::set_backend($this->originalRequirements);
     }
     // Mark test as no longer being run - we use originalIsRunningTest to allow for nested SapphireTest calls
     self::$is_running_test = $this->originalIsRunningTest;
     $this->originalIsRunningTest = null;
     // Reset mocked datetime
     DBDatetime::clear_mock_now();
     // Stop the redirection that might have been requested in the test.
     // Note: Ideally a clean Controller should be created for each test.
     // Now all tests executed in a batch share the same controller.
     $controller = Controller::has_curr() ? Controller::curr() : null;
     if ($controller && ($response = $controller->getResponse()) && $response->getHeader('Location')) {
         $response->setStatusCode(200);
         $response->removeHeader('Location');
     }
     Versioned::set_reading_mode($this->originalReadingMode);
     //unnest injector / config now that tests are over
     Injector::unnest();
     Config::unnest();
 }
 /**
  * Checks if the current HTTP-Request is an "Ajax-Request" by checking for a custom header set by
  * jQuery or whether a manually set request-parameter 'ajax' is present.
  *
  * @return bool
  */
 public static function is_ajax()
 {
     if (Controller::has_curr()) {
         return Controller::curr()->getRequest()->isAjax();
     } else {
         return isset($_REQUEST['ajax']) || isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == "XMLHttpRequest";
     }
 }
Ejemplo n.º 6
0
 protected static function current_session()
 {
     if (Controller::has_curr()) {
         return Controller::curr()->getSession();
     } else {
         if (!self::$default_session) {
             self::$default_session = Injector::inst()->create('SilverStripe\\Control\\Session', isset($_SESSION) ? $_SESSION : array());
         }
         return self::$default_session;
     }
 }
Ejemplo n.º 7
0
 /**
  * Get all menu items that the passed member can view.
  * Defaults to {@link Member::currentUser()}.
  *
  * @param Member $member
  * @return array
  */
 public static function get_viewable_menu_items($member = null)
 {
     if (!$member && $member !== FALSE) {
         $member = Member::currentUser();
     }
     $viewableMenuItems = array();
     $allMenuItems = self::get_menu_items();
     if ($allMenuItems) {
         foreach ($allMenuItems as $code => $menuItem) {
             // exclude all items which have a controller to perform permission
             // checks on
             if ($menuItem->controller) {
                 $controllerObj = singleton($menuItem->controller);
                 if (Controller::has_curr()) {
                     // Necessary for canView() to have request data available,
                     // e.g. to check permissions against LeftAndMain->currentPage()
                     $controllerObj->setRequest(Controller::curr()->getRequest());
                     if (!$controllerObj->canView($member)) {
                         continue;
                     }
                 }
             }
             $viewableMenuItems[$code] = $menuItem;
         }
     }
     return $viewableMenuItems;
 }