3 namespace Drupal\Tests\comment\Functional;
5 use Drupal\comment\Tests\CommentTestTrait;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\Core\Extension\ModuleUninstallValidatorException;
8 use Drupal\Tests\BrowserTestBase;
11 * Tests comment module uninstallation.
15 class CommentUninstallTest extends BrowserTestBase {
24 public static $modules = ['comment', 'node'];
26 protected function setUp() {
29 // Create an article content type.
30 $this->drupalCreateContentType(['type' => 'article', 'name' => t('Article')]);
31 // Create comment field on article so that adds 'comment_body' field.
32 $this->addDefaultCommentField('node', 'article');
36 * Tests if comment module uninstallation fails if the field exists.
38 * @throws \Drupal\Core\Extension\ModuleUninstallValidatorException
40 public function testCommentUninstallWithField() {
41 // Ensure that the field exists before uninstallation.
42 $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
43 $this->assertNotNull($field_storage, 'The comment_body field exists.');
45 // Uninstall the comment module which should trigger an exception.
47 $this->container->get('module_installer')->uninstall(['comment']);
48 $this->fail("Expected an exception when uninstall was attempted.");
50 catch (ModuleUninstallValidatorException $e) {
51 $this->pass("Caught an exception when uninstall was attempted.");
56 * Tests if uninstallation succeeds if the field has been deleted beforehand.
58 public function testCommentUninstallWithoutField() {
59 // Manually delete the comment_body field before module uninstallation.
60 $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
61 $this->assertNotNull($field_storage, 'The comment_body field exists.');
62 $field_storage->delete();
64 // Check that the field is now deleted.
65 $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
66 $this->assertNull($field_storage, 'The comment_body field has been deleted.');
68 // Manually delete the comment field on the node before module uninstallation.
69 $field_storage = FieldStorageConfig::loadByName('node', 'comment');
70 $this->assertNotNull($field_storage, 'The comment field exists.');
71 $field_storage->delete();
73 // Check that the field is now deleted.
74 $field_storage = FieldStorageConfig::loadByName('node', 'comment');
75 $this->assertNull($field_storage, 'The comment field has been deleted.');
77 field_purge_batch(10);
78 // Ensure that uninstallation succeeds even if the field has already been
79 // deleted manually beforehand.
80 $this->container->get('module_installer')->uninstall(['comment']);