Commit 684c23c9 authored by catch's avatar catch

Issue #3049637 by claudiu.cristea, Lendude: Convert CommentUninstallTest into a Kernel test

parent 23fad5b6
<?php
namespace Drupal\Tests\comment\Functional;
namespace Drupal\Tests\comment\Kernel;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Core\Extension\ModuleUninstallValidatorException;
use Drupal\Tests\BrowserTestBase;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\NodeType;
/**
* Tests comment module uninstallation.
* Tests comment module uninstall.
*
* @group comment
*/
class CommentUninstallTest extends BrowserTestBase {
class CommentUninstallTest extends KernelTestBase {
use CommentTestTrait;
/**
* Modules to install.
*
* @var array
* {@inheritdoc}
*/
public static $modules = ['comment', 'node'];
protected static $modules = [
'comment',
'field',
'node',
'system',
'text',
'user',
];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setup();
parent::setUp();
$this->installEntitySchema('comment');
$this->installConfig(['comment']);
$this->installSchema('user', ['users_data']);
// Create an article content type.
$this->drupalCreateContentType(['type' => 'article', 'name' => t('Article')]);
// Create comment field on article so that adds 'comment_body' field.
NodeType::create(['type' => 'article'])->save();
// Create comment field on article so that it adds 'comment_body' field.
FieldStorageConfig::create([
'type' => 'text_long',
'entity_type' => 'comment',
'field_name' => 'comment',
])->save();
$this->addDefaultCommentField('node', 'article');
}
/**
* Tests if comment module uninstallation fails if the field exists.
*
* @throws \Drupal\Core\Extension\ModuleUninstallValidatorException
* Tests if comment module uninstall fails if the field exists.
*/
public function testCommentUninstallWithField() {
// Ensure that the field exists before uninstallation.
// Ensure that the field exists before uninstalling.
$field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
$this->assertNotNull($field_storage, 'The comment_body field exists.');
$this->assertNotNull($field_storage);
// 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.");
}
$this->expectException(ModuleUninstallValidatorException::class);
$this->expectExceptionMessage('The following reasons prevent the modules from being uninstalled: The <em class="placeholder">Comments</em> field type is used in the following field: node.comment');
$this->container->get('module_installer')->uninstall(['comment']);
}
/**
* 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();
// Tests if uninstall succeeds if the field has been deleted beforehand.
// Manually delete the comment_body field before module uninstall.
FieldStorageConfig::loadByName('comment', 'comment_body')->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.');
$this->assertNull($field_storage);
// Manually delete the comment field on the node before module uninstallation.
// Manually delete the comment field on the node before module uninstall.
$field_storage = FieldStorageConfig::loadByName('node', 'comment');
$this->assertNotNull($field_storage, 'The comment field exists.');
$this->assertNotNull($field_storage);
$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.');
$this->assertNull($field_storage);
field_purge_batch(10);
// Ensure that uninstallation succeeds even if the field has already been
// deleted manually beforehand.
// Ensure that uninstall succeeds even if the field has already been deleted
// manually beforehand.
$this->container->get('module_installer')->uninstall(['comment']);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment