Skip to content

Commit

Permalink
Merged branches/2.3 into trunk
Browse files Browse the repository at this point in the history
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@66395 467b73ca-7a2a-4603-9d3b-597d59a354a9
  • Loading branch information
Sam Minnee committed Nov 22, 2008
1 parent ece0130 commit 2984355
Show file tree
Hide file tree
Showing 30 changed files with 423 additions and 88 deletions.
1 change: 1 addition & 0 deletions Makefile
Expand Up @@ -10,6 +10,7 @@ URL=`php5 ./cli-script.php SapphireInfo/baseurl`
test: phpunit

phpunit:
php5 ./cli-script.php dev/build
php5 ./cli-script.php dev/tests/all flush=1

windmill:
Expand Down
16 changes: 13 additions & 3 deletions core/Cookie.php
Expand Up @@ -5,6 +5,8 @@
* @subpackage misc
*/
class Cookie extends Object {
static $report_errors = true;

/**
* Set a cookie variable
* @param name The variable name
Expand All @@ -13,11 +15,12 @@ class Cookie extends Object {
*/
static function set($name, $value, $expiryDays = 90) {
if(!headers_sent($file, $line)) {
setcookie($name, $value, time()+(86400*$expiryDays), Director::baseURL());
$_COOKIE[$name] = $value;
$expiry = $expiryDays > 0 ? time()+(86400*$expiryDays) : 0;
setcookie($name, $value, $expiry, Director::baseURL());
} else {
// if(Director::isDevMode()) user_error("Cookie '$name' can't be set. The site started outputting was content at line $line in $file", E_USER_WARNING);
if(self::$report_errors) user_error("Cookie '$name' can't be set. The site started outputting was content at line $line in $file", E_USER_WARNING);
}
$_COOKIE[$name] = $value;
}

/**
Expand All @@ -32,6 +35,13 @@ static function forceExpiry( $name ) {
setcookie( $name, null, time() - 86400 );
}
}

static function set_report_errors($reportErrors) {
self::$report_errors = $reportErrors;
}
static function report_errors() {
return self::$report_errors;
}
}

?>
10 changes: 10 additions & 0 deletions core/ManifestBuilder.php
Expand Up @@ -520,6 +520,16 @@ private static function find_children() {
private static function get_children($class) {
return isset(self::$extendsArray[$class]) ? self::$extendsArray[$class] : array();
}

/**
* Returns if the Manifest has been included
*
* @return Boolean
*/
static function has_been_included() {
global $_CLASS_MANIFEST, $_TEMPLATE_MANIFEST, $_CSS_MANIFEST, $_ALL_CLASSES;
return (bool)(empty($_CLASS_MANIFEST) && empty($_TEMPLATE_MANIFEST) && empty($_CSS_MANIFEST) && empty($_ALL_CLASSES));
}

/**
* Returns a flat array with all children of a given class
Expand Down
2 changes: 1 addition & 1 deletion core/Requirements.php
Expand Up @@ -755,7 +755,7 @@ function clear_combined_files() {
*
*/
function process_combined_files() {
if(Director::isDev()) {
if(Director::isDev() && !SapphireTest::is_running_test()) {
return;
}

Expand Down
20 changes: 19 additions & 1 deletion core/SSViewer.php
Expand Up @@ -345,10 +345,28 @@ public function process($item) {
}

static function parseTemplateContent($content, $template="") {
// Add template filename comments on dev sites
if(Director::isDev() && $template) {
// If this template is a full HTML page, then put the comments just inside the HTML tag to prevent any IE glitches
if(stripos($content, "<html") !== false) {
$content = preg_replace('/(<html[^>]*>)/i', "\\1<!-- template $template -->", $content);
$content = preg_replace('/(<\/html[^>]*>)/i', "\\1<!-- end template $template -->", $content);
} else {
$content = "<!-- template $template -->\n" . $content . "\n<!-- end template $template -->";
}
}

while(true) {
$oldContent = $content;

// Add include filename comments on dev sites
if(Director::isDev()) $replacementCode = 'return "<!-- include " . SSViewer::getTemplateFile($matches[1]) . "-->\n"
. SSViewer::getTemplateContent($matches[1])
. "\n<!-- end include " . SSViewer::getTemplateFile($matches[1]) . "-->";';
else $replacementCode = 'return SSViewer::getTemplateContent($matches[1]);';

$content = preg_replace_callback('/<' . '% include +([A-Za-z0-9_]+) +%' . '>/', create_function(
'$matches', 'return SSViewer::getTemplateContent($matches[1]);'
'$matches', $replacementCode
), $content);
if($oldContent == $content) break;
}
Expand Down
6 changes: 2 additions & 4 deletions core/control/Controller.php
Expand Up @@ -79,9 +79,6 @@ function init() {
if(Director::isTest() && $this->basicAuthEnabled && Security::database_is_ready()) {
BasicAuth::requireLogin("SilverStripe test website. Use your CMS login", "ADMIN");
}

//
Cookie::set("PastVisitor", true);

// Directly access the session variable just in case the Group or Member tables don't yet exist
if(Session::get('loggedInAs') && Security::database_is_ready()) {
Expand Down Expand Up @@ -405,7 +402,8 @@ function CurrentMember() {
* @return boolean
*/
function PastVisitor() {
return Cookie::get("PastVisitor") ? true : false;
user_error("Controller::PastVisitor() is deprecated", E_USER_NOTICE);
return false;
}

/**
Expand Down
15 changes: 13 additions & 2 deletions core/control/Director.php
Expand Up @@ -170,12 +170,20 @@ function test($url, $postVars = null, $session = null, $httpMethod = null, $body
$existingGetVars = $_GET;
$existingPostVars = $_POST;
$existingSessionVars = $_SESSION;
$existingCookies = $_COOKIE;

$existingCookieReportErrors = Cookie::report_errors();
Cookie::set_report_errors(false);

$existingRequirementsBackend = Requirements::backend();
Requirements::set_backend(new Requirements_Backend());

// Replace the superglobals with appropriate test values
$_REQUEST = array_merge((array)$getVars, (array)$postVars);
$_GET = (array)$getVars;
$_POST = (array)$postVars;
$_SESSION = $session ? $session->inst_getAll() : array();
$_COOKIE = array();

$req = new HTTPRequest($httpMethod, $url, $getVars, $postVars, $body);
if($headers) foreach($headers as $k => $v) $req->addHeader($k, $v);
Expand All @@ -186,6 +194,9 @@ function test($url, $postVars = null, $session = null, $httpMethod = null, $body
$_GET = $existingGetVars;
$_POST = $existingPostVars;
$_SESSION = $existingSessionVars;
$_COOKIE = $existingCookies;
Cookie::set_report_errors($existingCookieReportErrors);
Requirements::set_backend($existingRequirementsBackend);

// These are needed so that calling Director::test() doesnt muck with whoever is calling it.
// Really, it's some inapproriate coupling and should be resolved by making less use of statics
Expand Down Expand Up @@ -692,7 +703,7 @@ static function isDev() {
if(self::$environment_type) return self::$environment_type == 'dev';

// Check if we are running on one of the development servers
if(in_array($_SERVER['HTTP_HOST'], Director::$dev_servers)) {
if(isset($_SERVER['HTTP_HOST']) && in_array($_SERVER['HTTP_HOST'], Director::$dev_servers)) {
return true;
}
/*
Expand All @@ -717,7 +728,7 @@ static function isTest() {
}

// Check if we are running on one of the test servers
if(in_array($_SERVER['HTTP_HOST'], Director::$test_servers)) {
if(isset($_SERVER['HTTP_HOST']) && in_array($_SERVER['HTTP_HOST'], Director::$test_servers)) {
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion core/control/ModelAsController.php
Expand Up @@ -72,7 +72,7 @@ public function getNestedController() {

return $controller;
} else {
return "The requested page couldn't be found.";
return new HTTPResponse("The requested page couldn't be found.",404);
}

} else {
Expand Down
14 changes: 13 additions & 1 deletion core/model/DataObject.php
Expand Up @@ -2062,7 +2062,11 @@ public function buildSQL($filter = "", $sort = "", $limit = "", $join = "", $res
// Get the tables to join to
$tableClasses = ClassInfo::dataClassesFor($this->class);
if(!$tableClasses) {
user_error("DataObject::buildSQL: Can't find data classes (classes linked to tables) for $this->class", E_USER_ERROR);
if(!ManifestBuilder::has_been_included()) {
user_error("DataObjects have been requested before the manifest is loaded. Please ensure you are not querying the database in _config.php.", E_USER_ERROR);
} else {
user_error("DataObject::buildSQL: Can't find data classes (classes linked to tables) for $this->class. Please ensure you run dev/build after creating a new DataObject.", E_USER_ERROR);
}
}

$baseClass = array_shift($tableClasses);
Expand Down Expand Up @@ -2202,6 +2206,10 @@ public static function get($callerClass, $filter = "", $sort = "", $join = "", $
* @return mixed The objects matching the filter, in the class specified by $containerClass
*/
public function instance_get($filter = "", $sort = "", $join = "", $limit="", $containerClass = "DataObjectSet") {
if(!DB::isActive()) {
user_error("DataObjects have been requested before the database is ready. Please ensure your database connection details are correct, your database has been built, and that you are not trying to query the database in _config.php.", E_USER_ERROR);
}

$query = $this->extendedSQL($filter, $sort, $limit, $join);
$records = $query->execute();

Expand Down Expand Up @@ -2300,6 +2308,10 @@ public function flushCache() {
* @return DataObject The first item matching the query
*/
public function instance_get_one($filter, $orderby = null) {
if(!DB::isActive()) {
user_error("DataObjects have been requested before the database is ready. Please ensure your database connection details are correct, your database has been built, and that you are not trying to query the database in _config.php.", E_USER_ERROR);
}

$query = $this->buildSQL($filter);
$query->limit = "1";
if($orderby) {
Expand Down
12 changes: 2 additions & 10 deletions core/model/ErrorPage.php
Expand Up @@ -94,16 +94,11 @@ function getCMSFields() {
* @param boolean $createNewVersion Set this to true to create a new version number. By default, the existing version number will be copied over.
*/
function publish($fromStage, $toStage, $createNewVersion = false) {
$alc_enc = isset($_COOKIE['alc_enc']) ? $_COOKIE['alc_enc'] : null;
Cookie::set('alc_enc', null);

$oldStage = Versioned::current_stage();

// Run the page
Requirements::clear();
$controller = new ErrorPage_Controller($this);
$errorContent = $controller->handleRequest(new HTTPRequest('GET',''))->getBody();
Requirements::clear();
$response = Director::test($this->Link());
$errorContent = $response->getBody();

if(!file_exists(ASSETS_PATH)) {
mkdir(ASSETS_PATH, 02775);
Expand All @@ -116,9 +111,6 @@ function publish($fromStage, $toStage, $createNewVersion = false) {

// Restore the version we're currently connected to.
Versioned::reading_stage($oldStage);

// Log back in
if(isset($alc_enc)) Cookie::set('alc_enc', $alc_enc);

return $this->extension_instances['Versioned']->publish($fromStage, $toStage, $createNewVersion);
}
Expand Down
4 changes: 2 additions & 2 deletions core/model/SQLQuery.php
Expand Up @@ -432,8 +432,8 @@ function execute() {
* @return boolean
*/
function filtersOnID() {
return ($query->where && count($query->where) == 1 &&
(strpos($query->where[0], ".`ID` = ") || strpos($query->where[0], ".ID = ") || strpos($query->where[0], "ID = ") )
return ($this->where && count($this->where) == 1 &&
(strpos($this->where[0], ".`ID` = ") || strpos($this->where[0], ".ID = ") || strpos($this->where[0], "ID = ") )
);
}

Expand Down
12 changes: 2 additions & 10 deletions core/model/SiteTree.php
Expand Up @@ -1370,8 +1370,6 @@ function doPublish() {
// Handle activities undertaken by decorators
$this->extend('onBeforePublish', $original);

$this->AssignedToID = 0;
$this->RequestedByID = 0;
$this->Status = "Published";
//$this->PublishedByID = Member::currentUser()->ID;
$this->write();
Expand Down Expand Up @@ -1410,8 +1408,6 @@ function doUnpublish() {
*/
function doRollbackTo($version) {
$this->publish($version, "Stage", true);
$this->AssignedToID = 0;
$this->RequestedByID = 0;
$this->Status = "Saved (update)";
$this->writeWithoutVersion();
}
Expand All @@ -1424,8 +1420,6 @@ function doRevertToLive() {

// Use a clone to get the updates made by $this->publish
$clone = DataObject::get_by_id("SiteTree", $this->ID);
$clone->AssignedToID = 0;
$clone->RequestedByID = 0;
$clone->Status = "Published";
$clone->writeWithoutVersion();
}
Expand All @@ -1443,11 +1437,9 @@ function isNew() {
* Changing the condition from empty($this->ID) to
* !$this->ID && !$this->record['ID'] fixed this.
*/
if(empty($this->ID))
return true;
if(empty($this->ID)) return true;

if(is_numeric($this->ID))
return false;
if(is_numeric($this->ID)) return false;

return stripos($this->ID, 'new') === 0;
}
Expand Down
2 changes: 0 additions & 2 deletions core/model/VirtualPage.php
Expand Up @@ -28,8 +28,6 @@ function getVirtualFields() {
$nonVirtualFields = array(
"SecurityTypeID",
"OwnerID",
"AssignedToID",
"RequestedByID",
"URLSegment",
"Sort",
"Status",
Expand Down
4 changes: 2 additions & 2 deletions core/model/fieldtypes/ForeignKey.php
Expand Up @@ -43,8 +43,8 @@ public function scaffoldFormField($title = null, $params = null) {
$field = new FileField($relationName, $title, $this->value);
}
} else {
$objs = DataObject::get($this->object->class);
$titleField = (singleton($this->object->class)->hasField('Title')) ? "Title" : "Name";
$objs = DataObject::get($hasOneClass);
$titleField = (singleton($hasOneClass)->hasField('Title')) ? "Title" : "Name";
$map = ($objs) ? $objs->toDropdownMap("ID", $titleField) : false;
$field = new DropdownField($this->name, $title, $map, null, null, ' ');
}
Expand Down
6 changes: 5 additions & 1 deletion dev/CsvBulkLoader.php
Expand Up @@ -73,7 +73,11 @@ protected function processRecord($record, $columnMap, &$results, $preview = fals
// trigger custom search method for finding a relation based on the given value
// and write it back to the relation (or create a new object)
$relationName = $this->relationCallbacks[$fieldName]['relationname'];
$relationObj = $obj->{$this->relationCallbacks[$fieldName]['callback']}($val, $record);
if($this->hasMethod($this->relationCallbacks[$fieldName]['callback'])) {
$relationObj = $this->{$this->relationCallbacks[$fieldName]['callback']}(&$obj, $val, $record);
} elseif($obj->hasMethod($this->relationCallbacks[$fieldName]['callback'])) {
$relationObj = $obj->{$this->relationCallbacks[$fieldName]['callback']}($val, $record);
}
if(!$relationObj || !$relationObj->exists()) {
$relationClass = $obj->has_one($relationName);
$relationObj = new $relationClass();
Expand Down
16 changes: 16 additions & 0 deletions dev/SapphireTest.php
Expand Up @@ -22,19 +22,31 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
protected $originalMailer;
protected $originalMemberPasswordValidator;
protected $originalRequirements;
protected $originalIsRunningTest;

protected $mailer;

protected static $is_running_test = false;

public static function is_running_test() {
return self::$is_running_test;
}

/**
* @var YamlFixture
*/
protected $fixture;

function setUp() {
// Mark test as being run
$this->originalIsRunningTest = self::$is_running_test;
self::$is_running_test = true;

// Remove password validation
$this->originalMemberPasswordValidator = Member::password_validator();
$this->originalRequirements = Requirements::backend();
Member::set_password_validator(null);
Cookie::set_report_errors(false);

$className = get_class($this);
$fixtureFile = eval("return {$className}::\$fixture_file;");
Expand Down Expand Up @@ -127,6 +139,10 @@ function tearDown() {

// Restore requirements
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;
}

/**
Expand Down
8 changes: 7 additions & 1 deletion filesystem/File.php
Expand Up @@ -480,6 +480,8 @@ function getFileType() {
'jpg' => 'JPEG image - good for photos',
'jpeg' => 'JPEG image - good for photos',
'png' => 'PNG image - good general-purpose format',
'ico' => 'Icon image',
'tiff' => 'Tagged image format',
'doc' => 'Word document',
'xls' => 'Excel spreadsheet',
'zip' => 'ZIP compressed file',
Expand All @@ -490,7 +492,11 @@ function getFileType() {
'wav' => 'WAV audo file',
'avi' => 'AVI video file',
'mpg' => 'MPEG video file',
'mpeg' => 'MPEG video file'
'mpeg' => 'MPEG video file',
'js' => 'Javascript file',
'css' => 'CSS file',
'html' => 'HTML file',
'htm' => 'HTML file'
);

$ext = $this->getExtension();
Expand Down

0 comments on commit 2984355

Please sign in to comment.