diff --git a/contact_storage.info.yml b/contact_storage.info.yml index c15ac22..99391bd 100644 --- a/contact_storage.info.yml +++ b/contact_storage.info.yml @@ -8,3 +8,6 @@ dependencies: - user - views configure: entity.contact_message.collection +test_dependencies: + - migrate + - migrate_drupal \ No newline at end of file diff --git a/migration_templates/contact_save_d6.yml b/migration_templates/contact_save_d6.yml new file mode 100644 index 0000000..a6f25d6 --- /dev/null +++ b/migration_templates/contact_save_d6.yml @@ -0,0 +1,36 @@ +id: contact_storage_d6_contact_save +label: Contact save to storage +migration_tags: + - Drupal 6 +source: + plugin: contact_storage_d6_contact_save +process: + id: id + contact_form: + - + plugin: machine_name + source: category + name: name + mail: email + subject: + - + plugin: contact_storage_truncate + max_length: 100 + source: subject + message: message + # By default, this will only save the first valid recipient from contact_save + # unless another module has altered the field cardinality. + recipient: + - + plugin: explode + source: recipients + delimiter: , + limit: 10 + - + plugin: contact_storage_recipients + created: created +destination: + plugin: 'entity:contact_message' +migration_dependencies: + required: + - contact_category \ No newline at end of file diff --git a/src/Plugin/migrate/destination/Message.php b/src/Plugin/migrate/destination/Message.php new file mode 100644 index 0000000..f430f40 --- /dev/null +++ b/src/Plugin/migrate/destination/Message.php @@ -0,0 +1,50 @@ + ['type' => 'integer']]; + } + + /** + * {@inheritdoc} + */ + public function fields(MigrationInterface $migration = NULL) { + return [ + 'id' => 'Contact storage message ID', + 'name' => "The sender's name", + 'contact_form' => 'The contact form ID', + 'mail' => "The sender's email address", + 'subject' => 'The email subject', + 'message' => 'The email body', + 'recipient' => 'The recipient user ID' + ]; + } + + /** + * {@inheritdoc} + */ + public function import(Row $row, array $old_destination_id_values = array()) { + return parent::import($row, $old_destination_id_values); + } +} \ No newline at end of file diff --git a/src/Plugin/migrate/process/Recipients.php b/src/Plugin/migrate/process/Recipients.php new file mode 100644 index 0000000..d50ade9 --- /dev/null +++ b/src/Plugin/migrate/process/Recipients.php @@ -0,0 +1,78 @@ +entityManager = $entity_manager; + } + + /** + * {@inheritdoc} + */ + static public function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('entity_type.manager') + ); + } + + /** + * {@inheritdoc} + */ + public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { + return $this->getUserId($value); + } + + /** + * Get the entity id of the user by the e-mail address. + * + * @param string $mail + * E-mail address + * @return integer|NULL + * Either the user id or NULL if not found. + */ + protected function getUserId($mail) { + $accounts = $this->entityManager->getStorage('user')->loadByProperties(['mail' => trim($mail)]); + if ($accounts) { + $account = array_shift($accounts); + return $account->id(); + } + return NULL; + } +} \ No newline at end of file diff --git a/src/Plugin/migrate/process/Truncate.php b/src/Plugin/migrate/process/Truncate.php new file mode 100644 index 0000000..9fe626d --- /dev/null +++ b/src/Plugin/migrate/process/Truncate.php @@ -0,0 +1,33 @@ +configuration['max_length'])) { + return substr($value, 0, $this->configuration['max_length']); + } + + throw new MigrateSkipProcessException('Missing max_length setting for the contact_storage_truncate plugin.'); + } + +} \ No newline at end of file diff --git a/src/Plugin/migrate/source/d6/ContactSave.php b/src/Plugin/migrate/source/d6/ContactSave.php new file mode 100644 index 0000000..61d2225 --- /dev/null +++ b/src/Plugin/migrate/source/d6/ContactSave.php @@ -0,0 +1,63 @@ + ['type' => 'integer'] + ]; + } + + /** + * {@inheritdoc} + */ + public function query() { + return $this->select('contact_save')->fields('contact_save'); + } + + /** + * {@inheritdoc} + */ + public function fields() { + return [ + 'name' => 'The contact name', + 'uid' => 'An optional contact user ID', + 'cid' => 'Contact form ID', + 'email' => 'The contact e-mail address', + 'subject' => 'The email subject', + 'category' => 'The contact form category', + 'recipients' => 'A string of recipients', + 'message' => 'The contact form message', + 'body' => 'The email body that was sent', + 'created' => 'The created date', + 'reply' => 'The email body of the reply email', + 'replied' => 'Whether a reply email was sent', + 'replier' => 'The user ID who sent the reply', + ]; + } + + /** + * {@inheritdoc} + */ + public function __toString() { + return $this->getPluginId(); + } +} \ No newline at end of file diff --git a/src/Tests/ContactSaveD6MigrateTest.php b/src/Tests/ContactSaveD6MigrateTest.php new file mode 100644 index 0000000..87f11d0 --- /dev/null +++ b/src/Tests/ContactSaveD6MigrateTest.php @@ -0,0 +1,42 @@ +loadFixture( __DIR__ . '/tests/fixtures/drupal6.php'); + $this->installMigrations('Drupal 6'); + $this->executeMigrations(['contact_category', 'contact_storage_d6_contact_save']); + } + + /** + * Assert that the message entities exist. + */ + public function testMessage() { + $messages = Message::loadMultiple(); + + $this->assertEqual(3, count($messages), 'Three contact messages were migrated.'); + } +} \ No newline at end of file diff --git a/tests/fixtures/drupal6.php b/tests/fixtures/drupal6.php new file mode 100644 index 0000000..427149e --- /dev/null +++ b/tests/fixtures/drupal6.php @@ -0,0 +1,154 @@ +schema()->createTable('contact_save', array( + 'fields' => array( + 'id' => array( + 'type' => 'serial', + 'not null' => TRUE, + 'unsigned' => TRUE + ), + 'mail_key' => array( + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE + ), + 'uid' => array( + 'type' => 'int', + 'size' => 'normal', + 'unsigned' => TRUE + ), + 'name' => array( + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE + ), + 'email' => array( + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE + ), + 'subject' => array( + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE + ), + 'cid' => array( + 'type' => 'int', + 'size' => 'normal', + 'unsigned' => TRUE, + 'not null' => TRUE + ), + 'category' => array( + 'type' => 'varchar', + 'length' => 255 + ), + 'recipients' => array( + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE + ), + 'message' => array( + 'type' => 'text', + 'not null' => TRUE, + 'size' => 'big' + ), + 'body' => array( + 'type' => 'text', + 'not null' => TRUE, + 'size' => 'big' + ), + 'created' => array( + 'type' => 'int', + 'not null' => TRUE, + 'unsigned' => TRUE + ), + 'reply' => array( + 'type' => 'text', + 'size' => 'big' + ), + 'replied' => array( + 'type' => 'int', + 'unsigned' => TRUE + ), + 'replier' => array( + 'type' => 'int', + 'size' => 'normal', + 'unsigned' => TRUE + ), + ), + 'primary key' => array( + 'id' + ), + 'indexes' => array( + 'category' => array( + 'category', + ), + ), +)); + +$connection->insert('contact_save') + ->fields(array( + 'id', + 'mail_key', + 'uid', + 'name', + 'email', + 'subject', + 'cid', + 'category', + 'recipients', + 'message', + 'body', + 'created', + )) + ->values(array( + 'id' => 1, + 'mail_key' => 'contact_page_mail', + 'uid' => 0, + 'name' => 'Pat Lewis', + 'email' => 'pat.lewis@example.com', + 'subject' => 'Hello', + 'cid' => 1, + 'category' => 'Website feedback', + 'recipients' => 'admin@example.com', + 'message' => "Hello, I have found that your web site is not responsive.\nPlease contact me,\n\nThanks", + 'body' => "Pat Lewis sent a message using the contact form at http://example.com/contact.\n\nHello, I have found that your web site is not responsive.\nPlease contact me,\n\nThanks", + 'created' => 146239771, + )) + ->values(array( + 'id' => 2, + 'mail_key' => 'contact_page_mail', + 'uid' => 0, + 'name' => 'Sam Davis', + 'email' => 'sam.davis@example.com', + 'subject' => 'Nice logo!', + 'cid' => 1, + 'category' => 'Website feedback', + 'recipients' => 'admin@example.com', + 'message' => "Wow!!!\n\nI am amazed by your fantastic web site!\n\nHow can I get one like that?\n\nPlease contact me back!", + 'body' => "Sam Davis sent a message using the contact form at http://example.com/contact.\n\nWow!!!\n\nI am amazed by your fantastic web site!\n\nHow can I get one like that?\n\nPlease contact me back!", + 'created' => 1462397735, + )) + ->values(array( + 'id' => 3, + 'mail_key' => 'contact_page_mail', + 'uid' => 0, + 'name' => 'Alex Donis', + 'email' => 'Alex.Donis@example.com', + 'subject' => 'About some other category', + 'cid' => 2, + 'category' => 'Some other category', + 'recipients' => 'test@example.com', + 'message' => "Hi!\n\nI would like to find out more about 'Some other category'.\n\nThank you in advance.", + 'body' => "Alex Donis sent a message using the contact form at http://example.com/contact.\n\nHi!\n\nI would like to find out more about 'Some other category'.\n\nThank you in advance.", + 'created' => 1462397390, + ));