diff --git a/core/modules/migrate/src/Event/EventBase.php b/core/modules/migrate/src/Event/EventBase.php new file mode 100644 index 0000000000000000000000000000000000000000..b2284744b92cceaba9a43c993e6ee7e12b4ff28e --- /dev/null +++ b/core/modules/migrate/src/Event/EventBase.php @@ -0,0 +1,60 @@ +<?php + +namespace Drupal\migrate\Event; + +use Drupal\migrate\Plugin\MigrationInterface; +use Drupal\migrate\MigrateMessageInterface; +use Symfony\Component\EventDispatcher\Event as SymfonyEvent; + +class EventBase extends SymfonyEvent { + + /** + * The migration. + * + * @var \Drupal\migrate\Plugin\MigrationInterface + */ + protected $migration; + + /** + * The current message service. + * + * @var \Drupal\migrate\MigrateMessageInterface + */ + protected $message; + + /** + * Constructs a Migrate event object. + * + * @param \Drupal\migrate\Plugin\MigrationInterface $migration + * The migration being run. + * @param \Drupal\migrate\MigrateMessageInterface $message + * The Migrate message service. + */ + public function __construct(MigrationInterface $migration, MigrateMessageInterface $message) { + $this->migration = $migration; + $this->message = $message; + } + + /** + * Gets the migration. + * + * @return \Drupal\migrate\Plugin\MigrationInterface + * The migration being run. + */ + public function getMigration() { + return $this->migration; + } + + /** + * Logs a message using the Migrate message service. + * + * @param string $message + * The message to log. + * @param string $type + * The type of message, for example: status or warning. + */ + public function logMessage($message, $type = 'status') { + $this->message->display($message, $type); + } + +} diff --git a/core/modules/migrate/src/Event/MigrateImportEvent.php b/core/modules/migrate/src/Event/MigrateImportEvent.php index 2ccecfb5f68c2073f3e1bb21cc872b176dfc8d5c..954117156de5b82679910f755fb9d436d74a8d6b 100644 --- a/core/modules/migrate/src/Event/MigrateImportEvent.php +++ b/core/modules/migrate/src/Event/MigrateImportEvent.php @@ -2,39 +2,7 @@ namespace Drupal\migrate\Event; -use Drupal\migrate\Plugin\MigrationInterface; -use Symfony\Component\EventDispatcher\Event; - /** * Wraps a pre- or post-import event for event listeners. */ -class MigrateImportEvent extends Event { - - /** - * Migration entity. - * - * @var \Drupal\migrate\Plugin\MigrationInterface - */ - protected $migration; - - /** - * Constructs an import event object. - * - * @param \Drupal\migrate\Plugin\MigrationInterface $migration - * Migration entity. - */ - public function __construct(MigrationInterface $migration) { - $this->migration = $migration; - } - - /** - * Gets the migration entity. - * - * @return \Drupal\migrate\Plugin\MigrationInterface - * The migration entity involved. - */ - public function getMigration() { - return $this->migration; - } - -} +class MigrateImportEvent extends EventBase {} diff --git a/core/modules/migrate/src/Event/MigratePostRowSaveEvent.php b/core/modules/migrate/src/Event/MigratePostRowSaveEvent.php index ce30a79c9127968517257e71106b2471a8bd5e6a..e16a259351c0ab8bb4504ffaf64ce8c6330c9e6c 100644 --- a/core/modules/migrate/src/Event/MigratePostRowSaveEvent.php +++ b/core/modules/migrate/src/Event/MigratePostRowSaveEvent.php @@ -3,6 +3,7 @@ namespace Drupal\migrate\Event; use Drupal\migrate\Plugin\MigrationInterface; +use Drupal\migrate\MigrateMessageInterface; use Drupal\migrate\Row; /** @@ -10,18 +11,27 @@ */ class MigratePostRowSaveEvent extends MigratePreRowSaveEvent { + /** + * The row's destination ID. + * + * @var array|bool + */ + protected $destinationIdValues = []; + /** * Constructs a post-save event object. * * @param \Drupal\migrate\Plugin\MigrationInterface $migration * Migration entity. + * @param \Drupal\migrate\MigrateMessageInterface $message + * The message interface. * @param \Drupal\migrate\Row $row * Row object. * @param array|bool $destination_id_values * Values represent the destination ID. */ - public function __construct(MigrationInterface $migration, Row $row, $destination_id_values) { - parent::__construct($migration, $row); + public function __construct(MigrationInterface $migration, MigrateMessageInterface $message, Row $row, $destination_id_values) { + parent::__construct($migration, $message, $row); $this->destinationIdValues = $destination_id_values; } diff --git a/core/modules/migrate/src/Event/MigratePreRowSaveEvent.php b/core/modules/migrate/src/Event/MigratePreRowSaveEvent.php index 059daff1f686a49c9d1f49fedc38a2d9c8cc519e..5e563bbd1080cf01f0209a4568f1ad246aaaf45b 100644 --- a/core/modules/migrate/src/Event/MigratePreRowSaveEvent.php +++ b/core/modules/migrate/src/Event/MigratePreRowSaveEvent.php @@ -3,13 +3,13 @@ namespace Drupal\migrate\Event; use Drupal\migrate\Plugin\MigrationInterface; +use Drupal\migrate\MigrateMessageInterface; use Drupal\migrate\Row; -use Symfony\Component\EventDispatcher\Event; /** * Wraps a pre-save event for event listeners. */ -class MigratePreRowSaveEvent extends Event { +class MigratePreRowSaveEvent extends EventBase { /** * Row object. @@ -18,34 +18,20 @@ class MigratePreRowSaveEvent extends Event { */ protected $row; - /** - * Migration entity. - * - * @var \Drupal\migrate\Plugin\MigrationInterface - */ - protected $migration; - /** * Constructs a pre-save event object. * * @param \Drupal\migrate\Plugin\MigrationInterface $migration * Migration entity. + * @param \Drupal\migrate\MigrateMessageInterface $message + * The current migrate message service. + * @param \Drupal\migrate\Row $row */ - public function __construct(MigrationInterface $migration, Row $row) { - $this->migration = $migration; + public function __construct(MigrationInterface $migration, MigrateMessageInterface $message, Row $row) { + parent::__construct($migration, $message); $this->row = $row; } - /** - * Gets the migration entity. - * - * @return \Drupal\migrate\Plugin\MigrationInterface - * The migration entity being imported. - */ - public function getMigration() { - return $this->migration; - } - /** * Gets the row object. * diff --git a/core/modules/migrate/src/MigrateExecutable.php b/core/modules/migrate/src/MigrateExecutable.php index c0b3537ac1e355a5cb247312e8bcf0bc3ed86b79..bd0468be0bae166ecffe57e32f77fe37aef7924d 100644 --- a/core/modules/migrate/src/MigrateExecutable.php +++ b/core/modules/migrate/src/MigrateExecutable.php @@ -177,7 +177,7 @@ public function import() { )), 'error'); return MigrationInterface::RESULT_FAILED; } - $this->getEventDispatcher()->dispatch(MigrateEvents::PRE_IMPORT, new MigrateImportEvent($this->migration)); + $this->getEventDispatcher()->dispatch(MigrateEvents::PRE_IMPORT, new MigrateImportEvent($this->migration, $this->message)); // Knock off migration if the requirements haven't been met. try { @@ -229,9 +229,9 @@ public function import() { if ($save) { try { - $this->getEventDispatcher()->dispatch(MigrateEvents::PRE_ROW_SAVE, new MigratePreRowSaveEvent($this->migration, $row)); + $this->getEventDispatcher()->dispatch(MigrateEvents::PRE_ROW_SAVE, new MigratePreRowSaveEvent($this->migration, $this->message, $row)); $destination_id_values = $destination->import($row, $id_map->lookupDestinationId($this->sourceIdValues)); - $this->getEventDispatcher()->dispatch(MigrateEvents::POST_ROW_SAVE, new MigratePostRowSaveEvent($this->migration, $row, $destination_id_values)); + $this->getEventDispatcher()->dispatch(MigrateEvents::POST_ROW_SAVE, new MigratePostRowSaveEvent($this->migration, $this->message, $row, $destination_id_values)); if ($destination_id_values) { // We do not save an idMap entry for config. if ($destination_id_values !== TRUE) { @@ -288,7 +288,7 @@ public function import() { } } - $this->getEventDispatcher()->dispatch(MigrateEvents::POST_IMPORT, new MigrateImportEvent($this->migration)); + $this->getEventDispatcher()->dispatch(MigrateEvents::POST_IMPORT, new MigrateImportEvent($this->migration, $this->message)); $this->migration->setStatus(MigrationInterface::STATUS_IDLE); return $return; } diff --git a/core/modules/migrate/tests/src/Unit/Event/EventBaseTest.php b/core/modules/migrate/tests/src/Unit/Event/EventBaseTest.php new file mode 100644 index 0000000000000000000000000000000000000000..6a52ed6efe3ecd1b735ff3bd595ed9d310f70873 --- /dev/null +++ b/core/modules/migrate/tests/src/Unit/Event/EventBaseTest.php @@ -0,0 +1,44 @@ +<?php + +namespace Drupal\Tests\migrate\Unit\Event; + +use Drupal\migrate\Event\EventBase; + +/** + * @coversDefaultClass \Drupal\migrate\Event\EventBase + * @group migrate + */ +class EventBaseTest extends \PHPUnit_Framework_TestCase { + + /** + * Test getMigration method. + * + * @covers ::__construct + * @covers ::getMigration + */ + public function testGetMigration() { + $migration = $this->prophesize('\Drupal\migrate\Plugin\MigrationInterface')->reveal(); + $message_service = $this->prophesize('\Drupal\migrate\MigrateMessageInterface')->reveal(); + $row = $this->prophesize('\Drupal\migrate\Row')->reveal(); + $event = new EventBase($migration, $message_service, $row, [1, 2, 3]); + $this->assertSame($migration, $event->getMigration()); + } + + /** + * Test logging a message. + * + * @covers ::__construct + * @covers ::logMessage + */ + public function testLogMessage() { + $migration = $this->prophesize('\Drupal\migrate\Plugin\MigrationInterface')->reveal(); + $message_service = $this->prophesize('\Drupal\migrate\MigrateMessageInterface'); + $event = new EventBase($migration, $message_service->reveal()); + // Assert that the intended calls to the services happen. + $message_service->display('status message', 'status')->shouldBeCalledTimes(1); + $event->logMessage('status message'); + $message_service->display('warning message', 'warning')->shouldBeCalledTimes(1); + $event->logMessage('warning message', 'warning'); + } + +} diff --git a/core/modules/migrate/tests/src/Unit/Event/MigrateImportEventTest.php b/core/modules/migrate/tests/src/Unit/Event/MigrateImportEventTest.php new file mode 100644 index 0000000000000000000000000000000000000000..480fe85a704b45f2e24bd5eb7ddab0cb76cdf186 --- /dev/null +++ b/core/modules/migrate/tests/src/Unit/Event/MigrateImportEventTest.php @@ -0,0 +1,43 @@ +<?php + +namespace Drupal\Tests\migrate\Unit\Event; + +use Drupal\migrate\Event\MigrateImportEvent; + +/** + * @coversDefaultClass \Drupal\migrate\Event\MigrateImportEvent + * @group migrate + */ +class MigrateImportEventTest extends \PHPUnit_Framework_TestCase { + + /** + * Test getMigration method. + * + * @covers ::__construct + * @covers ::getMigration + */ + public function testGetMigration() { + $migration = $this->prophesize('\Drupal\migrate\Plugin\MigrationInterface')->reveal(); + $message_service = $this->prophesize('\Drupal\migrate\MigrateMessageInterface')->reveal(); + $event = new MigrateImportEvent($migration, $message_service); + $this->assertSame($migration, $event->getMigration()); + } + + /** + * Test logging a message. + * + * @covers ::__construct + * @covers ::logMessage + */ + public function testLogMessage() { + $migration = $this->prophesize('\Drupal\migrate\Plugin\MigrationInterface'); + $message_service = $this->prophesize('\Drupal\migrate\MigrateMessageInterface'); + $event = new MigrateImportEvent($migration->reveal(), $message_service->reveal()); + // Assert that the intended calls to the services happen. + $message_service->display('status message', 'status')->shouldBeCalledTimes(1); + $event->logMessage('status message'); + $message_service->display('warning message', 'warning')->shouldBeCalledTimes(1); + $event->logMessage('warning message', 'warning'); + } + +} diff --git a/core/modules/migrate/tests/src/Unit/Event/MigratePostRowSaveEventTest.php b/core/modules/migrate/tests/src/Unit/Event/MigratePostRowSaveEventTest.php new file mode 100644 index 0000000000000000000000000000000000000000..f42cb06e9eda4b8c37435050d8cfe83b63aa92ba --- /dev/null +++ b/core/modules/migrate/tests/src/Unit/Event/MigratePostRowSaveEventTest.php @@ -0,0 +1,41 @@ +<?php + +namespace Drupal\Tests\migrate\Unit\Event; + +use Drupal\migrate\Event\MigratePostRowSaveEvent; + +/** + * @coversDefaultClass \Drupal\migrate\Event\MigratePostRowSaveEvent + * @group migrate + */ +class MigratePostRowSaveEventTest extends EventBaseTest { + + /** + * Test getDestinationIdValues method. + * + * @covers ::__construct + * @covers ::getDestinationIdValues + */ + public function testGetDestinationIdValues() { + $migration = $this->prophesize('\Drupal\migrate\Plugin\MigrationInterface')->reveal(); + $message_service = $this->prophesize('\Drupal\migrate\MigrateMessageInterface')->reveal(); + $row = $this->prophesize('\Drupal\migrate\Row')->reveal(); + $event = new MigratePostRowSaveEvent($migration, $message_service, $row, [1, 2, 3]); + $this->assertSame([1, 2, 3], $event->getDestinationIdValues()); + } + + /** + * Test getRow method. + * + * @covers ::__construct + * @covers ::getRow + */ + public function testGetRow() { + $migration = $this->prophesize('\Drupal\migrate\Plugin\MigrationInterface')->reveal(); + $message_service = $this->prophesize('\Drupal\migrate\MigrateMessageInterface'); + $row = $this->prophesize('\Drupal\migrate\Row')->reveal(); + $event = new MigratePostRowSaveEvent($migration, $message_service->reveal(), $row, [1, 2, 3]); + $this->assertSame($row, $event->getRow()); + } + +} diff --git a/core/modules/migrate/tests/src/Unit/Event/MigratePreRowSaveEventTest.php b/core/modules/migrate/tests/src/Unit/Event/MigratePreRowSaveEventTest.php new file mode 100644 index 0000000000000000000000000000000000000000..f15dac1912a2bb5459b7589ef3e5fdaea7f0a9f8 --- /dev/null +++ b/core/modules/migrate/tests/src/Unit/Event/MigratePreRowSaveEventTest.php @@ -0,0 +1,27 @@ +<?php + +namespace Drupal\Tests\migrate\Unit\Event; + +use Drupal\migrate\Event\MigratePreRowSaveEvent; + +/** + * @coversDefaultClass \Drupal\migrate\Event\MigratePreRowSaveEvent + * @group migrate + */ +class MigratePreRowSaveEventTest extends EventBaseTest { + + /** + * Test getRow method. + * + * @covers ::__construct + * @covers ::getRow + */ + public function testGetRow() { + $migration = $this->prophesize('\Drupal\migrate\Plugin\MigrationInterface')->reveal(); + $message_service = $this->prophesize('\Drupal\migrate\MigrateMessageInterface')->reveal(); + $row = $this->prophesize('\Drupal\migrate\Row')->reveal(); + $event = new MigratePreRowSaveEvent($migration, $message_service, $row); + $this->assertSame($row, $event->getRow()); + } + +}