Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Update API to reflect changes to CLI interaction #1252

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
emteknetnz marked this conversation as resolved.
Show resolved Hide resolved

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
Loading