2008-08-09 07:00:42 +02:00
|
|
|
<?php
|
|
|
|
class DataObjectDecoratorTest extends SapphireTest {
|
2008-11-07 13:18:35 +01:00
|
|
|
static $fixture_file = 'sapphire/tests/DataObjectDecoratorTest.yml';
|
2008-08-09 07:00:42 +02:00
|
|
|
|
|
|
|
function testOneToManyAssociationWithDecorator() {
|
2008-08-09 09:03:24 +02:00
|
|
|
// Fails in RestfulServerTest
|
|
|
|
// Error: Object::__call() Method 'RelatedObjects' not found in class 'RestfulServerTest_Comment'
|
2008-08-09 07:00:42 +02:00
|
|
|
$contact = new DataObjectDecoratorTest_Member();
|
|
|
|
$contact->Website = "http://www.example.com";
|
2008-08-09 07:57:44 +02:00
|
|
|
|
2008-08-09 07:00:42 +02:00
|
|
|
$object = new DataObjectDecoratorTest_RelatedObject();
|
|
|
|
$object->FieldOne = "Lorem ipsum dolor";
|
|
|
|
$object->FieldTwo = "Random notes";
|
2008-08-09 07:57:44 +02:00
|
|
|
|
2008-08-09 09:03:24 +02:00
|
|
|
// The following code doesn't currently work:
|
|
|
|
// $contact->RelatedObjects()->add($object);
|
|
|
|
// $contact->write();
|
2008-08-09 07:57:44 +02:00
|
|
|
|
2008-08-09 09:03:24 +02:00
|
|
|
// Instead we have to do the following
|
2008-08-09 07:57:44 +02:00
|
|
|
$contact->write();
|
|
|
|
$object->ContactID = $contact->ID;
|
|
|
|
$object->write();
|
|
|
|
|
2008-08-09 07:00:42 +02:00
|
|
|
unset($contact);
|
|
|
|
|
|
|
|
$contact = DataObject::get_one("DataObjectDecoratorTest_Member", "Website='http://www.example.com'");
|
2008-08-09 07:57:44 +02:00
|
|
|
|
2008-08-09 07:00:42 +02:00
|
|
|
$this->assertType('DataObjectDecoratorTest_RelatedObject', $contact->RelatedObjects()->First());
|
|
|
|
$this->assertEquals("Lorem ipsum dolor", $contact->RelatedObjects()->First()->FieldOne);
|
|
|
|
$this->assertEquals("Random notes", $contact->RelatedObjects()->First()->FieldTwo);
|
|
|
|
$contact->delete();
|
|
|
|
}
|
|
|
|
|
2008-11-07 13:18:35 +01:00
|
|
|
function testPermissionDecoration() {
|
|
|
|
// testing behaviour in isolation, too many sideeffects and other checks
|
|
|
|
// in SiteTree->can*() methods to test one single feature reliably with them
|
|
|
|
|
|
|
|
$obj = $this->objFromFixture('DataObjectDecoratorTest_MyObject', 'object1');
|
|
|
|
$websiteuser = $this->objFromFixture('Member', 'websiteuser');
|
|
|
|
$admin = $this->objFromFixture('Member', 'admin');
|
|
|
|
|
|
|
|
$this->assertFalse(
|
|
|
|
$obj->canOne($websiteuser),
|
|
|
|
'Both decorators return true, but original method returns false'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertFalse(
|
|
|
|
$obj->canTwo($websiteuser),
|
|
|
|
'One decorator returns false, original returns true, but decorator takes precedence'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
$obj->canThree($admin),
|
|
|
|
'Undefined decorator methods returning NULL dont influence the original method'
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-08-09 07:00:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class DataObjectDecoratorTest_Member extends DataObject implements TestOnly {
|
|
|
|
|
|
|
|
static $db = array(
|
|
|
|
"Name" => "Text",
|
|
|
|
"Email" => "Text"
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class DataObjectDecoratorTest_ContactRole extends DataObjectDecorator implements TestOnly {
|
|
|
|
|
2008-11-02 01:36:57 +01:00
|
|
|
function extraStatics() {
|
2008-08-09 07:00:42 +02:00
|
|
|
return array(
|
|
|
|
'db' => array(
|
|
|
|
'Website' => 'Text',
|
|
|
|
'Phone' => 'Varchar(255)',
|
|
|
|
),
|
|
|
|
'has_many' => array(
|
|
|
|
'RelatedObjects' => 'DataObjectDecoratorTest_RelatedObject'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2008-11-07 13:18:35 +01:00
|
|
|
|
2008-08-09 07:00:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class DataObjectDecoratorTest_RelatedObject extends DataObject implements TestOnly {
|
|
|
|
|
|
|
|
static $db = array(
|
|
|
|
"FieldOne" => "Text",
|
2008-08-09 07:57:44 +02:00
|
|
|
"FieldTwo" => "Text"
|
2008-08-09 07:00:42 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
static $has_one = array(
|
2008-08-09 07:57:44 +02:00
|
|
|
"Contact" => "DataObjectDecoratorTest_Member"
|
2008-08-09 07:00:42 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-08-11 08:01:16 +02:00
|
|
|
DataObject::add_extension('DataObjectDecoratorTest_Member', 'DataObjectDecoratorTest_ContactRole');
|
2008-11-07 13:18:35 +01:00
|
|
|
|
|
|
|
class DataObjectDecoratorTest_MyObject extends DataObject implements TestOnly {
|
|
|
|
|
|
|
|
static $db = array(
|
|
|
|
'Title' => 'Varchar',
|
|
|
|
);
|
|
|
|
|
|
|
|
function canOne($member = null) {
|
|
|
|
// decorated access checks
|
|
|
|
$results = $this->extend('canOne', $member);
|
|
|
|
if($results && is_array($results)) if(!min($results)) return false;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function canTwo($member = null) {
|
|
|
|
// decorated access checks
|
|
|
|
$results = $this->extend('canTwo', $member);
|
|
|
|
if($results && is_array($results)) if(!min($results)) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function canThree($member = null) {
|
|
|
|
// decorated access checks
|
|
|
|
$results = $this->extend('canThree', $member);
|
|
|
|
if($results && is_array($results)) if(!min($results)) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class DataObjectDecoratorTest_Ext1 extends DataObjectDecorator implements TestOnly {
|
|
|
|
|
|
|
|
function canOne($member = null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function canTwo($member = null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function canThree($member = null) {
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class DataObjectDecoratorTest_Ext2 extends DataObjectDecorator implements TestOnly {
|
|
|
|
|
|
|
|
function canOne($member = null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function canTwo($member = null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function canThree($member = null) {
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
DataObject::add_extension('DataObjectDecoratorTest_MyObject', 'DataObjectDecoratorTest_Ext1');
|
|
|
|
DataObject::add_extension('DataObjectDecoratorTest_MyObject', 'DataObjectDecoratorTest_Ext2');
|
2008-08-09 07:00:42 +02:00
|
|
|
?>
|