TelephoneItemTest.php in Telephone Validation 8.2
File
tests/src/Kernel/TelephoneItemTest.php
View source
<?php
namespace Drupal\Tests\telephone_validation\Kernel;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\telephone\Kernel\TelephoneItemTest as BaseItemTest;
use libphonenumber\PhoneNumberFormat;
class TelephoneItemTest extends BaseItemTest {
public static $modules = [
'telephone_validation',
];
protected function setUp() : void {
parent::setUp();
$config = FieldConfig::loadByName('entity_test', 'entity_test', 'field_test');
$config
->setThirdPartySetting('telephone_validation', 'format', PhoneNumberFormat::NATIONAL);
$config
->setThirdPartySetting('telephone_validation', 'country', [
'CA',
]);
$config
->save();
}
public function testTestItem() {
$value = '2507638884';
$entity = EntityTest::create();
$entity->field_test = $value;
$entity->name->value = $this
->randomMachineName();
$this
->entityValidateAndSave($entity);
}
public function testInvalidTelephoneNumber() {
$value = '999999';
$entity = EntityTest::create();
$entity->field_test = $value;
$entity->name->value = $this
->randomMachineName();
$violations = $entity
->validate();
$this
->assertEquals(count($violations), 1);
$this
->assertEquals($violations[0]
->getMessage(), '999999 is not a valid phone number.');
}
}