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