3 namespace Drupal\Tests\migrate\Kernel;
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\migrate\Plugin\MigrationInterface;
7 use Drupal\migrate\Event\MigrateEvents;
8 use Drupal\migrate\Event\MigrateIdMapMessageEvent;
9 use Drupal\migrate\MigrateExecutable;
10 use Drupal\migrate\MigrateMessageInterface;
13 * Tests whether idmap messages are sent to message interface when requested.
17 class MigrateMessageTest extends KernelTestBase implements MigrateMessageInterface {
24 public static $modules = ['migrate', 'system'];
29 * @var \Drupal\migrate\Plugin\MigrationInterface
34 * Messages accumulated during the migration run.
38 protected $messages = [];
43 protected function setUp() {
46 $this->installConfig(['system']);
48 // A simple migration, which will generate a message to the ID map because
49 // the concat plugin throws an exception if its source is not an array.
51 'migration_tags' => ['Message test'],
53 'plugin' => 'embedded_data',
55 ['name' => 'source_message', 'value' => 'a message'],
58 'name' => ['type' => 'string'],
69 'config_name' => 'system.maintenance',
73 $this->migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
77 * Tests migration interruptions.
79 public function testMessagesNotTeed() {
80 // We don't ask for messages to be teed, so don't expect any.
81 $executable = new MigrateExecutable($this->migration, $this);
82 $executable->import();
83 $this->assertIdentical(count($this->messages), 0);
87 * Tests migration interruptions.
89 public function testMessagesTeed() {
90 // Ask to receive any messages sent to the idmap.
91 \Drupal::service('event_dispatcher')->addListener(MigrateEvents::IDMAP_MESSAGE,
92 [$this, 'mapMessageRecorder']);
93 $executable = new MigrateExecutable($this->migration, $this);
94 $executable->import();
95 $this->assertIdentical(count($this->messages), 1);
96 $this->assertIdentical(reset($this->messages), "source_message: 'a message' is not an array");
100 * Reacts to map message event.
102 * @param \Drupal\Migrate\Event\MigrateIdMapMessageEvent $event
103 * The migration event.
104 * @param string $name
107 public function mapMessageRecorder(MigrateIdMapMessageEvent $event, $name) {
108 if ($event->getLevel() == MigrationInterface::MESSAGE_NOTICE ||
109 $event->getLevel() == MigrationInterface::MESSAGE_INFORMATIONAL) {
115 $source_id_string = implode(',', $event->getSourceIdValues());
116 $this->display($source_id_string . ': ' . $event->getMessage(), $type);
122 public function display($message, $type = 'status') {
123 $this->messages[] = $message;