3 namespace Drupal\Tests\telephone\Functional;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\field\Entity\FieldStorageConfig;
10 * Tests the creation of telephone fields.
14 class TelephoneFieldTest extends BrowserTestBase {
21 public static $modules = [
28 * A user with permission to create articles.
30 * @var \Drupal\user\UserInterface
37 protected function setUp() {
40 $this->drupalCreateContentType(['type' => 'article']);
41 $this->webUser = $this->drupalCreateUser(['create article content', 'edit own article content']);
42 $this->drupalLogin($this->webUser);
44 // Add the telephone field to the article content type.
45 FieldStorageConfig::create([
46 'field_name' => 'field_telephone',
47 'entity_type' => 'node',
48 'type' => 'telephone',
51 'field_name' => 'field_telephone',
52 'label' => 'Telephone Number',
53 'entity_type' => 'node',
54 'bundle' => 'article',
57 entity_get_form_display('node', 'article', 'default')
58 ->setComponent('field_telephone', [
59 'type' => 'telephone_default',
61 'placeholder' => '123-456-7890',
66 entity_get_display('node', 'article', 'default')
67 ->setComponent('field_telephone', [
68 'type' => 'telephone_link',
75 * Test to confirm the widget is setup.
77 * @covers \Drupal\telephone\Plugin\Field\FieldWidget\TelephoneDefaultWidget::formElement
79 public function testTelephoneWidget() {
80 $this->drupalGet('node/add/article');
81 $this->assertFieldByName("field_telephone[0][value]", '', 'Widget found.');
82 $this->assertRaw('placeholder="123-456-7890"');
86 * Test the telephone formatter.
88 * @covers \Drupal\telephone\Plugin\Field\FieldFormatter\TelephoneLinkFormatter::viewElements
90 * @dataProvider providerPhoneNumbers
92 public function testTelephoneFormatter($input, $expected) {
93 // Test basic entry of telephone field.
95 'title[0][value]' => $this->randomMachineName(),
96 'field_telephone[0][value]' => $input,
99 $this->drupalPostForm('node/add/article', $edit, t('Save'));
100 $this->assertRaw('<a href="tel:' . $expected . '">');
104 * Provides the phone numbers to check and expected results.
106 public function providerPhoneNumbers() {
108 'standard phone number' => ['123456789', '123456789'],
109 'whitespace is removed' => ['1234 56789', '123456789'],
110 'parse_url(0) return FALSE workaround' => ['0', '0-'],
111 'php bug 70588 workaround - lower edge check' => ['1', '1-'],
112 'php bug 70588 workaround' => ['123', '1-23'],
113 'php bug 70588 workaround - with whitespace removal' => ['1 2 3 4 5', '1-2345'],
114 'php bug 70588 workaround - upper edge check' => ['65534', '6-5534'],
115 'php bug 70588 workaround - edge check' => ['65535', '6-5535'],
116 'php bug 70588 workaround - invalid port number - lower edge check' => ['65536', '6-5536'],
117 'php bug 70588 workaround - invalid port number - upper edge check' => ['99999', '9-9999'],
118 'lowest number not affected by php bug 70588' => ['100000', '100000'],