2011-08-29 09:33:22 +02:00
|
|
|
<?php
|
2015-09-15 04:52:02 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\ORM\Tests;
|
|
|
|
|
2016-11-13 08:35:43 +01:00
|
|
|
use SilverStripe\Assets\Folder;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Assets\Image;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\Versioning\Versioned;
|
|
|
|
use SilverStripe\ORM\Versioning\DataDifferencer;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Assets\File;
|
|
|
|
use SilverStripe\Assets\Filesystem;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Assets\Tests\Storage\AssetStoreTest\TestAssetStore;
|
2011-08-29 09:33:22 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class DataDifferencerTest extends SapphireTest
|
|
|
|
{
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
protected static $fixture_file = 'DataDifferencerTest.yml';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
protected $extraDataObjects = array(
|
|
|
|
DataDifferencerTest\TestObject::class,
|
|
|
|
DataDifferencerTest\HasOneRelationObject::class
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
2015-09-15 04:52:02 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
Versioned::set_stage(Versioned::DRAFT);
|
2016-02-25 05:32:41 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// Set backend root to /DataDifferencerTest
|
|
|
|
TestAssetStore::activate('DataDifferencerTest');
|
2015-09-15 04:52:02 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// Create a test files for each of the fixture references
|
|
|
|
$files = File::get()->exclude('ClassName', Folder::class);
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$fromPath = __DIR__ . '/ImageTest/' . $file->Name;
|
|
|
|
$destPath = TestAssetStore::getLocalPath($file); // Only correct for test asset store
|
|
|
|
Filesystem::makeFolder(dirname($destPath));
|
|
|
|
copy($fromPath, $destPath);
|
|
|
|
}
|
|
|
|
}
|
2015-09-15 04:52:02 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
TestAssetStore::reset();
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
2015-09-15 04:52:02 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testArrayValues()
|
|
|
|
{
|
|
|
|
$obj1 = $this->objFromFixture(DataDifferencerTest\TestObject::class, 'obj1');
|
|
|
|
$beforeVersion = $obj1->Version;
|
|
|
|
// create a new version
|
|
|
|
$obj1->Choices = 'a';
|
|
|
|
$obj1->write();
|
|
|
|
$afterVersion = $obj1->Version;
|
|
|
|
$obj1v1 = Versioned::get_version(DataDifferencerTest\TestObject::class, $obj1->ID, $beforeVersion);
|
|
|
|
$obj1v2 = Versioned::get_version(DataDifferencerTest\TestObject::class, $obj1->ID, $afterVersion);
|
|
|
|
$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.
|
|
|
|
$this->assertContains('<ins>a</ins><del>a,b</del>', str_replace(' ', '', $obj1Diff->getField('Choices')));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testHasOnes()
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var DataDifferencerTest\TestObject $obj1
|
|
|
|
*/
|
|
|
|
$obj1 = $this->objFromFixture(DataDifferencerTest\TestObject::class, 'obj1');
|
|
|
|
$image1 = $this->objFromFixture(Image::class, 'image1');
|
|
|
|
$image2 = $this->objFromFixture(Image::class, 'image2');
|
|
|
|
$relobj2 = $this->objFromFixture(DataDifferencerTest\HasOneRelationObject::class, 'relobj2');
|
2011-08-29 22:23:22 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// create a new version
|
|
|
|
$beforeVersion = $obj1->Version;
|
|
|
|
$obj1->ImageID = $image2->ID;
|
|
|
|
$obj1->HasOneRelationID = $relobj2->ID;
|
|
|
|
$obj1->write();
|
|
|
|
$afterVersion = $obj1->Version;
|
|
|
|
$this->assertNotEquals($beforeVersion, $afterVersion);
|
|
|
|
/**
|
|
|
|
* @var DataDifferencerTest\TestObject $obj1v1
|
|
|
|
*/
|
|
|
|
$obj1v1 = Versioned::get_version(DataDifferencerTest\TestObject::class, $obj1->ID, $beforeVersion);
|
|
|
|
/**
|
|
|
|
* @var DataDifferencerTest\TestObject $obj1v2
|
|
|
|
*/
|
|
|
|
$obj1v2 = Versioned::get_version(DataDifferencerTest\TestObject::class, $obj1->ID, $afterVersion);
|
|
|
|
$differ = new DataDifferencer($obj1v1, $obj1v2);
|
|
|
|
$obj1Diff = $differ->diffedData();
|
2016-02-25 05:32:41 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
|
|
|
* @skipUpgrade
|
|
|
|
*/
|
|
|
|
$this->assertContains($image1->Name, $obj1Diff->getField('Image'));
|
|
|
|
/**
|
|
|
|
* @skipUpgrade
|
|
|
|
*/
|
|
|
|
$this->assertContains($image2->Name, $obj1Diff->getField('Image'));
|
|
|
|
$this->assertContains(
|
|
|
|
'<ins>obj2</ins><del>obj1</del>',
|
|
|
|
str_replace(' ', '', $obj1Diff->getField('HasOneRelationID'))
|
|
|
|
);
|
|
|
|
}
|
2011-08-29 09:33:22 +02:00
|
|
|
}
|