function DbLogTest::doNode

Same name in other branches
  1. 9 core/modules/dblog/tests/src/Functional/DbLogTest.php \Drupal\Tests\dblog\Functional\DbLogTest::doNode()
  2. 8.9.x core/modules/dblog/tests/src/Functional/DbLogTest.php \Drupal\Tests\dblog\Functional\DbLogTest::doNode()
  3. 11.x core/modules/dblog/tests/src/Functional/DbLogTest.php \Drupal\Tests\dblog\Functional\DbLogTest::doNode()

Generates and then verifies some node events.

Parameters

string $type: A node type (e.g., 'article' or 'page').

1 call to DbLogTest::doNode()
DbLogTest::verifyEvents in core/modules/dblog/tests/src/Functional/DbLogTest.php
Generates and then verifies various types of events.

File

core/modules/dblog/tests/src/Functional/DbLogTest.php, line 552

Class

DbLogTest
Verifies log entries and user access based on permissions.

Namespace

Drupal\Tests\dblog\Functional

Code

private function doNode($type) {
    // Create user.
    $perm = [
        'create ' . $type . ' content',
        'edit own ' . $type . ' content',
        'delete own ' . $type . ' content',
    ];
    $user = $this->drupalCreateUser($perm);
    // Log in user.
    $this->drupalLogin($user);
    // Create a node using the form in order to generate an add content event
    // (which is not triggered by drupalCreateNode).
    $edit = [
        'title[0][value]' => $this->randomMachineName(8),
        'body[0][value]' => $this->randomMachineName(32),
    ];
    $title = $edit['title[0][value]'];
    $this->drupalGet('node/add/' . $type);
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->statusCodeEquals(200);
    // Retrieve the node object.
    $node = $this->drupalGetNodeByTitle($title);
    $this->assertNotNull($node, "Node {$title} was loaded");
    // Edit the node.
    $edit = [
        'body[0][value]' => $this->randomMachineName(32),
    ];
    $this->drupalGet('node/' . $node->id() . '/edit');
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->statusCodeEquals(200);
    // Delete the node.
    $this->drupalGet('node/' . $node->id() . '/delete');
    $this->submitForm([], 'Delete');
    $this->assertSession()
        ->statusCodeEquals(200);
    // View the node (to generate page not found event).
    $this->drupalGet('node/' . $node->id());
    $this->assertSession()
        ->statusCodeEquals(404);
    // View the database log report (to generate access denied event).
    $this->drupalGet('admin/reports/dblog');
    $this->assertSession()
        ->statusCodeEquals(403);
    // Log in the admin user.
    $this->drupalLogin($this->adminUser);
    // View the database log report.
    $this->drupalGet('admin/reports/dblog');
    $this->assertSession()
        ->statusCodeEquals(200);
    // Verify that node events were recorded.
    // Was node content added?
    $this->assertLogMessage("{$type}: added {$title}.", 'DBLog event was recorded: [content added]');
    // Was node content updated?
    $this->assertLogMessage("{$type}: updated {$title}.", 'DBLog event was recorded: [content updated]');
    // Was node content deleted?
    $this->assertLogMessage("{$type}: deleted {$title}.", 'DBLog event was recorded: [content deleted]');
    // View the database log access-denied report page.
    $this->drupalGet('admin/reports/access-denied');
    $this->assertSession()
        ->statusCodeEquals(200);
    // Verify that the 'access denied' event was recorded.
    $this->assertSession()
        ->pageTextContains('admin/reports/dblog');
    // View the database log page-not-found report page.
    $this->drupalGet('admin/reports/page-not-found');
    $this->assertSession()
        ->statusCodeEquals(200);
    // Verify that the 'page not found' event was recorded.
    $this->assertSession()
        ->pageTextContains('node/' . $node->id());
}

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