View source
<?php
namespace Drupal\Tests\phone_international\Functional;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\BrowserTestBase;
class PhoneInternationalFieldTest extends BrowserTestBase {
public static $modules = [
'node',
'entity_test',
'field_ui',
'phone_international',
];
protected $defaultTheme = 'stark';
protected $fieldname;
protected $fieldStorage;
protected $field;
protected function setUp() : void {
parent::setUp();
$this
->drupalLogin($this
->drupalCreateUser([
'view test entity',
'administer entity_test content',
'administer content types',
]));
$this->fieldname = mb_strtolower($this
->randomMachineName());
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => $this->fieldname,
'entity_type' => 'entity_test',
'type' => 'phone_international',
]);
$this->fieldStorage
->save();
$this->field = FieldConfig::create([
'field_storage' => $this->fieldStorage,
'bundle' => 'entity_test',
]);
$this->field
->save();
$display_repository = \Drupal::service('entity_display.repository');
$display_repository
->getFormDisplay('entity_test', 'entity_test')
->setComponent($this->fieldname, [
'type' => 'phone_international_widget',
'settings' => [
'geolocation' => 'PT',
'initial_country' => 0,
'exclude_countries' => [],
'preferred_countries' => [],
],
])
->save();
$display_repository
->getViewDisplay('entity_test', 'entity_test', 'full')
->setComponent($this->fieldname, [
'type' => 'phone_international_formatter',
])
->save();
}
public function testTelephoneWidget() {
$this
->drupalGet('entity_test/add');
$this
->assertSession()
->fieldValueEquals("{$this->fieldname}[0][value][int_phone]", '');
}
public function testTelephoneFormatter($input, $expected) {
$entity = EntityTest::create();
$entity->{$this->fieldname}->value = $input;
$entity
->save();
$id = $entity
->id();
$entity = EntityTest::load($id);
$entity = EntityTest::load($entity
->id());
$this
->assertEquals($expected, $entity->{$this->fieldname}->value);
$this
->drupalGet($entity
->toUrl());
$this
->assertSession()
->responseContains($expected);
}
public function providerPhoneNumbers() {
return [
'standard phone number' => [
'+123456789',
'+123456789',
],
'standard phone number country' => [
'+351123456789',
'+351123456789',
],
];
}
}