207af4d8e1b192ef932931030059b60cf6490b9e
[yaffs-website] / web / core / modules / migrate / tests / src / Kernel / MigrateInterruptionTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate\Kernel;
4
5 use Drupal\migrate\Event\MigratePostRowSaveEvent;
6 use Drupal\migrate\Plugin\MigrationInterface;
7 use Drupal\migrate\Event\MigrateEvents;
8 use Drupal\migrate\MigrateExecutable;
9 use Drupal\KernelTests\KernelTestBase;
10
11 /**
12  * Tests interruptions triggered during migrations.
13  *
14  * @group migrate
15  */
16 class MigrateInterruptionTest extends KernelTestBase {
17
18   /**
19    * Modules to enable.
20    *
21    * @var array
22    */
23   public static $modules = ['migrate', 'migrate_events_test'];
24
25   /**
26    * {@inheritdoc}
27    */
28   protected function setUp() {
29     parent::setUp();
30     \Drupal::service('event_dispatcher')->addListener(MigrateEvents::POST_ROW_SAVE,
31       [$this, 'postRowSaveEventRecorder']);
32   }
33
34   /**
35    * Tests migration interruptions.
36    */
37   public function testMigrateEvents() {
38     // Run a simple little migration, which should trigger one of each event
39     // other than map_delete.
40     $definition = [
41       'migration_tags' => ['Interruption test'],
42       'source' => [
43         'plugin' => 'embedded_data',
44         'data_rows' => [
45           ['data' => 'dummy value'],
46           ['data' => 'dummy value2'],
47         ],
48         'ids' => [
49           'data' => ['type' => 'string'],
50         ],
51       ],
52       'process' => ['value' => 'data'],
53       'destination' => ['plugin' => 'dummy'],
54     ];
55
56     $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
57
58     $executable = new MigrateExecutable($migration);
59     // When the import runs, the first row imported will trigger an
60     // interruption.
61     $result = $executable->import();
62
63     $this->assertEqual($result, MigrationInterface::RESULT_INCOMPLETE);
64
65     // The status should have been reset to IDLE.
66     $this->assertEqual($migration->getStatus(), MigrationInterface::STATUS_IDLE);
67   }
68
69   /**
70    * Reacts to post-row-save event.
71    *
72    * @param \Drupal\Migrate\Event\MigratePostRowSaveEvent $event
73    *   The migration event.
74    * @param string $name
75    *   The event name.
76    */
77   public function postRowSaveEventRecorder(MigratePostRowSaveEvent $event, $name) {
78     $event->getMigration()->interruptMigration(MigrationInterface::RESULT_INCOMPLETE);
79   }
80
81 }