You are here

class ContentModerationNotificationTest in Content Moderation Notifications 8.2

Same name and namespace in other branches
  1. 8.3 tests/src/Unit/Entity/ContentModerationNotificationTest.php \Drupal\Tests\content_moderation_notifications\Unit\Entity\ContentModerationNotificationTest

Tests for the notification entity.

@group content_moderation_notifications

@coversDefaultClass \Drupal\content_moderation_notifications\Entity\ContentModerationNotification

Hierarchy

Expanded class hierarchy of ContentModerationNotificationTest

File

tests/src/Unit/Entity/ContentModerationNotificationTest.php, line 20

Namespace

Drupal\Tests\content_moderation_notifications\Unit\Entity
View source
class ContentModerationNotificationTest extends UnitTestCase {

  /**
   * Test fixture.
   *
   * @var \Drupal\content_moderation_notifications\Entity\ContentModerationNotification
   */
  protected $notification;

  /**
   * Test data for the fixture.
   *
   * @var array
   */
  protected static $data = [
    'id' => 'foo',
    'roles' => [
      'authenticated' => 'authenticated',
      'biz' => 'biz',
    ],
    'workflow' => 'foo_bar',
    'transitions' => [
      'foo_to_bar',
      'bar_to_foo',
    ],
    'subject' => 'A test notification',
    'body' => [
      'value' => 'Test message body',
      'format' => 'test_format',
    ],
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->notification = new ContentModerationNotification(static::$data, 'content_moderation_notification');
  }

  /**
   * @covers ::getWorkflowId
   */
  public function testGetWorkflowId() {
    $this
      ->assertEquals('foo_bar', $this->notification
      ->getWorkflowId());
  }

  /**
   * @covers ::getRoleIds
   */
  public function testGetRoleIds() {
    $this
      ->assertEquals(static::$data['roles'], $this->notification
      ->getRoleIds());
  }

  /**
   * @covers ::getTransitions
   */
  public function testGetTransitions() {
    $this
      ->assertEquals(static::$data['transitions'], $this->notification
      ->getTransitions());
  }

  /**
   * @covers ::getSubject
   */
  public function testGetSubject() {
    $this
      ->assertEquals(static::$data['subject'], $this->notification
      ->getSubject());
  }

  /**
   * @covers ::getMessage
   */
  public function testGetMessage() {
    $this
      ->assertEquals(static::$data['body']['value'], $this->notification
      ->getMessage());
  }

  /**
   * @covers ::getMessageFormat
   */
  public function getMessageFormat() {
    $this
      ->assertEquals(static::$data['body']['format'], $this->notification
      ->getMessageFormat());
  }

  /**
   * @covers ::preSave
   */
  public function testPreSave() {
    $data = static::$data;
    $data['roles']['not_set'] = 0;
    $data['transitions']['not_set'] = 0;
    $notification = new ContentModerationNotification($data, 'content_moderation_notification');

    // Mock out some necessary services.
    $container = new ContainerBuilder();
    $entity_manager = $this
      ->prophesize(EntityManagerInterface::class);
    $entity_type = $this
      ->prophesize(EntityTypeInterface::class)
      ->reveal();
    $entity_manager
      ->getDefinition('content_moderation_notification')
      ->willReturn($entity_type);
    $container
      ->set('entity.manager', $entity_manager
      ->reveal());
    \Drupal::setContainer($container);
    $storage = $this
      ->prophesize(EntityStorageInterface::class);
    $query = $this
      ->prophesize(QueryInterface::class);
    $query
      ->execute()
      ->willReturn([]);
    $query
      ->condition('uuid', NULL)
      ->willReturn($query
      ->reveal());
    $storage
      ->getQuery()
      ->willReturn($query
      ->reveal());
    $storage
      ->loadUnchanged('foo')
      ->willReturn($notification);
    $notification
      ->preSave($storage
      ->reveal());
    $this
      ->assertEquals(static::$data['roles'], $notification
      ->getRoleIds());
    $this
      ->assertEquals(static::$data['transitions'], $notification
      ->getTransitions());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentModerationNotificationTest::$data protected static property Test data for the fixture.
ContentModerationNotificationTest::$notification protected property Test fixture.
ContentModerationNotificationTest::getMessageFormat public function @covers ::getMessageFormat
ContentModerationNotificationTest::setUp protected function Overrides UnitTestCase::setUp
ContentModerationNotificationTest::testGetMessage public function @covers ::getMessage
ContentModerationNotificationTest::testGetRoleIds public function @covers ::getRoleIds
ContentModerationNotificationTest::testGetSubject public function @covers ::getSubject
ContentModerationNotificationTest::testGetTransitions public function @covers ::getTransitions
ContentModerationNotificationTest::testGetWorkflowId public function @covers ::getWorkflowId
ContentModerationNotificationTest::testPreSave public function @covers ::preSave
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.