You are here

public function MobileNumberFieldTestCase::testNumberUniqueness in Mobile Number 7

Test number validation.

File

tests/mobile_number.field.test, line 39

Class

MobileNumberFieldTestCase
Class for testing mobile_number field.

Code

public function testNumberUniqueness() {
  $tries = array(
    'New values',
    'Resubmit values',
  );
  $value_count = array(
    1 => 'One value',
    2 => 'Two values',
  );
  $number_types = array(
    'Unverified',
    'Verified',
  );
  $unique_types = array(
    MOBILE_NUMBER_UNIQUE_YES => 'Unique',
    MOBILE_NUMBER_UNIQUE_YES_VERIFIED => 'Unique verified',
  );
  foreach ($value_count as $count => $count_text) {
    foreach ($unique_types as $unique => $unique_text) {
      $name = 'unique_' . $unique . '_count_' . $count;
      $this
        ->drupalCreateContentType(array(
        'type' => $name,
      ));
      $this
        ->createField($name, "field_{$name}", $unique, $count);
    }
  }

  // Check for in-field duplicates.
  foreach ($unique_types as $unique => $unique_text) {
    $name = 'unique_' . $unique . '_count_2';
    foreach ($number_types as $verified => $verified_text) {
      foreach ($number_types as $verified2 => $verified_text2) {
        $errors = $this
          ->createMobileNumberNode($name, "+9725021111{$verified}{$verified2}", $verified, $verified2);
        $valid = FALSE;
        switch ($unique) {
          case MOBILE_NUMBER_UNIQUE_YES_VERIFIED:
            $valid = !($verified || $verified2);
            break;
        }
        $valid_text = $valid ? 'is unique' : 'is not unique';
        $this
          ->assertTrue($valid == !$errors, "New values, Two values, {$unique_text}, 1 = {$verified_text}, 2 = {$verified_text2}: {$valid_text}.", 'Number Uniqueness');
      }
    }
  }

  // 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) {
        foreach ($number_types as $verified2 => $verified_text2) {
          $this
            ->createMobileNumberNode($name, "+972542222{$existing_verified}{$verified}{$verified2}", $existing_verified);
          $errors = $this
            ->createMobileNumberNode($name, "+972542222{$existing_verified}{$verified}{$verified2}", $verified, $verified2);
          $valid = FALSE;
          switch ($unique) {
            case MOBILE_NUMBER_UNIQUE_YES_VERIFIED:
              $valid = $verified + $verified2 + $existing_verified < 2 && !($verified || $verified2);
              break;
          }
          $valid_text = $valid ? 'is unique' : 'is not unique';
          $this
            ->assertTrue($valid == !$errors, "Resubmit values, Two values, {$unique_text}, 1 = {$verified_text}, 2 = {$verified_text2}: {$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 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}.", 'Number Uniqueness');
      }
    }
  }
}