2011-08-29 09:33:22 +02:00
|
|
|
<?php
|
2015-09-15 04:52:02 +02:00
|
|
|
|
|
|
|
use Filesystem as SS_Filesystem;
|
|
|
|
|
2011-08-29 09:33:22 +02:00
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2011-08-29 09:33:22 +02:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
|
|
|
|
class DataDifferencerTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = 'DataDifferencerTest.yml';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-29 22:23:22 +02:00
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'DataDifferencerTest_Object',
|
2015-09-15 04:52:02 +02:00
|
|
|
'DataDifferencerTest_HasOneRelationObject'
|
2011-08-29 22:23:22 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
Versioned::set_stage(Versioned::DRAFT);
|
2016-02-25 05:32:41 +01:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Set backend root to /DataDifferencerTest
|
|
|
|
AssetStoreTest_SpyStore::activate('DataDifferencerTest');
|
|
|
|
|
|
|
|
// Create a test files for each of the fixture references
|
|
|
|
$files = File::get()->exclude('ClassName', 'Folder');
|
|
|
|
foreach($files as $file) {
|
|
|
|
$fromPath = BASE_PATH . '/framework/tests/model/testimages/' . $file->Name;
|
2015-10-19 06:27:27 +02:00
|
|
|
$destPath = AssetStoreTest_SpyStore::getLocalPath($file); // Only correct for test asset store
|
2015-09-15 04:52:02 +02:00
|
|
|
SS_Filesystem::makeFolder(dirname($destPath));
|
|
|
|
copy($fromPath, $destPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
|
|
|
AssetStoreTest_SpyStore::reset();
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testArrayValues() {
|
2011-08-29 09:33:22 +02:00
|
|
|
$obj1 = $this->objFromFixture('DataDifferencerTest_Object', 'obj1');
|
2016-02-25 05:32:41 +01:00
|
|
|
$beforeVersion = $obj1->Version;
|
2011-08-29 09:33:22 +02:00
|
|
|
// create a new version
|
2015-02-10 06:01:19 +01:00
|
|
|
$obj1->Choices = 'a';
|
2011-08-29 09:33:22 +02:00
|
|
|
$obj1->write();
|
2016-02-25 05:32:41 +01:00
|
|
|
$afterVersion = $obj1->Version;
|
|
|
|
$obj1v1 = Versioned::get_version('DataDifferencerTest_Object', $obj1->ID, $beforeVersion);
|
|
|
|
$obj1v2 = Versioned::get_version('DataDifferencerTest_Object', $obj1->ID, $afterVersion);
|
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
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testHasOnes() {
|
2016-02-25 05:32:41 +01:00
|
|
|
/** @var DataDifferencerTest_Object $obj1 */
|
2011-08-29 22:23:22 +02:00
|
|
|
$obj1 = $this->objFromFixture('DataDifferencerTest_Object', 'obj1');
|
2015-09-15 04:52:02 +02:00
|
|
|
$image1 = $this->objFromFixture('Image', 'image1');
|
|
|
|
$image2 = $this->objFromFixture('Image', 'image2');
|
2011-08-29 22:23:22 +02:00
|
|
|
$relobj1 = $this->objFromFixture('DataDifferencerTest_HasOneRelationObject', 'relobj1');
|
|
|
|
$relobj2 = $this->objFromFixture('DataDifferencerTest_HasOneRelationObject', 'relobj2');
|
|
|
|
|
|
|
|
// create a new version
|
2016-02-25 05:32:41 +01:00
|
|
|
$beforeVersion = $obj1->Version;
|
2011-08-29 22:23:22 +02:00
|
|
|
$obj1->ImageID = $image2->ID;
|
|
|
|
$obj1->HasOneRelationID = $relobj2->ID;
|
|
|
|
$obj1->write();
|
2016-02-25 05:32:41 +01:00
|
|
|
$afterVersion = $obj1->Version;
|
|
|
|
$this->assertNotEquals($beforeVersion, $afterVersion);
|
|
|
|
/** @var DataDifferencerTest_Object $obj1v1 */
|
|
|
|
$obj1v1 = Versioned::get_version('DataDifferencerTest_Object', $obj1->ID, $beforeVersion);
|
|
|
|
/** @var DataDifferencerTest_Object $obj1v2 */
|
|
|
|
$obj1v2 = Versioned::get_version('DataDifferencerTest_Object', $obj1->ID, $afterVersion);
|
2011-08-29 22:23:22 +02:00
|
|
|
$differ = new DataDifferencer($obj1v1, $obj1v2);
|
|
|
|
$obj1Diff = $differ->diffedData();
|
2016-02-25 05:32:41 +01:00
|
|
|
|
2015-06-10 01:16:11 +02:00
|
|
|
$this->assertContains($image1->Name, $obj1Diff->getField('Image'));
|
|
|
|
$this->assertContains($image2->Name, $obj1Diff->getField('Image'));
|
2015-09-15 04:52:02 +02:00
|
|
|
$this->assertContains(
|
|
|
|
'<ins>obj2</ins><del>obj1</del>',
|
|
|
|
str_replace(' ', '', $obj1Diff->getField('HasOneRelationID'))
|
|
|
|
);
|
2011-08-29 09:33:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-25 05:32:41 +01:00
|
|
|
/**
|
|
|
|
* @property string $Choices
|
|
|
|
* @method Image Image()
|
|
|
|
* @method DataDifferencerTest_HasOneRelationObject HasOneRelation()
|
|
|
|
*/
|
2011-08-29 09:33:22 +02:00
|
|
|
class DataDifferencerTest_Object extends DataObject implements TestOnly {
|
|
|
|
|
2016-04-01 05:27:59 +02:00
|
|
|
private static $extensions = array(
|
|
|
|
'Versioned'
|
|
|
|
);
|
2011-08-29 09:33:22 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2011-08-29 09:33:22 +02:00
|
|
|
'Choices' => "Varchar",
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array(
|
2015-09-15 04:52:02 +02:00
|
|
|
'Image' => 'Image',
|
2011-08-29 22:23:22 +02:00
|
|
|
'HasOneRelation' => 'DataDifferencerTest_HasOneRelationObject'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getCMSFields() {
|
2012-04-13 15:46:47 +02:00
|
|
|
$fields = parent::getCMSFields();
|
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);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-29 09:33:22 +02:00
|
|
|
return $fields;
|
|
|
|
}
|
2015-07-27 12:38:16 +02:00
|
|
|
|
2011-08-29 22:23:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class DataDifferencerTest_HasOneRelationObject extends DataObject implements TestOnly {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2011-08-29 22:23:22 +02:00
|
|
|
'Title' => 'Varchar'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_many = array(
|
2011-08-29 22:23:22 +02:00
|
|
|
'Objects' => 'DataDifferencerTest_Object'
|
|
|
|
);
|
2016-02-25 05:32:41 +01:00
|
|
|
}
|