Version 1
[yaffs-website] / web / core / modules / comment / src / Tests / CommentUninstallTest.php
1 <?php
2
3 namespace Drupal\comment\Tests;
4
5 use Drupal\field\Entity\FieldStorageConfig;
6 use Drupal\Core\Extension\ModuleUninstallValidatorException;
7 use Drupal\simpletest\WebTestBase;
8
9 /**
10  * Tests comment module uninstallation.
11  *
12  * @group comment
13  */
14 class CommentUninstallTest extends WebTestBase {
15
16   use CommentTestTrait;
17
18   /**
19    * Modules to install.
20    *
21    * @var array
22    */
23   public static $modules = ['comment', 'node'];
24
25   protected function setUp() {
26     parent::setup();
27
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');
32   }
33
34   /**
35    * Tests if comment module uninstallation fails if the field exists.
36    *
37    * @throws \Drupal\Core\Extension\ModuleUninstallValidatorException
38    */
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.');
43
44     // Uninstall the comment module which should trigger an exception.
45     try {
46       $this->container->get('module_installer')->uninstall(['comment']);
47       $this->fail("Expected an exception when uninstall was attempted.");
48     }
49     catch (ModuleUninstallValidatorException $e) {
50       $this->pass("Caught an exception when uninstall was attempted.");
51     }
52   }
53
54
55   /**
56    * Tests if uninstallation succeeds if the field has been deleted beforehand.
57    */
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();
63
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.');
67
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();
72
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.');
76
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']);
81   }
82
83 }