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