Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / migrate_tools / tests / src / Functional / SourceCsvFormTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate_tools\Functional;
4
5 use Drupal\Core\StreamWrapper\PublicStream;
6 use Drupal\Core\StreamWrapper\StreamWrapperInterface;
7 use Drupal\Tests\BrowserTestBase;
8 use Drupal\taxonomy\Entity\Vocabulary;
9 use Drupal\taxonomy\VocabularyInterface;
10
11 /**
12  * Test the CSV column alias edit form.
13  *
14  * @requires module migrate_source_csv
15  *
16  * @group migrate_tools
17  */
18 class SourceCsvFormTest extends BrowserTestBase {
19
20   /**
21    * Temporary store for column assignment changes.
22    *
23    * @var \Drupal\Core\TempStore\PrivateTempStoreFactory
24    */
25   protected $store;
26
27   /**
28    * {@inheritdoc}
29    */
30   protected static $modules = [
31     'taxonomy',
32     'migrate',
33     'migrate_plus',
34     'migrate_tools',
35     'migrate_source_csv',
36     'csv_source_test',
37   ];
38
39   /**
40    * {@inheritdoc}
41    */
42   protected $profile = 'testing';
43
44   /**
45    * The migration group for the test migration.
46    *
47    * @var string
48    */
49   protected $group;
50
51   /**
52    * The test migration id.
53    *
54    * @var string
55    */
56   protected $migration;
57
58   /**
59    * {@inheritdoc}
60    */
61   protected function setUp() {
62     parent::setUp();
63
64     // Log in as user 1. Migrations in the UI can only be performed as user 1.
65     $this->drupalLogin($this->rootUser);
66
67     // Setup the file system so we create the source CSV.
68     $this->container->get('stream_wrapper_manager')->registerWrapper('public', PublicStream::class, StreamWrapperInterface::NORMAL);
69     $fs = \Drupal::service('file_system');
70     $fs->mkdir('public://sites/default/files', NULL, TRUE);
71
72     // The source data for this test.
73     $source_data = <<<'EOD'
74 vid,name,description,hierarchy,weight
75 tags,Tags,Use tags to group articles,0,0
76 forums,Sujet de discussion,Forum navigation vocabulary,1,0
77 test_vocabulary,Test Vocabulary,This is the vocabulary description,1,0
78 genre,Genre,Genre description,1,0
79 EOD;
80
81     // Write the data to the filepath given in the test migration.
82     file_put_contents('public://test.csv', $source_data);
83
84     // Get the store.
85     $tempStoreFactory = \Drupal::service('tempstore.private');
86     $this->store = $tempStoreFactory->get('migrate_tools');
87
88     // Select the group and migration to test.
89     $this->group = 'csv_test';
90     $this->migration = 'csv_source_test';
91   }
92
93   /**
94    * Tests the form to edit CSV column aliases.
95    *
96    * @throws \Behat\Mink\Exception\ExpectationException
97    */
98   public function testSourceCsvForm() {
99     // Define the paths to be used.
100     $executeUrlPath = "/admin/structure/migrate/manage/{$this->group}/migrations/{$this->migration}/execute";
101     $editUrlPath = "/admin/structure/migrate/manage/{$this->group}/migrations/{$this->migration}/source/edit";
102
103     // Assert the test migration is listed.
104     $this->drupalGet("/admin/structure/migrate/manage/{$this->group}/migrations");
105     $session = $this->assertSession();
106     $session->responseContains('Test edit of column aliases for CSV source plugin');
107
108     // Proceed to the edit page.
109     $this->drupalGet($editUrlPath);
110     $session->responseContains('You can change the columns to be used by this migration for each source property.');
111
112     // Test that there are 3 select fields available which match the number of
113     // properties in the process pipeline.
114     $this->assertTrue($session->optionExists('edit-vid', 'vid')
115       ->isSelected());
116     $this->assertTrue($session->optionExists('edit-name', 'name')
117       ->isSelected());
118     $this->assertTrue($session->optionExists('edit-description', 'description')
119       ->isSelected());
120     $session->responseNotContains('edit-hierarchy');
121     $session->responseNotContains('edit-weight');
122
123     // Test that all 5 columns in the CSV source are available as options on
124     // one of the select fields.
125     $this->assertTrue($session->optionExists('edit-description', 'vid'));
126     $this->assertTrue($session->optionExists('edit-description', 'name'));
127     $this->assertTrue($session->optionExists('edit-description', 'description'));
128     $this->assertTrue($session->optionExists('edit-description', 'hierarchy'));
129     $this->assertTrue($session->optionExists('edit-description', 'weight'));
130
131     // Test that two aliases can not be the same.
132     $edit = [
133       'edit-vid' => 2,
134       'edit-name' => 1,
135       'edit-description' => 1,
136     ];
137     $this->drupalPostForm($editUrlPath, $edit, t('Submit'));
138     $session->responseContains('Source properties can not share the same source column.');
139     $this->assertTrue($session->optionExists('edit-vid', 'description')
140       ->isSelected());
141     $this->assertTrue($session->optionExists('edit-name', 'name')
142       ->isSelected());
143     $this->assertTrue($session->optionExists('edit-description', 'name')
144       ->isSelected());
145
146     // Test that changes to all the column aliases are saved.
147     $edit = [
148       'edit-vid' => 4,
149       'edit-name' => 0,
150       'edit-description' => 1,
151     ];
152     $this->drupalPostForm($editUrlPath, $edit, t('Submit'));
153     $this->assertTrue($session->optionExists('edit-vid', 'weight')
154       ->isSelected());
155     $this->assertTrue($session->optionExists('edit-name', 'vid')
156       ->isSelected());
157     $this->assertTrue($session->optionExists('edit-description', 'name')
158       ->isSelected());
159
160     // Test that the changes are saved to store.
161     $columnConfiguration = $this->store->get('csv_source_test');
162     $migrationsChanged = $this->store->get('migrations_changed');
163     $this->assertSame(['csv_source_test'], $migrationsChanged);
164     $expected =
165       [
166         'original' =>
167           [
168             0 => ['vid' => 'Vocabulary Id'],
169             1 => ['name' => 'Name'],
170             2 => ['description' => 'Description'],
171           ],
172         'changed' =>
173           [
174             4 => ['vid' => 'weight'],
175             0 => ['name' => 'vid'],
176             1 => ['description' => 'name'],
177           ],
178       ];
179     $this->assertSame($expected, $columnConfiguration);
180
181     // Test the migration with incorrect column aliases. Flush the cache to
182     // ensure the plugin alter is run.
183     drupal_flush_all_caches();
184     $edit = [
185       'operation' => 'import',
186     ];
187     $this->drupalPostForm($executeUrlPath, $edit, t('Execute'));
188     $session->responseContains("Processed 1 item (1 created, 0 updated, 0 failed, 0 ignored) - done with 'csv_source_test'");
189
190     // Rollback.
191     $edit = [
192       'operation' => 'rollback',
193     ];
194     $this->drupalPostForm($executeUrlPath, $edit, t('Execute'));
195
196     // Restore to an order that will succesfully migrate.
197     $edit = [
198       'edit-vid' => 0,
199       'edit-name' => 1,
200       'edit-description' => 2,
201     ];
202     $this->drupalPostForm($editUrlPath, $edit, t('Submit'));
203     $this->assertTrue($session->optionExists('edit-vid', 'vid')
204       ->isSelected());
205     $this->assertTrue($session->optionExists('edit-name', 'name')
206       ->isSelected());
207     $this->assertTrue($session->optionExists('edit-description', 'description')
208       ->isSelected());
209
210     // Test the vocabulary migration.
211     $edit = [
212       'operation' => 'import',
213     ];
214     drupal_flush_all_caches();
215     $this->drupalPostForm($executeUrlPath, $edit, t('Execute'));
216     $session->responseContains("Processed 4 items (4 created, 0 updated, 0 failed, 0 ignored) - done with 'csv_source_test'");
217     $this->assertEntity('tags', 'Tags', 'Use tags to group articles');
218     $this->assertEntity('forums', 'Sujet de discussion', 'Forum navigation vocabulary');
219     $this->assertEntity('test_vocabulary', 'Test Vocabulary', 'This is the vocabulary description');
220     $this->assertEntity('genre', 'Genre', 'Genre description');
221   }
222
223   /**
224    * Validate a migrated vocabulary contains the expected values.
225    *
226    * @param string $id
227    *   Entity ID to load and check.
228    * @param string $expected_name
229    *   The name the migrated entity should have.
230    * @param string $expected_description
231    *   The description the migrated entity should have.
232    */
233   protected function assertEntity($id, $expected_name, $expected_description) {
234     /** @var \Drupal\taxonomy\VocabularyInterface $entity */
235     $entity = Vocabulary::load($id);
236     $this->assertTrue($entity instanceof VocabularyInterface);
237     $this->assertSame($expected_name, $entity->label());
238     $this->assertSame($expected_description, $entity->getDescription());
239   }
240
241 }