Skip to content

Commit

Permalink
quiz-data - export whole course unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EJMFarrow committed Nov 28, 2024
1 parent 3ee8160 commit 6418f49
Show file tree
Hide file tree
Showing 4 changed files with 275 additions and 23 deletions.
49 changes: 41 additions & 8 deletions classes/create_repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public function get_curl_request($wsurl): curl_request {
* @param string $scriptdirectory - directory of CLI scripts
* @return void
*/
public function create_quiz_directories($clihelper, $scriptdirectory) {
public function create_quiz_directories(object $clihelper, string $scriptdirectory): void {
$contextinfo = $clihelper->check_context($this, false, true);
$arguments = $clihelper->get_arguments();
if ($arguments['directory']) {
Expand All @@ -251,7 +251,11 @@ public function create_quiz_directories($clihelper, $scriptdirectory) {
}
$moodleinstance = $arguments['moodleinstance'];
$instanceid = $arguments['instanceid'];
$token = $arguments['token'][$moodleinstance];
if (is_array($arguments['token'])) {
$token = $arguments['token'][$moodleinstance];
} else {
$token = $arguments['token'];
}
$ignorecat = $arguments['ignorecat'];
$ignorecat = ($ignorecat) ? ' -x "' . $ignorecat . '"' : '';
$quizlocations = [];
Expand All @@ -260,15 +264,11 @@ public function create_quiz_directories($clihelper, $scriptdirectory) {
$quizdirectory = cli_helper::get_quiz_directory($basedirectory, $quiz->name);
$rootdirectory = $clihelper->create_directory($quizdirectory);
echo "\nExporting quiz: {$quiz->name} to {$rootdirectory}\n";
chdir($scriptdirectory);
$output = shell_exec('php createrepo.php -w -r "' . $rootdirectory . '" -i "' . $moodleinstance .
'" -l "module" -n ' . $instanceid . ' -t ' . $token . ' -x ' . $ignorecat);
$output = $this->call_repo_creation($rootdirectory, $moodleinstance, $instanceid, $token, $ignorecat, $scriptdirectory);
echo $output;
$quizmanifestname = cli_helper::get_manifest_path($moodleinstance, 'module', null,
$contextinfo->contextinfo->coursename, $quiz->name, $rootdirectory);
chdir($scriptdirectory);
$output = shell_exec('php exportquizstructurefrommoodle.php -w -r "" -i "' . $moodleinstance . ' -t '
. $token. ' -p "' . $this->manifestpath . '" -f "' . $quizmanifestname . '"');
$output = $this->call_export_quiz($moodleinstance, $token, $quizmanifestname, $scriptdirectory);
$quizlocation = new \StdClass();
$quizlocation->moduleid = $instanceid;
$quizlocation->directory = basename($rootdirectory);
Expand All @@ -289,4 +289,37 @@ public function create_quiz_directories($clihelper, $scriptdirectory) {
exec('git commit -m "Initial Commit - final update"');
}
}

/**
* Separate out exec call for mocking.
*
* @param string $rootdirectory
* @param string $moodleinstance
* @param string $instanceid
* @param string $token
* @param string $ignorecat
* @return string
*/
public function call_repo_creation(string $rootdirectory, string $moodleinstance, string $instanceid,
string $token, string $ignorecat, string $scriptdirectory
): string {
chdir($scriptdirectory);
return shell_exec('php createrepo.php -w -r "' . $rootdirectory . '" -i "' . $moodleinstance .
'" -l "module" -n ' . $instanceid . ' -t ' . $token . ' -x ' . $ignorecat);
}

/**
* Separate out exec call for mocking.
*
* @param string $moodleinstance
* @param string $token
* @param string $quizmanifestname
* @return string
*/
public function call_export_quiz(string $moodleinstance, string $token,
string $quizmanifestname, string $scriptdirectory): string {
chdir($scriptdirectory);
return shell_exec('php exportquizstructurefrommoodle.php -w -r "" -i "' . $moodleinstance . ' -t '
. $token. ' -p "' . $this->manifestpath . '" -f "' . $quizmanifestname . '"');
}
}
80 changes: 68 additions & 12 deletions classes/export_repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,18 @@ public function update_quiz_directories($clihelper, $scriptdirectory) {
$contextinfo = $clihelper->check_context($this, false, true);
$basedirectory = dirname($this->manifestpath);
$moodleinstance = $arguments['moodleinstance'];
$token = $arguments['token'][$moodleinstance];
if (is_array($arguments['token'])) {
$token = $arguments['token'][$moodleinstance];
} else {
$token = $arguments['token'];
}
$ignorecat = $arguments['ignorecat'];
$ignorecat = ($ignorecat) ? ' -x "' . $ignorecat . '"' : '';
$quizlocations = $this->manifestcontents->quizzes;
$quizlocations = isset($this->manifestcontents->quizzes) ? $this->manifestcontents->quizzes : [];
$locarray = array_column($quizlocations, null, 'moduleid');
foreach ($contextinfo->quizzes as $quiz) {
$instanceid = (int) $quiz->instanceid;
if (!isset($locarray[$instanceid]) || !is_dir(dirname($basedirectory) . '/' . $locarray[$instanceid]->directory)) {
if (!isset($locarray[$instanceid])) {
$rootdirectory = $clihelper->create_directory(cli_helper::get_quiz_directory($basedirectory, $quiz->name));
if (!isset($locarray[$instanceid])) {
$quizlocation = new \StdClass();
Expand All @@ -321,25 +325,77 @@ public function update_quiz_directories($clihelper, $scriptdirectory) {
}
}
echo "\nExporting quiz: {$quiz->name} to {$rootdirectory}\n";
chdir($scriptdirectory);
$output = shell_exec('php createrepo.php -w -r "' . $rootdirectory . '" -i "' .
$moodleinstance . '" -l "module" -n ' . $instanceid . ' -t ' . $token . ' -x ' . $ignorecat);
$output = $this->call_repo_creation($rootdirectory, $moodleinstance,
$instanceid, $token, $ignorecat, $scriptdirectory);
} else if (!is_dir(dirname($basedirectory) . '/' . $locarray[$instanceid]->directory)) {
$rootdirectory = dirname($basedirectory) . '/' . $locarray[$instanceid]->directory;
mkdir($rootdirectory);
echo "\nExporting quiz: {$quiz->name} to {$rootdirectory}\n";
$output = $this->call_repo_creation($rootdirectory, $moodleinstance,
$instanceid, $token, $ignorecat, $scriptdirectory);
} else {
$rootdirectory = dirname($basedirectory) . '/' . $locarray[$instanceid]->directory;
echo "\nExporting quiz: {$quiz->name} to {$rootdirectory}\n";
chdir($scriptdirectory);
$quizmanifestname = cli_helper::get_manifest_path($moodleinstance, 'module', null,
$contextinfo->contextinfo->coursename, $quiz->name, '');
$output = shell_exec('php exportrepofrommoodle.php -w -r "' . $rootdirectory . '" -i "' .
$moodleinstance . '" -f "' . $quizmanifestname . '" -t ' . $token);
$output = $this->call_export_repo($rootdirectory, $moodleinstance, $token,
$quizmanifestname, $scriptdirectory);
}
echo $output;
$quizmanifestname = cli_helper::get_manifest_path($moodleinstance, 'module', null,
$contextinfo->contextinfo->coursename, $quiz->name, $rootdirectory);
chdir($scriptdirectory);
$output = shell_exec('php exportquizstructurefrommoodle.php -w -r "" -i "' . $moodleinstance . ' -t '
. $token. ' -p "' . $this->manifestpath. '" -f "' . $quizmanifestname . '"');
$output = $this->call_export_quiz($moodleinstance, $token, $quizmanifestname, $scriptdirectory);
echo $output;
}
}

/**
* Separate out exec call for mocking.
*
* @param string $rootdirectory
* @param string $moodleinstance
* @param string $instanceid
* @param string $token
* @param string $ignorecat
* @return string
*/
public function call_repo_creation(string $rootdirectory, string $moodleinstance, string $instanceid,
string $token, string $ignorecat, string $scriptdirectory
): string {
chdir($scriptdirectory);
return shell_exec('php createrepo.php -w -r "' . $rootdirectory . '" -i "' . $moodleinstance .
'" -l "module" -n ' . $instanceid . ' -t ' . $token . ' -x ' . $ignorecat);
}

/**
* Separate out exec call for mocking.
*
* @param string $moodleinstance
* @param string $token
* @param string $quizmanifestname
* @return string
*/
public function call_export_quiz(string $moodleinstance, string $token,
string $quizmanifestname, string $scriptdirectory): string {
chdir($scriptdirectory);
return shell_exec('php exportquizstructurefrommoodle.php -w -r "" -i "' . $moodleinstance . ' -t '
. $token. ' -p "' . $this->manifestpath . '" -f "' . $quizmanifestname . '"');
}

/**
* Separate out exec call for mocking.
*
* @param string $rootdirectory
* @param string $moodleinstance
* @param string $token
* @param string $quizmanifestname
* @param string $scriptdirectory
* @return string
*/
public function call_export_repo(string $rootdirectory, string $moodleinstance, string $token,
string $quizmanifestname, string $scriptdirectory): string {
chdir($scriptdirectory);
return shell_exec('php exportrepofrommoodle.php -w -r "' . $rootdirectory . '" -i "' .
$moodleinstance . '" -f "' . $quizmanifestname . '" -t ' . $token);
}
}
38 changes: 36 additions & 2 deletions tests/create_repo_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public function setUp(): void {
parent::setUp();
global $CFG;
$this->moodleinstances = [self::MOODLE => 'fakeurl.com'];
vfsStream::setup();
$root = vfsStream::setup();
vfsStream::copyFromFileSystem($CFG->dirroot . '/question/bank/gitsync/testrepoparent/', $root);
$this->rootpath = vfsStream::url('root');

// Mock the combined output of command line options and defaults.
Expand All @@ -75,6 +76,7 @@ public function setUp(): void {
'token' => 'XXXXXX',
'help' => false,
'ignorecat' => null,
'usegit' => false,
];
$this->clihelper = $this->getMockBuilder(\qbank_gitsync\cli_helper::class)->onlyMethods([
'get_arguments', 'check_context',
Expand All @@ -93,7 +95,7 @@ public function setUp(): void {
'execute',
])->setConstructorArgs(['xxxx'])->getMock();;
$this->createrepo = $this->getMockBuilder(\qbank_gitsync\create_repo::class)->onlyMethods([
'get_curl_request',
'get_curl_request', 'call_repo_creation', 'call_export_quiz'
])->setConstructorArgs([$this->clihelper, $this->moodleinstances])->getMock();
$this->createrepo->curlrequest = $this->curl;
$this->createrepo->listcurlrequest = $this->listcurl;
Expand Down Expand Up @@ -302,4 +304,36 @@ public function test_process_with_subcategory_id(): void {
$this->assertEquals("top/Default-for-Test-1/sub-2", $manifest->context->defaultsubdirectory);
$this->assertEquals(123, $manifest->context->defaultsubcategoryid);
}

/**
* Test full course.
*/
public function test_full_course(): void {
$this->options['directory'] = 'testrepo';
$this->clihelper = $this->getMockBuilder(\qbank_gitsync\cli_helper::class)->onlyMethods([
'get_arguments', 'check_context',
])->setConstructorArgs([[]])->getMock();
$this->clihelper->expects($this->any())->method('get_arguments')->will($this->returnValue($this->options));
$this->clihelper->expects($this->exactly(1))->method('check_context')->willReturn(
json_decode('{"contextinfo":{"contextlevel": "module", "categoryname":"", "coursename":"Course 1",
"modulename":"Module 1", "instanceid":"", "qcategoryname":"top", "qcategoryid":123},
"questions": [],
"quizzes": [{"instanceid":"1", "name":"Quiz 1"}, {"instanceid":"2", "name":"Quiz 2"}]}')
);
$this->createrepo->create_quiz_directories($this->clihelper, $this->rootpath . '/testrepoparent');

// Should have created a directory for each quiz and updated the manifest with locations.
$this->assertEquals(true, is_dir($this->rootpath . "/testrepo_quiz_quiz-1_1"));
$this->assertEquals(true, is_dir($this->rootpath . "/testrepo_quiz_quiz-2"));
$this->assertEquals(false, is_dir($this->rootpath . "/testrepo_quiz_quiz-3"));

$manifestcontents = json_decode(file_get_contents($this->rootpath . '/fakeexport_system_question_manifest.json'));
$this->assertEquals('1', $manifestcontents->quizzes[0]->moduleid);
$this->assertEquals('2', $manifestcontents->quizzes[1]->moduleid);
$this->assertEquals('testrepo_quiz_quiz-1_1', $manifestcontents->quizzes[0]->directory);
$this->assertEquals('testrepo_quiz_quiz-2', $manifestcontents->quizzes[1]->directory);
$this->expectOutputRegex(
'/^\nExporting quiz: Quiz 1.*testrepo_quiz_quiz-1_1.*Exporting quiz: Quiz 2.*testrepo_quiz_quiz-2.*$/s'
);
}
}
Loading

0 comments on commit 6418f49

Please sign in to comment.