You are here

public function MobileNumberFieldTest::testNumberUniqueness in Mobile Number 8

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

Test number validation.

File

src/Tests/MobileNumberFieldTest.php, line 98

Class

MobileNumberFieldTest
Mobile number field functionality.'.

Namespace

Drupal\mobile_number\Tests

Code

public function testNumberUniqueness() {
  $tries = [
    'New values',
    'Resubmit values',
  ];
  $value_count = [
    1 => 'One value',
    2 => 'Two values',
  ];
  $number_types = [
    'Unverified',
    'Verified',
  ];
  $unique_types = [
    MobileNumberUtilInterface::MOBILE_NUMBER_UNIQUE_YES => 'Unique',
    MobileNumberUtilInterface::MOBILE_NUMBER_UNIQUE_YES_VERIFIED => 'Unique verified',
  ];
  $names = [];
  foreach ($value_count as $count => $count_text) {
    foreach ($unique_types as $unique => $unique_text) {
      $name = 'unique_' . $unique . '_count_' . $count;
      $this
        ->drupalCreateContentType([
        'type' => $name,
      ]);
      $this
        ->createField($name, "field_{$name}", $unique, $count);
      $names[] = $name;
    }
  }
  $user = $this
    ->drupalCreateUser(array_map(function ($element) {
    return "create {$element} content";
  }, $names));
  $this
    ->setCurrentUser($user);

  // Check for inter-entity multi-value duplicates.
  foreach ($unique_types as $unique => $unique_text) {
    $name = 'unique_' . $unique . '_count_2';
    $count = 0;
    foreach ($number_types as $existing_verified => $existing_verified_text) {
      foreach ($number_types as $verified => $verified_text) {
        $this
          ->createMobileNumberNode($name, "+9725422222{$existing_verified}{$verified}", $existing_verified);
        $errors = $this
          ->createMobileNumberNode($name, "+9725422222{$existing_verified}{$verified}", $verified);
        $valid = FALSE;
        switch ($unique) {
          case MobileNumberUtilInterface::MOBILE_NUMBER_UNIQUE_YES_VERIFIED:
            $valid = $verified + $existing_verified < 2;
            break;
        }
        $valid_text = $valid ? 'is unique' : 'is not unique';
        $this
          ->assertTrue($valid == !$errors, "Resubmit values, One value, {$unique_text}, {$verified_text}, existing = {$existing_verified_text}: {$valid_text}.", 'Number Uniqueness');
        $count++;
      }
    }
  }

  // Check for inter-entity single-value duplicates.
  foreach ($unique_types as $unique => $unique_text) {
    $name = 'unique_' . $unique . '_count_1';
    foreach ($number_types as $existing_verified => $existing_verified_text) {
      foreach ($number_types as $verified => $verified_text) {
        $number = "+9725433333{$existing_verified}{$verified}";
        $this
          ->createMobileNumberNode($name, $number, $existing_verified);
        $errors = $this
          ->createMobileNumberNode($name, $number, $verified);
        $valid = FALSE;
        switch ($unique) {
          case MobileNumberUtilInterface::MOBILE_NUMBER_UNIQUE_YES_VERIFIED:
            $valid = $verified + $existing_verified < 2;
            break;
        }
        $valid_text = $valid ? 'is unique' : 'is not unique';
        $this
          ->assertTrue($valid == !$errors, "Resubmit values, One value, {$unique_text}, presaved = {$existing_verified_text}, new = {$verified_text}, {$valid_text}. " . ($errors ? reset($errors) : ''), 'Number Uniqueness');
      }
    }
  }
}