reEnableModuleFieldTest.php 3.98 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\field\Functional;
4

5
use Drupal\field\Entity\FieldConfig;
6
use Drupal\field\Entity\FieldStorageConfig;
7 8
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\Traits\Core\CronRunTrait;
9 10

/**
11 12 13
 * Tests the behavior of a field module after being disabled and re-enabled.
 *
 * @group field
14
 */
15 16 17
class reEnableModuleFieldTest extends BrowserTestBase {

  use CronRunTrait;
18 19 20 21 22 23

  /**
   * Modules to enable.
   *
   * @var array
   */
24
  public static $modules = [
25 26 27 28
    'field',
    'node',
    // We use telephone module instead of test_field because test_field is
    // hidden and does not display on the admin/modules page.
29
    'telephone',
30
  ];
31

32
  protected function setUp() {
33 34
    parent::setUp();

35 36
    $this->drupalCreateContentType(['type' => 'article']);
    $this->drupalLogin($this->drupalCreateUser([
37 38
      'create article content',
      '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
    // Add a telephone field to the article content type.
49
    $field_storage = FieldStorageConfig::create([
50
      'field_name' => 'field_telephone',
51
      'entity_type' => 'node',
52
      'type' => 'telephone',
53
    ]);
54
    $field_storage->save();
55
    FieldConfig::create([
56
      'field_storage' => $field_storage,
57
      'bundle' => 'article',
58
      'label' => 'Telephone Number',
59
    ])->save();
60

61 62 63
    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
    $display_repository = \Drupal::service('entity_display.repository');
    $display_repository->getFormDisplay('node', 'article')
64
      ->setComponent('field_telephone', [
65
        'type' => 'telephone_default',
66
        'settings' => [
67
          'placeholder' => '123-456-7890',
68 69
        ],
      ])
70 71
      ->save();

72
    $display_repository->getViewDisplay('node', 'article')
73
      ->setComponent('field_telephone', [
74 75
        'type' => 'telephone_link',
        'weight' => 1,
76
      ])
77 78 79 80
      ->save();

    // Display the article node form and verify the telephone widget is present.
    $this->drupalGet('node/add/article');
81
    $this->assertFieldByName("field_telephone[0][value]", '', 'Widget found.');
82 83 84

    // Submit an article node with a telephone field so data exist for the
    // field.
85
    $edit = [
86
      'title[0][value]' => $this->randomMachineName(),
87
      'field_telephone[0][value]' => "123456789",
88
    ];
89
    $this->drupalPostForm(NULL, $edit, t('Save'));
90 91
    $this->assertRaw('<a href="tel:123456789">');

92
    // Test that the module can't be uninstalled from the UI while there is data
93
    // for its fields.
94
    $admin_user = $this->drupalCreateUser(['access administration pages', 'administer modules']);
95
    $this->drupalLogin($admin_user);
96
    $this->drupalGet('admin/modules/uninstall');
97 98 99 100 101
    $this->assertText("The Telephone number field type is used in the following field: node.field_telephone");

    // Add another telephone field to a different entity type in order to test
    // the message for the case when multiple fields are blocking the
    // uninstallation of a module.
102
    $field_storage2 = FieldStorageConfig::create([
103 104 105
      'field_name' => 'field_telephone_2',
      'entity_type' => 'user',
      'type' => 'telephone',
106
    ]);
107
    $field_storage2->save();
108
    FieldConfig::create([
109 110 111
      'field_storage' => $field_storage2,
      'bundle' => 'user',
      'label' => 'User Telephone Number',
112
    ])->save();
113 114 115 116 117

    $this->drupalGet('admin/modules/uninstall');
    $this->assertText("The Telephone number field type is used in the following fields: node.field_telephone, user.field_telephone_2");

    // Delete both fields.
118
    $field_storage->delete();
119 120
    $field_storage2->delete();

121
    $this->drupalGet('admin/modules/uninstall');
122 123
    $this->assertText('Fields pending deletion');
    $this->cronRun();
124
    $this->assertNoText("The Telephone number field type is used in the following field: node.field_telephone");
125 126 127 128
    $this->assertNoText('Fields pending deletion');
  }

}