Commit 2ab6db19 authored by heddn's avatar heddn Committed by heddn

Issue #2940118 by quietone, heddn: Using fields() method causes fatal

parent 54420861
......@@ -87,7 +87,13 @@ class CSV extends SourcePluginBase implements ConfigurablePluginInterface {
public function initializeIterator() {
// File handler using header-rows-respecting extension of SPLFileObject.
$this->file = new $this->fileClass($this->getConfiguration()['path']);
return $this->setupFile();
}
/**
* @return \SplFileObject
*/
protected function setupFile() {
// Set basics of CSV behavior based on configuration.
$delimiter = $this->getConfiguration()['delimiter'];
$enclosure = $this->getConfiguration()['enclosure'];
......@@ -140,7 +146,10 @@ class CSV extends SourcePluginBase implements ConfigurablePluginInterface {
*/
public function fields() {
$fields = [];
foreach ($this->getIterator()->getColumnNames() as $column) {
if (!$this->file) {
$this->initializeIterator();
}
foreach ($this->file->getColumnNames() as $column) {
$fields[key($column)] = reset($column);
}
......
type: module
name: Source plugin using yield
description: 'Provides a source plugin using yield'
package: Migration
core: 8.x
dependencies:
- drupal:migrate (>=8.1)
<?php
namespace Drupal\source_plugin_yield_test\Plugin\migrate\source;
use Drupal\migrate_source_csv\Plugin\migrate\source\CSV;
/**
* Yields each image and sku.
*
* @MigrateSource(
* id = "yield_rows"
* )
*/
class YieldRows extends CSV {
/**
* {@inheritdoc}
*/
public function initializeIterator() {
$file = parent::initializeIterator();
return $this->getYield($file);
}
/**
* Prepare a test row using yield.
*
* @param \SplFileObject $file
* The source CSV file object.
*
* @codingStandardsIgnoreStart
*
* @return \Generator
* A new row with only the id value.
*
* @codingStandardsIgnoreEnd
*/
public function getYield(\SplFileObject $file) {
foreach ($file as $row_num => $row) {
$new_row = [];
$new_row['id'] = $row['id'];
yield($new_row);
}
}
}
<?php
namespace Drupal\Tests\migrate_source_csv\Unit\Plugin\migrate\source;
use Drupal\source_plugin_yield_test\Plugin\migrate\source\YieldRows;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\Tests\migrate_source_csv\Unit\CSVUnitBase;
/**
* @coversDefaultClass \Drupal\migrate_source_csv\Plugin\migrate\source\CSV
*
* @group migrate_source_csv
*/
class CSVSourceYieldTest extends CSVUnitBase {
/**
* The plugin id.
*
* @var string
*/
protected $pluginId;
/**
* The plugin definition.
*
* @var array
*/
protected $pluginDefinition;
/**
* The migration plugin.
*
* @var \Drupal\migrate\Plugin\MigrationInterface
*/
protected $migration;
/**
* {@inheritdoc}
*/
public static $modules = [
'migrate',
'migrate_source_csv',
'source_plugin_yield_test',
];
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->pluginId = 'test csv migration';
$this->pluginDefinition = [];
$migration = $this->prophesize(MigrationInterface::class);
$migration->getIdMap()
->willReturn(NULL);
$this->migration = $migration->reveal();
}
/**
* Test fields method with a source plugin using yield.
*
* @throws \Drupal\migrate\MigrateException
*/
public function testFields() {
$configuration = [
'path' => $this->happyPath,
'keys' => ['id'],
'header_row_count' => 1,
];
$plugin = new YieldRows($configuration, $this->pluginId, $this->pluginDefinition, $this->migration);
$expected_fields = [
'id' => 'id',
'first_name' => 'first_name',
'last_name' => 'last_name',
'email' => 'email',
'country' => 'country',
'ip_address' => 'ip_address',
];
$fields = $plugin->fields();
$this->assertArrayEquals($expected_fields, $fields);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment