2008-01-09 04:39:05 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-06-15 15:33:53 +02:00
|
|
|
* @subpackage tests
|
2008-01-09 04:39:05 +01:00
|
|
|
*/
|
|
|
|
class DataObjectTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = 'DataObjectTest.yml';
|
2009-03-04 04:44:11 +01:00
|
|
|
|
2010-04-12 04:03:16 +02:00
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'DataObjectTest_Team',
|
|
|
|
'DataObjectTest_Fixture',
|
|
|
|
'DataObjectTest_SubTeam',
|
|
|
|
'OtherSubclassWithSameField',
|
|
|
|
'DataObjectTest_FieldlessTable',
|
|
|
|
'DataObjectTest_FieldlessSubTable',
|
|
|
|
'DataObjectTest_ValidatedObject',
|
|
|
|
'DataObjectTest_Player',
|
2013-07-10 02:44:24 +02:00
|
|
|
'DataObjectTest_TeamComment',
|
2015-01-24 00:13:17 +01:00
|
|
|
'DataObjectTest_EquipmentCompany',
|
|
|
|
'DataObjectTest_SubEquipmentCompany',
|
2013-07-10 02:44:24 +02:00
|
|
|
'DataObjectTest\NamespacedClass',
|
|
|
|
'DataObjectTest\RelationClass',
|
2015-02-12 14:15:32 +01:00
|
|
|
'DataObjectTest_ExtendedTeamComment',
|
|
|
|
'DataObjectTest_Company',
|
|
|
|
'DataObjectTest_Staff',
|
|
|
|
'DataObjectTest_CEO',
|
|
|
|
'DataObjectTest_Fan',
|
2015-11-06 15:38:59 +01:00
|
|
|
'DataObjectTest_Play',
|
|
|
|
'DataObjectTest_Ploy',
|
|
|
|
'DataObjectTest_Bogey',
|
2016-05-10 16:56:15 +02:00
|
|
|
'ManyManyListTest_Product',
|
|
|
|
'ManyManyListTest_Category',
|
2010-04-12 04:03:16 +02:00
|
|
|
);
|
2013-07-02 15:09:05 +02:00
|
|
|
|
2014-11-10 22:12:25 +01:00
|
|
|
public function testDb() {
|
|
|
|
$obj = new DataObjectTest_TeamComment();
|
2013-07-04 06:31:57 +02:00
|
|
|
$dbFields = $obj->db();
|
2014-11-10 22:12:25 +01:00
|
|
|
|
|
|
|
// Assert fields are included
|
2013-07-04 06:31:57 +02:00
|
|
|
$this->assertArrayHasKey('Name', $dbFields);
|
2014-11-10 22:12:25 +01:00
|
|
|
|
|
|
|
// Assert the base fields are excluded
|
2013-07-04 06:31:57 +02:00
|
|
|
$this->assertArrayNotHasKey('Created', $dbFields);
|
|
|
|
$this->assertArrayNotHasKey('LastEdited', $dbFields);
|
|
|
|
$this->assertArrayNotHasKey('ClassName', $dbFields);
|
|
|
|
$this->assertArrayNotHasKey('ID', $dbFields);
|
2014-11-10 22:12:25 +01:00
|
|
|
|
|
|
|
// Assert that the correct field type is returned when passing a field
|
|
|
|
$this->assertEquals('Varchar', $obj->db('Name'));
|
|
|
|
$this->assertEquals('Text', $obj->db('Comment'));
|
|
|
|
|
|
|
|
$obj = new DataObjectTest_ExtendedTeamComment();
|
2015-01-19 21:38:08 +01:00
|
|
|
$dbFields = $obj->db();
|
2014-11-10 22:12:25 +01:00
|
|
|
|
|
|
|
// Assert overloaded fields have correct data type
|
|
|
|
$this->assertEquals('HTMLText', $obj->db('Comment'));
|
2015-01-19 21:38:08 +01:00
|
|
|
$this->assertEquals('HTMLText', $dbFields['Comment'],
|
|
|
|
'Calls to DataObject::db without a field specified return correct data types');
|
|
|
|
|
|
|
|
// assertEquals doesn't verify the order of array elements, so access keys manually to check order:
|
|
|
|
// expected: array('Name' => 'Varchar', 'Comment' => 'HTMLText')
|
|
|
|
reset($dbFields);
|
|
|
|
$this->assertEquals('Name', key($dbFields), 'DataObject::db returns fields in correct order');
|
|
|
|
next($dbFields);
|
|
|
|
$this->assertEquals('Comment', key($dbFields), 'DataObject::db returns fields in correct order');
|
2013-07-04 06:31:57 +02:00
|
|
|
}
|
|
|
|
|
2015-05-21 05:30:19 +02:00
|
|
|
public function testConstructAcceptsValues() {
|
|
|
|
// Values can be an array...
|
|
|
|
$player = new DataObjectTest_Player(array(
|
|
|
|
'FirstName' => 'James',
|
|
|
|
'Surname' => 'Smith'
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertEquals('James', $player->FirstName);
|
|
|
|
$this->assertEquals('Smith', $player->Surname);
|
|
|
|
|
|
|
|
// ... or a stdClass inst
|
|
|
|
$data = new stdClass();
|
|
|
|
$data->FirstName = 'John';
|
|
|
|
$data->Surname = 'Doe';
|
|
|
|
$player = new DataObjectTest_Player($data);
|
|
|
|
|
|
|
|
$this->assertEquals('John', $player->FirstName);
|
|
|
|
$this->assertEquals('Doe', $player->Surname);
|
|
|
|
|
|
|
|
// IDs should be stored as integers, not strings
|
|
|
|
$player = new DataObjectTest_Player(array('ID' => '5'));
|
|
|
|
$this->assertSame(5, $player->ID);
|
|
|
|
}
|
|
|
|
|
2013-07-02 15:09:05 +02:00
|
|
|
public function testValidObjectsForBaseFields() {
|
|
|
|
$obj = new DataObjectTest_ValidatedObject();
|
|
|
|
|
|
|
|
foreach (array('Created', 'LastEdited', 'ClassName', 'ID') as $field) {
|
|
|
|
$helper = $obj->dbObject($field);
|
|
|
|
$this->assertTrue(
|
|
|
|
($helper instanceof DBField),
|
|
|
|
"for {$field} expected helper to be DBField, but was " .
|
|
|
|
(is_object($helper) ? get_class($helper) : "null")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDataIntegrityWhenTwoSubclassesHaveSameField() {
|
2010-04-13 00:30:07 +02:00
|
|
|
// Save data into DataObjectTest_SubTeam.SubclassDatabaseField
|
|
|
|
$obj = new DataObjectTest_SubTeam();
|
|
|
|
$obj->SubclassDatabaseField = "obj-SubTeam";
|
|
|
|
$obj->write();
|
2011-09-27 04:01:06 +02:00
|
|
|
|
2010-04-13 00:30:07 +02:00
|
|
|
// Change the class
|
|
|
|
$obj->ClassName = 'OtherSubclassWithSameField';
|
|
|
|
$obj->write();
|
|
|
|
$obj->flushCache();
|
2011-09-27 04:01:06 +02:00
|
|
|
|
|
|
|
// Re-fetch from the database and confirm that the data is sourced from
|
2010-04-13 00:30:07 +02:00
|
|
|
// OtherSubclassWithSameField.SubclassDatabaseField
|
|
|
|
$obj = DataObject::get_by_id('DataObjectTest_Team', $obj->ID);
|
|
|
|
$this->assertNull($obj->SubclassDatabaseField);
|
2011-09-27 04:01:06 +02:00
|
|
|
|
2010-04-13 00:30:07 +02:00
|
|
|
// Confirm that save the object in the other direction.
|
|
|
|
$obj->SubclassDatabaseField = 'obj-Other';
|
|
|
|
$obj->write();
|
|
|
|
|
|
|
|
$obj->ClassName = 'DataObjectTest_SubTeam';
|
|
|
|
$obj->write();
|
|
|
|
$obj->flushCache();
|
|
|
|
|
|
|
|
// If we restore the class, the old value has been lying dormant and will be available again.
|
|
|
|
// NOTE: This behaviour is volatile; we may change this in the future to clear fields that
|
|
|
|
// are no longer relevant when changing ClassName
|
|
|
|
$obj = DataObject::get_by_id('DataObjectTest_Team', $obj->ID);
|
|
|
|
$this->assertEquals('obj-SubTeam', $obj->SubclassDatabaseField);
|
|
|
|
}
|
2010-04-12 04:03:16 +02:00
|
|
|
|
2008-01-09 04:39:05 +01:00
|
|
|
/**
|
|
|
|
* Test deletion of DataObjects
|
|
|
|
* - Deleting using delete() on the DataObject
|
|
|
|
* - Deleting using DataObject::delete_by_id()
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDelete() {
|
2008-01-09 04:39:05 +01:00
|
|
|
// Test deleting using delete() on the DataObject
|
|
|
|
// Get the first page
|
2011-03-23 04:32:24 +01:00
|
|
|
$obj = $this->objFromFixture('DataObjectTest_Player', 'captain1');
|
|
|
|
$objID = $obj->ID;
|
2008-01-09 04:39:05 +01:00
|
|
|
// Check the page exists before deleting
|
2011-03-23 04:32:24 +01:00
|
|
|
$this->assertTrue(is_object($obj) && $obj->exists());
|
2008-01-09 04:39:05 +01:00
|
|
|
// Delete the page
|
2011-03-23 04:32:24 +01:00
|
|
|
$obj->delete();
|
2008-01-09 04:39:05 +01:00
|
|
|
// Check that page does not exist after deleting
|
2011-03-23 04:32:24 +01:00
|
|
|
$obj = DataObject::get_by_id('DataObjectTest_Player', $objID);
|
|
|
|
$this->assertTrue(!$obj || !$obj->exists());
|
2011-09-27 04:01:06 +02:00
|
|
|
|
|
|
|
|
2008-01-09 04:39:05 +01:00
|
|
|
// Test deleting using DataObject::delete_by_id()
|
|
|
|
// Get the second page
|
2011-03-23 04:32:24 +01:00
|
|
|
$obj = $this->objFromFixture('DataObjectTest_Player', 'captain2');
|
|
|
|
$objID = $obj->ID;
|
2008-01-09 04:39:05 +01:00
|
|
|
// Check the page exists before deleting
|
2011-03-23 04:32:24 +01:00
|
|
|
$this->assertTrue(is_object($obj) && $obj->exists());
|
2008-01-09 04:39:05 +01:00
|
|
|
// Delete the page
|
2011-03-23 04:32:24 +01:00
|
|
|
DataObject::delete_by_id('DataObjectTest_Player', $obj->ID);
|
2008-01-09 04:39:05 +01:00
|
|
|
// Check that page does not exist after deleting
|
2011-03-23 04:32:24 +01:00
|
|
|
$obj = DataObject::get_by_id('DataObjectTest_Player', $objID);
|
|
|
|
$this->assertTrue(!$obj || !$obj->exists());
|
2008-01-09 04:39:05 +01:00
|
|
|
}
|
2011-09-27 04:01:06 +02:00
|
|
|
|
2008-01-09 04:39:05 +01:00
|
|
|
/**
|
|
|
|
* Test methods that get DataObjects
|
|
|
|
* - DataObject::get()
|
|
|
|
* - All records of a DataObject
|
|
|
|
* - Filtering
|
|
|
|
* - Sorting
|
|
|
|
* - Joins
|
|
|
|
* - Limit
|
|
|
|
* - Container class
|
|
|
|
* - DataObject::get_by_id()
|
|
|
|
* - DataObject::get_one()
|
|
|
|
* - With and without caching
|
|
|
|
* - With and without ordering
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testGet() {
|
2008-01-09 04:39:05 +01:00
|
|
|
// Test getting all records of a DataObject
|
2010-12-11 06:43:08 +01:00
|
|
|
$comments = DataObject::get('DataObjectTest_TeamComment');
|
|
|
|
$this->assertEquals(3, $comments->Count());
|
2011-09-27 04:01:06 +02:00
|
|
|
|
2008-01-09 04:39:05 +01:00
|
|
|
// Test WHERE clause
|
2010-12-11 06:43:08 +01:00
|
|
|
$comments = DataObject::get('DataObjectTest_TeamComment', "\"Name\"='Bob'");
|
|
|
|
$this->assertEquals(1, $comments->Count());
|
2008-01-09 04:39:05 +01:00
|
|
|
foreach($comments as $comment) {
|
2008-08-13 02:44:52 +02:00
|
|
|
$this->assertEquals('Bob', $comment->Name);
|
2008-01-09 04:39:05 +01:00
|
|
|
}
|
2011-09-27 04:01:06 +02:00
|
|
|
|
2008-01-09 04:39:05 +01:00
|
|
|
// Test sorting
|
2010-12-11 06:43:08 +01:00
|
|
|
$comments = DataObject::get('DataObjectTest_TeamComment', '', "\"Name\" ASC");
|
|
|
|
$this->assertEquals(3, $comments->Count());
|
2008-08-13 02:44:52 +02:00
|
|
|
$this->assertEquals('Bob', $comments->First()->Name);
|
2010-12-11 06:43:08 +01:00
|
|
|
$comments = DataObject::get('DataObjectTest_TeamComment', '', "\"Name\" DESC");
|
|
|
|
$this->assertEquals(3, $comments->Count());
|
|
|
|
$this->assertEquals('Phil', $comments->First()->Name);
|
2011-09-27 04:01:06 +02:00
|
|
|
|
2008-01-09 04:39:05 +01:00
|
|
|
// Test limit
|
2010-12-11 06:43:08 +01:00
|
|
|
$comments = DataObject::get('DataObjectTest_TeamComment', '', "\"Name\" ASC", '', '1,2');
|
2008-08-13 02:44:52 +02:00
|
|
|
$this->assertEquals(2, $comments->Count());
|
2010-12-11 06:43:08 +01:00
|
|
|
$this->assertEquals('Joe', $comments->First()->Name);
|
|
|
|
$this->assertEquals('Phil', $comments->Last()->Name);
|
2011-09-27 04:01:06 +02:00
|
|
|
|
2008-01-09 04:39:05 +01:00
|
|
|
// Test get_by_id()
|
2011-03-23 04:32:24 +01:00
|
|
|
$captain1ID = $this->idFromFixture('DataObjectTest_Player', 'captain1');
|
|
|
|
$captain1 = DataObject::get_by_id('DataObjectTest_Player', $captain1ID);
|
|
|
|
$this->assertEquals('Captain', $captain1->FirstName);
|
2011-09-27 04:01:06 +02:00
|
|
|
|
2008-01-09 04:39:05 +01:00
|
|
|
// Test get_one() without caching
|
2013-06-21 00:32:08 +02:00
|
|
|
$comment1 = DataObject::get_one('DataObjectTest_TeamComment', array(
|
|
|
|
'"DataObjectTest_TeamComment"."Name"' => 'Joe'
|
|
|
|
), false);
|
2008-01-09 04:39:05 +01:00
|
|
|
$comment1->Comment = "Something Else";
|
2011-09-27 04:01:06 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$comment2 = DataObject::get_one('DataObjectTest_TeamComment', array(
|
|
|
|
'"DataObjectTest_TeamComment"."Name"' => 'Joe'
|
|
|
|
), false);
|
2008-08-13 02:44:52 +02:00
|
|
|
$this->assertNotEquals($comment1->Comment, $comment2->Comment);
|
2011-09-27 04:01:06 +02:00
|
|
|
|
2008-01-09 04:39:05 +01:00
|
|
|
// Test get_one() with caching
|
2013-06-21 00:32:08 +02:00
|
|
|
$comment1 = DataObject::get_one('DataObjectTest_TeamComment', array(
|
|
|
|
'"DataObjectTest_TeamComment"."Name"' => 'Bob'
|
|
|
|
), true);
|
2008-01-09 04:39:05 +01:00
|
|
|
$comment1->Comment = "Something Else";
|
2011-09-27 04:01:06 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$comment2 = DataObject::get_one('DataObjectTest_TeamComment', array(
|
|
|
|
'"DataObjectTest_TeamComment"."Name"' => 'Bob'
|
|
|
|
), true);
|
2008-08-13 02:44:52 +02:00
|
|
|
$this->assertEquals((string)$comment1->Comment, (string)$comment2->Comment);
|
2011-09-27 04:01:06 +02:00
|
|
|
|
2008-01-09 04:39:05 +01:00
|
|
|
// Test get_one() with order by without caching
|
2010-12-11 06:43:08 +01:00
|
|
|
$comment = DataObject::get_one('DataObjectTest_TeamComment', '', false, "\"Name\" ASC");
|
2008-08-13 02:44:52 +02:00
|
|
|
$this->assertEquals('Bob', $comment->Name);
|
2011-09-27 04:01:06 +02:00
|
|
|
|
2010-12-11 06:43:08 +01:00
|
|
|
$comment = DataObject::get_one('DataObjectTest_TeamComment', '', false, "\"Name\" DESC");
|
|
|
|
$this->assertEquals('Phil', $comment->Name);
|
2011-09-27 04:01:06 +02:00
|
|
|
|
2008-01-09 04:39:05 +01:00
|
|
|
// Test get_one() with order by with caching
|
2010-12-11 06:43:08 +01:00
|
|
|
$comment = DataObject::get_one('DataObjectTest_TeamComment', '', true, '"Name" ASC');
|
2008-08-13 02:44:52 +02:00
|
|
|
$this->assertEquals('Bob', $comment->Name);
|
2010-12-11 06:43:08 +01:00
|
|
|
$comment = DataObject::get_one('DataObjectTest_TeamComment', '', true, '"Name" DESC');
|
|
|
|
$this->assertEquals('Phil', $comment->Name);
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
public function testGetCaseInsensitive() {
|
2013-01-11 00:20:28 +01:00
|
|
|
// Test get_one() with bad case on the classname
|
2013-06-21 00:32:08 +02:00
|
|
|
// Note: This will succeed only if the underlying DB server supports case-insensitive
|
|
|
|
// table names (e.g. such as MySQL, but not SQLite3)
|
|
|
|
if(!(DB::get_conn() instanceof MySQLDatabase)) {
|
|
|
|
$this->markTestSkipped('MySQL only');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$subteam1 = DataObject::get_one('dataobjecttest_subteam', array(
|
|
|
|
'"DataObjectTest_Team"."Title"' => 'Subteam 1'
|
|
|
|
), true);
|
|
|
|
$this->assertNotEmpty($subteam1);
|
2013-01-11 00:20:28 +01:00
|
|
|
$this->assertEquals($subteam1->Title, "Subteam 1");
|
2008-01-09 04:39:05 +01:00
|
|
|
}
|
2011-09-27 04:01:06 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testGetSubclassFields() {
|
2009-11-22 04:47:18 +01:00
|
|
|
/* Test that fields / has_one relations from the parent table and the subclass tables are extracted */
|
|
|
|
$captain1 = $this->objFromFixture("DataObjectTest_Player", "captain1");
|
|
|
|
// Base field
|
2009-11-22 06:16:38 +01:00
|
|
|
$this->assertEquals('Captain', $captain1->FirstName);
|
2009-11-22 04:47:18 +01:00
|
|
|
// Subclass field
|
|
|
|
$this->assertEquals('007', $captain1->ShirtNumber);
|
|
|
|
// Subclass has_one relation
|
|
|
|
$this->assertEquals($this->idFromFixture('DataObjectTest_Team', 'team1'), $captain1->FavouriteTeamID);
|
|
|
|
}
|
2011-09-27 04:01:06 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testGetRelationClass() {
|
2012-04-20 06:03:38 +02:00
|
|
|
$obj = new DataObjectTest_Player();
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals(singleton('DataObjectTest_Player')->getRelationClass('FavouriteTeam'),
|
|
|
|
'DataObjectTest_Team', 'has_one is properly inspected');
|
|
|
|
$this->assertEquals(singleton('DataObjectTest_Company')->getRelationClass('CurrentStaff'),
|
|
|
|
'DataObjectTest_Staff', 'has_many is properly inspected');
|
|
|
|
$this->assertEquals(singleton('DataObjectTest_Team')->getRelationClass('Players'), 'DataObjectTest_Player',
|
|
|
|
'many_many is properly inspected');
|
|
|
|
$this->assertEquals(singleton('DataObjectTest_Player')->getRelationClass('Teams'), 'DataObjectTest_Team',
|
|
|
|
'belongs_many_many is properly inspected');
|
|
|
|
$this->assertEquals(singleton('DataObjectTest_CEO')->getRelationClass('Company'), 'DataObjectTest_Company',
|
|
|
|
'belongs_to is properly inspected');
|
2013-07-10 02:44:24 +02:00
|
|
|
$this->assertEquals(singleton('DataObjectTest_Fan')->getRelationClass('Favourite'), 'DataObject',
|
|
|
|
'polymorphic has_one is properly inspected');
|
2012-04-20 06:03:38 +02:00
|
|
|
}
|
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
/**
|
|
|
|
* Test that has_one relations can be retrieved
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testGetHasOneRelations() {
|
2009-11-22 04:47:18 +01:00
|
|
|
$captain1 = $this->objFromFixture("DataObjectTest_Player", "captain1");
|
2013-07-10 02:44:24 +02:00
|
|
|
$team1ID = $this->idFromFixture('DataObjectTest_Team', 'team1');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
// There will be a field called (relname)ID that contains the ID of the
|
2013-07-10 02:44:24 +02:00
|
|
|
// object linked to via the has_one relation
|
|
|
|
$this->assertEquals($team1ID, $captain1->FavouriteTeamID);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
// There will be a method called $obj->relname() that returns the object itself
|
|
|
|
$this->assertEquals($team1ID, $captain1->FavouriteTeam()->ID);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
// Check entity with polymorphic has-one
|
|
|
|
$fan1 = $this->objFromFixture("DataObjectTest_Fan", "fan1");
|
2014-03-27 21:37:04 +01:00
|
|
|
$this->assertTrue((bool)$fan1->hasValue('Favourite'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
// There will be fields named (relname)ID and (relname)Class for polymorphic
|
|
|
|
// entities
|
|
|
|
$this->assertEquals($team1ID, $fan1->FavouriteID);
|
|
|
|
$this->assertEquals('DataObjectTest_Team', $fan1->FavouriteClass);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
// There will be a method called $obj->relname() that returns the object itself
|
|
|
|
$favourite = $fan1->Favourite();
|
|
|
|
$this->assertEquals($team1ID, $favourite->ID);
|
|
|
|
$this->assertInstanceOf('DataObjectTest_Team', $favourite);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-03-27 21:37:04 +01:00
|
|
|
// check behaviour of dbObject with polymorphic relations
|
|
|
|
$favouriteDBObject = $fan1->dbObject('Favourite');
|
|
|
|
$favouriteValue = $favouriteDBObject->getValue();
|
|
|
|
$this->assertInstanceOf('PolymorphicForeignKey', $favouriteDBObject);
|
|
|
|
$this->assertEquals($favourite->ID, $favouriteValue->ID);
|
|
|
|
$this->assertEquals($favourite->ClassName, $favouriteValue->ClassName);
|
2013-07-10 02:44:24 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
/**
|
|
|
|
* Simple test to ensure that namespaced classes and polymorphic relations work together
|
|
|
|
*/
|
|
|
|
public function testPolymorphicNamespacedRelations() {
|
|
|
|
$parent = new \DataObjectTest\NamespacedClass();
|
|
|
|
$parent->Name = 'New Parent';
|
|
|
|
$parent->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
$child = new \DataObjectTest\RelationClass();
|
|
|
|
$child->Title = 'New Child';
|
|
|
|
$child->write();
|
|
|
|
$parent->Relations()->add($child);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
$this->assertEquals(1, $parent->Relations()->count());
|
|
|
|
$this->assertEquals(array('New Child'), $parent->Relations()->column('Title'));
|
|
|
|
$this->assertEquals('New Parent', $child->Parent()->Name);
|
2009-11-22 04:47:18 +01:00
|
|
|
}
|
2011-09-27 04:01:06 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testLimitAndCount() {
|
2009-11-22 06:16:38 +01:00
|
|
|
$players = DataObject::get("DataObjectTest_Player");
|
2011-09-27 04:01:06 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
// There's 4 records in total
|
|
|
|
$this->assertEquals(4, $players->count());
|
2011-09-27 04:01:06 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
// Testing "##, ##" syntax
|
2012-03-09 02:02:37 +01:00
|
|
|
$this->assertEquals(4, $players->limit(20)->count());
|
|
|
|
$this->assertEquals(4, $players->limit(20, 0)->count());
|
|
|
|
$this->assertEquals(0, $players->limit(20, 20)->count());
|
|
|
|
$this->assertEquals(2, $players->limit(2, 0)->count());
|
|
|
|
$this->assertEquals(1, $players->limit(5, 3)->count());
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2008-08-09 05:54:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test writing of database columns which don't correlate to a DBField,
|
2011-09-27 04:01:06 +02:00
|
|
|
* e.g. all relation fields on has_one/has_many like "ParentID".
|
2008-08-09 05:54:55 +02:00
|
|
|
*
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testWritePropertyWithoutDBField() {
|
2011-03-23 04:32:24 +01:00
|
|
|
$obj = $this->objFromFixture('DataObjectTest_Player', 'captain1');
|
|
|
|
$obj->FavouriteTeamID = 99;
|
|
|
|
$obj->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
// reload the page from the database
|
2011-03-23 04:32:24 +01:00
|
|
|
$savedObj = DataObject::get_by_id('DataObjectTest_Player', $obj->ID);
|
|
|
|
$this->assertTrue($savedObj->FavouriteTeamID == 99);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
// Test with porymorphic relation
|
|
|
|
$obj2 = $this->objFromFixture("DataObjectTest_Fan", "fan1");
|
|
|
|
$obj2->FavouriteID = 99;
|
|
|
|
$obj2->FavouriteClass = 'DataObjectTest_Player';
|
|
|
|
$obj2->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
$savedObj2 = DataObject::get_by_id('DataObjectTest_Fan', $obj2->ID);
|
|
|
|
$this->assertTrue($savedObj2->FavouriteID == 99);
|
|
|
|
$this->assertTrue($savedObj2->FavouriteClass == 'DataObjectTest_Player');
|
2008-08-09 05:54:55 +02:00
|
|
|
}
|
2011-09-27 04:01:06 +02:00
|
|
|
|
2008-01-09 04:39:05 +01:00
|
|
|
/**
|
|
|
|
* Test has many relationships
|
|
|
|
* - Test getComponents() gets the ComponentSet of the other side of the relation
|
|
|
|
* - Test the IDs on the DataObjects are set correctly
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testHasManyRelationships() {
|
2013-07-12 05:18:06 +02:00
|
|
|
$team1 = $this->objFromFixture('DataObjectTest_Team', 'team1');
|
2011-09-27 04:01:06 +02:00
|
|
|
|
2008-01-09 04:39:05 +01:00
|
|
|
// Test getComponents() gets the ComponentSet of the other side of the relation
|
2013-07-12 05:18:06 +02:00
|
|
|
$this->assertTrue($team1->Comments()->Count() == 2);
|
2011-09-27 04:01:06 +02:00
|
|
|
|
2008-01-09 04:39:05 +01:00
|
|
|
// Test the IDs on the DataObjects are set correctly
|
2013-07-12 05:18:06 +02:00
|
|
|
foreach($team1->Comments() as $comment) {
|
|
|
|
$this->assertEquals($team1->ID, $comment->TeamID);
|
2008-01-09 04:39:05 +01:00
|
|
|
}
|
2011-09-27 04:01:06 +02:00
|
|
|
|
2009-11-22 04:47:18 +01:00
|
|
|
// Test that we can add and remove items that already exist in the database
|
2009-11-22 06:16:38 +01:00
|
|
|
$newComment = new DataObjectTest_TeamComment();
|
2009-11-22 04:47:18 +01:00
|
|
|
$newComment->Name = "Automated commenter";
|
|
|
|
$newComment->Comment = "This is a new comment";
|
|
|
|
$newComment->write();
|
2013-07-12 05:18:06 +02:00
|
|
|
$team1->Comments()->add($newComment);
|
|
|
|
$this->assertEquals($team1->ID, $newComment->TeamID);
|
2011-09-27 04:01:06 +02:00
|
|
|
|
2012-12-07 18:44:00 +01:00
|
|
|
$comment1 = $this->objFromFixture('DataObjectTest_TeamComment', 'comment1');
|
|
|
|
$comment2 = $this->objFromFixture('DataObjectTest_TeamComment', 'comment2');
|
2013-07-12 05:18:06 +02:00
|
|
|
$team1->Comments()->remove($comment2);
|
2009-11-22 04:47:18 +01:00
|
|
|
|
2013-07-12 05:18:06 +02:00
|
|
|
$team1CommentIDs = $team1->Comments()->sort('ID')->column('ID');
|
|
|
|
$this->assertEquals(array($comment1->ID, $newComment->ID), $team1CommentIDs);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-12 05:18:06 +02:00
|
|
|
// Test that removing an item from a list doesn't remove it from the same
|
|
|
|
// relation belonging to a different object
|
|
|
|
$team1 = $this->objFromFixture('DataObjectTest_Team', 'team1');
|
|
|
|
$team2 = $this->objFromFixture('DataObjectTest_Team', 'team2');
|
|
|
|
$team2->Comments()->remove($comment1);
|
|
|
|
$team1CommentIDs = $team1->Comments()->sort('ID')->column('ID');
|
|
|
|
$this->assertEquals(array($comment1->ID, $newComment->ID), $team1CommentIDs);
|
2008-01-09 04:39:05 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test has many relationships against polymorphic has_one fields
|
|
|
|
* - Test getComponents() gets the ComponentSet of the other side of the relation
|
|
|
|
* - Test the IDs on the DataObjects are set correctly
|
|
|
|
*/
|
|
|
|
public function testHasManyPolymorphicRelationships() {
|
|
|
|
$team1 = $this->objFromFixture('DataObjectTest_Team', 'team1');
|
|
|
|
|
|
|
|
// Test getComponents() gets the ComponentSet of the other side of the relation
|
|
|
|
$this->assertTrue($team1->Fans()->Count() == 2);
|
|
|
|
|
|
|
|
// Test the IDs/Classes on the DataObjects are set correctly
|
|
|
|
foreach($team1->Fans() as $fan) {
|
|
|
|
$this->assertEquals($team1->ID, $fan->FavouriteID, 'Fan has the correct FavouriteID');
|
|
|
|
$this->assertEquals('DataObjectTest_Team', $fan->FavouriteClass, 'Fan has the correct FavouriteClass');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that we can add and remove items that already exist in the database
|
|
|
|
$newFan = new DataObjectTest_Fan();
|
|
|
|
$newFan->Name = "New fan";
|
|
|
|
$newFan->write();
|
|
|
|
$team1->Fans()->add($newFan);
|
|
|
|
$this->assertEquals($team1->ID, $newFan->FavouriteID, 'Newly created fan has the correct FavouriteID');
|
|
|
|
$this->assertEquals(
|
|
|
|
'DataObjectTest_Team',
|
|
|
|
$newFan->FavouriteClass,
|
|
|
|
'Newly created fan has the correct FavouriteClass'
|
|
|
|
);
|
|
|
|
|
|
|
|
$fan1 = $this->objFromFixture('DataObjectTest_Fan', 'fan1');
|
|
|
|
$fan3 = $this->objFromFixture('DataObjectTest_Fan', 'fan3');
|
|
|
|
$team1->Fans()->remove($fan3);
|
|
|
|
|
|
|
|
$team1FanIDs = $team1->Fans()->sort('ID')->column('ID');
|
|
|
|
$this->assertEquals(array($fan1->ID, $newFan->ID), $team1FanIDs);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
// Test that removing an item from a list doesn't remove it from the same
|
|
|
|
// relation belonging to a different object
|
|
|
|
$team1 = $this->objFromFixture('DataObjectTest_Team', 'team1');
|
|
|
|
$player1 = $this->objFromFixture('DataObjectTest_Player', 'player1');
|
|
|
|
$player1->Fans()->remove($fan1);
|
|
|
|
$team1FanIDs = $team1->Fans()->sort('ID')->column('ID');
|
|
|
|
$this->assertEquals(array($fan1->ID, $newFan->ID), $team1FanIDs);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-21 02:14:47 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testHasOneRelationship() {
|
2009-07-08 02:06:16 +02:00
|
|
|
$team1 = $this->objFromFixture('DataObjectTest_Team', 'team1');
|
|
|
|
$player1 = $this->objFromFixture('DataObjectTest_Player', 'player1');
|
2015-06-16 18:38:34 +02:00
|
|
|
$player2 = $this->objFromFixture('DataObjectTest_Player', 'player2');
|
2013-07-10 02:44:24 +02:00
|
|
|
$fan1 = $this->objFromFixture('DataObjectTest_Fan', 'fan1');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-03-27 21:37:04 +01:00
|
|
|
// Test relation probing
|
|
|
|
$this->assertFalse((bool)$team1->hasValue('Captain', null, false));
|
|
|
|
$this->assertFalse((bool)$team1->hasValue('CaptainID', null, false));
|
2011-09-27 04:01:06 +02:00
|
|
|
|
2009-05-21 02:14:47 +02:00
|
|
|
// Add a captain to team 1
|
|
|
|
$team1->setField('CaptainID', $player1->ID);
|
|
|
|
$team1->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-03-27 21:37:04 +01:00
|
|
|
$this->assertTrue((bool)$team1->hasValue('Captain', null, false));
|
|
|
|
$this->assertTrue((bool)$team1->hasValue('CaptainID', null, false));
|
2011-09-27 04:01:06 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals($player1->ID, $team1->Captain()->ID,
|
|
|
|
'The captain exists for team 1');
|
|
|
|
$this->assertEquals($player1->ID, $team1->getComponent('Captain')->ID,
|
|
|
|
'The captain exists through the component getter');
|
2009-05-21 02:14:47 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals($team1->Captain()->FirstName, 'Player 1',
|
|
|
|
'Player 1 is the captain');
|
|
|
|
$this->assertEquals($team1->getComponent('Captain')->FirstName, 'Player 1',
|
|
|
|
'Player 1 is the captain');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-06-16 18:38:34 +02:00
|
|
|
$team1->CaptainID = $player2->ID;
|
|
|
|
$team1->write();
|
|
|
|
|
|
|
|
$this->assertEquals($player2->ID, $team1->Captain()->ID);
|
|
|
|
$this->assertEquals($player2->ID, $team1->getComponent('Captain')->ID);
|
|
|
|
$this->assertEquals('Player 2', $team1->Captain()->FirstName);
|
|
|
|
$this->assertEquals('Player 2', $team1->getComponent('Captain')->FirstName);
|
2015-06-17 01:24:25 +02:00
|
|
|
|
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
// Set the favourite team for fan1
|
|
|
|
$fan1->setField('FavouriteID', $team1->ID);
|
|
|
|
$fan1->setField('FavouriteClass', $team1->class);
|
|
|
|
|
|
|
|
$this->assertEquals($team1->ID, $fan1->Favourite()->ID, 'The team is assigned to fan 1');
|
|
|
|
$this->assertInstanceOf($team1->class, $fan1->Favourite(), 'The team is assigned to fan 1');
|
|
|
|
$this->assertEquals($team1->ID, $fan1->getComponent('Favourite')->ID,
|
|
|
|
'The team exists through the component getter'
|
|
|
|
);
|
|
|
|
$this->assertInstanceOf($team1->class, $fan1->getComponent('Favourite'),
|
|
|
|
'The team exists through the component getter'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals($fan1->Favourite()->Title, 'Team 1',
|
|
|
|
'Team 1 is the favourite');
|
|
|
|
$this->assertEquals($fan1->getComponent('Favourite')->Title, 'Team 1',
|
|
|
|
'Team 1 is the favourite');
|
2009-05-21 02:14:47 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-11 05:03:52 +02:00
|
|
|
/**
|
|
|
|
* @todo Extend type change tests (e.g. '0'==NULL)
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testChangedFields() {
|
2011-03-23 04:32:24 +01:00
|
|
|
$obj = $this->objFromFixture('DataObjectTest_Player', 'captain1');
|
|
|
|
$obj->FirstName = 'Captain-changed';
|
|
|
|
$obj->IsRetired = true;
|
2008-08-11 05:03:52 +02:00
|
|
|
|
|
|
|
$this->assertEquals(
|
2016-05-18 01:00:04 +02:00
|
|
|
$obj->getChangedFields(true, DataObject::CHANGE_STRICT),
|
2008-08-11 05:03:52 +02:00
|
|
|
array(
|
2011-03-23 04:32:24 +01:00
|
|
|
'FirstName' => array(
|
|
|
|
'before' => 'Captain',
|
|
|
|
'after' => 'Captain-changed',
|
2016-05-18 01:00:04 +02:00
|
|
|
'level' => DataObject::CHANGE_VALUE
|
2008-08-11 05:03:52 +02:00
|
|
|
),
|
2011-03-23 04:32:24 +01:00
|
|
|
'IsRetired' => array(
|
2008-08-11 05:03:52 +02:00
|
|
|
'before' => 1,
|
|
|
|
'after' => true,
|
2016-05-18 01:00:04 +02:00
|
|
|
'level' => DataObject::CHANGE_STRICT
|
2008-08-11 05:03:52 +02:00
|
|
|
)
|
|
|
|
),
|
|
|
|
'Changed fields are correctly detected with strict type changes (level=1)'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-11 05:03:52 +02:00
|
|
|
$this->assertEquals(
|
2016-05-18 01:00:04 +02:00
|
|
|
$obj->getChangedFields(true, DataObject::CHANGE_VALUE),
|
2008-08-11 05:03:52 +02:00
|
|
|
array(
|
2011-03-23 04:32:24 +01:00
|
|
|
'FirstName' => array(
|
|
|
|
'before'=>'Captain',
|
|
|
|
'after'=>'Captain-changed',
|
2016-05-18 01:00:04 +02:00
|
|
|
'level' => DataObject::CHANGE_VALUE
|
2008-08-11 05:03:52 +02:00
|
|
|
)
|
|
|
|
),
|
|
|
|
'Changed fields are correctly detected while ignoring type changes (level=2)'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 04:32:24 +01:00
|
|
|
$newObj = new DataObjectTest_Player();
|
|
|
|
$newObj->FirstName = "New Player";
|
2008-10-08 04:00:12 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
2011-03-23 04:32:24 +01:00
|
|
|
'FirstName' => array(
|
2008-10-08 04:00:12 +02:00
|
|
|
'before' => null,
|
2011-03-23 04:32:24 +01:00
|
|
|
'after' => 'New Player',
|
2016-05-18 01:00:04 +02:00
|
|
|
'level' => DataObject::CHANGE_VALUE
|
2008-10-08 04:00:12 +02:00
|
|
|
)
|
|
|
|
),
|
2016-05-18 01:00:04 +02:00
|
|
|
$newObj->getChangedFields(true, DataObject::CHANGE_VALUE),
|
2008-10-08 04:00:12 +02:00
|
|
|
'Initialised fields are correctly detected as full changes'
|
|
|
|
);
|
2008-08-11 05:03:52 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testIsChanged() {
|
2011-03-23 04:32:24 +01:00
|
|
|
$obj = $this->objFromFixture('DataObjectTest_Player', 'captain1');
|
2016-05-18 01:00:04 +02:00
|
|
|
$obj->NonDBField = 'bob';
|
2011-03-23 04:32:24 +01:00
|
|
|
$obj->FirstName = 'Captain-changed';
|
|
|
|
$obj->IsRetired = true; // type change only, database stores "1"
|
2009-05-27 02:09:23 +02:00
|
|
|
|
2016-05-18 01:00:04 +02:00
|
|
|
// Now that DB fields are changed, isChanged is true
|
|
|
|
$this->assertTrue($obj->isChanged('NonDBField'));
|
|
|
|
$this->assertFalse($obj->isChanged('NonField'));
|
|
|
|
$this->assertTrue($obj->isChanged('FirstName', DataObject::CHANGE_STRICT));
|
|
|
|
$this->assertTrue($obj->isChanged('FirstName', DataObject::CHANGE_VALUE));
|
|
|
|
$this->assertTrue($obj->isChanged('IsRetired', DataObject::CHANGE_STRICT));
|
|
|
|
$this->assertFalse($obj->isChanged('IsRetired', DataObject::CHANGE_VALUE));
|
2011-03-23 04:32:24 +01:00
|
|
|
$this->assertFalse($obj->isChanged('Email', 1), 'Doesnt change mark unchanged property');
|
|
|
|
$this->assertFalse($obj->isChanged('Email', 2), 'Doesnt change mark unchanged property');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 04:32:24 +01:00
|
|
|
$newObj = new DataObjectTest_Player();
|
|
|
|
$newObj->FirstName = "New Player";
|
2016-05-18 01:00:04 +02:00
|
|
|
$this->assertTrue($newObj->isChanged('FirstName', DataObject::CHANGE_STRICT));
|
|
|
|
$this->assertTrue($newObj->isChanged('FirstName', DataObject::CHANGE_VALUE));
|
|
|
|
$this->assertFalse($newObj->isChanged('Email', DataObject::CHANGE_STRICT));
|
|
|
|
$this->assertFalse($newObj->isChanged('Email', DataObject::CHANGE_VALUE));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 04:32:24 +01:00
|
|
|
$newObj->write();
|
2016-05-18 01:00:04 +02:00
|
|
|
$this->assertFalse($newObj->ischanged());
|
|
|
|
$this->assertFalse($newObj->isChanged('FirstName', DataObject::CHANGE_STRICT));
|
|
|
|
$this->assertFalse($newObj->isChanged('FirstName', DataObject::CHANGE_VALUE));
|
|
|
|
$this->assertFalse($newObj->isChanged('Email', DataObject::CHANGE_STRICT));
|
|
|
|
$this->assertFalse($newObj->isChanged('Email', DataObject::CHANGE_VALUE));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 04:32:24 +01:00
|
|
|
$obj = $this->objFromFixture('DataObjectTest_Player', 'captain1');
|
|
|
|
$obj->FirstName = null;
|
2016-05-18 01:00:04 +02:00
|
|
|
$this->assertTrue($obj->isChanged('FirstName', DataObject::CHANGE_STRICT));
|
|
|
|
$this->assertTrue($obj->isChanged('FirstName', DataObject::CHANGE_VALUE));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
/* Test when there's not field provided */
|
2016-05-18 01:00:04 +02:00
|
|
|
$obj = $this->objFromFixture('DataObjectTest_Player', 'captain2');
|
|
|
|
$this->assertFalse($obj->isChanged());
|
|
|
|
$obj->NonDBField = 'new value';
|
|
|
|
$this->assertFalse($obj->isChanged());
|
2014-08-15 08:53:05 +02:00
|
|
|
$obj->FirstName = "New Player";
|
2011-03-23 04:32:24 +01:00
|
|
|
$this->assertTrue($obj->isChanged());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
$obj->write();
|
2011-03-23 04:32:24 +01:00
|
|
|
$this->assertFalse($obj->isChanged());
|
2009-05-27 02:09:23 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRandomSort() {
|
2012-04-20 00:08:17 +02:00
|
|
|
/* If we perform the same regularly sorted query twice, it should return the same results */
|
2011-02-14 06:47:53 +01:00
|
|
|
$itemsA = DataObject::get("DataObjectTest_TeamComment", "", "ID");
|
2008-08-13 02:44:52 +02:00
|
|
|
foreach($itemsA as $item) $keysA[] = $item->ID;
|
|
|
|
|
2011-02-14 06:47:53 +01:00
|
|
|
$itemsB = DataObject::get("DataObjectTest_TeamComment", "", "ID");
|
2008-08-13 02:44:52 +02:00
|
|
|
foreach($itemsB as $item) $keysB[] = $item->ID;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
/* Test when there's not field provided */
|
2011-03-23 04:32:24 +01:00
|
|
|
$obj = $this->objFromFixture('DataObjectTest_Player', 'captain1');
|
2014-08-15 08:53:05 +02:00
|
|
|
$obj->FirstName = "New Player";
|
2011-03-23 04:32:24 +01:00
|
|
|
$this->assertTrue($obj->isChanged());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
$obj->write();
|
2011-03-23 04:32:24 +01:00
|
|
|
$this->assertFalse($obj->isChanged());
|
2009-11-22 04:47:18 +01:00
|
|
|
|
|
|
|
/* If we perform the same random query twice, it shouldn't return the same results */
|
2013-06-21 00:32:08 +02:00
|
|
|
$itemsA = DataObject::get("DataObjectTest_TeamComment", "", DB::get_conn()->random());
|
|
|
|
$itemsB = DataObject::get("DataObjectTest_TeamComment", "", DB::get_conn()->random());
|
|
|
|
$itemsC = DataObject::get("DataObjectTest_TeamComment", "", DB::get_conn()->random());
|
|
|
|
$itemsD = DataObject::get("DataObjectTest_TeamComment", "", DB::get_conn()->random());
|
2009-11-22 06:16:38 +01:00
|
|
|
foreach($itemsA as $item) $keysA[] = $item->ID;
|
2009-11-22 04:47:18 +01:00
|
|
|
foreach($itemsB as $item) $keysB[] = $item->ID;
|
2009-11-22 06:16:38 +01:00
|
|
|
foreach($itemsC as $item) $keysC[] = $item->ID;
|
|
|
|
foreach($itemsD as $item) $keysD[] = $item->ID;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
// These shouldn't all be the same (run it 4 times to minimise chance of an accidental collision)
|
|
|
|
// There's about a 1 in a billion chance of an accidental collision
|
|
|
|
$this->assertTrue($keysA != $keysB || $keysB != $keysC || $keysC != $keysD);
|
2008-08-13 02:44:52 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testWriteSavesToHasOneRelations() {
|
2008-08-15 05:08:03 +02:00
|
|
|
/* DataObject::write() should save to a has_one relationship if you set a field called (relname)ID */
|
|
|
|
$team = new DataObjectTest_Team();
|
|
|
|
$captainID = $this->idFromFixture('DataObjectTest_Player', 'player1');
|
|
|
|
$team->CaptainID = $captainID;
|
|
|
|
$team->write();
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals($captainID,
|
|
|
|
DB::query("SELECT \"CaptainID\" FROM \"DataObjectTest_Team\" WHERE \"ID\" = $team->ID")->value());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-15 05:08:03 +02:00
|
|
|
/* After giving it a value, you should also be able to set it back to null */
|
|
|
|
$team->CaptainID = '';
|
|
|
|
$team->write();
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals(0,
|
|
|
|
DB::query("SELECT \"CaptainID\" FROM \"DataObjectTest_Team\" WHERE \"ID\" = $team->ID")->value());
|
2008-08-15 05:08:03 +02:00
|
|
|
|
|
|
|
/* You should also be able to save a blank to it when it's first created */
|
|
|
|
$team = new DataObjectTest_Team();
|
|
|
|
$team->CaptainID = '';
|
|
|
|
$team->write();
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals(0,
|
|
|
|
DB::query("SELECT \"CaptainID\" FROM \"DataObjectTest_Team\" WHERE \"ID\" = $team->ID")->value());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-15 05:08:03 +02:00
|
|
|
/* Ditto for existing records without a value */
|
|
|
|
$existingTeam = $this->objFromFixture('DataObjectTest_Team', 'team1');
|
|
|
|
$existingTeam->CaptainID = '';
|
|
|
|
$existingTeam->write();
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals(0,
|
|
|
|
DB::query("SELECT \"CaptainID\" FROM \"DataObjectTest_Team\" WHERE \"ID\" = $existingTeam->ID")->value());
|
2008-08-15 05:08:03 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testCanAccessHasOneObjectsAsMethods() {
|
2012-09-26 23:34:00 +02:00
|
|
|
/* If you have a has_one relation 'Captain' on $obj, and you set the $obj->CaptainID = (ID), then the
|
|
|
|
* object itself should be accessible as $obj->Captain() */
|
2008-10-02 02:45:13 +02:00
|
|
|
$team = $this->objFromFixture('DataObjectTest_Team', 'team1');
|
|
|
|
$captainID = $this->idFromFixture('DataObjectTest_Player', 'captain1');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-02 02:45:13 +02:00
|
|
|
$team->CaptainID = $captainID;
|
|
|
|
$this->assertNotNull($team->Captain());
|
|
|
|
$this->assertEquals($captainID, $team->Captain()->ID);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
// Test for polymorphic has_one relations
|
|
|
|
$fan = $this->objFromFixture('DataObjectTest_Fan', 'fan1');
|
|
|
|
$fan->FavouriteID = $team->ID;
|
|
|
|
$fan->FavouriteClass = $team->class;
|
|
|
|
$this->assertNotNull($fan->Favourite());
|
|
|
|
$this->assertEquals($team->ID, $fan->Favourite()->ID);
|
|
|
|
$this->assertInstanceOf($team->class, $fan->Favourite());
|
2008-10-02 02:45:13 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFieldNamesThatMatchMethodNamesWork() {
|
2008-10-01 02:55:25 +02:00
|
|
|
/* Check that a field name that corresponds to a method on DataObject will still work */
|
2009-06-16 04:56:59 +02:00
|
|
|
$obj = new DataObjectTest_Fixture();
|
2008-10-01 02:55:25 +02:00
|
|
|
$obj->Data = "value1";
|
|
|
|
$obj->DbObject = "value2";
|
|
|
|
$obj->Duplicate = "value3";
|
|
|
|
$obj->write();
|
|
|
|
|
|
|
|
$this->assertNotNull($obj->ID);
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals('value1',
|
|
|
|
DB::query("SELECT \"Data\" FROM \"DataObjectTest_Fixture\" WHERE \"ID\" = $obj->ID")->value());
|
|
|
|
$this->assertEquals('value2',
|
|
|
|
DB::query("SELECT \"DbObject\" FROM \"DataObjectTest_Fixture\" WHERE \"ID\" = $obj->ID")->value());
|
|
|
|
$this->assertEquals('value3',
|
|
|
|
DB::query("SELECT \"Duplicate\" FROM \"DataObjectTest_Fixture\" WHERE \"ID\" = $obj->ID")->value());
|
2008-10-01 02:55:25 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-30 02:27:22 +02:00
|
|
|
/**
|
|
|
|
* @todo Re-enable all test cases for field existence after behaviour has been fixed
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFieldExistence() {
|
2008-09-30 02:20:30 +02:00
|
|
|
$teamInstance = $this->objFromFixture('DataObjectTest_Team', 'team1');
|
|
|
|
$teamSingleton = singleton('DataObjectTest_Team');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-30 02:20:30 +02:00
|
|
|
$subteamInstance = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam1');
|
|
|
|
$subteamSingleton = singleton('DataObjectTest_SubTeam');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-30 02:20:30 +02:00
|
|
|
/* hasField() singleton checks */
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertTrue($teamSingleton->hasField('ID'),
|
|
|
|
'hasField() finds built-in fields in singletons');
|
|
|
|
$this->assertTrue($teamSingleton->hasField('Title'),
|
|
|
|
'hasField() finds custom fields in singletons');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-30 02:20:30 +02:00
|
|
|
/* hasField() instance checks */
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertFalse($teamInstance->hasField('NonExistingField'),
|
|
|
|
'hasField() doesnt find non-existing fields in instances');
|
|
|
|
$this->assertTrue($teamInstance->hasField('ID'),
|
|
|
|
'hasField() finds built-in fields in instances');
|
|
|
|
$this->assertTrue($teamInstance->hasField('Created'),
|
|
|
|
'hasField() finds built-in fields in instances');
|
|
|
|
$this->assertTrue($teamInstance->hasField('DatabaseField'),
|
|
|
|
'hasField() finds custom fields in instances');
|
|
|
|
//$this->assertFalse($teamInstance->hasField('SubclassDatabaseField'),
|
|
|
|
//'hasField() doesnt find subclass fields in parentclass instances');
|
|
|
|
$this->assertTrue($teamInstance->hasField('DynamicField'),
|
|
|
|
'hasField() finds dynamic getters in instances');
|
|
|
|
$this->assertTrue($teamInstance->hasField('HasOneRelationshipID'),
|
|
|
|
'hasField() finds foreign keys in instances');
|
|
|
|
$this->assertTrue($teamInstance->hasField('ExtendedDatabaseField'),
|
|
|
|
'hasField() finds extended fields in instances');
|
|
|
|
$this->assertTrue($teamInstance->hasField('ExtendedHasOneRelationshipID'),
|
|
|
|
'hasField() finds extended foreign keys in instances');
|
|
|
|
//$this->assertTrue($teamInstance->hasField('ExtendedDynamicField'),
|
|
|
|
//'hasField() includes extended dynamic getters in instances');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-30 02:20:30 +02:00
|
|
|
/* hasField() subclass checks */
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertTrue($subteamInstance->hasField('ID'),
|
|
|
|
'hasField() finds built-in fields in subclass instances');
|
|
|
|
$this->assertTrue($subteamInstance->hasField('Created'),
|
|
|
|
'hasField() finds built-in fields in subclass instances');
|
|
|
|
$this->assertTrue($subteamInstance->hasField('DatabaseField'),
|
|
|
|
'hasField() finds custom fields in subclass instances');
|
|
|
|
$this->assertTrue($subteamInstance->hasField('SubclassDatabaseField'),
|
|
|
|
'hasField() finds custom fields in subclass instances');
|
|
|
|
$this->assertTrue($subteamInstance->hasField('DynamicField'),
|
|
|
|
'hasField() finds dynamic getters in subclass instances');
|
|
|
|
$this->assertTrue($subteamInstance->hasField('HasOneRelationshipID'),
|
|
|
|
'hasField() finds foreign keys in subclass instances');
|
|
|
|
$this->assertTrue($subteamInstance->hasField('ExtendedDatabaseField'),
|
|
|
|
'hasField() finds extended fields in subclass instances');
|
|
|
|
$this->assertTrue($subteamInstance->hasField('ExtendedHasOneRelationshipID'),
|
|
|
|
'hasField() finds extended foreign keys in subclass instances');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-30 02:20:30 +02:00
|
|
|
/* hasDatabaseField() singleton checks */
|
2012-09-26 23:34:00 +02:00
|
|
|
//$this->assertTrue($teamSingleton->hasDatabaseField('ID'),
|
|
|
|
//'hasDatabaseField() finds built-in fields in singletons');
|
|
|
|
$this->assertTrue($teamSingleton->hasDatabaseField('Title'),
|
|
|
|
'hasDatabaseField() finds custom fields in singletons');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-30 02:20:30 +02:00
|
|
|
/* hasDatabaseField() instance checks */
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertFalse($teamInstance->hasDatabaseField('NonExistingField'),
|
|
|
|
'hasDatabaseField() doesnt find non-existing fields in instances');
|
|
|
|
//$this->assertTrue($teamInstance->hasDatabaseField('ID'),
|
|
|
|
//'hasDatabaseField() finds built-in fields in instances');
|
|
|
|
$this->assertTrue($teamInstance->hasDatabaseField('Created'),
|
|
|
|
'hasDatabaseField() finds built-in fields in instances');
|
|
|
|
$this->assertTrue($teamInstance->hasDatabaseField('DatabaseField'),
|
|
|
|
'hasDatabaseField() finds custom fields in instances');
|
|
|
|
$this->assertFalse($teamInstance->hasDatabaseField('SubclassDatabaseField'),
|
|
|
|
'hasDatabaseField() doesnt find subclass fields in parentclass instances');
|
|
|
|
//$this->assertFalse($teamInstance->hasDatabaseField('DynamicField'),
|
|
|
|
//'hasDatabaseField() doesnt dynamic getters in instances');
|
|
|
|
$this->assertTrue($teamInstance->hasDatabaseField('HasOneRelationshipID'),
|
|
|
|
'hasDatabaseField() finds foreign keys in instances');
|
|
|
|
$this->assertTrue($teamInstance->hasDatabaseField('ExtendedDatabaseField'),
|
|
|
|
'hasDatabaseField() finds extended fields in instances');
|
|
|
|
$this->assertTrue($teamInstance->hasDatabaseField('ExtendedHasOneRelationshipID'),
|
|
|
|
'hasDatabaseField() finds extended foreign keys in instances');
|
|
|
|
$this->assertFalse($teamInstance->hasDatabaseField('ExtendedDynamicField'),
|
|
|
|
'hasDatabaseField() doesnt include extended dynamic getters in instances');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-30 02:20:30 +02:00
|
|
|
/* hasDatabaseField() subclass checks */
|
2013-07-10 02:44:24 +02:00
|
|
|
$this->assertTrue($subteamInstance->hasDatabaseField('DatabaseField'),
|
2012-09-26 23:34:00 +02:00
|
|
|
'hasField() finds custom fields in subclass instances');
|
2013-07-10 02:44:24 +02:00
|
|
|
$this->assertTrue($subteamInstance->hasDatabaseField('SubclassDatabaseField'),
|
2012-09-26 23:34:00 +02:00
|
|
|
'hasField() finds custom fields in subclass instances');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-30 02:20:30 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-30 02:27:22 +02:00
|
|
|
/**
|
|
|
|
* @todo Re-enable all test cases for field inheritance aggregation after behaviour has been fixed
|
2014-08-15 08:53:05 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFieldInheritance() {
|
2008-09-30 02:20:30 +02:00
|
|
|
$teamInstance = $this->objFromFixture('DataObjectTest_Team', 'team1');
|
|
|
|
$subteamInstance = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam1');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-30 02:20:30 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
array_keys($teamInstance->inheritedDatabaseFields()),
|
|
|
|
array(
|
|
|
|
//'ID',
|
|
|
|
//'ClassName',
|
|
|
|
//'Created',
|
|
|
|
//'LastEdited',
|
|
|
|
'Title',
|
|
|
|
'DatabaseField',
|
2011-04-15 11:35:30 +02:00
|
|
|
'ExtendedDatabaseField',
|
2008-09-30 02:20:30 +02:00
|
|
|
'CaptainID',
|
|
|
|
'HasOneRelationshipID',
|
2011-04-15 11:35:30 +02:00
|
|
|
'ExtendedHasOneRelationshipID'
|
2008-09-30 02:20:30 +02:00
|
|
|
),
|
2012-09-26 23:34:00 +02:00
|
|
|
'inheritedDatabaseFields() contains all fields defined on instance: base, extended and foreign keys'
|
2008-09-30 02:20:30 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-30 02:20:30 +02:00
|
|
|
$this->assertEquals(
|
2013-06-21 00:32:08 +02:00
|
|
|
array_keys(DataObject::database_fields('DataObjectTest_Team', false)),
|
2008-09-30 02:20:30 +02:00
|
|
|
array(
|
|
|
|
//'ID',
|
|
|
|
'ClassName',
|
|
|
|
'LastEdited',
|
2013-06-21 00:32:08 +02:00
|
|
|
'Created',
|
2008-09-30 02:20:30 +02:00
|
|
|
'Title',
|
|
|
|
'DatabaseField',
|
2011-04-15 11:35:30 +02:00
|
|
|
'ExtendedDatabaseField',
|
2008-09-30 02:20:30 +02:00
|
|
|
'CaptainID',
|
|
|
|
'HasOneRelationshipID',
|
2011-04-15 11:35:30 +02:00
|
|
|
'ExtendedHasOneRelationshipID'
|
2008-09-30 02:20:30 +02:00
|
|
|
),
|
2012-09-26 23:34:00 +02:00
|
|
|
'databaseFields() contains only fields defined on instance, including base, extended and foreign keys'
|
2008-09-30 02:20:30 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-30 02:20:30 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
array_keys($subteamInstance->inheritedDatabaseFields()),
|
|
|
|
array(
|
|
|
|
//'ID',
|
|
|
|
//'ClassName',
|
|
|
|
//'Created',
|
|
|
|
//'LastEdited',
|
|
|
|
'SubclassDatabaseField',
|
2012-04-20 00:08:17 +02:00
|
|
|
'ParentTeamID',
|
2008-09-30 02:20:30 +02:00
|
|
|
'Title',
|
|
|
|
'DatabaseField',
|
2011-04-15 11:35:30 +02:00
|
|
|
'ExtendedDatabaseField',
|
2008-09-30 02:20:30 +02:00
|
|
|
'CaptainID',
|
|
|
|
'HasOneRelationshipID',
|
2011-04-15 11:35:30 +02:00
|
|
|
'ExtendedHasOneRelationshipID',
|
2008-09-30 02:20:30 +02:00
|
|
|
),
|
2012-09-26 23:34:00 +02:00
|
|
|
'inheritedDatabaseFields() on subclass contains all fields, including base, extended and foreign keys'
|
2008-09-30 02:20:30 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-30 02:20:30 +02:00
|
|
|
$this->assertEquals(
|
2013-06-21 00:32:08 +02:00
|
|
|
array_keys(DataObject::database_fields('DataObjectTest_SubTeam', false)),
|
2008-09-30 02:20:30 +02:00
|
|
|
array(
|
|
|
|
'SubclassDatabaseField',
|
2012-04-20 00:08:17 +02:00
|
|
|
'ParentTeamID',
|
2008-09-30 02:20:30 +02:00
|
|
|
),
|
|
|
|
'databaseFields() on subclass contains only fields defined on instance'
|
|
|
|
);
|
|
|
|
}
|
2014-02-12 16:37:59 +01:00
|
|
|
|
|
|
|
public function testSearchableFields() {
|
|
|
|
$player = $this->objFromFixture('DataObjectTest_Player', 'captain1');
|
|
|
|
$fields = $player->searchableFields();
|
|
|
|
$this->assertArrayHasKey(
|
|
|
|
'IsRetired',
|
|
|
|
$fields,
|
|
|
|
'Fields defined by $searchable_fields static are correctly detected'
|
|
|
|
);
|
|
|
|
$this->assertArrayHasKey(
|
|
|
|
'ShirtNumber',
|
|
|
|
$fields,
|
|
|
|
'Fields defined by $searchable_fields static are correctly detected'
|
|
|
|
);
|
|
|
|
|
|
|
|
$team = $this->objFromFixture('DataObjectTest_Team', 'team1');
|
2014-08-15 08:53:05 +02:00
|
|
|
$fields = $team->searchableFields();
|
2014-02-12 16:37:59 +01:00
|
|
|
$this->assertArrayHasKey(
|
|
|
|
'Title',
|
|
|
|
$fields,
|
|
|
|
'Fields can be inherited from the $summary_fields static, including methods called on fields'
|
|
|
|
);
|
|
|
|
$this->assertArrayHasKey(
|
|
|
|
'Captain.ShirtNumber',
|
|
|
|
$fields,
|
|
|
|
'Fields on related objects can be inherited from the $summary_fields static'
|
|
|
|
);
|
|
|
|
$this->assertArrayHasKey(
|
|
|
|
'Captain.FavouriteTeam.Title',
|
|
|
|
$fields,
|
|
|
|
'Fields on related objects can be inherited from the $summary_fields static'
|
|
|
|
);
|
|
|
|
|
|
|
|
$testObj = new DataObjectTest_Fixture();
|
|
|
|
$fields = $testObj->searchableFields();
|
|
|
|
$this->assertEmpty($fields);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-10-07 16:12:00 +02:00
|
|
|
public function testCastingHelper() {
|
|
|
|
$team = $this->objFromFixture('DataObjectTest_Team', 'team1');
|
|
|
|
|
|
|
|
$this->assertEquals('Varchar', $team->castingHelper('Title'), 'db field wasn\'t casted correctly');
|
|
|
|
$this->assertEquals('HTMLVarchar', $team->castingHelper('DatabaseField'), 'db field wasn\'t casted correctly');
|
|
|
|
|
|
|
|
$sponsor = $team->Sponsors()->first();
|
|
|
|
$this->assertEquals('Int', $sponsor->castingHelper('SponsorFee'), 'many_many_extraFields not casted correctly');
|
|
|
|
}
|
|
|
|
|
2014-08-14 03:50:23 +02:00
|
|
|
public function testSummaryFieldsCustomLabels() {
|
|
|
|
$team = $this->objFromFixture('DataObjectTest_Team', 'team1');
|
|
|
|
$summaryFields = $team->summaryFields();
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'Custom Title',
|
|
|
|
$summaryFields['Title'],
|
|
|
|
'Custom title is preserved'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'Captain\'s shirt number',
|
|
|
|
$summaryFields['Captain.ShirtNumber'],
|
|
|
|
'Custom title on relation is preserved'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDataObjectUpdate() {
|
2012-09-26 23:34:00 +02:00
|
|
|
/* update() calls can use the dot syntax to reference has_one relations and other methods that return
|
|
|
|
* objects */
|
2008-10-02 02:45:13 +02:00
|
|
|
$team1 = $this->objFromFixture('DataObjectTest_Team', 'team1');
|
|
|
|
$team1->CaptainID = $this->idFromFixture('DataObjectTest_Player', 'captain1');
|
2013-05-09 01:45:25 +02:00
|
|
|
|
2008-10-02 02:45:13 +02:00
|
|
|
$team1->update(array(
|
|
|
|
'DatabaseField' => 'Something',
|
|
|
|
'Captain.FirstName' => 'Jim',
|
|
|
|
'Captain.Email' => 'jim@example.com',
|
|
|
|
'Captain.FavouriteTeam.Title' => 'New and improved team 1',
|
|
|
|
));
|
2013-05-09 01:45:25 +02:00
|
|
|
|
2008-10-02 02:45:13 +02:00
|
|
|
/* Test the simple case of updating fields on the object itself */
|
|
|
|
$this->assertEquals('Something', $team1->DatabaseField);
|
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
/* Setting Captain.Email and Captain.FirstName will have updated DataObjectTest_Captain.captain1 in
|
|
|
|
* the database. Although update() doesn't usually write, it does write related records automatically. */
|
2008-10-02 02:45:13 +02:00
|
|
|
$captain1 = $this->objFromFixture('DataObjectTest_Player', 'captain1');
|
|
|
|
$this->assertEquals('Jim', $captain1->FirstName);
|
|
|
|
$this->assertEquals('jim@example.com', $captain1->Email);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
/* Jim's favourite team is team 1; we need to reload the object to the the change that setting Captain.
|
|
|
|
* FavouriteTeam.Title made */
|
2008-10-02 02:45:13 +02:00
|
|
|
$reloadedTeam1 = $this->objFromFixture('DataObjectTest_Team', 'team1');
|
|
|
|
$this->assertEquals('New and improved team 1', $reloadedTeam1->Title);
|
|
|
|
}
|
2009-03-04 04:44:11 +01:00
|
|
|
|
2013-05-09 01:45:25 +02:00
|
|
|
public function testDataObjectUpdateNew() {
|
|
|
|
/* update() calls can use the dot syntax to reference has_one relations and other methods that return
|
|
|
|
* objects */
|
|
|
|
$team1 = $this->objFromFixture('DataObjectTest_Team', 'team1');
|
|
|
|
$team1->CaptainID = 0;
|
|
|
|
|
|
|
|
$team1->update(array(
|
|
|
|
'Captain.FirstName' => 'Jim',
|
|
|
|
'Captain.FavouriteTeam.Title' => 'New and improved team 1',
|
|
|
|
));
|
|
|
|
/* Test that the captain ID has been updated */
|
|
|
|
$this->assertGreaterThan(0, $team1->CaptainID);
|
|
|
|
|
|
|
|
/* Fetch the newly created captain */
|
|
|
|
$captain1 = DataObjectTest_Player::get()->byID($team1->CaptainID);
|
|
|
|
$this->assertEquals('Jim', $captain1->FirstName);
|
|
|
|
|
|
|
|
/* Grab the favourite team and make sure it has the correct values */
|
|
|
|
$reloadedTeam1 = $captain1->FavouriteTeam();
|
|
|
|
$this->assertEquals($reloadedTeam1->ID, $captain1->FavouriteTeamID);
|
|
|
|
$this->assertEquals('New and improved team 1', $reloadedTeam1->Title);
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:44:11 +01:00
|
|
|
public function testWritingInvalidDataObjectThrowsException() {
|
|
|
|
$validatedObject = new DataObjectTest_ValidatedObject();
|
|
|
|
|
|
|
|
$this->setExpectedException('ValidationException');
|
|
|
|
$validatedObject->write();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-04 04:44:11 +01:00
|
|
|
public function testWritingValidDataObjectDoesntThrowException() {
|
2008-10-08 22:42:09 +02:00
|
|
|
$validatedObject = new DataObjectTest_ValidatedObject();
|
|
|
|
$validatedObject->Name = "Mr. Jones";
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-04 04:44:11 +01:00
|
|
|
$validatedObject->write();
|
|
|
|
$this->assertTrue($validatedObject->isInDB(), "Validated object was not saved to database");
|
2008-10-08 22:42:09 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-28 02:23:41 +01:00
|
|
|
public function testSubclassCreation() {
|
|
|
|
/* Creating a new object of a subclass should set the ClassName field correctly */
|
|
|
|
$obj = new DataObjectTest_SubTeam();
|
|
|
|
$obj->write();
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals("DataObjectTest_SubTeam",
|
|
|
|
DB::query("SELECT \"ClassName\" FROM \"DataObjectTest_Team\" WHERE \"ID\" = $obj->ID")->value());
|
2008-10-28 02:23:41 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testForceInsert() {
|
2008-10-28 02:23:41 +01:00
|
|
|
/* If you set an ID on an object and pass forceInsert = true, then the object should be correctly created */
|
2013-06-21 00:32:08 +02:00
|
|
|
$conn = DB::get_conn();
|
2009-08-08 06:23:05 +02:00
|
|
|
if(method_exists($conn, 'allowPrimaryKeyEditing')) $conn->allowPrimaryKeyEditing('DataObjectTest_Team', true);
|
2008-10-28 02:23:41 +01:00
|
|
|
$obj = new DataObjectTest_SubTeam();
|
|
|
|
$obj->ID = 1001;
|
|
|
|
$obj->Title = 'asdfasdf';
|
|
|
|
$obj->SubclassDatabaseField = 'asdfasdf';
|
|
|
|
$obj->write(false, true);
|
2009-08-08 06:23:05 +02:00
|
|
|
if(method_exists($conn, 'allowPrimaryKeyEditing')) $conn->allowPrimaryKeyEditing('DataObjectTest_Team', false);
|
2008-10-28 02:23:41 +01:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals("DataObjectTest_SubTeam",
|
|
|
|
DB::query("SELECT \"ClassName\" FROM \"DataObjectTest_Team\" WHERE \"ID\" = $obj->ID")->value());
|
2008-10-28 02:23:41 +01:00
|
|
|
|
|
|
|
/* Check that it actually saves to the database with the correct ID */
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals("1001", DB::query(
|
|
|
|
"SELECT \"ID\" FROM \"DataObjectTest_SubTeam\" WHERE \"SubclassDatabaseField\" = 'asdfasdf'")->value());
|
|
|
|
$this->assertEquals("1001",
|
|
|
|
DB::query("SELECT \"ID\" FROM \"DataObjectTest_Team\" WHERE \"Title\" = 'asdfasdf'")->value());
|
2008-10-28 02:23:41 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-06 05:51:25 +01:00
|
|
|
public function TestHasOwnTable() {
|
|
|
|
/* Test DataObject::has_own_table() returns true if the object has $has_one or $db values */
|
|
|
|
$this->assertTrue(DataObject::has_own_table("DataObjectTest_Player"));
|
|
|
|
$this->assertTrue(DataObject::has_own_table("DataObjectTest_Team"));
|
2009-06-16 04:56:59 +02:00
|
|
|
$this->assertTrue(DataObject::has_own_table("DataObjectTest_Fixture"));
|
2008-11-06 05:51:25 +01:00
|
|
|
|
|
|
|
/* Root DataObject that always have a table, even if they lack both $db and $has_one */
|
|
|
|
$this->assertTrue(DataObject::has_own_table("DataObjectTest_FieldlessTable"));
|
|
|
|
|
|
|
|
/* Subclasses without $db or $has_one don't have a table */
|
|
|
|
$this->assertFalse(DataObject::has_own_table("DataObjectTest_FieldlessSubTable"));
|
|
|
|
|
|
|
|
/* Return false if you don't pass it a subclass of DataObject */
|
|
|
|
$this->assertFalse(DataObject::has_own_table("DataObject"));
|
|
|
|
$this->assertFalse(DataObject::has_own_table("ViewableData"));
|
|
|
|
$this->assertFalse(DataObject::has_own_table("ThisIsntADataObject"));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-01-07 02:25:43 +01:00
|
|
|
public function testMerge() {
|
|
|
|
// test right merge of subclasses
|
|
|
|
$left = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam1');
|
|
|
|
$right = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam2_with_player_relation');
|
|
|
|
$leftOrigID = $left->ID;
|
|
|
|
$left->merge($right, 'right', false, false);
|
|
|
|
$this->assertEquals(
|
|
|
|
$left->Title,
|
|
|
|
'Subteam 2',
|
|
|
|
'merge() with "right" priority overwrites fields with existing values on subclasses'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$left->ID,
|
|
|
|
$leftOrigID,
|
|
|
|
'merge() with "right" priority doesnt overwrite database ID'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-01-07 02:25:43 +01:00
|
|
|
// test overwriteWithEmpty flag on existing left values
|
|
|
|
$left = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam2_with_player_relation');
|
|
|
|
$right = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam3_with_empty_fields');
|
|
|
|
$left->merge($right, 'right', false, true);
|
|
|
|
$this->assertEquals(
|
|
|
|
$left->Title,
|
2014-08-15 08:53:05 +02:00
|
|
|
'Subteam 3',
|
2009-01-07 02:25:43 +01:00
|
|
|
'merge() with $overwriteWithEmpty overwrites non-empty fields on left object'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-01-07 02:25:43 +01:00
|
|
|
// test overwriteWithEmpty flag on empty left values
|
|
|
|
$left = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam1');
|
2012-09-26 23:34:00 +02:00
|
|
|
// $SubclassDatabaseField is empty on here
|
2014-08-15 08:53:05 +02:00
|
|
|
$right = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam2_with_player_relation');
|
2009-01-07 02:25:43 +01:00
|
|
|
$left->merge($right, 'right', false, true);
|
|
|
|
$this->assertEquals(
|
|
|
|
$left->SubclassDatabaseField,
|
2014-08-15 08:53:05 +02:00
|
|
|
NULL,
|
2009-01-07 02:25:43 +01:00
|
|
|
'merge() with $overwriteWithEmpty overwrites empty fields on left object'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-01-07 02:25:43 +01:00
|
|
|
// @todo test "left" priority flag
|
|
|
|
// @todo test includeRelations flag
|
|
|
|
// @todo test includeRelations in combination with overwriteWithEmpty
|
|
|
|
// @todo test has_one relations
|
|
|
|
// @todo test has_many and many_many relations
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testPopulateDefaults() {
|
2009-06-16 04:56:59 +02:00
|
|
|
$obj = new DataObjectTest_Fixture();
|
2009-03-16 14:43:03 +01:00
|
|
|
$this->assertEquals(
|
2009-06-16 04:56:59 +02:00
|
|
|
$obj->MyFieldWithDefault,
|
2012-11-06 23:28:36 +01:00
|
|
|
'Default Value',
|
|
|
|
'Defaults are populated for in-memory object from $defaults array'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$obj->MyFieldWithAltDefault,
|
|
|
|
'Default Value',
|
|
|
|
'Defaults are populated from overloaded populateDefaults() method'
|
2009-03-16 14:43:03 +01:00
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-03-03 13:41:04 +01:00
|
|
|
protected function makeAccessible($object, $method) {
|
|
|
|
$reflectionMethod = new ReflectionMethod($object, $method);
|
|
|
|
$reflectionMethod->setAccessible(true);
|
|
|
|
return $reflectionMethod;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateModelDefinitionsFailsWithArray() {
|
2016-05-18 01:00:04 +02:00
|
|
|
|
2015-03-03 13:41:04 +01:00
|
|
|
$object = new DataObjectTest_Team;
|
|
|
|
$method = $this->makeAccessible($object, 'validateModelDefinitions');
|
|
|
|
|
|
|
|
Config::inst()->update('DataObjectTest_Team', 'has_one', array('NotValid' => array('NoArraysAllowed')));
|
|
|
|
$this->setExpectedException('LogicException');
|
|
|
|
|
2017-05-24 15:13:46 +02:00
|
|
|
$method->invoke($object);
|
2015-03-03 13:41:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateModelDefinitionsFailsWithIntKey() {
|
|
|
|
$object = new DataObjectTest_Team;
|
|
|
|
$method = $this->makeAccessible($object, 'validateModelDefinitions');
|
|
|
|
|
|
|
|
Config::inst()->update('DataObjectTest_Team', 'has_many', array(12 => 'DataObjectTest_Player'));
|
|
|
|
$this->setExpectedException('LogicException');
|
|
|
|
|
2017-05-24 15:13:46 +02:00
|
|
|
$method->invoke($object);
|
2015-03-03 13:41:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateModelDefinitionsFailsWithIntValue() {
|
|
|
|
$object = new DataObjectTest_Team;
|
|
|
|
$method = $this->makeAccessible($object, 'validateModelDefinitions');
|
|
|
|
|
|
|
|
Config::inst()->update('DataObjectTest_Team', 'many_many', array('Players' => 12));
|
|
|
|
$this->setExpectedException('LogicException');
|
|
|
|
|
2017-05-24 15:13:46 +02:00
|
|
|
$method->invoke($object);
|
2015-03-03 13:41:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* many_many_extraFields is allowed to have an array value, so shouldn't throw an exception
|
|
|
|
*/
|
|
|
|
public function testValidateModelDefinitionsPassesWithExtraFields() {
|
|
|
|
$object = new DataObjectTest_Team;
|
|
|
|
$method = $this->makeAccessible($object, 'validateModelDefinitions');
|
|
|
|
|
|
|
|
Config::inst()->update('DataObjectTest_Team', 'many_many_extraFields',
|
|
|
|
array('Relations' => array('Price' => 'Int')));
|
|
|
|
|
|
|
|
try {
|
|
|
|
$method->invoke($object);
|
|
|
|
} catch(Exception $e) {
|
|
|
|
$this->fail('Exception should not be thrown');
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testNewClassInstance() {
|
2011-05-19 22:38:21 +02:00
|
|
|
$dataObject = $this->objFromFixture('DataObjectTest_Team', 'team1');
|
|
|
|
$changedDO = $dataObject->newClassInstance('DataObjectTest_SubTeam');
|
2011-03-18 03:01:09 +01:00
|
|
|
$changedFields = $changedDO->getChangedFields();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-27 07:55:25 +02:00
|
|
|
// Don't write the record, it will reset changed fields
|
2012-05-09 12:43:22 +02:00
|
|
|
$this->assertInstanceOf('DataObjectTest_SubTeam', $changedDO);
|
2011-05-19 22:38:21 +02:00
|
|
|
$this->assertEquals($changedDO->ClassName, 'DataObjectTest_SubTeam');
|
2009-04-27 07:55:25 +02:00
|
|
|
$this->assertContains('ClassName', array_keys($changedFields));
|
2011-05-19 22:38:21 +02:00
|
|
|
$this->assertEquals($changedFields['ClassName']['before'], 'DataObjectTest_Team');
|
|
|
|
$this->assertEquals($changedFields['ClassName']['after'], 'DataObjectTest_SubTeam');
|
|
|
|
|
2011-03-18 03:01:09 +01:00
|
|
|
$changedDO->write();
|
2011-05-19 22:38:21 +02:00
|
|
|
|
2012-05-09 12:43:22 +02:00
|
|
|
$this->assertInstanceOf('DataObjectTest_SubTeam', $changedDO);
|
2011-05-19 22:38:21 +02:00
|
|
|
$this->assertEquals($changedDO->ClassName, 'DataObjectTest_SubTeam');
|
2009-04-27 07:55:25 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-01-24 00:13:17 +01:00
|
|
|
public function testMultipleManyManyWithSameClass() {
|
|
|
|
$team = $this->objFromFixture('DataObjectTest_Team', 'team1');
|
|
|
|
$sponsors = $team->Sponsors();
|
|
|
|
$equipmentSuppliers = $team->EquipmentSuppliers();
|
|
|
|
|
|
|
|
// Check that DataObject::many_many() works as expected
|
2015-02-25 16:21:33 +01:00
|
|
|
list($class, $targetClass, $parentField, $childField, $joinTable) = $team->manyManyComponent('Sponsors');
|
2015-01-24 00:13:17 +01:00
|
|
|
$this->assertEquals('DataObjectTest_Team', $class,
|
|
|
|
'DataObject::many_many() didn\'t find the correct base class');
|
|
|
|
$this->assertEquals('DataObjectTest_EquipmentCompany', $targetClass,
|
|
|
|
'DataObject::many_many() didn\'t find the correct target class for the relation');
|
|
|
|
$this->assertEquals('DataObjectTest_EquipmentCompany_SponsoredTeams', $joinTable,
|
|
|
|
'DataObject::many_many() didn\'t find the correct relation table');
|
|
|
|
|
|
|
|
// Check that ManyManyList still works
|
|
|
|
$this->assertEquals(2, $sponsors->count(), 'Rows are missing from relation');
|
|
|
|
$this->assertEquals(1, $equipmentSuppliers->count(), 'Rows are missing from relation');
|
|
|
|
|
|
|
|
// Check everything works when no relation is present
|
|
|
|
$teamWithoutSponsor = $this->objFromFixture('DataObjectTest_Team', 'team3');
|
|
|
|
$this->assertInstanceOf('ManyManyList', $teamWithoutSponsor->Sponsors());
|
|
|
|
$this->assertEquals(0, $teamWithoutSponsor->Sponsors()->count());
|
|
|
|
|
|
|
|
// Check many_many_extraFields still works
|
|
|
|
$equipmentCompany = $this->objFromFixture('DataObjectTest_EquipmentCompany', 'equipmentcompany1');
|
|
|
|
$equipmentCompany->SponsoredTeams()->add($teamWithoutSponsor, array('SponsorFee' => 1000));
|
|
|
|
$sponsoredTeams = $equipmentCompany->SponsoredTeams();
|
|
|
|
$this->assertEquals(1000, $sponsoredTeams->byID($teamWithoutSponsor->ID)->SponsorFee,
|
|
|
|
'Data from many_many_extraFields was not stored/extracted correctly');
|
|
|
|
|
|
|
|
// Check subclasses correctly inherit multiple many_manys
|
|
|
|
$subTeam = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam1');
|
|
|
|
$this->assertEquals(2, $subTeam->Sponsors()->count(),
|
|
|
|
'Child class did not inherit multiple many_manys');
|
|
|
|
$this->assertEquals(1, $subTeam->EquipmentSuppliers()->count(),
|
|
|
|
'Child class did not inherit multiple many_manys');
|
|
|
|
// Team 2 has one EquipmentCompany sponsor and one SubEquipmentCompany
|
|
|
|
$team2 = $this->objFromFixture('DataObjectTest_Team', 'team2');
|
|
|
|
$this->assertEquals(2, $team2->Sponsors()->count(),
|
|
|
|
'Child class did not inherit multiple belongs_many_manys');
|
|
|
|
|
|
|
|
// Check many_many_extraFields also works from the belongs_many_many side
|
|
|
|
$sponsors = $team2->Sponsors();
|
|
|
|
$sponsors->add($equipmentCompany, array('SponsorFee' => 750));
|
|
|
|
$this->assertEquals(750, $sponsors->byID($equipmentCompany->ID)->SponsorFee,
|
|
|
|
'Data from many_many_extraFields was not stored/extracted correctly');
|
|
|
|
|
|
|
|
$subEquipmentCompany = $this->objFromFixture('DataObjectTest_SubEquipmentCompany', 'subequipmentcompany1');
|
|
|
|
$subTeam->Sponsors()->add($subEquipmentCompany, array('SponsorFee' => 1200));
|
|
|
|
$this->assertEquals(1200, $subTeam->Sponsors()->byID($subEquipmentCompany->ID)->SponsorFee,
|
|
|
|
'Data from inherited many_many_extraFields was not stored/extracted correctly');
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testManyManyExtraFields() {
|
2009-07-08 02:06:16 +02:00
|
|
|
$player = $this->objFromFixture('DataObjectTest_Player', 'player1');
|
2012-04-20 00:08:17 +02:00
|
|
|
$team = $this->objFromFixture('DataObjectTest_Team', 'team1');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-02-25 10:49:36 +01:00
|
|
|
// Get all extra fields
|
2015-02-25 16:21:33 +01:00
|
|
|
$teamExtraFields = $team->manyManyExtraFields();
|
2015-02-25 10:49:36 +01:00
|
|
|
$this->assertEquals(array(
|
|
|
|
'Players' => array('Position' => 'Varchar(100)')
|
|
|
|
), $teamExtraFields);
|
|
|
|
|
|
|
|
// Ensure fields from parent classes are included
|
|
|
|
$subTeam = singleton('DataObjectTest_SubTeam');
|
2015-02-25 16:21:33 +01:00
|
|
|
$teamExtraFields = $subTeam->manyManyExtraFields();
|
2015-02-25 10:49:36 +01:00
|
|
|
$this->assertEquals(array(
|
|
|
|
'Players' => array('Position' => 'Varchar(100)'),
|
|
|
|
'FormerPlayers' => array('Position' => 'Varchar(100)')
|
|
|
|
), $teamExtraFields);
|
2015-02-25 12:13:26 +01:00
|
|
|
|
2009-05-01 05:49:34 +02:00
|
|
|
// Extra fields are immediately available on the Team class (defined in $many_many_extraFields)
|
2015-02-25 16:21:33 +01:00
|
|
|
$teamExtraFields = $team->manyManyExtraFieldsForComponent('Players');
|
2009-05-01 05:49:34 +02:00
|
|
|
$this->assertEquals($teamExtraFields, array(
|
|
|
|
'Position' => 'Varchar(100)'
|
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-01 05:49:34 +02:00
|
|
|
// We'll have to go through the relation to get the extra fields on Player
|
2015-02-25 16:21:33 +01:00
|
|
|
$playerExtraFields = $player->manyManyExtraFieldsForComponent('Teams');
|
2009-05-01 05:49:34 +02:00
|
|
|
$this->assertEquals($playerExtraFields, array(
|
|
|
|
'Position' => 'Varchar(100)'
|
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-16 00:40:38 +02:00
|
|
|
// Iterate through a many-many relationship and confirm that extra fields are included
|
|
|
|
$newTeam = new DataObjectTest_Team();
|
|
|
|
$newTeam->Title = "New team";
|
|
|
|
$newTeam->write();
|
|
|
|
$newTeamID = $newTeam->ID;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-16 00:40:38 +02:00
|
|
|
$newPlayer = new DataObjectTest_Player();
|
|
|
|
$newPlayer->FirstName = "Sam";
|
|
|
|
$newPlayer->Surname = "Minnee";
|
|
|
|
$newPlayer->write();
|
|
|
|
|
|
|
|
// The idea of Sam as a prop is essentially humourous.
|
|
|
|
$newTeam->Players()->add($newPlayer, array("Position" => "Prop"));
|
|
|
|
|
|
|
|
// Requery and uncache everything
|
|
|
|
$newTeam->flushCache();
|
|
|
|
$newTeam = DataObject::get_by_id('DataObjectTest_Team', $newTeamID);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-16 00:40:38 +02:00
|
|
|
// Check that the Position many_many_extraField is extracted.
|
|
|
|
$player = $newTeam->Players()->First();
|
|
|
|
$this->assertEquals('Sam', $player->FirstName);
|
|
|
|
$this->assertEquals("Prop", $player->Position);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-13 06:00:52 +02:00
|
|
|
// Check that ordering a many-many relation by an aggregate column doesn't fail
|
|
|
|
$player = $this->objFromFixture('DataObjectTest_Player', 'player2');
|
2015-02-25 16:21:33 +01:00
|
|
|
$player->Teams()->sort("count(DISTINCT \"DataObjectTest_Team_Players\".\"DataObjectTest_PlayerID\") DESC");
|
2009-05-01 05:49:34 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-13 06:00:14 +02:00
|
|
|
/**
|
|
|
|
* Check that the queries generated for many-many relation queries can have unlimitedRowCount
|
|
|
|
* called on them.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testManyManyUnlimitedRowCount() {
|
2010-10-13 06:00:14 +02:00
|
|
|
$player = $this->objFromFixture('DataObjectTest_Player', 'player2');
|
2009-11-22 06:16:38 +01:00
|
|
|
// TODO: What's going on here?
|
|
|
|
$this->assertEquals(2, $player->Teams()->dataQuery()->query()->unlimitedRowCount());
|
2010-10-13 06:00:14 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-22 05:49:15 +02:00
|
|
|
/**
|
|
|
|
* Tests that singular_name() generates sensible defaults.
|
|
|
|
*/
|
|
|
|
public function testSingularName() {
|
2014-08-20 04:50:42 +02:00
|
|
|
$assertions = array(
|
2009-05-22 05:49:15 +02:00
|
|
|
'DataObjectTest_Player' => 'Data Object Test Player',
|
|
|
|
'DataObjectTest_Team' => 'Data Object Test Team',
|
2009-06-16 04:56:59 +02:00
|
|
|
'DataObjectTest_Fixture' => 'Data Object Test Fixture'
|
2009-05-22 05:49:15 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-22 05:49:15 +02:00
|
|
|
foreach($assertions as $class => $expectedSingularName) {
|
2014-08-20 04:50:42 +02:00
|
|
|
$this->assertEquals(
|
2009-05-22 05:49:15 +02:00
|
|
|
$expectedSingularName,
|
|
|
|
singleton($class)->singular_name(),
|
|
|
|
"Assert that the singular_name for '$class' is correct."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-08-20 04:50:42 +02:00
|
|
|
/**
|
|
|
|
* Tests that plural_name() generates sensible defaults.
|
|
|
|
*/
|
|
|
|
public function testPluralName() {
|
|
|
|
$assertions = array(
|
|
|
|
'DataObjectTest_Player' => 'Data Object Test Players',
|
|
|
|
'DataObjectTest_Team' => 'Data Object Test Teams',
|
2015-11-06 15:38:59 +01:00
|
|
|
'DataObjectTest_Fixture' => 'Data Object Test Fixtures',
|
|
|
|
'DataObjectTest_Play' => 'Data Object Test Plays',
|
|
|
|
'DataObjectTest_Bogey' => 'Data Object Test Bogeys',
|
|
|
|
'DataObjectTest_Ploy' => 'Data Object Test Ploys',
|
2014-08-20 04:50:42 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
foreach($assertions as $class => $expectedPluralName) {
|
|
|
|
$this->assertEquals(
|
|
|
|
$expectedPluralName,
|
|
|
|
singleton($class)->plural_name(),
|
|
|
|
"Assert that the plural_name for '$class' is correct."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testHasDatabaseField() {
|
2009-06-02 05:43:45 +02:00
|
|
|
$team = singleton('DataObjectTest_Team');
|
|
|
|
$subteam = singleton('DataObjectTest_SubTeam');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-02 05:43:45 +02:00
|
|
|
$this->assertTrue(
|
|
|
|
$team->hasDatabaseField('Title'),
|
|
|
|
"hasOwnDatabaseField() works with \$db fields"
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$team->hasDatabaseField('CaptainID'),
|
|
|
|
"hasOwnDatabaseField() works with \$has_one fields"
|
|
|
|
);
|
|
|
|
$this->assertFalse(
|
|
|
|
$team->hasDatabaseField('NonExistentField'),
|
|
|
|
"hasOwnDatabaseField() doesn't detect non-existend fields"
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
2011-04-15 11:35:30 +02:00
|
|
|
$team->hasDatabaseField('ExtendedDatabaseField'),
|
|
|
|
"hasOwnDatabaseField() works with extended fields"
|
2009-06-02 05:43:45 +02:00
|
|
|
);
|
|
|
|
$this->assertFalse(
|
|
|
|
$team->hasDatabaseField('SubclassDatabaseField'),
|
|
|
|
"hasOwnDatabaseField() doesn't pick up fields in subclasses on parent class"
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-04 23:00:41 +02:00
|
|
|
$this->assertTrue(
|
2009-06-02 05:43:45 +02:00
|
|
|
$subteam->hasDatabaseField('SubclassDatabaseField'),
|
|
|
|
"hasOwnDatabaseField() picks up fields in subclasses"
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-02 05:43:45 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFieldTypes() {
|
2009-06-16 04:56:59 +02:00
|
|
|
$obj = new DataObjectTest_Fixture();
|
|
|
|
$obj->DateField = '1988-01-02';
|
|
|
|
$obj->DatetimeField = '1988-03-04 06:30';
|
|
|
|
$obj->write();
|
|
|
|
$obj->flushCache();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-16 04:56:59 +02:00
|
|
|
$obj = DataObject::get_by_id('DataObjectTest_Fixture', $obj->ID);
|
|
|
|
$this->assertEquals('1988-01-02', $obj->DateField);
|
2009-06-16 05:21:13 +02:00
|
|
|
$this->assertEquals('1988-03-04 06:30:00', $obj->DatetimeField);
|
2009-06-16 04:56:59 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testTwoSubclassesWithTheSameFieldNameWork() {
|
2009-08-19 06:34:28 +02:00
|
|
|
// Create two objects of different subclasses, setting the values of fields that are
|
|
|
|
// defined separately in each subclass
|
|
|
|
$obj1 = new DataObjectTest_SubTeam();
|
|
|
|
$obj1->SubclassDatabaseField = "obj1";
|
|
|
|
$obj2 = new OtherSubclassWithSameField();
|
|
|
|
$obj2->SubclassDatabaseField = "obj2";
|
|
|
|
|
|
|
|
// Write them to the database
|
|
|
|
$obj1->write();
|
|
|
|
$obj2->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-19 06:34:28 +02:00
|
|
|
// Check that the values of those fields are properly read from the database
|
2014-08-15 08:53:05 +02:00
|
|
|
$values = DataObject::get("DataObjectTest_Team", "\"DataObjectTest_Team\".\"ID\" IN
|
2009-08-19 06:34:28 +02:00
|
|
|
($obj1->ID, $obj2->ID)")->column("SubclassDatabaseField");
|
2012-08-23 07:34:17 +02:00
|
|
|
$this->assertEquals(array_intersect($values, array('obj1', 'obj2')), $values);
|
2009-08-19 06:34:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testClassNameSetForNewObjects() {
|
2009-08-21 07:02:31 +02:00
|
|
|
$d = new DataObjectTest_Player();
|
|
|
|
$this->assertEquals('DataObjectTest_Player', $d->ClassName);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
public function testHasValue() {
|
|
|
|
$team = new DataObjectTest_Team();
|
|
|
|
$this->assertFalse($team->hasValue('Title', null, false));
|
|
|
|
$this->assertFalse($team->hasValue('DatabaseField', null, false));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
$team->Title = 'hasValue';
|
|
|
|
$this->assertTrue($team->hasValue('Title', null, false));
|
|
|
|
$this->assertFalse($team->hasValue('DatabaseField', null, false));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
$team->DatabaseField = '<p></p>';
|
|
|
|
$this->assertTrue($team->hasValue('Title', null, false));
|
|
|
|
$this->assertFalse (
|
|
|
|
$team->hasValue('DatabaseField', null, false),
|
|
|
|
'Test that a blank paragraph on a HTML field is not a valid value.'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
$team->Title = '<p></p>';
|
|
|
|
$this->assertTrue (
|
|
|
|
$team->hasValue('Title', null, false),
|
|
|
|
'Test that an empty paragraph is a value for non-HTML fields.'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
$team->DatabaseField = 'hasValue';
|
|
|
|
$this->assertTrue($team->hasValue('Title', null, false));
|
|
|
|
$this->assertTrue($team->hasValue('DatabaseField', null, false));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-24 01:26:10 +02:00
|
|
|
public function testHasMany() {
|
|
|
|
$company = new DataObjectTest_Company();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-24 01:26:10 +02:00
|
|
|
$this->assertEquals (
|
|
|
|
array (
|
|
|
|
'CurrentStaff' => 'DataObjectTest_Staff',
|
|
|
|
'PreviousStaff' => 'DataObjectTest_Staff'
|
|
|
|
),
|
2015-02-25 16:21:33 +01:00
|
|
|
$company->hasMany(),
|
2009-10-24 01:26:10 +02:00
|
|
|
'has_many strips field name data by default.'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-24 01:26:10 +02:00
|
|
|
$this->assertEquals (
|
|
|
|
'DataObjectTest_Staff',
|
2015-02-25 16:21:33 +01:00
|
|
|
$company->hasManyComponent('CurrentStaff'),
|
2009-10-24 01:26:10 +02:00
|
|
|
'has_many strips field name data by default on single relationships.'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-24 01:26:10 +02:00
|
|
|
$this->assertEquals (
|
|
|
|
array (
|
|
|
|
'CurrentStaff' => 'DataObjectTest_Staff.CurrentCompany',
|
|
|
|
'PreviousStaff' => 'DataObjectTest_Staff.PreviousCompany'
|
|
|
|
),
|
2015-02-25 16:21:33 +01:00
|
|
|
$company->hasMany(null, false),
|
2009-10-24 01:26:10 +02:00
|
|
|
'has_many returns field name data when $classOnly is false.'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-24 01:26:10 +02:00
|
|
|
$this->assertEquals (
|
|
|
|
'DataObjectTest_Staff.CurrentCompany',
|
2015-02-25 16:21:33 +01:00
|
|
|
$company->hasManyComponent('CurrentStaff', false),
|
2009-10-24 01:26:10 +02:00
|
|
|
'has_many returns field name data on single records when $classOnly is false.'
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-24 01:26:10 +02:00
|
|
|
public function testGetRemoteJoinField() {
|
|
|
|
$company = new DataObjectTest_Company();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
$staffJoinField = $company->getRemoteJoinField('CurrentStaff', 'has_many', $polymorphic);
|
|
|
|
$this->assertEquals('CurrentCompanyID', $staffJoinField);
|
|
|
|
$this->assertFalse($polymorphic, 'DataObjectTest_Company->CurrentStaff is not polymorphic');
|
|
|
|
$previousStaffJoinField = $company->getRemoteJoinField('PreviousStaff', 'has_many', $polymorphic);
|
|
|
|
$this->assertEquals('PreviousCompanyID', $previousStaffJoinField);
|
|
|
|
$this->assertFalse($polymorphic, 'DataObjectTest_Company->PreviousStaff is not polymorphic');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-24 01:27:51 +02:00
|
|
|
$ceo = new DataObjectTest_CEO();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
$this->assertEquals('CEOID', $ceo->getRemoteJoinField('Company', 'belongs_to', $polymorphic));
|
|
|
|
$this->assertFalse($polymorphic, 'DataObjectTest_CEO->Company is not polymorphic');
|
|
|
|
$this->assertEquals('PreviousCEOID', $ceo->getRemoteJoinField('PreviousCompany', 'belongs_to', $polymorphic));
|
|
|
|
$this->assertFalse($polymorphic, 'DataObjectTest_CEO->PreviousCompany is not polymorphic');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
$team = new DataObjectTest_Team();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
$this->assertEquals('Favourite', $team->getRemoteJoinField('Fans', 'has_many', $polymorphic));
|
|
|
|
$this->assertTrue($polymorphic, 'DataObjectTest_Team->Fans is polymorphic');
|
|
|
|
$this->assertEquals('TeamID', $team->getRemoteJoinField('Comments', 'has_many', $polymorphic));
|
|
|
|
$this->assertFalse($polymorphic, 'DataObjectTest_Team->Comments is not polymorphic');
|
2009-10-24 01:27:51 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-24 01:27:51 +02:00
|
|
|
public function testBelongsTo() {
|
|
|
|
$company = new DataObjectTest_Company();
|
|
|
|
$ceo = new DataObjectTest_CEO();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-24 01:27:51 +02:00
|
|
|
$company->write();
|
|
|
|
$ceo->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
// Test belongs_to assignment
|
2009-10-24 01:27:51 +02:00
|
|
|
$company->CEOID = $ceo->ID;
|
|
|
|
$company->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-24 01:27:51 +02:00
|
|
|
$this->assertEquals($company->ID, $ceo->Company()->ID, 'belongs_to returns the right results.');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
// Test automatic creation of class where no assigment exists
|
2009-10-24 01:27:51 +02:00
|
|
|
$ceo = new DataObjectTest_CEO();
|
|
|
|
$ceo->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-24 01:27:51 +02:00
|
|
|
$this->assertTrue (
|
|
|
|
$ceo->Company() instanceof DataObjectTest_Company,
|
|
|
|
'DataObjects across belongs_to relations are automatically created.'
|
|
|
|
);
|
|
|
|
$this->assertEquals($ceo->ID, $ceo->Company()->CEOID, 'Remote IDs are automatically set.');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
// Write object with components
|
2013-06-21 00:32:08 +02:00
|
|
|
$ceo->Name = 'Edward Scissorhands';
|
2009-10-24 01:27:51 +02:00
|
|
|
$ceo->write(false, false, false, true);
|
|
|
|
$this->assertTrue($ceo->Company()->isInDB(), 'write() writes belongs_to components to the database.');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-24 01:27:51 +02:00
|
|
|
$newCEO = DataObject::get_by_id('DataObjectTest_CEO', $ceo->ID);
|
|
|
|
$this->assertEquals (
|
|
|
|
$ceo->Company()->ID, $newCEO->Company()->ID, 'belongs_to can be retrieved from the database.'
|
|
|
|
);
|
2009-10-24 01:26:10 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
public function testBelongsToPolymorphic() {
|
|
|
|
$company = new DataObjectTest_Company();
|
|
|
|
$ceo = new DataObjectTest_CEO();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
$company->write();
|
|
|
|
$ceo->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
// Test belongs_to assignment
|
|
|
|
$company->OwnerID = $ceo->ID;
|
|
|
|
$company->OwnerClass = $ceo->class;
|
|
|
|
$company->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
$this->assertEquals($company->ID, $ceo->CompanyOwned()->ID, 'belongs_to returns the right results.');
|
|
|
|
$this->assertEquals($company->class, $ceo->CompanyOwned()->class, 'belongs_to returns the right results.');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
// Test automatic creation of class where no assigment exists
|
|
|
|
$ceo = new DataObjectTest_CEO();
|
|
|
|
$ceo->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
$this->assertTrue (
|
|
|
|
$ceo->CompanyOwned() instanceof DataObjectTest_Company,
|
|
|
|
'DataObjects across polymorphic belongs_to relations are automatically created.'
|
|
|
|
);
|
|
|
|
$this->assertEquals($ceo->ID, $ceo->CompanyOwned()->OwnerID, 'Remote IDs are automatically set.');
|
|
|
|
$this->assertInstanceOf($ceo->CompanyOwned()->OwnerClass, $ceo, 'Remote class is automatically set');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
// Write object with components
|
|
|
|
$ceo->write(false, false, false, true);
|
|
|
|
$this->assertTrue($ceo->CompanyOwned()->isInDB(), 'write() writes belongs_to components to the database.');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
$newCEO = DataObject::get_by_id('DataObjectTest_CEO', $ceo->ID);
|
|
|
|
$this->assertEquals (
|
|
|
|
$ceo->CompanyOwned()->ID,
|
|
|
|
$newCEO->CompanyOwned()->ID,
|
|
|
|
'polymorphic belongs_to can be retrieved from the database.'
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-11-23 14:55:19 +01:00
|
|
|
/**
|
|
|
|
* @expectedException LogicException
|
|
|
|
*/
|
2010-10-19 00:40:56 +02:00
|
|
|
public function testInvalidate() {
|
|
|
|
$do = new DataObjectTest_Fixture();
|
|
|
|
$do->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 00:40:56 +02:00
|
|
|
$do->delete();
|
|
|
|
|
2012-11-23 14:55:19 +01:00
|
|
|
$do->delete(); // Prohibit invalid object manipulation
|
|
|
|
$do->write();
|
|
|
|
$do->duplicate();
|
2010-10-19 00:40:56 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testToMap() {
|
2010-12-06 22:47:14 +01:00
|
|
|
$obj = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam1');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-06 22:47:14 +01:00
|
|
|
$map = $obj->toMap();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-06 22:47:14 +01:00
|
|
|
$this->assertArrayHasKey('ID', $map, 'Contains base fields');
|
|
|
|
$this->assertArrayHasKey('Title', $map, 'Contains fields from parent class');
|
|
|
|
$this->assertArrayHasKey('SubclassDatabaseField', $map, 'Contains fields from concrete class');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals($obj->ID, $map['ID'],
|
|
|
|
'Contains values from base fields');
|
|
|
|
$this->assertEquals($obj->Title, $map['Title'],
|
|
|
|
'Contains values from parent class fields');
|
|
|
|
$this->assertEquals($obj->SubclassDatabaseField, $map['SubclassDatabaseField'],
|
|
|
|
'Contains values from concrete class fields');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-06 22:47:14 +01:00
|
|
|
$newObj = new DataObjectTest_SubTeam();
|
|
|
|
$this->assertArrayHasKey('Title', $map, 'Contains null fields');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testIsEmpty() {
|
2010-12-06 23:12:24 +01:00
|
|
|
$objEmpty = new DataObjectTest_Team();
|
|
|
|
$this->assertTrue($objEmpty->isEmpty(), 'New instance without populated defaults is empty');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
$objEmpty->Title = '0'; //
|
2010-12-06 23:12:24 +01:00
|
|
|
$this->assertFalse($objEmpty->isEmpty(), 'Zero value in attribute considered non-empty');
|
|
|
|
}
|
2012-04-20 00:08:17 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRelField() {
|
2012-03-24 02:03:55 +01:00
|
|
|
$captain = $this->objFromFixture('DataObjectTest_Player', 'captain1');
|
|
|
|
// Test traversal of a single has_one
|
|
|
|
$this->assertEquals("Team 1", $captain->relField('FavouriteTeam.Title'));
|
|
|
|
// Test direct field access
|
|
|
|
$this->assertEquals("Captain", $captain->relField('FirstName'));
|
|
|
|
|
|
|
|
$player = $this->objFromFixture('DataObjectTest_Player', 'player2');
|
|
|
|
// Test that we can traverse more than once, and that arbitrary methods are okay
|
|
|
|
$this->assertEquals("Team 1", $player->relField('Teams.First.Title'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-19 04:40:13 +01:00
|
|
|
$newPlayer = new DataObjectTest_Player();
|
|
|
|
$this->assertNull($newPlayer->relField('Teams.First.Title'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-12 23:41:24 +02:00
|
|
|
// Test that relField works on db field manipulations
|
|
|
|
$comment = $this->objFromFixture('DataObjectTest_TeamComment', 'comment3');
|
|
|
|
$this->assertEquals("PHIL IS A UNIQUE GUY, AND COMMENTS ON TEAM2" , $comment->relField('Comment.UpperCase'));
|
2012-03-24 02:03:55 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRelObject() {
|
2012-03-24 02:03:55 +01:00
|
|
|
$captain = $this->objFromFixture('DataObjectTest_Player', 'captain1');
|
2012-04-18 13:11:53 +02:00
|
|
|
|
2012-03-24 02:03:55 +01:00
|
|
|
// Test traversal of a single has_one
|
|
|
|
$this->assertInstanceOf("Varchar", $captain->relObject('FavouriteTeam.Title'));
|
|
|
|
$this->assertEquals("Team 1", $captain->relObject('FavouriteTeam.Title')->getValue());
|
2012-04-18 13:11:53 +02:00
|
|
|
|
2012-03-24 02:03:55 +01:00
|
|
|
// Test direct field access
|
|
|
|
$this->assertInstanceOf("Boolean", $captain->relObject('IsRetired'));
|
|
|
|
$this->assertEquals(1, $captain->relObject('IsRetired')->getValue());
|
|
|
|
|
|
|
|
$player = $this->objFromFixture('DataObjectTest_Player', 'player2');
|
|
|
|
// Test that we can traverse more than once, and that arbitrary methods are okay
|
|
|
|
$this->assertInstanceOf("Varchar", $player->relObject('Teams.First.Title'));
|
|
|
|
$this->assertEquals("Team 1", $player->relObject('Teams.First.Title')->getValue());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testLateStaticBindingStyle() {
|
2012-04-20 04:58:24 +02:00
|
|
|
// Confirm that DataObjectTest_Player::get() operates as excepted
|
|
|
|
$this->assertEquals(4, DataObjectTest_Player::get()->Count());
|
|
|
|
$this->assertInstanceOf('DataObjectTest_Player', DataObjectTest_Player::get()->First());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-20 04:58:24 +02:00
|
|
|
// You can't pass arguments to LSB syntax - use the DataList methods instead.
|
|
|
|
$this->setExpectedException('InvalidArgumentException');
|
|
|
|
DataObjectTest_Player::get(null, "\"ID\" = 1");
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-20 04:58:24 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testBrokenLateStaticBindingStyle() {
|
2012-04-20 04:58:24 +02:00
|
|
|
// If you call DataObject::get() you have to pass a first argument
|
|
|
|
$this->setExpectedException('InvalidArgumentException');
|
|
|
|
DataObject::get();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-20 04:58:24 +02:00
|
|
|
}
|
2011-03-30 03:19:27 +02:00
|
|
|
|
2008-01-09 04:39:05 +01:00
|
|
|
}
|
|
|
|
|
2016-10-04 20:14:16 +02:00
|
|
|
class DataObjectTest_Sortable extends DataObject implements TestOnly {
|
|
|
|
private static $db = array(
|
|
|
|
'Sort' => 'Int',
|
|
|
|
'Name' => 'Varchar',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2008-08-11 06:51:58 +02:00
|
|
|
class DataObjectTest_Player extends Member implements TestOnly {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2009-11-22 04:47:18 +01:00
|
|
|
'IsRetired' => 'Boolean',
|
|
|
|
'ShirtNumber' => 'Varchar',
|
2011-03-23 04:32:24 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array(
|
2008-10-02 02:45:13 +02:00
|
|
|
'FavouriteTeam' => 'DataObjectTest_Team',
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $belongs_many_many = array(
|
2008-10-02 02:45:13 +02:00
|
|
|
'Teams' => 'DataObjectTest_Team'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
private static $has_many = array(
|
|
|
|
'Fans' => 'DataObjectTest_Fan.Favourite' // Polymorphic - Player fans
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
private static $belongs_to = array (
|
|
|
|
'CompanyOwned' => 'DataObjectTest_Company.Owner'
|
|
|
|
);
|
2014-02-12 16:37:59 +01:00
|
|
|
|
|
|
|
private static $searchable_fields = array(
|
|
|
|
'IsRetired',
|
|
|
|
'ShirtNumber'
|
|
|
|
);
|
2008-08-11 06:51:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class DataObjectTest_Team extends DataObject implements TestOnly {
|
2008-08-15 05:08:03 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2014-08-15 08:53:05 +02:00
|
|
|
'Title' => 'Varchar',
|
2009-10-11 02:06:58 +02:00
|
|
|
'DatabaseField' => 'HTMLVarchar'
|
2008-08-15 05:08:03 +02:00
|
|
|
);
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array(
|
2008-08-15 05:08:03 +02:00
|
|
|
"Captain" => 'DataObjectTest_Player',
|
2008-09-30 02:20:30 +02:00
|
|
|
'HasOneRelationship' => 'DataObjectTest_Player',
|
2008-08-15 05:08:03 +02:00
|
|
|
);
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_many = array(
|
2012-04-20 00:08:17 +02:00
|
|
|
'SubTeams' => 'DataObjectTest_SubTeam',
|
2013-07-10 02:44:24 +02:00
|
|
|
'Comments' => 'DataObjectTest_TeamComment',
|
|
|
|
'Fans' => 'DataObjectTest_Fan.Favourite' // Polymorphic - Team fans
|
2010-12-11 06:43:08 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $many_many = array(
|
2008-08-15 05:08:03 +02:00
|
|
|
'Players' => 'DataObjectTest_Player'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $many_many_extraFields = array(
|
2009-02-10 07:04:36 +01:00
|
|
|
'Players' => array(
|
|
|
|
'Position' => 'Varchar(100)'
|
|
|
|
)
|
|
|
|
);
|
2012-03-15 05:42:42 +01:00
|
|
|
|
2015-01-24 00:13:17 +01:00
|
|
|
private static $belongs_many_many = array(
|
|
|
|
'Sponsors' => 'DataObjectTest_EquipmentCompany.SponsoredTeams',
|
|
|
|
'EquipmentSuppliers' => 'DataObjectTest_EquipmentCompany.EquipmentCustomers'
|
|
|
|
);
|
|
|
|
|
2014-02-12 16:37:59 +01:00
|
|
|
private static $summary_fields = array(
|
2014-08-14 03:50:23 +02:00
|
|
|
'Title' => 'Custom Title',
|
2014-02-12 16:37:59 +01:00
|
|
|
'Title.UpperCase' => 'Title',
|
|
|
|
'Captain.ShirtNumber' => 'Captain\'s shirt number',
|
|
|
|
'Captain.FavouriteTeam.Title' => 'Captain\'s favourite team'
|
|
|
|
);
|
|
|
|
|
2013-04-02 13:33:14 +02:00
|
|
|
private static $default_sort = '"Title"';
|
2012-08-21 06:23:00 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function MyTitle() {
|
2012-03-15 05:42:42 +01:00
|
|
|
return 'Team ' . $this->Title;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getDynamicField() {
|
2008-09-30 02:20:30 +02:00
|
|
|
return 'dynamicfield';
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-06-16 04:56:59 +02:00
|
|
|
class DataObjectTest_Fixture extends DataObject implements TestOnly {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2009-06-16 04:56:59 +02:00
|
|
|
// Funny field names
|
2009-05-07 08:00:50 +02:00
|
|
|
'Data' => 'Varchar',
|
|
|
|
'Duplicate' => 'Varchar',
|
|
|
|
'DbObject' => 'Varchar',
|
2012-11-06 23:28:36 +01:00
|
|
|
|
2009-06-16 04:56:59 +02:00
|
|
|
// Field types
|
2012-11-06 23:28:36 +01:00
|
|
|
'DateField' => 'Date',
|
|
|
|
'DatetimeField' => 'Datetime',
|
|
|
|
|
|
|
|
'MyFieldWithDefault' => 'Varchar',
|
|
|
|
'MyFieldWithAltDefault' => 'Varchar'
|
2009-03-16 14:43:03 +01:00
|
|
|
);
|
2009-06-16 04:56:59 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $defaults = array(
|
2012-11-06 23:28:36 +01:00
|
|
|
'MyFieldWithDefault' => 'Default Value',
|
2009-03-16 14:43:03 +01:00
|
|
|
);
|
2012-11-06 23:28:36 +01:00
|
|
|
|
2014-02-12 16:37:59 +01:00
|
|
|
private static $summary_fields = array(
|
|
|
|
'Data' => 'Data',
|
|
|
|
'DateField.Nice' => 'Date'
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $searchable_fields = array();
|
|
|
|
|
2012-11-06 23:28:36 +01:00
|
|
|
public function populateDefaults() {
|
|
|
|
parent::populateDefaults();
|
|
|
|
|
|
|
|
$this->MyFieldWithAltDefault = 'Default Value';
|
|
|
|
}
|
|
|
|
|
2009-03-16 14:43:03 +01:00
|
|
|
}
|
|
|
|
|
2008-09-30 02:20:30 +02:00
|
|
|
class DataObjectTest_SubTeam extends DataObjectTest_Team implements TestOnly {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2009-05-07 08:00:50 +02:00
|
|
|
'SubclassDatabaseField' => 'Varchar'
|
2008-09-30 02:20:30 +02:00
|
|
|
);
|
2012-04-20 00:08:17 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array(
|
2012-04-20 00:08:17 +02:00
|
|
|
"ParentTeam" => 'DataObjectTest_Team',
|
|
|
|
);
|
2015-02-25 10:49:36 +01:00
|
|
|
|
|
|
|
private static $many_many = array(
|
|
|
|
'FormerPlayers' => 'DataObjectTest_Player'
|
|
|
|
);
|
2016-05-18 01:00:04 +02:00
|
|
|
|
2015-02-25 10:49:36 +01:00
|
|
|
private static $many_many_extraFields = array(
|
|
|
|
'FormerPlayers' => array(
|
|
|
|
'Position' => 'Varchar(100)'
|
|
|
|
)
|
|
|
|
);
|
2008-09-30 02:20:30 +02:00
|
|
|
}
|
2010-04-12 04:03:16 +02:00
|
|
|
class OtherSubclassWithSameField extends DataObjectTest_Team implements TestOnly {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2009-08-19 06:34:28 +02:00
|
|
|
'SubclassDatabaseField' => 'Varchar',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2008-08-15 05:08:03 +02:00
|
|
|
|
2008-11-06 05:51:25 +01:00
|
|
|
class DataObjectTest_FieldlessTable extends DataObject implements TestOnly {
|
|
|
|
}
|
|
|
|
|
|
|
|
class DataObjectTest_FieldlessSubTable extends DataObjectTest_Team implements TestOnly {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
class DataObjectTest_Team_Extension extends DataExtension implements TestOnly {
|
2012-03-09 04:37:50 +01:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2012-03-09 04:37:50 +01:00
|
|
|
'ExtendedDatabaseField' => 'Varchar'
|
|
|
|
);
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array(
|
2012-03-09 04:37:50 +01:00
|
|
|
'ExtendedHasOneRelationship' => 'DataObjectTest_Player'
|
|
|
|
);
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getExtendedDynamicField() {
|
2011-04-15 11:35:30 +02:00
|
|
|
return "extended dynamic field";
|
2008-09-30 02:20:30 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-11 06:51:58 +02:00
|
|
|
}
|
|
|
|
|
2008-10-08 22:42:09 +02:00
|
|
|
class DataObjectTest_ValidatedObject extends DataObject implements TestOnly {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2008-10-08 22:42:09 +02:00
|
|
|
'Name' => 'Varchar(50)'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-06-16 01:39:32 +02:00
|
|
|
protected function validate() {
|
2008-10-08 22:42:09 +02:00
|
|
|
if(!empty($this->Name)) {
|
|
|
|
return new ValidationResult();
|
|
|
|
} else {
|
|
|
|
return new ValidationResult(false, "This object needs a name. Otherwise it will have an identity crisis!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-12 14:15:32 +01:00
|
|
|
class DataObjectTest_Company extends DataObject implements TestOnly {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
private static $db = array(
|
|
|
|
'Name' => 'Varchar'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array (
|
2009-10-24 01:27:51 +02:00
|
|
|
'CEO' => 'DataObjectTest_CEO',
|
2013-07-10 02:44:24 +02:00
|
|
|
'PreviousCEO' => 'DataObjectTest_CEO',
|
|
|
|
'Owner' => 'DataObject' // polymorphic
|
2009-10-24 01:27:51 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_many = array (
|
2009-10-24 01:26:10 +02:00
|
|
|
'CurrentStaff' => 'DataObjectTest_Staff.CurrentCompany',
|
|
|
|
'PreviousStaff' => 'DataObjectTest_Staff.PreviousCompany'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-01-24 00:13:17 +01:00
|
|
|
class DataObjectTest_EquipmentCompany extends DataObjectTest_Company implements TestOnly {
|
|
|
|
private static $many_many = array(
|
|
|
|
'SponsoredTeams' => 'DataObjectTest_Team',
|
|
|
|
'EquipmentCustomers' => 'DataObjectTest_Team'
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $many_many_extraFields = array(
|
|
|
|
'SponsoredTeams' => array(
|
|
|
|
'SponsorFee' => 'Int'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
class DataObjectTest_SubEquipmentCompany extends DataObjectTest_EquipmentCompany implements TestOnly {
|
|
|
|
private static $db = array(
|
|
|
|
'SubclassDatabaseField' => 'Varchar'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-02-12 14:15:32 +01:00
|
|
|
class DataObjectTest_Staff extends DataObject implements TestOnly {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array (
|
2009-10-24 01:26:10 +02:00
|
|
|
'CurrentCompany' => 'DataObjectTest_Company',
|
|
|
|
'PreviousCompany' => 'DataObjectTest_Company'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2009-10-24 01:27:51 +02:00
|
|
|
class DataObjectTest_CEO extends DataObjectTest_Staff {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $belongs_to = array (
|
2009-10-25 00:39:20 +02:00
|
|
|
'Company' => 'DataObjectTest_Company.CEO',
|
2013-07-10 02:44:24 +02:00
|
|
|
'PreviousCompany' => 'DataObjectTest_Company.PreviousCEO',
|
|
|
|
'CompanyOwned' => 'DataObjectTest_Company.Owner'
|
2009-10-24 01:27:51 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-02-12 14:15:32 +01:00
|
|
|
class DataObjectTest_TeamComment extends DataObject implements TestOnly {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2012-05-01 04:43:52 +02:00
|
|
|
'Name' => 'Varchar',
|
|
|
|
'Comment' => 'Text'
|
2010-12-11 06:43:08 +01:00
|
|
|
);
|
2012-05-01 04:43:52 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array(
|
2012-05-01 04:43:52 +02:00
|
|
|
'Team' => 'DataObjectTest_Team'
|
2010-12-11 06:43:08 +01:00
|
|
|
);
|
2012-11-06 23:28:36 +01:00
|
|
|
|
2015-09-07 03:44:16 +02:00
|
|
|
private static $default_sort = '"Name" ASC';
|
2010-12-11 06:43:08 +01:00
|
|
|
}
|
|
|
|
|
2015-02-12 14:15:32 +01:00
|
|
|
class DataObjectTest_Fan extends DataObject implements TestOnly {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
private static $db = array(
|
|
|
|
'Name' => 'Varchar(255)'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
private static $has_one = array(
|
|
|
|
'Favourite' => 'DataObject', // Polymorphic relation
|
|
|
|
'SecondFavourite' => 'DataObject'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-11-10 22:12:25 +01:00
|
|
|
class DataObjectTest_ExtendedTeamComment extends DataObjectTest_TeamComment {
|
|
|
|
private static $db = array(
|
|
|
|
'Comment' => 'HTMLText'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-11-06 15:38:59 +01:00
|
|
|
class DataObjectTest_Play extends DataObject implements TestOnly {}
|
|
|
|
class DataObjectTest_Ploy extends DataObject implements TestOnly {}
|
|
|
|
class DataObjectTest_Bogey extends DataObject implements TestOnly {}
|
|
|
|
|
2012-10-09 05:55:37 +02:00
|
|
|
DataObjectTest_Team::add_extension('DataObjectTest_Team_Extension');
|
2008-09-30 02:20:30 +02:00
|
|
|
|