Commit 8a296bae authored by SylvainM's avatar SylvainM Committed by heddn

Issue #2906264 by SylvainM, Amerie, leewbutler, heddn: Undefined index...

Issue #2906264 by SylvainM, Amerie, leewbutler, heddn: Undefined index notifications when JSON source missing fields
parent 56dfb25a
......@@ -119,7 +119,12 @@ class Json extends DataParserPluginBase implements ContainerFactoryPluginInterfa
$field_data = $current;
$field_selectors = explode('/', trim($selector, '/'));
foreach ($field_selectors as $field_selector) {
$field_data = $field_data[$field_selector];
if (is_array($field_data) && array_key_exists($field_selector, $field_data)) {
$field_data = $field_data[$field_selector];
}
else {
$field_data = '';
}
}
$this->currentItem[$field_name] = $field_data;
}
......
[
{
"id": "1",
"title": "Title",
"video": {
"title": "Video title",
"url": "https://localhost/"
}
},
{
"id": "2",
"video": {
"title": "Video title",
"url": "https://localhost/"
}
},
{
"id": "3",
"title": "Title 3"
}
]
<?php
namespace Drupal\Tests\migrate_plus\Kernel\Plugin\migrate_plus\data_parser;
use Drupal\KernelTests\KernelTestBase;
/**
* Test of the data_parser Json migrate_plus plugin.
*
* @group migrate_plus
*/
class JsonTest extends KernelTestBase {
public static $modules = ['migrate', 'migrate_plus'];
/**
* Tests missing properties in json file.
*
* @param string $file
* File name in tests/data/ directory of this module.
* @param array $ids
* Array of ids to pass to the plugin.
* @param array $fields
* Array of fields to pass to the plugin.
* @param array $expected
* Expected array from json decoded file.
*
* @dataProvider jsonBaseDataProvider
*
* @throws \Drupal\Component\Plugin\Exception\PluginException
* @throws \Exception
*/
public function testMissingProperties($file, array $ids, array $fields, array $expected) {
$path = $this->container
->get('module_handler')
->getModule('migrate_plus')
->getPath();
$url = $path . '/tests/data/' . $file;
/** @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' => 'json',
'destination' => 'node',
'urls' => [$url],
'ids' => $ids,
'fields' => $fields,
'item_selector' => NULL,
];
$json_parser = $plugin_manager->createInstance('json', $conf);
$data = [];
foreach ($json_parser as $item) {
$data[] = $item;
}
$this->assertEquals($expected, $data);
}
/**
* Provides multiple test cases for the testMissingProperty method.
*
* @return array
* The test cases.
*/
public function jsonBaseDataProvider() {
return [
'missing properties' => [
'file' => 'missing_properties.json',
'ids' => ['id' => ['type' => 'integer']],
'fields' => [
[
'name' => 'id',
'label' => 'Id',
'selector' => '/id',
],
[
'name' => 'title',
'label' => 'Title',
'selector' => '/title',
],
[
'name' => 'video_url',
'label' => 'Video url',
'selector' => '/video/url',
],
],
'expected' => [
[
'id' => '1',
'title' => 'Title',
'video_url' => 'https://localhost/',
],
[
'id' => '2',
'title' => '',
'video_url' => 'https://localhost/',
],
[
'id' => '3',
'title' => 'Title 3',
'video_url' => '',
],
],
],
];
}
}
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