You are here

public function ContentEntityExampleTest::testPaths in Examples for Developers 3.x

Same name and namespace in other branches
  1. 8 content_entity_example/tests/src/Functional/ContentEntityExampleTest.php \Drupal\Tests\content_entity_example\Functional\ContentEntityExampleTest::testPaths()

Test all paths exposed by the module, by permission.

File

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

Class

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

Namespace

Drupal\Tests\content_entity_example\Functional

Code

public function testPaths() {
  $assert = $this
    ->assertSession();

  // Generate a contact so that we can test the paths against it.
  $contact = Contact::create([
    'name' => 'somename',
    'first_name' => 'Joe',
    'role' => 'administrator',
  ]);
  $contact
    ->save();

  // Gather the test data.
  $data = $this
    ->providerTestPaths($contact
    ->id());

  // Run the tests.
  foreach ($data as $datum) {

    // drupalCreateUser() doesn't know what to do with an empty permission
    // array, so we help it out.
    if ($datum[2]) {
      $user = $this
        ->drupalCreateUser([
        $datum[2],
      ]);
      $this
        ->drupalLogin($user);
    }
    else {
      $user = $this
        ->drupalCreateUser();
      $this
        ->drupalLogin($user);
    }
    $this
      ->drupalGet($datum[1]);
    $assert
      ->statusCodeEquals($datum[0]);
  }
}