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 6, 2024
1 parent 7eb74af commit 4679e25
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 31 deletions.
25 changes: 15 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\HybridExecution\HybridOutput;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;

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

protected $title = 'MigrateContentToElement';
protected string $title = 'MigrateContentToElement';

protected $description = 'When installing Elemental this task converts content in the $Content '
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, HybridOutput $output): int
{
$pageTypes = singleton(ElementalArea::class)->supportedPageTypes();
$count = 0;
Expand Down Expand Up @@ -91,10 +93,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 +129,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
62 changes: 41 additions & 21 deletions tests/Tasks/MigrateContentToElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\HybridExecution\HybridOutput;
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 +45,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 HybridOutput(HybridOutput::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 +78,12 @@ public function testContentIsNotClearedWhenConfigured()

$task = new MigrateContentToElement();

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

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

Expand All @@ -85,9 +94,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, $hybridOutput);
$output = $buffer->fetch();

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

$task = new MigrateContentToElement();

ob_start();
$task->run(new HTTPRequest('GET', ''));
$output = ob_get_clean();
$buffer = new BufferedOutput();
$output = new HybridOutput(HybridOutput::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 +136,12 @@ public function testPublishingConfigurationIsRespected()

$task = new MigrateContentToElement();

ob_start();
$task->run(new HTTPRequest('GET', ''));
$output = ob_get_clean();
$buffer = new BufferedOutput();
$output = new HybridOutput(HybridOutput::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 +166,12 @@ public function testPagesThatWereNotPublishedAreNotPublishedDuringThisTask()
{
$task = new MigrateContentToElement();

ob_start();
$task->run(new HTTPRequest('GET', ''));
$output = ob_get_clean();
$buffer = new BufferedOutput();
$output = new HybridOutput(HybridOutput::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 +204,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 HybridOutput(HybridOutput::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 4679e25

Please sign in to comment.