View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\sms\Kernel;
use Drupal\sms\Entity\SmsMessage;
use Drupal\sms\Direction;
class SmsFrameworkQueueTest extends SmsFrameworkKernelBase {
public static $modules = [
'sms',
'sms_test_gateway',
'field',
'telephone',
'dynamic_entity_reference',
];
protected $smsProvider;
protected $smsQueueProcessor;
protected $gateway;
protected $cronService;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('sms');
$this
->installEntitySchema('sms_result');
$this
->installEntitySchema('sms_report');
$this->gateway = $this
->createMemoryGateway();
$this->smsProvider = $this->container
->get('sms.provider');
$this
->setFallbackGateway($this->gateway);
$this->smsQueueProcessor = $this->container
->get('sms.queue');
$this->cronService = $this->container
->get('cron');
}
public function testProcessUnqueued() {
$sms_message = $this
->createSmsMessage();
$result = $this->smsProvider
->queue($sms_message);
$id = $result[0]
->id();
$this->smsQueueProcessor
->processUnqueued();
$sms_message_saved = SmsMessage::load($id);
$this
->assertTrue($sms_message_saved
->isQueued(), 'SMS message is queued.');
$this
->assertEquals(1, \Drupal::queue('sms.messages')
->numberOfItems(), 'SMS message processor queue item created.');
}
public function testQueueIncoming() {
$sms_message = $this
->createSmsMessage()
->setDirection(Direction::INCOMING)
->addRecipients($this
->randomPhoneNumbers())
->setGateway($this->gateway);
$sms_message
->setResult($this
->createMessageResult($sms_message));
$this->smsProvider
->queue($sms_message);
$this
->assertEquals(0, count($this
->getTestMessages($this->gateway)), 'Message not received yet.');
$this->cronService
->run();
$this
->assertEquals($sms_message
->getMessage(), sms_test_gateway_get_incoming()['message'], 'Message was received.');
}
public function testQueueOutgoing() {
$sms_message = $this
->createSmsMessage()
->setDirection(Direction::OUTGOING);
$this->smsProvider
->queue($sms_message);
$this
->assertEquals(0, count($this
->getTestMessages($this->gateway)), 'Message not sent yet.');
$this->cronService
->run();
$this
->assertEquals(1, count($this
->getTestMessages($this->gateway)), 'Message was sent.');
}
public function testQueueDelayed() {
$sms_message = $this
->createSmsMessage()
->setSendTime(\Drupal::time()
->getRequestTime() + 9999);
$this->smsProvider
->queue($sms_message);
$this->cronService
->run();
$this
->assertEquals(0, count($this
->getTestMessages($this->gateway)), 'Message not sent yet.');
}
public function testQueueNotDelayedScheduleAware() {
$gateway = $this
->createMemoryGateway([
'plugin' => 'memory_schedule_aware',
]);
$sms_message = $this
->createSmsMessage()
->setSendTime(\Drupal::time()
->getRequestTime() + 9999)
->setGateway($gateway);
$this->smsProvider
->queue($sms_message);
$this->cronService
->run();
$this
->assertEquals(1, count($this
->getTestMessages($gateway)), 'Message sent.');
}
public function testRetentionImmediateDelete() {
$this->gateway
->setRetentionDuration(Direction::OUTGOING, 0)
->save();
$sms_message = $this
->createSmsMessage();
$this->smsProvider
->queue($sms_message);
$this->cronService
->run();
$this
->assertEquals(1, count($this
->getTestMessages($this->gateway)), 'One message was sent.');
$this
->assertEquals(0, count(SmsMessage::loadMultiple()), 'There are no SMS entities in storage.');
}
public function testRetentionPersist() {
$this->gateway
->setRetentionDuration(Direction::OUTGOING, 9999)
->save();
$sms_message = $this
->createSmsMessage();
$this->smsProvider
->queue($sms_message);
$this->cronService
->run();
$sms_messages = SmsMessage::loadMultiple();
$sms_message_new = reset($sms_messages);
$this
->assertEquals(1, count($this
->getTestMessages($this->gateway)), 'One message was sent.');
$this
->assertEquals(1, count($sms_messages), 'There are SMS entities in storage.');
$this
->assertEquals(\Drupal::time()
->getRequestTime(), $sms_message_new
->getProcessedTime());
$this
->assertEquals(FALSE, $sms_message_new
->isQueued());
}
public function testRetentionUnlimited() {
$this->gateway
->setRetentionDuration(Direction::OUTGOING, -1)
->save();
$this
->createSmsMessage()
->setGateway($this->gateway)
->setQueued(FALSE)
->setProcessedTime(1)
->save();
$this->cronService
->run();
$this
->assertEquals(1, count(SmsMessage::loadMultiple()), 'There are SMS entities in storage.');
}
protected function createSmsMessage(array $values = []) {
return SmsMessage::create($values)
->setDirection(Direction::OUTGOING)
->setMessage($this
->randomString())
->addRecipients($this
->randomPhoneNumbers(1));
}
}