/**
  * Test that DataList::relation works with PolymorphicHasManyList
  */
 public function testFilterRelation()
 {
     // 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'));
     // Check that fan list exists
     $fans = $list->relation('Fans');
     $this->assertEquals(array('Damian', 'Mitch', 'Richard'), $fans->sort('Name')->column('Name'));
     // Modify list of fans and retest
     $team1 = $this->objFromFixture('DataObjectTest_Team', 'team1');
     $subteam1 = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam1');
     $newFan1 = DataObjectTest_Fan::create();
     $newFan1->Name = 'Bobby';
     $newFan1->write();
     $newFan2 = DataObjectTest_Fan::create();
     $newFan2->Name = 'Mindy';
     $newFan2->write();
     $team1->Fans()->add($newFan1);
     $subteam1->Fans()->add($newFan2);
     $fans = DataObjectTest_Team::get()->relation('Fans');
     $this->assertEquals(array('Bobby', 'Damian', 'Richard'), $team1->Fans()->sort('Name')->column('Name'));
     $this->assertEquals(array('Mindy', 'Mitch'), $subteam1->Fans()->sort('Name')->column('Name'));
     $this->assertEquals(array('Bobby', 'Damian', 'Mindy', 'Mitch', 'Richard'), $fans->sort('Name')->column('Name'));
 }
 /**
  * Test null checks with case modifiers
  */
 public function testFilterByNullCase()
 {
     // Test with case (case/nocase both use same code path)
     // Test with and without null, and with inclusion/exclusion permutations
     $list = DataObjectTest_Fan::get();
     // Only an explicit NOT NULL should include null values
     $items6 = $list->filter('Email:not:case', array(null, '', '*****@*****.**'));
     $this->assertSQLContains(' AND "DataObjectTest_Fan"."Email" IS NOT NULL', $items6->sql());
     // These should all include values where Email IS NULL
     $items7 = $list->filter('Email:nocase', array(null, '', '*****@*****.**'));
     $this->assertSQLContains(' OR "DataObjectTest_Fan"."Email" IS NULL', $items7->sql());
     $items8 = $list->filter('Email:not:case', array('', '*****@*****.**'));
     $this->assertSQLContains(' OR "DataObjectTest_Fan"."Email" IS NULL', $items8->sql());
     // These should not contain any null checks at all
     $items9 = $list->filter('Email:nocase', array('', '*****@*****.**'));
     $this->assertSQLNotContains('"DataObjectTest_Fan"."Email" IS NULL', $items9->sql());
     $this->assertSQLNotContains('"DataObjectTest_Fan"."Email" IS NOT NULL', $items9->sql());
 }
 /**
  * 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);
     // 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);
 }