You are here

public function MobileNumberFieldTest::testNumberValidity in Mobile Number 8

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

Test number validation.

File

src/Tests/MobileNumberFieldTest.php, line 47

Class

MobileNumberFieldTest
Mobile number field 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' => [],
  ];
  $input = [
    'country-code' => 'IL',
    'mobile' => '0502345678',
  ];
  $name = 'validity';
  $this
    ->drupalCreateContentType([
    'type' => $name,
  ]);
  $this
    ->createField($name, "field_{$name}", MobileNumberUtilInterface::MOBILE_NUMBER_UNIQUE_NO, 1);
  $user = $this
    ->drupalCreateUser([
    "create {$name} content",
  ]);
  $this
    ->setCurrentUser($user);
  foreach ($allowed_countries as $type => $allowed) {
    foreach ($local_numbers as $ln => $local_number) {
      foreach ($countries as $c => $country) {
        $input['country-code'] = $c;
        $input['mobile'] = $ln;
        $this
          ->updateFieldConfig($name, 'countries', $allowed);
        $errors = $this
          ->createMobileNumberNodeFromInput($name, $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}. " . ($errors ? reset($errors) : ''), 'Number Validity');
      }
    }
  }
}