You are here

public function SubscribersTest::testSendMessage in Message Subscribe 8

Same name in this branch
  1. 8 tests/src/Unit/SubscribersTest.php \Drupal\Tests\message_subscribe\Unit\SubscribersTest::testSendMessage()
  2. 8 tests/src/Kernel/SubscribersTest.php \Drupal\Tests\message_subscribe\Kernel\SubscribersTest::testSendMessage()

Test the sendMessage method.

@covers ::sendMessage

File

tests/src/Unit/SubscribersTest.php, line 160

Class

SubscribersTest
Unit tests for the subscribers service.

Namespace

Drupal\Tests\message_subscribe\Unit

Code

public function testSendMessage() {

  // Mock config.
  $config = $this
    ->prophesize(ImmutableConfig::class);
  $config
    ->get('use_queue')
    ->willReturn(FALSE);
  $config
    ->get('notify_own_actions')
    ->willReturn(FALSE);
  $config
    ->get('default_notifiers')
    ->willReturn(FALSE);
  $config
    ->get('debug_mode')
    ->willReturn(TRUE);
  $config_factory = $this
    ->prophesize(ConfigFactoryInterface::class);
  $config_factory
    ->get('message_subscribe.settings')
    ->willReturn($config);
  $this->configFactory = $config_factory
    ->reveal();

  // Mock logger.
  $logger = $this
    ->prophesize(LoggerChannelInterface::class);
  $logger
    ->debug(Argument::any(), Argument::any())
    ->shouldBeCalled();

  // Mock module handler.
  $module_handler = $this
    ->prophesize(ModuleHandlerInterface::class);
  $module_handler
    ->getImplementations(Argument::any())
    ->willReturn([
    'foo',
  ]);
  $module_handler
    ->alter('message_subscribe_get_subscribers', Argument::any(), Argument::any())
    ->shouldBeCalled();
  $module_handler
    ->alter('message_subscribe_message', Argument::any(), Argument::any())
    ->shouldBeCalled();
  $this->moduleHandler = $module_handler
    ->reveal();

  // Mock query.
  $query = $this
    ->prophesize(QueryInterface::class);
  $query
    ->condition(Argument::any(), Argument::any(), Argument::any())
    ->willReturn($query
    ->reveal());

  // User 4 is blocked.
  $query
    ->execute()
    ->willReturn([
    1 => 1,
    2 => 2,
    7 => 7,
  ]);

  // Mock user storage.
  $account = $this
    ->prophesize(UserInterface::class)
    ->reveal();
  $entity_storage = $this
    ->prophesize(EntityStorageInterface::class);
  $entity_storage
    ->load(Argument::any())
    ->willReturn($account);
  $entity_storage
    ->getQuery()
    ->willReturn($query
    ->reveal());

  // Mock entity type manager.
  $entity_type_manager = $this
    ->prophesize(EntityTypeManagerInterface::class);
  $entity_type_manager
    ->getStorage('user')
    ->willReturn($entity_storage
    ->reveal());
  $this->entityTypeManager = $entity_type_manager
    ->reveal();
  $subscribers = $this
    ->getSubscriberService();
  $subscribers
    ->setLoggerChannel($logger
    ->reveal());
  $entity = $this
    ->prophesize(EntityInterface::class);
  $entity
    ->access('view', $account)
    ->willReturn(TRUE);
  $entity
    ->id()
    ->willReturn(42);
  $entity
    ->getEntityTypeId()
    ->willReturn('foo');
  $message = $this
    ->prophesize(Message::class);
  $message
    ->createDuplicate()
    ->willReturn($message
    ->reveal());
  $message
    ->id()
    ->willReturn(22);
  $message
    ->getFieldDefinitions()
    ->willReturn([]);
  $message
    ->setOwnerId(1)
    ->shouldBeCalled();
  $message
    ->setOwnerId(2)
    ->shouldBeCalled();
  $message
    ->setOwnerId(7)
    ->shouldBeCalled();

  // User 4 is blocked.
  $message
    ->setOwnerId(4)
    ->shouldNotBeCalled();
  $subscribers
    ->sendMessage($entity
    ->reveal(), $message
    ->reveal());
}