You are here

public function SmsSendToPhoneBrowserTest::testAdminSettingsAndSendToPhone in SMS Framework 2.1.x

Same name and namespace in other branches
  1. 8 modules/sms_sendtophone/tests/src/Functional/SmsSendToPhoneBrowserTest.php \Drupal\Tests\sms_sendtophone\Functional\SmsSendToPhoneBrowserTest::testAdminSettingsAndSendToPhone()
  2. 2.x modules/sms_sendtophone/tests/src/Functional/SmsSendToPhoneBrowserTest.php \Drupal\Tests\sms_sendtophone\Functional\SmsSendToPhoneBrowserTest::testAdminSettingsAndSendToPhone()

Tests admin settings page and sendtophone node integration.

File

modules/sms_sendtophone/tests/src/Functional/SmsSendToPhoneBrowserTest.php, line 99

Class

SmsSendToPhoneBrowserTest
Integration tests for the SMS SendToPhone Module.

Namespace

Drupal\Tests\sms_sendtophone\Functional

Code

public function testAdminSettingsAndSendToPhone() {
  $user = $this
    ->drupalCreateUser([
    'administer smsframework',
  ]);
  $this
    ->drupalLogin($user);
  $this
    ->drupalGet('admin/config/smsframework/sendtophone');
  $edit = [];
  $expected = [];
  foreach (NodeType::loadMultiple() as $type) {
    $this
      ->assertText($type
      ->get('name'));
    if (rand(0, 1) > 0.5) {
      $edit["content_types[" . $type
        ->get('type') . "]"] = $expected[$type
        ->get('type')] = $type
        ->get('type');
    }
  }

  // Ensure at least one type is enabled.
  $edit["content_types[page]"] = $expected['page'] = 'page';
  $this
    ->drupalPostForm('admin/config/smsframework/sendtophone', $edit, 'Save configuration');
  $saved = $this
    ->config('sms_sendtophone.settings')
    ->get('content_types', []);
  $this
    ->assertEqual($expected, $saved);

  // Create a new node with sendtophone enabled and verify that the button is
  // added.
  $types = array_keys(array_filter($expected));
  $node = $this
    ->drupalCreateNode([
    'type' => $types[0],
  ]);
  $this
    ->drupalGet($node
    ->toUrl());

  // Confirm message for user without confirmed number.
  $this
    ->assertText(t('Set up and confirm your mobile number to send to phone.'));

  // Confirm phone number.
  $phone_number = $this
    ->randomPhoneNumbers(1)[0];
  $user->{$this->phoneField
    ->getName()} = [
    $phone_number,
  ];
  $user
    ->save();
  $this
    ->verifyPhoneNumber($user, $phone_number);
  $this
    ->drupalGet($node
    ->toUrl());

  // Confirm message for user without confirmed number.
  $this
    ->assertText('Send to phone');
  $this
    ->assertFieldByXPath('//a[@title="Send a link via SMS." and @class="sms-sendtophone"]', NULL);

  // Navigate to the "Send to phone" link.
  $this
    ->clickLink('Send to phone');
  $this
    ->assertResponse(200);
  $this
    ->assertFieldByName('number', $phone_number);
  $this
    ->assertFieldByName('message_display', $node
    ->toUrl()
    ->setAbsolute()
    ->toString());

  // Click the send button there.
  $this
    ->drupalPostForm(NULL, [
    'number' => $phone_number,
  ], t('Send'));
  $sms_message = $this
    ->getLastTestMessage($this->gateway);
  $this
    ->assertTrue(in_array($phone_number, $sms_message
    ->getRecipients()));
  $this
    ->assertEqual($sms_message
    ->getMessage(), $node
    ->toUrl()
    ->setAbsolute()
    ->toString());
}