18bedf0725ebeef363a0e4d65b3de06a3875bd9f
[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   protected function setUp() {
35     parent::setUp();
36
37     $this->drupalCreateContentType(['type' => 'article']);
38     $this->webUser = $this->drupalCreateUser(['create article content', 'edit own article content']);
39     $this->drupalLogin($this->webUser);
40   }
41
42   // Test fields.
43
44   /**
45    * Helper function for testTelephoneField().
46    */
47   public function testTelephoneField() {
48
49     // Add the telephone field to the article content type.
50     FieldStorageConfig::create([
51       'field_name' => 'field_telephone',
52       'entity_type' => 'node',
53       'type' => 'telephone',
54     ])->save();
55     FieldConfig::create([
56       'field_name' => 'field_telephone',
57       'label' => 'Telephone Number',
58       'entity_type' => 'node',
59       'bundle' => 'article',
60     ])->save();
61
62     entity_get_form_display('node', 'article', 'default')
63       ->setComponent('field_telephone', [
64         'type' => 'telephone_default',
65         'settings' => [
66           'placeholder' => '123-456-7890',
67         ],
68       ])
69       ->save();
70
71     entity_get_display('node', 'article', 'default')
72       ->setComponent('field_telephone', [
73         'type' => 'telephone_link',
74         'weight' => 1,
75       ])
76       ->save();
77
78     // Display creation form.
79     $this->drupalGet('node/add/article');
80     $this->assertFieldByName("field_telephone[0][value]", '', 'Widget found.');
81     $this->assertRaw('placeholder="123-456-7890"');
82
83     // Test basic entry of telephone field.
84     $edit = [
85       'title[0][value]' => $this->randomMachineName(),
86       'field_telephone[0][value]' => "123456789",
87     ];
88
89     $this->drupalPostForm(NULL, $edit, t('Save'));
90     $this->assertRaw('<a href="tel:123456789">', 'A telephone link is provided on the article node page.');
91
92     // Add number with a space in it. Need to ensure it is stripped on output.
93     $edit = [
94       'title[0][value]' => $this->randomMachineName(),
95       'field_telephone[0][value]' => "1234 56789",
96     ];
97
98     $this->drupalPostForm('node/add/article', $edit, t('Save'));
99     $this->assertRaw('<a href="tel:123456789">', 'Telephone link is output with whitespace removed.');
100   }
101
102 }