2008-01-09 04:39:05 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2008-06-15 15:33:53 +02:00
|
|
|
* @package sapphire
|
|
|
|
* @subpackage tests
|
2008-01-09 04:39:05 +01:00
|
|
|
*/
|
|
|
|
class DataObjectTest extends SapphireTest {
|
|
|
|
static $fixture_file = 'sapphire/tests/DataObjectTest.yml';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test deletion of DataObjects
|
|
|
|
* - Deleting using delete() on the DataObject
|
|
|
|
* - Deleting using DataObject::delete_by_id()
|
|
|
|
*/
|
|
|
|
function testDelete() {
|
|
|
|
// Test deleting using delete() on the DataObject
|
|
|
|
// Get the first page
|
2008-08-11 01:17:51 +02:00
|
|
|
$page = $this->fixture->objFromFixture('Page', 'page1');
|
2008-01-09 04:39:05 +01:00
|
|
|
// Check the page exists before deleting
|
|
|
|
$this->assertTrue(is_object($page) && $page->exists());
|
|
|
|
// Delete the page
|
|
|
|
$page->delete();
|
|
|
|
// Check that page does not exist after deleting
|
2008-08-11 01:17:51 +02:00
|
|
|
$page = $this->fixture->objFromFixture('Page', 'page1');
|
2008-01-09 04:39:05 +01:00
|
|
|
$this->assertTrue(!$page || !$page->exists());
|
|
|
|
|
|
|
|
|
|
|
|
// Test deleting using DataObject::delete_by_id()
|
|
|
|
// Get the second page
|
2008-08-11 01:17:51 +02:00
|
|
|
$page2 = $this->fixture->objFromFixture('Page', 'page2');
|
2008-01-09 04:39:05 +01:00
|
|
|
// Check the page exists before deleting
|
|
|
|
$this->assertTrue(is_object($page2) && $page2->exists());
|
|
|
|
// Delete the page
|
|
|
|
DataObject::delete_by_id('Page', $page2->ID);
|
|
|
|
// Check that page does not exist after deleting
|
2008-08-11 01:17:51 +02:00
|
|
|
$page2 = $this->fixture->objFromFixture('Page', 'page2');
|
2008-01-09 04:39:05 +01:00
|
|
|
$this->assertTrue(!$page2 || !$page2->exists());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test methods that get DataObjects
|
|
|
|
* - DataObject::get()
|
|
|
|
* - All records of a DataObject
|
|
|
|
* - Filtering
|
|
|
|
* - Sorting
|
|
|
|
* - Joins
|
|
|
|
* - Limit
|
|
|
|
* - Container class
|
|
|
|
* - DataObject::get_by_id()
|
|
|
|
* - DataObject::get_by_url()
|
|
|
|
* - DataObject::get_one()
|
|
|
|
* - With and without caching
|
|
|
|
* - With and without ordering
|
|
|
|
*/
|
|
|
|
function testGet() {
|
|
|
|
// Test getting all records of a DataObject
|
|
|
|
$comments = DataObject::get('PageComment');
|
2008-08-13 02:44:52 +02:00
|
|
|
$this->assertEquals(8, $comments->Count());
|
2008-01-09 04:39:05 +01:00
|
|
|
|
|
|
|
// Test WHERE clause
|
|
|
|
$comments = DataObject::get('PageComment', 'Name="Bob"');
|
2008-08-13 02:44:52 +02:00
|
|
|
$this->assertEquals(2, $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
|
|
|
}
|
|
|
|
|
|
|
|
// Test sorting
|
|
|
|
$comments = DataObject::get('PageComment', '', 'Name ASC');
|
2008-08-13 02:44:52 +02:00
|
|
|
$this->assertEquals(8, $comments->Count());
|
|
|
|
$this->assertEquals('Bob', $comments->First()->Name);
|
2008-01-09 04:39:05 +01:00
|
|
|
$comments = DataObject::get('PageComment', '', 'Name DESC');
|
2008-08-13 02:44:52 +02:00
|
|
|
$this->assertEquals(8, $comments->Count());
|
|
|
|
$this->assertEquals('Joe', $comments->First()->Name);
|
2008-01-09 04:39:05 +01:00
|
|
|
|
|
|
|
// Test join
|
|
|
|
$comments = DataObject::get('PageComment', '`SiteTree`.Title="First Page"', '', 'INNER JOIN SiteTree ON PageComment.ParentID = SiteTree.ID');
|
2008-08-13 02:44:52 +02:00
|
|
|
$this->assertEquals(2, $comments->Count());
|
|
|
|
$this->assertEquals('Bob', $comments->First()->Name);
|
|
|
|
$this->assertEquals('Bob', $comments->Last()->Name);
|
2008-01-09 04:39:05 +01:00
|
|
|
|
|
|
|
// Test limit
|
|
|
|
$comments = DataObject::get('PageComment', '', 'Name ASC', '', '1,2');
|
2008-08-13 02:44:52 +02:00
|
|
|
$this->assertEquals(2, $comments->Count());
|
|
|
|
$this->assertEquals('Bob', $comments->First()->Name);
|
|
|
|
$this->assertEquals('Dean', $comments->Last()->Name);
|
2008-01-09 04:39:05 +01:00
|
|
|
|
|
|
|
// Test container class
|
|
|
|
$comments = DataObject::get('PageComment', '', '', '', '', 'DataObjectSet');
|
2008-08-13 02:44:52 +02:00
|
|
|
$this->assertEquals('DataObjectSet', get_class($comments));
|
2008-01-09 04:39:05 +01:00
|
|
|
$comments = DataObject::get('PageComment', '', '', '', '', 'ComponentSet');
|
2008-08-13 02:44:52 +02:00
|
|
|
$this->assertEquals('ComponentSet', get_class($comments));
|
2008-01-09 04:39:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
// Test get_by_id()
|
2008-08-13 02:44:52 +02:00
|
|
|
$homepageID = $this->idFromFixture('Page', 'home');
|
|
|
|
$page = DataObject::get_by_id('Page', $homepageID);
|
|
|
|
$this->assertEquals('Home', $page->Title);
|
2008-01-09 04:39:05 +01:00
|
|
|
|
|
|
|
// Test get_by_url()
|
|
|
|
$page = DataObject::get_by_url('home');
|
2008-08-13 02:44:52 +02:00
|
|
|
$this->assertEquals($homepageID, $page->ID);
|
2008-01-09 04:39:05 +01:00
|
|
|
|
|
|
|
// Test get_one() without caching
|
|
|
|
$comment1 = DataObject::get_one('PageComment', 'Name="Joe"', false);
|
|
|
|
$comment1->Comment = "Something Else";
|
|
|
|
$comment2 = DataObject::get_one('PageComment', 'Name="Joe"', false);
|
2008-08-13 02:44:52 +02:00
|
|
|
$this->assertNotEquals($comment1->Comment, $comment2->Comment);
|
2008-01-09 04:39:05 +01:00
|
|
|
|
|
|
|
// Test get_one() with caching
|
|
|
|
$comment1 = DataObject::get_one('PageComment', 'Name="Jane"', true);
|
|
|
|
$comment1->Comment = "Something Else";
|
|
|
|
$comment2 = DataObject::get_one('PageComment', 'Name="Jane"', true);
|
2008-08-13 02:44:52 +02:00
|
|
|
$this->assertEquals((string)$comment1->Comment, (string)$comment2->Comment);
|
2008-01-09 04:39:05 +01:00
|
|
|
|
|
|
|
// Test get_one() with order by without caching
|
|
|
|
$comment = DataObject::get_one('PageComment', '', false, 'Name ASC');
|
2008-08-13 02:44:52 +02:00
|
|
|
$this->assertEquals('Bob', $comment->Name);
|
2008-01-09 04:39:05 +01:00
|
|
|
$comment = DataObject::get_one('PageComment', '', false, 'Name DESC');
|
2008-08-13 02:44:52 +02:00
|
|
|
$this->assertEquals('Joe', $comment->Name);
|
2008-01-09 04:39:05 +01:00
|
|
|
|
|
|
|
// Test get_one() with order by with caching
|
|
|
|
$comment = DataObject::get_one('PageComment', '', true, 'Name ASC');
|
2008-08-13 02:44:52 +02:00
|
|
|
$this->assertEquals('Bob', $comment->Name);
|
2008-01-09 04:39:05 +01:00
|
|
|
$comment = DataObject::get_one('PageComment', '', true, 'Name DESC');
|
2008-08-13 02:44:52 +02:00
|
|
|
$this->assertEquals('Joe', $comment->Name);
|
2008-01-09 04:39:05 +01:00
|
|
|
}
|
2008-08-09 05:54:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test writing of database columns which don't correlate to a DBField,
|
|
|
|
* e.g. all relation fields on has_one/has_many like "ParentID".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function testWritePropertyWithoutDBField() {
|
2008-08-11 01:17:51 +02:00
|
|
|
$page = $this->fixture->objFromFixture('Page', 'page1');
|
2008-08-09 05:54:55 +02:00
|
|
|
$page->ParentID = 99;
|
|
|
|
$page->write();
|
|
|
|
// reload the page from the database
|
|
|
|
$savedPage = DataObject::get_by_id('Page', $page->ID);
|
|
|
|
$this->assertTrue($savedPage->ParentID == 99);
|
|
|
|
}
|
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
|
|
|
|
*/
|
|
|
|
function testHasManyRelationships() {
|
2008-08-11 01:17:51 +02:00
|
|
|
$page = $this->fixture->objFromFixture('Page', 'home');
|
2008-01-09 04:39:05 +01:00
|
|
|
|
|
|
|
// Test getComponents() gets the ComponentSet of the other side of the relation
|
|
|
|
$this->assertTrue($page->getComponents('Comments')->Count() == 2);
|
|
|
|
|
|
|
|
// Test the IDs on the DataObjects are set correctly
|
|
|
|
foreach($page->getComponents('Comments') as $comment) {
|
|
|
|
$this->assertTrue($comment->ParentID == $page->ID);
|
|
|
|
}
|
|
|
|
}
|
2008-08-11 05:03:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo Test removeMany() and addMany() on $many_many relationships
|
|
|
|
*/
|
|
|
|
function testManyManyRelationships() {
|
|
|
|
$player1 = $this->fixture->objFromFixture('DataObjectTest_Player', 'player1');
|
|
|
|
$player2 = $this->fixture->objFromFixture('DataObjectTest_Player', 'player2');
|
|
|
|
$team1 = $this->fixture->objFromFixture('DataObjectTest_Team', 'team1');
|
|
|
|
$team2 = $this->fixture->objFromFixture('DataObjectTest_Team', 'team2');
|
|
|
|
|
|
|
|
// Test adding single DataObject by reference
|
|
|
|
$player1->Teams()->add($team1);
|
|
|
|
$player1->flushCache();
|
|
|
|
$compareTeams = new ComponentSet($team1);
|
|
|
|
$this->assertEquals(
|
|
|
|
$player1->Teams()->column('ID'),
|
|
|
|
$compareTeams->column('ID'),
|
|
|
|
"Adding single record as DataObject to many_many"
|
|
|
|
);
|
|
|
|
|
|
|
|
// test removing single DataObject by reference
|
|
|
|
$player1->Teams()->remove($team1);
|
|
|
|
$player1->flushCache();
|
|
|
|
$compareTeams = new ComponentSet();
|
|
|
|
$this->assertEquals(
|
|
|
|
$player1->Teams()->column('ID'),
|
|
|
|
$compareTeams->column('ID'),
|
|
|
|
"Removing single record as DataObject from many_many"
|
|
|
|
);
|
|
|
|
|
|
|
|
// test adding single DataObject by ID
|
|
|
|
$player1->Teams()->add($team1->ID);
|
|
|
|
$player1->flushCache();
|
|
|
|
$compareTeams = new ComponentSet($team1);
|
|
|
|
$this->assertEquals(
|
|
|
|
$player1->Teams()->column('ID'),
|
|
|
|
$compareTeams->column('ID'),
|
|
|
|
"Adding single record as ID to many_many"
|
|
|
|
);
|
|
|
|
|
|
|
|
// test removing single DataObject by ID
|
|
|
|
$player1->Teams()->remove($team1->ID);
|
|
|
|
$player1->flushCache();
|
|
|
|
$compareTeams = new ComponentSet();
|
|
|
|
$this->assertEquals(
|
|
|
|
$player1->Teams()->column('ID'),
|
|
|
|
$compareTeams->column('ID'),
|
|
|
|
"Removing single record as ID from many_many"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo Extend type change tests (e.g. '0'==NULL)
|
|
|
|
*/
|
|
|
|
function testChangedFields() {
|
|
|
|
$page = $this->fixture->objFromFixture('Page', 'home');
|
|
|
|
$page->Title = 'Home-Changed';
|
|
|
|
$page->ShowInMenus = true;
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$page->getChangedFields(false, 1),
|
|
|
|
array(
|
|
|
|
'Title' => array(
|
|
|
|
'before' => 'Home',
|
|
|
|
'after' => 'Home-Changed',
|
|
|
|
'level' => 2
|
|
|
|
),
|
|
|
|
'ShowInMenus' => array(
|
|
|
|
'before' => 1,
|
|
|
|
'after' => true,
|
|
|
|
'level' => 1
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'Changed fields are correctly detected with strict type changes (level=1)'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$page->getChangedFields(false, 2),
|
|
|
|
array(
|
|
|
|
'Title' => array(
|
|
|
|
'before'=>'Home',
|
|
|
|
'after'=>'Home-Changed',
|
|
|
|
'level' => 2
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'Changed fields are correctly detected while ignoring type changes (level=2)'
|
|
|
|
);
|
|
|
|
}
|
2008-08-13 02:44:52 +02:00
|
|
|
|
|
|
|
function testRandomSort() {
|
|
|
|
/* If we perforn the same regularly sorted query twice, it should return the same results */
|
|
|
|
$itemsA = DataObject::get("PageComment", "", "ID");
|
|
|
|
foreach($itemsA as $item) $keysA[] = $item->ID;
|
|
|
|
|
|
|
|
$itemsB = DataObject::get("PageComment", "", "ID");
|
|
|
|
foreach($itemsB as $item) $keysB[] = $item->ID;
|
|
|
|
|
|
|
|
$this->assertEquals($keysA, $keysB);
|
|
|
|
|
|
|
|
/* If we perform the same random query twice, it shouldn't return the same results */
|
|
|
|
$itemsA = DataObject::get("PageComment", "", "RAND()");
|
|
|
|
foreach($itemsA as $item) $keysA[] = $item->ID;
|
|
|
|
|
|
|
|
$itemsB = DataObject::get("PageComment", "", "RAND()");
|
|
|
|
foreach($itemsB as $item) $keysB[] = $item->ID;
|
|
|
|
|
|
|
|
$this->assertNotEquals($keysA, $keysB);
|
|
|
|
}
|
2008-08-15 05:08:03 +02:00
|
|
|
|
|
|
|
function testWriteSavesToHasOneRelations() {
|
|
|
|
/* 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();
|
|
|
|
$this->assertEquals($captainID, DB::query("SELECT CaptainID FROM DataObjectTest_Team WHERE ID = $team->ID")->value());
|
|
|
|
|
|
|
|
/* After giving it a value, you should also be able to set it back to null */
|
|
|
|
$team->CaptainID = '';
|
|
|
|
$team->write();
|
|
|
|
$this->assertEquals(0, DB::query("SELECT CaptainID FROM DataObjectTest_Team WHERE ID = $team->ID")->value());
|
|
|
|
|
|
|
|
/* You should also be able to save a blank to it when it's first created */
|
|
|
|
$team = new DataObjectTest_Team();
|
|
|
|
$team->CaptainID = '';
|
|
|
|
$team->write();
|
|
|
|
$this->assertEquals(0, DB::query("SELECT CaptainID FROM DataObjectTest_Team WHERE ID = $team->ID")->value());
|
|
|
|
|
|
|
|
/* Ditto for existing records without a value */
|
|
|
|
$existingTeam = $this->objFromFixture('DataObjectTest_Team', 'team1');
|
|
|
|
$existingTeam->CaptainID = '';
|
|
|
|
$existingTeam->write();
|
|
|
|
$this->assertEquals(0, DB::query("SELECT CaptainID FROM DataObjectTest_Team WHERE ID = $existingTeam->ID")->value());
|
|
|
|
}
|
2008-01-09 04:39:05 +01:00
|
|
|
}
|
|
|
|
|
2008-08-11 06:51:58 +02:00
|
|
|
class DataObjectTest_Player extends Member implements TestOnly {
|
|
|
|
|
|
|
|
static $belongs_many_many = array(
|
|
|
|
'Teams' => 'DataObjectTest_Team'
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class DataObjectTest_Team extends DataObject implements TestOnly {
|
2008-08-15 05:08:03 +02:00
|
|
|
|
|
|
|
static $db = array(
|
|
|
|
'Title' => 'Text',
|
|
|
|
);
|
|
|
|
|
|
|
|
static $has_one = array(
|
|
|
|
"Captain" => 'DataObjectTest_Player',
|
|
|
|
);
|
|
|
|
|
|
|
|
static $many_many = array(
|
|
|
|
'Players' => 'DataObjectTest_Player'
|
|
|
|
);
|
|
|
|
|
2008-08-11 06:51:58 +02:00
|
|
|
}
|
|
|
|
|
2008-01-09 04:39:05 +01:00
|
|
|
?>
|