Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\comment\Functional;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\comment\Tests\CommentTestTrait;
|
Chris@0
|
6 use Drupal\field\Entity\FieldStorageConfig;
|
Chris@0
|
7 use Drupal\Core\Extension\ModuleUninstallValidatorException;
|
Chris@0
|
8 use Drupal\Tests\BrowserTestBase;
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * Tests comment module uninstallation.
|
Chris@0
|
12 *
|
Chris@0
|
13 * @group comment
|
Chris@0
|
14 */
|
Chris@0
|
15 class CommentUninstallTest extends BrowserTestBase {
|
Chris@0
|
16
|
Chris@0
|
17 use CommentTestTrait;
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * Modules to install.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @var array
|
Chris@0
|
23 */
|
Chris@0
|
24 public static $modules = ['comment', 'node'];
|
Chris@0
|
25
|
Chris@0
|
26 protected function setUp() {
|
Chris@0
|
27 parent::setup();
|
Chris@0
|
28
|
Chris@0
|
29 // Create an article content type.
|
Chris@0
|
30 $this->drupalCreateContentType(['type' => 'article', 'name' => t('Article')]);
|
Chris@0
|
31 // Create comment field on article so that adds 'comment_body' field.
|
Chris@0
|
32 $this->addDefaultCommentField('node', 'article');
|
Chris@0
|
33 }
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * Tests if comment module uninstallation fails if the field exists.
|
Chris@0
|
37 *
|
Chris@0
|
38 * @throws \Drupal\Core\Extension\ModuleUninstallValidatorException
|
Chris@0
|
39 */
|
Chris@0
|
40 public function testCommentUninstallWithField() {
|
Chris@0
|
41 // Ensure that the field exists before uninstallation.
|
Chris@0
|
42 $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
|
Chris@0
|
43 $this->assertNotNull($field_storage, 'The comment_body field exists.');
|
Chris@0
|
44
|
Chris@0
|
45 // Uninstall the comment module which should trigger an exception.
|
Chris@0
|
46 try {
|
Chris@0
|
47 $this->container->get('module_installer')->uninstall(['comment']);
|
Chris@0
|
48 $this->fail("Expected an exception when uninstall was attempted.");
|
Chris@0
|
49 }
|
Chris@0
|
50 catch (ModuleUninstallValidatorException $e) {
|
Chris@0
|
51 $this->pass("Caught an exception when uninstall was attempted.");
|
Chris@0
|
52 }
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@0
|
55 /**
|
Chris@0
|
56 * Tests if uninstallation succeeds if the field has been deleted beforehand.
|
Chris@0
|
57 */
|
Chris@0
|
58 public function testCommentUninstallWithoutField() {
|
Chris@0
|
59 // Manually delete the comment_body field before module uninstallation.
|
Chris@0
|
60 $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
|
Chris@0
|
61 $this->assertNotNull($field_storage, 'The comment_body field exists.');
|
Chris@0
|
62 $field_storage->delete();
|
Chris@0
|
63
|
Chris@0
|
64 // Check that the field is now deleted.
|
Chris@0
|
65 $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
|
Chris@0
|
66 $this->assertNull($field_storage, 'The comment_body field has been deleted.');
|
Chris@0
|
67
|
Chris@0
|
68 // Manually delete the comment field on the node before module uninstallation.
|
Chris@0
|
69 $field_storage = FieldStorageConfig::loadByName('node', 'comment');
|
Chris@0
|
70 $this->assertNotNull($field_storage, 'The comment field exists.');
|
Chris@0
|
71 $field_storage->delete();
|
Chris@0
|
72
|
Chris@0
|
73 // Check that the field is now deleted.
|
Chris@0
|
74 $field_storage = FieldStorageConfig::loadByName('node', 'comment');
|
Chris@0
|
75 $this->assertNull($field_storage, 'The comment field has been deleted.');
|
Chris@0
|
76
|
Chris@0
|
77 field_purge_batch(10);
|
Chris@0
|
78 // Ensure that uninstallation succeeds even if the field has already been
|
Chris@0
|
79 // deleted manually beforehand.
|
Chris@0
|
80 $this->container->get('module_installer')->uninstall(['comment']);
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 }
|