View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\sms\Functional;
use Drupal\Core\Entity\EntityInterface;
use Drupal\sms\Message\SmsMessageInterface;
class SmsFrameworkPhoneNumberTest extends SmsFrameworkBrowserTestBase {
public static $modules = [
'entity_test',
];
protected function setUp() {
parent::setUp();
$test_gateway = $this
->createMemoryGateway([
'skip_queue' => TRUE,
]);
$this
->setFallbackGateway($test_gateway);
}
public function testPhoneNumberVerificationCreated() {
$phone_number_settings = $this
->createPhoneNumberSettings('entity_test', 'entity_test');
$phone_numbers = [
'+123123123',
'+456456456',
'+789789789',
];
for ($quantity = 1; $quantity < 3; $quantity++) {
$test_entity = $this
->createEntityWithPhoneNumber($phone_number_settings, array_slice($phone_numbers, 0, $quantity));
$this
->assertEqual($quantity, $this
->countVerificationCodes($test_entity), 'There is ' . $quantity . ' verification code.');
$test_entity
->save();
$this
->assertEqual($quantity, $this
->countVerificationCodes($test_entity), 'Additional verification codes were not created.');
}
}
public function testPhoneNumberVerificationMessage() {
$test_gateway = $this
->createMemoryGateway([
'skip_queue' => TRUE,
]);
$this
->setFallbackGateway($test_gateway);
$phone_numbers = [
'+123123123',
];
$phone_number_settings = $this
->createPhoneNumberSettings('entity_test', 'entity_test');
$this
->createEntityWithPhoneNumber($phone_number_settings, $phone_numbers);
$sms_message = $this
->getLastTestMessage($test_gateway);
$this
->assertTrue($sms_message instanceof SmsMessageInterface, 'SMS verification message sent.');
$this
->assertEqual($sms_message
->getRecipients(), $phone_numbers, 'Sent to correct phone number.');
$phone_verification = $this
->getLastVerification();
$data['sms_verification_code'] = $phone_verification
->getCode();
$message = \Drupal::token()
->replace($phone_number_settings
->getVerificationMessage(), $data);
$this
->assertEqual($sms_message
->getMessage(), $message, 'Sent correct message.');
}
public function testPhoneNumberVerificationDeleted() {
$phone_number_settings = $this
->createPhoneNumberSettings('entity_test', 'entity_test');
$entities = [];
for ($i = 0; $i < 3; $i++) {
$phone_numbers = [
'+123123123',
'+456456456',
];
$entities[] = $this
->createEntityWithPhoneNumber($phone_number_settings, $phone_numbers);
}
$this
->assertEqual(6, $this
->countVerificationCodes());
$entities[1]
->delete();
$this
->assertEqual(4, $this
->countVerificationCodes(), 'Verification codes deleted.');
}
protected function countVerificationCodes(EntityInterface $entity = NULL) {
$query = \Drupal::entityTypeManager()
->getStorage('sms_phone_number_verification')
->getQuery();
if ($entity) {
$query
->condition('entity__target_type', $entity
->getEntityTypeId());
$query
->condition('entity__target_id', $entity
->id());
}
return $query
->count()
->execute();
}
}