View source
<?php
namespace Drupal\Tests\phone_international\Functional;
use Drupal;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\BrowserTestBase;
class PhoneInternationalFieldTest extends BrowserTestBase {
public static $modules = [
'field',
'node',
'phone_international',
];
protected $defaultTheme = 'stark';
protected $webUser;
public function testTelephoneWidget() {
$this
->drupalGet('node/add/article');
$this
->assertSession()
->fieldExists("field_phone_international[0][value]");
}
public function testTelephoneFormatter($input, $expected) {
$edit = [
'title[0][value]' => $this
->randomMachineName(),
'field_phone_international[0][value]' => $input,
];
$this
->drupalPostForm('node/add/article', $edit, t('Save'));
$this
->assertSession()
->responseContains($expected);
}
public function providerPhoneNumbers() {
return [
'standard phone number' => [
'+123456789',
'+123456789',
],
'standard phone number country' => [
'+351123456789',
'+351123456789',
],
];
}
protected function setUp() : void {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'article',
]);
$this->webUser = $this
->drupalCreateUser([
'create article content',
'edit own article content',
]);
$this
->drupalLogin($this->webUser);
FieldStorageConfig::create([
'field_name' => 'field_phone_international',
'entity_type' => 'node',
'type' => 'phone_international',
])
->save();
FieldConfig::create([
'field_name' => 'field_phone_international',
'label' => 'International Phone Number',
'entity_type' => 'node',
'bundle' => 'article',
])
->save();
$display_repository = Drupal::service('entity_display.repository');
$display_repository
->getFormDisplay('node', 'article')
->setComponent('field_phone_international', [
'type' => 'phone_international_widget',
'settings' => [
'geolocation' => 'PT',
'initial_country' => 0,
],
])
->save();
$display_repository
->getViewDisplay('node', 'article')
->setComponent('field_phone_international', [
'type' => 'phone_international_formatter',
'weight' => 1,
])
->save();
}
}