From 157a22551f700aa334ba6a5ed13bbc7882168afc Mon Sep 17 00:00:00 2001 From: Anupama Sarjoshi Date: Tue, 19 Mar 2024 13:07:58 +0000 Subject: [PATCH] Fix warning - undefined property behat_prefix --- classes/jobesandbox.php | 3 ++- classes/sandbox.php | 22 +++++++++++++++++++++- edit_coderunner_form.php | 2 +- renderer.php | 3 ++- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/classes/jobesandbox.php b/classes/jobesandbox.php index 725e87fb..848bcb28 100644 --- a/classes/jobesandbox.php +++ b/classes/jobesandbox.php @@ -71,7 +71,8 @@ public function __construct() { global $CFG; qtype_coderunner_sandbox::__construct(); $this->jobeserver = get_config('qtype_coderunner', 'jobe_host'); - if ($this->jobeserver === 'jobe2.cosc.canterbury.ac.nz' && $CFG->prefix === $CFG->behat_prefix) { + if (qtype_coderunner_sandbox::is_canterbury_server($this->jobeserver) + && qtype_coderunner_sandbox::is_using_test_sandbox()) { throw new Exception("Please don't use the Canterbury jobe server for test runs"); } $this->apikey = get_config('qtype_coderunner', 'jobe_apikey'); diff --git a/classes/sandbox.php b/classes/sandbox.php index 922649fd..825518dc 100644 --- a/classes/sandbox.php +++ b/classes/sandbox.php @@ -34,9 +34,11 @@ defined('MOODLE_INTERNAL') || die(); +use qtype_coderunner\constants; + global $CFG; -if ($CFG->prefix == $CFG->behat_prefix) { +if (qtype_coderunner_sandbox::is_using_test_sandbox()) { require_once($CFG->dirroot .'/question/type/coderunner/tests/fixtures/test-sandbox-config.php'); } @@ -209,6 +211,24 @@ public static function enabled_sandboxes() { return $enabled; } + /** + * Returns true if sandbox is being used for tests. + * @return bool + */ + public static function is_using_test_sandbox(): bool { + global $CFG; + return !empty($CFG->behat_prefix) && $CFG->prefix === $CFG->behat_prefix; + } + + /** + * Returns true if canterbury jobe server is being used. + * @param string jobeserver being used. + * @return bool + */ + public static function is_canterbury_server(string $jobeserver): bool { + return $jobeserver === constants::JOBE_HOST_DEFAULT; + } + /** * Get the filename containing the given external sandbox name. * @param string $externalsandboxname diff --git a/edit_coderunner_form.php b/edit_coderunner_form.php index e9aba7d0..7f6f1978 100644 --- a/edit_coderunner_form.php +++ b/edit_coderunner_form.php @@ -250,7 +250,7 @@ protected function add_sample_answer_field($mform) { $mform->addHelpButton('sampleanswerattachments', 'sampleanswerattachments', 'qtype_coderunner'); // Unless behat is running, hide the attachments file picker. // behat barfs if it's hidden. - if ($CFG->prefix !== $CFG->behat_prefix) { + if (!qtype_coderunner_sandbox::is_using_test_sandbox()) { $method = method_exists($mform, 'hideIf') ? 'hideIf' : 'disabledIf'; $mform->$method('sampleanswerattachments', 'attachments', 'eq', 0); } diff --git a/renderer.php b/renderer.php index e81d2663..890c2436 100644 --- a/renderer.php +++ b/renderer.php @@ -411,7 +411,8 @@ protected function build_results_table($outcome, qtype_coderunner_question $ques if (isset($sandboxinfo['jobeserver'])) { $jobeserver = $sandboxinfo['jobeserver']; $apikey = $sandboxinfo['jobeapikey']; - if ($jobeserver == constants::JOBE_HOST_DEFAULT && $CFG->prefix !== $CFG->behat_prefix) { + if (qtype_coderunner_sandbox::is_canterbury_server($jobeserver) + && (!qtype_coderunner_sandbox::is_using_test_sandbox())) { if ($apikey == constants::JOBE_HOST_DEFAULT_API_KEY) { $fb .= get_string('jobe_warning_html', 'qtype_coderunner'); } else {