You are here

class DigestTest in Message Digest 8

Tests the Digest plugin.

@group message_digest

@coversDefaultClass \Drupal\message_digest\Plugin\Notifier\Digest

Hierarchy

Expanded class hierarchy of DigestTest

File

tests/src/Unit/Plugin/Notifier/DigestTest.php, line 23

Namespace

Drupal\Tests\message_digest\Unit\Plugin\Notifier
View source
class DigestTest extends UnitTestCase {

  /**
   * Digest configuration.
   *
   * @var array
   */
  protected $configuration = [];

  /**
   * Mocked database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * Mocked entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The mocked rendering service.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * Plugin definition.
   *
   * @var array
   */
  protected $pluginDefinition = [
    'digest_interval' => '1 day',
  ];

  /**
   * Plugin ID.
   *
   * @var string
   */
  protected $pluginId;

  /**
   * Mocked time service.
   *
   * @var \Drupal\Component\Datetime\TimeInterface
   */
  protected $time;

  /**
   * Mocked state.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * {@inheritdoc}
   */
  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();
  }

  /**
   * Test delivery without a saved message entity.
   *
   * @covers ::deliver
   */
  public function testDeliverUnsavedMessage() {

    // Setup an unsaved message.
    $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([]);
  }

  /**
   * Test delivery without a saved original message entity.
   *
   * @covers ::deliver
   */
  public function testDeliveryUnsavedOriginal() {

    // Setup an unsaved original message.
    $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([]);
  }

  /**
   * Test delivery with an unsaved message, but a saved original.
   *
   * @covers ::deliver
   */
  public function testDeliverySavedOriginal() {

    // Setup a saved original.
    $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();

    // Mock up the insert.
    $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([]));
  }

  /**
   * Test that digests will be processed at the correct time.
   *
   * @covers ::processDigest
   *
   * @dataProvider processTimeProvider
   */
  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());
  }

  /**
   * Provides test data for testProcessTimeThreshold().
   *
   * @return array
   *   Test data.
   */
  public function processTimeProvider() {
    return [
      // 25 hours ago.
      [
        952646400,
        952556400,
        TRUE,
      ],
      // 24 hours, 15 seconds ago.
      [
        952646400,
        952559985,
        TRUE,
      ],
      // 24 hours ago.
      [
        952646400,
        952560000,
        TRUE,
      ],
      // 23 hours, 59 minutes, 45 seconds ago.
      [
        952646400,
        952560015,
        TRUE,
      ],
      // 23 hours, 59 minutes ago.
      [
        952646400,
        952560060,
        FALSE,
      ],
      // 23 hours ago.
      [
        952646400,
        952563600,
        FALSE,
      ],
    ];
  }

  /**
   * Helper method to construct a digest notifier.
   *
   * @param \Drupal\message\MessageInterface $message
   *   Mocked message for the notifier.
   *
   * @return \Drupal\message_digest\Plugin\Notifier\Digest
   *   The message notifier.
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DigestTest::$configuration protected property Digest configuration.
DigestTest::$connection protected property Mocked database connection.
DigestTest::$entityTypeManager protected property Mocked entity type manager.
DigestTest::$pluginDefinition protected property Plugin definition.
DigestTest::$pluginId protected property Plugin ID.
DigestTest::$renderer protected property The mocked rendering service.
DigestTest::$state protected property Mocked state.
DigestTest::$time protected property Mocked time service.
DigestTest::getNotifier protected function Helper method to construct a digest notifier.
DigestTest::processTimeProvider public function Provides test data for testProcessTimeThreshold().
DigestTest::setUp public function Overrides UnitTestCase::setUp
DigestTest::testDeliverUnsavedMessage public function Test delivery without a saved message entity.
DigestTest::testDeliverySavedOriginal public function Test delivery with an unsaved message, but a saved original.
DigestTest::testDeliveryUnsavedOriginal public function Test delivery without a saved original message entity.
DigestTest::testProcessTimeThreshold public function Test that digests will be processed at the correct time.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.