Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\migrate\Kernel;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\KernelTests\KernelTestBase;
|
Chris@0
|
6 use Drupal\migrate\Plugin\MigrationInterface;
|
Chris@0
|
7 use Drupal\migrate\Event\MigrateEvents;
|
Chris@0
|
8 use Drupal\migrate\Event\MigrateIdMapMessageEvent;
|
Chris@0
|
9 use Drupal\migrate\MigrateExecutable;
|
Chris@0
|
10 use Drupal\migrate\MigrateMessageInterface;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Tests whether idmap messages are sent to message interface when requested.
|
Chris@0
|
14 *
|
Chris@0
|
15 * @group migrate
|
Chris@0
|
16 */
|
Chris@0
|
17 class MigrateMessageTest extends KernelTestBase implements MigrateMessageInterface {
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * Modules to enable.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @var array
|
Chris@0
|
23 */
|
Chris@0
|
24 public static $modules = ['migrate', 'system'];
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * Migration to run.
|
Chris@0
|
28 *
|
Chris@0
|
29 * @var \Drupal\migrate\Plugin\MigrationInterface
|
Chris@0
|
30 */
|
Chris@0
|
31 protected $migration;
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * Messages accumulated during the migration run.
|
Chris@0
|
35 *
|
Chris@0
|
36 * @var array
|
Chris@0
|
37 */
|
Chris@0
|
38 protected $messages = [];
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * {@inheritdoc}
|
Chris@0
|
42 */
|
Chris@0
|
43 protected function setUp() {
|
Chris@0
|
44 parent::setUp();
|
Chris@0
|
45
|
Chris@0
|
46 $this->installConfig(['system']);
|
Chris@0
|
47
|
Chris@0
|
48 // A simple migration, which will generate a message to the ID map because
|
Chris@0
|
49 // the concat plugin throws an exception if its source is not an array.
|
Chris@0
|
50 $definition = [
|
Chris@0
|
51 'migration_tags' => ['Message test'],
|
Chris@0
|
52 'source' => [
|
Chris@0
|
53 'plugin' => 'embedded_data',
|
Chris@0
|
54 'data_rows' => [
|
Chris@0
|
55 ['name' => 'source_message', 'value' => 'a message'],
|
Chris@0
|
56 ],
|
Chris@0
|
57 'ids' => [
|
Chris@0
|
58 'name' => ['type' => 'string'],
|
Chris@0
|
59 ],
|
Chris@0
|
60 ],
|
Chris@0
|
61 'process' => [
|
Chris@0
|
62 'message' => [
|
Chris@0
|
63 'plugin' => 'concat',
|
Chris@0
|
64 'source' => 'value',
|
Chris@0
|
65 ],
|
Chris@0
|
66 ],
|
Chris@0
|
67 'destination' => [
|
Chris@0
|
68 'plugin' => 'config',
|
Chris@0
|
69 'config_name' => 'system.maintenance',
|
Chris@0
|
70 ],
|
Chris@0
|
71 ];
|
Chris@0
|
72
|
Chris@0
|
73 $this->migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 /**
|
Chris@0
|
77 * Tests migration interruptions.
|
Chris@0
|
78 */
|
Chris@0
|
79 public function testMessagesNotTeed() {
|
Chris@0
|
80 // We don't ask for messages to be teed, so don't expect any.
|
Chris@0
|
81 $executable = new MigrateExecutable($this->migration, $this);
|
Chris@0
|
82 $executable->import();
|
Chris@0
|
83 $this->assertIdentical(count($this->messages), 0);
|
Chris@0
|
84 }
|
Chris@0
|
85
|
Chris@0
|
86 /**
|
Chris@0
|
87 * Tests migration interruptions.
|
Chris@0
|
88 */
|
Chris@0
|
89 public function testMessagesTeed() {
|
Chris@0
|
90 // Ask to receive any messages sent to the idmap.
|
Chris@0
|
91 \Drupal::service('event_dispatcher')->addListener(MigrateEvents::IDMAP_MESSAGE,
|
Chris@0
|
92 [$this, 'mapMessageRecorder']);
|
Chris@0
|
93 $executable = new MigrateExecutable($this->migration, $this);
|
Chris@0
|
94 $executable->import();
|
Chris@0
|
95 $this->assertIdentical(count($this->messages), 1);
|
Chris@0
|
96 $this->assertIdentical(reset($this->messages), "source_message: 'a message' is not an array");
|
Chris@0
|
97 }
|
Chris@0
|
98
|
Chris@0
|
99 /**
|
Chris@0
|
100 * Reacts to map message event.
|
Chris@0
|
101 *
|
Chris@18
|
102 * @param \Drupal\migrate\Event\MigrateIdMapMessageEvent $event
|
Chris@0
|
103 * The migration event.
|
Chris@0
|
104 * @param string $name
|
Chris@0
|
105 * The event name.
|
Chris@0
|
106 */
|
Chris@0
|
107 public function mapMessageRecorder(MigrateIdMapMessageEvent $event, $name) {
|
Chris@0
|
108 if ($event->getLevel() == MigrationInterface::MESSAGE_NOTICE ||
|
Chris@0
|
109 $event->getLevel() == MigrationInterface::MESSAGE_INFORMATIONAL) {
|
Chris@0
|
110 $type = 'status';
|
Chris@0
|
111 }
|
Chris@0
|
112 else {
|
Chris@0
|
113 $type = 'error';
|
Chris@0
|
114 }
|
Chris@0
|
115 $source_id_string = implode(',', $event->getSourceIdValues());
|
Chris@0
|
116 $this->display($source_id_string . ': ' . $event->getMessage(), $type);
|
Chris@0
|
117 }
|
Chris@0
|
118
|
Chris@0
|
119 /**
|
Chris@0
|
120 * {@inheritdoc}
|
Chris@0
|
121 */
|
Chris@0
|
122 public function display($message, $type = 'status') {
|
Chris@0
|
123 $this->messages[] = $message;
|
Chris@0
|
124 }
|
Chris@0
|
125
|
Chris@0
|
126 }
|