MDL-61759 admin: non-existing site policy handler means no policy

This commit is contained in:
Marina Glancy 2018-04-23 10:25:00 +08:00
parent 0683abccfc
commit a74c511e7f
2 changed files with 47 additions and 0 deletions

View File

@ -45,6 +45,11 @@ class default_handler extends handler {
*/
public static function is_defined($forguests = false) {
global $CFG;
if (!empty($CFG->sitepolicyhandler)) {
// This handler can also be used as a fallback in case of invalid $CFG->sitepolicyhandler,
// in this case assume that no site policy is set.
return false;
}
if (!$forguests) {
return !empty($CFG->sitepolicy);
} else {
@ -76,6 +81,11 @@ class default_handler extends handler {
*/
public static function get_embed_url($forguests = false) {
global $CFG;
if (!empty($CFG->sitepolicyhandler)) {
// This handler can also be used as a fallback in case of invalid $CFG->sitepolicyhandler,
// in this case assume that no site policy is set.
return null;
}
if ($forguests && !empty($CFG->sitepolicyguest)) {
return new moodle_url($CFG->sitepolicyguest);
} else if (!$forguests && !empty($CFG->sitepolicy)) {

View File

@ -83,6 +83,13 @@ class sitepolicy_test extends advanced_testcase {
$CFG->sitepolicy = null;
$this->assertTrue($manager->is_defined(true));
$this->assertFalse($manager->is_defined(false));
// When non existing plugin is set as $CFG->sitepolicyhandler, assume that $CFG->sitepolicy* are all not set.
$CFG->sitepolicy = 'http://example.com/sitepolicy.html';
$CFG->sitepolicyguest = 'http://example.com/sitepolicyguest.html';
$CFG->sitepolicyhandler = 'non_existing_plugin_which_i_really_hope_will_never_exist';
$this->assertFalse($manager->is_defined(true));
$this->assertFalse($manager->is_defined(false));
}
/**
@ -108,6 +115,13 @@ class sitepolicy_test extends advanced_testcase {
$CFG->sitepolicy = null;
$this->assertEquals($CFG->wwwroot.'/user/policy.php', $manager->get_redirect_url(true)->out(false));
$this->assertEquals(null, $manager->get_redirect_url(false));
// When non existing plugin is set as $CFG->sitepolicyhandler, assume that $CFG->sitepolicy* are all not set.
$CFG->sitepolicy = 'http://example.com/sitepolicy.html';
$CFG->sitepolicyguest = 'http://example.com/sitepolicyguest.html';
$CFG->sitepolicyhandler = 'non_existing_plugin_which_i_really_hope_will_never_exist';
$this->assertEquals(null, $manager->get_redirect_url(true));
$this->assertEquals(null, $manager->get_redirect_url(false));
}
/**
@ -133,6 +147,13 @@ class sitepolicy_test extends advanced_testcase {
$CFG->sitepolicy = null;
$this->assertEquals($CFG->sitepolicyguest, $manager->get_embed_url(true)->out(false));
$this->assertEquals(null, $manager->get_embed_url(false));
// When non existing plugin is set as $CFG->sitepolicyhandler, assume that $CFG->sitepolicy* are all not set.
$CFG->sitepolicy = 'http://example.com/sitepolicy.html';
$CFG->sitepolicyguest = 'http://example.com/sitepolicyguest.html';
$CFG->sitepolicyhandler = 'non_existing_plugin_which_i_really_hope_will_never_exist';
$this->assertEquals(null, $manager->get_embed_url(true));
$this->assertEquals(null, $manager->get_embed_url(false));
}
/**
@ -161,6 +182,15 @@ class sitepolicy_test extends advanced_testcase {
$this->assertTrue($manager->accept());
$this->assertEquals(1, $USER->policyagreed);
$this->assertEquals(1, $DB->get_field('user', 'policyagreed', ['id' => $USER->id]));
// When non existing plugin is set as $CFG->sitepolicyhandler, assume that $CFG->sitepolicy* are all not set.
$CFG->sitepolicy = 'http://example.com/sitepolicy.html';
$CFG->sitepolicyhandler = 'non_existing_plugin_which_i_really_hope_will_never_exist';
$user3 = $this->getDataGenerator()->create_user();
$this->setUser($user3);
$this->assertEquals(0, $USER->policyagreed);
$this->assertFalse($manager->accept());
$this->assertEquals(0, $USER->policyagreed);
}
/**
@ -185,6 +215,13 @@ class sitepolicy_test extends advanced_testcase {
$this->assertTrue($manager->accept());
$this->assertEquals(1, $USER->policyagreed);
$this->assertEquals(0, $DB->get_field('user', 'policyagreed', ['id' => $USER->id]));
// When non existing plugin is set as $CFG->sitepolicyhandler, assume that $CFG->sitepolicy* are all not set.
$USER->policyagreed = 0; // Reset.
$CFG->sitepolicyguest = 'http://example.com/sitepolicyguest.html';
$CFG->sitepolicyhandler = 'non_existing_plugin_which_i_really_hope_will_never_exist';
$this->assertFalse($manager->accept());
$this->assertEquals(0, $USER->policyagreed);
}
/**