Commit d24565d6 authored by SylvainM's avatar SylvainM Committed by heddn

Issue #2976091 by SylvainM, pookmish, heddn: Simple Xml data_parser reduce...

Issue #2976091 by SylvainM, pookmish, heddn: Simple Xml data_parser reduce single-value to parent's attribute
parent c7cb7068
...@@ -77,7 +77,7 @@ class SimpleXml extends DataParserPluginBase { ...@@ -77,7 +77,7 @@ class SimpleXml extends DataParserPluginBase {
} }
// Reduce single-value results to scalars. // Reduce single-value results to scalars.
foreach ($this->currentItem as $field_name => $values) { foreach ($this->currentItem as $field_name => $values) {
if (count($values) == 1) { if (is_array($values) && count($values) == 1) {
$this->currentItem[$field_name] = reset($values); $this->currentItem[$field_name] = reset($values);
} }
} }
......
<?xml version="1.0" encoding="utf-8"?>
<items>
<item id="1">
<values title="Values">
<value>Value 1</value>
<value>Value 2</value>
</values>
</item>
<item id="2">
<values title="Values">
<value>Value 1 (single)</value>
</values>
</item>
</items>
<?php
namespace Drupal\Tests\migrate_plus\Kernel\Plugin\migrate_plus\data_parser;
use Drupal\KernelTests\KernelTestBase;
/**
* Test of the data_parser SimpleXml migrate_plus plugin.
*
* @group migrate_plus
*/
class SimpleXmlTest extends KernelTestBase {
public static $modules = ['migrate', 'migrate_plus'];
/**
* Tests reducing single values.
*
* @throws \Drupal\Component\Plugin\Exception\PluginException
* @throws \Exception
*/
public function testReduceSingleValue() {
$path = $this->container
->get('module_handler')
->getModule('migrate_plus')
->getPath();
$url = $path . '/tests/data/simple_xml_reduce_single_value.xml';
/** @var \Drupal\migrate_plus\DataParserPluginManager $plugin_manager */
$plugin_manager = $this->container
->get('plugin.manager.migrate_plus.data_parser');
$conf = [
'plugin' => 'url',
'data_fetcher_plugin' => 'file',
'data_parser_plugin' => 'simple_xml',
'destination' => 'node',
'urls' => [$url],
'ids' => ['id' => ['type' => 'integer']],
'fields' => [
[
'name' => 'id',
'label' => 'Id',
'selector' => '@id',
],
[
'name' => 'values',
'label' => 'Values',
'selector' => 'values',
],
],
'item_selector' => '/items/item',
];
$parser = $plugin_manager->createInstance('simple_xml', $conf);
$data = [];
foreach ($parser as $item) {
$values = [];
foreach ($item['values'] as $value) {
$values[] = (string) $value;
}
$data[] = $values;
}
$expected = [
[
'Value 1',
'Value 2',
],
[
'Value 1 (single)',
],
];
$this->assertEquals($expected, $data);
}
}
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