You are here

trait SmsFrameworkTestTrait in SMS Framework 8

Same name and namespace in other branches
  1. 2.x tests/src/Functional/SmsFrameworkTestTrait.php \Drupal\Tests\sms\Functional\SmsFrameworkTestTrait
  2. 2.1.x tests/src/Functional/SmsFrameworkTestTrait.php \Drupal\Tests\sms\Functional\SmsFrameworkTestTrait

Shared SMS Framework helpers for kernel and web tests.

Hierarchy

7 files declare their use of SmsFrameworkTestTrait
RecipientGatewayEventTest.php in tests/src/Unit/Event/RecipientGatewayEventTest.php
SmsFrameworkDeliveryReportEntityTest.php in tests/src/Kernel/SmsFrameworkDeliveryReportEntityTest.php
SmsFrameworkDeliveryReportUpdateTest.php in tests/src/Kernel/SmsFrameworkDeliveryReportUpdateTest.php
SmsFrameworkKernelBase.php in tests/src/Kernel/SmsFrameworkKernelBase.php
SmsFrameworkMessageEntityTest.php in tests/src/Kernel/SmsFrameworkMessageEntityTest.php

... See full list

File

tests/src/Functional/SmsFrameworkTestTrait.php, line 18

Namespace

Drupal\Tests\sms\Functional
View source
trait SmsFrameworkTestTrait {

  /**
   * Sets the fallback gateway.
   *
   * @param \Drupal\sms\Entity\SmsGatewayInterface|null $sms_gateway
   *   The new site fallback SMS Gateway, or NULL to unset.
   */
  protected function setFallbackGateway(SmsGatewayInterface $sms_gateway = NULL) {
    $sms_gateway = $sms_gateway ? $sms_gateway
      ->id() : NULL;
    $this
      ->config('sms.settings')
      ->set('fallback_gateway', $sms_gateway)
      ->save();
  }

  /**
   * Creates a memory gateway.
   *
   * @param array $values
   *   Additional values to use when creating the gateway.
   *
   * @return \Drupal\sms\Entity\SmsGatewayInterface
   *   A saved memory gateway.
   */
  protected function createMemoryGateway(array $values = []) {
    $id = isset($values['id']) ? $values['id'] : Unicode::strtolower($this
      ->randomMachineName(16));
    $gateway = SmsGateway::create($values + [
      'plugin' => 'memory',
      'id' => $id,
      'label' => $this
        ->randomString(),
      'settings' => [
        'gateway_id' => $id,
      ],
    ] + $values);
    $gateway
      ->enable();
    $gateway
      ->save();
    return $gateway;
  }

  /**
   * Get all SMS messages sent to a 'Memory' gateway.
   *
   * @param \Drupal\sms\Entity\SmsGatewayInterface $sms_gateway
   *   A gateway plugin instance.
   *
   * @return \Drupal\sms\Message\SmsMessageInterface[]
   *   An array of SMS messages sent to a 'Memory' gateway.
   */
  public function getTestMessages(SmsGatewayInterface $sms_gateway) {
    $gateway_id = $sms_gateway
      ->id();
    $sms_messages = \Drupal::state()
      ->get('sms_test_gateway.memory.send', []);
    return isset($sms_messages[$gateway_id]) ? $sms_messages[$gateway_id] : [];
  }

  /**
   * Get the last SMS message sent to 'Memory' gateway.
   *
   * @param \Drupal\sms\Entity\SmsGatewayInterface $sms_gateway
   *   A gateway plugin.
   *
   * @return \Drupal\sms\Message\SmsMessageInterface|false
   *   The last SMS message, or FALSE if no messages have been sent.
   */
  public function getLastTestMessage(SmsGatewayInterface $sms_gateway) {
    $gateway_id = $sms_gateway
      ->id();
    $sms_messages = \Drupal::state()
      ->get('sms_test_gateway.memory.send', []);
    return isset($sms_messages[$gateway_id]) ? end($sms_messages[$gateway_id]) : FALSE;
  }

  /**
   * Resets SMS messages stored in memory by 'Memory' gateway.
   *
   * @param \Drupal\sms\Entity\SmsGatewayInterface|null $sms_gateway
   *   A gateway plugin, or NULL to reset all messages.
   */
  public function resetTestMessages(SmsGatewayInterface $sms_gateway = NULL) {
    $sms_messages = \Drupal::state()
      ->get('sms_test_gateway.memory.send', []);
    if ($sms_gateway) {
      $sms_messages[$sms_gateway
        ->id()] = [];
    }
    else {
      $sms_messages = [];
    }
    \Drupal::state()
      ->set('sms_test_gateway.memory.send', $sms_messages);
  }

  /**
   * Get all messages received by a gateway.
   *
   * @param \Drupal\sms\Entity\SmsGatewayInterface $sms_gateway
   *   A gateway plugin instance.
   *
   * @return \Drupal\sms\Message\SmsMessageInterface[]
   *   An array of messages received by a gateway.
   */
  protected function getIncomingMessages(SmsGatewayInterface $sms_gateway) {
    $gateway_id = $sms_gateway
      ->id();
    $sms_messages = \Drupal::state()
      ->get('sms_test_gateway.memory.incoming', []);
    return isset($sms_messages[$gateway_id]) ? $sms_messages[$gateway_id] : [];
  }

  /**
   * Get the last message sent to gateway.
   *
   * @param \Drupal\sms\Entity\SmsGatewayInterface $sms_gateway
   *   A gateway plugin.
   *
   * @return \Drupal\sms\Message\SmsMessageInterface|false
   *   The last message, or FALSE if no messages were received.
   */
  protected function getLastIncomingMessage(SmsGatewayInterface $sms_gateway) {
    $gateway_id = $sms_gateway
      ->id();
    $sms_messages = \Drupal::state()
      ->get('sms_test_gateway.memory.incoming', []);
    return isset($sms_messages[$gateway_id]) ? end($sms_messages[$gateway_id]) : FALSE;
  }

  /**
   * Resets incoming messages stored in memory by gateway.
   *
   * @param \Drupal\sms\Entity\SmsGatewayInterface|null $sms_gateway
   *   A gateway plugin, or NULL to reset all messages.
   */
  protected function resetIncomingMessages(SmsGatewayInterface $sms_gateway = NULL) {
    $sms_messages = \Drupal::state()
      ->get('sms_test_gateway.memory.incoming', []);
    if ($sms_gateway) {
      $sms_messages[$sms_gateway
        ->id()] = [];
    }
    else {
      $sms_messages = [];
    }
    \Drupal::state()
      ->set('sms_test_gateway.memory.incoming', $sms_messages);
  }

  /**
   * Gets all SMS reports for messages sent to 'Memory' gateway.
   *
   * @param \Drupal\sms\Entity\SmsGatewayInterface $sms_gateway
   *   A gateway plugin.
   *
   * @return \Drupal\sms\Message\SmsDeliveryReportInterface[]
   *   An array of SMS reports for messages sent to 'Memory' gateway.
   */
  protected function getTestMessageReports(SmsGatewayInterface $sms_gateway) {
    $gateway_id = $sms_gateway
      ->id();
    $sms_reports = \Drupal::state()
      ->get('sms_test_gateway.memory.report', []);
    return isset($sms_reports[$gateway_id]) ? $sms_reports[$gateway_id] : [];
  }

  /**
   * Gets the last SMS report for messages sent to 'Memory' gateway.
   *
   * @param \Drupal\sms\Entity\SmsGatewayInterface $sms_gateway
   *   A gateway plugin.
   *
   * @return \Drupal\sms\Message\SmsDeliveryReportInterface|false
   *   The last SMS message, or FALSE if no messages have been sent.
   */
  protected function getLastTestMessageReport(SmsGatewayInterface $sms_gateway) {
    $gateway_id = $sms_gateway
      ->id();
    $sms_reports = \Drupal::state()
      ->get('sms_test_gateway.memory.report', []);
    return isset($sms_reports[$gateway_id]) ? end($sms_reports[$gateway_id]) : FALSE;
  }

  /**
   * Gets an SMS report for message with message ID.
   *
   * @param string $message_id
   *   The message ID.
   * @param \Drupal\sms\Entity\SmsGatewayInterface $sms_gateway
   *   A gateway plugin.
   *
   * @return \Drupal\sms\Message\SmsDeliveryReportInterface
   *   The last SMS message, or FALSE if no messages have been sent.
   */
  protected function getTestMessageReport($message_id, SmsGatewayInterface $sms_gateway) {
    $gateway_id = $sms_gateway
      ->id();
    $reports = \Drupal::state()
      ->get('sms_test_gateway.memory.report', []);
    return $reports[$gateway_id][$message_id];
  }

  /**
   * Resets the SMS reports stored in memory by 'Memory' gateway.
   */
  protected function resetTestMessageReports() {
    \Drupal::state()
      ->set('sms_test_gateway.memory.report', []);
  }

  /**
   * Creates an entity, and optionally adds phone numbers.
   *
   * @param \Drupal\sms\Entity\PhoneNumberSettingsInterface $phone_number_settings
   *   Phone number settings.
   * @param array $phone_numbers
   *   An array of phone numbers to add to the entity.
   *
   * @return \Drupal\Core\Entity\EntityInterface
   *   An entity with phone numbers.
   */
  protected function createEntityWithPhoneNumber(PhoneNumberSettingsInterface $phone_number_settings, array $phone_numbers = []) {
    $entity_type = $phone_number_settings
      ->getPhoneNumberEntityTypeId();
    $field_name = $phone_number_settings
      ->getFieldName('phone_number');
    $entity_type_manager = \Drupal::entityTypeManager();
    $test_entity = $entity_type_manager
      ->getStorage($entity_type)
      ->create([
      'name' => $this
        ->randomMachineName(),
    ]);
    foreach ($phone_numbers as $phone_number) {
      $test_entity->{$field_name}[] = $phone_number;
    }
    $test_entity
      ->save();
    return $test_entity;
  }

  /**
   * Forces verification of a phone number for an entity.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   An entity to verify phone number.
   * @param string $phone_number
   *   A phone number.
   */
  protected function verifyPhoneNumber(EntityInterface $entity, $phone_number) {
    $verifications = \Drupal::entityTypeManager()
      ->getStorage('sms_phone_number_verification')
      ->loadByProperties([
      'entity__target_type' => $entity
        ->getEntityTypeId(),
      'entity__target_id' => $entity
        ->id(),
      'phone' => $phone_number,
    ]);
    $verification = reset($verifications);
    $verification
      ->setStatus(TRUE)
      ->save();
  }

  /**
   * Gets the last phone number verification that was created.
   *
   * @return \Drupal\sms\Entity\PhoneNumberVerificationInterface|false
   *   The last verification created, or FALSE if no verifications exist.
   */
  protected function getLastVerification() {
    $verification_storage = \Drupal::entityTypeManager()
      ->getStorage('sms_phone_number_verification');
    $verification_ids = $verification_storage
      ->getQuery()
      ->sort('created', 'DESC')
      ->range(0, 1)
      ->execute();
    $verifications = $verification_storage
      ->loadMultiple($verification_ids);
    return reset($verifications);
  }

  /**
   * Create a result and reports for a message.
   *
   * @param \Drupal\sms\Message\SmsMessageInterface $sms_message
   *   A message object.
   *
   * @return \Drupal\sms\Message\SmsMessageResult
   *   A message result with reports for each message recipient.
   */
  protected function createMessageResult(SmsMessageInterface $sms_message) {
    $reports = array_map(function ($recipient) {
      return (new SmsDeliveryReport())
        ->setRecipient($recipient);
    }, $sms_message
      ->getRecipients());
    return (new SmsMessageResult())
      ->setErrorMessage($this
      ->randomString())
      ->setReports($reports);
  }

  /**
   * Generates a random SMS message by the specified user.
   *
   * @param int $uid
   *   (optional) The user ID to generate the message as. Defaults to 1.
   *
   * @return \Drupal\sms\Message\SmsMessageInterface
   *   A random SMS message by the specified user.
   */
  protected function randomSmsMessage($uid = 1) {
    $phone_numbers = $this
      ->randomPhoneNumbers(1);
    return new SmsMessage($phone_numbers[0], $this
      ->randomPhoneNumbers(), $this
      ->randomString(), [], $uid);
  }

  /**
   * Generates random phone numbers for tests.
   *
   * @param int|null $quantity
   *   Quantity of phone numbers, or NULL to generate at least 2.
   *
   * @return array
   *   An array of phone numbers.
   */
  protected function randomPhoneNumbers($quantity = NULL) {
    $quantity = isset($quantity) ? $quantity : rand(2, 20);
    $numbers = [];
    for ($i = 0; $i < $quantity; $i++) {
      $numbers[] = '+' . rand(1000, 999999999);
    }
    return $numbers;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SmsFrameworkTestTrait::createEntityWithPhoneNumber protected function Creates an entity, and optionally adds phone numbers.
SmsFrameworkTestTrait::createMemoryGateway protected function Creates a memory gateway.
SmsFrameworkTestTrait::createMessageResult protected function Create a result and reports for a message.
SmsFrameworkTestTrait::getIncomingMessages protected function Get all messages received by a gateway.
SmsFrameworkTestTrait::getLastIncomingMessage protected function Get the last message sent to gateway.
SmsFrameworkTestTrait::getLastTestMessage public function Get the last SMS message sent to 'Memory' gateway.
SmsFrameworkTestTrait::getLastTestMessageReport protected function Gets the last SMS report for messages sent to 'Memory' gateway.
SmsFrameworkTestTrait::getLastVerification protected function Gets the last phone number verification that was created.
SmsFrameworkTestTrait::getTestMessageReport protected function Gets an SMS report for message with message ID.
SmsFrameworkTestTrait::getTestMessageReports protected function Gets all SMS reports for messages sent to 'Memory' gateway.
SmsFrameworkTestTrait::getTestMessages public function Get all SMS messages sent to a 'Memory' gateway.
SmsFrameworkTestTrait::randomPhoneNumbers protected function Generates random phone numbers for tests.
SmsFrameworkTestTrait::randomSmsMessage protected function Generates a random SMS message by the specified user.
SmsFrameworkTestTrait::resetIncomingMessages protected function Resets incoming messages stored in memory by gateway.
SmsFrameworkTestTrait::resetTestMessageReports protected function Resets the SMS reports stored in memory by 'Memory' gateway.
SmsFrameworkTestTrait::resetTestMessages public function Resets SMS messages stored in memory by 'Memory' gateway.
SmsFrameworkTestTrait::setFallbackGateway protected function Sets the fallback gateway.
SmsFrameworkTestTrait::verifyPhoneNumber protected function Forces verification of a phone number for an entity.