Skip to content

Commit

Permalink
MDL-52656 unittest: Reset original session while setting user
Browse files Browse the repository at this point in the history
  • Loading branch information
rajeshtaneja committed Jan 7, 2016
1 parent 4aaa38e commit faea241
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
3 changes: 3 additions & 0 deletions lib/phpunit/classes/advanced_testcase.php
Expand Up @@ -492,6 +492,9 @@ public static function setUser($user = null) {
unset($user->access);
unset($user->preference);

// Enusre session is empty, as it may contain caches and user specific info.
\core\session\manager::init_empty_session();

\core\session\manager::set_user($user);
}

Expand Down
9 changes: 8 additions & 1 deletion lib/phpunit/tests/advanced_test.php
Expand Up @@ -72,7 +72,9 @@ public function test_debugging() {
}

public function test_set_user() {
global $USER, $DB;
global $USER, $DB, $SESSION;

$this->resetAfterTest();

$this->assertEquals(0, $USER->id);
$this->assertSame($_SESSION['USER'], $USER);
Expand Down Expand Up @@ -109,6 +111,11 @@ public function test_set_user() {
$this->assertEquals(0, $USER->id);
$this->assertSame($_SESSION['USER'], $USER);
$this->assertSame($GLOBALS['USER'], $USER);

// Ensure session is reset after setUser, as it may contain extra info.
$SESSION->sometestvalue = true;
$this->setUser($user);
$this->assertObjectNotHasAttribute('sometestvalue', $SESSION);
}

public function test_set_admin_user() {
Expand Down
10 changes: 0 additions & 10 deletions mod/forum/tests/lib_test.php
Expand Up @@ -2170,7 +2170,6 @@ public function test_print_overview() {
}

public function test_print_overview_groups() {
global $SESSION;
$this->resetAfterTest();
$course1 = self::getDataGenerator()->create_course();
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
Expand Down Expand Up @@ -2220,10 +2219,6 @@ public function test_print_overview_groups() {
$this->assertCount(1, $results[$course1->id]);
$this->assertArrayHasKey('forum', $results[$course1->id]);

// As viewer2 (different group to post).
// Temporary hack to workaround for MDL-52656.
$SESSION->currentgroup = null;

$this->setUser($viewer2->id);
$results = array();
forum_print_overview($courses, $results);
Expand Down Expand Up @@ -2290,7 +2285,6 @@ public function test_print_overview_timed($config, $hasresult) {
* @dataProvider print_overview_timed_provider
*/
public function test_print_overview_timed_groups($config, $hasresult) {
global $SESSION;
$this->resetAfterTest();
$course1 = self::getDataGenerator()->create_course();
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
Expand Down Expand Up @@ -2351,10 +2345,6 @@ public function test_print_overview_timed_groups($config, $hasresult) {
$this->assertCount(0, $results);
}

// As viewer2 (different group to post).
// Temporary hack to workaround for MDL-52656.
$SESSION->currentgroup = null;

$this->setUser($viewer2->id);
$results = array();
forum_print_overview($courses, $results);
Expand Down

0 comments on commit faea241

Please sign in to comment.