Skip to content
Snippets Groups Projects
Commit 5b488d04 authored by Mike Decker's avatar Mike Decker Committed by Ivan Doroshenko
Browse files

Issue #3050058 by pookmish, Matroskeen, chrisfromredfin, jigarius: Failure...

Issue #3050058 by pookmish, Matroskeen, chrisfromredfin, jigarius: Failure with Json parser when item_selector not present
parent 88c4f71d
No related branches found
No related tags found
1 merge request!59#3050058 Json parser fails if item_selector not present
......@@ -51,7 +51,7 @@ class Json extends DataParserPluginBase implements ContainerFactoryPluginInterfa
// Otherwise, we're using xpath-like selectors.
$selectors = explode('/', trim((string) $this->itemSelector, '/'));
foreach ($selectors as $selector) {
if (!empty($selector) || $selector === '0') {
if ((!empty($selector) || $selector === '0') && array_key_exists($selector, $source_data)) {
$source_data = $source_data[$selector];
}
}
......
{
"status": "ok",
"data": [
{
"id": 1,
"title": "1 item"
},
{
"id": 2,
"title": "2 item"
}
],
"data_empty": [],
"data_false": false
}
......@@ -5,6 +5,7 @@ declare(strict_types = 1);
namespace Drupal\Tests\migrate_plus\Kernel\Plugin\migrate_plus\data_parser;
use Drupal\KernelTests\KernelTestBase;
use Drupal\migrate_plus\DataParserPluginManager;
/**
* Test of the data_parser Json migrate_plus plugin.
......@@ -18,6 +19,29 @@ final class JsonTest extends KernelTestBase {
*/
protected static $modules = ['migrate', 'migrate_plus'];
/**
* Path for the module.
*/
protected ?string $path;
/**
* The plugin manager.
*/
protected ?DataParserPluginManager $pluginManager = NULL;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->path = $this->container->get('module_handler')
->getModule('migrate_plus')->getPath();
$this->pluginManager = $this->container
->get('plugin.manager.migrate_plus.data_parser');
}
/**
* Tests missing properties in json file.
*
......@@ -30,21 +54,14 @@ final class JsonTest extends KernelTestBase {
* @param array $expected
* Expected array from json decoded file.
*
* @dataProvider jsonBaseDataProvider
* @dataProvider providerTestMissingProperties
*
* @throws \Drupal\Component\Plugin\Exception\PluginException
* @throws \Exception
*/
public function testMissingProperties($file, array $ids, array $fields, array $expected): void {
$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');
$url = $this->path . '/tests/data/' . $file;
$conf = [
'plugin' => 'url',
'data_fetcher_plugin' => 'file',
......@@ -55,7 +72,7 @@ final class JsonTest extends KernelTestBase {
'fields' => $fields,
'item_selector' => NULL,
];
$json_parser = $plugin_manager->createInstance('json', $conf);
$json_parser = $this->pluginManager->createInstance('json', $conf);
$data = [];
foreach ($json_parser as $item) {
......@@ -71,7 +88,7 @@ final class JsonTest extends KernelTestBase {
* @return array
* The test cases.
*/
public function jsonBaseDataProvider(): array {
public function providerTestMissingProperties(): array {
return [
'missing properties' => [
'file' => 'missing_properties.json',
......@@ -114,4 +131,89 @@ final class JsonTest extends KernelTestBase {
];
}
/**
* Tests item_selector parser property.
*
* @dataProvider providerItemSelector
*/
public function testItemSelector($item_selector, $fields, $expected): void {
$url = $this->path . '/tests/data/item_selector.json';
$conf = [
'plugin' => 'url',
'data_fetcher_plugin' => 'file',
'data_parser_plugin' => 'json',
'destination' => 'node',
'urls' => [$url],
'ids' => ['id' => ['type' => 'integer']],
'item_selector' => $item_selector,
'fields' => $fields,
];
$json_parser = $this->pluginManager->createInstance('json', $conf);
$data = [];
foreach ($json_parser as $item) {
$data[] = $item;
}
$this->assertEquals($expected, $data);
}
/**
* Provides multiple test cases for the testItemSelector method.
*
* @return array
* The test cases.
*/
public function providerItemSelector(): array {
$fields = [
[
'name' => 'id',
'label' => 'Id',
'selector' => '/id',
],
[
'name' => 'title',
'label' => 'Title',
'selector' => '/title',
],
];
return [
'item_selector 1st level' => [
'item_selector' => '/data',
'fields' => $fields,
'expected' => [
[
'id' => '1',
'title' => '1 item',
],
[
'id' => '2',
'title' => '2 item',
],
],
],
'item_selector is available, data is empty' => [
'item_selector' => '/data_empty',
'fields' => $fields,
'expected' => [],
],
'item_selector not available' => [
'item_selector' => '/data_unavailable',
'fields' => $fields,
'expected' => [
[
'id' => '',
'title' => '',
],
[
'id' => '',
'title' => '',
],
],
],
];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment