Skip to content

Commit

Permalink
Merge branch '5.1' into 5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Apr 7, 2024
2 parents cfa66b9 + 44c6e0b commit c689a7a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 12 deletions.
7 changes: 1 addition & 6 deletions src/Extensions/ElementalAreasExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use SilverStripe\Forms\LiteralField;
use SilverStripe\ORM\DataExtension;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\RelatedData\StandardRelatedDataService;
use SilverStripe\Versioned\Versioned;
use SilverStripe\View\ViewableData;

Expand Down Expand Up @@ -329,15 +330,9 @@ public function requireDefaultRecords()
}
}

$needsPublishing = ViewableData::has_extension($elementalObject, Versioned::class)
&& $elementalObject->isPublished();

/** @var ElementalAreasExtension $elementalObject */
$elementalObject->ensureElementalAreasExist($elementalAreas);
$elementalObject->write();
if ($needsPublishing) {
$elementalObject->publishRecursive();
}
}

$this->owner->extend('onAfterRequireDefaultElementalRecords');
Expand Down
45 changes: 39 additions & 6 deletions tests/Extensions/ElementalAreasExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,34 @@

namespace DNADesign\Elemental\Tests\Extensions;

use DNADesign\Elemental\Extensions\ElementalAreasExtension;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Core\Config\Config;
use SilverStripe\Forms\LiteralField;
use DNADesign\Elemental\Models\ElementalArea;
use DNADesign\Elemental\Models\ElementContent;
use DNADesign\Elemental\Tests\Src\TestElement;
use DNADesign\Elemental\Tests\Src\TestUnusedElement;
use SilverStripe\Core\Config\Config;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
use SilverStripe\Forms\LiteralField;
use DNADesign\Elemental\Tests\Src\TestUnusedElement;
use DNADesign\Elemental\Tests\Src\TestVersionedDataObject;
use DNADesign\Elemental\Extensions\ElementalAreasExtension;
use SilverStripe\ORM\DB;

class ElementalAreasExtensionTest extends SapphireTest
{
protected static $required_extensions = [
SiteTree::class => [
ElementalAreasExtension::class,
],
TestVersionedDataObject::class => [
ElementalAreasExtension::class,
],
];

protected static $extra_dataobjects = [
TestElement::class,
TestUnusedElement::class,
TestVersionedDataObject::class,
];

protected function setUp(): void
Expand Down Expand Up @@ -110,4 +117,30 @@ public function provideContentFieldPreservationSettings()
[true, true, HTMLEditorField::class],
];
}

public function testRequireDefaultRecords()
{
/** @var TestVersionedDataObject|ElementalAreasExtension|Versioned $object */
$object = new TestVersionedDataObject();
$object->Title = 'abc';
$id = $object->write();
$object->publishSingle();
$object->Title = 'def';
$object->write();
$this->assertTrue($object->isModifiedOnDraft());
$tableName = $object->getSchema()->tableName(TestVersionedDataObject::class);
$tableNameLive = $tableName . '_Live';
// Explicitly set ElementalAreaID to null to simulate a fresh object that
// has the ElementalAreasExtension applied to it
// There's an onBeforeWrite() hook that will set ElementalAreaID so update DB directly
DB::query("UPDATE $tableName SET ElementalAreaID = NULL WHERE ID = $id");
DB::query("UPDATE $tableNameLive SET ElementalAreaID = NULL WHERE ID = $id");
$object->requireDefaultRecords();
// refetch object to ensure we're not using a cached version
$object = TestVersionedDataObject::get()->byID($id);
// assert that an ElementalID was set on the object
$this->assertSame(ElementalArea::get()->max('ID'), $object->ElementalAreaID);
// assert that we didn't accidentally publish the modified object
$this->assertTrue($object->isModifiedOnDraft());
}
}
29 changes: 29 additions & 0 deletions tests/Src/TestVersionedDataObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace DNADesign\Elemental\Tests\Src;

use DNADesign\Elemental\Models\ElementalArea;
use SilverStripe\ORM\DataObject;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Versioned\Versioned;

class TestVersionedDataObject extends DataObject implements TestOnly
{
private static $table_name = 'TestVersionedDataObject';

private static $extensions = [
Versioned::class,
];

private static $db = [
'Title' => 'Varchar(255)',
];

private static $has_one = [
'ElementalArea' => ElementalArea::class,
];

private static $owns = [
'ElementalArea',
];
}

0 comments on commit c689a7a

Please sign in to comment.