function SessionTest::testSessionWrite

Same name in other branches
  1. 9 core/modules/system/tests/src/Functional/Session/SessionTest.php \Drupal\Tests\system\Functional\Session\SessionTest::testSessionWrite()
  2. 8.9.x core/modules/system/tests/src/Functional/Session/SessionTest.php \Drupal\Tests\system\Functional\Session\SessionTest::testSessionWrite()
  3. 11.x core/modules/system/tests/src/Functional/Session/SessionTest.php \Drupal\Tests\system\Functional\Session\SessionTest::testSessionWrite()

Tests that sessions are only saved when necessary.

File

core/modules/system/tests/src/Functional/Session/SessionTest.php, line 250

Class

SessionTest
Drupal session handling tests.

Namespace

Drupal\Tests\system\Functional\Session

Code

public function testSessionWrite() : void {
    $user = $this->drupalCreateUser([]);
    $this->drupalLogin($user);
    $connection = Database::getConnection();
    $query = $connection->select('users_field_data', 'u');
    $query->innerJoin('sessions', 's', '[u].[uid] = [s].[uid]');
    $query->fields('u', [
        'access',
    ])
        ->fields('s', [
        'timestamp',
    ])
        ->condition('u.uid', $user->id());
    $times1 = $query->execute()
        ->fetchObject();
    // Before every request we sleep one second to make sure that if the session
    // is saved, its timestamp will change.
    // Modify the session.
    sleep(1);
    $this->drupalGet('session-test/set/foo');
    $times2 = $query->execute()
        ->fetchObject();
    $this->assertEquals($times1->access, $times2->access, 'Users table was not updated.');
    $this->assertNotEquals($times1->timestamp, $times2->timestamp, 'Sessions table was updated.');
    // Write the same value again, i.e. do not modify the session.
    sleep(1);
    $this->drupalGet('session-test/set/foo');
    $times3 = $query->execute()
        ->fetchObject();
    $this->assertEquals($times1->access, $times3->access, 'Users table was not updated.');
    $this->assertEquals($times2->timestamp, $times3->timestamp, 'Sessions table was not updated.');
    // Do not change the session.
    sleep(1);
    $this->drupalGet('');
    $times4 = $query->execute()
        ->fetchObject();
    $this->assertEquals($times3->access, $times4->access, 'Users table was not updated.');
    $this->assertEquals($times3->timestamp, $times4->timestamp, 'Sessions table was not updated.');
    // Force updating of users and sessions table once per second.
    $settings['settings']['session_write_interval'] = (object) [
        'value' => 0,
        'required' => TRUE,
    ];
    $this->writeSettings($settings);
    $this->drupalGet('');
    $times5 = $query->execute()
        ->fetchObject();
    $this->assertNotEquals($times4->access, $times5->access, 'Users table was updated.');
    $this->assertNotEquals($times4->timestamp, $times5->timestamp, 'Sessions table was updated.');
}

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