View source
<?php
namespace Drupal\Tests\migrate_tools\Functional;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\VocabularyInterface;
use Drupal\Tests\BrowserTestBase;
class MigrateExecutionFormTest extends BrowserTestBase {
use StringTranslationTrait;
protected static $modules = [
'user',
'filter',
'field',
'node',
'text',
'taxonomy',
'migrate',
'migrate_plus',
'migrate_tools',
'migrate_tools_test',
];
protected $defaultTheme = 'stark';
protected $vocabulary;
protected $vocabularyQuery;
protected function setUp() : void {
parent::setUp();
$this->vocabulary = $this
->createVocabulary([
'vid' => 'fruit',
'name' => 'Fruit',
]);
$this->vocabularyQuery = $this->container
->get('entity_type.manager')
->getStorage('taxonomy_term')
->getQuery();
$this
->drupalLogin($this->rootUser);
}
public function testExecution() : void {
$group = 'default';
$migration = 'fruit_terms';
$urlPath = "/admin/structure/migrate/manage/{$group}/migrations/{$migration}/execute";
$real_count = $this->vocabularyQuery
->count()
->execute();
$expected_count = 0;
$this
->assertEquals($expected_count, $real_count);
$this
->drupalGet($urlPath);
$this
->assertSession()
->responseContains('Choose an operation to run');
$edit = [
'operation' => 'import',
];
$this
->drupalPostForm($urlPath, $edit, $this
->t('Execute'));
$real_count = $this->vocabularyQuery
->count()
->execute();
$expected_count = 3;
$this
->assertEquals($expected_count, $real_count);
$edit = [
'operation' => 'rollback',
];
$this
->drupalPostForm($urlPath, $edit, $this
->t('Execute'));
$real_count = $this->vocabularyQuery
->count()
->execute();
$expected_count = 0;
$this
->assertEquals($expected_count, $real_count);
$edit = [
'operation' => 'import',
];
$this
->drupalPostForm($urlPath, $edit, $this
->t('Execute'));
$real_count = $this->vocabularyQuery
->count()
->execute();
$expected_count = 3;
$this
->assertEquals($expected_count, $real_count);
}
protected function createVocabulary(array $values = []) : VocabularyInterface {
if (!isset($values['vid'])) {
do {
$id = strtolower($this
->randomMachineName(8));
} while (Vocabulary::load($id));
}
else {
$id = $values['vid'];
}
$values += [
'id' => $id,
'name' => $id,
];
$vocabulary = Vocabulary::create($values);
$status = $vocabulary
->save();
$this
->assertSame($status, SAVED_NEW);
return $vocabulary;
}
}