You are here

public function SmsFrameworkDeliveryReportTest::testDeliveryReports in SMS Framework 2.1.x

Same name and namespace in other branches
  1. 8 tests/src/Functional/SmsFrameworkDeliveryReportTest.php \Drupal\Tests\sms\Functional\SmsFrameworkDeliveryReportTest::testDeliveryReports()
  2. 2.x tests/src/Functional/SmsFrameworkDeliveryReportTest.php \Drupal\Tests\sms\Functional\SmsFrameworkDeliveryReportTest::testDeliveryReports()

Tests delivery reports integration.

File

tests/src/Functional/SmsFrameworkDeliveryReportTest.php, line 21

Class

SmsFrameworkDeliveryReportTest
Integration tests for delivery reports.

Namespace

Drupal\Tests\sms\Functional

Code

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();

  /** @var \Drupal\sms\Message\SmsDeliveryReportInterface $first_report */
  $first_report = reset($reports);
  $message_id = $first_report
    ->getMessageId();
  $this
    ->assertTrue($first_report instanceof SmsDeliveryReportInterface);
  $this
    ->assertEqual($first_report
    ->getStatus(), SmsMessageReportStatus::QUEUED);

  // Get the delivery reports url and simulate push delivery report.
  $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>
  -&gt;<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;

  /** @var \Symfony\Component\BrowserKit\Client $client */
  $client = $this
    ->getSession()
    ->getDriver()
    ->getClient();
  $client
    ->request('post', $url, [
    'delivery_report' => $delivery_report,
  ]);
  $this
    ->assertText('custom response content');
  \Drupal::state()
    ->resetCache();

  // Get the stored report and verify that it was properly parsed.
  $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());
}