Skip to content

Commit

Permalink
API Update API to reflect changes to CLI interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Sep 16, 2024
1 parent 7eb74af commit e14ce1f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 32 deletions.
27 changes: 17 additions & 10 deletions src/Tasks/MigrateContentToElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\BuildTask;
use SilverStripe\Versioned\Versioned;
use SilverStripe\PolyExecution\PolyOutput;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;

class MigrateContentToElement extends BuildTask
{
Expand Down Expand Up @@ -49,12 +51,14 @@ class MigrateContentToElement extends BuildTask
*/
private static $publish_changes = true;

protected $title = 'MigrateContentToElement';
protected static string $commandName = 'elemental-migrate-content';

protected $description = 'When installing Elemental this task converts content in the $Content '
protected string $title = 'MigrateContentToElement';

protected static string $description = 'When installing Elemental this task converts content in the $Content '
. 'field to an ElementContent';

public function run($request)
protected function execute(InputInterface $input, PolyOutput $output): int
{
$pageTypes = singleton(ElementalArea::class)->supportedPageTypes();
$count = 0;
Expand Down Expand Up @@ -91,10 +95,12 @@ public function run($request)
try {
$page->write();
} catch (Exception $e) {
echo sprintf(
'Could not clear content on page %s: %s',
$page->ID,
$e->getMessage()
$output->writeln(
'<comment>' . sprintf(
'Could not clear content on page %s: %s',
$page->ID,
$e->getMessage()
) . '</>'
);
}

Expand Down Expand Up @@ -125,9 +131,10 @@ public function run($request)
$pageTypeCount++;
}
$count += $pageTypeCount;
echo 'Migrated ' . $pageTypeCount . ' ' . $pageType . ' pages\' content<br>';
$output->writeln('Migrated ' . $pageTypeCount . ' ' . $pageType . ' pages\' content');
}
echo 'Finished migrating ' . $count . ' pages\' content<br>';
$output->writeln('Finished migrating ' . $count . ' pages\' content');
return Command::SUCCESS;
}

/**
Expand Down
63 changes: 41 additions & 22 deletions tests/Tasks/MigrateContentToElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
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\PolyExecution\PolyOutput;
use SilverStripe\ORM\HasManyList;
use SilverStripe\Versioned\Versioned;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;

class MigrateContentToElementTest extends SapphireTest
{
Expand Down Expand Up @@ -42,9 +44,12 @@ public function testContentIsMigratedFromPagesToNewElements()
$page = $this->objFromFixture(TestPage::class, 'page3');
$page->publishSingle();

ob_start();
$task->run(new HTTPRequest('GET', ''));
$output = ob_get_clean();
$buffer = new BufferedOutput();
$output = new PolyOutput(PolyOutput::FORMAT_ANSI, wrappedOutput: $buffer);
$input = new ArrayInput([]);
$input->setInteractive(false);
$task->run($input, $output);
$output = $buffer->fetch();

$this->assertStringContainsString('Finished migrating 1 pages\' content', $output);

Expand Down Expand Up @@ -72,9 +77,12 @@ public function testContentIsNotClearedWhenConfigured()

$task = new MigrateContentToElement();

ob_start();
$task->run(new HTTPRequest('GET', ''));
$output = ob_get_clean();
$buffer = new BufferedOutput();
$polyOutput = new PolyOutput(PolyOutput::FORMAT_ANSI, wrappedOutput: $buffer);
$input = new ArrayInput([]);
$input->setInteractive(false);
$task->run($input, $polyOutput);
$output = $buffer->fetch();

$this->assertStringContainsString('Finished migrating 1 pages\' content', $output);

Expand All @@ -85,9 +93,8 @@ public function testContentIsNotClearedWhenConfigured()
$this->assertStringContainsString('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();
$task->run($input, $polyOutput);
$output = $buffer->fetch();

$this->assertStringContainsString('Finished migrating 0 pages\' content', $output);
$page = $this->objFromFixture(TestPage::class, 'page3');
Expand All @@ -101,9 +108,12 @@ public function testTargetElementConfigurationIsRespected()

$task = new MigrateContentToElement();

ob_start();
$task->run(new HTTPRequest('GET', ''));
$output = ob_get_clean();
$buffer = new BufferedOutput();
$output = new PolyOutput(PolyOutput::FORMAT_ANSI, wrappedOutput: $buffer);
$input = new ArrayInput([]);
$input->setInteractive(false);
$task->run($input, $output);
$output = $buffer->fetch();

$this->assertStringContainsString('Finished migrating 1 pages\' content', $output);

Expand All @@ -125,9 +135,12 @@ public function testPublishingConfigurationIsRespected()

$task = new MigrateContentToElement();

ob_start();
$task->run(new HTTPRequest('GET', ''));
$output = ob_get_clean();
$buffer = new BufferedOutput();
$output = new PolyOutput(PolyOutput::FORMAT_ANSI, wrappedOutput: $buffer);
$input = new ArrayInput([]);
$input->setInteractive(false);
$task->run($input, $output);
$output = $buffer->fetch();

$this->assertStringContainsString('Finished migrating 1 pages\' content', $output);

Expand All @@ -152,9 +165,12 @@ public function testPagesThatWereNotPublishedAreNotPublishedDuringThisTask()
{
$task = new MigrateContentToElement();

ob_start();
$task->run(new HTTPRequest('GET', ''));
$output = ob_get_clean();
$buffer = new BufferedOutput();
$output = new PolyOutput(PolyOutput::FORMAT_ANSI, wrappedOutput: $buffer);
$input = new ArrayInput([]);
$input->setInteractive(false);
$task->run($input, $output);
$output = $buffer->fetch();

$this->assertStringContainsString('Finished migrating 1 pages\' content', $output);

Expand Down Expand Up @@ -187,9 +203,12 @@ public function testIgnoredClassesContentIsNotCleared()
$page = $this->objFromFixture(TestPage::class, 'page3');
$page->publishSingle();

ob_start();
$task->run(new HTTPRequest('GET', ''));
$output = ob_get_clean();
$buffer = new BufferedOutput();
$output = new PolyOutput(PolyOutput::FORMAT_ANSI, wrappedOutput: $buffer);
$input = new ArrayInput([]);
$input->setInteractive(false);
$task->run($input, $output);
$output = $buffer->fetch();

$this->assertStringContainsString('Finished migrating 0 pages\' content', $output);

Expand Down

0 comments on commit e14ce1f

Please sign in to comment.