You are here

public function SmsSendToPhoneBrowserTest::testFieldFormatAndWidget 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::testFieldFormatAndWidget()
  2. 2.x modules/sms_sendtophone/tests/src/Functional/SmsSendToPhoneBrowserTest.php \Drupal\Tests\sms_sendtophone\Functional\SmsSendToPhoneBrowserTest::testFieldFormatAndWidget()

Tests field format integration and widget.

File

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

Class

SmsSendToPhoneBrowserTest
Integration tests for the SMS SendToPhone Module.

Namespace

Drupal\Tests\sms_sendtophone\Functional

Code

public function testFieldFormatAndWidget() {

  // Create a custom field of type 'text' using the sms_sendtophone formatter.
  $bundles = array_keys(NodeType::loadMultiple());
  $field_name = mb_strtolower($this
    ->randomMachineName());
  $field_definition = [
    'field_name' => $field_name,
    'entity_type' => 'node',
    'bundle' => $bundles[0],
  ];
  $field_storage = FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'node',
    'type' => 'text',
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create($field_definition);
  $field
    ->save();
  $display = EntityViewDisplay::load('node.' . $bundles[0] . '.default');
  if (!$display) {
    $display = EntityViewDisplay::create([
      'targetEntityType' => 'node',
      'bundle' => $bundles[0],
      'mode' => 'default',
      'status' => TRUE,
    ]);
  }
  $display
    ->setComponent($field_name)
    ->save();
  $random_text = $this
    ->randomMachineName(32);
  $test_node = $this
    ->drupalCreateNode([
    'type' => $bundles[0],
    $field_name => [
      [
        'value' => $random_text,
      ],
    ],
  ]);

  // This is a quick-fix. Need to find out how to add display filters in code.
  $this
    ->drupalLogin($this->rootUser);
  $this
    ->drupalPostForm('admin/structure/types/manage/article/display', [
    'fields[' . $field_name . '][type]' => 'sms_link',
  ], 'Save');

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

  // Click send button.
  $this
    ->drupalGet('node/' . $test_node
    ->id());
  $this
    ->assertText($random_text, 'Field format works');
  $this
    ->assertText($random_text . ' (Send to phone)');
  $this
    ->clickLink('Send to phone');

  // Click the send button there.
  $this
    ->drupalPostForm(NULL, [], 'Send', [
    'query' => [
      'text' => $random_text,
    ],
  ]);
  $sms_message = $this
    ->getLastTestMessage($this->gateway);
  $this
    ->assertTrue(in_array($phone_number, $sms_message
    ->getRecipients()), 'Message sent to correct number');
  $this
    ->assertEqual($sms_message
    ->getMessage(), $random_text, 'Field content sent to user');
}