/**
  * Test that related objects can be removed from a relation
  */
 public function testRemoveRelation()
 {
     // Check that expected teams exist
     $list = DataObjectTest_Team::get();
     $this->assertEquals(array('Subteam 1', 'Subteam 2', 'Subteam 3', 'Team 1', 'Team 2', 'Team 3'), $list->sort('Title')->column('Title'));
     // Test that each team has the correct fans
     $team1 = $this->objFromFixture('DataObjectTest_Team', 'team1');
     $subteam1 = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam1');
     $this->assertEquals(array('Damian', 'Richard'), $team1->Fans()->sort('Name')->column('Name'));
     $this->assertEquals(array('Mitch'), $subteam1->Fans()->sort('Name')->column('Name'));
     // Test that removing items from unrelated team has no effect
     $team1fan = $this->objFromFixture('DataObjectTest_Fan', 'fan1');
     $subteam1fan = $this->objFromFixture('DataObjectTest_Fan', 'fan4');
     $team1->Fans()->remove($subteam1fan);
     $subteam1->Fans()->remove($team1fan);
     $this->assertEquals(array('Damian', 'Richard'), $team1->Fans()->sort('Name')->column('Name'));
     $this->assertEquals(array('Mitch'), $subteam1->Fans()->sort('Name')->column('Name'));
     $this->assertEquals($team1->ID, $team1fan->FavouriteID);
     $this->assertEquals('DataObjectTest_Team', $team1fan->FavouriteClass);
     $this->assertEquals($subteam1->ID, $subteam1fan->FavouriteID);
     $this->assertEquals('DataObjectTest_SubTeam', $subteam1fan->FavouriteClass);
     // Test that removing items from the related team resets the has_one relations on the fan
     $team1fan = $this->objFromFixture('DataObjectTest_Fan', 'fan1');
     $subteam1fan = $this->objFromFixture('DataObjectTest_Fan', 'fan4');
     $team1->Fans()->remove($team1fan);
     $subteam1->Fans()->remove($subteam1fan);
     $this->assertEquals(array('Richard'), $team1->Fans()->sort('Name')->column('Name'));
     $this->assertEquals(array(), $subteam1->Fans()->sort('Name')->column('Name'));
     $this->assertEmpty($team1fan->FavouriteID);
     $this->assertEmpty($team1fan->FavouriteClass);
     $this->assertEmpty($subteam1fan->FavouriteID);
     $this->assertEmpty($subteam1fan->FavouriteClass);
 }
Ejemplo n.º 2
0
 function testMethodAsValueField()
 {
     $list = DataObjectTest_Team::get();
     $list->sort('Title');
     $map = new SS_Map($list, 'ID', 'MyTitle');
     $this->assertEquals(array('Team Subteam 1', 'Team Subteam 2', 'Team Subteam 3', 'Team Team 1', 'Team Team 2', 'Team Team 3'), $map->values());
 }
 /**
  * Test that related objects can be removed from a relation
  */
 public function testRemoveRelation()
 {
     // Check that expected teams exist
     $list = DataObjectTest_Team::get();
     $this->assertEquals(array('Subteam 1', 'Subteam 2', 'Subteam 3', 'Team 1', 'Team 2', 'Team 3'), $list->sort('Title')->column('Title'));
     // Test that each team has the correct fans
     $team1 = $this->objFromFixture('DataObjectTest_Team', 'team1');
     $team2 = $this->objFromFixture('DataObjectTest_Team', 'team2');
     $this->assertEquals(array('Bob', 'Joe'), $team1->Comments()->sort('Name')->column('Name'));
     $this->assertEquals(array('Phil'), $team2->Comments()->sort('Name')->column('Name'));
     // Test that removing comments from unrelated team has no effect
     $team1comment = $this->objFromFixture('DataObjectTest_TeamComment', 'comment1');
     $team2comment = $this->objFromFixture('DataObjectTest_TeamComment', 'comment3');
     $team1->Comments()->remove($team2comment);
     $team2->Comments()->remove($team1comment);
     $this->assertEquals(array('Bob', 'Joe'), $team1->Comments()->sort('Name')->column('Name'));
     $this->assertEquals(array('Phil'), $team2->Comments()->sort('Name')->column('Name'));
     $this->assertEquals($team1->ID, $team1comment->TeamID);
     $this->assertEquals($team2->ID, $team2comment->TeamID);
     // Test that removing items from the related team resets the has_one relations on the fan
     $team1comment = $this->objFromFixture('DataObjectTest_TeamComment', 'comment1');
     $team2comment = $this->objFromFixture('DataObjectTest_TeamComment', 'comment3');
     $team1->Comments()->remove($team1comment);
     $team2->Comments()->remove($team2comment);
     $this->assertEquals(array('Bob'), $team1->Comments()->sort('Name')->column('Name'));
     $this->assertEquals(array(), $team2->Comments()->sort('Name')->column('Name'));
     $this->assertEmpty($team1comment->TeamID);
     $this->assertEmpty($team2comment->TeamID);
 }
Ejemplo n.º 4
0
 public function testAddingWithMultipleForeignKeys()
 {
     $newPlayer = new DataObjectTest_Player();
     $newPlayer->write();
     $team1 = $this->objFromFixture('DataObjectTest_Team', 'team1');
     $team2 = $this->objFromFixture('DataObjectTest_Team', 'team2');
     $playersTeam1Team2 = DataObjectTest_Team::get()->relation('Players')->setForeignID(array($team1->ID, $team2->ID));
     $playersTeam1Team2->add($newPlayer);
     $this->assertEquals(array($team1->ID, $team2->ID), $newPlayer->Teams()->sort('Title')->column('ID'));
 }
 public function testHasValue()
 {
     $team = new DataObjectTest_Team();
     $this->assertFalse($team->hasValue('Title', null, false));
     $this->assertFalse($team->hasValue('DatabaseField', null, false));
     $team->Title = 'hasValue';
     $this->assertTrue($team->hasValue('Title', null, false));
     $this->assertFalse($team->hasValue('DatabaseField', null, false));
     $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.');
     $team->Title = '<p></p>';
     $this->assertTrue($team->hasValue('Title', null, false), 'Test that an empty paragraph is a value for non-HTML fields.');
     $team->DatabaseField = 'hasValue';
     $this->assertTrue($team->hasValue('Title', null, false));
     $this->assertTrue($team->hasValue('DatabaseField', null, false));
 }
Ejemplo n.º 6
0
 public function testSortByComplexExpression()
 {
     // Test an expression with both spaces and commas. This test also tests that column() can be called
     // with a complex sort expression, so keep using column() below
     $list = DataObjectTest_Team::get()->sort('CASE WHEN "DataObjectTest_Team"."ClassName" = \'DataObjectTest_SubTeam\' THEN 0 ELSE 1 END, "Title" DESC');
     $this->assertEquals(array('Subteam 3', 'Subteam 2', 'Subteam 1', 'Team 3', 'Team 2', 'Team 1'), $list->column("Title"));
 }
Ejemplo n.º 7
0
 public function testIsEmpty()
 {
     $objEmpty = new DataObjectTest_Team();
     $this->assertTrue($objEmpty->isEmpty(), 'New instance without populated defaults is empty');
     $objEmpty->Title = '0';
     //
     $this->assertFalse($objEmpty->isEmpty(), 'Zero value in attribute considered non-empty');
 }
Ejemplo n.º 8
0
	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());
	}
Ejemplo n.º 9
0
 public function testRemoveAll()
 {
     $first = new DataObjectTest_Team();
     $first->write();
     $second = new DataObjectTest_Team();
     $second->write();
     $firstPlayers = $first->Players();
     $secondPlayers = $second->Players();
     $a = new DataObjectTest_Player();
     $a->ShirtNumber = 'a';
     $a->write();
     $b = new DataObjectTest_Player();
     $b->ShirtNumber = 'b';
     $b->write();
     $firstPlayers->add($a);
     $firstPlayers->add($b);
     $secondPlayers->add($a);
     $secondPlayers->add($b);
     $this->assertEquals(array('a', 'b'), $firstPlayers->sort('ShirtNumber')->column('ShirtNumber'));
     $this->assertEquals(array('a', 'b'), $secondPlayers->sort('ShirtNumber')->column('ShirtNumber'));
     $firstPlayers->removeAll();
     $this->assertEquals(0, count($firstPlayers));
     $this->assertEquals(2, count($secondPlayers));
     $firstPlayers->removeAll();
     $firstPlayers->add($a);
     $firstPlayers->add($b);
     $this->assertEquals(array('a', 'b'), $firstPlayers->sort('ShirtNumber')->column('ShirtNumber'));
     $firstPlayers->filter('ShirtNumber', 'b')->removeAll();
     $this->assertEquals(array('a'), $firstPlayers->column('ShirtNumber'));
     $this->assertEquals(array('a', 'b'), $secondPlayers->sort('ShirtNumber')->column('ShirtNumber'));
     $this->assertNotNull(DataObjectTest_Player::get()->byID($a->ID));
     $this->assertNotNull(DataObjectTest_Player::get()->byID($b->ID));
 }
Ejemplo n.º 10
0
	public function testRelationshipEmptyOnNewRecords() {
		// Relies on the fact that (unrelated) comments exist in the fixture file already
		$newTeam = new DataObjectTest_Team(); // has_many Comments
		$this->assertEquals(array(), $newTeam->Comments()->column('ID'));
	}