Skip to content

Commit

Permalink
iss58 - Check local and Moodle versions of Gitsync are the same
Browse files Browse the repository at this point in the history
  • Loading branch information
EJMFarrow committed Dec 18, 2024
1 parent 84242f0 commit 5396f49
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 16 deletions.
5 changes: 5 additions & 0 deletions classes/cli_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class cli_helper {
*/
public ?array $processedoptions = null;

/**
* GITSYNC_VERSION - Current version of Gitsync.
* Should match version in version.php .
*/
public const GITSYNC_VERSION = '2024121800';
/**
* CATEGORY_FILE - Name of file containing category information in each directory and subdirectory.
*/
Expand Down
1 change: 1 addition & 0 deletions classes/create_repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ public function __construct(cli_helper $clihelper, array $moodleinstances) {
'contextonly' => 0,
'qbankentryids[]' => null,
'ignorecat' => $this->ignorecat,
'localversion' => cli_helper::GITSYNC_VERSION,
];
$this->listcurlrequest->set_option(CURLOPT_RETURNTRANSFER, true);
$this->listcurlrequest->set_option(CURLOPT_POST, 1);
Expand Down
1 change: 1 addition & 0 deletions classes/export_quiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public function __construct(cli_helper $clihelper, array $moodleinstances) {
'contextonly' => 1,
'qbankentryids[]' => null,
'ignorecat' => null,
'localversion' => cli_helper::GITSYNC_VERSION,
];
$this->listcurlrequest->set_option(CURLOPT_RETURNTRANSFER, true);
$this->listcurlrequest->set_option(CURLOPT_POST, 1);
Expand Down
1 change: 1 addition & 0 deletions classes/export_repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ public function __construct(cli_helper $clihelper, array $moodleinstances) {
'contextonly' => 0,
'qbankentryids[]' => null,
'ignorecat' => $this->ignorecat,
'localversion' => cli_helper::GITSYNC_VERSION,
];
$this->listcurlrequest->set_option(CURLOPT_RETURNTRANSFER, true);
$this->listcurlrequest->set_option(CURLOPT_POST, 1);
Expand Down
13 changes: 12 additions & 1 deletion classes/external/get_question_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public static function execute_parameters() {
return new external_function_parameters([
'qcategoryname' => new external_value(PARAM_TEXT, 'Category of questions in form top/$category/$subcat1/$subcat2'),
'contextlevel' => new external_value(PARAM_SEQUENCE, 'Context level: 10, 40, 50, 70'),
'localversion' => new external_value(PARAM_SEQUENCE, 'Version of Gitsync installed locally'),
'coursename' => new external_value(PARAM_TEXT, 'Unique course name'),
'modulename' => new external_value(PARAM_TEXT, 'Unique (within course) module name'),
'coursecategory' => new external_value(PARAM_TEXT, 'Unique course category name'),
Expand Down Expand Up @@ -101,6 +102,7 @@ public static function execute_returns(): external_single_structure {
*
* @param string|null $qcategoryname category to search in form top/$category/$subcat1/$subcat2
* @param int $contextlevel Moodle code for context level e.g. 10 for system
* @param string $localversion Version of Gitsync installed locally
* @param string|null $coursename Unique course name (optional unless course or module context level)
* @param string|null $modulename Unique (within course) module name (optional unless module context level)
* @param string|null $coursecategory course category name (optional unless course catgeory context level)
Expand All @@ -113,7 +115,8 @@ public static function execute_returns(): external_single_structure {
* @return object containing context info and an array of question data
*/
public static function execute(?string $qcategoryname,
int $contextlevel, ?string $coursename = null, ?string $modulename = null,
int $contextlevel, string $localversion,
?string $coursename = null, ?string $modulename = null,
?string $coursecategory = null, ?string $qcategoryid = null,
?string $instanceid = null, bool $contextonly = false,
?array $qbankentryids = [''], ?string $ignorecat = null): object {
Expand All @@ -129,7 +132,15 @@ public static function execute(?string $qcategoryname,
'contextonly' => $contextonly,
'qbankentryids' => $qbankentryids,
'ignorecat' => $ignorecat,
'localversion' => $localversion,
]);

$serverversion = get_config('qbank_gitsync')->version;
if ($params['localversion'] !== $serverversion) {
throw new \moodle_exception('versionmismatch', 'qbank_gitsync', null,
['local' => $params['localversion'], 'moodle' => $serverversion]);
}

$contextinfo = get_context($params['contextlevel'], $params['coursecategory'],
$params['coursename'], $params['modulename'],
$params['instanceid']
Expand Down
1 change: 1 addition & 0 deletions classes/import_quiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ public function __construct(cli_helper $clihelper, array $moodleinstances) {
'contextonly' => 1,
'qbankentryids[]' => null,
'ignorecat' => null,
'localversion' => cli_helper::GITSYNC_VERSION,
];
$this->listcurlrequest->set_option(CURLOPT_RETURNTRANSFER, true);
$this->listcurlrequest->set_option(CURLOPT_POST, 1);
Expand Down
1 change: 1 addition & 0 deletions classes/import_repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ public function __construct(cli_helper $clihelper, array $moodleinstances) {
'contextonly' => 0,
'qbankentryids[]' => null,
'ignorecat' => $this->ignorecat,
'localversion' => cli_helper::GITSYNC_VERSION,
];
$this->listcurlrequest->set_option(CURLOPT_RETURNTRANSFER, true);
$this->listcurlrequest->set_option(CURLOPT_POST, 1);
Expand Down
1 change: 1 addition & 0 deletions lang/en/qbank_gitsync.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@
$string['importversionerror'] = 'Could not import question : {$a->name} Current version in Moodle is {$a->currentversion}. Last imported version is {$a->importedversion}. Last exported version is {$a->exportedversion}. You need to export the question.';
$string['noquestionerror'] = 'Question does not exist. Questionbankentryid: {$a}';
$string['pluginname'] = 'Gitsync';
$string['versionmismatch'] = 'Your local version of Gitsync ({$a->local}) does not match the version of the plugin installed in Moodle ({$a->moodle}).';
48 changes: 35 additions & 13 deletions tests/external/get_question_list_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public function test_capabilities(): void {
$managerroleid = $DB->get_field('role', 'id', ['shortname' => 'manager']);
role_assign($managerroleid, $this->user->id, $context->id);

$returnvalue = get_question_list::execute('top', 50, $this->course->fullname, null, null,
$returnvalue = get_question_list::execute('top', 50, get_config('qbank_gitsync')->version,
$this->course->fullname, null, null,
null, null, false, ['']);

// We need to execute the return values cleaning process to simulate
Expand All @@ -112,7 +113,23 @@ public function test_not_logged_in(): void {
$this->expectException(require_login_exception::class);
// Exception messages don't seem to get translated.
$this->expectExceptionMessage('not logged in');
get_question_list::execute('top', 50, $this->course->fullname, null, null,
get_question_list::execute('top', 50, get_config('qbank_gitsync')->version, $this->course->fullname, null, null,
null, null, false, ['']);
}

/**
* Test if mismatched versions.
*/
public function test_mismatched_versions(): void {
global $DB;
// Set the required capabilities - webservice access and list rights on course.
$context = context_course::instance($this->course->id);
$managerroleid = $DB->get_field('role', 'id', ['shortname' => 'manager']);
role_assign($managerroleid, $this->user->id, $context->id);
$this->expectException(moodle_exception::class);
// Exception messages don't seem to get translated.
$this->expectExceptionMessage('Your local version of Gitsync (12345) does not match');
get_question_list::execute('top', 50, '12345', $this->course->fullname, null, null,
null, null, false, ['']);
}

Expand All @@ -127,7 +144,7 @@ public function test_no_webservice_access(): void {
$this->unassignUserCapability('qbank/gitsync:listquestions', \context_system::instance()->id, $managerroleid);
$this->expectException(required_capability_exception::class);
$this->expectExceptionMessage('you do not currently have permissions to do that (List)');
get_question_list::execute('top', 50, $this->course->fullname, null, null,
get_question_list::execute('top', 50, get_config('qbank_gitsync')->version, $this->course->fullname, null, null,
null, null, false, ['']);
}

Expand All @@ -137,7 +154,7 @@ public function test_no_webservice_access(): void {
public function test_list_capability(): void {
$this->expectException(require_login_exception::class);
$this->expectExceptionMessage('Not enrolled');
get_question_list::execute('top', 50, $this->course->fullname, null, null,
get_question_list::execute('top', 50, get_config('qbank_gitsync')->version, $this->course->fullname, null, null,
null, null, false, ['']);
}

Expand All @@ -160,7 +177,7 @@ public function test_question_is_in_supplied_context(): void {
$this->expectExceptionMessage('Not enrolled');
// Trying to list question from course 2 using context of course 1.
// User has list capability on course 1 but not course 2.
get_question_list::execute('top', 50, $course2->fullname, null, null,
get_question_list::execute('top', 50, get_config('qbank_gitsync')->version, $course2->fullname, null, null,
null, null, false, ['']);
}

Expand Down Expand Up @@ -195,7 +212,8 @@ public function test_list(): void {
}
\mod_quiz\structure::create_for_quiz($quizobj);
$sink = $this->redirectEvents();
$returnvalue = get_question_list::execute('top', 50, $this->course->fullname, null, null,
$returnvalue = get_question_list::execute('top', 50, get_config('qbank_gitsync')->version,
$this->course->fullname, null, null,
null, null, false, ['']);

$returnvalue = external_api::clean_returnvalue(
Expand Down Expand Up @@ -247,7 +265,7 @@ public function test_list_instanceid(): void {
$qbankentryid2 = $DB->get_field('question_versions', 'questionbankentryid',
['questionid' => $q2->id], $strictness = MUST_EXIST);
$sink = $this->redirectEvents();
$returnvalue = get_question_list::execute('top', 50, null, null, null,
$returnvalue = get_question_list::execute('top', 50, get_config('qbank_gitsync')->version, null, null, null,
null, $this->course->id, false, ['']);

$returnvalue = external_api::clean_returnvalue(
Expand Down Expand Up @@ -303,7 +321,7 @@ public function test_list_with_array(): void {
['questionid' => $qincourse2->id], $strictness = MUST_EXIST);

$sink = $this->redirectEvents();
$returnvalue = get_question_list::execute('top', 50, null, null, null,
$returnvalue = get_question_list::execute('top', 50, get_config('qbank_gitsync')->version, null, null, null,
null, $this->course->id, false, [$qbankentryid2, $qbankentryid3]);

$returnvalue = external_api::clean_returnvalue(
Expand Down Expand Up @@ -355,7 +373,7 @@ public function test_question_category_is_in_supplied_context(): void {
$this->expectExceptionMessage('The category is not in the supplied context.');
// Trying to list question from course 2 using context of course 1.
// User has list capability on course 1 but not course 2.
get_question_list::execute('top', 50, $this->course->fullname, null, null,
get_question_list::execute('top', 50, get_config('qbank_gitsync')->version, $this->course->fullname, null, null,
$catincourse2->id, null, false, ['']);
}

Expand Down Expand Up @@ -404,7 +422,8 @@ public function test_list_with_subcategory_name(): void {
$qbankentryid2 = $DB->get_field('question_versions', 'questionbankentryid',
['questionid' => $q2->id], $strictness = MUST_EXIST);
$sink = $this->redirectEvents();
$returnvalue = get_question_list::execute('top/' . $qcategory2->name, 50, $this->course->fullname, null, null,
$returnvalue = get_question_list::execute('top/' . $qcategory2->name, 50, get_config('qbank_gitsync')->version,
$this->course->fullname, null, null,
null, null, false, ['']);

$returnvalue = external_api::clean_returnvalue(
Expand Down Expand Up @@ -448,7 +467,8 @@ public function test_list_with_subcategory_id(): void {
$qbankentryid2 = $DB->get_field('question_versions', 'questionbankentryid',
['questionid' => $q2->id], $strictness = MUST_EXIST);
$sink = $this->redirectEvents();
$returnvalue = get_question_list::execute('top', 50, $this->course->fullname, null, null,
$returnvalue = get_question_list::execute('top', 50, get_config('qbank_gitsync')->version,
$this->course->fullname, null, null,
$qcategory2->id, null, false, ['']);

$returnvalue = external_api::clean_returnvalue(
Expand Down Expand Up @@ -503,7 +523,8 @@ public function test_list_with_subcategory_name_and_ignore(): void {
['name' => self::QNAME . '4', 'category' => $qcategory4->id]);

$sink = $this->redirectEvents();
$returnvalue = get_question_list::execute('top', 50, $this->course->fullname, null, null,
$returnvalue = get_question_list::execute('top', 50, get_config('qbank_gitsync')->version,
$this->course->fullname, null, null,
null, null, false, [''], '/.*DO_NOT_SHARE/');

$returnvalue = external_api::clean_returnvalue(
Expand Down Expand Up @@ -563,7 +584,8 @@ public function test_list_with_ignore(): void {
['questionid' => $q3->id], $strictness = MUST_EXIST);

$sink = $this->redirectEvents();
$returnvalue = get_question_list::execute('top/' . $qcategory2->name, 50, $this->course->fullname, null, null,
$returnvalue = get_question_list::execute('top/' . $qcategory2->name, 50, get_config('qbank_gitsync')->version,
$this->course->fullname, null, null,
null, null, false, [''], '/.*DO_NOT_SHARE/');

$returnvalue = external_api::clean_returnvalue(
Expand Down
5 changes: 3 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with Stack. If not, see <http://www.gnu.org/licenses/>.

/**
* Version information for GitSync question bank plugin.
* Version information for Gitsync question bank plugin.
*
* @package qbank_gitsync
* @copyright 2023 The Open University
Expand All @@ -24,7 +24,8 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2024121000;
// Should match GITSYNC_VERSION in cli_helper.
$plugin->version = 2024121800;
// Question versions functionality of Moodle 4 required.
// Question delete fix for Moodle 4.1.5 required.
// NB 4.2.0 and 4.2.1 do not have the fix.
Expand Down

0 comments on commit 5396f49

Please sign in to comment.