Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / migrate / tests / src / Kernel / MigrateMessageTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate\Kernel;
4
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;
11
12 /**
13  * Tests whether idmap messages are sent to message interface when requested.
14  *
15  * @group migrate
16  */
17 class MigrateMessageTest extends KernelTestBase implements MigrateMessageInterface {
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = ['migrate', 'system'];
25
26   /**
27    * Migration to run.
28    *
29    * @var \Drupal\migrate\Plugin\MigrationInterface
30    */
31   protected $migration;
32
33   /**
34    * Messages accumulated during the migration run.
35    *
36    * @var array
37    */
38   protected $messages = [];
39
40   /**
41    * {@inheritdoc}
42    */
43   protected function setUp() {
44     parent::setUp();
45
46     $this->installConfig(['system']);
47
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.
50     $definition = [
51       'migration_tags' => ['Message test'],
52       'source' => [
53         'plugin' => 'embedded_data',
54         'data_rows' => [
55           ['name' => 'source_message', 'value' => 'a message'],
56         ],
57         'ids' => [
58           'name' => ['type' => 'string'],
59         ],
60       ],
61       'process' => [
62         'message' => [
63           'plugin' => 'concat',
64           'source' => 'value',
65         ],
66       ],
67       'destination' => [
68         'plugin' => 'config',
69         'config_name' => 'system.maintenance',
70       ],
71     ];
72
73     $this->migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
74   }
75
76   /**
77    * Tests migration interruptions.
78    */
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);
84   }
85
86   /**
87    * Tests migration interruptions.
88    */
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");
97   }
98
99   /**
100    * Reacts to map message event.
101    *
102    * @param \Drupal\Migrate\Event\MigrateIdMapMessageEvent $event
103    *   The migration event.
104    * @param string $name
105    *   The event name.
106    */
107   public function mapMessageRecorder(MigrateIdMapMessageEvent $event, $name) {
108     if ($event->getLevel() == MigrationInterface::MESSAGE_NOTICE ||
109         $event->getLevel() == MigrationInterface::MESSAGE_INFORMATIONAL) {
110       $type = 'status';
111     }
112     else {
113       $type = 'error';
114     }
115     $source_id_string = implode(',', $event->getSourceIdValues());
116     $this->display($source_id_string . ': ' . $event->getMessage(), $type);
117   }
118
119   /**
120    * {@inheritdoc}
121    */
122   public function display($message, $type = 'status') {
123     $this->messages[] = $message;
124   }
125
126 }