annotate core/modules/migrate/tests/src/Unit/MigrateExecutableTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\migrate\Unit;
Chris@0 4
Chris@0 5 use Drupal\Component\Utility\Html;
Chris@0 6 use Drupal\migrate\Plugin\MigrateProcessInterface;
Chris@0 7 use Drupal\migrate\Plugin\MigrationInterface;
Chris@0 8 use Drupal\migrate\Plugin\MigrateIdMapInterface;
Chris@0 9 use Drupal\migrate\MigrateException;
Chris@0 10 use Drupal\migrate\Row;
Chris@0 11
Chris@0 12 /**
Chris@0 13 * @coversDefaultClass \Drupal\migrate\MigrateExecutable
Chris@0 14 * @group migrate
Chris@0 15 */
Chris@0 16 class MigrateExecutableTest extends MigrateTestCase {
Chris@0 17
Chris@0 18 /**
Chris@0 19 * The mocked migration entity.
Chris@0 20 *
Chris@0 21 * @var \Drupal\migrate\Plugin\MigrationInterface|\PHPUnit_Framework_MockObject_MockObject
Chris@0 22 */
Chris@0 23 protected $migration;
Chris@0 24
Chris@0 25 /**
Chris@0 26 * The mocked migrate message.
Chris@0 27 *
Chris@0 28 * @var \Drupal\migrate\MigrateMessageInterface|\PHPUnit_Framework_MockObject_MockObject
Chris@0 29 */
Chris@0 30 protected $message;
Chris@0 31
Chris@0 32 /**
Chris@0 33 * The tested migrate executable.
Chris@0 34 *
Chris@0 35 * @var \Drupal\Tests\migrate\Unit\TestMigrateExecutable
Chris@0 36 */
Chris@0 37 protected $executable;
Chris@0 38
Chris@0 39 /**
Chris@0 40 * The migration's configuration values.
Chris@0 41 *
Chris@0 42 * @var array
Chris@0 43 */
Chris@0 44 protected $migrationConfiguration = [
Chris@0 45 'id' => 'test',
Chris@0 46 ];
Chris@0 47
Chris@0 48 /**
Chris@0 49 * {@inheritdoc}
Chris@0 50 */
Chris@0 51 protected function setUp() {
Chris@0 52 parent::setUp();
Chris@0 53 $this->migration = $this->getMigration();
Chris@0 54 $this->message = $this->getMock('Drupal\migrate\MigrateMessageInterface');
Chris@0 55 $event_dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
Chris@0 56 $this->executable = new TestMigrateExecutable($this->migration, $this->message, $event_dispatcher);
Chris@0 57 $this->executable->setStringTranslation($this->getStringTranslationStub());
Chris@0 58 }
Chris@0 59
Chris@0 60 /**
Chris@0 61 * Tests an import with an incomplete rewinding.
Chris@0 62 */
Chris@0 63 public function testImportWithFailingRewind() {
Chris@0 64 $exception_message = $this->getRandomGenerator()->string();
Chris@0 65 $source = $this->getMock('Drupal\migrate\Plugin\MigrateSourceInterface');
Chris@0 66 $source->expects($this->once())
Chris@0 67 ->method('rewind')
Chris@0 68 ->will($this->throwException(new \Exception($exception_message)));
Chris@0 69
Chris@0 70 $this->migration->expects($this->any())
Chris@0 71 ->method('getSourcePlugin')
Chris@0 72 ->will($this->returnValue($source));
Chris@0 73
Chris@0 74 // Ensure that a message with the proper message was added.
Chris@0 75 $this->message->expects($this->once())
Chris@0 76 ->method('display')
Chris@0 77 ->with("Migration failed with source plugin exception: " . Html::escape($exception_message));
Chris@0 78
Chris@0 79 $result = $this->executable->import();
Chris@0 80 $this->assertEquals(MigrationInterface::RESULT_FAILED, $result);
Chris@0 81 }
Chris@0 82
Chris@0 83 /**
Chris@0 84 * Tests the import method with a valid row.
Chris@0 85 */
Chris@0 86 public function testImportWithValidRow() {
Chris@0 87 $source = $this->getMockSource();
Chris@0 88
Chris@0 89 $row = $this->getMockBuilder('Drupal\migrate\Row')
Chris@0 90 ->disableOriginalConstructor()
Chris@0 91 ->getMock();
Chris@0 92
Chris@0 93 $row->expects($this->once())
Chris@0 94 ->method('getSourceIdValues')
Chris@0 95 ->will($this->returnValue(['id' => 'test']));
Chris@0 96
Chris@0 97 $this->idMap->expects($this->once())
Chris@17 98 ->method('lookupDestinationIds')
Chris@0 99 ->with(['id' => 'test'])
Chris@17 100 ->will($this->returnValue([['test']]));
Chris@0 101
Chris@0 102 $source->expects($this->once())
Chris@0 103 ->method('current')
Chris@0 104 ->will($this->returnValue($row));
Chris@0 105
Chris@0 106 $this->executable->setSource($source);
Chris@0 107
Chris@0 108 $this->migration->expects($this->once())
Chris@0 109 ->method('getProcessPlugins')
Chris@0 110 ->will($this->returnValue([]));
Chris@0 111
Chris@0 112 $destination = $this->getMock('Drupal\migrate\Plugin\MigrateDestinationInterface');
Chris@0 113 $destination->expects($this->once())
Chris@0 114 ->method('import')
Chris@0 115 ->with($row, ['test'])
Chris@0 116 ->will($this->returnValue(['id' => 'test']));
Chris@0 117
Chris@0 118 $this->migration
Chris@0 119 ->method('getDestinationPlugin')
Chris@0 120 ->willReturn($destination);
Chris@0 121
Chris@0 122 $this->assertSame(MigrationInterface::RESULT_COMPLETED, $this->executable->import());
Chris@0 123 }
Chris@0 124
Chris@0 125 /**
Chris@0 126 * Tests the import method with a valid row.
Chris@0 127 */
Chris@0 128 public function testImportWithValidRowWithoutDestinationId() {
Chris@0 129 $source = $this->getMockSource();
Chris@0 130
Chris@0 131 $row = $this->getMockBuilder('Drupal\migrate\Row')
Chris@0 132 ->disableOriginalConstructor()
Chris@0 133 ->getMock();
Chris@0 134
Chris@0 135 $row->expects($this->once())
Chris@0 136 ->method('getSourceIdValues')
Chris@0 137 ->will($this->returnValue(['id' => 'test']));
Chris@0 138
Chris@0 139 $this->idMap->expects($this->once())
Chris@17 140 ->method('lookupDestinationIds')
Chris@0 141 ->with(['id' => 'test'])
Chris@17 142 ->will($this->returnValue([['test']]));
Chris@0 143
Chris@0 144 $source->expects($this->once())
Chris@0 145 ->method('current')
Chris@0 146 ->will($this->returnValue($row));
Chris@0 147
Chris@0 148 $this->executable->setSource($source);
Chris@0 149
Chris@0 150 $this->migration->expects($this->once())
Chris@0 151 ->method('getProcessPlugins')
Chris@0 152 ->will($this->returnValue([]));
Chris@0 153
Chris@0 154 $destination = $this->getMock('Drupal\migrate\Plugin\MigrateDestinationInterface');
Chris@0 155 $destination->expects($this->once())
Chris@0 156 ->method('import')
Chris@0 157 ->with($row, ['test'])
Chris@0 158 ->will($this->returnValue(TRUE));
Chris@0 159
Chris@0 160 $this->migration
Chris@0 161 ->method('getDestinationPlugin')
Chris@0 162 ->willReturn($destination);
Chris@0 163
Chris@0 164 $this->idMap->expects($this->never())
Chris@0 165 ->method('saveIdMapping');
Chris@0 166
Chris@0 167 $this->assertSame(MigrationInterface::RESULT_COMPLETED, $this->executable->import());
Chris@0 168 }
Chris@0 169
Chris@0 170 /**
Chris@0 171 * Tests the import method with a valid row.
Chris@0 172 */
Chris@0 173 public function testImportWithValidRowNoDestinationValues() {
Chris@0 174 $source = $this->getMockSource();
Chris@0 175
Chris@0 176 $row = $this->getMockBuilder('Drupal\migrate\Row')
Chris@0 177 ->disableOriginalConstructor()
Chris@0 178 ->getMock();
Chris@0 179
Chris@0 180 $row->expects($this->once())
Chris@0 181 ->method('getSourceIdValues')
Chris@0 182 ->will($this->returnValue(['id' => 'test']));
Chris@0 183
Chris@0 184 $source->expects($this->once())
Chris@0 185 ->method('current')
Chris@0 186 ->will($this->returnValue($row));
Chris@0 187
Chris@0 188 $this->executable->setSource($source);
Chris@0 189
Chris@0 190 $this->migration->expects($this->once())
Chris@0 191 ->method('getProcessPlugins')
Chris@0 192 ->will($this->returnValue([]));
Chris@0 193
Chris@0 194 $destination = $this->getMock('Drupal\migrate\Plugin\MigrateDestinationInterface');
Chris@0 195 $destination->expects($this->once())
Chris@0 196 ->method('import')
Chris@0 197 ->with($row, ['test'])
Chris@0 198 ->will($this->returnValue([]));
Chris@0 199
Chris@0 200 $this->migration
Chris@0 201 ->method('getDestinationPlugin')
Chris@0 202 ->willReturn($destination);
Chris@0 203
Chris@0 204 $this->idMap->expects($this->once())
Chris@0 205 ->method('saveIdMapping')
Chris@0 206 ->with($row, [], MigrateIdMapInterface::STATUS_FAILED, NULL);
Chris@0 207
Chris@0 208 $this->idMap->expects($this->once())
Chris@0 209 ->method('messageCount')
Chris@0 210 ->will($this->returnValue(0));
Chris@0 211
Chris@0 212 $this->idMap->expects($this->once())
Chris@0 213 ->method('saveMessage');
Chris@0 214
Chris@0 215 $this->idMap->expects($this->once())
Chris@17 216 ->method('lookupDestinationIds')
Chris@0 217 ->with(['id' => 'test'])
Chris@17 218 ->will($this->returnValue([['test']]));
Chris@0 219
Chris@0 220 $this->message->expects($this->once())
Chris@0 221 ->method('display')
Chris@0 222 ->with('New object was not saved, no error provided');
Chris@0 223
Chris@0 224 $this->assertSame(MigrationInterface::RESULT_COMPLETED, $this->executable->import());
Chris@0 225 }
Chris@0 226
Chris@0 227 /**
Chris@0 228 * Tests the import method with a thrown MigrateException.
Chris@0 229 *
Chris@0 230 * The MigrationException in this case is being thrown from the destination.
Chris@0 231 */
Chris@0 232 public function testImportWithValidRowWithDestinationMigrateException() {
Chris@0 233 $exception_message = $this->getRandomGenerator()->string();
Chris@0 234 $source = $this->getMockSource();
Chris@0 235
Chris@0 236 $row = $this->getMockBuilder('Drupal\migrate\Row')
Chris@0 237 ->disableOriginalConstructor()
Chris@0 238 ->getMock();
Chris@0 239
Chris@0 240 $row->expects($this->once())
Chris@0 241 ->method('getSourceIdValues')
Chris@0 242 ->will($this->returnValue(['id' => 'test']));
Chris@0 243
Chris@0 244 $source->expects($this->once())
Chris@0 245 ->method('current')
Chris@0 246 ->will($this->returnValue($row));
Chris@0 247
Chris@0 248 $this->executable->setSource($source);
Chris@0 249
Chris@0 250 $this->migration->expects($this->once())
Chris@0 251 ->method('getProcessPlugins')
Chris@0 252 ->will($this->returnValue([]));
Chris@0 253
Chris@0 254 $destination = $this->getMock('Drupal\migrate\Plugin\MigrateDestinationInterface');
Chris@0 255 $destination->expects($this->once())
Chris@0 256 ->method('import')
Chris@0 257 ->with($row, ['test'])
Chris@0 258 ->will($this->throwException(new MigrateException($exception_message)));
Chris@0 259
Chris@0 260 $this->migration
Chris@0 261 ->method('getDestinationPlugin')
Chris@0 262 ->willReturn($destination);
Chris@0 263
Chris@0 264 $this->idMap->expects($this->once())
Chris@0 265 ->method('saveIdMapping')
Chris@0 266 ->with($row, [], MigrateIdMapInterface::STATUS_FAILED, NULL);
Chris@0 267
Chris@0 268 $this->idMap->expects($this->once())
Chris@0 269 ->method('saveMessage');
Chris@0 270
Chris@0 271 $this->idMap->expects($this->once())
Chris@17 272 ->method('lookupDestinationIds')
Chris@0 273 ->with(['id' => 'test'])
Chris@17 274 ->will($this->returnValue([['test']]));
Chris@0 275
Chris@0 276 $this->assertSame(MigrationInterface::RESULT_COMPLETED, $this->executable->import());
Chris@0 277 }
Chris@0 278
Chris@0 279 /**
Chris@0 280 * Tests the import method with a thrown MigrateException.
Chris@0 281 *
Chris@0 282 * The MigrationException in this case is being thrown from a process plugin.
Chris@0 283 */
Chris@0 284 public function testImportWithValidRowWithProcesMigrateException() {
Chris@0 285 $exception_message = $this->getRandomGenerator()->string();
Chris@0 286 $source = $this->getMockSource();
Chris@0 287
Chris@0 288 $row = $this->getMockBuilder('Drupal\migrate\Row')
Chris@0 289 ->disableOriginalConstructor()
Chris@0 290 ->getMock();
Chris@0 291
Chris@0 292 $row->expects($this->once())
Chris@0 293 ->method('getSourceIdValues')
Chris@0 294 ->willReturn(['id' => 'test']);
Chris@0 295
Chris@0 296 $source->expects($this->once())
Chris@0 297 ->method('current')
Chris@0 298 ->willReturn($row);
Chris@0 299
Chris@0 300 $this->executable->setSource($source);
Chris@0 301
Chris@0 302 $this->migration->expects($this->once())
Chris@0 303 ->method('getProcessPlugins')
Chris@0 304 ->willThrowException(new MigrateException($exception_message));
Chris@0 305
Chris@0 306 $destination = $this->getMock('Drupal\migrate\Plugin\MigrateDestinationInterface');
Chris@0 307 $destination->expects($this->never())
Chris@0 308 ->method('import');
Chris@0 309
Chris@0 310 $this->migration
Chris@0 311 ->method('getDestinationPlugin')
Chris@0 312 ->willReturn($destination);
Chris@0 313
Chris@0 314 $this->idMap->expects($this->once())
Chris@0 315 ->method('saveIdMapping')
Chris@0 316 ->with($row, [], MigrateIdMapInterface::STATUS_FAILED, NULL);
Chris@0 317
Chris@0 318 $this->idMap->expects($this->once())
Chris@0 319 ->method('saveMessage');
Chris@0 320
Chris@0 321 $this->idMap->expects($this->never())
Chris@17 322 ->method('lookupDestinationIds');
Chris@0 323
Chris@0 324 $this->assertSame(MigrationInterface::RESULT_COMPLETED, $this->executable->import());
Chris@0 325 }
Chris@0 326
Chris@0 327 /**
Chris@0 328 * Tests the import method with a regular Exception being thrown.
Chris@0 329 */
Chris@0 330 public function testImportWithValidRowWithException() {
Chris@0 331 $exception_message = $this->getRandomGenerator()->string();
Chris@0 332 $source = $this->getMockSource();
Chris@0 333
Chris@0 334 $row = $this->getMockBuilder('Drupal\migrate\Row')
Chris@0 335 ->disableOriginalConstructor()
Chris@0 336 ->getMock();
Chris@0 337
Chris@0 338 $row->expects($this->once())
Chris@0 339 ->method('getSourceIdValues')
Chris@0 340 ->will($this->returnValue(['id' => 'test']));
Chris@0 341
Chris@0 342 $source->expects($this->once())
Chris@0 343 ->method('current')
Chris@0 344 ->will($this->returnValue($row));
Chris@0 345
Chris@0 346 $this->executable->setSource($source);
Chris@0 347
Chris@0 348 $this->migration->expects($this->once())
Chris@0 349 ->method('getProcessPlugins')
Chris@0 350 ->will($this->returnValue([]));
Chris@0 351
Chris@0 352 $destination = $this->getMock('Drupal\migrate\Plugin\MigrateDestinationInterface');
Chris@0 353 $destination->expects($this->once())
Chris@0 354 ->method('import')
Chris@0 355 ->with($row, ['test'])
Chris@0 356 ->will($this->throwException(new \Exception($exception_message)));
Chris@0 357
Chris@0 358 $this->migration
Chris@0 359 ->method('getDestinationPlugin')
Chris@0 360 ->willReturn($destination);
Chris@0 361
Chris@0 362 $this->idMap->expects($this->once())
Chris@0 363 ->method('saveIdMapping')
Chris@0 364 ->with($row, [], MigrateIdMapInterface::STATUS_FAILED, NULL);
Chris@0 365
Chris@0 366 $this->idMap->expects($this->once())
Chris@0 367 ->method('saveMessage');
Chris@0 368
Chris@0 369 $this->idMap->expects($this->once())
Chris@17 370 ->method('lookupDestinationIds')
Chris@0 371 ->with(['id' => 'test'])
Chris@17 372 ->will($this->returnValue([['test']]));
Chris@0 373
Chris@0 374 $this->message->expects($this->once())
Chris@0 375 ->method('display')
Chris@0 376 ->with($exception_message);
Chris@0 377
Chris@0 378 $this->assertSame(MigrationInterface::RESULT_COMPLETED, $this->executable->import());
Chris@0 379 }
Chris@0 380
Chris@0 381 /**
Chris@0 382 * Tests the processRow method.
Chris@0 383 */
Chris@0 384 public function testProcessRow() {
Chris@0 385 $expected = [
Chris@0 386 'test' => 'test destination',
Chris@17 387 'test1' => 'test1 destination',
Chris@0 388 ];
Chris@0 389 foreach ($expected as $key => $value) {
Chris@0 390 $plugins[$key][0] = $this->getMock('Drupal\migrate\Plugin\MigrateProcessInterface');
Chris@0 391 $plugins[$key][0]->expects($this->once())
Chris@0 392 ->method('getPluginDefinition')
Chris@0 393 ->will($this->returnValue([]));
Chris@0 394 $plugins[$key][0]->expects($this->once())
Chris@0 395 ->method('transform')
Chris@0 396 ->will($this->returnValue($value));
Chris@0 397 }
Chris@0 398 $this->migration->expects($this->once())
Chris@0 399 ->method('getProcessPlugins')
Chris@0 400 ->with(NULL)
Chris@0 401 ->will($this->returnValue($plugins));
Chris@0 402 $row = new Row();
Chris@0 403 $this->executable->processRow($row);
Chris@0 404 foreach ($expected as $key => $value) {
Chris@0 405 $this->assertSame($row->getDestinationProperty($key), $value);
Chris@0 406 }
Chris@0 407 $this->assertSame(count($row->getDestination()), count($expected));
Chris@0 408 }
Chris@0 409
Chris@0 410 /**
Chris@0 411 * Tests the processRow method with an empty pipeline.
Chris@0 412 */
Chris@0 413 public function testProcessRowEmptyPipeline() {
Chris@0 414 $this->migration->expects($this->once())
Chris@0 415 ->method('getProcessPlugins')
Chris@0 416 ->with(NULL)
Chris@0 417 ->will($this->returnValue(['test' => []]));
Chris@0 418 $row = new Row();
Chris@0 419 $this->executable->processRow($row);
Chris@0 420 $this->assertSame($row->getDestination(), []);
Chris@0 421 }
Chris@0 422
Chris@0 423 /**
Chris@0 424 * Tests the processRow pipeline exception.
Chris@0 425 */
Chris@0 426 public function testProcessRowPipelineException() {
Chris@0 427 $row = new Row();
Chris@0 428 $plugin = $this->prophesize(MigrateProcessInterface::class);
Chris@0 429 $plugin->getPluginDefinition()->willReturn(['handle_multiples' => FALSE]);
Chris@0 430 $plugin->transform(NULL, $this->executable, $row, 'destination_id')
Chris@0 431 ->willReturn('transform_return_string');
Chris@0 432 $plugin->multiple()->willReturn(TRUE);
Chris@0 433 $plugin->getPluginId()->willReturn('plugin_id');
Chris@0 434 $plugin = $plugin->reveal();
Chris@0 435 $plugins['destination_id'] = [$plugin, $plugin];
Chris@0 436 $this->migration->method('getProcessPlugins')->willReturn($plugins);
Chris@0 437
Chris@0 438 $this->setExpectedException(MigrateException::class, 'Pipeline failed at plugin_id plugin for destination destination_id: transform_return_string received instead of an array,');
Chris@0 439 $this->executable->processRow($row);
Chris@0 440 }
Chris@0 441
Chris@0 442 /**
Chris@0 443 * Tests the processRow method.
Chris@0 444 */
Chris@0 445 public function testProcessRowEmptyDestination() {
Chris@0 446 $expected = [
Chris@0 447 'test' => 'test destination',
Chris@0 448 'test1' => 'test1 destination',
Chris@0 449 'test2' => NULL,
Chris@0 450 ];
Chris@0 451 $row = new Row();
Chris@0 452 $plugins = [];
Chris@0 453 foreach ($expected as $key => $value) {
Chris@0 454 $plugin = $this->prophesize(MigrateProcessInterface::class);
Chris@0 455 $plugin->getPluginDefinition()->willReturn([]);
Chris@0 456 $plugin->transform(NULL, $this->executable, $row, $key)->willReturn($value);
Chris@0 457 $plugin->multiple()->willReturn(TRUE);
Chris@0 458 $plugins[$key][0] = $plugin->reveal();
Chris@0 459 }
Chris@0 460 $this->migration->method('getProcessPlugins')->willReturn($plugins);
Chris@0 461 $this->executable->processRow($row);
Chris@0 462 foreach ($expected as $key => $value) {
Chris@0 463 $this->assertSame($value, $row->getDestinationProperty($key));
Chris@0 464 }
Chris@0 465 $this->assertCount(2, $row->getDestination());
Chris@0 466 $this->assertSame(['test2'], $row->getEmptyDestinationProperties());
Chris@0 467 }
Chris@0 468
Chris@0 469 /**
Chris@0 470 * Returns a mock migration source instance.
Chris@0 471 *
Chris@0 472 * @return \Drupal\migrate\Plugin\MigrateSourceInterface|\PHPUnit_Framework_MockObject_MockObject
Chris@0 473 * The mocked migration source.
Chris@0 474 */
Chris@0 475 protected function getMockSource() {
Chris@0 476 $iterator = $this->getMock('\Iterator');
Chris@0 477
Chris@0 478 $class = 'Drupal\migrate\Plugin\migrate\source\SourcePluginBase';
Chris@0 479 $source = $this->getMockBuilder($class)
Chris@0 480 ->disableOriginalConstructor()
Chris@0 481 ->setMethods(get_class_methods($class))
Chris@0 482 ->getMockForAbstractClass();
Chris@0 483 $source->expects($this->once())
Chris@0 484 ->method('rewind')
Chris@0 485 ->will($this->returnValue(TRUE));
Chris@0 486 $source->expects($this->any())
Chris@0 487 ->method('initializeIterator')
Chris@0 488 ->will($this->returnValue([]));
Chris@0 489 $source->expects($this->any())
Chris@0 490 ->method('valid')
Chris@0 491 ->will($this->onConsecutiveCalls(TRUE, FALSE));
Chris@0 492
Chris@0 493 return $source;
Chris@0 494 }
Chris@0 495
Chris@0 496 }