3 namespace Drupal\field\Tests;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\simpletest\WebTestBase;
7 use Drupal\field\Entity\FieldStorageConfig;
10 * Tests the behavior of a field module after being disabled and re-enabled.
14 class reEnableModuleFieldTest extends WebTestBase {
21 public static $modules = [
24 // We use telephone module instead of test_field because test_field is
25 // hidden and does not display on the admin/modules page.
29 protected function setUp() {
32 $this->drupalCreateContentType(['type' => 'article']);
33 $this->drupalLogin($this->drupalCreateUser([
34 'create article content',
35 'edit own article content',
40 * Test the behavior of a field module after being disabled and re-enabled.
42 * @see field_system_info_alter()
44 public function testReEnabledField() {
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',
52 $field_storage->save();
54 'field_storage' => $field_storage,
55 'bundle' => 'article',
56 'label' => 'Telephone Number',
59 entity_get_form_display('node', 'article', 'default')
60 ->setComponent('field_telephone', [
61 'type' => 'telephone_default',
63 'placeholder' => '123-456-7890',
68 entity_get_display('node', 'article', 'default')
69 ->setComponent('field_telephone', [
70 'type' => 'telephone_link',
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.');
79 // Submit an article node with a telephone field so data exist for the
82 'title[0][value]' => $this->randomMachineName(),
83 'field_telephone[0][value]' => "123456789",
85 $this->drupalPostForm(NULL, $edit, t('Save'));
86 $this->assertRaw('<a href="tel:123456789">');
88 // Test that the module can't be uninstalled from the UI while there is data
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");
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',
103 $field_storage2->save();
104 FieldConfig::create([
105 'field_storage' => $field_storage2,
107 'label' => 'User Telephone Number',
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");
113 // Delete both fields.
114 $field_storage->delete();
115 $field_storage2->delete();
117 $this->drupalGet('admin/modules/uninstall');
118 $this->assertText('Fields pending deletion');
120 $this->assertNoText("The Telephone number field type is used in the following field: node.field_telephone");
121 $this->assertNoText('Fields pending deletion');