Commit e5908d6b authored by alexpott's avatar alexpott

Issue #2546004 by neclimdul, quietone, hussainweb, benjy, phenaproxima,...

Issue #2546004 by neclimdul, quietone, hussainweb, benjy, phenaproxima, mikeryan: Add message service to migrate events
parent d2ab9b84
<?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);
}
}
......@@ -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 {}
......@@ -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;
}
......
......@@ -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.
*
......
......@@ -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;
}
......
<?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');
}
}
<?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');
}
}
<?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());
}
}
<?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());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment