View source
<?php
namespace Drupal\Tests\message_digest\Unit\Plugin\Notifier;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\Insert;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\State\StateInterface;
use Drupal\message\MessageInterface;
use Drupal\message_digest\Plugin\Notifier\Digest;
use Drupal\Tests\UnitTestCase;
class DigestTest extends UnitTestCase {
protected $configuration = [];
protected $connection;
protected $entityTypeManager;
protected $renderer;
protected $pluginDefinition = [
'digest_interval' => '1 day',
];
protected $pluginId;
protected $time;
protected $state;
public function setUp() {
parent::setUp();
$this->connection = $this
->prophesize(Connection::class)
->reveal();
$this->entityTypeManager = $this
->prophesize(EntityTypeManagerInterface::class)
->reveal();
$this->pluginId = $this
->randomMachineName();
$this->renderer = $this
->prophesize(RendererInterface::class)
->reveal();
$this->time = $this
->prophesize(TimeInterface::class)
->reveal();
$this->state = $this
->prophesize(StateInterface::class)
->reveal();
}
public function testDeliverUnsavedMessage() {
$message = $this
->prophesize(MessageInterface::class)
->reveal();
$this
->expectException(\AssertionError::class);
$this
->expectExceptionMessage('The message entity (or $message->original_message) must be saved in order to create a digest entry.');
$notifier = $this
->getNotifier($message);
$notifier
->deliver([]);
}
public function testDeliveryUnsavedOriginal() {
$original = $this
->prophesize(MessageInterface::class)
->reveal();
$message = $this
->prophesize(MessageInterface::class)
->reveal();
$message->original_message = $original;
$this
->expectException(\AssertionError::class);
$this
->expectExceptionMessage('The message entity (or $message->original_message) must be saved in order to create a digest entry.');
$notifier = $this
->getNotifier($message);
$notifier
->deliver([]);
}
public function testDeliverySavedOriginal() {
$original = $this
->prophesize(MessageInterface::class);
$original
->id()
->willReturn(42);
$message = $this
->prophesize(MessageInterface::class);
$message
->getOwnerId()
->willReturn(4);
$message
->getCreatedTime()
->willReturn(123);
$message
->id()
->willReturn(NULL);
$message = $message
->reveal();
$message->original_message = $original
->reveal();
$expected_row = [
'receiver' => 4,
'entity_type' => '',
'entity_id' => '',
'notifier' => $this->pluginId,
'timestamp' => 123,
'mid' => 42,
];
$insert = $this
->prophesize(Insert::class);
$insert
->fields($expected_row)
->willReturn($insert
->reveal());
$insert
->execute()
->shouldBeCalled();
$connection = $this
->prophesize(Connection::class);
$connection
->insert('message_digest')
->willReturn($insert
->reveal());
$this->connection = $connection
->reveal();
$notifier = $this
->getNotifier($message);
$this
->assertTrue($notifier
->deliver([]));
}
public function testProcessTimeThreshold($currentTime, $lastRun, $expectation) {
$time = $this
->prophesize(TimeInterface::class);
$time
->getRequestTime()
->willReturn($currentTime);
$this->time = $time
->reveal();
$state = $this
->prophesize(StateInterface::class);
$state
->get($this->pluginId . '_last_run', 0)
->willReturn($lastRun);
$this->state = $state
->reveal();
$message = $this
->prophesize(MessageInterface::class)
->reveal();
$notifier = $this
->getNotifier($message);
self::assertEquals($expectation, $notifier
->processDigest());
}
public function processTimeProvider() {
return [
[
952646400,
952556400,
TRUE,
],
[
952646400,
952559985,
TRUE,
],
[
952646400,
952560000,
TRUE,
],
[
952646400,
952560015,
TRUE,
],
[
952646400,
952560060,
FALSE,
],
[
952646400,
952563600,
FALSE,
],
];
}
protected function getNotifier(MessageInterface $message) {
$logger = $this
->prophesize(LoggerChannelInterface::class)
->reveal();
$notifier = new Digest($this->configuration, $this->pluginId, $this->pluginDefinition, $logger, $this->entityTypeManager, $this->renderer, $message, $this->state, $this->connection, $this->time);
return $notifier;
}
}