View source
<?php
namespace Drupal\Tests\migrate_source_csv\Unit\Plugin\migrate\source;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate_source_csv\Plugin\migrate\source\CSV;
use Drupal\Tests\UnitTestCase;
use org\bovigo\vfs\vfsStream;
class CSVUnitTest extends UnitTestCase {
protected $pluginId;
protected $pluginDefinition;
protected $migration;
protected $standardCharsPath;
protected $nonStandardCharsPath;
protected function setUp() : void {
parent::setUp();
$this->pluginId = 'test csv migration';
$this->pluginDefinition = [];
$migration = $this
->prophesize(MigrationInterface::class);
$migration
->getIdMap()
->willReturn(NULL);
$this->migration = $migration
->reveal();
$standard_chars = <<<'EOD'
id,first_name,last_name,email,country,ip_address
1,Justin,Dean,jdean0@example.com,Indonesia,60.242.130.40
2,Joan,Jordan,jjordan1@example.com,Thailand,137.230.209.171
EOD;
$non_standard_chars = <<<'EOD'
1|%Justin%|Dean|jdean0@example.com|Indonesia|60.242.130.40
2|Joan|Jordan|jjordan1@example.com|Thailand|137.230.209.171
EOD;
$root_dir = vfsStream::setup('root');
$this->standardCharsPath = vfsStream::newFile('data.csv')
->at($root_dir)
->withContent($standard_chars)
->url();
$this->nonStandardCharsPath = vfsStream::newFile('data_edge_case.csv')
->at($root_dir)
->withContent($non_standard_chars)
->url();
}
public function testCreate() : void {
$configuration = [
'path' => $this->standardCharsPath,
'ids' => [
'id',
],
];
$csv = new CSV($configuration, $this->pluginId, $this->pluginDefinition, $this->migration);
$this
->assertInstanceOf(CSV::class, $csv);
}
public function testMigrateExceptionPathMissing() : void {
$this
->expectException(\InvalidArgumentException::class);
$this
->expectExceptionMessage('You must declare the "path" to the source CSV file in your source settings.');
new CSV([], $this->pluginId, $this->pluginDefinition, $this->migration);
}
public function testMigrateExceptionKeysMissing() : void {
$configuration = [
'path' => $this->standardCharsPath,
];
$this
->expectException(\InvalidArgumentException::class);
$this
->expectExceptionMessage('You must declare "ids" as a unique array of fields in your source settings.');
new CSV($configuration, $this->pluginId, $this->pluginDefinition, $this->migration);
}
public function testToString() : void {
$configuration = [
'path' => $this->standardCharsPath,
'ids' => [
'id',
],
];
$csv = new CSV($configuration, $this->pluginId, $this->pluginDefinition, $this->migration);
$this
->assertEquals($configuration['path'], (string) $csv);
}
public function testInitializeIterator(array $configuration, array $expected) : void {
$file_path = $this->standardCharsPath;
if (isset($configuration['path']) && $configuration['path'] === 'non standard') {
$file_path = $this->nonStandardCharsPath;
}
$configuration['path'] = $file_path;
$csv = new CSV($configuration, $this->pluginId, $this->pluginDefinition, $this->migration);
$iterator = $csv
->initializeIterator();
$this
->assertEquals(count($expected), iterator_count($iterator));
$iterator = $csv
->initializeIterator();
foreach ($expected as $record) {
$this
->assertSame($record, $iterator
->current());
$iterator
->next();
}
}
public function iteratorDataProvider() : array {
$data['non standard'] = [
'configuration' => [
'ids' => [
'ids',
],
'path' => 'non standard',
'header_offset' => NULL,
'delimiter' => '|',
'enclosure' => '%',
'escape' => '`',
],
'expected rows' => [
[
'1',
'Justin',
'Dean',
'jdean0@example.com',
'Indonesia',
'60.242.130.40',
],
[
'2',
'Joan',
'Jordan',
'jjordan1@example.com',
'Thailand',
'137.230.209.171',
],
],
];
$data['standard'] = [
'configuration' => [
'ids' => [
'ids',
],
],
'expected rows' => [
[
'id' => '1',
'first_name' => 'Justin',
'last_name' => 'Dean',
'email' => 'jdean0@example.com',
'country' => 'Indonesia',
'ip_address' => '60.242.130.40',
],
[
'id' => '2',
'first_name' => 'Joan',
'last_name' => 'Jordan',
'email' => 'jjordan1@example.com',
'country' => 'Thailand',
'ip_address' => '137.230.209.171',
],
],
];
$data['with defined fields'] = [
'configuration' => [
'ids' => [
'ids',
],
'fields' => [
[
'name' => 'id',
],
[
'name' => 'first_name',
'label' => 'First Name',
],
],
],
'expected rows' => [
[
'id' => '1',
'first_name' => 'Justin',
],
[
'id' => '2',
'first_name' => 'Joan',
],
],
];
$data['default record number field name'] = [
'configuration' => [
'ids' => [
'id',
],
'create_record_number' => TRUE,
],
'expected rows' => [
[
'id' => '1',
'first_name' => 'Justin',
'last_name' => 'Dean',
'email' => 'jdean0@example.com',
'country' => 'Indonesia',
'ip_address' => '60.242.130.40',
'record_number' => 1,
],
[
'id' => '2',
'first_name' => 'Joan',
'last_name' => 'Jordan',
'email' => 'jjordan1@example.com',
'country' => 'Thailand',
'ip_address' => '137.230.209.171',
'record_number' => 2,
],
],
];
$data['custom record number field name'] = [
'configuration' => [
'ids' => [
'MyRowNumber',
],
'create_record_number' => TRUE,
'record_number_field' => 'MyRowNumber',
'header_offset' => 1,
],
'expected rows' => [
[
'1' => 'id',
'Justin' => 'first_name',
'Dean' => 'last_name',
'jdean0@example.com' => 'email',
'Indonesia' => 'country',
'60.242.130.40' => 'ip_address',
'MyRowNumber' => 2,
],
[
'1' => '2',
'Justin' => 'Joan',
'Dean' => 'Jordan',
'jdean0@example.com' => 'jjordan1@example.com',
'Indonesia' => 'Thailand',
'60.242.130.40' => '137.230.209.171',
'MyRowNumber' => 3,
],
],
];
return $data;
}
public function testGetIds(array $configuration, array $expected) : void {
$csv = new CSV($configuration + [
'path' => $this->standardCharsPath,
], $this->pluginId, $this->pluginDefinition, $this->migration);
$this
->assertSame($expected, $csv
->getIds());
}
public function idsDataProvider() : array {
$data['ids'] = [
'configuration' => [
'ids' => [
'id',
'paragraph',
],
],
'expected' => [
'id' => [
'type' => 'string',
],
'paragraph' => [
'type' => 'string',
],
],
];
return $data;
}
public function testFields(array $configuration, array $expected) : void {
$csv = new CSV($configuration + [
'path' => $this->standardCharsPath,
], $this->pluginId, $this->pluginDefinition, $this->migration);
$this
->assertSame($expected, $csv
->fields());
}
public function fieldsDataProvider() : array {
$data['no fields'] = [
'configuration' => [
'ids' => [
'id',
],
],
'expected' => [
'id' => 'id',
'first_name' => 'first_name',
'last_name' => 'last_name',
'email' => 'email',
'country' => 'country',
'ip_address' => 'ip_address',
],
];
$data['with fields override'] = [
'configuration' => [
'ids' => [
'id',
],
'fields' => [
[
'name' => 'id',
],
[
'name' => 'first_name',
'label' => 'First Name',
],
],
],
'expected' => [
'id' => 'id',
'first_name' => 'First Name',
],
];
return $data;
}
public function testMalformedFilePath() : void {
$configuration = [
'path' => 'non-existent-path',
'ids' => [
'id',
],
];
$csv = new CSV($configuration, $this->pluginId, $this->pluginDefinition, $this->migration);
$this
->expectException(\RuntimeException::class);
$this
->expectExceptionMessage('File "non-existent-path" was not found.');
$csv
->initializeIterator();
}
}