You are here

public function MobileNumberElementTestCase::testVerification in Mobile Number 7

Test number verification.

File

tests/mobile_number.element.test, line 99

Class

MobileNumberElementTestCase
Class for testing mobile_number element.

Code

public function testVerification() {
  $number = '0502345678';
  $country = 'IL';
  $value = '+972502345678';
  $code = MobileNumber::generateVerificationCode();
  $token = MobileNumber::registerVerificationCode($code, $value);
  $tokens = array(
    'abc' => 'Wrong token',
    NULL => 'No token',
    $token => 'Correct token',
  );
  $codes = array(
    '000' => 'Wrong code',
    NULL => 'No code',
    $code => 'Correct code',
  );
  $required_states = array(
    TRUE => 'Required',
    FALSE => 'Not required',
  );
  $required_inputs = array(
    TRUE => array(
      'country-code' => $country,
      'mobile' => $number,
    ),
    FALSE => array(
      'country-code' => $country,
      'mobile' => '',
    ),
  );
  $element = array(
    '#type' => 'mobile_number',
    '#title' => 'M',
    '#mobile_number' => array(
      'verify' => MOBILE_NUMBER_VERIFY_OPTIONAL,
    ),
  );
  foreach ($required_states as $required => $required_msg) {
    $element['#required'] = $required;
    foreach ($required_inputs as $has_value => $input) {
      $errors = $this
        ->submitFormElement($element, $input, $value);
      $this
        ->assertTrue(!!$errors == ($required && !$has_value), "Input is required: " . ($required ? 'TRUE' : 'FALSE') . ', Input has value: ' . ($has_value ? 'TRUE' : 'FALSE') . ', has error: ' . ($required && !$has_value ? 'TRUE' : 'FALSE'), 'Number Verification');
    }
  }
  $element['#mobile_number']['verify'] = MOBILE_NUMBER_VERIFY_REQUIRED;
  $input['country-code'] = $country;
  $input['mobile'] = $number;
  $errors = $this
    ->submitFormElement($element, $input, $value);
  $this
    ->assertTrue(!$errors, "Bypass verification requirement.", 'Number Verification');
  global $user;
  $user = $this
    ->drupalCreateUser(array());
  $element['#mobile_number']['verify'] = MOBILE_NUMBER_VERIFY_OPTIONAL;
  $errors = $this
    ->submitFormElement($element, $input, $value);
  $this
    ->assertTrue(!$errors, "Optional verification allowed unverified.", 'Number Verification');
  $element['#mobile_number']['verify'] = MOBILE_NUMBER_VERIFY_REQUIRED;
  foreach ($tokens as $input_token => $t) {
    foreach ($codes as $input_code => $c) {
      $input['country-code'] = $country;
      $input['mobile'] = $number;
      $input['verification_token'] = $input_token;
      $input['verification_code'] = $input_code;
      $errors = $this
        ->submitFormElement($element, $input, $value);
      $validated = $token == $input_token && $code == $input_code;
      $valid_text = $validated ? 'verified' : 'not verified';
      $this
        ->assertTrue($validated == !$errors, "{$t}, {$c}, is {$valid_text}.", 'Number Verification');
    }
  }
  $element['#default_value'] = array(
    'value' => $value,
    'verified' => 1,
  );
  $input['country-code'] = $country;
  $input['mobile'] = $number;
  $errors = $this
    ->submitFormElement($element, $input, $value);
  $this
    ->assertTrue(!$errors, "Already verified number is verified.", 'Number Verification');
  $element['#default_value']['verified'] = 0;
  $errors = $this
    ->submitFormElement($element, $input, $value);
  $this
    ->assertTrue(!$errors, "Not yet verified number is not verified.", 'Number Verification');
}