View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\sms\Kernel;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\sms\Direction;
use Drupal\sms\Entity\SmsDeliveryReport;
use Drupal\sms\Entity\SmsMessageInterface;
use Drupal\sms\Entity\SmsMessageResult;
use Drupal\sms\Message\SmsMessage as StandardSmsMessage;
use Drupal\sms\Entity\SmsMessage;
use Drupal\sms\Message\SmsMessageResultInterface;
use Drupal\Tests\sms\Functional\SmsFrameworkMessageTestTrait;
use Drupal\Tests\sms\Functional\SmsFrameworkTestTrait;
use Drupal\user\Entity\User;
class SmsFrameworkMessageEntityTest extends SmsFrameworkKernelBase {
use SmsFrameworkTestTrait;
use SmsFrameworkMessageTestTrait {
testUid as originalUid;
}
public static $modules = [
'sms',
'sms_test_gateway',
'telephone',
'dynamic_entity_reference',
'user',
'sms_test_time',
'entity_test',
];
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('sms');
$this
->installEntitySchema('sms_result');
$this
->installEntitySchema('sms_report');
$this
->installEntitySchema('user');
$this
->installEntitySchema('entity_test');
}
protected function createSmsMessage(array $values = []) {
return SmsMessage::create($values);
}
public function testMessageEmpty() {
$sms_message = $this
->createSmsMessage();
$this
->assertTrue(in_array('message', $sms_message
->validate()
->getFieldNames()));
}
public function testUid() {
User::create([
'uid' => 22,
'name' => 'user',
])
->save();
$this
->originalUid();
}
public function testSenderNameWithSenderEntity() {
$sender_name = $this
->randomMachineName();
$sender = EntityTest::create()
->setName($this
->randomMachineName());
$sender
->save();
$sms_message1 = $this
->createSmsMessage();
$sms_message1
->setSender($sender_name);
$sms_message1
->setSenderEntity($sender);
$this
->assertEquals($sender_name, $sms_message1
->getSender());
$sms_message2 = $this
->createSmsMessage();
$sms_message2
->setSenderEntity($sender);
$this
->assertEquals($sender
->label(), $sms_message2
->getSender());
}
public function testDirectionEntityValidation() {
$sms_message1 = $this
->createSmsMessage();
$this
->assertTrue(in_array('direction', $sms_message1
->validate()
->getFieldNames()));
}
public function testGateway() {
$sms_message1 = $this
->createSmsMessage();
$this
->assertTrue(in_array('gateway', $sms_message1
->validate()
->getFieldNames()));
$gateway = $this
->createMemoryGateway();
$sms_message2 = $this
->createSmsMessage();
$sms_message2
->setGateway($gateway);
$this
->assertEquals($gateway, $sms_message2
->getGateway());
}
public function testSenderEntity() {
$sms_message1 = $this
->createSmsMessage();
$this
->assertEquals(NULL, $sms_message1
->getSenderEntity());
$sender = EntityTest::create();
$sender
->save();
$sms_message2 = $this
->createSmsMessage();
$sms_message2
->setSenderEntity($sender);
$this
->assertEquals($sender
->id(), $sms_message2
->getSenderEntity()
->id());
}
public function testRecipientEntity() {
$sms_message1 = $this
->createSmsMessage();
$this
->assertEquals(NULL, $sms_message1
->getRecipientEntity());
$sender = EntityTest::create();
$sender
->save();
$sms_message2 = $this
->createSmsMessage();
$sms_message2
->setRecipientEntity($sender);
$this
->assertEquals($sender
->id(), $sms_message2
->getRecipientEntity()
->id());
}
public function testQueued() {
$sms_message1 = $this
->createSmsMessage();
$this
->assertFalse($sms_message1
->isQueued());
$sms_message2 = $this
->createSmsMessage();
$sms_message2
->setQueued(TRUE);
$this
->assertTrue($sms_message2
->isQueued());
}
public function testCreatedTime() {
$sms_message = $this
->createSmsMessage();
$this
->markTestSkipped();
$this
->assertEquals('877098600', $sms_message
->getCreatedTime());
}
public function testSendTime() {
$sms_message1 = $this
->createSmsMessage();
$this
->markTestSkipped();
$this
->assertEquals('877098600', $sms_message1
->getSendTime());
$time = (new \DateTime('+7 days'))
->getTimestamp();
$sms_message2 = $this
->createSmsMessage();
$sms_message2
->setSendTime($time);
$this
->assertEquals($time, $sms_message2
->getSendTime());
}
public function testProcessedTime() {
$sms_message1 = $this
->createSmsMessage();
$this
->assertEquals(NULL, $sms_message1
->getProcessedTime());
$time = (new DrupalDateTime('+7 days'))
->format('U');
$sms_message2 = $this
->createSmsMessage();
$sms_message2
->setProcessedTime($time);
$this
->assertEquals($time, $sms_message2
->getProcessedTime());
}
public function testChunkByRecipientsEntity() {
$sms_message = $this
->createSmsMessage();
$sms_message
->addRecipients([
'100',
'200',
]);
$sms_messages = $sms_message
->chunkByRecipients(1);
$this
->assertTrue($sms_messages[0]
->isNew());
$this
->assertTrue($sms_messages[1]
->isNew());
}
public function testConvertToEntityFromStandardSmsMessage() {
$user = User::create([
'uid' => 1,
'name' => 'user',
]);
$user
->save();
$gateway = $this
->createMemoryGateway();
$sender_number = $this
->randomPhoneNumbers(1);
$original = new StandardSmsMessage('', [], '', [], NULL);
$original
->setAutomated(TRUE)
->setSender($this
->randomMachineName())
->setSenderNumber($sender_number[0])
->addRecipients([
'123123123',
'456456456',
])
->setMessage($this
->randomMachineName())
->setUid($user
->id())
->setGateway($gateway)
->setOption('foo', $this
->randomMachineName())
->setOption('bar', $this
->randomMachineName())
->setResult($this
->createMessageResult($original));
$sms_message = SmsMessage::convertFromSmsMessage($original);
$this
->assertEquals($original
->isAutomated(), $sms_message
->isAutomated());
$this
->assertEquals($original
->getSender(), $sms_message
->getSender());
$this
->assertEquals($original
->getSenderNumber(), $sms_message
->getSenderNumber());
$this
->assertEquals($original
->getRecipients(), $sms_message
->getRecipients());
$this
->assertEquals($original
->getMessage(), $sms_message
->getMessage());
$this
->assertEquals($user
->id(), $sms_message
->getSenderEntity()
->id());
$this
->assertEquals($original
->getOption('foo'), $sms_message
->getOption('foo'));
$this
->assertEquals($original
->getOption('bar'), $sms_message
->getOption('bar'));
$this
->assertEquals($original
->getGateway(), $sms_message
->getGateway());
$this
->assertEquals($original
->getResult()
->getErrorMessage(), $sms_message
->getResult()
->getErrorMessage());
$this
->assertEquals(count($original
->getReports()), count($sms_message
->getReports()));
$this
->assertEquals($original
->getReport('123123123')
->getRecipient(), $sms_message
->getReport('123123123')
->getRecipient());
$this
->assertEquals($original
->getReport('456456456')
->getRecipient(), $sms_message
->getReport('456456456')
->getRecipient());
}
public function testConvertToEntityFromEntitySmsMessage() {
$recipient = EntityTest::create()
->setName($this
->randomMachineName());
$recipient
->save();
$original = SmsMessage::create();
$original
->setMessage($this
->randomMachineName());
$original
->setRecipientEntity($recipient);
$sms_message = SmsMessage::convertFromSmsMessage($original);
$this
->assertEquals($original
->getMessage(), $sms_message
->getMessage());
$this
->assertEquals($original
->getRecipientEntity(), $sms_message
->getRecipientEntity());
}
public function testSaveAndRetrieveSmsMessage() {
$sms_message = SmsMessage::create()
->setMessage($this
->randomMachineName(100))
->setSender($this
->randomMachineName())
->setDirection(Direction::OUTGOING)
->addRecipients([
'1234567890',
'2345678901',
]);
$sms_message
->setResult($this
->createMessageResult($sms_message))
->save();
$saved = SmsMessage::load($sms_message
->id());
$this
->assertEquals($sms_message
->getMessage(), $saved
->getMessage());
$this
->assertEquals($sms_message
->getSender(), $saved
->getSender());
$this
->assertEquals($sms_message
->getDirection(), $saved
->getDirection());
$this
->assertEquals($sms_message
->getRecipients(), $saved
->getRecipients());
$this
->assertEquals($sms_message
->getResult()
->getErrorMessage(), $saved
->getResult()
->getErrorMessage());
$this
->assertEquals(count($sms_message
->getReports()), count($saved
->getReports()));
$this
->assertEquals(2, count($sms_message
->getReports()));
}
public function testGetResultNoResult() {
$sms_message = SmsMessage::create();
$this
->assertNull($sms_message
->getResult());
}
public function testCascadeDelete() {
$sms_message = SmsMessage::create()
->setMessage($this
->getRandomGenerator()
->paragraphs())
->setGateway($this
->createMemoryGateway())
->addRecipients($this
->randomPhoneNumbers())
->setSender($this
->randomMachineName());
$this
->assertNull($sms_message
->getResult());
$sms_result = $this
->createMessageResult($sms_message);
$sms_message
->setResult($sms_result)
->save();
$sms_reports = $sms_result
->getReports();
$this
->assertInstanceOf(SmsMessageResultInterface::class, $sms_message
->getResult());
$this
->assertInstanceOf(SmsMessageInterface::class, SmsMessage::load($sms_message
->id()));
$this
->assertEquals(count($sms_reports), count(SmsDeliveryReport::loadMultiple()));
$sms_message
->delete();
$this
->assertNull(SmsMessage::load($sms_message
->id()));
$this
->assertEquals([], SmsMessageResult::loadMultiple());
$this
->assertEquals([], SmsDeliveryReport::loadMultiple());
}
}