3 namespace Drupal\Tests\migrate_tools\Functional;
5 use Drupal\taxonomy\Entity\Vocabulary;
6 use Drupal\Tests\BrowserTestBase;
11 * @group migrate_tools
13 class MigrateExecutionFormTest extends BrowserTestBase {
18 protected static $modules = [
34 protected $profile = 'testing';
39 * @var \Drupal\taxonomy\VocabularyInterface
41 protected $vocabulary;
44 * The vocabulary query.
46 * @var \Drupal\Core\Entity\Query\QueryInterface
48 protected $vocabularyQuery;
53 protected function setUp() {
55 $this->vocabulary = $this->createVocabulary(['vid' => 'fruit', 'name' => 'Fruit']);
56 $this->vocabularyQuery = $this->container->get('entity_type.manager')
57 ->getStorage('taxonomy_term')
59 // Log in as user 1. Migrations in the UI can only be performed as user 1.
60 $this->drupalLogin($this->rootUser);
64 * Tests execution of import and rollback of a migration.
66 * @throws \Behat\Mink\Exception\ExpectationException
68 public function testExecution() {
70 $migration = 'fruit_terms';
71 $urlPath = "/admin/structure/migrate/manage/{$group}/migrations/{$migration}/execute";
72 $real_count = $this->vocabularyQuery->count()->execute();
74 $this->assertEquals($expected_count, $real_count);
75 $this->drupalGet($urlPath);
76 $this->assertSession()->responseContains('Choose an operation to run');
78 'operation' => 'import',
80 $this->drupalPostForm($urlPath, $edit, t('Execute'));
81 $real_count = $this->vocabularyQuery->count()->execute();
83 $this->assertEquals($expected_count, $real_count);
85 'operation' => 'rollback',
87 $this->drupalPostForm($urlPath, $edit, t('Execute'));
88 $real_count = $this->vocabularyQuery->count()->execute();
90 $this->assertEquals($expected_count, $real_count);
92 'operation' => 'import',
94 $this->drupalPostForm($urlPath, $edit, t('Execute'));
95 $real_count = $this->vocabularyQuery->count()->execute();
97 $this->assertEquals($expected_count, $real_count);
101 * Creates a custom vocabulary based on default settings.
103 * @param array $values
104 * An array of settings to change from the defaults.
105 * Example: 'vid' => 'foo'.
107 * @return \Drupal\taxonomy\VocabularyInterface
108 * Created vocabulary.
110 protected function createVocabulary(array $values = []) {
111 // Find a non-existent random vocabulary name.
112 if (!isset($values['vid'])) {
114 $id = strtolower($this->randomMachineName(8));
115 } while (Vocabulary::load($id));
118 $id = $values['vid'];
124 $vocabulary = Vocabulary::create($values);
125 $status = $vocabulary->save();
127 $this->assertSame($status, SAVED_NEW);