2008-08-09 07:00:42 +02:00
|
|
|
<?php
|
2010-04-12 04:03:16 +02:00
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
class DataExtensionTest extends SapphireTest {
|
|
|
|
static $fixture_file = 'DataExtensionTest.yml';
|
2008-08-09 07:00:42 +02:00
|
|
|
|
2010-04-12 04:03:16 +02:00
|
|
|
protected $extraDataObjects = array(
|
2011-04-15 11:35:30 +02:00
|
|
|
'DataExtensionTest_Member',
|
|
|
|
'DataExtensionTest_Player',
|
|
|
|
'DataExtensionTest_RelatedObject',
|
|
|
|
'DataExtensionTest_MyObject',
|
2010-04-12 04:03:16 +02:00
|
|
|
);
|
|
|
|
|
2010-10-13 03:35:19 +02:00
|
|
|
protected $requiredExtensions = array(
|
2011-04-15 11:35:30 +02:00
|
|
|
'DataObject' => array( 'DataExtensionTest_AppliedToDO' ),
|
2010-10-13 03:35:19 +02:00
|
|
|
);
|
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
function testOneToManyAssociationWithExtension() {
|
|
|
|
$contact = new DataExtensionTest_Member();
|
2008-08-09 07:00:42 +02:00
|
|
|
$contact->Website = "http://www.example.com";
|
2008-08-09 07:57:44 +02:00
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
$object = new DataExtensionTest_RelatedObject();
|
2008-08-09 07:00:42 +02:00
|
|
|
$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
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
$contact = DataObject::get_one("DataExtensionTest_Member", "\"Website\"='http://www.example.com'");
|
|
|
|
$object = DataObject::get_one('DataExtensionTest_RelatedObject', "\"ContactID\" = {$contactID}");
|
2009-06-29 07:14:06 +02:00
|
|
|
|
|
|
|
$this->assertNotNull($object, 'Related object not null');
|
2012-05-09 12:43:22 +02:00
|
|
|
$this->assertInstanceOf('DataExtensionTest_Member', $object->Contact(), 'Related contact is a member dataobject');
|
|
|
|
$this->assertInstanceOf('DataExtensionTest_Member', $object->getComponent('Contact'), 'getComponent does the same thing as Contact()');
|
2008-08-09 07:57:44 +02:00
|
|
|
|
2012-05-09 12:43:22 +02:00
|
|
|
$this->assertInstanceOf('DataExtensionTest_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();
|
|
|
|
}
|
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
function testManyManyAssociationWithExtension() {
|
|
|
|
$parent = new DataExtensionTest_MyObject();
|
2009-06-19 02:11:05 +02:00
|
|
|
$parent->Title = 'My Title';
|
|
|
|
$parent->write();
|
|
|
|
|
|
|
|
$this->assertEquals(0, $parent->Faves()->Count());
|
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
$obj1 = $this->objFromFixture('DataExtensionTest_RelatedObject', 'obj1');
|
|
|
|
$obj2 = $this->objFromFixture('DataExtensionTest_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-30 03:19:27 +02:00
|
|
|
$parent->Faves()->removeByID($obj2->ID);
|
2009-06-19 02:11:05 +02:00
|
|
|
$this->assertEquals(1, $parent->Faves()->Count());
|
|
|
|
}
|
|
|
|
|
2009-06-17 11:24:02 +02:00
|
|
|
/**
|
2011-04-15 11:35:30 +02:00
|
|
|
* Test {@link Object::add_extension()} has loaded DataExtension statics correctly.
|
2009-06-17 11:24:02 +02:00
|
|
|
*/
|
|
|
|
function testAddExtensionLoadsStatics() {
|
2011-04-15 11:35:30 +02:00
|
|
|
// Object::add_extension() will load DOD statics directly, so let's try adding a extension on the fly
|
|
|
|
Object::add_extension('DataExtensionTest_Player', 'DataExtensionTest_PlayerExtension');
|
2009-06-17 11:24:02 +02:00
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
// Now that we've just added the extension, 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
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
// Create a test record with extended fields, writing to the DB
|
|
|
|
$player = new DataExtensionTest_Player();
|
2009-06-17 11:24:02 +02:00
|
|
|
$player->setField('Name', 'Joe');
|
|
|
|
$player->setField('DateBirth', '1990-5-10');
|
|
|
|
$player->Address = '123 somewhere street';
|
|
|
|
$player->write();
|
2012-04-20 05:41:51 +02:00
|
|
|
|
2009-06-17 11:24:02 +02:00
|
|
|
unset($player);
|
2012-04-20 05:41:51 +02:00
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
// Pull the record out of the DB and examine the extended fields
|
|
|
|
$player = DataObject::get_one('DataExtensionTest_Player', "\"Name\" = 'Joe'");
|
2009-06-17 11:24:02 +02:00
|
|
|
$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
|
|
|
/**
|
2011-04-15 11:35:30 +02:00
|
|
|
* Test that DataObject::$api_access can be set to true via a extension
|
2009-05-26 02:52:54 +02:00
|
|
|
*/
|
2011-04-15 11:35:30 +02:00
|
|
|
function testApiAccessCanBeExtended() {
|
2012-04-18 13:55:37 +02:00
|
|
|
$this->assertTrue(Config::inst()->get('DataExtensionTest_Member', 'api_access', Config::FIRST_SET));
|
2009-05-26 02:52:54 +02:00
|
|
|
}
|
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
function testPermissionExtension() {
|
2008-11-07 13:18:35 +01:00
|
|
|
// testing behaviour in isolation, too many sideeffects and other checks
|
|
|
|
// in SiteTree->can*() methods to test one single feature reliably with them
|
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
$obj = $this->objFromFixture('DataExtensionTest_MyObject', 'object1');
|
2008-11-07 13:18:35 +01:00
|
|
|
$websiteuser = $this->objFromFixture('Member', 'websiteuser');
|
|
|
|
$admin = $this->objFromFixture('Member', 'admin');
|
|
|
|
|
|
|
|
$this->assertFalse(
|
|
|
|
$obj->canOne($websiteuser),
|
2011-04-15 11:35:30 +02:00
|
|
|
'Both extensions return true, but original method returns false'
|
2008-11-07 13:18:35 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertFalse(
|
|
|
|
$obj->canTwo($websiteuser),
|
2011-04-15 11:35:30 +02:00
|
|
|
'One extension returns false, original returns true, but extension takes precedence'
|
2008-11-07 13:18:35 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
$obj->canThree($admin),
|
2011-04-15 11:35:30 +02:00
|
|
|
'Undefined extension methods returning NULL dont influence the original method'
|
2008-11-07 13:18:35 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-03-16 14:47:52 +01:00
|
|
|
function testPopulateDefaults() {
|
2011-04-15 11:35:30 +02:00
|
|
|
$obj = new DataExtensionTest_Member();
|
2009-03-16 14:47:52 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
$obj->Phone,
|
|
|
|
'123',
|
2011-04-15 11:35:30 +02:00
|
|
|
'Defaults can be populated through extension'
|
2009-03-16 14:47:52 +01:00
|
|
|
);
|
|
|
|
}
|
2009-04-27 02:44:10 +02:00
|
|
|
|
|
|
|
/**
|
2011-04-15 11:35:30 +02:00
|
|
|
* Test that DataObject::dbObject() works for fields applied by a extension
|
2009-04-27 02:44:10 +02:00
|
|
|
*/
|
2011-04-15 11:35:30 +02:00
|
|
|
function testDbObjectOnExtendedFields() {
|
|
|
|
$member = $this->objFromFixture('DataExtensionTest_Member', 'member1');
|
2009-04-27 02:44:10 +02:00
|
|
|
$this->assertNotNull($member->dbObject('Website'));
|
2012-05-09 12:43:22 +02:00
|
|
|
$this->assertInstanceOf('Varchar', $member->dbObject('Website'));
|
2009-04-27 02:44:10 +02:00
|
|
|
}
|
2010-10-13 03:35:19 +02:00
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
function testExtensionCanBeAppliedToDataObject() {
|
2010-10-13 03:35:19 +02:00
|
|
|
$do = new DataObject();
|
2011-04-15 11:35:30 +02:00
|
|
|
$mo = new DataExtensionTest_MyObject();
|
2010-10-13 03:35:19 +02:00
|
|
|
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
class DataExtensionTest_Member extends DataObject implements TestOnly {
|
2008-08-09 07:00:42 +02:00
|
|
|
|
|
|
|
static $db = array(
|
2009-05-07 08:00:50 +02:00
|
|
|
"Name" => "Varchar",
|
|
|
|
"Email" => "Varchar"
|
2008-08-09 07:00:42 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
class DataExtensionTest_Player extends DataObject implements TestOnly {
|
2009-06-17 11:24:02 +02:00
|
|
|
|
|
|
|
static $db = array(
|
|
|
|
'Name' => 'Varchar'
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
class DataExtensionTest_PlayerExtension extends DataExtension implements TestOnly {
|
2009-06-17 11:24:02 +02:00
|
|
|
|
2012-04-18 22:30:12 +02:00
|
|
|
public static function add_to_class($class = null, $extensionClass = null, $args = null) {
|
2011-04-15 11:35:30 +02:00
|
|
|
// Only add these extensions if the $class is set to DataExtensionTest_Player, to
|
2010-04-12 23:58:35 +02:00
|
|
|
// test that the argument works.
|
2011-04-15 11:35:30 +02:00
|
|
|
if($class == 'DataExtensionTest_Player') {
|
2012-04-20 05:41:51 +02:00
|
|
|
Config::inst()->update($class, 'db', array(
|
|
|
|
'Address' => 'Text',
|
|
|
|
'DateBirth' => 'Date',
|
|
|
|
'Status' => "Enum('Shooter,Goalie')"
|
|
|
|
));
|
|
|
|
Config::inst()->update($class, 'defaults', array(
|
|
|
|
'Status' => 'Goalie'
|
|
|
|
));
|
2010-04-12 23:58:35 +02:00
|
|
|
}
|
2009-06-17 11:24:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
class DataExtensionTest_ContactRole extends DataExtension implements TestOnly {
|
2012-04-18 22:30:12 +02:00
|
|
|
|
2012-04-20 05:41:51 +02:00
|
|
|
public static $db = array(
|
|
|
|
'Website' => 'Varchar',
|
|
|
|
'Phone' => 'Varchar(255)',
|
2012-04-18 22:30:12 +02:00
|
|
|
);
|
|
|
|
|
2012-04-20 05:41:51 +02:00
|
|
|
public static $has_many = array(
|
|
|
|
'RelatedObjects' => 'DataExtensionTest_RelatedObject'
|
|
|
|
);
|
|
|
|
|
|
|
|
public static $defaults = array(
|
|
|
|
'Phone' => '123'
|
|
|
|
);
|
|
|
|
|
|
|
|
public static $api_access = true;
|
|
|
|
|
2008-08-09 07:00:42 +02:00
|
|
|
}
|
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
class DataExtensionTest_RelatedObject extends DataObject implements TestOnly {
|
2008-08-09 07:00:42 +02:00
|
|
|
|
|
|
|
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(
|
2011-04-15 11:35:30 +02:00
|
|
|
"Contact" => "DataExtensionTest_Member"
|
2008-08-09 07:00:42 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
DataObject::add_extension('DataExtensionTest_Member', 'DataExtensionTest_ContactRole');
|
2008-11-07 13:18:35 +01:00
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
class DataExtensionTest_MyObject extends DataObject implements TestOnly {
|
2008-11-07 13:18:35 +01:00
|
|
|
|
|
|
|
static $db = array(
|
|
|
|
'Title' => 'Varchar',
|
|
|
|
);
|
|
|
|
|
|
|
|
function canOne($member = null) {
|
2011-04-15 11:35:30 +02:00
|
|
|
// extended access checks
|
2008-11-07 13:18:35 +01:00
|
|
|
$results = $this->extend('canOne', $member);
|
|
|
|
if($results && is_array($results)) if(!min($results)) return false;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function canTwo($member = null) {
|
2011-04-15 11:35:30 +02:00
|
|
|
// extended access checks
|
2008-11-07 13:18:35 +01:00
|
|
|
$results = $this->extend('canTwo', $member);
|
|
|
|
if($results && is_array($results)) if(!min($results)) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function canThree($member = null) {
|
2011-04-15 11:35:30 +02:00
|
|
|
// extended access checks
|
2008-11-07 13:18:35 +01:00
|
|
|
$results = $this->extend('canThree', $member);
|
|
|
|
if($results && is_array($results)) if(!min($results)) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
class DataExtensionTest_Ext1 extends DataExtension implements TestOnly {
|
2008-11-07 13:18:35 +01:00
|
|
|
|
|
|
|
function canOne($member = null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function canTwo($member = null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function canThree($member = null) {
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
class DataExtensionTest_Ext2 extends DataExtension implements TestOnly {
|
2008-11-07 13:18:35 +01:00
|
|
|
|
|
|
|
function canOne($member = null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function canTwo($member = null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function canThree($member = null) {
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
class DataExtensionTest_Faves extends DataExtension implements TestOnly {
|
2012-04-18 22:30:12 +02:00
|
|
|
|
|
|
|
public static $many_many = array(
|
|
|
|
'Faves' => 'DataExtensionTest_RelatedObject'
|
|
|
|
);
|
|
|
|
|
2009-06-19 02:11:05 +02:00
|
|
|
}
|
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
class DataExtensionTest_AppliedToDO extends DataExtension implements TestOnly {
|
2012-04-18 22:30:12 +02:00
|
|
|
|
2010-10-13 03:35:19 +02:00
|
|
|
public function testMethodApplied() {
|
|
|
|
return "hello world";
|
|
|
|
}
|
2012-04-18 22:30:12 +02:00
|
|
|
|
2010-10-13 03:35:19 +02:00
|
|
|
}
|
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
DataObject::add_extension('DataExtensionTest_MyObject', 'DataExtensionTest_Ext1');
|
|
|
|
DataObject::add_extension('DataExtensionTest_MyObject', 'DataExtensionTest_Ext2');
|
|
|
|
DataObject::add_extension('DataExtensionTest_MyObject', 'DataExtensionTest_Faves');
|
2012-02-12 21:22:11 +01:00
|
|
|
|