annotate core/modules/comment/tests/src/Functional/CommentUninstallTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
rev   line source
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 /**
Chris@0 57 * Tests if uninstallation succeeds if the field has been deleted beforehand.
Chris@0 58 */
Chris@0 59 public function testCommentUninstallWithoutField() {
Chris@0 60 // Manually delete the comment_body field before module uninstallation.
Chris@0 61 $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
Chris@0 62 $this->assertNotNull($field_storage, 'The comment_body field exists.');
Chris@0 63 $field_storage->delete();
Chris@0 64
Chris@0 65 // Check that the field is now deleted.
Chris@0 66 $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
Chris@0 67 $this->assertNull($field_storage, 'The comment_body field has been deleted.');
Chris@0 68
Chris@0 69 // Manually delete the comment field on the node before module uninstallation.
Chris@0 70 $field_storage = FieldStorageConfig::loadByName('node', 'comment');
Chris@0 71 $this->assertNotNull($field_storage, 'The comment field exists.');
Chris@0 72 $field_storage->delete();
Chris@0 73
Chris@0 74 // Check that the field is now deleted.
Chris@0 75 $field_storage = FieldStorageConfig::loadByName('node', 'comment');
Chris@0 76 $this->assertNull($field_storage, 'The comment field has been deleted.');
Chris@0 77
Chris@0 78 field_purge_batch(10);
Chris@0 79 // Ensure that uninstallation succeeds even if the field has already been
Chris@0 80 // deleted manually beforehand.
Chris@0 81 $this->container->get('module_installer')->uninstall(['comment']);
Chris@0 82 }
Chris@0 83
Chris@0 84 }