View source
<?php
namespace Drupal\Tests\private_message\Kernel;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Test\AssertMailTrait;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\private_message\Entity\PrivateMessage;
class UserNotificationEmailTest extends EntityKernelTestBase {
use AssertMailTrait;
public static $modules = [
'private_message',
];
protected $strictConfigSchema = FALSE;
private $threadManager;
private $userData;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('pm_thread_access_time');
$this
->installEntitySchema('pm_thread_delete_time');
$this
->installEntitySchema('private_message');
$this
->installEntitySchema('private_message_thread');
$this
->installEntitySchema('user');
$this
->installSchema('user', [
'users_data',
]);
$this
->installConfig([
'private_message',
]);
$this->threadManager = \Drupal::service('private_message.thread_manager');
$this->userData = \Drupal::service('user.data');
}
public function testANotificationEmailIsSent() {
$settings = \Drupal::config('private_message.settings')
->getRawData();
$this
->assertTrue($settings['enable_email_notifications']);
$this
->assertTrue($settings['send_by_default']);
$owner = $this
->createUser();
$member1 = $this
->createUser([
'mail' => 'member1@example.com',
]);
\Drupal::currentUser()
->setAccount($owner);
$message = $this
->createMessage([
'owner' => $owner,
]);
$this->threadManager
->saveThread($message, [
$owner,
$member1,
]);
$mails = $this
->getMails();
$this
->assertCount(1, $mails);
$this
->assertEquals('private_message_message_notification', $mails[0]['id']);
$this
->assertEquals('member1@example.com', $mails[0]['to']);
}
function testNotificationEmailsCanBeDisabled() {
$settings = \Drupal::configFactory()
->getEditable('private_message.settings');
$settings
->set('enable_email_notifications', FALSE);
$settings
->save(TRUE);
$this
->assertFalse($settings
->get('enable_email_notifications'));
$user1 = $this
->createUser();
$user2 = $this
->createUser();
$message = $this
->createMessage();
$this->threadManager
->saveThread($message, [
$user1,
$user2,
]);
$this
->assertCount(0, $this
->getMails());
}
public function testAUserCanDisableEmailNotifications() {
$owner = $this
->createUser();
$member1 = $this
->createUser([
'mail' => 'member1@example.com',
]);
$member2 = $this
->createUser([
'mail' => 'member2@example.com',
]);
\Drupal::currentUser()
->setAccount($owner);
$this
->disableNotificationsForUser($member2);
$message = $this
->createMessage([
'owner' => $owner,
]);
$this->threadManager
->saveThread($message, [
$owner,
$member1,
$member2,
]);
$this
->assertCount(1, $this
->getMails());
}
public function testNoEmailToUserWithNoEmailAddress() {
$owner = $this
->createUser();
$member = $this
->createUser([
'mail' => NULL,
]);
\Drupal::currentUser()
->setAccount($owner);
$message = $this
->createMessage([
'owner' => $owner,
]);
$this->threadManager
->saveThread($message, [
$owner,
$member,
]);
$this
->assertEmpty($this
->getMails(), 'Attempted to send email to a NULL address.');
}
private function createMessage(array $values = []) {
$message = PrivateMessage::create(array_merge([
'message' => $this
->randomString(),
], $values));
$message
->save();
return $message;
}
private function disableNotificationsForUser(AccountInterface $user) {
$this->userData
->set('private_message', $user
->id(), 'email_notification', FALSE);
$this
->assertFalse($this->userData
->get('private_message', $user
->id(), 'email_notification'));
}
}