Source of file PopulateFactoryTest.php
Size: 4,219 Bytes - Last Modified: 2021-12-24T06:45:02+00:00
/var/www/docs.ssmods.com/process/src/tests/php/PopulateFactoryTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 | <?php namespace DNADesign\Populate\Tests; use DNADesign\Populate\PopulateFactory; use DNADesign\Populate\Tests\PopulateFactoryTest\PopulateFactoryTestObject; use DNADesign\Populate\Tests\PopulateFactoryTest\PopulateFactoryTestVersionedObject; use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\TestOnly; use SilverStripe\Versioned\Versioned; /** * @package populate */ class PopulateFactoryTest extends SapphireTest implements TestOnly { /** * @var PopulateFactory */ private $factory; protected static $fixture_file = "PopulateFactoryTest.yml"; protected $usesDatabase = true; protected static $extra_dataobjects = [ PopulateFactoryTestObject::class, PopulateFactoryTestVersionedObject::class, ]; public function setUp() { parent::setUp(); $this->factory = new PopulateFactory(); } /** * Test version support. If an object has versioned then both the live and * staging tables should be updated. Other live records should be removed * as well. */ public function testVersionedObjects() { $versioned = $this->objFromFixture(PopulateFactoryTestVersionedObject::class, 'objV1'); $versioned->publish('Stage', 'Live'); $obj = $this->factory->createObject(PopulateFactoryTestVersionedObject::class, 'test', [ 'Content' => 'Updated Version Foo', 'PopulateMergeWhen' => "Title = 'Version Foo'", ]); $this->assertEquals($versioned->ID, $obj->ID); $this->assertEquals('Updated Version Foo', $obj->Content); $check = Versioned::get_one_by_stage( PopulateFactoryTestVersionedObject::class, 'Live', "Title = 'Version Foo'" ); $this->assertEquals('Updated Version Foo', $check->Content); } /** * As a utility you can include code to be evaluated the the yaml using * Field: `something::foo()`; */ public function testCreateObjectPhpEval() { $obj = $this->factory->createObject(PopulateFactoryTestObject::class, 'test', [ 'Title' => '`sprintf("hi")`;', ]); $this->assertEquals('hi', $obj->Title); } /** * When a populatemergewhen clause is supplied, make sure it merges. If no * record found, one should be created * */ public function testCreateObjectPopulateMergeWhen() { $obj = $this->factory->createObject(PopulateFactoryTestObject::class, 'test', [ 'Title' => 'Updated', 'PopulateMergeWhen' => "Title = 'Foo'", ]); $this->assertEquals('Foo Content', $obj->Content, 'Records merged'); $obj = $this->factory->createObject(PopulateFactoryTestObject::class, 'test', [ 'Title' => 'Updated', 'PopulateMergeWhen' => "Title = 'This title is unknown'", ]); $this->assertGreaterThan(0, $obj->ID); $this->assertEquals('Updated', $obj->Title); } /** * When populatemergematch is provided then the matching should be done on * the given fields */ public function testCreateObjectPopulateMergeMatch() { $id = PopulateFactoryTestObject::get()->filter([ 'Title' => 'Foo', ])->first()->ID; $obj = $this->factory->createObject(PopulateFactoryTestObject::class, 'test', [ 'Title' => 'Foo', 'Content' => 'This has been replaced', 'PopulateMergeMatch' => [ 'Title', ], ]); $this->assertEquals($id, $obj->ID, 'ID value has not changed'); $this->assertEquals('This has been replaced', $obj->Content); } /** * When a lookup matches more than one page, only the first one should be * removed. */ public function testMultipleMatchesRemoved() { $obj = $this->factory->createObject(PopulateFactoryTestObject::class, 'test', [ 'Title' => 'Updated', 'PopulateMergeAny' => true, ]); $list = PopulateFactoryTestObject::get(); $this->assertEquals(1, $list->count()); $this->assertEquals('Updated', $list->first()->Title); } } |