PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/migrate/tests/src/Kernel/MigrateMessageTest.php

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