2011-08-29 09:33:22 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
|
|
|
|
class DataDifferencerTest extends SapphireTest {
|
|
|
|
|
|
|
|
static $fixture_file = 'sapphire/tests/model/DataDifferencerTest.yml';
|
|
|
|
|
2011-08-29 22:23:22 +02:00
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'DataDifferencerTest_Object',
|
|
|
|
'DataDifferencerTest_HasOneRelationObject',
|
|
|
|
'DataDifferencerTest_MockImage',
|
|
|
|
);
|
2011-08-29 09:33:22 +02:00
|
|
|
|
|
|
|
function testArrayValues() {
|
|
|
|
$obj1 = $this->objFromFixture('DataDifferencerTest_Object', 'obj1');
|
|
|
|
// create a new version
|
|
|
|
$obj1->Choices = array('a');
|
|
|
|
$obj1->write();
|
2011-08-29 22:23:22 +02:00
|
|
|
$obj1v1 = Versioned::get_version('DataDifferencerTest_Object', $obj1->ID, $obj1->Version-1);
|
|
|
|
$obj1v2 = Versioned::get_version('DataDifferencerTest_Object', $obj1->ID, $obj1->Version);
|
2011-08-29 09:33:22 +02:00
|
|
|
$differ = new DataDifferencer($obj1v1, $obj1v2);
|
|
|
|
$obj1Diff = $differ->diffedData();
|
|
|
|
// TODO Using getter would split up field again, bug only caused by simulating
|
|
|
|
// an array-based value in the first place.
|
2012-02-10 23:26:46 +01:00
|
|
|
$this->assertContains('<ins>a</ins><del>a,b</del>', str_replace(' ','',$obj1Diff->getField('Choices')));
|
2011-08-29 22:23:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function testHasOnes() {
|
|
|
|
$obj1 = $this->objFromFixture('DataDifferencerTest_Object', 'obj1');
|
|
|
|
$image1 = $this->objFromFixture('DataDifferencerTest_MockImage', 'image1');
|
|
|
|
$image2 = $this->objFromFixture('DataDifferencerTest_MockImage', 'image2');
|
|
|
|
$relobj1 = $this->objFromFixture('DataDifferencerTest_HasOneRelationObject', 'relobj1');
|
|
|
|
$relobj2 = $this->objFromFixture('DataDifferencerTest_HasOneRelationObject', 'relobj2');
|
|
|
|
|
|
|
|
// create a new version
|
|
|
|
$obj1->ImageID = $image2->ID;
|
|
|
|
$obj1->HasOneRelationID = $relobj2->ID;
|
|
|
|
$obj1->write();
|
|
|
|
$obj1v1 = Versioned::get_version('DataDifferencerTest_Object', $obj1->ID, $obj1->Version-1);
|
|
|
|
$obj1v2 = Versioned::get_version('DataDifferencerTest_Object', $obj1->ID, $obj1->Version);
|
|
|
|
$differ = new DataDifferencer($obj1v1, $obj1v2);
|
|
|
|
$obj1Diff = $differ->diffedData();
|
|
|
|
$this->assertContains($image1->Filename, $obj1Diff->getField('Image'));
|
|
|
|
$this->assertContains($image2->Filename, $obj1Diff->getField('Image'));
|
2012-02-10 23:26:46 +01:00
|
|
|
$this->assertContains('<ins>obj2</ins><del>obj1</del>', str_replace(' ','',$obj1Diff->getField('HasOneRelationID')));
|
2011-08-29 09:33:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class DataDifferencerTest_Object extends DataObject implements TestOnly {
|
|
|
|
|
|
|
|
static $extensions = array('Versioned("Stage", "Live")');
|
|
|
|
|
|
|
|
static $db = array(
|
|
|
|
'Choices' => "Varchar",
|
|
|
|
);
|
|
|
|
|
2011-08-29 22:23:22 +02:00
|
|
|
static $has_one = array(
|
|
|
|
'Image' => 'DataDifferencerTest_MockImage',
|
|
|
|
'HasOneRelation' => 'DataDifferencerTest_HasOneRelationObject'
|
|
|
|
);
|
|
|
|
|
2012-03-27 06:04:11 +02:00
|
|
|
function getCMSFields($params = null) {
|
|
|
|
$fields = parent::getCMSFields($params);
|
2011-08-29 09:33:22 +02:00
|
|
|
$choices = array(
|
|
|
|
'a' => 'a',
|
|
|
|
'b' => 'b',
|
|
|
|
'c' => 'c',
|
|
|
|
);
|
|
|
|
$listField = new ListboxField('Choices', 'Choices', $choices);
|
|
|
|
$fields->push($listField);
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getChoices() {
|
|
|
|
return explode(',', $this->getField('Choices'));
|
|
|
|
}
|
|
|
|
|
|
|
|
function setChoices($val) {
|
|
|
|
$this->setField('Choices', (is_array($val)) ? implode(',', $val) : $val);
|
|
|
|
}
|
|
|
|
|
2011-08-29 22:23:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class DataDifferencerTest_HasOneRelationObject extends DataObject implements TestOnly {
|
|
|
|
|
|
|
|
static $db = array(
|
|
|
|
'Title' => 'Varchar'
|
|
|
|
);
|
|
|
|
|
|
|
|
static $has_many = array(
|
|
|
|
'Objects' => 'DataDifferencerTest_Object'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
class DataDifferencerTest_MockImage extends Image implements TestOnly {
|
|
|
|
function generateFormattedImage($format, $arg1 = null, $arg2 = null) {
|
|
|
|
$cacheFile = $this->cacheFilename($format, $arg1, $arg2);
|
|
|
|
$gd = new GD(Director::baseFolder()."/" . $this->Filename);
|
|
|
|
// Skip aktual generation
|
|
|
|
return $gd;
|
|
|
|
}
|
2012-03-27 06:04:11 +02:00
|
|
|
}
|