2008-08-09 07:00:42 +02:00
|
|
|
<?php
|
2010-04-12 04:03:16 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\ORM\Tests;
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\ORM\Tests\DataExtensionTest\TestMember;
|
|
|
|
use SilverStripe\Security\Member;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
class DataExtensionTest extends SapphireTest {
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = 'DataExtensionTest.yml';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 04:03:16 +02:00
|
|
|
protected $extraDataObjects = array(
|
2016-10-14 03:30:05 +02:00
|
|
|
DataExtensionTest\TestMember::class,
|
|
|
|
DataExtensionTest\Player::class,
|
|
|
|
DataExtensionTest\RelatedObject::class,
|
|
|
|
DataExtensionTest\MyObject::class,
|
|
|
|
DataExtensionTest\CMSFieldsBase::class,
|
|
|
|
DataExtensionTest\CMSFieldsChild::class,
|
|
|
|
DataExtensionTest\CMSFieldsGrandChild::class
|
2010-04-12 04:03:16 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
protected $requiredExtensions = [
|
|
|
|
DataObject::class => [
|
|
|
|
DataExtensionTest\AppliedToDO::class,
|
|
|
|
]
|
|
|
|
];
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testOneToManyAssociationWithExtension() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$contact = new DataExtensionTest\TestMember();
|
2008-08-09 07:00:42 +02:00
|
|
|
$contact->Website = "http://www.example.com";
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$object = new DataExtensionTest\RelatedObject();
|
2008-08-09 07:00:42 +02:00
|
|
|
$object->FieldOne = "Lorem ipsum dolor";
|
|
|
|
$object->FieldTwo = "Random notes";
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 09:03:24 +02:00
|
|
|
// The following code doesn't currently work:
|
|
|
|
// $contact->RelatedObjects()->add($object);
|
|
|
|
// $contact->write();
|
2014-08-15 08:53:05 +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);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$contact = DataObject::get_one(DataExtensionTest\TestMember::class, array(
|
2013-06-21 00:32:08 +02:00
|
|
|
'"DataExtensionTest_Member"."Website"' => 'http://www.example.com'
|
|
|
|
));
|
2016-10-14 03:30:05 +02:00
|
|
|
$object = DataObject::get_one(DataExtensionTest\RelatedObject::class, array(
|
2013-06-21 00:32:08 +02:00
|
|
|
'"DataExtensionTest_RelatedObject"."ContactID"' => $contactID
|
|
|
|
));
|
2009-06-29 07:14:06 +02:00
|
|
|
|
|
|
|
$this->assertNotNull($object, 'Related object not null');
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertInstanceOf(DataExtensionTest\TestMember::class, $object->Contact(),
|
2012-09-26 23:34:00 +02:00
|
|
|
'Related contact is a member dataobject');
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertInstanceOf(DataExtensionTest\TestMember::class, $object->getComponent('Contact'),
|
2012-09-26 23:34:00 +02:00
|
|
|
'getComponent does the same thing as Contact()');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertInstanceOf(DataExtensionTest\RelatedObject::class, $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();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testManyManyAssociationWithExtension() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$parent = new DataExtensionTest\MyObject();
|
2009-06-19 02:11:05 +02:00
|
|
|
$parent->Title = 'My Title';
|
|
|
|
$parent->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-19 02:11:05 +02:00
|
|
|
$this->assertEquals(0, $parent->Faves()->Count());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$obj1 = $this->objFromFixture(DataExtensionTest\RelatedObject::class, 'obj1');
|
|
|
|
$obj2 = $this->objFromFixture(DataExtensionTest\RelatedObject::class, '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());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public 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
|
2016-10-14 03:30:05 +02:00
|
|
|
DataExtensionTest\Player::add_extension('DataExtensionTest_PlayerExtension');
|
2014-08-15 08:53:05 +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);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
// Create a test record with extended fields, writing to the DB
|
2016-10-14 03:30:05 +02:00
|
|
|
$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
|
2016-10-14 03:30:05 +02:00
|
|
|
$player = DataObject::get_one(DataExtensionTest\Player::class, array(
|
2013-06-21 00:32:08 +02:00
|
|
|
'"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');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testApiAccessCanBeExtended() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertTrue(Config::inst()->get(DataExtensionTest\TestMember::class, 'api_access', Config::FIRST_SET));
|
2009-05-26 02:52:54 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public 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
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$obj = $this->objFromFixture(DataExtensionTest\MyObject::class, 'object1');
|
|
|
|
$websiteuser = $this->objFromFixture(Member::class, 'websiteuser');
|
|
|
|
$admin = $this->objFromFixture(Member::class, 'admin');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-07 13:18:35 +01:00
|
|
|
$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
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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
|
|
|
);
|
|
|
|
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testPopulateDefaults() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$obj = new DataExtensionTest\TestMember();
|
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
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDbObjectOnExtendedFields() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$member = $this->objFromFixture(DataExtensionTest\TestMember::class, 'member1');
|
2009-04-27 02:44:10 +02:00
|
|
|
$this->assertNotNull($member->dbObject('Website'));
|
2016-06-15 06:03:16 +02:00
|
|
|
$this->assertInstanceOf('SilverStripe\\ORM\\FieldType\\DBVarchar', $member->dbObject('Website'));
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testExtensionCanBeAppliedToDataObject() {
|
2010-10-13 03:35:19 +02:00
|
|
|
$do = new DataObject();
|
2016-10-14 03:30:05 +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());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-03-14 00:28:45 +01:00
|
|
|
public function testExtensionAllMethodNamesHasOwner() {
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var \SilverStripe\ORM\Tests\DataExtensionTest\DataExtensionTest_MyObject $do */
|
|
|
|
$do = DataExtensionTest\MyObject::create();
|
2016-03-14 00:28:45 +01:00
|
|
|
|
|
|
|
$this->assertTrue($do->hasMethod('getTestValueWithDataExtensionTest_MyObject'));
|
|
|
|
}
|
|
|
|
|
2013-05-16 00:34:45 +02:00
|
|
|
public function testPageFieldGeneration() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$page = new DataExtensionTest\CMSFieldsBase();
|
2013-05-16 00:34:45 +02:00
|
|
|
$fields = $page->getCMSFields();
|
|
|
|
$this->assertNotEmpty($fields);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-16 00:34:45 +02:00
|
|
|
// Check basic field exists
|
|
|
|
$this->assertNotEmpty($fields->dataFieldByName('PageField'));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-16 00:34:45 +02:00
|
|
|
public function testPageExtensionsFieldGeneration() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$page = new DataExtensionTest\CMSFieldsBase();
|
2013-05-16 00:34:45 +02:00
|
|
|
$fields = $page->getCMSFields();
|
|
|
|
$this->assertNotEmpty($fields);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-16 00:34:45 +02:00
|
|
|
// Check extending fields exist
|
|
|
|
$this->assertNotEmpty($fields->dataFieldByName('ExtendedFieldRemove')); // Not removed yet!
|
|
|
|
$this->assertNotEmpty($fields->dataFieldByName('ExtendedFieldKeep'));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-16 00:34:45 +02:00
|
|
|
public function testSubpageFieldGeneration() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$page = new DataExtensionTest\CMSFieldsChild();
|
2013-05-16 00:34:45 +02:00
|
|
|
$fields = $page->getCMSFields();
|
|
|
|
$this->assertNotEmpty($fields);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-16 00:34:45 +02:00
|
|
|
// Check extending fields exist
|
|
|
|
$this->assertEmpty($fields->dataFieldByName('ExtendedFieldRemove')); // Removed by child class
|
|
|
|
$this->assertNotEmpty($fields->dataFieldByName('ExtendedFieldKeep'));
|
|
|
|
$this->assertNotEmpty($preExtendedField = $fields->dataFieldByName('ChildFieldBeforeExtension'));
|
|
|
|
$this->assertEquals($preExtendedField->Title(), 'ChildFieldBeforeExtension: Modified Title');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-16 00:34:45 +02:00
|
|
|
// Post-extension fields
|
|
|
|
$this->assertNotEmpty($fields->dataFieldByName('ChildField'));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-16 00:34:45 +02:00
|
|
|
public function testSubSubpageFieldGeneration() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$page = new DataExtensionTest\CMSFieldsGrandChild();
|
2013-05-16 00:34:45 +02:00
|
|
|
$fields = $page->getCMSFields();
|
|
|
|
$this->assertNotEmpty($fields);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-16 00:34:45 +02:00
|
|
|
// Check extending fields exist
|
|
|
|
$this->assertEmpty($fields->dataFieldByName('ExtendedFieldRemove')); // Removed by child class
|
|
|
|
$this->assertNotEmpty($fields->dataFieldByName('ExtendedFieldKeep'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-16 00:34:45 +02:00
|
|
|
// Check child fields removed by grandchild in beforeUpdateCMSFields
|
|
|
|
$this->assertEmpty($fields->dataFieldByName('ChildFieldBeforeExtension')); // Removed by grandchild class
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-16 00:34:45 +02:00
|
|
|
// Check grandchild field modified by extension
|
|
|
|
$this->assertNotEmpty($preExtendedField = $fields->dataFieldByName('GrandchildFieldBeforeExtension'));
|
|
|
|
$this->assertEquals($preExtendedField->Title(), 'GrandchildFieldBeforeExtension: Modified Title');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-16 00:34:45 +02:00
|
|
|
// Post-extension fields
|
|
|
|
$this->assertNotEmpty($fields->dataFieldByName('ChildField'));
|
|
|
|
$this->assertNotEmpty($fields->dataFieldByName('GrandchildField'));
|
|
|
|
}
|
2016-03-22 02:39:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test setOwner behaviour
|
|
|
|
*/
|
|
|
|
public function testSetOwner() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$extension = new DataExtensionTest\Extension1();
|
|
|
|
$obj1 = $this->objFromFixture(DataExtensionTest\RelatedObject::class, 'obj1');
|
|
|
|
$obj2 = $this->objFromFixture(DataExtensionTest\RelatedObject::class, 'obj1');
|
2016-03-22 02:39:25 +01:00
|
|
|
|
|
|
|
$extension->setOwner(null);
|
|
|
|
$this->assertNull($extension->getOwner());
|
|
|
|
|
|
|
|
// Set original owner
|
|
|
|
$extension->setOwner($obj1);
|
|
|
|
$this->assertEquals($obj1, $extension->getOwner());
|
|
|
|
|
|
|
|
// Set nested owner
|
|
|
|
$extension->setOwner($obj2);
|
|
|
|
$this->assertEquals($obj2, $extension->getOwner());
|
|
|
|
|
|
|
|
// Clear nested owner
|
|
|
|
$extension->clearOwner();
|
|
|
|
$this->assertEquals($obj1, $extension->getOwner());
|
|
|
|
|
|
|
|
// Clear original owner
|
|
|
|
$extension->clearOwner();
|
|
|
|
$this->assertNull($extension->getOwner());
|
|
|
|
|
|
|
|
// Another clearOwner should error
|
|
|
|
$this->setExpectedException("BadMethodCallException", "clearOwner() called more than setOwner()");
|
|
|
|
$extension->clearOwner();
|
|
|
|
}
|
|
|
|
|
2008-08-09 07:00:42 +02:00
|
|
|
}
|