View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\sms\Kernel;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\sms\Entity\PhoneNumberSettings;
use Drupal\sms\Entity\PhoneNumberSettingsInterface;
use Drupal\sms\Entity\PhoneNumberVerificationInterface;
use Drupal\sms\Exception\PhoneNumberSettingsException;
class SmsFrameworkPhoneNumberVerificationTest extends SmsFrameworkKernelBase {
public static $modules = [
'sms',
'entity_test',
'user',
'field',
'telephone',
'dynamic_entity_reference',
'sms_test_gateway',
];
protected $phoneNumberProvider;
protected $phoneNumberVerificationProvider;
protected $phoneField;
protected $phoneNumberSettings;
protected $gateway;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('entity_test');
$this
->installEntitySchema('sms_phone_number_verification');
$this->gateway = $this
->createMemoryGateway([
'skip_queue' => TRUE,
]);
$this
->setFallbackGateway($this->gateway);
$this->phoneNumberVerificationProvider = $this->container
->get('sms.phone_number.verification');
$this->phoneField = FieldStorageConfig::create([
'entity_type' => 'entity_test',
'field_name' => mb_strtolower($this
->randomMachineName()),
'type' => 'telephone',
]);
$this->phoneField
->save();
FieldConfig::create([
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'field_name' => $this->phoneField
->getName(),
])
->save();
$this->phoneNumberSettings = PhoneNumberSettings::create();
$this->phoneNumberSettings
->setPhoneNumberEntityTypeId('entity_test')
->setPhoneNumberBundle('entity_test')
->setFieldName('phone_number', $this->phoneField
->getName())
->setVerificationMessage($this
->randomString())
->save();
}
public function testGetPhoneNumberSettings() {
$return = $this->phoneNumberVerificationProvider
->getPhoneNumberSettings($this
->randomMachineName(), $this
->randomMachineName());
$this
->assertNull($return, 'Phone number settings does not exist.');
$return = $this->phoneNumberVerificationProvider
->getPhoneNumberSettings('entity_test', $this
->randomMachineName());
$this
->assertNull($return, 'Phone number settings does not exist.');
$return = $this->phoneNumberVerificationProvider
->getPhoneNumberSettings('entity_test', 'entity_test');
$this
->assertTrue($return instanceof PhoneNumberSettingsInterface);
}
public function testGetPhoneNumberSettingsForEntityNoSettings() {
$test_entity_random_bundle = EntityTest::create([
'name' => $this
->randomMachineName(),
'type' => $this
->randomMachineName(),
]);
$this
->expectException(PhoneNumberSettingsException::class);
$this->phoneNumberVerificationProvider
->getPhoneNumberSettingsForEntity($test_entity_random_bundle);
}
public function testGetPhoneNumberSettingsForEntity() {
$entity = $this
->createEntityWithPhoneNumber($this->phoneNumberSettings);
$return = $this->phoneNumberVerificationProvider
->getPhoneNumberSettingsForEntity($entity);
$this
->assertTrue($return instanceof PhoneNumberSettingsInterface);
}
public function testGetPhoneVerificationByCode() {
$this
->createEntityWithPhoneNumber($this->phoneNumberSettings, [
'+123123123',
]);
$verification = $this
->getLastVerification();
$return = $this->phoneNumberVerificationProvider
->getPhoneVerificationByCode($verification
->getCode());
$this
->assertEquals($return
->id(), $verification
->id());
}
public function testGetPhoneVerificationByFakeCode() {
$this
->createEntityWithPhoneNumber($this->phoneNumberSettings, [
'+123123123',
]);
$return = $this->phoneNumberVerificationProvider
->getPhoneVerificationByCode($this
->randomMachineName());
$this
->assertFalse($return);
}
public function testGetPhoneVerificationByPhoneNumber() {
$phone_number1 = '+123123123';
$this
->createEntityWithPhoneNumber($this->phoneNumberSettings, [
$phone_number1,
]);
$phone_number2 = '+456456456';
$this
->createEntityWithPhoneNumber($this->phoneNumberSettings, [
$phone_number2,
]);
$return = $this->phoneNumberVerificationProvider
->getPhoneVerificationByPhoneNumber($phone_number1, NULL);
$this
->assertEquals(1, count($return));
}
public function testGetPhoneVerificationByPhoneNumberVerified() {
$phone_number1 = '+123123123';
$phone_number2 = '+456456456';
$entity = $this
->createEntityWithPhoneNumber($this->phoneNumberSettings, [
$phone_number1,
$phone_number2,
]);
$this
->verifyPhoneNumber($entity, $phone_number2);
$return = $this->phoneNumberVerificationProvider
->getPhoneVerificationByPhoneNumber($phone_number1, TRUE);
$this
->assertEquals(0, count($return));
$return = $this->phoneNumberVerificationProvider
->getPhoneVerificationByPhoneNumber($phone_number1, FALSE);
$this
->assertEquals(1, count($return));
$return = $this->phoneNumberVerificationProvider
->getPhoneVerificationByPhoneNumber($phone_number2, FALSE);
$this
->assertEquals(0, count($return));
$return = $this->phoneNumberVerificationProvider
->getPhoneVerificationByPhoneNumber($phone_number2, TRUE);
$this
->assertEquals(1, count($return));
}
public function testGetPhoneVerificationByPhoneNumberEntityType() {
$phone_number = '+123123123';
$this
->createEntityWithPhoneNumber($this->phoneNumberSettings, [
$phone_number,
]);
$return = $this->phoneNumberVerificationProvider
->getPhoneVerificationByPhoneNumber($phone_number, NULL, 'entity_test');
$this
->assertEquals(1, count($return));
$return = $this->phoneNumberVerificationProvider
->getPhoneVerificationByPhoneNumber($phone_number, NULL, $this
->randomMachineName());
$this
->assertEquals(0, count($return));
}
public function testGetPhoneVerificationByEntity() {
$phone_number = '+123123123';
$entity = $this
->createEntityWithPhoneNumber($this->phoneNumberSettings, [
$phone_number,
]);
$return = $this->phoneNumberVerificationProvider
->getPhoneVerificationByEntity($entity, $phone_number);
$this
->assertNotFalse($return);
}
public function testGetPhoneVerificationByEntityInvalidPhone() {
$entity = $this
->createEntityWithPhoneNumber($this->phoneNumberSettings, [
'+123123123',
]);
$return = $this->phoneNumberVerificationProvider
->getPhoneVerificationByEntity($entity, '+456456456');
$this
->assertFalse($return);
}
public function testNewPhoneVerification() {
$phone_number = '+123123123';
$entity = $this
->createEntityWithPhoneNumber($this->phoneNumberSettings);
$return = $this->phoneNumberVerificationProvider
->newPhoneVerification($entity, $phone_number);
$this
->assertTrue($return instanceof PhoneNumberVerificationInterface);
$sent_messages = $this
->getTestMessages($this->gateway);
$this
->assertEquals(1, count($sent_messages));
$verification_message = reset($sent_messages);
$this
->assertTrue($verification_message
->getOption('_is_verification_message'));
$verification = $this
->getLastVerification();
$this
->assertEquals($entity
->id(), $verification
->getEntity()
->id());
$this
->assertEquals($phone_number, $verification
->getPhoneNumber());
}
}