SimpleXmlTest.php 5.49 KB
Newer Older
1
2
3
4
5
<?php

namespace Drupal\Tests\migrate_plus\Kernel\Plugin\migrate_plus\data_parser;

use Drupal\KernelTests\KernelTestBase;
heddn's avatar
heddn committed
6
use Drupal\migrate\MigrateException;
7
8
9
10
11
12
13
14

/**
 * Test of the data_parser SimpleXml migrate_plus plugin.
 *
 * @group migrate_plus
 */
class SimpleXmlTest extends KernelTestBase {

15
16
17
18
  /**
   * {@inheritdoc}
   */
  protected static $modules = ['migrate', 'migrate_plus'];
19
20

  /**
21
   * Path for the xml file.
22
   *
23
   * @var string
24
   */
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  protected $path;

  /**
   * The plugin manager.
   *
   * @var \Drupal\migrate_plus\DataParserPluginManager
   */
  protected $pluginManager;

  /**
   * The plugin configuration.
   *
   * @var array
   */
  protected $configuration;

  /**
   * The expected result.
   *
   * @var array
   */
  protected $expected;

  /**
   * {@inheritdoc}
   */
51
  protected function setUp(): void {
52
53
54
55
    parent::setUp();
    $this->path = $this->container->get('module_handler')
      ->getModule('migrate_plus')->getPath();
    $this->pluginManager = $this->container
56
      ->get('plugin.manager.migrate_plus.data_parser');
57
    $this->configuration = [
58
59
60
61
      'plugin' => 'url',
      'data_fetcher_plugin' => 'file',
      'data_parser_plugin' => 'simple_xml',
      'destination' => 'node',
62
      'urls' => [],
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
      'ids' => ['id' => ['type' => 'integer']],
      'fields' => [
        [
          'name' => 'id',
          'label' => 'Id',
          'selector' => '@id',
        ],
        [
          'name' => 'values',
          'label' => 'Values',
          'selector' => 'values',
        ],
      ],
      'item_selector' => '/items/item',
    ];
78
    $this->expected = [
79
80
81
82
83
84
85
86
      [
        'Value 1',
        'Value 2',
      ],
      [
        'Value 1 (single)',
      ],
    ];
87
88
89
90
91
  }

  /**
   * Tests reducing single values.
   */
92
  public function testReduceSingleValue(): void {
93
94
95
96
97
98
99
100
101
102
103
    $url = $this->path . '/tests/data/simple_xml_reduce_single_value.xml';
    $this->configuration['urls'][0] = $url;
    $parser = $this->pluginManager->createInstance('simple_xml', $this->configuration);
    $this->assertResults($this->expected, $parser);
  }

  /**
   * Test reading non standard conforming XML.
   *
   * XML file with lots of different white spaces before the starting tag.
   */
104
  public function testReadNonStandardXmlWhitespace(): void {
105
106
107
108
109
110
    $url = $this->path . '/tests/data/simple_xml_invalid_multi_whitespace.xml';
    $this->configuration['urls'][0] = $url;

    $parser = $this->pluginManager->createInstance('simple_xml', $this->configuration);
    $this->assertResults($this->expected, $parser);
  }
111

112
113
114
115
116
  /**
   * Test reading non standard conforming XML .
   *
   * XML file with one empty line before the starting tag.
   */
117
  public function testReadNonStandardXml2(): void {
118
119
120
121
122
123
124
125
126
127
128
129
    $url = $this->path . '/tests/data/simple_xml_invalid_single_line.xml';
    $this->configuration['urls'][0] = $url;

    $parser = $this->pluginManager->createInstance('simple_xml', $this->configuration);
    $this->assertResults($this->expected, $parser);
  }

  /**
   * Test reading broken XML (missing closing tag).
   *
   * @throws \Drupal\Migrate\MigrateException
   */
130
  public function testReadBrokenXmlMissingTag(): void {
131
132
    $url = $this->path . '/tests/data/simple_xml_broken_missing_tag.xml';
    $this->configuration['urls'][0] = $url;
133
    $this->expectException(MigrateException::class);
134
    $this->expectExceptionMessageMatches('/^Fatal Error 73/');
135
    $parser = $this->pluginManager->createInstance('simple_xml', $this->configuration);
136
    $parser->next();
137
138
139
140
141
142
143
  }

  /**
   * Test reading broken XML (tag mismatch).
   *
   * @throws \Drupal\Migrate\MigrateException
   */
144
  public function testReadBrokenXmlTagMismatch(): void {
145
146
147
    $url = $this->path . '/tests/data/simple_xml_broken_tag_mismatch.xml';
    $this->configuration['urls'][0] = $url;

148
    $this->expectException(MigrateException::class);
149
    $this->expectExceptionMessageMatches('/^Fatal Error 76/');
150

151
    $parser = $this->pluginManager->createInstance('simple_xml', $this->configuration);
152
    $parser->next();
153
154
155
156
157
158
159
  }

  /**
   * Test reading non XML.
   *
   * @throws \Drupal\Migrate\MigrateException
   */
160
  public function testReadNonXml(): void {
161
162
163
    $url = $this->path . '/tests/data/simple_xml_non_xml.xml';
    $this->configuration['urls'][0] = $url;

164
165
    $this->expectException(MigrateException::class);
    $this->expectExceptionMessageRegExp('/^Fatal Error 46/');
166
    $parser = $this->pluginManager->createInstance('simple_xml', $this->configuration);
167
    $parser->next();
168
169
170
171
172
173
174
  }

  /**
   * Tests reading non-existing XML.
   *
   * @throws \Drupal\Migrate\MigrateException
   */
175
  public function testReadNonExistingXml(): void {
176
177
178
    $url = $this->path . '/tests/data/simple_xml_non_existing.xml';
    $this->configuration['urls'][0] = $url;

179
180
    $this->expectException(MigrateException::class);
    $this->expectExceptionMessage('file parser plugin: could not retrieve data from modules/contrib/migrate_plus/tests/data/simple_xml_non_existing.xml');
181
    $parser = $this->pluginManager->createInstance('simple_xml', $this->configuration);
182
    $parser->next();
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
  }

  /**
   * Parses and asserts the results match expectations.
   *
   * @param array|string $expected
   *   The expected results.
   * @param \Traversable $parser
   *   An iterable data result to parse.
   */
  protected function assertResults($expected, \Traversable $parser) {
    $data = [];
    foreach ($parser as $item) {
      $values = [];
      foreach ($item['values'] as $value) {
        $values[] = (string) $value;
      }
      $data[] = $values;
    }
202
203
204
205
    $this->assertEquals($expected, $data);
  }

}