View source
<?php
namespace Drupal\Tests\message_subscribe\Unit;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Queue\QueueFactory;
use Drupal\flag\FlagInterface;
use Drupal\flag\FlagServiceInterface;
use Drupal\message\Entity\Message;
use Drupal\message_notify\MessageNotifier;
use Drupal\message_subscribe\Subscribers;
use Drupal\Tests\UnitTestCase;
use Drupal\user\UserInterface;
use Prophecy\Argument;
class SubscribersTest extends UnitTestCase {
protected $flagService;
protected $configFactory;
protected $entityTypeManager;
protected $messageNotifier;
protected $moduleHandler;
protected $queue;
public function setUp() {
parent::setUp();
require __DIR__ . '/../fixture_foo.module.php';
$this->flagService = $this
->prophesize(FlagServiceInterface::class)
->reveal();
$this->configFactory = $this
->prophesize(ConfigFactoryInterface::class)
->reveal();
$this->entityTypeManager = $this
->prophesize(EntityTypeManagerInterface::class)
->reveal();
$this->messageNotifier = $this
->prophesize(MessageNotifier::class)
->reveal();
$this->moduleHandler = $this
->prophesize(ModuleHandlerInterface::class)
->reveal();
$this->queue = $this
->prophesize(QueueFactory::class)
->reveal();
}
protected function getSubscriberService() {
return new Subscribers($this->flagService, $this->configFactory, $this->entityTypeManager, $this->messageNotifier, $this->moduleHandler, $this->queue);
}
public function testGetFlags() {
$config = $this
->prophesize(ImmutableConfig::class);
$config
->get('flag_prefix')
->willReturn('blah');
$config
->get('debug_mode')
->willReturn(FALSE);
$config_factory = $this
->prophesize(ConfigFactoryInterface::class);
$config_factory
->get('message_subscribe.settings')
->willReturn($config);
$this->configFactory = $config_factory
->reveal();
$flag_service = $this
->prophesize(FlagServiceInterface::class);
$flag_service
->getAllFlags(NULL, NULL, NULL)
->willReturn([]);
$this->flagService = $flag_service
->reveal();
$subscribers = $this
->getSubscriberService();
$this
->assertEquals([], $subscribers
->getFlags());
$flag = $this
->prophesize(FlagInterface::class)
->reveal();
$flag_service = $this
->prophesize(FlagServiceInterface::class);
$flag_service
->getAllFlags(NULL, NULL, NULL)
->willReturn([
'foo' => $flag,
'bar' => $flag,
]);
$this->flagService = $flag_service
->reveal();
$subscribers = $this
->getSubscriberService();
$this
->assertEquals([], $subscribers
->getFlags());
$flag_service = $this
->prophesize(FlagServiceInterface::class);
$flag_service
->getAllFlags(NULL, NULL, NULL)
->willReturn([
'foo' => $flag,
'bar' => $flag,
'blah_foo' => $flag,
]);
$this->flagService = $flag_service
->reveal();
$subscribers = $this
->getSubscriberService();
$this
->assertEquals([
'blah_foo' => $flag,
], $subscribers
->getFlags());
}
public function testSendMessage() {
$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();
$logger = $this
->prophesize(LoggerChannelInterface::class);
$logger
->debug(Argument::any(), Argument::any())
->shouldBeCalled();
$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();
$query = $this
->prophesize(QueryInterface::class);
$query
->condition(Argument::any(), Argument::any(), Argument::any())
->willReturn($query
->reveal());
$query
->execute()
->willReturn([
1 => 1,
2 => 2,
7 => 7,
]);
$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());
$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();
$message
->setOwnerId(4)
->shouldNotBeCalled();
$subscribers
->sendMessage($entity
->reveal(), $message
->reveal());
}
}