Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / migrate_tools / tests / src / Functional / MigrateExecutionFormTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate_tools\Functional;
4
5 use Drupal\taxonomy\Entity\Vocabulary;
6 use Drupal\Tests\BrowserTestBase;
7
8 /**
9  * Execution form test.
10  *
11  * @group migrate_tools
12  */
13 class MigrateExecutionFormTest extends BrowserTestBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   protected static $modules = [
19     'user',
20     'filter',
21     'field',
22     'node',
23     'text',
24     'taxonomy',
25     'migrate',
26     'migrate_plus',
27     'migrate_tools',
28     'migrate_tools_test',
29   ];
30
31   /**
32    * {@inheritdoc}
33    */
34   protected $profile = 'testing';
35
36   /**
37    * The vocabulary.
38    *
39    * @var \Drupal\taxonomy\VocabularyInterface
40    */
41   protected $vocabulary;
42
43   /**
44    * The vocabulary query.
45    *
46    * @var \Drupal\Core\Entity\Query\QueryInterface
47    */
48   protected $vocabularyQuery;
49
50   /**
51    * {@inheritdoc}
52    */
53   protected function setUp() {
54     parent::setUp();
55     $this->vocabulary = $this->createVocabulary(['vid' => 'fruit', 'name' => 'Fruit']);
56     $this->vocabularyQuery = $this->container->get('entity_type.manager')
57       ->getStorage('taxonomy_term')
58       ->getQuery();
59     // Log in as user 1. Migrations in the UI can only be performed as user 1.
60     $this->drupalLogin($this->rootUser);
61   }
62
63   /**
64    * Tests execution of import and rollback of a migration.
65    *
66    * @throws \Behat\Mink\Exception\ExpectationException
67    */
68   public function testExecution() {
69     $group = 'default';
70     $migration = 'fruit_terms';
71     $urlPath = "/admin/structure/migrate/manage/{$group}/migrations/{$migration}/execute";
72     $real_count = $this->vocabularyQuery->count()->execute();
73     $expected_count = 0;
74     $this->assertEquals($expected_count, $real_count);
75     $this->drupalGet($urlPath);
76     $this->assertSession()->responseContains('Choose an operation to run');
77     $edit = [
78       'operation' => 'import',
79     ];
80     $this->drupalPostForm($urlPath, $edit, t('Execute'));
81     $real_count = $this->vocabularyQuery->count()->execute();
82     $expected_count = 3;
83     $this->assertEquals($expected_count, $real_count);
84     $edit = [
85       'operation' => 'rollback',
86     ];
87     $this->drupalPostForm($urlPath, $edit, t('Execute'));
88     $real_count = $this->vocabularyQuery->count()->execute();
89     $expected_count = 0;
90     $this->assertEquals($expected_count, $real_count);
91     $edit = [
92       'operation' => 'import',
93     ];
94     $this->drupalPostForm($urlPath, $edit, t('Execute'));
95     $real_count = $this->vocabularyQuery->count()->execute();
96     $expected_count = 3;
97     $this->assertEquals($expected_count, $real_count);
98   }
99
100   /**
101    * Creates a custom vocabulary based on default settings.
102    *
103    * @param array $values
104    *   An array of settings to change from the defaults.
105    *   Example: 'vid' => 'foo'.
106    *
107    * @return \Drupal\taxonomy\VocabularyInterface
108    *   Created vocabulary.
109    */
110   protected function createVocabulary(array $values = []) {
111     // Find a non-existent random vocabulary name.
112     if (!isset($values['vid'])) {
113       do {
114         $id = strtolower($this->randomMachineName(8));
115       } while (Vocabulary::load($id));
116     }
117     else {
118       $id = $values['vid'];
119     }
120     $values += [
121       'id' => $id,
122       'name' => $id,
123     ];
124     $vocabulary = Vocabulary::create($values);
125     $status = $vocabulary->save();
126
127     $this->assertSame($status, SAVED_NEW);
128
129     return $vocabulary;
130   }
131
132 }