3 namespace Drupal\Tests\migrate_tools\Functional;
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;
12 * Test the CSV column alias edit form.
14 * @requires module migrate_source_csv
16 * @group migrate_tools
18 class SourceCsvFormTest extends BrowserTestBase {
21 * Temporary store for column assignment changes.
23 * @var \Drupal\Core\TempStore\PrivateTempStoreFactory
30 protected static $modules = [
42 protected $profile = 'testing';
45 * The migration group for the test migration.
52 * The test migration id.
61 protected function setUp() {
64 // Log in as user 1. Migrations in the UI can only be performed as user 1.
65 $this->drupalLogin($this->rootUser);
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);
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
81 // Write the data to the filepath given in the test migration.
82 file_put_contents('public://test.csv', $source_data);
85 $tempStoreFactory = \Drupal::service('tempstore.private');
86 $this->store = $tempStoreFactory->get('migrate_tools');
88 // Select the group and migration to test.
89 $this->group = 'csv_test';
90 $this->migration = 'csv_source_test';
94 * Tests the form to edit CSV column aliases.
96 * @throws \Behat\Mink\Exception\ExpectationException
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";
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');
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.');
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')
116 $this->assertTrue($session->optionExists('edit-name', 'name')
118 $this->assertTrue($session->optionExists('edit-description', 'description')
120 $session->responseNotContains('edit-hierarchy');
121 $session->responseNotContains('edit-weight');
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'));
131 // Test that two aliases can not be the same.
135 'edit-description' => 1,
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')
141 $this->assertTrue($session->optionExists('edit-name', 'name')
143 $this->assertTrue($session->optionExists('edit-description', 'name')
146 // Test that changes to all the column aliases are saved.
150 'edit-description' => 1,
152 $this->drupalPostForm($editUrlPath, $edit, t('Submit'));
153 $this->assertTrue($session->optionExists('edit-vid', 'weight')
155 $this->assertTrue($session->optionExists('edit-name', 'vid')
157 $this->assertTrue($session->optionExists('edit-description', 'name')
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);
168 0 => ['vid' => 'Vocabulary Id'],
169 1 => ['name' => 'Name'],
170 2 => ['description' => 'Description'],
174 4 => ['vid' => 'weight'],
175 0 => ['name' => 'vid'],
176 1 => ['description' => 'name'],
179 $this->assertSame($expected, $columnConfiguration);
181 // Test the migration with incorrect column aliases. Flush the cache to
182 // ensure the plugin alter is run.
183 drupal_flush_all_caches();
185 'operation' => 'import',
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'");
192 'operation' => 'rollback',
194 $this->drupalPostForm($executeUrlPath, $edit, t('Execute'));
196 // Restore to an order that will succesfully migrate.
200 'edit-description' => 2,
202 $this->drupalPostForm($editUrlPath, $edit, t('Submit'));
203 $this->assertTrue($session->optionExists('edit-vid', 'vid')
205 $this->assertTrue($session->optionExists('edit-name', 'name')
207 $this->assertTrue($session->optionExists('edit-description', 'description')
210 // Test the vocabulary migration.
212 'operation' => 'import',
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');
224 * Validate a migrated vocabulary contains the expected values.
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.
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());