diff --git a/classes/local/cli/cli_exception.php b/classes/local/cli/cli_exception.php index 750e98a..6a6e38e 100644 --- a/classes/local/cli/cli_exception.php +++ b/classes/local/cli/cli_exception.php @@ -64,6 +64,11 @@ class cli_exception extends Exception { */ const ERROR_OUTAGE_CHANGED = 7; + /** + * The outage plugin is not enabled. + */ + const ERROR_PLUGIN_DISABLED = 8; + /** * cliexception constructor. * @param string $message An explanation of the exception. diff --git a/classes/local/cli/clibase.php b/classes/local/cli/clibase.php index 6d3cae5..064f5a6 100644 --- a/classes/local/cli/clibase.php +++ b/classes/local/cli/clibase.php @@ -20,6 +20,7 @@ use coding_exception; use core\session\manager; defined('MOODLE_INTERNAL') || die(); +require_once($CFG->libdir.'/clilib.php'); /** * Outage CLI base class. @@ -47,7 +48,9 @@ abstract class clibase { */ public function __construct(array $options = null) { global $CFG; - require_once($CFG->libdir.'/clilib.php'); + if (!is_enabled_auth('outage')) { + throw new cli_exception(get_string('cliplugindisabled', 'auth_outage'), cli_exception::ERROR_PLUGIN_DISABLED); + } $this->become_admin_user(); diff --git a/cli/create.php b/cli/create.php index e8123b7..9764321 100644 --- a/cli/create.php +++ b/cli/create.php @@ -29,20 +29,19 @@ use auth_outage\local\outagelib; define('CLI_SCRIPT', true); require_once(__DIR__.'/../../../config.php'); - -$cli = new create(); - -$config = outagelib::get_config(); -$cli->set_defaults([ - 'help' => false, - 'warn' => (int)($config->default_warning_duration), - 'start' => null, - 'duration' => (int)($config->default_duration), - 'title' => $config->default_title, - 'description' => $config->default_description, -]); +require_once($CFG->libdir.'/clilib.php'); try { + $cli = new create(); + $config = outagelib::get_config(); + $cli->set_defaults([ + 'help' => false, + 'warn' => (int)($config->default_warning_duration), + 'start' => null, + 'duration' => (int)($config->default_duration), + 'title' => $config->default_title, + 'description' => $config->default_description, + ]); $cli->execute(); } catch (cli_exception $e) { cli_error($e->getMessage()); diff --git a/cli/finish.php b/cli/finish.php index a840f57..d3e0fb0 100644 --- a/cli/finish.php +++ b/cli/finish.php @@ -28,10 +28,10 @@ use auth_outage\local\cli\finish; define('CLI_SCRIPT', true); require_once(__DIR__.'/../../../config.php'); - -$cli = new finish(); +require_once($CFG->libdir.'/clilib.php'); try { + $cli = new finish(); $cli->execute(); } catch (cli_exception $e) { cli_error($e->getMessage()); diff --git a/cli/waitforit.php b/cli/waitforit.php index fb8c0fa..c9859c9 100644 --- a/cli/waitforit.php +++ b/cli/waitforit.php @@ -28,10 +28,10 @@ use auth_outage\local\cli\waitforit; define('CLI_SCRIPT', true); require_once(__DIR__.'/../../../config.php'); - -$cli = new waitforit(); +require_once($CFG->libdir.'/clilib.php'); try { + $cli = new waitforit(); $cli->execute(); } catch (cli_exception $e) { cli_error($e->getMessage()); diff --git a/lang/en/auth_outage.php b/lang/en/auth_outage.php index 82004de..d57a927 100644 --- a/lang/en/auth_outage.php +++ b/lang/en/auth_outage.php @@ -42,6 +42,7 @@ $string['clifinishnotongoing'] = 'Outage is not ongoing.'; $string['clifinishparamhelp'] = 'shows parameters help.'; $string['clifinishparamactive'] = 'finishes the currently active outage.'; $string['clifinishparamoutageid'] = 'the id of the outage to finish.'; +$string['cliplugindisabled'] = 'The authentication plugin \'Outage\' is disabled. Please enable it in the site administration it and try again.'; $string['cliwaitforiterroridxoractive'] = 'You must use --outageid=# or --active parameter but not both.'; $string['cliwaitforithelp'] = 'Waits until an outage starts.'; $string['cliwaitforitoutagestarted'] = 'Outage started!'; diff --git a/tests/phpunit/cli/cli_test.php b/tests/phpunit/cli/cli_test.php index 400acc5..d26425a 100644 --- a/tests/phpunit/cli/cli_test.php +++ b/tests/phpunit/cli/cli_test.php @@ -79,4 +79,17 @@ class cli_test extends cli_testcase { public function test_exception() { throw new cli_exception('An CLI exception.'); } + + /** + * @expectedException auth_outage\local\cli\cli_exception + * @expectedExceptionCode 8 + */ + public function test_authdisabled() { + // Disable all auth plugins. + set_config('auth', ''); + \core\session\manager::gc(); // Remove stale sessions. + core_plugin_manager::reset_caches(); + // Try to create an CLI + $cli = new create(); + } } diff --git a/tests/phpunit/cli/cli_testcase.php b/tests/phpunit/cli/cli_testcase.php index 5bf709c..fe2a095 100644 --- a/tests/phpunit/cli/cli_testcase.php +++ b/tests/phpunit/cli/cli_testcase.php @@ -28,6 +28,11 @@ defined('MOODLE_INTERNAL') || die(); */ class cli_testcase extends advanced_testcase { public function setUp() { + // Enable auth plugins. + set_config('auth', 'outage'); + \core\session\manager::gc(); // Remove stale sessions. + core_plugin_manager::reset_caches(); + $this->resetAfterTest(true); $this->set_parameters([]); parent::setUp();