function NodeTypeTest::testNodeTypeDeletion

Same name and namespace in other branches
  1. 9 core/modules/node/tests/src/Functional/NodeTypeTest.php \Drupal\Tests\node\Functional\NodeTypeTest::testNodeTypeDeletion()
  2. 10 core/modules/node/tests/src/Functional/NodeTypeTest.php \Drupal\Tests\node\Functional\NodeTypeTest::testNodeTypeDeletion()
  3. 11.x 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 172

Class

NodeTypeTest
Ensures that node type functions work correctly.

Namespace

Drupal\Tests\node\Functional

Code

public function testNodeTypeDeletion() {
    $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->assertRaw(t('%type 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 content.', [
        '%type' => $type->label(),
    ]), 'The content type will not be deleted until all nodes of that type are removed.');
    $this->assertNoText(t('This action cannot be undone.'), 'The node type deletion confirmation form is not available.');
    // 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->assertRaw(t('Are you sure you want to delete the content type %type?', [
        '%type' => $type->label(),
    ]), 'The content type is available for deletion.');
    $this->assertText(t('This action cannot be undone.'), 'The node type deletion confirmation form is available.');
    // 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 forum module in the same
    // way installing a module through the UI does.
    $this->resetAll();
    $this->drupalGet('admin/structure/types/manage/default');
    $this->assertSession()
        ->linkNotExists(t('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(t('Delete'));
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->drupalPostForm(NULL, [], t('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.