mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merged from branches/2.3
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@75590 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
13b358a8dd
commit
a5e82ddff1
@ -263,6 +263,7 @@ class HTTPRequest extends Object implements ArrayAccess {
|
||||
$response->addHeader("Content-Type", "$mimeType; name=\"" . addslashes($fileName) . "\"");
|
||||
$response->addHeader("Content-disposition", "attachment; filename=" . addslashes($fileName));
|
||||
$response->addHeader("Content-Length", strlen($fileData));
|
||||
$response->addHeader("Pragma", ""); // Necessary because IE has issues sending files over SSL
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
@ -133,7 +133,7 @@ class RequestHandler extends ViewableData {
|
||||
|
||||
// But if we have more content on the URL and we don't know what to do with it, return an error.
|
||||
} else {
|
||||
return $this->httpError(400, "I can't handle sub-URLs of a $this->class object.");
|
||||
return $this->httpError(404, "I can't handle sub-URLs of a $this->class object.");
|
||||
}
|
||||
|
||||
return $this;
|
||||
|
@ -184,9 +184,7 @@ class ErrorPage extends Page {
|
||||
* @package cms
|
||||
*/
|
||||
class ErrorPage_Controller extends Page_Controller {
|
||||
public function init() {
|
||||
parent::init();
|
||||
|
||||
public function index() {
|
||||
Director::set_status_code($this->failover->ErrorCode ? $this->failover->ErrorCode : 404);
|
||||
}
|
||||
}
|
||||
|
@ -19,11 +19,11 @@ class Boolean extends DBField {
|
||||
}
|
||||
|
||||
function Nice() {
|
||||
return ($this->value) ? "yes" : "no";
|
||||
return ($this->value) ? _t('Boolean.YES', 'Yes') : _t('Boolean.NO', 'No');
|
||||
}
|
||||
|
||||
function NiceAsBoolean() {
|
||||
return ($this->value) ? "true" : "false";
|
||||
return ($this->value) ? 'true' : 'false';
|
||||
}
|
||||
|
||||
/**
|
||||
@ -32,7 +32,7 @@ class Boolean extends DBField {
|
||||
function saveInto($dataObject) {
|
||||
$fieldName = $this->name;
|
||||
if($fieldName) {
|
||||
$dataObject->$fieldName = $this->value ? 1 : 0;
|
||||
$dataObject->$fieldName = ($this->value) ? 1 : 0;
|
||||
} else {
|
||||
user_error("DBField::saveInto() Called on a nameless '$this->class' object", E_USER_ERROR);
|
||||
}
|
||||
@ -42,6 +42,16 @@ class Boolean extends DBField {
|
||||
return new CheckboxField($this->name, $title);
|
||||
}
|
||||
|
||||
public function scaffoldSearchField($title = null) {
|
||||
$anyText = _t('Boolean.ANY', 'Any');
|
||||
$source = array(
|
||||
1 => _t('Boolean.YES', 'Yes'),
|
||||
0 => _t('Boolean.NO', 'No')
|
||||
);
|
||||
|
||||
return new DropdownField($this->name, $title, $source, '', null, "($anyText)");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an encoding of the given value suitable for inclusion in a SQL statement.
|
||||
* If necessary, this should include quotes.
|
||||
|
@ -91,7 +91,7 @@ class Date extends DBField {
|
||||
/**
|
||||
* Return the date formatted using the given strftime formatting string.
|
||||
*
|
||||
* strftime obeyes the current LC_TIME/LC_ALL when printing lexical values
|
||||
* strftime obeys the current LC_TIME/LC_ALL when printing lexical values
|
||||
* like day- and month-names
|
||||
*/
|
||||
function FormatI18N($formattingString) {
|
||||
|
@ -47,27 +47,25 @@ class Enum extends DBField {
|
||||
DB::requireField($this->tableName, $this->name, $values);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a dropdown field suitable for editing this field
|
||||
*/
|
||||
function formField($title = null, $name = null, $hasEmpty = false, $value = "", $form = null, $emptyString = null) {
|
||||
if(!$title) $title = $this->name;
|
||||
if(!$name) $name = $this->name;
|
||||
|
||||
$field = new DropdownField($name, $title, $this->enumValues($hasEmpty), $value, $form, $emptyString);
|
||||
|
||||
return $field;
|
||||
}
|
||||
|
||||
public function scaffoldFormField($title = null, $params = null) {
|
||||
return $this->formField($title);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a dropdown field suitable for editing this field
|
||||
*/
|
||||
function formField($title = null, $name = null, $hasEmpty = false, $value = "", $form = null) {
|
||||
if(!$title) $title = $this->name;
|
||||
if(!$name) $name = $this->name;
|
||||
|
||||
$field = new DropdownField($name, $title, $this->enumValues($hasEmpty), $value, $form);
|
||||
|
||||
return $field;
|
||||
}
|
||||
|
||||
function scaffoldSearchField($title = null) {
|
||||
$field = $this->formField($title);
|
||||
$field->Source = array_merge(array("" => "(Any)"), $this->enumValues());
|
||||
return $field;
|
||||
$anyText = _t('Enum.ANY', 'Any');
|
||||
return $this->formField($title, null, false, '', null, "($anyText)");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -74,8 +74,8 @@ class CSVParser extends Object implements Iterator {
|
||||
function __construct($filename, $delimiter = ",", $enclosure = '"') {
|
||||
$filename = Director::getAbsFile($filename);
|
||||
$this->filename = $filename;
|
||||
$tis->delimiter = ",";
|
||||
$this->enclosure = '"';
|
||||
$this->delimiter = $delimiter;
|
||||
$this->enclosure = $enclosure;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -256,7 +256,7 @@ class Debug {
|
||||
echo $friendlyErrorMessage;
|
||||
} else {
|
||||
$errorFilePath = ErrorPage::get_filepath_for_errorcode($statusCode, Translatable::current_locale());
|
||||
if(file_exists($errorfilePath)) {
|
||||
if(file_exists($errorFilePath)) {
|
||||
echo file_get_contents(ASSETS_PATH . "/error-$statusCode.html");
|
||||
} else {
|
||||
$renderer = new DebugView();
|
||||
|
@ -316,7 +316,7 @@ class Folder extends File {
|
||||
$this,
|
||||
"Files",
|
||||
"File",
|
||||
array("Title" => _t('Folder.TITLE', "Title"), "LinkedURL" => _t('Folder.FILENAME', "Filename")),
|
||||
array("Title" => _t('Folder.TITLE', "Title"), "Filename" => _t('Folder.FILENAME', "Filename")),
|
||||
""
|
||||
);
|
||||
$fileList->setFolder($this);
|
||||
@ -330,9 +330,6 @@ class Folder extends File {
|
||||
$deleteButton = new HiddenField('deletemarked');
|
||||
}
|
||||
|
||||
$inlineFormAction = new InlineFormAction("delete_unused_thumbnails", _t('Folder.DELETEUNUSEDTHUMBNAILS', 'Delete unused thumbnails'));
|
||||
$inlineFormAction->includeDefaultJS(false) ;
|
||||
|
||||
$fields = new FieldSet(
|
||||
new HiddenField("Title"),
|
||||
new TabSet("Root",
|
||||
@ -355,12 +352,7 @@ class Folder extends File {
|
||||
)
|
||||
),
|
||||
new Tab("UnusedFiles", _t('Folder.UNUSEDFILESTAB', "Unused files"),
|
||||
new LiteralField( "UnusedAssets", "<h2>"._t('Folder.UNUSEDFILESTITLE', 'Unused files')."</h2>" ),
|
||||
$this->getAssetList(),
|
||||
new FieldGroup(
|
||||
new LiteralField( "UnusedThumbnails", "<h2>"._t('Folder.UNUSEDTHUMBNAILSTITLE', 'Unused thumbnails')."</h2>"),
|
||||
$inlineFormAction
|
||||
)
|
||||
new Folder_UnusedAssetsField($this)
|
||||
)
|
||||
),
|
||||
new HiddenField("ID")
|
||||
@ -371,34 +363,12 @@ class Folder extends File {
|
||||
return $fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates table for displaying unused files.
|
||||
*
|
||||
* @returns AssetTableField
|
||||
*/
|
||||
protected function getAssetList() {
|
||||
$where = $this->getUsedFilesList();
|
||||
$assetList = new AssetTableField(
|
||||
$this,
|
||||
"AssetList",
|
||||
"File",
|
||||
array("Title" => _t('Folder.TITLE', "Title"), "LinkedURL" => _t('Folder.FILENAME', "Filename")),
|
||||
"",
|
||||
$where
|
||||
);
|
||||
$assetList->setPopupCaption(_t('Folder.VIEWASSET', "View Asset"));
|
||||
$assetList->setPermissions(array("show","delete"));
|
||||
$assetList->Markable = false;
|
||||
return $assetList;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Looks for files used in system and create where clause which contains all ID's of files.
|
||||
*
|
||||
* @returns String where clause which will work as filter.
|
||||
*/
|
||||
protected function getUsedFilesList() {
|
||||
public function getUsedFilesList() {
|
||||
$result = DB::query("SELECT DISTINCT \"FileID\" FROM \"SiteTree_ImageTracking\"");
|
||||
$usedFiles = array();
|
||||
$where = "";
|
||||
@ -446,4 +416,60 @@ HTML;
|
||||
|
||||
}
|
||||
|
||||
class Folder_UnusedAssetsField extends CompositeField {
|
||||
protected $folder;
|
||||
|
||||
public function __construct($folder) {
|
||||
$this->folder = $folder;
|
||||
parent::__construct(new FieldSet());
|
||||
}
|
||||
|
||||
public function getChildren() {
|
||||
if($this->children->Count() == 0) {
|
||||
$inlineFormAction = new InlineFormAction("delete_unused_thumbnails", _t('Folder.DELETEUNUSEDTHUMBNAILS', 'Delete unused thumbnails'));
|
||||
$inlineFormAction->includeDefaultJS(false) ;
|
||||
|
||||
$this->children = new FieldSet(
|
||||
new LiteralField( "UnusedAssets", "<h2>"._t('Folder.UNUSEDFILESTITLE', 'Unused files')."</h2>" ),
|
||||
$this->getAssetList(),
|
||||
new FieldGroup(
|
||||
new LiteralField( "UnusedThumbnails", "<h2>"._t('Folder.UNUSEDTHUMBNAILSTITLE', 'Unused thumbnails')."</h2>"),
|
||||
$inlineFormAction
|
||||
)
|
||||
);
|
||||
$this->children->setForm($this->form);
|
||||
}
|
||||
return $this->children;
|
||||
}
|
||||
|
||||
public function FieldHolder() {
|
||||
$output = "";
|
||||
foreach($this->getChildren() as $child) {
|
||||
$output .= $child->FieldHolder();
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates table for displaying unused files.
|
||||
*
|
||||
* @returns AssetTableField
|
||||
*/
|
||||
protected function getAssetList() {
|
||||
$where = $this->folder->getUsedFilesList();
|
||||
$assetList = new AssetTableField(
|
||||
$this->folder,
|
||||
"AssetList",
|
||||
"File",
|
||||
array("Title" => _t('Folder.TITLE', "Title"), "LinkedURL" => _t('Folder.FILENAME', "Filename")),
|
||||
"",
|
||||
$where
|
||||
);
|
||||
$assetList->setPopupCaption(_t('Folder.VIEWASSET', "View Asset"));
|
||||
$assetList->setPermissions(array("show","delete"));
|
||||
$assetList->Markable = false;
|
||||
return $assetList;
|
||||
}
|
||||
}
|
||||
?>
|
@ -5,20 +5,28 @@
|
||||
* @subpackage fields-basic
|
||||
*/
|
||||
class CheckboxField extends FormField {
|
||||
/**
|
||||
* Returns a single checkbox field - used by templates.
|
||||
*
|
||||
* Shouldn't this have a value?
|
||||
*/
|
||||
|
||||
protected $disabled;
|
||||
|
||||
function setValue($value) {
|
||||
$this->value = ($value) ? 1 : 0;
|
||||
}
|
||||
|
||||
function dataValue() {
|
||||
return ($this->value) ? 1 : 0;
|
||||
}
|
||||
|
||||
function Value() {
|
||||
return ($this->value) ? 1 : 0;
|
||||
}
|
||||
|
||||
function Field() {
|
||||
$attributes = array(
|
||||
'type' => 'checkbox',
|
||||
'class' => ($this->extraClass() ? $this->extraClass() : ''),
|
||||
'id' => $this->id(),
|
||||
'name' => $this->Name(),
|
||||
'value' => 1,
|
||||
'checked' => $this->value ? 'checked' : '',
|
||||
'tabindex' => $this->getTabIndex()
|
||||
);
|
||||
@ -28,11 +36,6 @@ class CheckboxField extends FormField {
|
||||
return $this->createTag('input', $attributes);
|
||||
}
|
||||
|
||||
|
||||
function dataValue() {
|
||||
return $this->value ? 1 : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checkboxes use the RightLabelledFieldHolder template, to put the field on the left
|
||||
* and the label on the right. See {@link FormField::FieldHolder} for more information about
|
||||
@ -76,7 +79,7 @@ HTML;
|
||||
*/
|
||||
|
||||
function performReadonlyTransformation() {
|
||||
$field = new CheckboxField_Readonly($this->name, $this->title, $this->value ? 'Yes' : 'No');
|
||||
$field = new CheckboxField_Readonly($this->name, $this->title, $this->value ? _t('CheckboxField.YES', 'Yes') : _t('CheckboxField.NO', 'No'));
|
||||
$field->setForm($this->form);
|
||||
return $field;
|
||||
}
|
||||
@ -99,7 +102,7 @@ class CheckboxField_Readonly extends ReadonlyField {
|
||||
}
|
||||
|
||||
function setValue($val) {
|
||||
$this->value = ($val) ? 'Yes' : 'No';
|
||||
$this->value = ($val) ? _t('CheckboxField.YES', 'Yes') : _t('CheckboxField.NO', 'No');
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -77,9 +77,20 @@ class DropdownField extends FormField {
|
||||
}
|
||||
|
||||
foreach($source as $value => $title) {
|
||||
|
||||
// Blank value of field and source (e.g. "" => "(Any)")
|
||||
if($value === '' && ($this->value === '' || $this->value === null)) {
|
||||
$selected = 'selected';
|
||||
} else {
|
||||
// Normal value from the source
|
||||
if($value) {
|
||||
$selected = ($value == $this->value) ? 'selected' : null;
|
||||
if($selected && $this->value != 0) {
|
||||
$this->isSelected = true;
|
||||
} else {
|
||||
// Do a type check comparison, we might have an array key of 0
|
||||
$selected = ($value === $this->value) ? 'selected' : null;
|
||||
}
|
||||
|
||||
$this->isSelected = ($selected) ? true : false;
|
||||
}
|
||||
|
||||
$options .= $this->createTag(
|
||||
|
@ -1,13 +1,6 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Routines for DNS to country resolution
|
||||
*
|
||||
* - convert address (either ip or domainname) to country.
|
||||
* - returns false if IP address not found / not known;
|
||||
* - otherwise an array
|
||||
* - set $codeOnly to true if you just want the country code
|
||||
* - give a default for IP
|
||||
* Routines for IP to country resolution.
|
||||
*
|
||||
* @package sapphire
|
||||
* @subpackage misc
|
||||
@ -272,17 +265,22 @@ class Geoip extends Object {
|
||||
);
|
||||
|
||||
/**
|
||||
* Returns the country code of the person given an IP.
|
||||
* Find the country for an IP address.
|
||||
*
|
||||
* @param address - The IP address of the user,
|
||||
* @param codeOnly - Returns just the code of the IP instead of array( $CountryCode => $CountryName)
|
||||
* By default, it will return an array, keyed by
|
||||
* the country code with a value of the country
|
||||
* name.
|
||||
*
|
||||
* To return the code only, pass in true for the
|
||||
* $codeOnly parameter.
|
||||
*
|
||||
* @param string $address The IP address to get the country of
|
||||
* @param boolean $codeOnly Returns just the country code
|
||||
*/
|
||||
static function ip2country($address, $codeOnly = false) {
|
||||
// Return if in CLI, or you'll get this error: "sh: geoiplookup: command not found"
|
||||
if(Director::is_cli()) return false;
|
||||
|
||||
// Detect internal networks - this is us, so we're NZ
|
||||
if(substr($address, 0, 7) == "192.168" || substr($address,0,4) == "127.") {
|
||||
$code = 'NZ';
|
||||
} else {
|
||||
$cmd = 'geoiplookup ' . escapeshellarg($address);
|
||||
exec($cmd, $result, $code);
|
||||
// Note: At time of writing, $result is always zero for this program
|
||||
@ -298,11 +296,13 @@ class Geoip extends Object {
|
||||
$start = strpos($country, ':');
|
||||
if($start) $start += 2;
|
||||
$code = substr($country, $start, 2); // skip space
|
||||
}
|
||||
|
||||
if($code == 'IP' || $code == '--') {
|
||||
if(self::$default_country_code) $code = self::$default_country_code;
|
||||
else return false;
|
||||
if(self::$default_country_code) {
|
||||
$code = self::$default_country_code;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if(!$codeOnly) {
|
||||
@ -315,7 +315,6 @@ class Geoip extends Object {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the country code, for the current visitor
|
||||
*/
|
||||
@ -378,5 +377,4 @@ class Geoip extends Object {
|
||||
return $dropdown;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -240,6 +240,23 @@ class Member extends DataObject {
|
||||
$this->extend('memberLoggedIn');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the member ID logged in session actually
|
||||
* has a database record of the same ID. If there is
|
||||
* no logged in user, FALSE is returned anyway.
|
||||
*
|
||||
* @return boolean TRUE record found FALSE no record found
|
||||
*/
|
||||
static function logged_in_session_exists() {
|
||||
if($id = Member::currentUserID()) {
|
||||
if($member = DataObject::get_by_id('Member', $id)) {
|
||||
if($member->exists()) return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Log the user in if the "remember login" cookie is set
|
||||
*
|
||||
|
@ -43,16 +43,7 @@ class MemberLoginForm extends LoginForm {
|
||||
$backURL = Session::get('BackURL');
|
||||
}
|
||||
|
||||
// We assume if session is storing a member ID, that member exists in the DB
|
||||
$sessMemberExistsInDB = true;
|
||||
if($sessionMemberID = Member::currentUserID()) {
|
||||
$sessMemberInDB = DataObject::get_by_id('Member', $sessionMemberID);
|
||||
if(!($sessMemberInDB && $sessMemberInDB->exists())) {
|
||||
$sessMemberExistsInDB = false;
|
||||
}
|
||||
}
|
||||
|
||||
if($checkCurrentUser && Member::currentUserID() && $sessMemberExistsInDB) {
|
||||
if($checkCurrentUser && Member::currentUserID() && Member::logged_in_session_exists()) {
|
||||
$fields = new FieldSet(
|
||||
new HiddenField("AuthenticationMethod", null, $this->authenticator_class, $this)
|
||||
);
|
||||
|
@ -1,31 +1,39 @@
|
||||
<?php
|
||||
|
||||
class ErrorPageTest extends SapphireTest {
|
||||
/**
|
||||
* @package sapphire
|
||||
* @subpackage tests
|
||||
*/
|
||||
class ErrorPageTest extends FunctionalTest {
|
||||
|
||||
static $fixture_file = 'sapphire/tests/ErrorPageTest.yml';
|
||||
|
||||
function test404ErrorPage() {
|
||||
$errorPage = DataObject::get_one('ErrorPage', "ErrorCode = '404'");
|
||||
$page = $this->objFromFixture('ErrorPage', '404');
|
||||
|
||||
/* We have an ErrorPage object to use */
|
||||
$this->assertTrue($errorPage instanceof ErrorPage);
|
||||
/* The page is an instance of ErrorPage */
|
||||
$this->assertTrue($page instanceof ErrorPage, 'The page is an instance of ErrorPage');
|
||||
|
||||
/* Test the URL of the error page out to get a response */
|
||||
$response = Director::test(Director::makeRelative($errorPage->Link()));
|
||||
|
||||
/* We have an HTTPResponse object for the error page */
|
||||
$this->assertTrue($response instanceof HTTPResponse);
|
||||
$response = $this->get($page->URLSegment);
|
||||
|
||||
/* We have body text from the error page */
|
||||
$this->assertTrue($response->getBody() != null);
|
||||
$this->assertNotNull($response->getBody(), 'We have body text from the error page');
|
||||
|
||||
/* Status code of the HTTPResponse for error page is "404" */
|
||||
$this->assertTrue($response->getStatusCode() == '404');
|
||||
$this->assertEquals($response->getStatusCode(), '404', 'Status cod eof the HTTPResponse for error page is "404"');
|
||||
|
||||
/* Status message of the HTTPResponse for error page is "Not Found" */
|
||||
$this->assertTrue($response->getStatusDescription() == 'Not Found');
|
||||
$this->assertEquals($response->getStatusDescription(), 'Not Found', 'Status message of the HTTResponse for error page is "Not found"');
|
||||
}
|
||||
|
||||
function testBehaviourOfShowInMenuAndShowInSearchFlags() {
|
||||
$page = $this->objFromFixture('ErrorPage', '404');
|
||||
|
||||
/* Don't show the error page in the menus */
|
||||
$this->assertEquals($page->ShowInMenus, 0, 'Don\'t show the error page in the menus');
|
||||
|
||||
/* Don't show the error page in the search */
|
||||
$this->assertEquals($page->ShowInSearch, 0, 'Don\'t show the error page in search');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
@ -119,4 +119,3 @@ class ObjectStaticTest_Combined3 extends ObjectStaticTest_Combined2 {
|
||||
public static $first = array('test_3');
|
||||
public static $second = array('test_3');
|
||||
}
|
||||
/**#@-*/
|
||||
|
123
tests/forms/CheckboxFieldTest.php
Normal file
123
tests/forms/CheckboxFieldTest.php
Normal file
@ -0,0 +1,123 @@
|
||||
<?php
|
||||
/**
|
||||
* @package sapphire
|
||||
* @subpackage tests
|
||||
*/
|
||||
class CheckboxFieldTest extends SapphireTest {
|
||||
|
||||
function testFieldValueTrue() {
|
||||
/* Create the field, and set the value as boolean true */
|
||||
$field = new CheckboxField('IsChecked', 'Checked');
|
||||
$field->setValue(true);
|
||||
|
||||
/* dataValue() for the field is 1 */
|
||||
$this->assertEquals($field->dataValue(), 1, 'dataValue() returns a 1');
|
||||
|
||||
/* Value() returns 1 as well */
|
||||
$this->assertEquals($field->Value(), 1, 'Value() returns a 1');
|
||||
}
|
||||
|
||||
function testFieldValueString() {
|
||||
/* Create the field, and set the value as "on" (raw request field value from DOM) */
|
||||
$field = new CheckboxField('IsChecked', 'Checked');
|
||||
$field->setValue('on');
|
||||
|
||||
/* dataValue() for the field is 1 */
|
||||
$this->assertEquals($field->dataValue(), 1, 'dataValue() returns a 1');
|
||||
|
||||
/* Value() returns 1 as well */
|
||||
$this->assertEquals($field->Value(), 1, 'Value() returns a 1');
|
||||
}
|
||||
|
||||
function testFieldValueSettingNull() {
|
||||
/* Create the field, and set the value as NULL */
|
||||
$field = new CheckboxField('IsChecked', 'Checked');
|
||||
$field->setValue(null);
|
||||
|
||||
/* dataValue() for the field is 0 */
|
||||
$this->assertEquals($field->dataValue(), 0, 'dataValue() returns a 0');
|
||||
|
||||
/* Value() returns 0 as well */
|
||||
$this->assertEquals($field->Value(), 0, 'Value() returns a 0');
|
||||
}
|
||||
|
||||
function testFieldValueSettingFalse() {
|
||||
/* Create the field, and set the value as NULL */
|
||||
$field = new CheckboxField('IsChecked', 'Checked');
|
||||
$field->setValue(false);
|
||||
|
||||
/* dataValue() for the field is 0 */
|
||||
$this->assertEquals($field->dataValue(), 0, 'dataValue() returns a 0');
|
||||
|
||||
/* Value() returns 0 as well */
|
||||
$this->assertEquals($field->Value(), 0, 'Value() returns a 0');
|
||||
}
|
||||
|
||||
function testFieldValueWithoutSettingValue() {
|
||||
/* Create the field, but don't set any value on it */
|
||||
$field = new CheckboxField('IsChecked', 'Checked');
|
||||
|
||||
/* dataValue() for the field is 0 */
|
||||
$this->assertEquals($field->dataValue(), 0, 'dataValue() returns a 0');
|
||||
|
||||
/* Value() returns 0 as well */
|
||||
$this->assertEquals($field->Value(), 0, 'Value() returns a 0');
|
||||
}
|
||||
|
||||
function testSavingChecked() {
|
||||
/* Create a new test data record */
|
||||
$article = new CheckboxFieldTest_Article();
|
||||
|
||||
/* Create a field, with a value of 1 */
|
||||
$field = new CheckboxField('IsChecked', 'Checked', 1);
|
||||
|
||||
/* Save the field into our Article object */
|
||||
$field->saveInto($article);
|
||||
|
||||
/* Write the record to the test database */
|
||||
$article->write();
|
||||
|
||||
/* Check that IsChecked column contains a 1 */
|
||||
$this->assertEquals(
|
||||
DB::query("SELECT IsChecked FROM CheckboxFieldTest_Article")->value(),
|
||||
1,
|
||||
'We have a 1 set in the database, because the field saved into as a 1'
|
||||
);
|
||||
|
||||
/* Delete the record we tested */
|
||||
$article->delete();
|
||||
}
|
||||
|
||||
function testSavingUnchecked() {
|
||||
/* Create a new test data record */
|
||||
$article = new CheckboxFieldTest_Article();
|
||||
|
||||
/* Create a field, with no value */
|
||||
$field = new CheckboxField('IsChecked', 'Checked');
|
||||
|
||||
/* Save the field into our Article object */
|
||||
$field->saveInto($article);
|
||||
|
||||
/* Write the record to the test database */
|
||||
$article->write();
|
||||
|
||||
/* Check that IsChecked column contains a 0 */
|
||||
$this->assertEquals(
|
||||
DB::query("SELECT IsChecked FROM CheckboxFieldTest_Article")->value(),
|
||||
0,
|
||||
'We have a 0 set in the database, because the field saved into as a 0'
|
||||
);
|
||||
|
||||
/* Delete the record we tested */
|
||||
$article->delete();
|
||||
}
|
||||
|
||||
}
|
||||
class CheckboxFieldTest_Article extends DataObject implements TestOnly {
|
||||
|
||||
public static $db = array(
|
||||
'IsChecked' => 'Boolean'
|
||||
);
|
||||
|
||||
}
|
||||
?>
|
@ -1,5 +1,8 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package sapphire
|
||||
* @subpackage tests
|
||||
*/
|
||||
class CheckboxSetFieldTest extends SapphireTest {
|
||||
|
||||
static $fixture_file = 'sapphire/tests/forms/CheckboxSetFieldTest.yml';
|
||||
|
@ -10,7 +10,7 @@ class DropdownFieldTest extends SapphireTest {
|
||||
$dropdownField = new DropdownField('FeelingOk', 'Are you feeling ok?', array(0 => 'No', 1 => 'Yes'), '', null, '(Select one)');
|
||||
$dropdownField->addExtraClass('thisIsMyExtraClassForDropdownField');
|
||||
preg_match('/thisIsMyExtraClassForDropdownField/', $dropdownField->Field(), $matches);
|
||||
$this->assertTrue($matches[0] == 'thisIsMyExtraClassForDropdownField');
|
||||
$this->assertEquals($matches[0], 'thisIsMyExtraClassForDropdownField');
|
||||
}
|
||||
|
||||
function testGetSource() {
|
||||
@ -89,5 +89,127 @@ class DropdownFieldTest extends SapphireTest {
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function testNumberOfSelectOptionsAvailable() {
|
||||
/* Create a field with a blank value */
|
||||
$field = $this->testDropdownField('(Any)');
|
||||
|
||||
/* 3 options are available */
|
||||
$this->assertEquals(count($this->findOptionElements($field->Field())), 3, '3 options are available');
|
||||
|
||||
/* There is one selected option, since a dropdown can only possibly have one! */
|
||||
$selectedOptions = $this->findSelectedOptionElements($field->Field());
|
||||
$this->assertEquals(count($selectedOptions), 1, 'We only have 1 selected option, since a dropdown can only possibly have one!');
|
||||
|
||||
/* Create a field without a blank value */
|
||||
$field = $this->testDropdownField();
|
||||
|
||||
/* 2 options are available */
|
||||
$this->assertEquals(count($this->findOptionElements($field->Field())), 2, '2 options are available');
|
||||
|
||||
/* There are no selected options */
|
||||
$selectedOptions = $this->findSelectedOptionElements($field->Field());
|
||||
$this->assertEquals(count($selectedOptions), 0, 'There are no selected options');
|
||||
}
|
||||
|
||||
function testIntegerZeroValueSeelctedOptionBehaviour() {
|
||||
$field = $this->testDropdownField('(Any)', 0);
|
||||
$selectedOptions = $this->findSelectedOptionElements($field->Field());
|
||||
|
||||
/* The selected option is "No" */
|
||||
$this->assertEquals((string) $selectedOptions[0], 'No', 'The selected option is "No"');
|
||||
}
|
||||
|
||||
function testBlankStringValueSelectedOptionBehaviour() {
|
||||
$field = $this->testDropdownField('(Any)');
|
||||
$selectedOptions = $this->findSelectedOptionElements($field->Field());
|
||||
|
||||
/* The selected option is "(Any)" */
|
||||
$this->assertEquals((string) $selectedOptions[0], '(Any)', 'The selected option is "(Any)"');
|
||||
}
|
||||
|
||||
function testNullValueSelectedOptionBehaviour() {
|
||||
$field = $this->testDropdownField('(Any)', null);
|
||||
$selectedOptions = $this->findSelectedOptionElements($field->Field());
|
||||
|
||||
/* The selected option is "(Any)" */
|
||||
$this->assertEquals((string) $selectedOptions[0], '(Any)', 'The selected option is "(Any)"');
|
||||
}
|
||||
|
||||
function testStringValueSelectedOptionBehaviour() {
|
||||
$field = $this->testDropdownField('(Any)', '1');
|
||||
$selectedOptions = $this->findSelectedOptionElements($field->Field());
|
||||
|
||||
/* The selected option is "Yes" */
|
||||
$this->assertEquals((string) $selectedOptions[0], 'Yes', 'The selected option is "Yes"');
|
||||
|
||||
$field->setSource(array(
|
||||
'Cats' => 'Cats and Kittens',
|
||||
'Dogs' => 'Dogs and Puppies'
|
||||
));
|
||||
$field->setValue('Cats');
|
||||
|
||||
$selectedOptions = $this->findSelectedOptionElements($field->Field());
|
||||
|
||||
/* The selected option is "Cats and Kittens" */
|
||||
$this->assertEquals((string) $selectedOptions[0], 'Cats and Kittens', 'The selected option is "Cats and Kittens"');
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a test dropdown field, with the option to
|
||||
* set what source and blank value it should contain
|
||||
* as optional parameters.
|
||||
*
|
||||
* @param string|null $emptyString The text to display for the empty value
|
||||
* @param string|integer $value The default value of the field
|
||||
* @return DropdownField object
|
||||
*/
|
||||
function testDropdownField($emptyString = null, $value = '') {
|
||||
/* Set up source, with 0 and 1 integers as the values */
|
||||
$source = array(
|
||||
0 => 'No',
|
||||
1 => 'Yes'
|
||||
);
|
||||
|
||||
return new DropdownField('Field', null, $source, $value, null, $emptyString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all the <OPTION> elements from a
|
||||
* string of HTML.
|
||||
*
|
||||
* @param string $html HTML to scan for elements
|
||||
* @return SimpleXMLElement
|
||||
*/
|
||||
function findOptionElements($html) {
|
||||
$parser = new CSSContentParser($html);
|
||||
return $parser->getBySelector('option');
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all the <OPTION> elements from a
|
||||
* string of HTML that have the "selected"
|
||||
* attribute.
|
||||
*
|
||||
* @param string $html HTML to parse for elements
|
||||
* @return array of SimpleXMLElement objects
|
||||
*/
|
||||
function findSelectedOptionElements($html) {
|
||||
$options = $this->findOptionElements($html);
|
||||
|
||||
/* Find any elements that have the "selected" attribute and put them into a list */
|
||||
$foundSelected = array();
|
||||
foreach($options as $option) {
|
||||
$attributes = $option->attributes();
|
||||
if($attributes) foreach($attributes as $attribute => $value) {
|
||||
if($attribute == 'selected') {
|
||||
$foundSelected[] = $option;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $foundSelected;
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
@ -27,6 +27,8 @@ class GroupTest extends FunctionalTest {
|
||||
}
|
||||
|
||||
function testMemberGroupRelationForm() {
|
||||
Session::set('loggedInAs', $this->idFromFixture('GroupTest_Member', 'admin'));
|
||||
|
||||
$adminGroup = $this->fixture->objFromFixture('Group', 'admingroup');
|
||||
$parentGroup = $this->fixture->objFromFixture('Group', 'parentgroup');
|
||||
$childGroup = $this->fixture->objFromFixture('Group', 'childgroup');
|
||||
@ -42,6 +44,7 @@ class GroupTest extends FunctionalTest {
|
||||
));
|
||||
$form->saveInto($member);
|
||||
$updatedGroups = $member->Groups();
|
||||
|
||||
$controlGroups = new Member_GroupSet(
|
||||
$adminGroup,
|
||||
$parentGroup
|
||||
|
@ -23,3 +23,7 @@ GroupTest_Member:
|
||||
allgroupuser:
|
||||
FirstName: All Group User
|
||||
Groups: =>Group.admingroup,=>Group.parentgroup,=>Group.childgroup
|
||||
Permission:
|
||||
admincode:
|
||||
Code: ADMIN
|
||||
Group: =>Group.admingroup
|
Loading…
Reference in New Issue
Block a user