Skip to content

Commit

Permalink
Merge pull request kaliop-uk#113 from slaci/fix-executor-language
Browse files Browse the repository at this point in the history
Fix default language for migrations
  • Loading branch information
gggeek committed Apr 17, 2017
2 parents b59618b + c00054d commit a384620
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 30 deletions.
3 changes: 3 additions & 0 deletions Core/MigrationService.php
Expand Up @@ -255,6 +255,9 @@ public function executeMigration(MigrationDefinition $migrationDefinition, $useT
foreach ($migrationDefinition->steps as $step) {
// we validated the fact that we have a good executor at parsing time
$executor = $this->executors[$step->type];
if ($executor instanceof LanguageAwareInterface) {
$executor->setLanguageCode(null);
}

$beforeStepExecutionEvent = new BeforeStepExecutionEvent($step, $executor);
$this->dispatcher->dispatch($this->eventPrefix . 'before_execution', $beforeStepExecutionEvent);
Expand Down
41 changes: 41 additions & 0 deletions Tests/dsl/UnitTestOK018_defaultLanguage.yml
@@ -0,0 +1,41 @@
-
type: language
mode: create
lang: def-LA
name: Kaliop Migration Bundle Language 1
-
type: content_type
mode: create
content_type_group: 1
identifier: kmb_test_18
name: Kaliop Migration Bundle Test Class 18
name_pattern: '<ezstring>'
attributes:
-
type: ezstring
name: ezstring
identifier: ezstring
references:
-
identifier: kmb_test_18
attribute: identifier
-
type: content
mode: create
content_type: 'reference:kmb_test_18'
remote_id: kmb_test_18_content_1
parent_location: 2
lang: eng-GB
attributes:
-
ezstring: hello world 1
-
type: content
mode: create
content_type: 'reference:kmb_test_18'
remote_id: kmb_test_18_content_2
parent_location: 2
# lang: should be the default language (def-LA and not eng-GB from the previous)
attributes:
-
ezstring: hello world 2
145 changes: 115 additions & 30 deletions Tests/phpunit/2_MigrateTest.php
Expand Up @@ -22,16 +22,7 @@ public function testExecuteGoodDSL($filePath = '')
return;
}

// Make sure migration is not in the db: delete it, ignoring errors
$input = new ArrayInput(array('command' => 'kaliop:migration:migration', 'migration' => basename($filePath), '--delete' => true, '-n' => true));
$this->app->run($input, $this->output);
$this->fetchOutput();

$input = new ArrayInput(array('command' => 'kaliop:migration:migration', 'migration' => $filePath, '--add' => true, '-n' => true));
$exitCode = $this->app->run($input, $this->output);
$output = $this->fetchOutput();
$this->assertSame(0, $exitCode, 'CLI Command failed. Output: ' . $output);
$this->assertRegexp('?Added migration?', $output);
$this->prepareMigration($filePath);

$count1 = BeforeStepExecutionListener::getExecutions();
$count2 = StepExecutedListener::getExecutions();
Expand Down Expand Up @@ -64,16 +55,7 @@ public function testExecuteInvalidDSL($filePath = '')
return;
}

// Make user migration is not in the db: delete it, ignoring errors
$input = new ArrayInput(array('command' => 'kaliop:migration:migration', 'migration' => basename($filePath), '--delete' => true, '-n' => true));
$this->app->run($input, $this->output);
$this->fetchOutput();

$input = new ArrayInput(array('command' => 'kaliop:migration:migration', 'migration' => $filePath, '--add' => true, '-n' => true));
$exitCode = $this->app->run($input, $this->output);
$output = $this->fetchOutput();
$this->assertSame(0, $exitCode, 'CLI Command failed. Output: ' . $output);
$this->assertRegexp('?Added migration?', $output);
$this->prepareMigration($filePath);

$input = new ArrayInput(array('command' => 'kaliop:migration:migrate', '--path' => array($filePath), '-n' => true, '-u' => true));
$exitCode = $this->app->run($input, $this->output);
Expand All @@ -99,16 +81,7 @@ public function testExecuteBadDSL($filePath = '')
return;
}

// Make user migration is not in the db: delete it, ignoring errors
$input = new ArrayInput(array('command' => 'kaliop:migration:migration', 'migration' => basename($filePath), '--delete' => true, '-n' => true));
$this->app->run($input, $this->output);
$this->fetchOutput();

$input = new ArrayInput(array('command' => 'kaliop:migration:migration', 'migration' => $filePath, '--add' => true, '-n' => true));
$exitCode = $this->app->run($input, $this->output);
$output = $this->fetchOutput();
$this->assertSame(0, $exitCode, 'CLI Command failed. Output: ' . $output);
$this->assertRegexp('?Added migration?', $output);
$this->prepareMigration($filePath);

$input = new ArrayInput(array('command' => 'kaliop:migration:migrate', '--path' => array($filePath), '-n' => true, '-u' => true));
$exitCode = $this->app->run($input, $this->output);
Expand All @@ -123,6 +96,46 @@ public function testExecuteBadDSL($filePath = '')
$this->assertSame(0, $exitCode, 'CLI Command failed. Output: ' . $output);
}

/**
* Test the --default-language option for the migrate command.
*/
public function testDefaultLanguage()
{
$filePath = $this->dslDir . '/UnitTestOK018_defaultLanguage.yml';
$defaultLanguage = 'def-LA';

$this->prepareMigration($filePath);

$exitCode = $this->runCommand('kaliop:migration:migrate', array(
'--path' => array($filePath),
'-n' => true,
'-u' => true,
'--default-language' => $defaultLanguage,
));
$output = $this->fetchOutput();
$this->assertSame(0, $exitCode, 'CLI Command failed. Output: ' . $output);
// check that there are no notes after adding the migration
$this->assertRegexp('?\| ' . basename($filePath) . ' +\| +\|?', $output);

$repository = $this->getRepository();
$contentService = $repository->getContentService();

// check if the content was created with the default language
$content = $contentService->loadContentByRemoteId('kmb_test_18_content_2', [$defaultLanguage], null, false);
$this->assertInstanceOf('eZ\Publish\API\Repository\Values\Content\Content', $content);
$this->assertSame($defaultLanguage, $content->contentInfo->mainLanguageCode);

// cleanup
$contentService->deleteContent($content->contentInfo);
$contentService->deleteContent($contentService->loadContentInfoByRemoteId('kmb_test_18_content_1'));

$contentTypeService = $repository->getContentTypeService();
$contentTypeService->deleteContentType($contentTypeService->loadContentTypeByIdentifier('kmb_test_18'));

$langService = $repository->getContentLanguageService();
$langService->deleteLanguage($langService->loadLanguage($defaultLanguage));
}

public function goodDSLProvider()
{
$dslDir = $this->dslDir.'/good';
Expand Down Expand Up @@ -173,4 +186,76 @@ public function badDSLProvider()
}
return $out;
}

/**
* Add a migration from a file to the migration service.
* @param string $filePath
*/
protected function addMigration($filePath)
{
$exitCode = $this->runCommand('kaliop:migration:migration', [
'migration' => $filePath,
'--add' => true,
'-n' => true,
]);
$output = $this->fetchOutput();
$this->assertSame(0, $exitCode, 'CLI Command failed. Output: ' . $output);
$this->assertRegexp('?Added migration?', $output);
}

/**
* Delete the migration from the database table
* @param string $filePath
* @return string
*/
protected function deleteMigration($filePath)
{
$this->runCommand('kaliop:migration:migration', [
'migration' => basename($filePath),
'--delete' => true,
'-n' => true,
]);

return $this->fetchOutput();
}

/**
* Prepare a migration file for a test.
* @param string $filePath
*/
protected function prepareMigration($filePath)
{
// Make user migration is not in the db: delete it, ignoring errors
$this->deleteMigration($filePath);
$this->addMigration($filePath);
}

/**
* Run a symfony command
* @param string $commandName
* @param array $params
* @return int
*/
protected function runCommand($commandName, array $params)
{
$params = array_merge(['command' => $commandName], $params);
$input = new ArrayInput($params);

return $this->app->run($input, $this->output);
}

/**
* Get the eZ repository
* @param int $loginUserId
* @return \eZ\Publish\Core\SignalSlot\Repository
*/
protected function getRepository($loginUserId = \Kaliop\eZMigrationBundle\Core\MigrationService::ADMIN_USER_ID)
{
$repository = $this->getContainer()->get('ezpublish.api.repository');
if ($loginUserId !== false && (is_null($repository->getCurrentUser()) || $repository->getCurrentUser()->id != $loginUserId)) {
$repository->setCurrentUser($repository->getUserService()->loadUser($loginUserId));
}

return $repository;
}
}

0 comments on commit a384620

Please sign in to comment.