3 namespace Drupal\Tests\field\Functional;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\Tests\BrowserTestBase;
8 use Drupal\Tests\Traits\Core\CronRunTrait;
11 * Tests the behavior of a field module after being disabled and re-enabled.
15 class reEnableModuleFieldTest extends BrowserTestBase {
24 public static $modules = [
27 // We use telephone module instead of test_field because test_field is
28 // hidden and does not display on the admin/modules page.
32 protected function setUp() {
35 $this->drupalCreateContentType(['type' => 'article']);
36 $this->drupalLogin($this->drupalCreateUser([
37 'create article content',
38 'edit own article content',
43 * Test the behavior of a field module after being disabled and re-enabled.
45 * @see field_system_info_alter()
47 public function testReEnabledField() {
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',
55 $field_storage->save();
57 'field_storage' => $field_storage,
58 'bundle' => 'article',
59 'label' => 'Telephone Number',
62 entity_get_form_display('node', 'article', 'default')
63 ->setComponent('field_telephone', [
64 'type' => 'telephone_default',
66 'placeholder' => '123-456-7890',
71 entity_get_display('node', 'article', 'default')
72 ->setComponent('field_telephone', [
73 'type' => 'telephone_link',
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.');
82 // Submit an article node with a telephone field so data exist for the
85 'title[0][value]' => $this->randomMachineName(),
86 'field_telephone[0][value]' => "123456789",
88 $this->drupalPostForm(NULL, $edit, t('Save'));
89 $this->assertRaw('<a href="tel:123456789">');
91 // Test that the module can't be uninstalled from the UI while there is data
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");
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',
106 $field_storage2->save();
107 FieldConfig::create([
108 'field_storage' => $field_storage2,
110 'label' => 'User Telephone Number',
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");
116 // Delete both fields.
117 $field_storage->delete();
118 $field_storage2->delete();
120 $this->drupalGet('admin/modules/uninstall');
121 $this->assertText('Fields pending deletion');
123 $this->assertNoText("The Telephone number field type is used in the following field: node.field_telephone");
124 $this->assertNoText('Fields pending deletion');