View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\sms\Kernel;
use Drupal\sms\Exception\SmsDirectionException;
use Drupal\sms\Exception\SmsException;
use Drupal\sms\Exception\RecipientRouteException;
use Drupal\sms\Message\SmsMessage as StandardSmsMessage;
use Drupal\sms\Message\SmsMessageInterface as StandardSmsMessageInterface;
use Drupal\sms\Entity\SmsMessage;
use Drupal\sms\Entity\SmsMessageInterface;
use Drupal\sms\Entity\SmsGateway;
use Drupal\sms\Direction;
use Drupal\sms\Event\SmsEvents;
use Drupal\sms\Message\SmsMessageResultInterface;
class SmsFrameworkProviderTest extends SmsFrameworkKernelBase {
public static $modules = [
'sms',
'sms_test',
'sms_test_gateway',
'field',
'telephone',
'dynamic_entity_reference',
];
protected $smsStorage;
protected $smsProvider;
protected $gateway;
protected $incomingGateway;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('sms');
$this
->installEntitySchema('sms_result');
$this
->installEntitySchema('sms_report');
$this->gateway = $this
->createMemoryGateway();
$this->incomingGateway = $this
->createMemoryGateway([
'plugin' => 'incoming',
]);
$this->smsStorage = $this->container
->get('entity_type.manager')
->getStorage('sms');
$this->smsProvider = $this->container
->get('sms.provider');
$this
->setFallbackGateway($this->gateway);
}
public function testSend() {
$message = $this
->createSmsMessage()
->addRecipients($this
->randomPhoneNumbers());
$sms_messages = $this->smsProvider
->send($message);
$this
->assertEquals(1, count($sms_messages), 'Return value contains 1 item.');
$this
->assertTrue($sms_messages[0] instanceof StandardSmsMessageInterface, 'Return value is a SMS message.');
$this
->assertEquals(1, count($this
->getTestMessages($this->gateway)));
$this
->assertTrue($sms_messages[0]
->getResult() instanceof SmsMessageResultInterface);
}
public function testSendNoDirection() {
$sms_message = SmsMessage::create()
->setMessage($this
->randomString())
->addRecipients($this
->randomPhoneNumbers())
->setGateway($this->gateway);
$this->smsProvider
->send($sms_message);
$messages = $this
->getTestMessages($this->gateway);
$this
->assertEquals(1, count($messages), 'Message was added to outgoing queue without direction being explicitly set');
$this
->assertEquals(Direction::OUTGOING, $messages[0]
->getDirection(), 'Message direction set to outgoing.');
}
public function testIncoming() {
$message = $this
->randomString();
$sms_message = SmsMessage::create()
->setDirection(Direction::INCOMING)
->setMessage($message)
->setGateway($this->gateway);
$sms_message
->setResult($this
->createMessageResult($sms_message));
$sms_messages = $this->smsProvider
->incoming($sms_message);
$this
->assertEquals($message, sms_test_gateway_get_incoming()['message'], 'Message was received.');
$this
->assertEquals(1, count($sms_messages), 'Return value contains 1 item.');
$this
->assertTrue($sms_messages[0] instanceof StandardSmsMessageInterface, 'Return value is a SMS message.');
$this
->assertTrue($sms_messages[0]
->getResult() instanceof SmsMessageResultInterface);
}
public function testIncomingNoDirection() {
$sms_message = SmsMessage::create()
->setMessage($this
->randomString())
->addRecipients($this
->randomPhoneNumbers())
->setGateway($this->incomingGateway);
$sms_message
->setResult($this
->createMessageResult($sms_message));
$this->smsProvider
->incoming($sms_message);
$messages = $this
->getIncomingMessages($this->incomingGateway);
$this
->assertEquals(1, count($messages), 'Message was added to incoming queue without direction being explicitly set');
$this
->assertEquals(Direction::INCOMING, $messages[0]
->getDirection(), 'Message direction set to incoming.');
}
public function testIncomingNoRecipients() {
$this->incomingGateway
->setSkipQueue(TRUE)
->save();
$sms_message = SmsMessage::create()
->setMessage($this
->randomString())
->setGateway($this->incomingGateway)
->setDirection(Direction::INCOMING);
$sms_message
->setResult($this
->createMessageResult($sms_message));
$this->smsProvider
->queue($sms_message);
$messages = $this
->getIncomingMessages($this->incomingGateway);
$this
->assertEquals(1, count($messages), 'Message was added to incoming queue without recipients.');
}
public function testNoSendNoRecipients() {
$sms_message = SmsMessage::create()
->setDirection(Direction::OUTGOING)
->setMessage($this
->randomString());
$this
->expectException(RecipientRouteException::class);
$this
->expectExceptionMessage('There are no recipients');
$this->smsProvider
->send($sms_message);
$this
->assertEquals(0, count($this
->getTestMessages($this->gateway)));
}
public function testNoSendNoMessage() {
$sms_message = SmsMessage::create()
->setDirection(Direction::OUTGOING)
->addRecipients($this
->randomPhoneNumbers());
$this
->expectException(SmsException::class);
$this
->expectExceptionMessage('Can not queue SMS message because there are 1 validation error(s): [message]: This value should not be null.');
$this->smsProvider
->queue($sms_message);
}
public function testQueueNoDirection() {
$sms_message = SmsMessage::create()
->setMessage($this
->randomString())
->addRecipients($this
->randomPhoneNumbers());
$this
->expectException(SmsDirectionException::class);
$this
->expectExceptionMessage('Missing direction for message.');
$this->smsProvider
->queue($sms_message);
}
public function testSendNoFallbackGateway() {
$this
->setFallbackGateway(NULL);
$this
->expectException(RecipientRouteException::class);
$message = $this
->createSmsMessage()
->addRecipients($this
->randomPhoneNumbers());
$this->smsProvider
->send($message);
}
public function testQueueBasic() {
$sms_message = $this
->createSmsMessage()
->addRecipients($this
->randomPhoneNumbers());
$return = $this->smsProvider
->queue($sms_message);
$this
->assertEquals(1, count(SmsMessage::loadMultiple()), 'SMS message saved.');
$this
->assertEquals(1, count($return));
$this
->assertTrue($return[0] instanceof SmsMessageInterface);
}
public function testQueueNoFallbackGateway() {
$this
->setFallbackGateway(NULL);
$this
->expectException(RecipientRouteException::class);
$message = $this
->createSmsMessage()
->addRecipients($this
->randomPhoneNumbers());
$this->smsProvider
->queue($message);
}
public function testSkipQueue() {
$this->gateway
->setSkipQueue(TRUE)
->save();
$sms_message = $this
->createSmsMessage()
->addRecipients($this
->randomPhoneNumbers());
$this->smsProvider
->queue($sms_message);
$this
->assertEquals(1, count($this
->getTestMessages($this->gateway)));
}
public function testQueueIn() {
$sms_message = new StandardSmsMessage();
$sms_message
->addRecipients($this
->randomPhoneNumbers())
->setMessage($this
->randomString())
->setDirection(Direction::INCOMING)
->setGateway($this->gateway);
$sms_message
->setResult($this
->createMessageResult($sms_message));
$sms_messages = $this->smsStorage
->loadByProperties([
'direction' => Direction::INCOMING,
]);
$this
->assertEquals(0, count($sms_messages), 'There is zero SMS message in the incoming queue.');
$this->smsProvider
->queue($sms_message);
$sms_messages = $this->smsStorage
->loadByProperties([
'direction' => Direction::INCOMING,
]);
$this
->assertEquals(1, count($sms_messages), 'There is one SMS message in the incoming queue.');
$sms_message_loaded = reset($sms_messages);
$this
->assertEquals(Direction::INCOMING, $sms_message_loaded
->getDirection());
}
public function testQueueOut() {
$sms_message = new StandardSmsMessage();
$sms_message
->addRecipients($this
->randomPhoneNumbers())
->setMessage($this
->randomString())
->setDirection(Direction::OUTGOING);
$sms_messages = $this->smsStorage
->loadByProperties([
'direction' => Direction::OUTGOING,
]);
$this
->assertEquals(0, count($sms_messages), 'There is zero SMS message in the outgoing queue.');
$this->smsProvider
->queue($sms_message);
$sms_messages = $this->smsStorage
->loadByProperties([
'direction' => Direction::OUTGOING,
]);
$this
->assertEquals(1, count($sms_messages), 'There is one SMS message in the outgoing queue.');
$sms_message_loaded = reset($sms_messages);
$this
->assertEquals(Direction::OUTGOING, $sms_message_loaded
->getDirection());
}
public function testQueueOutSkipQueue() {
$this->gateway
->setSkipQueue(TRUE)
->save();
$sms_message = new StandardSmsMessage('', [], '', [], NULL);
$sms_message
->addRecipients($this
->randomPhoneNumbers())
->setMessage($this
->randomString())
->setDirection(Direction::OUTGOING);
$this->smsProvider
->queue($sms_message);
$this
->assertEquals(1, count($this
->getTestMessages($this->gateway)), 'One standard SMS send skipped queue.');
}
public function testNoRecipients() {
$this
->expectException(RecipientRouteException::class);
$this
->expectExceptionMessage('There are no recipients.');
$sms_message = SmsMessage::create()
->setDirection(Direction::OUTGOING)
->setMessage($this
->randomString());
$this->smsProvider
->send($sms_message);
}
public function testChunking() {
$gateway_chunked = SmsGateway::create([
'plugin' => 'memory_chunked',
'id' => 'memory_chunked',
'settings' => [
'gateway_id' => 'memory_chunked',
],
]);
$gateway_chunked
->enable()
->save();
$sms_message = $this
->createSmsMessage()
->setGateway($gateway_chunked)
->addRecipients([
'123123123',
'456456456',
'789789789',
]);
$return = $this->smsProvider
->queue($sms_message);
$this
->assertEquals(2, count(SmsMessage::loadMultiple()), 'One SMS message has been split into two.');
$this
->assertEquals(2, count($return), 'Provider queue method returned two messages.');
}
public function testNoChunking() {
$sms_message = $this
->createSmsMessage()
->setGateway($this->gateway)
->addRecipients($this
->randomPhoneNumbers());
$this->smsProvider
->queue($sms_message);
$this
->assertEquals(1, count(SmsMessage::loadMultiple()), 'SMS message has not been split.');
}
public function testIncomingNotChunked() {
$this->incomingGateway
->setSkipQueue(TRUE)
->save();
$message = (new StandardSmsMessage())
->setMessage($this
->randomString())
->addRecipients($this
->randomPhoneNumbers())
->setDirection(Direction::INCOMING)
->setGateway($this->incomingGateway);
$message
->setResult($this
->createMessageResult($message));
$this->smsProvider
->queue($message);
$incoming_messages = $this
->getIncomingMessages($this->incomingGateway);
$this
->assertEquals(1, count($incoming_messages), 'There is one incoming message.');
}
public function testEventsQueueOutgoing() {
$sms_message = $this
->createSmsMessage()
->setGateway($this->gateway)
->setDirection(Direction::OUTGOING)
->addRecipients($this
->randomPhoneNumbers());
$this->smsProvider
->queue($sms_message);
$expected = [
SmsEvents::MESSAGE_PRE_PROCESS,
SmsEvents::MESSAGE_QUEUE_PRE_PROCESS,
SmsEvents::MESSAGE_QUEUE_POST_PROCESS,
];
$execution_order = \Drupal::state()
->get('sms_test_event_subscriber__execution_order', []);
$this
->assertEquals($expected, $execution_order);
$this->container
->get('cron')
->run();
$expected[] = SmsEvents::MESSAGE_OUTGOING_PRE_PROCESS;
$expected[] = SmsEvents::MESSAGE_OUTGOING_POST_PROCESS;
$expected[] = SmsEvents::MESSAGE_POST_PROCESS;
$execution_order = \Drupal::state()
->get('sms_test_event_subscriber__execution_order', []);
$this
->assertEquals($expected, $execution_order);
}
public function testEventsQueueOutgoingSkipQueue() {
$this->gateway
->setSkipQueue(TRUE)
->save();
$sms_message = $this
->createSmsMessage()
->setGateway($this->gateway)
->setDirection(Direction::OUTGOING)
->addRecipients($this
->randomPhoneNumbers());
$this->smsProvider
->queue($sms_message);
$expected = [
SmsEvents::MESSAGE_PRE_PROCESS,
SmsEvents::MESSAGE_QUEUE_PRE_PROCESS,
SmsEvents::MESSAGE_OUTGOING_PRE_PROCESS,
SmsEvents::MESSAGE_OUTGOING_POST_PROCESS,
SmsEvents::MESSAGE_POST_PROCESS,
SmsEvents::MESSAGE_QUEUE_POST_PROCESS,
];
$execution_order = \Drupal::state()
->get('sms_test_event_subscriber__execution_order', []);
$this
->assertEquals($expected, $execution_order);
}
public function testEventsQueueIncoming() {
$sms_message = $this
->createSmsMessage()
->setGateway($this->gateway)
->setDirection(Direction::INCOMING)
->addRecipients($this
->randomPhoneNumbers());
$sms_message
->setResult($this
->createMessageResult($sms_message));
$this->smsProvider
->queue($sms_message);
$expected = [
SmsEvents::MESSAGE_PRE_PROCESS,
SmsEvents::MESSAGE_QUEUE_PRE_PROCESS,
SmsEvents::MESSAGE_QUEUE_POST_PROCESS,
];
$execution_order = \Drupal::state()
->get('sms_test_event_subscriber__execution_order', []);
$this
->assertEquals($expected, $execution_order);
$this->container
->get('cron')
->run();
$expected[] = SmsEvents::MESSAGE_INCOMING_PRE_PROCESS;
$expected[] = 'Drupal\\sms_test_gateway\\Plugin\\SmsGateway\\Memory::incomingEvent';
$expected[] = SmsEvents::MESSAGE_INCOMING_POST_PROCESS;
$expected[] = SmsEvents::MESSAGE_POST_PROCESS;
$execution_order = \Drupal::state()
->get('sms_test_event_subscriber__execution_order', []);
$this
->assertEquals($expected, $execution_order);
}
public function testEventsQueueIncomingSkipQueue() {
$this->gateway
->setSkipQueue(TRUE)
->save();
$sms_message = $this
->createSmsMessage()
->setGateway($this->gateway)
->setDirection(Direction::INCOMING)
->addRecipients($this
->randomPhoneNumbers());
$sms_message
->setResult($this
->createMessageResult($sms_message));
$this->smsProvider
->queue($sms_message);
$expected = [
SmsEvents::MESSAGE_PRE_PROCESS,
SmsEvents::MESSAGE_QUEUE_PRE_PROCESS,
SmsEvents::MESSAGE_INCOMING_PRE_PROCESS,
'Drupal\\sms_test_gateway\\Plugin\\SmsGateway\\Memory::incomingEvent',
SmsEvents::MESSAGE_INCOMING_POST_PROCESS,
SmsEvents::MESSAGE_POST_PROCESS,
SmsEvents::MESSAGE_QUEUE_POST_PROCESS,
];
$execution_order = \Drupal::state()
->get('sms_test_event_subscriber__execution_order', []);
$this
->assertEquals($expected, $execution_order);
}
public function testEventsOutgoing() {
$sms_message = $this
->createSmsMessage()
->setGateway($this->gateway)
->setDirection(Direction::OUTGOING)
->addRecipients($this
->randomPhoneNumbers());
$this->smsProvider
->send($sms_message);
$expected = [
SmsEvents::MESSAGE_PRE_PROCESS,
SmsEvents::MESSAGE_OUTGOING_PRE_PROCESS,
SmsEvents::MESSAGE_OUTGOING_POST_PROCESS,
SmsEvents::MESSAGE_POST_PROCESS,
];
$execution_order = \Drupal::state()
->get('sms_test_event_subscriber__execution_order', []);
$this
->assertEquals($expected, $execution_order);
}
public function testEventsIncoming() {
$sms_message = $this
->createSmsMessage()
->setGateway($this->gateway)
->setDirection(Direction::INCOMING)
->addRecipients($this
->randomPhoneNumbers());
$sms_message
->setResult($this
->createMessageResult($sms_message));
$this->smsProvider
->incoming($sms_message);
$expected = [
SmsEvents::MESSAGE_PRE_PROCESS,
SmsEvents::MESSAGE_INCOMING_PRE_PROCESS,
'Drupal\\sms_test_gateway\\Plugin\\SmsGateway\\Memory::incomingEvent',
SmsEvents::MESSAGE_INCOMING_POST_PROCESS,
SmsEvents::MESSAGE_POST_PROCESS,
];
$execution_order = \Drupal::state()
->get('sms_test_event_subscriber__execution_order', []);
$this
->assertEquals($expected, $execution_order);
}
protected function createSmsMessage(array $values = []) {
return SmsMessage::create($values)
->setDirection(Direction::OUTGOING)
->setMessage($this
->randomString());
}
}