You are here

public function MobileNumberElementTestCase::testNumberValidity in Mobile Number 7

Test number validation.

File

tests/mobile_number.element.test, line 41

Class

MobileNumberElementTestCase
Class for testing mobile_number element.

Code

public function testNumberValidity() {
  $local_numbers = array(
    '0502345678' => 'Valid IL',
    '111' => 'Invalid IL',
    NULL => 'Empty',
  );
  $countries = array(
    'IL' => 'IL',
    'US' => 'US',
    NULL => 'Empty',
  );
  $allowed_countries = array(
    'IL' => array(
      'IL' => 'IL',
    ),
    'US' => array(
      'US' => 'US',
    ),
    'All' => array(),
  );
  $element = array(
    '#type' => 'mobile_number',
    '#title' => 'M',
    '#required' => TRUE,
    '#mobile_number' => array(),
  );
  $input = array(
    'country-code' => 'IL',
    'mobile' => '0502345678',
  );
  foreach ($allowed_countries as $type => $allowed) {
    $element['#mobile_number']['allowed_countries'] = $allowed;
    $errors = $this
      ->submitFormElement($element, $input);
    $success = $type == 'US' ? 'failure' : 'success';
    $this
      ->assertTrue($errors == ($type == 'US'), "IL mobile number with {$type} allowed: {$success}.", 'Number Validity');
  }
  unset($element['#mobile_number']['allowed_countries']);
  foreach ($local_numbers as $ln => $local_number) {
    foreach ($countries as $c => $country) {
      $input['country-code'] = $c;
      $input['mobile'] = $ln;
      $errors = $this
        ->submitFormElement($element, $input);
      $valid = '0502345678' == $ln && 'IL' == $c;
      $success = $valid ? 'Success' : 'Failure';
      $this
        ->assertTrue($valid == !$errors, "{$country} country, {$local_number} local number: {$success}.", 'Number Validity');
    }
  }
}