Chris@0: installConfig(['system']); Chris@0: Chris@0: // A simple migration, which will generate a message to the ID map because Chris@0: // the concat plugin throws an exception if its source is not an array. Chris@0: $definition = [ Chris@0: 'migration_tags' => ['Message test'], Chris@0: 'source' => [ Chris@0: 'plugin' => 'embedded_data', Chris@0: 'data_rows' => [ Chris@0: ['name' => 'source_message', 'value' => 'a message'], Chris@0: ], Chris@0: 'ids' => [ Chris@0: 'name' => ['type' => 'string'], Chris@0: ], Chris@0: ], Chris@0: 'process' => [ Chris@0: 'message' => [ Chris@0: 'plugin' => 'concat', Chris@0: 'source' => 'value', Chris@0: ], Chris@0: ], Chris@0: 'destination' => [ Chris@0: 'plugin' => 'config', Chris@0: 'config_name' => 'system.maintenance', Chris@0: ], Chris@0: ]; Chris@0: Chris@0: $this->migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests migration interruptions. Chris@0: */ Chris@0: public function testMessagesNotTeed() { Chris@0: // We don't ask for messages to be teed, so don't expect any. Chris@0: $executable = new MigrateExecutable($this->migration, $this); Chris@0: $executable->import(); Chris@0: $this->assertIdentical(count($this->messages), 0); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests migration interruptions. Chris@0: */ Chris@0: public function testMessagesTeed() { Chris@0: // Ask to receive any messages sent to the idmap. Chris@0: \Drupal::service('event_dispatcher')->addListener(MigrateEvents::IDMAP_MESSAGE, Chris@0: [$this, 'mapMessageRecorder']); Chris@0: $executable = new MigrateExecutable($this->migration, $this); Chris@0: $executable->import(); Chris@0: $this->assertIdentical(count($this->messages), 1); Chris@0: $this->assertIdentical(reset($this->messages), "source_message: 'a message' is not an array"); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Reacts to map message event. Chris@0: * Chris@18: * @param \Drupal\migrate\Event\MigrateIdMapMessageEvent $event Chris@0: * The migration event. Chris@0: * @param string $name Chris@0: * The event name. Chris@0: */ Chris@0: public function mapMessageRecorder(MigrateIdMapMessageEvent $event, $name) { Chris@0: if ($event->getLevel() == MigrationInterface::MESSAGE_NOTICE || Chris@0: $event->getLevel() == MigrationInterface::MESSAGE_INFORMATIONAL) { Chris@0: $type = 'status'; Chris@0: } Chris@0: else { Chris@0: $type = 'error'; Chris@0: } Chris@0: $source_id_string = implode(',', $event->getSourceIdValues()); Chris@0: $this->display($source_id_string . ': ' . $event->getMessage(), $type); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function display($message, $type = 'status') { Chris@0: $this->messages[] = $message; Chris@0: } Chris@0: Chris@0: }