CommentUninstallTest.php in Drupal 10        
                          
                  
                        
  
  
  
  
File
  core/modules/comment/tests/src/Kernel/CommentUninstallTest.php
  
    View source  
  <?php
namespace Drupal\Tests\comment\Kernel;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\Extension\ModuleUninstallValidatorException;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\NodeType;
class CommentUninstallTest extends KernelTestBase {
  use CommentTestTrait;
  
  protected static $modules = [
    'comment',
    'field',
    'node',
    'system',
    'text',
    'user',
  ];
  
  protected function setUp() : void {
    parent::setUp();
    $this
      ->installEntitySchema('comment');
    $this
      ->installConfig([
      'comment',
    ]);
    $this
      ->installSchema('user', [
      'users_data',
    ]);
    NodeType::create([
      'type' => 'article',
    ])
      ->save();
    
    FieldStorageConfig::create([
      'type' => 'text_long',
      'entity_type' => 'comment',
      'field_name' => 'comment',
    ])
      ->save();
    $this
      ->addDefaultCommentField('node', 'article');
  }
  
  public function testCommentUninstallWithField() {
    
    $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
    $this
      ->assertNotNull($field_storage);
    
    $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',
    ]);
  }
  
  public function testCommentUninstallWithoutField() {
    
    FieldStorageConfig::loadByName('comment', 'comment_body')
      ->delete();
    
    $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
    $this
      ->assertNull($field_storage);
    
    $field_storage = FieldStorageConfig::loadByName('node', 'comment');
    $this
      ->assertNotNull($field_storage);
    $field_storage
      ->delete();
    
    $field_storage = FieldStorageConfig::loadByName('node', 'comment');
    $this
      ->assertNull($field_storage);
    field_purge_batch(10);
    
    $this->container
      ->get('module_installer')
      ->uninstall([
      'comment',
    ]);
  }
}