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