mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
MINOR Merged in DataObjectDecorator tests from trunk
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@83720 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
d838529953
commit
bd28434884
@ -20,10 +20,17 @@ class DataObjectDecoratorTest extends SapphireTest {
|
||||
$contact->write();
|
||||
$object->ContactID = $contact->ID;
|
||||
$object->write();
|
||||
|
||||
|
||||
$contactID = $contact->ID;
|
||||
unset($contact);
|
||||
unset($object);
|
||||
|
||||
$contact = DataObject::get_one("DataObjectDecoratorTest_Member", "Website='http://www.example.com'");
|
||||
$contact = DataObject::get_one("DataObjectDecoratorTest_Member", "Website = 'http://www.example.com'");
|
||||
$object = DataObject::get_one('DataObjectDecoratorTest_RelatedObject', "ContactID = {$contactID}");
|
||||
|
||||
$this->assertNotNull($object, 'Related object not null');
|
||||
$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()');
|
||||
|
||||
$this->assertType('DataObjectDecoratorTest_RelatedObject', $contact->RelatedObjects()->First());
|
||||
$this->assertEquals("Lorem ipsum dolor", $contact->RelatedObjects()->First()->FieldOne);
|
||||
@ -31,6 +38,53 @@ class DataObjectDecoratorTest extends SapphireTest {
|
||||
$contact->delete();
|
||||
}
|
||||
|
||||
function testManyManyAssociationWithDecorator() {
|
||||
$parent = new DataObjectDecoratorTest_MyObject();
|
||||
$parent->Title = 'My Title';
|
||||
$parent->write();
|
||||
|
||||
$this->assertEquals(0, $parent->Faves()->Count());
|
||||
|
||||
$homepage = $this->objFromFixture('Page', 'home');
|
||||
$firstpage = $this->objFromFixture('Page', 'page1');
|
||||
|
||||
$parent->Faves()->add($homepage->ID);
|
||||
$this->assertEquals(1, $parent->Faves()->Count());
|
||||
|
||||
$parent->Faves()->add($firstpage->ID);
|
||||
$this->assertEquals(2, $parent->Faves()->Count());
|
||||
|
||||
$parent->Faves()->remove($firstpage->ID);
|
||||
$this->assertEquals(1, $parent->Faves()->Count());
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
$da = new DatabaseAdmin();
|
||||
$da->doBuild(true, false, true);
|
||||
|
||||
// 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');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that DataObject::$api_access can be set to true via a decorator
|
||||
*/
|
||||
@ -82,6 +136,31 @@ class DataObjectDecoratorTest_Member extends DataObject implements TestOnly {
|
||||
|
||||
}
|
||||
|
||||
class DataObjectDecoratorTest_Player extends DataObject implements TestOnly {
|
||||
|
||||
static $db = array(
|
||||
'Name' => 'Varchar'
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
class DataObjectDecoratorTest_PlayerDecorator extends DataObjectDecorator implements TestOnly {
|
||||
|
||||
function extraStatics() {
|
||||
return array(
|
||||
'db' => array(
|
||||
'Address' => 'Text',
|
||||
'DateBirth' => 'Date',
|
||||
'Status' => "Enum('Shooter,Goalie')"
|
||||
),
|
||||
'defaults' => array(
|
||||
'Status' => 'Goalie'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class DataObjectDecoratorTest_ContactRole extends DataObjectDecorator implements TestOnly {
|
||||
|
||||
function extraStatics() {
|
||||
@ -188,4 +267,4 @@ class DataObjectDecoratorTest_Faves extends DataObjectDecorator implements TestO
|
||||
DataObject::add_extension('DataObjectDecoratorTest_MyObject', 'DataObjectDecoratorTest_Ext1');
|
||||
DataObject::add_extension('DataObjectDecoratorTest_MyObject', 'DataObjectDecoratorTest_Ext2');
|
||||
DataObject::add_extension('DataObjectDecoratorTest_MyObject', 'DataObjectDecoratorTest_Faves');
|
||||
?>
|
||||
?>
|
||||
|
Loading…
x
Reference in New Issue
Block a user