MDL-61672 admin: Fix sitepolicy handler API

The base class had abstract static methods which raises strict standards
warning on PHP 5.6.
This commit is contained in:
David Mudrák
2018-03-16 17:22:45 +01:00
parent 9973df93f4
commit b2865ca1e4
3 changed files with 86 additions and 2 deletions
+12 -2
View File
@@ -24,6 +24,8 @@
namespace core_privacy\local\sitepolicy;
use coding_exception;
defined('MOODLE_INTERNAL') || die();
/**
@@ -54,10 +56,14 @@ abstract class handler {
* This is a regular interactive page for web users. It should have normal Moodle header/footers, it should
* allow user to view policies and accept them.
*
* This would normally be an abstract static method but PHP 5.6 raises strict standards warning for such ones.
*
* @param bool $forguests
* @return moodle_url|null (returns null if site policy is not defined)
*/
abstract public static function get_redirect_url($forguests = false);
public static function get_redirect_url($forguests = false) {
throw new coding_exception('Method get_redirect_url() not implemented by the handler');
}
/**
* Returns URL of the site policy that needs to be displayed to the user (inside iframe or to use in WS such as mobile app)
@@ -65,10 +71,14 @@ abstract class handler {
* This page should not have any header/footer, it does not also have any buttons/checkboxes. The caller needs to implement
* the "Accept" button and call {@link self::accept()} on completion.
*
* This would normally be an abstract static method but PHP 5.6 raises strict standards warning for such ones.
*
* @param bool $forguests
* @return moodle_url|null
*/
abstract public static function get_embed_url($forguests = false);
public static function get_embed_url($forguests = false) {
throw new coding_exception('Method get_embed_url() not implemented by the handler');
}
/**
* Accept site policy for the current user
@@ -0,0 +1,48 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Provides {@link mock_incomplete_sitepolicy_handler} class.
*
* @package core_privacy
* @category test
* @copyright 2018 David Mudrák <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Incomplete mock handler for site policies not implementing all required methods.
*
* Particularly, get_embed_url() is missing.
*
* @package core_privacy
* @copyright 2018 David Mudrák <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mock_incomplete_sitepolicy_handler extends \core_privacy\local\sitepolicy\handler {
/**
* Returns URL to redirect user to when user needs to agree to site policy
*
* @param bool $forguests
* @return moodle_url|string|null
*/
public static function get_redirect_url($forguests = false) {
return 'http://example.com/policy.php';
}
}
+26
View File
@@ -259,6 +259,32 @@ class sitepolicy_test extends advanced_testcase {
$this->assertEquals(2, $USER->policyagreed);
$this->assertEquals(0, $DB->get_field('user', 'policyagreed', ['id' => $USER->id]));
}
/**
* Test behaviour of \core_privacy\local\sitepolicy\manager with a handler not implementing all required methods.
*/
public function test_incomplete_handler() {
global $CFG;
require_once($CFG->dirroot.'/privacy/tests/fixtures/mock_incomplete_sitepolicy_handler.php');
$this->resetAfterTest(true);
$CFG->sitepolicyhandler = 'testtool_incompletehandler';
$manager = $this->getMockBuilder(\core_privacy\local\sitepolicy\manager::class)
->setMethods(['get_all_handlers'])
->getMock();
$manager->expects($this->any())
->method('get_all_handlers')
->will($this->returnValue(['testtool_incompletehandler' => 'mock_incomplete_sitepolicy_handler']));
// This works because the handler implements get_redirect_url().
$this->assertEquals('http://example.com/policy.php', $manager->get_redirect_url()->out());
// This must inform them developer that the handler does not implement a method.
$this->expectException('coding_exception');
$this->expectExceptionMessage('Method get_embed_url() not implemented by the handler');
$manager->get_embed_url();
}
}
/**