function ContentEntityExampleTest::testCreateAdminPermission

Same name and namespace in other branches
  1. 4.0.x modules/content_entity_example/tests/src/Functional/ContentEntityExampleTest.php \Drupal\Tests\content_entity_example\Functional\ContentEntityExampleTest::testCreateAdminPermission()

Ensure admin and permissioned users can create contacts.

File

modules/content_entity_example/tests/src/Functional/ContentEntityExampleTest.php, line 259

Class

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

Namespace

Drupal\Tests\content_entity_example\Functional

Code

public function testCreateAdminPermission() {
  $assert = $this->assertSession();
  $add_url = Url::fromRoute('content_entity_example.contact_add');
  // Create a Contact entity object so that we can query it for it's annotated
  // properties. We don't need to save it.
  /** @var \Drupal\content_entity_example\Entity\Contact $contact */
  $contact = Contact::create();
  // Create an admin user and log them in. We use the entity annotation for
  // admin_permission in order to validate it. We also have to add the view
  // list permission because the add form redirects to the list on success.
  $this->drupalLogin($this->drupalCreateUser([
    $contact->getEntityType()
      ->getAdminPermission(),
    'view contact entity',
  ]));
  // Post a contact.
  $edit = [
    'name[0][value]' => 'Test Admin Name',
    'first_name[0][value]' => 'Admin First Name',
    'role' => 'administrator',
  ];
  $this->drupalGet($add_url);
  $this->submitForm($edit, 'Save');
  $assert->statusCodeEquals(200);
  $assert->pageTextContains('Test Admin Name');
  // Create a user with 'add contact entity' permission. We also have to add
  // the view list permission because the add form redirects to the list on
  // success.
  $this->drupalLogin($this->drupalCreateUser([
    'add contact entity',
    'view contact entity',
  ]));
  // Post a contact.
  $edit = [
    'name[0][value]' => 'Mere Mortal Name',
    'first_name[0][value]' => 'Mortal First Name',
    'role' => 'user',
  ];
  $this->drupalGet($add_url);
  $this->submitForm($edit, 'Save');
  $assert->statusCodeEquals(200);
  $assert->pageTextContains('Mere Mortal Name');
  // Finally, a user who can only view should not be able to get to the add
  // form.
  $this->drupalLogin($this->drupalCreateUser([
    'view contact entity',
  ]));
  $this->drupalGet($add_url);
  $assert->statusCodeEquals(403);
}