DomMigrationLookupTest.php 6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php

namespace Drupal\Tests\migrate_plus\Unit\process;

use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Utility\Html;
use Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\Plugin\MigratePluginManager;
use Drupal\migrate\Plugin\MigrateProcessInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate_plus\Plugin\migrate\process\DomMigrationLookup;

/**
 * Tests the dom_migration_lookup process plugin.
 *
 * @group migrate
 * @coversDefaultClass \Drupal\migrate_plus\Plugin\migrate\process\DomMigrationLookup
 */
class DomMigrationLookupTest extends MigrateProcessTestCase {

  /**
   * Example configuration for the dom_migration_lookup process plugin.
   *
   * @var array
   */
  protected $exampleConfiguration = [
    'plugin' => 'dom_migration_lookup',
    'mode' => 'attribute',
    'xpath' => '//a',
    'attribute_options' => [
      'name' => 'href',
    ],
    'search' => '@/user/(\d+)@',
    'replace' => '/user/[mapped-id]',
    'migrations' => [
      'users' => [],
      'people' => [
        'replace' => '/people/[mapped-id]',
      ],
    ],
  ];

  /**
   * Mock a migration.
   *
   * @var \Drupal\migrate\Plugin\MigrationInterface
   */
  protected $migration;

  /**
   * Mock a process plugin manager.
   *
   * @var \Drupal\migrate\Plugin\MigratePluginManagerInterface
   */
  protected $processPluginManager;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    // Mock a  migration.
    $prophecy = $this->prophesize(MigrationInterface::class);
    $this->migration = $prophecy->reveal();

    // Mock two migration lookup plugins.
    $prophecy = $this->prophesize(MigrateProcessInterface::class);
    $prophecy
      ->transform('123', $this->migrateExecutable, $this->row, 'destinationproperty')
      ->willReturn('321');
    $prophecy
      ->transform('456', $this->migrateExecutable, $this->row, 'destinationproperty')
      ->willReturn(NULL);
    $users_lookup_plugin = $prophecy->reveal();
    $prophecy = $this->prophesize(MigrateProcessInterface::class);
    $prophecy
      ->transform('123', $this->migrateExecutable, $this->row, 'destinationproperty')
      ->willReturn('ignored');
    $prophecy
      ->transform('456', $this->migrateExecutable, $this->row, 'destinationproperty')
      ->willReturn('654');
    $people_lookup_plugin = $prophecy->reveal();

    // Mock a process plugin manager.
    $prophecy = $this->prophesize(MigratePluginManager::class);
    $users_configuration = [
      'migration' => 'users',
      'no_stub' => TRUE,
    ];
    $people_configuration = [
      'migration' => 'people',
      'no_stub' => TRUE,
    ];
    $prophecy
      ->createInstance('migration_lookup', $users_configuration, $this->migration)
      ->willReturn($users_lookup_plugin);
    $prophecy
      ->createInstance('migration_lookup', $people_configuration, $this->migration)
      ->willReturn($people_lookup_plugin);
    $this->processPluginManager = $prophecy->reveal();
  }

  /**
   * @covers ::__construct
   *
   * @dataProvider providerTestConfigValidation
   */
  public function testConfigValidation(array $config_overrides, $message) {
    $configuration = $config_overrides + $this->exampleConfiguration;
    $value = '<p>A simple paragraph.</p>';
    $this->setExpectedException(InvalidPluginDefinitionException::class, $message);
    (new DomMigrationLookup($configuration, 'dom_migration_lookup', [], $this->migration, $this->processPluginManager))
      ->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
  }

  /**
   * Dataprovider for testConfigValidation().
   */
  public function providerTestConfigValidation() {
    $cases = [
      'migrations-empty' => [
        ['migrations' => []],
heddn's avatar
heddn committed
125
        "Configuration option 'migration' is required.",
126
127
128
      ],
      'migrations-invalid' => [
        ['migrations' => 42],
heddn's avatar
heddn committed
129
        "Configuration option 'migration' should be a keyed array.",
130
131
132
      ],
      'replace-null' => [
        ['replace' => NULL],
heddn's avatar
heddn committed
133
        "Please define either a global replace for all migrations, or a specific one for 'migrations.users'.",
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
      ],
    ];

    return $cases;
  }

  /**
   * @covers ::transform
   */
  public function testTransformInvalidInput() {
    $value = 'string';
    $this->setExpectedException(MigrateSkipRowException::class, 'The dom_migration_lookup plugin in the destinationproperty process pipeline requires a \DOMDocument object. You can use the dom plugin to convert a string to \DOMDocument.');
    (new DomMigrationLookup($this->exampleConfiguration, 'dom_migration_lookup', [], $this->migration, $this->processPluginManager))
      ->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
  }

  /**
   * @covers ::transform
   *
   * @dataProvider providerTestTransform
   */
  public function testTransform($config_overrides, $input_string, $output_string) {
    $configuration = $config_overrides + $this->exampleConfiguration;
    $value = Html::load($input_string);
    $document = (new DomMigrationLookup($configuration, 'dom_migration_lookup', [], $this->migration, $this->processPluginManager))
      ->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
    $this->assertTrue($document instanceof \DOMDocument);
    $this->assertEquals($output_string, Html::serialize($document));
  }

  /**
   * Dataprovider for testTransform().
   */
  public function providerTestTransform() {
    $cases = [
      'users-migration' => [
        [],
        '<a href="/user/123">text</a>',
        '<a href="/user/321">text</a>',
      ],
      'people-migration' => [
        [],
        '<a href="https://www.example.com/user/456">text</a>',
        '<a href="https://www.example.com/people/654">text</a>',
      ],
      'no-match' => [
        ['search' => '@www\.mysite\.com/user/(\d+)@'],
        '<a href="https://www.example.com/user/456">text</a>',
        '<a href="https://www.example.com/user/456">text</a>',
      ],
    ];

    return $cases;
  }

}