-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NEW Content migration task has a bunch of new features...:
- Fixes #617 - The task now checks for pages with no content instead of no elemental area - Developers can configure a specific element type and attribute to migrate content too - Content is now published during the migration - Added a flag to control whether content is published - Whether this task clears content from the page during migration is configurable. If it is not cleared out, content will not migrate if the page already has elements. - Added some extension points and extracted some logic into methods to allow flexible subclassing / extensions
- Loading branch information
Showing
5 changed files
with
299 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<?php | ||
namespace DNADesign\Elemental\Tests\Tasks; | ||
|
||
use DNADesign\Elemental\Extensions\ElementalPageExtension; | ||
use DNADesign\Elemental\Models\ElementContent; | ||
use DNADesign\Elemental\Tasks\MigrateContentToElement; | ||
use DNADesign\Elemental\Tests\Src\TestElement; | ||
use DNADesign\Elemental\Tests\Src\TestPage; | ||
use SilverStripe\Control\HTTPRequest; | ||
use SilverStripe\Core\Config\Config; | ||
use SilverStripe\Dev\SapphireTest; | ||
use SilverStripe\ORM\HasManyList; | ||
use SilverStripe\Versioned\Versioned; | ||
|
||
class MigrateContentToElementTest extends SapphireTest | ||
{ | ||
protected static $fixture_file = 'MigrateContentToElementTest.yml'; | ||
|
||
protected static $required_extensions = [ | ||
TestPage::class => [ | ||
ElementalPageExtension::class, | ||
], | ||
]; | ||
|
||
protected static $extra_dataobjects = [ | ||
TestElement::class, | ||
TestPage::class, | ||
]; | ||
|
||
public function testContentIsMigratedFromPagesToNewElements() | ||
{ | ||
$task = new MigrateContentToElement(); | ||
|
||
ob_start(); | ||
$task->run(new HTTPRequest('GET', '')); | ||
$output = ob_get_clean(); | ||
|
||
$this->assertContains('Finished migrating 1 pages\' content', $output); | ||
|
||
// Get the page that should've been updated and the content should be removed | ||
/** @var TestPage&Versioned $page */ | ||
$page = $this->objFromFixture(TestPage::class, 'page3'); | ||
$this->assertEmpty($page->Content); | ||
|
||
// Check that there's one element and it contains the old content | ||
/** @var HasManyList $elements */ | ||
$elements = $page->ElementalArea->Elements(); | ||
$this->assertCount(1, $elements); | ||
|
||
/** @var ElementContent&Versioned $contentElement */ | ||
$contentElement = $elements->first(); | ||
$this->assertSame('This is page 3', $contentElement->HTML); | ||
|
||
// Assert that the element and page are "live" | ||
$this->assertTrue($contentElement->isLiveVersion()); | ||
$this->assertTrue($page->isLiveVersion()); | ||
} | ||
|
||
public function testContentIsNotClearedWhenConfigured() | ||
{ | ||
Config::modify()->set(MigrateContentToElement::class, 'clear_content', false); | ||
|
||
$task = new MigrateContentToElement(); | ||
|
||
ob_start(); | ||
$task->run(new HTTPRequest('GET', '')); | ||
$output = ob_get_clean(); | ||
|
||
$this->assertContains('Finished migrating 1 pages\' content', $output); | ||
|
||
$page = $this->objFromFixture(TestPage::class, 'page3'); | ||
$this->assertContains('This is page 3', $page->Content, 'Content is not removed from the page'); | ||
|
||
$element = $page->ElementalArea->Elements()->first(); | ||
$this->assertContains('This is page 3', $element->HTML, 'Content is still added to a new element'); | ||
|
||
// Run the task again and assert the page is not picked up again | ||
ob_start(); | ||
$task->run(new HTTPRequest('GET', '')); | ||
$output = ob_get_clean(); | ||
|
||
$this->assertContains('Finished migrating 0 pages\' content', $output); | ||
$page = $this->objFromFixture(TestPage::class, 'page3'); | ||
$this->assertCount(1, $page->ElementalArea->Elements()); | ||
} | ||
|
||
public function testTargetElementConfigurationIsRespected() | ||
{ | ||
Config::modify()->set(MigrateContentToElement::class, 'target_element', TestElement::class); | ||
Config::modify()->set(MigrateContentToElement::class, 'target_element_field', 'TestValue'); | ||
|
||
$task = new MigrateContentToElement(); | ||
|
||
ob_start(); | ||
$task->run(new HTTPRequest('GET', '')); | ||
$output = ob_get_clean(); | ||
|
||
$this->assertContains('Finished migrating 1 pages\' content', $output); | ||
|
||
// Get the page that should've been updated and the content should be removed | ||
$element = $this->objFromFixture(TestPage::class, 'page3')->ElementalArea->Elements()->first(); | ||
|
||
$this->assertInstanceOf(TestElement::class, $element); | ||
$this->assertSame('This is page 3', $element->TestValue); | ||
} | ||
|
||
public function testPublishingConfigurationIsRespected() | ||
{ | ||
Config::modify()->set(MigrateContentToElement::class, 'publish_changes', false); | ||
|
||
// Ensure the page is published to begin with | ||
/** @var TestPage&Versioned $page */ | ||
$page = $this->objFromFixture(TestPage::class, 'page3'); | ||
$page->publishSingle(); | ||
|
||
$task = new MigrateContentToElement(); | ||
|
||
ob_start(); | ||
$task->run(new HTTPRequest('GET', '')); | ||
$output = ob_get_clean(); | ||
|
||
$this->assertContains('Finished migrating 1 pages\' content', $output); | ||
|
||
// Get the page that should've been updated and the content should be removed | ||
$page = $this->objFromFixture(TestPage::class, 'page3'); | ||
/** @var ElementContent&Versioned $element */ | ||
$element = $page->ElementalArea->Elements()->first(); | ||
|
||
$this->assertSame('This is page 3', $element->HTML); | ||
|
||
$this->assertFalse($page->isLiveVersion()); | ||
$this->assertFalse($element->isLiveVersion()); | ||
|
||
$this->assertFalse($element->isPublished()); | ||
$this->assertEmpty($page->Content); | ||
|
||
$livePage = Versioned::get_by_stage(TestPage::class, Versioned::LIVE)->byID($page->ID); | ||
$this->assertSame('This is page 3', $livePage->Content); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
Page: | ||
page1: | ||
Title: Page 1 | ||
URLSegment: test-page-1 | ||
Content: "This is page 1" | ||
page2: | ||
Title: Page 2 | ||
URLSegment: test-page-2 | ||
|
||
DNADesign\Elemental\Tests\Src\TestPage: | ||
page3: | ||
Title: Page 3 | ||
URLSegment: test-page-3 | ||
Content: "This is page 3" | ||
page4: | ||
Title: Page 4 | ||
URLSegment: test-page-4 |