Merge branch 'w20_MDL-32926_m23_phpunitevil' of git://github.com/skodak/moodle

This commit is contained in:
Dan Poltawski 2012-05-14 13:36:11 +08:00
commit 3359a6fe07
6 changed files with 49 additions and 7 deletions

View file

@ -258,7 +258,7 @@ abstract class advanced_testcase extends PHPUnit_Framework_TestCase {
/** /**
* Set current $USER, reset access cache. * Set current $USER, reset access cache.
* @static * @static
* @param null|int|stdClass $user user record, null means non-logged-in, integer means userid * @param null|int|stdClass $user user record, null or 0 means non-logged-in, positive integer means userid
* @return void * @return void
*/ */
public static function setUser($user = null) { public static function setUser($user = null) {
@ -275,10 +275,29 @@ abstract class advanced_testcase extends PHPUnit_Framework_TestCase {
} }
unset($user->description); unset($user->description);
unset($user->access); unset($user->access);
unset($user->preference);
session_set_user($user); session_set_user($user);
} }
/**
* Set current $USER to admin account, reset access cache.
* @static
* @return void
*/
public static function setAdminUser() {
self::setUser(2);
}
/**
* Set current $USER to guest account, reset access cache.
* @static
* @return void
*/
public static function setGuestUser() {
self::setUser(1);
}
/** /**
* Get data generator * Get data generator
* @static * @static

View file

@ -43,6 +43,7 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
$this->assertSame($_SESSION['USER'], $USER); $this->assertSame($_SESSION['USER'], $USER);
$user = $DB->get_record('user', array('id'=>2)); $user = $DB->get_record('user', array('id'=>2));
$this->assertNotEmpty($user);
$this->setUser($user); $this->setUser($user);
$this->assertEquals(2, $USER->id); $this->assertEquals(2, $USER->id);
$this->assertEquals(2, $_SESSION['USER']->id); $this->assertEquals(2, $_SESSION['USER']->id);
@ -59,6 +60,7 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
$this->assertSame($_SESSION['USER'], $USER); $this->assertSame($_SESSION['USER'], $USER);
$USER = $DB->get_record('user', array('id'=>1)); $USER = $DB->get_record('user', array('id'=>1));
$this->assertNotEmpty($USER);
$this->assertEquals(1, $USER->id); $this->assertEquals(1, $USER->id);
$this->assertEquals(1, $_SESSION['USER']->id); $this->assertEquals(1, $_SESSION['USER']->id);
$this->assertSame($_SESSION['USER'], $USER); $this->assertSame($_SESSION['USER'], $USER);
@ -68,6 +70,26 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
$this->assertSame($_SESSION['USER'], $USER); $this->assertSame($_SESSION['USER'], $USER);
} }
public function test_set_admin_user() {
global $USER;
$this->resetAfterTest(true);
$this->setAdminUser();
$this->assertEquals($USER->id, 2);
$this->assertTrue(is_siteadmin());
}
public function test_set_guest_user() {
global $USER;
$this->resetAfterTest(true);
$this->setGuestUser();
$this->assertEquals($USER->id, 1);
$this->assertTrue(isguestuser());
}
public function test_database_reset() { public function test_database_reset() {
global $DB; global $DB;

View file

@ -51,7 +51,7 @@ class moodle_page_test extends advanced_testcase {
protected $testpage; protected $testpage;
public function setUp() { public function setUp() {
parent::setUser(); parent::setUp();
$this->resetAfterTest(); $this->resetAfterTest();
$this->testpage = new testable_moodle_page(); $this->testpage = new testable_moodle_page();
} }
@ -561,7 +561,8 @@ class moodle_page_editing_test extends advanced_testcase {
protected $originaluserediting; protected $originaluserediting;
protected function setUp() { protected function setUp() {
$this->setUser(2); // admin parent::setUp();
$this->setAdminUser();
$this->testpage = new testable_moodle_page(); $this->testpage = new testable_moodle_page();
$this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM)); $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM));
$this->resetAfterTest(); $this->resetAfterTest();

View file

@ -39,7 +39,7 @@ class repositorylib_testcase extends advanced_testcase {
$PAGE->set_url('/'); $PAGE->set_url('/');
$PAGE->set_course($SITE); $PAGE->set_course($SITE);
$this->setUser(2); $this->setAdminUser();
$args = new stdClass(); $args = new stdClass();
$args->accepted_types = '*'; $args->accepted_types = '*';

View file

@ -70,8 +70,8 @@ class mod_assignment_generator_testcase extends advanced_testcase {
$this->assertEquals(GRADE_TYPE_VALUE, $gitem->gradetype); $this->assertEquals(GRADE_TYPE_VALUE, $gitem->gradetype);
// test eventslib integration // test eventslib integration
$this->setUser(2); // admin $this->setAdminUser();
$generator->create_instance(array('course'=>$course->id, 'timedue'=>(time()+60*60+24))); $generator->create_instance(array('course'=>$course->id, 'timedue'=>(time()+60*60+24)));
$this->setUser(0); $this->setUser(null);
} }
} }

View file

@ -43,7 +43,7 @@ class workshopform_rubric_strategy_test extends advanced_testcase {
* Setup testing environment * Setup testing environment
*/ */
protected function setUp() { protected function setUp() {
parent::setUser(); parent::setUp();
$cm = new stdclass(); $cm = new stdclass();
$course = new stdclass(); $course = new stdclass();
$context = new stdclass(); $context = new stdclass();