59aaf229053814587b17acee943d1b4d97452795
[yaffs-website] / web / core / modules / field / tests / src / Functional / reEnableModuleFieldTest.php
1 <?php
2
3 namespace Drupal\Tests\field\Functional;
4
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\Tests\BrowserTestBase;
8 use Drupal\Tests\Traits\Core\CronRunTrait;
9
10 /**
11  * Tests the behavior of a field module after being disabled and re-enabled.
12  *
13  * @group field
14  */
15 class reEnableModuleFieldTest extends BrowserTestBase {
16
17   use CronRunTrait;
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = [
25     'field',
26     'node',
27     // We use telephone module instead of test_field because test_field is
28     // hidden and does not display on the admin/modules page.
29     'telephone',
30   ];
31
32   protected function setUp() {
33     parent::setUp();
34
35     $this->drupalCreateContentType(['type' => 'article']);
36     $this->drupalLogin($this->drupalCreateUser([
37       'create article content',
38       'edit own article content',
39     ]));
40   }
41
42   /**
43    * Test the behavior of a field module after being disabled and re-enabled.
44    *
45    * @see field_system_info_alter()
46    */
47   public function testReEnabledField() {
48
49     // Add a telephone field to the article content type.
50     $field_storage = FieldStorageConfig::create([
51       'field_name' => 'field_telephone',
52       'entity_type' => 'node',
53       'type' => 'telephone',
54     ]);
55     $field_storage->save();
56     FieldConfig::create([
57       'field_storage' => $field_storage,
58       'bundle' => 'article',
59       'label' => 'Telephone Number',
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 the article node form and verify the telephone widget is present.
79     $this->drupalGet('node/add/article');
80     $this->assertFieldByName("field_telephone[0][value]", '', 'Widget found.');
81
82     // Submit an article node with a telephone field so data exist for the
83     // field.
84     $edit = [
85       'title[0][value]' => $this->randomMachineName(),
86       'field_telephone[0][value]' => "123456789",
87     ];
88     $this->drupalPostForm(NULL, $edit, t('Save'));
89     $this->assertRaw('<a href="tel:123456789">');
90
91     // Test that the module can't be uninstalled from the UI while there is data
92     // for its fields.
93     $admin_user = $this->drupalCreateUser(['access administration pages', 'administer modules']);
94     $this->drupalLogin($admin_user);
95     $this->drupalGet('admin/modules/uninstall');
96     $this->assertText("The Telephone number field type is used in the following field: node.field_telephone");
97
98     // Add another telephone field to a different entity type in order to test
99     // the message for the case when multiple fields are blocking the
100     // uninstallation of a module.
101     $field_storage2 = entity_create('field_storage_config', [
102       'field_name' => 'field_telephone_2',
103       'entity_type' => 'user',
104       'type' => 'telephone',
105     ]);
106     $field_storage2->save();
107     FieldConfig::create([
108       'field_storage' => $field_storage2,
109       'bundle' => 'user',
110       'label' => 'User Telephone Number',
111     ])->save();
112
113     $this->drupalGet('admin/modules/uninstall');
114     $this->assertText("The Telephone number field type is used in the following fields: node.field_telephone, user.field_telephone_2");
115
116     // Delete both fields.
117     $field_storage->delete();
118     $field_storage2->delete();
119
120     $this->drupalGet('admin/modules/uninstall');
121     $this->assertText('Fields pending deletion');
122     $this->cronRun();
123     $this->assertNoText("The Telephone number field type is used in the following field: node.field_telephone");
124     $this->assertNoText('Fields pending deletion');
125   }
126
127 }