Skip to content

Commit

Permalink
Rename property for original message.
Browse files Browse the repository at this point in the history
- Fixes #84.
  • Loading branch information
jhedstrom committed Mar 24, 2017
1 parent 061322b commit 8125bb4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Subscribers.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,11 @@ public function sendMessage(EntityInterface $entity, MessageInterface $message,
// Clone the message in case it will need to be saved, it won't
// overwrite the existing one.
$cloned_message = $message->createDuplicate();
// Push a copy of the original message into the new one.
$cloned_message->original = $message;
// Push a copy of the original message into the new one. The key
// `original` is not used here as that has special meaning and can prevent
// field values from being saved.
// @see SqlContentEntityStorage::saveToDedicatedTables().
$cloned_message->original_message = $message;
// Set the owner to this user.
$cloned_message->setOwnerId($delivery_candidate->getAccountId());

Expand Down
42 changes: 42 additions & 0 deletions tests/src/Kernel/SubscribersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace Drupal\Tests\message_subscribe\Kernel;

use Drupal\Component\Utility\Unicode;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Test\AssertMailTrait;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\message\Entity\Message;
use Drupal\message_subscribe\Subscribers\DeliveryCandidate;

Expand Down Expand Up @@ -396,4 +399,43 @@ public function testNotifyEntityOwner() {
$this->assertEquals($expected, $subscribers);
}

/**
* Tests message bundles with fields are properly cloned.
*
* @covers ::sendMessage
*/
public function testFieldedMessageBundles() {
$field_name = Unicode::strtolower($this->randomMachineName());
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'message',
'type' => 'text',
]);
$field_storage->save();
$field_config = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => $this->template->id(),
]);
$field_config->save();

// Enable the email notifier.
\Drupal::configFactory()->getEditable('message_subscribe.settings')->set('default_notifiers', ['email'])->save();

$message = Message::create([
'template' => $this->template->id(),
'uid' => $this->users[1],
$field_name => $this->randomString(),
]);

// Save and reload to mimic queue behavior.
$message->save();
$message = $message->load($message->id());

// Send message set to save the cloned message.
$node = $this->nodes[0];
$this->messageSubscribers->sendMessage($node, $message, ['email' => ['save on success' => TRUE]], ['entity access' => FALSE]);
$cloned_message = \Drupal::entityTypeManager()->getStorage('message')->load($message->id() + 1);
$this->assertEquals($message->{$field_name}->value, $cloned_message->{$field_name}->value);
}

}

0 comments on commit 8125bb4

Please sign in to comment.