X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fcomment%2Fsrc%2FTests%2FCommentUninstallTest.php;fp=web%2Fcore%2Fmodules%2Fcomment%2Fsrc%2FTests%2FCommentUninstallTest.php;h=7873915f03a105c049e0d2441fe99e3ba233f7d4;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/core/modules/comment/src/Tests/CommentUninstallTest.php b/web/core/modules/comment/src/Tests/CommentUninstallTest.php new file mode 100644 index 000000000..7873915f0 --- /dev/null +++ b/web/core/modules/comment/src/Tests/CommentUninstallTest.php @@ -0,0 +1,83 @@ +drupalCreateContentType(['type' => 'article', 'name' => t('Article')]); + // Create comment field on article so that adds 'comment_body' field. + $this->addDefaultCommentField('node', 'article'); + } + + /** + * Tests if comment module uninstallation fails if the field exists. + * + * @throws \Drupal\Core\Extension\ModuleUninstallValidatorException + */ + public function testCommentUninstallWithField() { + // Ensure that the field exists before uninstallation. + $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body'); + $this->assertNotNull($field_storage, 'The comment_body field exists.'); + + // Uninstall the comment module which should trigger an exception. + try { + $this->container->get('module_installer')->uninstall(['comment']); + $this->fail("Expected an exception when uninstall was attempted."); + } + catch (ModuleUninstallValidatorException $e) { + $this->pass("Caught an exception when uninstall was attempted."); + } + } + + + /** + * Tests if uninstallation succeeds if the field has been deleted beforehand. + */ + public function testCommentUninstallWithoutField() { + // Manually delete the comment_body field before module uninstallation. + $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body'); + $this->assertNotNull($field_storage, 'The comment_body field exists.'); + $field_storage->delete(); + + // Check that the field is now deleted. + $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body'); + $this->assertNull($field_storage, 'The comment_body field has been deleted.'); + + // Manually delete the comment field on the node before module uninstallation. + $field_storage = FieldStorageConfig::loadByName('node', 'comment'); + $this->assertNotNull($field_storage, 'The comment field exists.'); + $field_storage->delete(); + + // Check that the field is now deleted. + $field_storage = FieldStorageConfig::loadByName('node', 'comment'); + $this->assertNull($field_storage, 'The comment field has been deleted.'); + + field_purge_batch(10); + // Ensure that uninstallation succeeds even if the field has already been + // deleted manually beforehand. + $this->container->get('module_installer')->uninstall(['comment']); + } + +}