2008-08-09 07:00:42 +02:00
|
|
|
<?php
|
2010-04-12 04:03:16 +02:00
|
|
|
|
2008-08-09 07:00:42 +02:00
|
|
|
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
|
|
|
|
2010-04-12 04:03:16 +02:00
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'DataObjectDecoratorTest_Member',
|
|
|
|
'DataObjectDecoratorTest_Player',
|
|
|
|
'DataObjectDecoratorTest_RelatedObject',
|
|
|
|
'DataObjectDecoratorTest_MyObject',
|
|
|
|
);
|
|
|
|
|
2010-10-13 03:35:19 +02:00
|
|
|
protected $requiredExtensions = array(
|
|
|
|
'DataObject' => array( 'DataObjectDecoratorTest_AppliedToDO' ),
|
|
|
|
);
|
|
|
|
|
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();
|
2009-06-29 07:14:06 +02:00
|
|
|
|
|
|
|
$contactID = $contact->ID;
|
2008-08-09 07:00:42 +02:00
|
|
|
unset($contact);
|
2009-06-29 07:14:06 +02:00
|
|
|
unset($object);
|
2008-08-09 07:00:42 +02:00
|
|
|
|
2008-11-24 10:31:14 +01:00
|
|
|
$contact = DataObject::get_one("DataObjectDecoratorTest_Member", "\"Website\"='http://www.example.com'");
|
2009-06-29 07:14:06 +02:00
|
|
|
$object = DataObject::get_one('DataObjectDecoratorTest_RelatedObject', "\"ContactID\" = {$contactID}");
|
|
|
|
|
|
|
|
$this->assertNotNull($object, 'Related object not null');
|
2011-03-11 03:06:09 +01:00
|
|
|
$this->assertType('DataObjectDecoratorTest_Member', $object->Contact(), 'Related contact is a member dataobject');
|
|
|
|
$this->assertType('DataObjectDecoratorTest_Member', $object->getComponent('Contact'), 'getComponent does the same thing as Contact()');
|
2008-08-09 07:57:44 +02:00
|
|
|
|
2011-03-11 03:06:09 +01:00
|
|
|
$this->assertType('DataObjectDecoratorTest_RelatedObject', $contact->RelatedObjects()->First());
|
2008-08-09 07:00:42 +02:00
|
|
|
$this->assertEquals("Lorem ipsum dolor", $contact->RelatedObjects()->First()->FieldOne);
|
|
|
|
$this->assertEquals("Random notes", $contact->RelatedObjects()->First()->FieldTwo);
|
|
|
|
$contact->delete();
|
|
|
|
}
|
|
|
|
|
2009-06-19 02:11:05 +02:00
|
|
|
function testManyManyAssociationWithDecorator() {
|
|
|
|
$parent = new DataObjectDecoratorTest_MyObject();
|
|
|
|
$parent->Title = 'My Title';
|
|
|
|
$parent->write();
|
|
|
|
|
|
|
|
$this->assertEquals(0, $parent->Faves()->Count());
|
|
|
|
|
2011-03-23 04:32:24 +01:00
|
|
|
$obj1 = $this->objFromFixture('DataObjectDecoratorTest_RelatedObject', 'obj1');
|
|
|
|
$obj2 = $this->objFromFixture('DataObjectDecoratorTest_RelatedObject', 'obj2');
|
2009-06-19 02:11:05 +02:00
|
|
|
|
2011-03-23 04:32:24 +01:00
|
|
|
$parent->Faves()->add($obj1->ID);
|
2009-06-19 02:11:05 +02:00
|
|
|
$this->assertEquals(1, $parent->Faves()->Count());
|
|
|
|
|
2011-03-23 04:32:24 +01:00
|
|
|
$parent->Faves()->add($obj2->ID);
|
2009-06-19 02:11:05 +02:00
|
|
|
$this->assertEquals(2, $parent->Faves()->Count());
|
|
|
|
|
2011-03-23 04:32:24 +01:00
|
|
|
$parent->Faves()->remove($obj2->ID);
|
2009-06-19 02:11:05 +02:00
|
|
|
$this->assertEquals(1, $parent->Faves()->Count());
|
|
|
|
}
|
|
|
|
|
2009-06-17 11:24:02 +02:00
|
|
|
/**
|
|
|
|
* Test {@link Object::add_extension()} has loaded DataObjectDecorator statics correctly.
|
|
|
|
*/
|
|
|
|
function testAddExtensionLoadsStatics() {
|
|
|
|
// Object::add_extension() will load DOD statics directly, so let's try adding a decorator on the fly
|
|
|
|
Object::add_extension('DataObjectDecoratorTest_Player', 'DataObjectDecoratorTest_PlayerDecorator');
|
|
|
|
|
|
|
|
// Now that we've just added the decorator, we need to rebuild the database
|
2010-04-12 04:03:16 +02:00
|
|
|
$this->resetDBSchema(true);
|
2009-06-17 11:24:02 +02:00
|
|
|
|
|
|
|
// Create a test record with decorated fields, writing to the DB
|
|
|
|
$player = new DataObjectDecoratorTest_Player();
|
|
|
|
$player->setField('Name', 'Joe');
|
|
|
|
$player->setField('DateBirth', '1990-5-10');
|
|
|
|
$player->Address = '123 somewhere street';
|
|
|
|
$player->write();
|
|
|
|
|
|
|
|
unset($player);
|
|
|
|
|
|
|
|
// Pull the record out of the DB and examine the decorated fields
|
|
|
|
$player = DataObject::get_one('DataObjectDecoratorTest_Player', "\"Name\" = 'Joe'");
|
|
|
|
$this->assertEquals($player->DateBirth, '1990-05-10');
|
|
|
|
$this->assertEquals($player->Address, '123 somewhere street');
|
|
|
|
$this->assertEquals($player->Status, 'Goalie');
|
|
|
|
}
|
|
|
|
|
2009-05-26 02:52:54 +02:00
|
|
|
/**
|
|
|
|
* Test that DataObject::$api_access can be set to true via a decorator
|
|
|
|
*/
|
|
|
|
function testApiAccessCanBeDecorated() {
|
|
|
|
$this->assertTrue(Object::get_static('DataObjectDecoratorTest_Member', 'api_access'));
|
|
|
|
}
|
|
|
|
|
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'
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-03-16 14:47:52 +01:00
|
|
|
function testPopulateDefaults() {
|
|
|
|
$obj = new DataObjectDecoratorTest_Member();
|
|
|
|
$this->assertEquals(
|
|
|
|
$obj->Phone,
|
|
|
|
'123',
|
|
|
|
'Defaults can be populated through decorator'
|
|
|
|
);
|
|
|
|
}
|
2009-04-27 02:44:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that DataObject::dbObject() works for fields applied by a decorator
|
|
|
|
*/
|
|
|
|
function testDbObjectOnDecoratedFields() {
|
|
|
|
$member = $this->objFromFixture('DataObjectDecoratorTest_Member', 'member1');
|
|
|
|
$this->assertNotNull($member->dbObject('Website'));
|
2011-03-11 03:06:09 +01:00
|
|
|
$this->assertType('Varchar', $member->dbObject('Website'));
|
2009-04-27 02:44:10 +02:00
|
|
|
}
|
2010-10-13 03:35:19 +02:00
|
|
|
|
|
|
|
function testDecoratorCanBeAppliedToDataObject() {
|
|
|
|
$do = new DataObject();
|
|
|
|
$mo = new DataObjectDecoratorTest_MyObject();
|
|
|
|
|
|
|
|
$this->assertTrue($do->hasMethod('testMethodApplied'));
|
|
|
|
$this->assertTrue($mo->hasMethod('testMethodApplied'));
|
|
|
|
|
|
|
|
$this->assertEquals("hello world", $mo->testMethodApplied());
|
|
|
|
$this->assertEquals("hello world", $do->testMethodApplied());
|
|
|
|
}
|
2008-08-09 07:00:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class DataObjectDecoratorTest_Member extends DataObject implements TestOnly {
|
|
|
|
|
|
|
|
static $db = array(
|
2009-05-07 08:00:50 +02:00
|
|
|
"Name" => "Varchar",
|
|
|
|
"Email" => "Varchar"
|
2008-08-09 07:00:42 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-06-17 11:24:02 +02:00
|
|
|
class DataObjectDecoratorTest_Player extends DataObject implements TestOnly {
|
|
|
|
|
|
|
|
static $db = array(
|
|
|
|
'Name' => 'Varchar'
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class DataObjectDecoratorTest_PlayerDecorator extends DataObjectDecorator implements TestOnly {
|
|
|
|
|
2010-04-12 23:58:35 +02:00
|
|
|
function extraStatics($class) {
|
|
|
|
// Only add these extensions if the $class is set to DataObjectDecoratorTest_Player, to
|
|
|
|
// test that the argument works.
|
|
|
|
if($class == 'DataObjectDecoratorTest_Player') {
|
|
|
|
return array(
|
|
|
|
'db' => array(
|
|
|
|
'Address' => 'Text',
|
|
|
|
'DateBirth' => 'Date',
|
|
|
|
'Status' => "Enum('Shooter,Goalie')"
|
|
|
|
),
|
|
|
|
'defaults' => array(
|
|
|
|
'Status' => 'Goalie'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2009-06-17 11:24:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-08-09 07:00:42 +02:00
|
|
|
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(
|
2009-05-07 08:00:50 +02:00
|
|
|
'Website' => 'Varchar',
|
2008-08-09 07:00:42 +02:00
|
|
|
'Phone' => 'Varchar(255)',
|
|
|
|
),
|
|
|
|
'has_many' => array(
|
|
|
|
'RelatedObjects' => 'DataObjectDecoratorTest_RelatedObject'
|
2009-03-16 14:47:52 +01:00
|
|
|
),
|
|
|
|
'defaults' => array(
|
|
|
|
'Phone' => '123'
|
2009-05-26 02:52:54 +02:00
|
|
|
),
|
|
|
|
'api_access' => true,
|
2008-08-09 07:00:42 +02:00
|
|
|
);
|
|
|
|
}
|
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(
|
2009-05-07 08:00:50 +02:00
|
|
|
"FieldOne" => "Varchar",
|
|
|
|
"FieldTwo" => "Varchar"
|
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) {
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-06-19 02:11:05 +02:00
|
|
|
class DataObjectDecoratorTest_Faves extends DataObjectDecorator implements TestOnly {
|
2009-06-19 02:58:02 +02:00
|
|
|
public function extraStatics() {
|
2009-06-19 02:11:05 +02:00
|
|
|
return array(
|
|
|
|
'many_many' => array(
|
2011-03-23 04:32:24 +01:00
|
|
|
'Faves' => 'DataObjectDecoratorTest_RelatedObject'
|
2009-06-19 02:11:05 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-13 03:35:19 +02:00
|
|
|
class DataObjectDecoratorTest_AppliedToDO extends DataObjectDecorator implements TestOnly {
|
|
|
|
public function testMethodApplied() {
|
|
|
|
return "hello world";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-07 13:18:35 +01:00
|
|
|
DataObject::add_extension('DataObjectDecoratorTest_MyObject', 'DataObjectDecoratorTest_Ext1');
|
|
|
|
DataObject::add_extension('DataObjectDecoratorTest_MyObject', 'DataObjectDecoratorTest_Ext2');
|
2009-06-19 02:11:05 +02:00
|
|
|
DataObject::add_extension('DataObjectDecoratorTest_MyObject', 'DataObjectDecoratorTest_Faves');
|
2009-06-19 02:58:02 +02:00
|
|
|
?>
|