View source
<?php
namespace Drupal\Tests\migrate_tools\Functional;
use Drupal\Core\StreamWrapper\PublicStream;
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Tests\BrowserTestBase;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\VocabularyInterface;
class SourceCsvFormTest extends BrowserTestBase {
use StringTranslationTrait;
protected $store;
protected static $modules = [
'csv_source_test',
'migrate',
'migrate_plus',
'migrate_source_csv',
'migrate_tools',
'taxonomy',
];
protected $profile = 'testing';
protected $group;
protected $migration;
protected function setUp() {
parent::setUp();
$this
->drupalLogin($this->rootUser);
$this->container
->get('stream_wrapper_manager')
->registerWrapper('public', PublicStream::class, StreamWrapperInterface::NORMAL);
$fs = \Drupal::service('file_system');
$fs
->mkdir('public://sites/default/files', NULL, TRUE);
$source_data = <<<'EOD'
vid,name,description,hierarchy,weight
tags,Tags,Use tags to group articles,0,0
forums,Sujet de discussion,Forum navigation vocabulary,1,0
test_vocabulary,Test Vocabulary,This is the vocabulary description,1,0
genre,Genre,Genre description,1,0
EOD;
file_put_contents('public://test.csv', $source_data);
$tempStoreFactory = \Drupal::service('tempstore.private');
$this->store = $tempStoreFactory
->get('migrate_tools');
$this->group = 'csv_test';
$this->migration = 'csv_source_test';
}
public function testSourceCsvForm() {
$executeUrlPath = "/admin/structure/migrate/manage/{$this->group}/migrations/{$this->migration}/execute";
$editUrlPath = "/admin/structure/migrate/manage/{$this->group}/migrations/{$this->migration}/source/edit";
$this
->drupalGet("/admin/structure/migrate/manage/{$this->group}/migrations");
$session = $this
->assertSession();
$session
->responseContains('Test edit of column aliases for CSV source plugin');
$this
->drupalGet($editUrlPath);
$session
->responseContains('You can change the columns to be used by this migration for each source property.');
$this
->assertTrue($session
->optionExists('edit-vid', 'vid')
->isSelected());
$this
->assertTrue($session
->optionExists('edit-name', 'name')
->isSelected());
$this
->assertTrue($session
->optionExists('edit-description', 'description')
->isSelected());
$session
->responseNotContains('edit-hierarchy');
$session
->responseNotContains('edit-weight');
$this
->assertTrue($session
->optionExists('edit-description', 'vid'));
$this
->assertTrue($session
->optionExists('edit-description', 'name'));
$this
->assertTrue($session
->optionExists('edit-description', 'description'));
$this
->assertTrue($session
->optionExists('edit-description', 'hierarchy'));
$this
->assertTrue($session
->optionExists('edit-description', 'weight'));
$edit = [
'edit-vid' => 2,
'edit-name' => 1,
'edit-description' => 1,
];
$this
->drupalPostForm($editUrlPath, $edit, $this
->t('Submit'));
$session
->responseContains('Source properties can not share the same source column.');
$this
->assertTrue($session
->optionExists('edit-vid', 'description')
->isSelected());
$this
->assertTrue($session
->optionExists('edit-name', 'name')
->isSelected());
$this
->assertTrue($session
->optionExists('edit-description', 'name')
->isSelected());
$edit = [
'edit-vid' => 4,
'edit-name' => 0,
'edit-description' => 1,
];
$this
->drupalPostForm($editUrlPath, $edit, $this
->t('Submit'));
$this
->assertTrue($session
->optionExists('edit-vid', 'weight')
->isSelected());
$this
->assertTrue($session
->optionExists('edit-name', 'vid')
->isSelected());
$this
->assertTrue($session
->optionExists('edit-description', 'name')
->isSelected());
$columnConfiguration = $this->store
->get('csv_source_test');
$migrationsChanged = $this->store
->get('migrations_changed');
$this
->assertSame([
'csv_source_test',
], $migrationsChanged);
$expected = [
'original' => [
0 => [
'vid' => 'Vocabulary Id',
],
1 => [
'name' => 'Name',
],
2 => [
'description' => 'Description',
],
],
'changed' => [
4 => [
'vid' => 'weight',
],
0 => [
'name' => 'vid',
],
1 => [
'description' => 'name',
],
],
];
$this
->assertSame($expected, $columnConfiguration);
drupal_flush_all_caches();
$edit = [
'operation' => 'import',
];
$this
->drupalPostForm($executeUrlPath, $edit, $this
->t('Execute'));
$session
->responseContains("Processed 1 item (1 created, 0 updated, 0 failed, 0 ignored) - done with 'csv_source_test'");
$edit = [
'operation' => 'rollback',
];
$this
->drupalPostForm($executeUrlPath, $edit, $this
->t('Execute'));
$edit = [
'edit-vid' => 0,
'edit-name' => 1,
'edit-description' => 2,
];
$this
->drupalPostForm($editUrlPath, $edit, $this
->t('Submit'));
$this
->assertTrue($session
->optionExists('edit-vid', 'vid')
->isSelected());
$this
->assertTrue($session
->optionExists('edit-name', 'name')
->isSelected());
$this
->assertTrue($session
->optionExists('edit-description', 'description')
->isSelected());
$edit = [
'operation' => 'import',
];
drupal_flush_all_caches();
$this
->drupalPostForm($executeUrlPath, $edit, $this
->t('Execute'));
$session
->responseContains("Processed 4 items (4 created, 0 updated, 0 failed, 0 ignored) - done with 'csv_source_test'");
$this
->assertEntity('tags', 'Tags', 'Use tags to group articles');
$this
->assertEntity('forums', 'Sujet de discussion', 'Forum navigation vocabulary');
$this
->assertEntity('test_vocabulary', 'Test Vocabulary', 'This is the vocabulary description');
$this
->assertEntity('genre', 'Genre', 'Genre description');
}
protected function assertEntity($id, $expected_name, $expected_description) {
$entity = Vocabulary::load($id);
$this
->assertTrue($entity instanceof VocabularyInterface);
$this
->assertSame($expected_name, $entity
->label());
$this
->assertSame($expected_description, $entity
->getDescription());
}
}