8d351819397b255ecd8a57546622e3876f266b36
[yaffs-website] / web / core / modules / telephone / tests / src / Functional / TelephoneFieldTest.php
1 <?php
2
3 namespace Drupal\Tests\telephone\Functional;
4
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\field\Entity\FieldStorageConfig;
8
9 /**
10  * Tests the creation of telephone fields.
11  *
12  * @group telephone
13  */
14 class TelephoneFieldTest extends BrowserTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = [
22     'field',
23     'node',
24     'telephone',
25   ];
26
27   /**
28    * A user with permission to create articles.
29    *
30    * @var \Drupal\user\UserInterface
31    */
32   protected $webUser;
33
34   /**
35    * {@inheritdoc}
36    */
37   protected function setUp() {
38     parent::setUp();
39
40     $this->drupalCreateContentType(['type' => 'article']);
41     $this->webUser = $this->drupalCreateUser(['create article content', 'edit own article content']);
42     $this->drupalLogin($this->webUser);
43
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',
49     ])->save();
50     FieldConfig::create([
51       'field_name' => 'field_telephone',
52       'label' => 'Telephone Number',
53       'entity_type' => 'node',
54       'bundle' => 'article',
55     ])->save();
56
57     entity_get_form_display('node', 'article', 'default')
58       ->setComponent('field_telephone', [
59         'type' => 'telephone_default',
60         'settings' => [
61           'placeholder' => '123-456-7890',
62         ],
63       ])
64       ->save();
65
66     entity_get_display('node', 'article', 'default')
67       ->setComponent('field_telephone', [
68         'type' => 'telephone_link',
69         'weight' => 1,
70       ])
71       ->save();
72   }
73
74   /**
75    * Test to confirm the widget is setup.
76    *
77    * @covers \Drupal\telephone\Plugin\Field\FieldWidget\TelephoneDefaultWidget::formElement
78    */
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"');
83   }
84
85   /**
86    * Test the telephone formatter.
87    *
88    * @covers \Drupal\telephone\Plugin\Field\FieldFormatter\TelephoneLinkFormatter::viewElements
89    *
90    * @dataProvider providerPhoneNumbers
91    */
92   public function testTelephoneFormatter($input, $expected) {
93     // Test basic entry of telephone field.
94     $edit = [
95       'title[0][value]' => $this->randomMachineName(),
96       'field_telephone[0][value]' => $input,
97     ];
98
99     $this->drupalPostForm('node/add/article', $edit, t('Save'));
100     $this->assertRaw('<a href="tel:' . $expected . '">');
101   }
102
103   /**
104    * Provides the phone numbers to check and expected results.
105    */
106   public function providerPhoneNumbers() {
107     return [
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'],
119     ];
120   }
121
122 }