You are here

public function SimpleTestExampleTest::testSimpleTestExampleEdit in Examples for Developers 8

Create a testing_example node and then see if our user can edit it.

Note that some assertions in this test will fail. We do this to show what a failing test looks like. Since we don't want this to interfere with automated tests, however, we jump through some hoops to determine our environment.

File

testing_example/src/Tests/SimpleTestExampleTest.php, line 119

Class

SimpleTestExampleTest
Ensure that the simpletest_example content type provided functions properly.

Namespace

Drupal\testing_example\Tests

Code

public function testSimpleTestExampleEdit() {

  // Create a user with our special permission.
  $user = $this
    ->drupalCreateUser([
    'extra special edit any testing_example',
  ]);

  // Log in our user.
  $this
    ->drupalLogin($user);

  // Create a node with our user as the creator.
  // drupalCreateNode() uses the logged-in user by default.
  $settings = [
    'type' => 'testing_example',
    'title' => $this
      ->randomMachineName(32),
  ];
  $node = $this
    ->drupalCreateNode($settings);

  // For debugging, we might output some information using $this->verbose()
  // It will only be output if the testing settings have 'verbose' set.
  $this
    ->verbose('Node created: ' . $node
    ->getTitle());

  // This section demonstrates a failing test. However, we want this test to
  // pass when it's running on the Drupal QA testbot. So we need to determine
  // which environment we're running inside of before we continue.
  if (!$this
    ->runningOnTestbot()) {
    $this
      ->drupalGet('node/' . $node
      ->id() . '/edit');

    // The debug() statement will output information into the test results.
    // It can also be used in Drupal anywhere in code and will come out
    // as a message from the Messenger service.
    debug('The following test should fail. Examine the verbose message above it to see why.');

    // Make sure we don't get a 401 unauthorized response:
    $this
      ->assertResponse(200, 'User is allowed to edit the content.');

    // Looking for title text in the page to determine whether we were
    // successful opening edit form.
    $this
      ->assertText((string) new FormattableMarkup('@title', [
      '@title' => $settings['title'],
    ]), "Found title in edit form");
  }
}