public function setUp()
 {
     parent::setUp();
     Director::config()->update('rules', array('FormTest_Controller' => 'FormTest_Controller'));
     // Suppress themes
     SSViewer::config()->remove('theme');
 }
 public function testConfigVary()
 {
     $body = "<html><head></head><body><h1>Mysite</h1></body></html>";
     $response = new HTTPResponse($body, 200);
     Director::config()->update('environment_type', 'live');
     HTTP::set_cache_age(30);
     HTTP::add_cache_headers($response);
     $v = $response->getHeader('Vary');
     $this->assertNotEmpty($v);
     $this->assertContains("Cookie", $v);
     $this->assertContains("X-Forwarded-Protocol", $v);
     $this->assertContains("User-Agent", $v);
     $this->assertContains("Accept", $v);
     HTTP::config()->update('vary', '');
     $response = new HTTPResponse($body, 200);
     HTTP::add_cache_headers($response);
     $v = $response->getHeader('Vary');
     $this->assertEmpty($v);
 }
use SilverStripe\Security\BasicAuth;
use SilverStripe\Security\Security;
/*
 * _ss_environment.php handler
 */
if (defined('SS_ENVIRONMENT_FILE')) {
    // Only perform validation if SS_ENVIRONMENT_FILE is actually set, which is to say, there is an
    // _ss_environment.php file
    foreach (array('SS_DATABASE_PASSWORD', 'SS_DATABASE_USERNAME', 'SS_ENVIRONMENT_TYPE') as $reqDefine) {
        if (!defined($reqDefine)) {
            user_error("{$reqDefine} must be defined in your _ss_environment.php." . "See http://doc.silverstripe.org/framework/en/topics/environment-management for more information", E_USER_ERROR);
        }
    }
}
if (defined('SS_ENVIRONMENT_TYPE')) {
    Director::config()->environment_type = SS_ENVIRONMENT_TYPE;
}
global $database;
// No database provided
if (!isset($database) || !$database) {
    if (defined('SS_DATABASE_NAME')) {
        $database = SS_DATABASE_NAME;
    } else {
        if (defined('SS_DATABASE_CHOOSE_NAME') && SS_DATABASE_CHOOSE_NAME) {
            $loopCount = (int) SS_DATABASE_CHOOSE_NAME;
            $databaseDir = BASE_PATH;
            for ($i = 0; $i < $loopCount - 1; $i++) {
                $databaseDir = dirname($databaseDir);
            }
            $database = "SS_" . basename($databaseDir);
            $database = str_replace('.', '', $database);
 /**
  * Convenience function to return the admin route config.
  * Looks for the {@link Director::$rules} for the current admin Controller.
  *
  * @return string
  */
 public static function get_admin_route()
 {
     $rules = Director::config()->rules;
     $adminRoute = array_search(__CLASS__, $rules);
     return $adminRoute ?: static::config()->url_base;
 }
 public function setUp()
 {
     //nest config and injector for each test so they are effectively sandboxed per test
     Config::nest();
     Injector::nest();
     $this->originalReadingMode = Versioned::get_reading_mode();
     // We cannot run the tests on this abstract class.
     if (get_class($this) == __CLASS__) {
         $this->markTestSkipped(sprintf('Skipping %s ', get_class($this)));
         return;
     }
     // Mark test as being run
     $this->originalIsRunningTest = self::$is_running_test;
     self::$is_running_test = true;
     // i18n needs to be set to the defaults or tests fail
     i18n::set_locale(i18n::config()->get('default_locale'));
     i18n::config()->date_format = null;
     i18n::config()->time_format = null;
     // Set default timezone consistently to avoid NZ-specific dependencies
     date_default_timezone_set('UTC');
     // Remove password validation
     $this->originalMemberPasswordValidator = Member::password_validator();
     $this->originalRequirements = Requirements::backend();
     Member::set_password_validator(null);
     Cookie::config()->update('report_errors', false);
     if (class_exists('SilverStripe\\CMS\\Controllers\\RootURLController')) {
         RootURLController::reset();
     }
     if (class_exists('Translatable')) {
         Translatable::reset();
     }
     Versioned::reset();
     DataObject::reset();
     if (class_exists('SilverStripe\\CMS\\Model\\SiteTree')) {
         SiteTree::reset();
     }
     Hierarchy::reset();
     if (Controller::has_curr()) {
         Controller::curr()->setSession(Session::create(array()));
     }
     Security::$database_is_ready = null;
     // Add controller-name auto-routing
     // @todo Fix to work with namespaced controllers
     Director::config()->update('rules', array('$Controller//$Action/$ID/$OtherID' => '*'));
     $fixtureFiles = $this->getFixturePaths();
     // Todo: this could be a special test model
     $this->model = DataModel::inst();
     // Set up fixture
     if ($fixtureFiles || $this->usesDatabase) {
         if (!self::using_temp_db()) {
             self::create_temp_db();
         }
         DataObject::singleton()->flushCache();
         self::empty_temp_db();
         foreach ($this->requireDefaultRecordsFrom as $className) {
             $instance = singleton($className);
             if (method_exists($instance, 'requireDefaultRecords')) {
                 $instance->requireDefaultRecords();
             }
             if (method_exists($instance, 'augmentDefaultRecords')) {
                 $instance->augmentDefaultRecords();
             }
         }
         foreach ($fixtureFiles as $fixtureFilePath) {
             $fixture = YamlFixture::create($fixtureFilePath);
             $fixture->writeInto($this->getFixtureFactory());
         }
         $this->logInWithPermission("ADMIN");
     }
     // Preserve memory settings
     $this->originalMemoryLimit = ini_get('memory_limit');
     // turn off template debugging
     SSViewer::config()->update('source_file_comments', false);
     // Clear requirements
     Requirements::clear();
     // Set up email
     $this->mailer = new TestMailer();
     Injector::inst()->registerService($this->mailer, 'SilverStripe\\Control\\Email\\Mailer');
     Email::config()->remove('send_all_emails_to');
 }
 /**
  * Handle an HTTP request, defined with a HTTPRequest object.
  *
  * @skipUpgrade
  * @param HTTPRequest $request
  * @param Session $session
  * @param DataModel $model
  * @return HTTPResponse|string
  */
 protected static function handleRequest(HTTPRequest $request, Session $session, DataModel $model)
 {
     $rules = Director::config()->get('rules');
     if (isset($_REQUEST['debug'])) {
         Debug::show($rules);
     }
     foreach ($rules as $pattern => $controllerOptions) {
         if (is_string($controllerOptions)) {
             if (substr($controllerOptions, 0, 2) == '->') {
                 $controllerOptions = array('Redirect' => substr($controllerOptions, 2));
             } else {
                 $controllerOptions = array('Controller' => $controllerOptions);
             }
         }
         if (($arguments = $request->match($pattern, true)) !== false) {
             $request->setRouteParams($controllerOptions);
             // controllerOptions provide some default arguments
             $arguments = array_merge($controllerOptions, $arguments);
             // Pop additional tokens from the tokenizer if necessary
             if (isset($controllerOptions['_PopTokeniser'])) {
                 $request->shift($controllerOptions['_PopTokeniser']);
             }
             // Handle redirection
             if (isset($arguments['Redirect'])) {
                 return "redirect:" . Director::absoluteURL($arguments['Redirect'], true);
             } else {
                 // Find the controller name
                 $controller = $arguments['Controller'];
                 Director::$urlParams = $arguments;
                 $controllerObj = Injector::inst()->create($controller);
                 $controllerObj->setSession($session);
                 try {
                     $result = $controllerObj->handleRequest($request, $model);
                 } catch (HTTPResponse_Exception $responseException) {
                     $result = $responseException->getResponse();
                 }
                 if (!is_object($result) || $result instanceof HTTPResponse) {
                     return $result;
                 }
                 user_error("Bad result from url " . $request->getURL() . " handled by " . get_class($controllerObj) . " controller: " . get_class($result), E_USER_WARNING);
             }
         }
     }
     // No URL rules matched, so return a 404 error.
     return new HTTPResponse('No URL rule was matched', 404);
 }
 public function testRenderWithSourceFileComments()
 {
     Director::config()->update('environment_type', 'dev');
     SSViewer::config()->update('source_file_comments', true);
     $i = FRAMEWORK_PATH . '/tests/templates/Includes';
     $f = FRAMEWORK_PATH . '/tests/templates/SSViewerTestComments';
     $templates = array(array('name' => 'SSViewerTestCommentsFullSource', 'expected' => "" . "<!doctype html>" . "<!-- template {$f}/SSViewerTestCommentsFullSource.ss -->" . "<html>" . "\t<head></head>" . "\t<body></body>" . "</html>" . "<!-- end template {$f}/SSViewerTestCommentsFullSource.ss -->"), array('name' => 'SSViewerTestCommentsFullSourceHTML4Doctype', 'expected' => "" . "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML " . "4.01//EN\"\t\t\"http://www.w3.org/TR/html4/strict.dtd\">" . "<!-- template {$f}/SSViewerTestCommentsFullSourceHTML4Doctype.ss -->" . "<html>" . "\t<head></head>" . "\t<body></body>" . "</html>" . "<!-- end template {$f}/SSViewerTestCommentsFullSourceHTML4Doctype.ss -->"), array('name' => 'SSViewerTestCommentsFullSourceNoDoctype', 'expected' => "" . "<html><!-- template {$f}/SSViewerTestCommentsFullSourceNoDoctype.ss -->" . "\t<head></head>" . "\t<body></body>" . "<!-- end template {$f}/SSViewerTestCommentsFullSourceNoDoctype.ss --></html>"), array('name' => 'SSViewerTestCommentsFullSourceIfIE', 'expected' => "" . "<!doctype html>" . "<!-- template {$f}/SSViewerTestCommentsFullSourceIfIE.ss -->" . "<!--[if lte IE 8]> <html class='old-ie'> <![endif]-->" . "<!--[if gt IE 8]> <html class='new-ie'> <![endif]-->" . "<!--[if !IE]><!--> <html class='no-ie'> <!--<![endif]-->" . "\t<head></head>" . "\t<body></body>" . "</html>" . "<!-- end template {$f}/SSViewerTestCommentsFullSourceIfIE.ss -->"), array('name' => 'SSViewerTestCommentsFullSourceIfIENoDoctype', 'expected' => "" . "<!--[if lte IE 8]> <html class='old-ie'> <![endif]-->" . "<!--[if gt IE 8]> <html class='new-ie'> <![endif]-->" . "<!--[if !IE]><!--> <html class='no-ie'>" . "<!-- template {$f}/SSViewerTestCommentsFullSourceIfIENoDoctype.ss -->" . " <!--<![endif]-->" . "\t<head></head>" . "\t<body></body>" . "<!-- end template {$f}/SSViewerTestCommentsFullSourceIfIENoDoctype.ss --></html>"), array('name' => 'SSViewerTestCommentsPartialSource', 'expected' => "<!-- template {$f}/SSViewerTestCommentsPartialSource.ss -->" . "<div class='typography'></div>" . "<!-- end template {$f}/SSViewerTestCommentsPartialSource.ss -->"), array('name' => 'SSViewerTestCommentsWithInclude', 'expected' => "<!-- template {$f}/SSViewerTestCommentsWithInclude.ss -->" . "<div class='typography'>" . "<!-- include 'SSViewerTestCommentsInclude' -->" . "<!-- template {$i}/SSViewerTestCommentsInclude.ss -->" . "Included" . "<!-- end template {$i}/SSViewerTestCommentsInclude.ss -->" . "<!-- end include 'SSViewerTestCommentsInclude' -->" . "</div>" . "<!-- end template {$f}/SSViewerTestCommentsWithInclude.ss -->"));
     foreach ($templates as $template) {
         $this->_renderWithSourceFileComments('SSViewerTestComments/' . $template['name'], $template['expected']);
     }
 }
 public function setUp()
 {
     parent::setUp();
     Director::config()->update('rules', array('testGoodBase1' => "RequestHandlingTest_Controller", 'testGoodBase2//$Action/$ID/$OtherID' => "RequestHandlingTest_Controller", 'testBadBase/$Action/$ID/$OtherID' => "RequestHandlingTest_Controller", 'testBaseWithExtension/virtualfile.xml' => "RequestHandlingTest_Controller", 'testBaseWithExtension//$Action/$ID/$OtherID' => "RequestHandlingTest_Controller", 'testParentBase/testChildBase//$Action/$ID/$OtherID' => "RequestHandlingTest_Controller"));
 }