SmsFrameworkVerificationMaintenanceTest.php in SMS Framework 8
File
tests/src/Kernel/SmsFrameworkVerificationMaintenanceTest.php
View source
<?php
namespace Drupal\Tests\sms\Kernel;
use Drupal\sms\Entity\PhoneNumberSettings;
use Drupal\sms\Entity\PhoneNumberVerificationInterface;
use Drupal\Component\Utility\Unicode;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
use Drupal\entity_test\Entity\EntityTest;
class SmsFrameworkVerificationMaintenanceTest extends SmsFrameworkKernelBase {
public static $modules = [
'sms',
'sms_test_gateway',
'entity_test',
'user',
'field',
'telephone',
'dynamic_entity_reference',
];
protected $phoneNumberSettings;
protected $phoneField;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('entity_test');
$this
->installEntitySchema('sms_phone_number_verification');
$this->phoneField = FieldStorageConfig::create([
'entity_type' => 'entity_test',
'field_name' => Unicode::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())
->setPurgeVerificationPhoneNumber(FALSE)
->setVerificationCodeLifetime(3600)
->setVerificationMessage($this
->randomString())
->save();
$gateway = $this
->createMemoryGateway([
'skip_queue' => TRUE,
]);
$this
->setFallbackGateway($gateway);
$this->testEntity = $this
->createEntityWithPhoneNumber($this->phoneNumberSettings, [
'+123123123',
]);
}
public function testVerificationUnverifiedNotExpired() {
$this
->getLastVerification()
->setStatus(FALSE)
->save();
$this->container
->get('cron')
->run();
$this
->assertTrue($this
->getLastVerification() instanceof PhoneNumberVerificationInterface);
}
public function testVerificationUnverifiedExpired() {
$this
->getLastVerification()
->setStatus(FALSE)
->set('created', 0)
->save();
$this->container
->get('cron')
->run();
$this
->assertFalse($this
->getLastVerification());
}
public function testVerificationUnverifiedExpiredNoPurgeFieldData() {
$this
->getLastVerification()
->setStatus(FALSE)
->set('created', 0)
->save();
$this->container
->get('cron')
->run();
$this->testEntity = EntityTest::load($this->testEntity
->id());
$this
->assertNotEmpty($this->testEntity->{$this->phoneField
->getName()});
}
public function testVerificationUnverifiedExpiredPurgeFieldData() {
$this->phoneNumberSettings
->setPurgeVerificationPhoneNumber(TRUE)
->save();
$this
->getLastVerification()
->setStatus(FALSE)
->set('created', 0)
->save();
$this->container
->get('cron')
->run();
$this->testEntity = EntityTest::load($this->testEntity
->id());
$this
->assertEmpty($this->testEntity->{$this->phoneField
->getName()});
}
public function testVerificationVerifiedExpired() {
$this
->getLastVerification()
->setStatus(TRUE)
->set('created', 0)
->save();
$this->container
->get('cron')
->run();
$this
->assertTrue($this
->getLastVerification() instanceof PhoneNumberVerificationInterface);
}
}