You are here

function SmsUserWebTest::testNumberConfirmationAndSmsUserSend in SMS Framework 7

Tests user adding phone number.

File

modules/sms_user/tests/sms_user.test, line 37
Contains tests for the functions in sms_user.module and user integration.

Class

SmsUserWebTest
Provides integration tests for the sms_user module.

Code

function testNumberConfirmationAndSmsUserSend() {

  // Set up test default gateway.
  variable_set('sms_default_gateway', 'test');
  $user = $this
    ->drupalCreateUser(array(
    'receive sms',
    'edit own sms number',
  ));
  $this
    ->drupalLogin($user);

  // Post user confirmation request.
  $edit = array(
    'number' => '1234567890',
  );
  $this
    ->drupalPost('user/' . $user->uid . '/edit/mobile', $edit, t('Confirm number'));
  $this
    ->assertResponse(200);

  // Confirm that opt-out and sleep settings options are not available yet.
  $this
    ->assertNoFieldByXPath('//input[@name="sms_user_opt_out"]', null, 'SMS User opt out settings not available until number confirmed.');
  $this
    ->assertNoFieldByXPath('//input[@name="sleep_enabled"]', null, 'SMS User sleep enable settings not available until number confirmed.');

  // Get the code that was sent.
  $gw_result = sms_test_gateway_result();
  preg_match('/\\b([0-9]{4})\\b/', $gw_result['message'], $matches);
  $code = $matches[1];

  // Post the code for confirmation.
  $this
    ->drupalPost('user/' . $user->uid . '/edit/mobile', array(
    'confirm_code' => $code,
  ), t('Confirm number'));
  $this
    ->assertResponse(200);

  // Confirm user's number is verified.
  $user = user_load($user->uid, TRUE);
  $this
    ->assertTrue($user->sms_user['number'] == $edit['number'] && $user->sms_user['status'] == SMS_USER_CONFIRMED, 'Successfully confirmed user phone number ' . $edit['number']);

  // Confirm that opt-out and sleep settings options are now available.
  $this
    ->assertFieldByXPath('//input[@name="sms_user_opt_out"]', null, 'SMS User opt out settings available after number confirmed.');
  $this
    ->assertFieldByXPath('//input[@name="sleep_enabled"]', null, 'SMS User sleep enable settings available after number confirmed.');

  // Send sms to user with registered number.
  $message = 'Test user message';
  $this
    ->assertTrue(sms_user_send($user->uid, $message), 'Successfully sent message to user with permission');
  $this
    ->assertEqual(sms_test_gateway_result(), array(
    'number' => $user->sms_user['number'],
    'message' => $message,
    'options' => $user->sms_user['gateway'],
  ), 'Message sent through the correct gateway.');

  // Test sms_user_authenticate() on this user.
  $account = sms_user_authenticate($user->sms_user['number']);
  $this
    ->assertEqual($user->uid, $account->uid, 'Correctly authenticated user by phone number.');

  // Get a user with no permissions and test failed sending.
  $user1 = $this
    ->drupalCreateUser(array());
  $this
    ->assertFalse(sms_user_send($user1->uid, $message), 'Failed sending to user without permission');

  // Clear user confirmed number.
  $this
    ->drupalPost('user/' . $user->uid . '/edit/mobile', array(), t('Delete & start over'));
  $this
    ->assertResponse(200);
  $user = user_load($user->uid, TRUE);
  $this
    ->assertTrue($user->sms_user['number'] == '' && $user->sms_user['status'] == 0, 'Successfully deleted user confirmed number');

  // Test that sending fails when confirmed number is deleted.
  $this
    ->assertFalse(sms_user_send($user->uid, $message), 'Failed sending to user without confirmed number');

  // Test failure to authenticate a non-existent number.
  $this
    ->assertFalse(sms_user_authenticate(rand(23400000000, 23499999999)), 'Failed to authenticate non-existent number.');
}