function NodeTypeTest::testNodeTypeEditing

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

Tests editing a node type using the UI.

File

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

Class

NodeTypeTest
Ensures that node type functions work correctly.

Namespace

Drupal\Tests\node\Functional

Code

public function testNodeTypeEditing() {
    $assert = $this->assertSession();
    $this->drupalPlaceBlock('system_breadcrumb_block');
    $web_user = $this->drupalCreateUser([
        'bypass node access',
        'administer content types',
        'administer node fields',
    ]);
    $this->drupalLogin($web_user);
    $field = FieldConfig::loadByName('node', 'page', 'body');
    $this->assertEqual($field->getLabel(), 'Body', 'Body field was found.');
    // Verify that title and body fields are displayed.
    $this->drupalGet('node/add/page');
    $assert->pageTextContains('Title');
    $assert->pageTextContains('Body');
    // Rename the title field.
    $edit = [
        'title_label' => 'Foo',
    ];
    $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type'));
    $this->drupalGet('node/add/page');
    $assert->pageTextContains('Foo');
    $assert->pageTextNotContains('Title');
    // Change the name and the description.
    $edit = [
        'name' => 'Bar',
        'description' => 'Lorem ipsum.',
    ];
    $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type'));
    $this->drupalGet('node/add');
    $assert->pageTextContains('Bar');
    $assert->pageTextContains('Lorem ipsum');
    $this->clickLink('Bar');
    $assert->pageTextContains('Foo');
    $assert->pageTextContains('Body');
    // Change the name through the API
    
    /** @var \Drupal\node\NodeTypeInterface $node_type */
    $node_type = NodeType::load('page');
    $node_type->set('name', 'NewBar');
    $node_type->save();
    
    /** @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info */
    $bundle_info = \Drupal::service('entity_type.bundle.info');
    $node_bundles = $bundle_info->getBundleInfo('node');
    $this->assertEqual($node_bundles['page']['label'], 'NewBar', 'Node type bundle cache is updated');
    // Remove the body field.
    $this->drupalPostForm('admin/structure/types/manage/page/fields/node.page.body/delete', [], t('Delete'));
    // Resave the settings for this type.
    $this->drupalPostForm('admin/structure/types/manage/page', [], t('Save content type'));
    $front_page_path = Url::fromRoute('<front>')->toString();
    $this->assertBreadcrumb('admin/structure/types/manage/page/fields', [
        $front_page_path => 'Home',
        'admin/structure/types' => 'Content types',
        'admin/structure/types/manage/page' => 'NewBar',
    ]);
    // Check that the body field doesn't exist.
    $this->drupalGet('node/add/page');
    $assert->pageTextNotContains('Body');
}

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