public function MobileNumberFieldTest::testVerification in Mobile Number 8
Same name and namespace in other branches
- 2.0.x src/Tests/MobileNumberFieldTest.php \Drupal\mobile_number\Tests\MobileNumberFieldTest::testVerification()
Test number verification.
File
- src/
Tests/ MobileNumberFieldTest.php, line 180
Class
- MobileNumberFieldTest
- Mobile number field functionality.'.
Namespace
Drupal\mobile_number\TestsCode
public function testVerification() {
$number = '0502345678';
$country = 'IL';
$value = '+972502345678';
$mobile_number = $this->util
->getMobileNumber($value);
$code = $this->util
->generateVerificationCode();
$required_name = 'ver_required';
$this
->drupalCreateContentType([
'type' => $required_name,
]);
$this
->createField($required_name, "field_{$required_name}", MobileNumberUtilInterface::MOBILE_NUMBER_UNIQUE_NO, 1, MobileNumberUtilInterface::MOBILE_NUMBER_VERIFY_REQUIRED);
$optional_name = 'ver_optional';
$this
->drupalCreateContentType([
'type' => $optional_name,
]);
$this
->createField($optional_name, "field_{$optional_name}", MobileNumberUtilInterface::MOBILE_NUMBER_UNIQUE_NO, 1, MobileNumberUtilInterface::MOBILE_NUMBER_VERIFY_OPTIONAL);
$tokens = [
FALSE => 'Wrong token',
NULL => 'No token',
TRUE => 'Correct token',
];
$codes = [
'000' => 'Wrong code',
NULL => 'No code',
$code => 'Correct code',
];
$user = $this
->drupalCreateUser([
"create {$required_name} content",
"create {$optional_name} content",
]);
$admin = $this
->drupalCreateUser([
"create {$required_name} content",
'bypass mobile number verification requirement',
]);
$input['country-code'] = $country;
$input['mobile'] = $number;
$this
->setCurrentUser($admin);
$errors = $this
->createMobileNumberNode($required_name, $value, FALSE);
$this
->assertTrue(!$errors, "Admin bypass verification requirement.", 'Number Verification');
$this
->setCurrentUser($user);
$errors = $this
->createMobileNumberNode($required_name, $value, FALSE);
$this
->assertTrue($errors, "Bypass verification requirement blocked.", 'Number Verification');
$errors = $this
->createMobileNumberNode($optional_name, $value, FALSE);
$this
->assertTrue(!$errors, "Optional verification allowed unverified.", 'Number Verification');
/** @var \Drupal\Core\Flood\FloodInterface $flood */
$flood = \Drupal::service('flood');
foreach ($tokens as $is_valid_token => $t) {
foreach ($codes as $input_code => $c) {
$input['country-code'] = $country;
$input['mobile'] = $number;
$input['verification_token'] = isset($is_valid_token) ? $is_valid_token ? $this->util
->registerVerificationCode($mobile_number, $code) : 'abc' : NULL;
$input['verification_code'] = $input_code;
$flood
->clear('mobile_number_verification', $value);
$errors = $this
->createMobileNumberNodeFromInput($required_name, $input);
$validated = $is_valid_token && $code == $input_code;
$valid_text = $validated ? 'verified' : 'not verified';
$this
->assertTrue($validated == !$errors, "{$t}, {$c}, is {$valid_text}. " . ($errors ? reset($errors) : ''), 'Number Verification');
}
}
$input = [
'country-code' => $country,
'mobile' => $value,
];
$_SESSION['mobile_number_verification'][$value]['verified'] = TRUE;
$errors = $this
->createMobileNumberNodeFromInput($required_name, $input);
$this
->assertTrue(!$errors, "Already verified number is verified." . ($errors ? reset($errors) : ''), 'Number Verification');
$input = [
'country-code' => $country,
'mobile' => substr($number, 0, 9) . '0',
];
$errors = $this
->createMobileNumberNodeFromInput($required_name, $input);
$this
->assertTrue($errors, "Not yet verified number is not verified. " . ($errors ? reset($errors) : ''), 'Number Verification');
}