silverstripe-framework/tests/php/ORM/DataExtensionTest.php

274 lines
9.4 KiB
PHP
Raw Normal View History

<?php
2016-10-14 03:30:05 +02:00
namespace SilverStripe\ORM\Tests;
use SilverStripe\ORM\DataObject;
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;
class DataExtensionTest extends SapphireTest {
protected static $fixture_file = 'DataExtensionTest.yml';
2014-08-15 08:53:05 +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
);
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
public function testOneToManyAssociationWithExtension() {
2016-10-14 03:30:05 +02:00
$contact = new DataExtensionTest\TestMember();
$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();
$object->FieldOne = "Lorem ipsum dolor";
$object->FieldTwo = "Random notes";
2014-08-15 08:53:05 +02:00
// The following code doesn't currently work:
// $contact->RelatedObjects()->add($object);
// $contact->write();
2014-08-15 08:53:05 +02:00
// Instead we have to do the following
$contact->write();
$object->ContactID = $contact->ID;
$object->write();
$contactID = $contact->ID;
unset($contact);
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(
'"DataExtensionTest_Member"."Website"' => 'http://www.example.com'
));
2016-10-14 03:30:05 +02:00
$object = DataObject::get_one(DataExtensionTest\RelatedObject::class, array(
'"DataExtensionTest_RelatedObject"."ContactID"' => $contactID
));
$this->assertNotNull($object, 'Related object not null');
2016-10-14 03:30:05 +02:00
$this->assertInstanceOf(DataExtensionTest\TestMember::class, $object->Contact(),
'Related contact is a member dataobject');
2016-10-14 03:30:05 +02:00
$this->assertInstanceOf(DataExtensionTest\TestMember::class, $object->getComponent('Contact'),
'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());
$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
public function testManyManyAssociationWithExtension() {
2016-10-14 03:30:05 +02:00
$parent = new DataExtensionTest\MyObject();
$parent->Title = 'My Title';
$parent->write();
2014-08-15 08:53: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');
$parent->Faves()->add($obj1->ID);
$this->assertEquals(1, $parent->Faves()->Count());
2014-08-15 08:53:05 +02:00
$parent->Faves()->add($obj2->ID);
$this->assertEquals(2, $parent->Faves()->Count());
2014-08-15 08:53:05 +02:00
$parent->Faves()->removeByID($obj2->ID);
$this->assertEquals(1, $parent->Faves()->Count());
}
2014-08-15 08:53:05 +02:00
/**
* Test {@link Object::add_extension()} has loaded DataExtension statics correctly.
*/
public function testAddExtensionLoadsStatics() {
// 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
// Now that we've just added the extension, we need to rebuild the database
$this->resetDBSchema(true);
2014-08-15 08:53:05 +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();
$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
unset($player);
2012-04-20 05:41:51 +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(
'"DataExtensionTest_Player"."Name"' => 'Joe'
));
$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
/**
* Test that DataObject::$api_access can be set to true via a extension
*/
public function testApiAccessCanBeExtended() {
2016-10-14 03:30:05 +02:00
$this->assertTrue(Config::inst()->get(DataExtensionTest\TestMember::class, 'api_access', Config::FIRST_SET));
}
2014-08-15 08:53:05 +02:00
public function testPermissionExtension() {
// 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
$this->assertFalse(
$obj->canOne($websiteuser),
'Both extensions return true, but original method returns false'
);
$this->assertFalse(
$obj->canTwo($websiteuser),
'One extension returns false, original returns true, but extension takes precedence'
);
2014-08-15 08:53:05 +02:00
$this->assertTrue(
$obj->canThree($admin),
'Undefined extension methods returning NULL dont influence the original method'
);
}
2014-08-15 08:53:05 +02:00
public function testPopulateDefaults() {
2016-10-14 03:30:05 +02:00
$obj = new DataExtensionTest\TestMember();
$this->assertEquals(
$obj->Phone,
'123',
'Defaults can be populated through extension'
);
}
/**
* Test that DataObject::dbObject() works for fields applied by a extension
*/
public function testDbObjectOnExtendedFields() {
2016-10-14 03:30:05 +02:00
$member = $this->objFromFixture(DataExtensionTest\TestMember::class, 'member1');
$this->assertNotNull($member->dbObject('Website'));
$this->assertInstanceOf('SilverStripe\\ORM\\FieldType\\DBVarchar', $member->dbObject('Website'));
2014-08-15 08:53:05 +02:00
}
public function testExtensionCanBeAppliedToDataObject() {
$do = new DataObject();
2016-10-14 03:30:05 +02:00
$mo = new DataExtensionTest\MyObject();
$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
public function testExtensionAllMethodNamesHasOwner() {
2016-10-14 03:30:05 +02:00
/** @var \SilverStripe\ORM\Tests\DataExtensionTest\DataExtensionTest_MyObject $do */
$do = DataExtensionTest\MyObject::create();
$this->assertTrue($do->hasMethod('getTestValueWithDataExtensionTest_MyObject'));
}
public function testPageFieldGeneration() {
2016-10-14 03:30:05 +02:00
$page = new DataExtensionTest\CMSFieldsBase();
$fields = $page->getCMSFields();
$this->assertNotEmpty($fields);
2014-08-15 08:53:05 +02:00
// Check basic field exists
$this->assertNotEmpty($fields->dataFieldByName('PageField'));
}
2014-08-15 08:53:05 +02:00
public function testPageExtensionsFieldGeneration() {
2016-10-14 03:30:05 +02:00
$page = new DataExtensionTest\CMSFieldsBase();
$fields = $page->getCMSFields();
$this->assertNotEmpty($fields);
2014-08-15 08:53:05 +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
public function testSubpageFieldGeneration() {
2016-10-14 03:30:05 +02:00
$page = new DataExtensionTest\CMSFieldsChild();
$fields = $page->getCMSFields();
$this->assertNotEmpty($fields);
2014-08-15 08:53:05 +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
// Post-extension fields
$this->assertNotEmpty($fields->dataFieldByName('ChildField'));
}
2014-08-15 08:53:05 +02:00
public function testSubSubpageFieldGeneration() {
2016-10-14 03:30:05 +02:00
$page = new DataExtensionTest\CMSFieldsGrandChild();
$fields = $page->getCMSFields();
$this->assertNotEmpty($fields);
2014-08-15 08:53:05 +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
// 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
// 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
// Post-extension fields
$this->assertNotEmpty($fields->dataFieldByName('ChildField'));
$this->assertNotEmpty($fields->dataFieldByName('GrandchildField'));
}
/**
* 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');
$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();
}
}