function NodeTypeTest::testNodeTypeDeletion

Same name in other branches
  1. 9 core/modules/node/tests/src/Functional/NodeTypeTest.php \Drupal\Tests\node\Functional\NodeTypeTest::testNodeTypeDeletion()
  2. 8.9.x core/modules/node/tests/src/Functional/NodeTypeTest.php \Drupal\Tests\node\Functional\NodeTypeTest::testNodeTypeDeletion()
  3. 10 core/modules/node/tests/src/Functional/NodeTypeTest.php \Drupal\Tests\node\Functional\NodeTypeTest::testNodeTypeDeletion()

Tests deleting a content type that still has content.

File

core/modules/node/tests/src/Functional/NodeTypeTest.php, line 181

Class

NodeTypeTest
Ensures that node type functions work correctly.

Namespace

Drupal\Tests\node\Functional

Code

public function testNodeTypeDeletion() : void {
    $this->drupalPlaceBlock('page_title_block');
    // Create a content type programmatically.
    $type = $this->drupalCreateContentType();
    // Log in a test user.
    $web_user = $this->drupalCreateUser([
        'bypass node access',
        'administer content types',
    ]);
    $this->drupalLogin($web_user);
    // Add a new node of this type.
    $node = $this->drupalCreateNode([
        'type' => $type->id(),
    ]);
    // Attempt to delete the content type, which should not be allowed.
    $this->drupalGet('admin/structure/types/manage/' . $type->label() . '/delete');
    $this->assertSession()
        ->pageTextContains("{$type->label()} is used by 1 piece of content on your site. You can not remove this content type until you have removed all of the {$type->label()} content.");
    $this->assertSession()
        ->pageTextNotContains('This action cannot be undone.');
    // Delete the node.
    $node->delete();
    // Attempt to delete the content type, which should now be allowed.
    $this->drupalGet('admin/structure/types/manage/' . $type->label() . '/delete');
    $this->assertSession()
        ->pageTextContains("Are you sure you want to delete the content type {$type->label()}?");
    $this->assertSession()
        ->pageTextContains('This action cannot be undone.');
    // Test that a locked node type could not be deleted.
    $this->container
        ->get('module_installer')
        ->install([
        'node_test_config',
    ]);
    // Lock the default node type.
    $locked = \Drupal::state()->get('node.type.locked');
    $locked['default'] = 'default';
    \Drupal::state()->set('node.type.locked', $locked);
    // Call to flush all caches after installing the node_test_config module in
    // the same way installing a module through the UI does.
    $this->resetAll();
    $this->drupalGet('admin/structure/types/manage/default');
    $this->assertSession()
        ->linkNotExists('Delete');
    $this->drupalGet('admin/structure/types/manage/default/delete');
    $this->assertSession()
        ->statusCodeEquals(403);
    $this->container
        ->get('module_installer')
        ->uninstall([
        'node_test_config',
    ]);
    $this->container = \Drupal::getContainer();
    unset($locked['default']);
    \Drupal::state()->set('node.type.locked', $locked);
    $this->drupalGet('admin/structure/types/manage/default');
    $this->clickLink('Delete');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->submitForm([], 'Delete');
    $this->assertFalse((bool) NodeType::load('default'), 'Node type with machine default deleted.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.