SmsFrameworkDeliveryReportTest.php in SMS Framework 2.1.x
File
tests/src/Functional/SmsFrameworkDeliveryReportTest.php
View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\sms\Functional;
use Drupal\sms\Message\SmsDeliveryReportInterface;
use Drupal\sms\Message\SmsMessageReportStatus;
use Drupal\sms\Message\SmsMessageResultInterface;
class SmsFrameworkDeliveryReportTest extends SmsFrameworkBrowserTestBase {
public function testDeliveryReports() {
$user = $this
->drupalCreateUser();
$this
->drupalLogin($user);
$test_gateway = $this
->createMemoryGateway([
'skip_queue' => TRUE,
]);
$this->container
->get('router.builder')
->rebuild();
$sms_message = $this
->randomSmsMessage($user
->id())
->setGateway($test_gateway);
$sms_messages = $this->defaultSmsProvider
->send($sms_message);
$result = $sms_messages[0]
->getResult();
$this
->assertTrue($result instanceof SmsMessageResultInterface);
$this
->assertEqual(count($sms_message
->getRecipients()), count($result
->getReports()));
$reports = $result
->getReports();
$first_report = reset($reports);
$message_id = $first_report
->getMessageId();
$this
->assertTrue($first_report instanceof SmsDeliveryReportInterface);
$this
->assertEqual($first_report
->getStatus(), SmsMessageReportStatus::QUEUED);
$url = $test_gateway
->getPushReportUrl()
->setAbsolute()
->toString();
$delivered_time = \Drupal::time()
->getRequestTime();
$delivery_report = <<<EOF
{
"reports":[
{
"message_id":"{<span class="php-variable">$message_id</span>}",
"recipient":"{<span class="php-variable">$first_report</span>
-><span class="php-function-or-constant function member-of-variable">getRecipient</span>()}",
"status":"delivered",
"status_time": {<span class="php-variable">$delivered_time</span>},
"status_message": "status message"
}
]
}
EOF;
$client = $this
->getSession()
->getDriver()
->getClient();
$client
->request('post', $url, [
'delivery_report' => $delivery_report,
]);
$this
->assertText('custom response content');
\Drupal::state()
->resetCache();
$second_report = $this
->getTestMessageReport($message_id, $test_gateway);
$this
->assertTrue($second_report instanceof SmsDeliveryReportInterface);
$this
->assertEqual("status message", $second_report
->getStatusMessage());
$this
->assertEqual($delivered_time, $second_report
->getTimeDelivered());
$this
->assertEqual($message_id, $second_report
->getMessageId());
}
}