You are here

public function SmsFrameworkUpdateTest::testSmsUpdate8101 in SMS Framework 2.x

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

Tests sms_update_8101.

File

tests/src/Functional/SmsFrameworkUpdateTest.php, line 47

Class

SmsFrameworkUpdateTest
Update path test coverage for sms module.

Namespace

Drupal\Tests\sms\Functional

Code

public function testSmsUpdate8101() {
  $db_schema = \Drupal::database()
    ->schema();

  // Check that the sms tables exist but the others don't.
  $this
    ->assertTrue($db_schema
    ->tableExists('sms'));
  $this
    ->assertTrue($db_schema
    ->tableExists('sms__recipient_phone_number'));
  $this
    ->assertTrue($db_schema
    ->tableExists('sms_phone_number_verification'));
  $this
    ->assertFalse($db_schema
    ->tableExists('sms_result'));
  $this
    ->assertFalse($db_schema
    ->tableExists('sms_report'));
  $this
    ->assertFalse($db_schema
    ->tableExists('sms_report_revision'));
  $this
    ->runUpdates();

  // Check that all the sms entity tables exist.
  $this
    ->assertTrue($db_schema
    ->tableExists('sms'));
  $this
    ->assertTrue($db_schema
    ->tableExists('sms__recipient_phone_number'));
  $this
    ->assertTrue($db_schema
    ->tableExists('sms_phone_number_verification'));
  $this
    ->assertTrue($db_schema
    ->tableExists('sms_result'));
  $this
    ->assertTrue($db_schema
    ->tableExists('sms_report'));
  $this
    ->assertTrue($db_schema
    ->tableExists('sms_report_revision'));

  // Confirm that the existing SMS message was not clobbered.

  /** @var \Drupal\sms\Entity\SmsMessageInterface[] $sms_messages */
  $sms_messages = SmsMessage::loadMultiple();
  $this
    ->assertEqual(1, count($sms_messages));
  $this
    ->assertEqual(2, count($sms_messages[1]
    ->getRecipients()));
  $this
    ->assertNull($sms_messages[1]
    ->getResult());

  // Create new SMS with delivery report and save it.
  $sms_message = SmsMessage::create()
    ->addRecipients($this
    ->randomPhoneNumbers())
    ->setSender($this
    ->randomMachineName())
    ->setDirection(Direction::OUTGOING)
    ->setAutomated(TRUE);
  $reports = array_map(function ($recipient) {
    return SmsDeliveryReport::create()
      ->setMessageId($this
      ->randomString())
      ->setRecipient($recipient)
      ->setStatus(SmsMessageReportStatus::DELIVERED)
      ->setStatusMessage($this
      ->randomString());
  }, $sms_message
    ->getRecipients());
  $result = SmsMessageResult::create()
    ->setReports($reports);
  $sms_message
    ->setResult($result)
    ->save();
  \Drupal::entityTypeManager()
    ->getStorage('sms')
    ->resetCache();
  $sms_messages = SmsMessage::loadMultiple();
  $this
    ->assertEqual(2, count($sms_messages));
  $this
    ->assertTrue($sms_messages[2]
    ->getResult() instanceof SmsMessageResultInterface);
  $this
    ->assertEqual(count($sms_message
    ->getRecipients()), count($sms_messages[2]
    ->getResult()
    ->getReports()));
}