You are here

public function MobileNumberElementTest::testNumberValidity in Mobile Number 2.0.x

Same name and namespace in other branches
  1. 8 src/Tests/MobileNumberElementTest.php \Drupal\mobile_number\Tests\MobileNumberElementTest::testNumberValidity()

Test number validation.

File

src/Tests/MobileNumberElementTest.php, line 48

Class

MobileNumberElementTest
Mobile number form element functionality.

Namespace

Drupal\mobile_number\Tests

Code

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