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();
 }
 public function testProcessOnlyIncludesRequirementsOnce()
 {
     $template = new SSViewer(array('SSViewerTestProcess'));
     $basePath = dirname($this->getCurrentRelativePath()) . '/forms';
     $backend = Injector::inst()->create('SilverStripe\\View\\Requirements_Backend');
     $backend->setCombinedFilesEnabled(false);
     $backend->combineFiles('RequirementsTest_ab.css', array($basePath . '/RequirementsTest_a.css', $basePath . '/RequirementsTest_b.css'));
     Requirements::set_backend($backend);
     $this->assertEquals(1, substr_count($template->process(array()), "a.css"));
     $this->assertEquals(1, substr_count($template->process(array()), "b.css"));
     // if we disable the requirements then we should get nothing
     $template->includeRequirements(false);
     $this->assertEquals(0, substr_count($template->process(array()), "a.css"));
     $this->assertEquals(0, substr_count($template->process(array()), "b.css"));
 }
 /**
  * Test a URL request, returning a response object. This method is the counterpart of
  * Director::direct() that is used in functional testing. It will execute the URL given, and
  * return the result as an HTTPResponse object.
  *
  * @uses Controller::handleRequest() Handles the page logic for a Director::direct() call.
  *
  * @param string $url The URL to visit.
  * @param array $postVars The $_POST & $_FILES variables.
  * @param array|Session $session The {@link Session} object representing the current session.
  * By passing the same object to multiple  calls of Director::test(), you can simulate a persisted
  * session.
  * @param string $httpMethod The HTTP method, such as GET or POST.  It will default to POST if
  * postVars is set, GET otherwise. Overwritten by $postVars['_method'] if present.
  * @param string $body The HTTP body.
  * @param array $headers HTTP headers with key-value pairs.
  * @param array|Cookie_Backend $cookies to populate $_COOKIE.
  * @param HTTPRequest $request The {@see SS_HTTP_Request} object generated as a part of this request.
  *
  * @return HTTPResponse
  *
  * @throws HTTPResponse_Exception
  */
 public static function test($url, $postVars = null, $session = array(), $httpMethod = null, $body = null, $headers = array(), $cookies = array(), &$request = null)
 {
     Config::nest();
     Injector::nest();
     // These are needed so that calling Director::test() does not muck with whoever is calling it.
     // Really, it's some inappropriate coupling and should be resolved by making less use of statics.
     $oldReadingMode = Versioned::get_reading_mode();
     $getVars = array();
     if (!$httpMethod) {
         $httpMethod = $postVars || is_array($postVars) ? "POST" : "GET";
     }
     if (!$session) {
         $session = Injector::inst()->create('SilverStripe\\Control\\Session', array());
     }
     $cookieJar = $cookies instanceof Cookie_Backend ? $cookies : Injector::inst()->createWithArgs('SilverStripe\\Control\\Cookie_Backend', array($cookies ?: array()));
     // Back up the current values of the superglobals
     $existingRequestVars = isset($_REQUEST) ? $_REQUEST : array();
     $existingGetVars = isset($_GET) ? $_GET : array();
     $existingPostVars = isset($_POST) ? $_POST : array();
     $existingSessionVars = isset($_SESSION) ? $_SESSION : array();
     $existingCookies = isset($_COOKIE) ? $_COOKIE : array();
     $existingServer = isset($_SERVER) ? $_SERVER : array();
     $existingRequirementsBackend = Requirements::backend();
     Cookie::config()->update('report_errors', false);
     Requirements::set_backend(Requirements_Backend::create());
     // Set callback to invoke prior to return
     $onCleanup = function () use($existingRequestVars, $existingGetVars, $existingPostVars, $existingSessionVars, $existingCookies, $existingServer, $existingRequirementsBackend, $oldReadingMode) {
         // Restore the super globals
         $_REQUEST = $existingRequestVars;
         $_GET = $existingGetVars;
         $_POST = $existingPostVars;
         $_SESSION = $existingSessionVars;
         $_COOKIE = $existingCookies;
         $_SERVER = $existingServer;
         Requirements::set_backend($existingRequirementsBackend);
         // These are needed so that calling Director::test() does not muck with whoever is calling it.
         // Really, it's some inappropriate coupling and should be resolved by making less use of statics
         Versioned::set_reading_mode($oldReadingMode);
         Injector::unnest();
         // Restore old CookieJar, etc
         Config::unnest();
     };
     if (strpos($url, '#') !== false) {
         $url = substr($url, 0, strpos($url, '#'));
     }
     // Handle absolute URLs
     if (parse_url($url, PHP_URL_HOST)) {
         $bits = parse_url($url);
         // If a port is mentioned in the absolute URL, be sure to add that into the HTTP host
         if (isset($bits['port'])) {
             $_SERVER['HTTP_HOST'] = $bits['host'] . ':' . $bits['port'];
         } else {
             $_SERVER['HTTP_HOST'] = $bits['host'];
         }
     }
     // Ensure URL is properly made relative.
     // Example: url passed is "/ss31/my-page" (prefixed with BASE_URL), this should be changed to "my-page"
     $url = self::makeRelative($url);
     $urlWithQuerystring = $url;
     if (strpos($url, '?') !== false) {
         list($url, $getVarsEncoded) = explode('?', $url, 2);
         parse_str($getVarsEncoded, $getVars);
     }
     // Replace the super globals with appropriate test values
     $_REQUEST = ArrayLib::array_merge_recursive((array) $getVars, (array) $postVars);
     $_GET = (array) $getVars;
     $_POST = (array) $postVars;
     $_SESSION = $session ? $session->inst_getAll() : array();
     $_COOKIE = $cookieJar->getAll(false);
     Injector::inst()->registerService($cookieJar, 'SilverStripe\\Control\\Cookie_Backend');
     $_SERVER['REQUEST_URI'] = Director::baseURL() . $urlWithQuerystring;
     $request = new HTTPRequest($httpMethod, $url, $getVars, $postVars, $body);
     if ($headers) {
         foreach ($headers as $k => $v) {
             $request->addHeader($k, $v);
         }
     }
     // Pre-request filtering
     // @see issue #2517
     $model = DataModel::inst();
     $output = Injector::inst()->get('SilverStripe\\Control\\RequestProcessor')->preRequest($request, $session, $model);
     if ($output === false) {
         $onCleanup();
         throw new HTTPResponse_Exception(_t('Director.INVALID_REQUEST', 'Invalid request'), 400);
     }
     // TODO: Pass in the DataModel
     $result = Director::handleRequest($request, $session, $model);
     // Ensure that the result is an HTTPResponse object
     if (is_string($result)) {
         if (substr($result, 0, 9) == 'redirect:') {
             $response = new HTTPResponse();
             $response->redirect(substr($result, 9));
             $result = $response;
         } else {
             $result = new HTTPResponse($result);
         }
     }
     $output = Injector::inst()->get('SilverStripe\\Control\\RequestProcessor')->postRequest($request, $result, $model);
     if ($output === false) {
         $onCleanup();
         throw new HTTPResponse_Exception("Invalid response");
     }
     // Return valid response
     $onCleanup();
     return $result;
 }
 public function testConditionalTemplateRequire()
 {
     $testPath = ltrim(preg_replace('#^' . BASE_PATH . '#', '', dirname(__DIR__)), '/');
     /** @var Requirements_Backend $backend */
     $backend = Injector::inst()->create('SilverStripe\\View\\Requirements_Backend');
     $this->setupRequirements($backend);
     $holder = Requirements::backend();
     Requirements::set_backend($backend);
     $data = new ArrayData(array('FailTest' => true));
     $data->renderWith('RequirementsTest_Conditionals');
     $this->assertFileIncluded($backend, 'css', $testPath . '/css/forms/RequirementsTest_a.css');
     $this->assertFileIncluded($backend, 'js', array($testPath . '/javascript/forms/RequirementsTest_b.js', $testPath . '/javascript/forms/RequirementsTest_c.js'));
     $this->assertFileNotIncluded($backend, 'js', $testPath . '/javascript/forms/RequirementsTest_a.js');
     $this->assertFileNotIncluded($backend, 'css', array($testPath . '/css/forms/RequirementsTest_b.css', $testPath . '/css/forms/RequirementsTest_c.css'));
     $backend->clear();
     $data = new ArrayData(array('FailTest' => false));
     $data->renderWith('RequirementsTest_Conditionals');
     $this->assertFileNotIncluded($backend, 'css', $testPath . '/css/forms/RequirementsTest_a.css');
     $this->assertFileNotIncluded($backend, 'js', array($testPath . '/javascript/forms/RequirementsTest_b.js', $testPath . '/javascript/forms/RequirementsTest_c.js'));
     $this->assertFileIncluded($backend, 'js', $testPath . '/javascript/forms/RequirementsTest_a.js');
     $this->assertFileIncluded($backend, 'css', array($testPath . '/css/forms/RequirementsTest_b.css', $testPath . '/css/forms/RequirementsTest_c.css'));
     Requirements::set_backend($holder);
 }