Skip to content

Commit

Permalink
MDL-64452 cache: Add tests for key prefix based on session id
Browse files Browse the repository at this point in the history
Unfortunately, we can't simply use session_id() to regenerate the
session id in unit tests. Starting from PHP 7.2, it would trigger
"session_id(): Cannot change session id when headers already sent",
refer to MDL-60978 and PHP bug #75628 for more details.

As a workaround, we use a static property allowing us to inject the
value that we then use as a session identifier. This is reasonably
enough to make sure that the identifier is used as a part of the key
prefix.
  • Loading branch information
mudrd8mz committed Feb 15, 2019
1 parent 0ea0dfd commit ea43f6c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
47 changes: 47 additions & 0 deletions cache/tests/cache_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2327,4 +2327,51 @@ public function test_session_event_purge_same_second() {
$this->assertEquals('test data 2', $cache->get('testkey1'));
}

/**
* Test that values set in different sessions are stored with different key prefixes.
*/
public function test_session_distinct_storage_key() {
$this->resetAfterTest();

// Prepare a dummy session cache configuration.
$config = cache_config_testing::instance();
$config->phpunit_add_definition('phpunit/test_session_distinct_storage_key', array(
'mode' => cache_store::MODE_SESSION,
'component' => 'phpunit',
'area' => 'test_session_distinct_storage_key'
));

// First anonymous user's session cache.
cache_phpunit_session::phpunit_mockup_session_id('foo');
$this->setUser(0);
$cache1 = cache::make('phpunit', 'test_session_distinct_storage_key');

// Reset cache instances to emulate a new request.
cache_factory::instance()->reset_cache_instances();

// Another anonymous user's session cache.
cache_phpunit_session::phpunit_mockup_session_id('bar');
$this->setUser(0);
$cache2 = cache::make('phpunit', 'test_session_distinct_storage_key');

cache_factory::instance()->reset_cache_instances();

// Guest user's session cache.
cache_phpunit_session::phpunit_mockup_session_id('baz');
$this->setGuestUser();
$cache3 = cache::make('phpunit', 'test_session_distinct_storage_key');

cache_factory::instance()->reset_cache_instances();

// Same guest user's session cache but in another browser window.
cache_phpunit_session::phpunit_mockup_session_id('baz');
$this->setGuestUser();
$cache4 = cache::make('phpunit', 'test_session_distinct_storage_key');

// Assert that different PHP session implies different key prefix for storing values.
$this->assertNotEquals($cache1->phpunit_get_key_prefix(), $cache2->phpunit_get_key_prefix());

// Assert that same PHP session implies same key prefix for storing values.
$this->assertEquals($cache3->phpunit_get_key_prefix(), $cache4->phpunit_get_key_prefix());
}
}
28 changes: 28 additions & 0 deletions cache/tests/fixtures/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,9 @@ public function phpunit_static_acceleration_purge() {
*/
class cache_phpunit_session extends cache_session {

/** @var Static member used for emulating the behaviour of session_id() during the tests. */
protected static $sessionidmockup = 'phpunitmockupsessionid';

/**
* Returns the class of the store immediately associated with this cache.
* @return string
Expand All @@ -480,6 +483,31 @@ public function phpunit_get_store_class() {
public function phpunit_get_store_implements() {
return class_implements($this->get_store());
}

/**
* Provide access to the {@link cache_session::get_key_prefix()} method.
*
* @return string
*/
public function phpunit_get_key_prefix() {
return $this->get_key_prefix();
}

/**
* Allows to inject the session identifier.
*
* @param string $sessionid
*/
public static function phpunit_mockup_session_id($sessionid) {
static::$sessionidmockup = $sessionid;
}

/**
* Override the parent behaviour so that it does not need the actual session_id() call.
*/
protected function set_session_id() {
$this->sessionid = static::$sessionidmockup;
}
}

/**
Expand Down

0 comments on commit ea43f6c

Please sign in to comment.