function ContentEntityExampleTest::testContentEntityExample

Same name in other branches
  1. 3.x modules/content_entity_example/tests/src/Functional/ContentEntityExampleTest.php \Drupal\Tests\content_entity_example\Functional\ContentEntityExampleTest::testContentEntityExample()
  2. 4.0.x modules/content_entity_example/tests/src/Functional/ContentEntityExampleTest.php \Drupal\Tests\content_entity_example\Functional\ContentEntityExampleTest::testContentEntityExample()

Basic tests for Content Entity Example.

File

content_entity_example/tests/src/Functional/ContentEntityExampleTest.php, line 35

Class

ContentEntityExampleTest
Tests the basic functions of the Content Entity Example module.

Namespace

Drupal\Tests\content_entity_example\Functional

Code

public function testContentEntityExample() {
    $assert = $this->assertSession();
    $web_user = $this->drupalCreateUser([
        'add contact entity',
        'edit contact entity',
        'view contact entity',
        'delete contact entity',
        'administer contact entity',
        'administer content_entity_example_contact display',
        'administer content_entity_example_contact fields',
        'administer content_entity_example_contact form display',
    ]);
    // Anonymous User should not see the link to the listing.
    $assert->pageTextNotContains('Content Entity Example');
    $this->drupalLogin($web_user);
    // Web_user user has the right to view listing.
    $assert->linkExists('Content Entity Example');
    $this->clickLink('Content Entity Example');
    // WebUser can add entity content.
    $assert->linkExists('Add contact');
    $this->clickLink($this->t('Add contact'));
    $assert->fieldValueEquals('name[0][value]', '');
    $assert->fieldValueEquals('name[0][value]', '');
    $assert->fieldValueEquals('name[0][value]', '');
    $assert->fieldValueEquals('name[0][value]', '');
    $user_ref = $web_user->name->value . ' (' . $web_user->id() . ')';
    $assert->fieldValueEquals('user_id[0][target_id]', $user_ref);
    // Post content, save an instance. Go back to list after saving.
    $edit = [
        'name[0][value]' => 'test name',
        'first_name[0][value]' => 'test first name',
        'role' => 'administrator',
    ];
    $this->drupalPostForm(NULL, $edit, 'Save');
    // Entity listed.
    $assert->linkExists('Edit');
    $assert->linkExists('Delete');
    $this->clickLink('test name');
    // Entity shown.
    $assert->pageTextContains('test name');
    $assert->pageTextContains('test first name');
    $assert->pageTextContains('administrator');
    $assert->linkExists('Add contact');
    $assert->linkExists('Edit');
    $assert->linkExists('Delete');
    // Delete the entity.
    $this->clickLink('Delete');
    // Confirm deletion.
    $assert->linkExists('Cancel');
    $this->drupalPostForm(NULL, [], 'Delete');
    // Back to list, must be empty.
    $assert->pageTextNotContains('test name');
    // Settings page.
    $this->drupalGet('admin/structure/content_entity_example_contact_settings');
    $assert->pageTextContains('Contact Settings');
    // Make sure the field manipulation links are available.
    $assert->linkExists('Settings');
    $assert->linkExists('Manage fields');
    $assert->linkExists('Manage form display');
    $assert->linkExists('Manage display');
}