function StatisticsAdminTest::testDeleteNode

Same name in other branches
  1. 9 core/modules/statistics/tests/src/Functional/StatisticsAdminTest.php \Drupal\Tests\statistics\Functional\StatisticsAdminTest::testDeleteNode()
  2. 8.9.x core/modules/statistics/tests/src/Functional/StatisticsAdminTest.php \Drupal\Tests\statistics\Functional\StatisticsAdminTest::testDeleteNode()
  3. 10 core/modules/statistics/tests/src/Functional/StatisticsAdminTest.php \Drupal\Tests\statistics\Functional\StatisticsAdminTest::testDeleteNode()

Tests that when a node is deleted, the node counter is deleted too.

File

core/modules/statistics/tests/src/Functional/StatisticsAdminTest.php, line 126

Class

StatisticsAdminTest
Tests the statistics admin.

Namespace

Drupal\Tests\statistics\Functional

Code

public function testDeleteNode() {
    $this->config('statistics.settings')
        ->set('count_content_views', 1)
        ->save();
    $this->drupalGet('node/' . $this->testNode
        ->id());
    // Manually calling statistics.php, simulating ajax behavior.
    $nid = $this->testNode
        ->id();
    $post = [
        'nid' => $nid,
    ];
    global $base_url;
    $stats_path = $base_url . '/' . $this->getModulePath('statistics') . '/statistics.php';
    $this->client
        ->post($stats_path, [
        'form_params' => $post,
    ]);
    $connection = Database::getConnection();
    $result = $connection->select('node_counter', 'n')
        ->fields('n', [
        'nid',
    ])
        ->condition('n.nid', $this->testNode
        ->id())
        ->execute()
        ->fetchAssoc();
    $this->assertEquals($result['nid'], $this->testNode
        ->id(), 'Verifying that the node counter is incremented.');
    $this->testNode
        ->delete();
    $result = $connection->select('node_counter', 'n')
        ->fields('n', [
        'nid',
    ])
        ->condition('n.nid', $this->testNode
        ->id())
        ->execute()
        ->fetchAssoc();
    $this->assertFalse($result, 'Verifying that the node counter is deleted.');
}

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