Chris@0: drupalCreateContentType(['type' => 'article', 'name' => t('Article')]); Chris@0: // Create comment field on article so that adds 'comment_body' field. Chris@0: $this->addDefaultCommentField('node', 'article'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests if comment module uninstallation fails if the field exists. Chris@0: * Chris@0: * @throws \Drupal\Core\Extension\ModuleUninstallValidatorException Chris@0: */ Chris@0: public function testCommentUninstallWithField() { Chris@0: // Ensure that the field exists before uninstallation. Chris@0: $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body'); Chris@0: $this->assertNotNull($field_storage, 'The comment_body field exists.'); Chris@0: Chris@0: // Uninstall the comment module which should trigger an exception. Chris@0: try { Chris@0: $this->container->get('module_installer')->uninstall(['comment']); Chris@0: $this->fail("Expected an exception when uninstall was attempted."); Chris@0: } Chris@0: catch (ModuleUninstallValidatorException $e) { Chris@0: $this->pass("Caught an exception when uninstall was attempted."); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests if uninstallation succeeds if the field has been deleted beforehand. Chris@0: */ Chris@0: public function testCommentUninstallWithoutField() { Chris@0: // Manually delete the comment_body field before module uninstallation. Chris@0: $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body'); Chris@0: $this->assertNotNull($field_storage, 'The comment_body field exists.'); Chris@0: $field_storage->delete(); Chris@0: Chris@0: // Check that the field is now deleted. Chris@0: $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body'); Chris@0: $this->assertNull($field_storage, 'The comment_body field has been deleted.'); Chris@0: Chris@0: // Manually delete the comment field on the node before module uninstallation. Chris@0: $field_storage = FieldStorageConfig::loadByName('node', 'comment'); Chris@0: $this->assertNotNull($field_storage, 'The comment field exists.'); Chris@0: $field_storage->delete(); Chris@0: Chris@0: // Check that the field is now deleted. Chris@0: $field_storage = FieldStorageConfig::loadByName('node', 'comment'); Chris@0: $this->assertNull($field_storage, 'The comment field has been deleted.'); Chris@0: Chris@0: field_purge_batch(10); Chris@0: // Ensure that uninstallation succeeds even if the field has already been Chris@0: // deleted manually beforehand. Chris@0: $this->container->get('module_installer')->uninstall(['comment']); Chris@0: } Chris@0: Chris@0: }