You are here

public function SmsFrameworkDeliveryReportUpdateTest::testDeliveryReportUpdate in SMS Framework 8

Same name and namespace in other branches
  1. 2.x tests/src/Kernel/SmsFrameworkDeliveryReportUpdateTest.php \Drupal\Tests\sms\Kernel\SmsFrameworkDeliveryReportUpdateTest::testDeliveryReportUpdate()
  2. 2.1.x tests/src/Kernel/SmsFrameworkDeliveryReportUpdateTest.php \Drupal\Tests\sms\Kernel\SmsFrameworkDeliveryReportUpdateTest::testDeliveryReportUpdate()

Tests that delivery reports are updated after initial sending.

File

tests/src/Kernel/SmsFrameworkDeliveryReportUpdateTest.php, line 58

Class

SmsFrameworkDeliveryReportUpdateTest
Tests the update of SMS Delivery report entities.

Namespace

Drupal\Tests\sms\Kernel

Code

public function testDeliveryReportUpdate() {
  $user = User::create();
  $request_time = $this->container
    ->get('datetime.time')
    ->getRequestTime();
  $test_gateway = $this
    ->createMemoryGateway();
  $test_gateway
    ->setRetentionDuration(Direction::OUTGOING, 1000)
    ->save();
  $this->container
    ->get('router.builder')
    ->rebuild();

  // Get the delivery reports url for simulating push delivery report.
  $url = $test_gateway
    ->getPushReportUrl()
    ->setAbsolute()
    ->toString();
  $sms_message = (new SmsMessage())
    ->setSender($this
    ->randomMachineName())
    ->addRecipients([
    '1234567890',
    '987654321',
  ])
    ->setMessage($this
    ->randomString())
    ->setUid($user
    ->id())
    ->setGateway($test_gateway)
    ->setDirection(Direction::OUTGOING);
  $this->defaultSmsProvider
    ->queue($sms_message);
  $this->container
    ->get('cron')
    ->run();
  $saved_reports = SmsDeliveryReport::loadMultiple();
  $this
    ->assertEquals(2, count($saved_reports));
  $this
    ->assertEquals(SmsMessageReportStatus::QUEUED, $saved_reports[1]
    ->getStatus());
  $this
    ->assertEquals(SmsMessageReportStatus::QUEUED, $saved_reports[2]
    ->getStatus());

  /** @var \Drupal\sms\Message\SmsDeliveryReportInterface $first_report */
  $first_report = reset($saved_reports);
  $this
    ->assertEquals($request_time, $first_report
    ->getStatusTime());
  $message_id = $first_report
    ->getMessageId();
  $status_time = $request_time + 100;

  // Simulate push delivery report.
  $request = $this
    ->buildDeliveryReportRequest($message_id, $first_report
    ->getRecipient(), 'pending', $status_time);
  $this->defaultSmsProvider
    ->processDeliveryReport($request, $test_gateway);
  $this->container
    ->get('entity_type.manager')
    ->getStorage('sms_report')
    ->resetCache();
  $updated_report = SmsDeliveryReport::load($first_report
    ->id());
  $this
    ->assertEquals('pending', $updated_report
    ->getStatus());
  $this
    ->assertEquals($status_time, $updated_report
    ->getStatusTime());
  $this
    ->assertNull($updated_report
    ->getTimeDelivered());

  // Simulate push delivery report.
  $status_time = $request_time + 500;
  $request = $this
    ->buildDeliveryReportRequest($message_id, $first_report
    ->getRecipient(), SmsMessageReportStatus::DELIVERED, $status_time);
  $this->defaultSmsProvider
    ->processDeliveryReport($request, $test_gateway);
  $this->container
    ->get('entity_type.manager')
    ->getStorage('sms_report')
    ->resetCache();
  $updated_report = SmsDeliveryReport::load($first_report
    ->id());
  $this
    ->assertEquals(SmsMessageReportStatus::DELIVERED, $updated_report
    ->getStatus());
  $this
    ->assertEquals($status_time, $updated_report
    ->getStatusTime());
  $this
    ->assertEquals($status_time, $updated_report
    ->getTimeDelivered());
}