3 namespace Drupal\comment\Tests;
5 use Drupal\field\Entity\FieldStorageConfig;
6 use Drupal\Core\Extension\ModuleUninstallValidatorException;
7 use Drupal\simpletest\WebTestBase;
10 * Tests comment module uninstallation.
14 class CommentUninstallTest extends WebTestBase {
23 public static $modules = ['comment', 'node'];
25 protected function setUp() {
28 // Create an article content type.
29 $this->drupalCreateContentType(['type' => 'article', 'name' => t('Article')]);
30 // Create comment field on article so that adds 'comment_body' field.
31 $this->addDefaultCommentField('node', 'article');
35 * Tests if comment module uninstallation fails if the field exists.
37 * @throws \Drupal\Core\Extension\ModuleUninstallValidatorException
39 public function testCommentUninstallWithField() {
40 // Ensure that the field exists before uninstallation.
41 $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
42 $this->assertNotNull($field_storage, 'The comment_body field exists.');
44 // Uninstall the comment module which should trigger an exception.
46 $this->container->get('module_installer')->uninstall(['comment']);
47 $this->fail("Expected an exception when uninstall was attempted.");
49 catch (ModuleUninstallValidatorException $e) {
50 $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']);