Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / migrate_plus / tests / src / Kernel / Plugin / migrate_plus / data_parser / JsonTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate_plus\Kernel\Plugin\migrate_plus\data_parser;
4
5 use Drupal\KernelTests\KernelTestBase;
6
7 /**
8  * Test of the data_parser Json migrate_plus plugin.
9  *
10  * @group migrate_plus
11  */
12 class JsonTest extends KernelTestBase {
13
14   public static $modules = ['migrate', 'migrate_plus'];
15
16   /**
17    * Tests missing properties in json file.
18    *
19    * @param string $file
20    *   File name in tests/data/ directory of this module.
21    * @param array $ids
22    *   Array of ids to pass to the plugin.
23    * @param array $fields
24    *   Array of fields to pass to the plugin.
25    * @param array $expected
26    *   Expected array from json decoded file.
27    *
28    * @dataProvider jsonBaseDataProvider
29    *
30    * @throws \Drupal\Component\Plugin\Exception\PluginException
31    * @throws \Exception
32    */
33   public function testMissingProperties($file, array $ids, array $fields, array $expected) {
34     $path = $this->container
35       ->get('module_handler')
36       ->getModule('migrate_plus')
37       ->getPath();
38     $url = $path . '/tests/data/' . $file;
39
40     /** @var \Drupal\migrate_plus\DataParserPluginManager $plugin_manager */
41     $plugin_manager = $this->container
42       ->get('plugin.manager.migrate_plus.data_parser');
43     $conf = [
44       'plugin' => 'url',
45       'data_fetcher_plugin' => 'file',
46       'data_parser_plugin' => 'json',
47       'destination' => 'node',
48       'urls' => [$url],
49       'ids' => $ids,
50       'fields' => $fields,
51       'item_selector' => NULL,
52     ];
53     $json_parser = $plugin_manager->createInstance('json', $conf);
54
55     $data = [];
56     foreach ($json_parser as $item) {
57       $data[] = $item;
58     }
59
60     $this->assertEquals($expected, $data);
61   }
62
63   /**
64    * Provides multiple test cases for the testMissingProperty method.
65    *
66    * @return array
67    *   The test cases.
68    */
69   public function jsonBaseDataProvider() {
70     return [
71       'missing properties' => [
72         'file' => 'missing_properties.json',
73         'ids' => ['id' => ['type' => 'integer']],
74         'fields' => [
75           [
76             'name' => 'id',
77             'label' => 'Id',
78             'selector' => '/id',
79           ],
80           [
81             'name' => 'title',
82             'label' => 'Title',
83             'selector' => '/title',
84           ],
85           [
86             'name' => 'video_url',
87             'label' => 'Video url',
88             'selector' => '/video/url',
89           ],
90         ],
91         'expected' => [
92           [
93             'id' => '1',
94             'title' => 'Title',
95             'video_url' => 'https://localhost/',
96           ],
97           [
98             'id' => '2',
99             'title' => '',
100             'video_url' => 'https://localhost/',
101           ],
102           [
103             'id' => '3',
104             'title' => 'Title 3',
105             'video_url' => '',
106           ],
107         ],
108       ],
109     ];
110   }
111
112 }