From 52be5ac3dc3a2ec9dd08c96da4b37a69c7ca25e9 Mon Sep 17 00:00:00 2001 From: Edmund Farrow Date: Wed, 20 Nov 2024 14:24:13 +0000 Subject: [PATCH] quiz-data - Additional cli_helper tests + fix old tests --- classes/cli_helper.php | 10 +- classes/create_repo.php | 2 +- classes/export_quiz.php | 2 +- classes/import_repo.php | 4 +- cli/exportquizstructurefrommoodle.php | 4 +- tests/cli_helper_test.php | 145 ++++++++++++++++++++------ tests/create_repo_test.php | 4 - tests/export_quiz_test.php | 24 ----- tests/import_repo_test.php | 19 ++-- 9 files changed, 129 insertions(+), 85 deletions(-) diff --git a/classes/cli_helper.php b/classes/cli_helper.php index c7ad19c..83a2816 100644 --- a/classes/cli_helper.php +++ b/classes/cli_helper.php @@ -96,12 +96,12 @@ public function get_arguments(): array { $longopts = $parsed['longopts']; $commandlineargs = getopt($shortopts, $longopts); $argcount = count($commandlineargs); - if (!isset($commandlineargs['z'])) { + if (!isset($commandlineargs['w'])) { echo "\nProcessed {$argcount} valid command line argument" . (($argcount !== 1) ? 's' : '') . ".\n"; } $this->processedoptions = $this->prioritise_options($commandlineargs); - if ($this->processedoptions['help']) { + if (!empty($this->processedoptions['help'])) { $this->show_help(); exit; } @@ -572,7 +572,7 @@ public function commit_hash_setup(object $activity):void { $this->create_gitignore($activity->manifestpath); $manifestdirname = dirname($activity->manifestpath); chdir($manifestdirname); - if (!(isset($this->get_arguments()['subcall']) && $this->get_arguments()['subcall'])) { + if (empty($this->get_arguments()['subcall'])) { exec("git add --all"); exec('git commit -m "Initial Commit - ' . basename($activity->manifestpath) . '"'); } @@ -684,7 +684,7 @@ public function create_directory(string $directory):string { * @return void */ public function check_for_changes($fullmanifestpath) { - if (!$this->get_arguments()['usegit'] || (isset($this->get_arguments()['subcall']) && $this->get_arguments()['subcall'])) { + if (!$this->get_arguments()['usegit'] || !empty($this->get_arguments()['subcall'])) { return; } $this->check_repo_initialised($fullmanifestpath); @@ -758,7 +758,7 @@ public function check_context(object $activity, bool $defaultwarning=false, bool echo "Failed to get list of questions from Moodle.\n"; static::call_exit(); return new \stdClass(); // Required for PHPUnit. - } else if (!$silent && !(isset($this->get_arguments()['subcall']) && $this->get_arguments()['subcall'])) { + } else if (!$silent && empty($arguments['subcall'])) { $activityname = get_class($activity); switch ($activityname) { case 'qbank_gitsync\export_repo': diff --git a/classes/create_repo.php b/classes/create_repo.php index 8f5dabf..43385c7 100644 --- a/classes/create_repo.php +++ b/classes/create_repo.php @@ -279,7 +279,7 @@ public function create_quiz_directories($clihelper, $scriptdirectory) { } echo $output; } - if ($arguments['usegit'] && !(isset($arguments()['subcall']) && $arguments()['subcall'])) { + if ($arguments['usegit'] && empty($arguments['subcall'])) { // Commit the final quiz file. // The others are committed by the following createrepo. chdir($basedirectory); diff --git a/classes/export_quiz.php b/classes/export_quiz.php index f406054..b509837 100644 --- a/classes/export_quiz.php +++ b/classes/export_quiz.php @@ -144,7 +144,7 @@ public function __construct(cli_helper $clihelper, array $moodleinstances) { $this->curlrequest->set_option(CURLOPT_RETURNTRANSFER, true); $this->curlrequest->set_option(CURLOPT_POST, 1); $this->curlrequest->set_option(CURLOPT_POSTFIELDS, $this->postsettings); - if (!$arguments['subcall']) { + if (empty($arguments['subcall'])) { $this->listcurlrequest = $this->get_curl_request($wsurl); $this->listpostsettings = [ 'wstoken' => $token, diff --git a/classes/import_repo.php b/classes/import_repo.php index ed22a88..2ca24af 100644 --- a/classes/import_repo.php +++ b/classes/import_repo.php @@ -344,7 +344,7 @@ public function __construct(cli_helper $clihelper, array $moodleinstances) { $this->listpostsettings['qcategoryname'] = $qcategoryname; $this->listcurlrequest->set_option(CURLOPT_POSTFIELDS, $this->listpostsettings); - if (count($this->manifestcontents->questions) === 0 && !$arguments['subcall']) { + if (count($this->manifestcontents->questions) === 0 && empty($arguments['subcall'])) { // A quiz in a whole course set up can have an empty manifest as // the questions may be in the course. echo "\nManifest file is empty. This should only be the case if you are importing "; @@ -786,7 +786,7 @@ public function check_question_versions(): void { } else if (property_exists($questionsinmoodle, 'exception')) { if (isset($questionsinmoodle->errorcode) && $questionsinmoodle->errorcode === 'categoryerror') { $arguments = $this->clihelper->get_arguments(); - if (!$arguments['subcall']) { + if (empty($arguments['subcall'])) { echo "Target category {$this->listpostsettings['qcategoryname']} does not exist in Moodle.\n"; echo "This should only be the case if you're importing it for the first time and\n"; echo "want to create new questions in Moodle.\n"; diff --git a/cli/exportquizstructurefrommoodle.php b/cli/exportquizstructurefrommoodle.php index 4c62ff1..0e7ef00 100644 --- a/cli/exportquizstructurefrommoodle.php +++ b/cli/exportquizstructurefrommoodle.php @@ -105,13 +105,13 @@ $clihelper = new cli_helper($options); $exportquiz = new export_quiz($clihelper, $moodleinstances); if ($exportquiz->nonquizmanifestpath) { - if (!isset($clihelper->get_arguments()['subcall'])) { + if (empty($clihelper->get_arguments()['subcall'])) { echo "Checking repo...\n"; } $clihelper->check_for_changes($exportquiz->nonquizmanifestpath); } if ($exportquiz->quizmanifestpath) { - if (!isset($clihelper->get_arguments()['subcall'])) { + if (empty($clihelper->get_arguments()['subcall'])) { echo "Checking quiz repo...\n"; } $clihelper->check_for_changes($exportquiz->quizmanifestpath); diff --git a/tests/cli_helper_test.php b/tests/cli_helper_test.php index e67ebe8..5a02917 100644 --- a/tests/cli_helper_test.php +++ b/tests/cli_helper_test.php @@ -28,6 +28,7 @@ global $CFG; use advanced_testcase; +use org\bovigo\vfs\vfsStream; /** * Allows testing of errors that lead to an exit. @@ -104,6 +105,23 @@ class cli_helper_test extends advanced_testcase { 'variable' => 'fake', 'valuerequired' => false, ], + [ + 'longopt' => 'usegit', + 'shortopt' => 'u', + 'description' => 'Is the repo controlled using Git?', + 'default' => true, + 'variable' => 'usegit', + 'valuerequired' => true, + ], + [ + 'longopt' => 'hidey', + 'shortopt' => 'q', + 'description' => 'Not settable', + 'default' => 'Sneaky', + 'variable' => 'hidey', + 'valuerequired' => true, + 'hidden' => true, + ], ]; /** @@ -113,9 +131,11 @@ class cli_helper_test extends advanced_testcase { public function test_parse_options(): void { $helper = new cli_helper($this->options); $options = $helper->parse_options(); - $this->assertEquals($options['shortopts'], 'i:l:c:m:hf'); + $this->assertEquals($options['shortopts'], 'i:l:c:m:hfu:q:'); $this->assertEquals($options['longopts'], - ['moodleinstance:', 'contextlevel:', 'coursename:', 'modulename:', 'help', 'fake']); + ['moodleinstance:', 'contextlevel:', 'coursename:', + 'modulename:', 'help', 'fake', + 'usegit:', 'hidey:']); } /** @@ -130,6 +150,7 @@ public function test_prioritise_options(): void { 'contextlevel' => 'module', 'coursename' => 'Course long', 'c' => 'Course short', + 'usegit' => 'false', ]; $options = $helper->prioritise_options($commandlineargs); @@ -145,6 +166,10 @@ public function test_prioritise_options(): void { $this->assertEquals($options['coursename'], 'Course long'); // Test option default returned when not set. $this->assertEquals($options['modulename'], 'Default module'); + // Test usegit is false when command line set to 'false. + $this->assertEquals($options['usegit'], false); + // Test hidden option set to default. + $this->assertEquals($options['hidey'], 'Sneaky'); } /** @@ -208,6 +233,49 @@ public function test_manifest_path(): void { $manifestpath); } + /** + * Quiz structure path name creation + * @covers \gitsync\cli_helper\get_quiz_structure_path() + */ + public function test_quiz_structure_path(): void { + $helper = new cli_helper($this->options); + // Module level, including replacements. + $datapath = $helper->get_quiz_structure_path('ModulassertEquals('directoryname/modul-name' . cli_helper::QUIZ_FILE, $datapath); + } + + /** + * Quiz directory name creation + * @covers \gitsync\cli_helper\get_quiz_directory() + */ + public function test_get_quiz_directory(): void { + $helper = new cli_helper($this->options); + // Module level, including replacements. + $quizdir = $helper->get_quiz_directory('directoryname', 'ModulassertEquals('directoryname_quiz_modul-name', $quizdir); + } + + /** + * Create directory + * @covers \gitsync\cli_helper\get_quiz_directory() + */ + public function test_create_directory(): void { + global $CFG; + $root = vfsStream::setup(); + vfsStream::copyFromFileSystem($CFG->dirroot . '/question/bank/gitsync/testrepoparent/', $root); + $rootpath = vfsStream::url('root'); + $helper = new cli_helper($this->options); + // Module level, including replacements. + $quizdir = $helper->get_quiz_directory($rootpath . '/below', 'Modulcreate_directory($quizdir); + $helper->create_directory($quizdir); + $helper->create_directory($quizdir); + $this->assertEquals(true, is_dir($quizdir)); + $this->assertEquals(true, is_dir($quizdir . '_1')); + $this->assertEquals(true, is_dir($quizdir . '_2')); + $this->assertEquals(false, is_dir($quizdir . '_3')); + } + /** * Validation * @covers \gitsync\cli_helper\validate_and_clean_args() @@ -225,7 +293,7 @@ public function test_validation_token(): void { */ public function test_validation_subdirectory(): void { $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'contextlevel' => 'system', + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'contextlevel' => 'system', 'subdirectory' => 'cat1', 'questioncategoryid' => 3, ]; $helper->validate_and_clean_args(); @@ -238,32 +306,32 @@ public function test_validation_subdirectory(): void { */ public function test_validation_subdirectory_format(): void { $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'contextlevel' => 'system', 'subdirectory' => 'cat1/subcat']; + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'contextlevel' => 'system', 'subdirectory' => 'cat1/subcat']; $helper->validate_and_clean_args(); $this->assertEquals('top/cat1/subcat', $helper->processedoptions['subdirectory']); $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'contextlevel' => 'system', 'subdirectory' => '/top/cat1']; + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'contextlevel' => 'system', 'subdirectory' => '/top/cat1']; $helper->validate_and_clean_args(); $this->assertEquals('top/cat1', $helper->processedoptions['subdirectory']); $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'contextlevel' => 'system', 'subdirectory' => '/top/cat1/']; + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'contextlevel' => 'system', 'subdirectory' => '/top/cat1/']; $helper->validate_and_clean_args(); $this->assertEquals('top/cat1', $helper->processedoptions['subdirectory']); $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'contextlevel' => 'system', 'subdirectory' => 'top/cat1/']; + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'contextlevel' => 'system', 'subdirectory' => 'top/cat1/']; $helper->validate_and_clean_args(); $this->assertEquals('top/cat1', $helper->processedoptions['subdirectory']); $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'contextlevel' => 'system', 'subdirectory' => '/cat1/']; + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'contextlevel' => 'system', 'subdirectory' => '/cat1/']; $helper->validate_and_clean_args(); $this->assertEquals('top/cat1', $helper->processedoptions['subdirectory']); $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'contextlevel' => 'system', 'subdirectory' => '']; + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'contextlevel' => 'system', 'subdirectory' => '']; $helper->validate_and_clean_args(); $this->assertEquals(null, $helper->processedoptions['subdirectory']); } @@ -274,32 +342,32 @@ public function test_validation_subdirectory_format(): void { */ public function test_validation_subcategory_format(): void { $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'contextlevel' => 'system', 'subcategory' => 'cat1/subcat']; + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'contextlevel' => 'system', 'subcategory' => 'cat1/subcat']; $helper->validate_and_clean_args(); $this->assertEquals('top/cat1/subcat', $helper->processedoptions['subcategory']); $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'contextlevel' => 'system', 'subcategory' => '/top/cat1']; + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'contextlevel' => 'system', 'subcategory' => '/top/cat1']; $helper->validate_and_clean_args(); $this->assertEquals('top/cat1', $helper->processedoptions['subcategory']); $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'contextlevel' => 'system', 'subcategory' => '/top/cat1/']; + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'contextlevel' => 'system', 'subcategory' => '/top/cat1/']; $helper->validate_and_clean_args(); $this->assertEquals('top/cat1', $helper->processedoptions['subcategory']); $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'contextlevel' => 'system', 'subcategory' => 'top/cat1/']; + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'contextlevel' => 'system', 'subcategory' => 'top/cat1/']; $helper->validate_and_clean_args(); $this->assertEquals('top/cat1', $helper->processedoptions['subcategory']); $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'contextlevel' => 'system', 'subcategory' => '/cat1/']; + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'contextlevel' => 'system', 'subcategory' => '/cat1/']; $helper->validate_and_clean_args(); $this->assertEquals('top/cat1', $helper->processedoptions['subcategory']); $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'contextlevel' => 'system', 'subcategory' => '']; + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'contextlevel' => 'system', 'subcategory' => '']; $helper->validate_and_clean_args(); $this->assertEquals('top', $helper->processedoptions['subcategory']); } @@ -310,12 +378,12 @@ public function test_validation_subcategory_format(): void { */ public function test_validation_manifestpath(): void { $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'contextlevel' => 'system', 'manifestpath' => '/path/subpath/']; + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'contextlevel' => 'system', 'manifestpath' => '/path/subpath/']; $helper->validate_and_clean_args(); $this->assertEquals('path/subpath', $helper->processedoptions['manifestpath']); $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'contextlevel' => 'system', 'manifestpath' => '/path/subpath/', + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'contextlevel' => 'system', 'manifestpath' => '/path/subpath/', 'coursename' => 'course1', 'instanceid' => '2', ]; $helper->validate_and_clean_args(); @@ -329,7 +397,7 @@ public function test_validation_manifestpath(): void { */ public function test_validation_instanceid(): void { $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'contextlevel' => 'system', + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'contextlevel' => 'system', 'coursename' => 'course1', 'instanceid' => '2', ]; $helper->validate_and_clean_args(); @@ -343,7 +411,7 @@ public function test_validation_instanceid(): void { */ public function test_validation_contextlevel_system(): void { $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'contextlevel' => 'system', + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'contextlevel' => 'system', 'coursename' => 'course1', 'instanceid' => '2', ]; $helper->validate_and_clean_args(); @@ -357,7 +425,7 @@ public function test_validation_contextlevel_system(): void { */ public function test_validation_contextlevel_coursecategory(): void { $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'contextlevel' => 'coursecategory', + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'contextlevel' => 'coursecategory', 'coursecategory' => 'cat1', 'coursename' => 'course1', 'modulename' => '2', ]; @@ -372,7 +440,7 @@ public function test_validation_contextlevel_coursecategory(): void { */ public function test_validation_contextlevel_course(): void { $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'contextlevel' => 'course', + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'contextlevel' => 'course', 'coursecategory' => 'cat1', 'coursename' => 'course1', 'modulename' => '2', ]; @@ -387,7 +455,7 @@ public function test_validation_contextlevel_course(): void { */ public function test_validation_contextlevel_module(): void { $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'contextlevel' => 'module', + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'contextlevel' => 'module', 'coursecategory' => 'cat1', 'coursename' => 'course1', 'modulename' => '2', ]; @@ -402,7 +470,7 @@ public function test_validation_contextlevel_module(): void { */ public function test_validation_contextlevel_coursecategory_missing(): void { $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'contextlevel' => 'coursecategory']; + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'contextlevel' => 'coursecategory']; $helper->validate_and_clean_args(); $this->expectOutputRegex('/^\nYou have specified course category level.*instanceid\).\n$/s'); } @@ -413,7 +481,7 @@ public function test_validation_contextlevel_coursecategory_missing(): void { */ public function test_validation_contextlevel_course_missing(): void { $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'contextlevel' => 'course']; + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'contextlevel' => 'course']; $helper->validate_and_clean_args(); $this->expectOutputRegex('/^\nYou have specified course level.*instanceid\).\n$/s'); } @@ -424,7 +492,7 @@ public function test_validation_contextlevel_course_missing(): void { */ public function test_validation_contextlevel_module_missing(): void { $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'contextlevel' => 'module', 'modulename' => 'mod1']; + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'contextlevel' => 'module', 'modulename' => 'mod1']; $helper->validate_and_clean_args(); $this->expectOutputRegex('/^\nYou have specified module level.*instanceid\).\n$/s'); } @@ -435,7 +503,7 @@ public function test_validation_contextlevel_module_missing(): void { */ public function test_validation_contextlevel_coursecategory_instanceid(): void { $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'contextlevel' => 'coursecategory', 'instanceid' => 3]; + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'contextlevel' => 'coursecategory', 'instanceid' => 3]; $helper->validate_and_clean_args(); $this->expectOutputString(''); } @@ -446,7 +514,7 @@ public function test_validation_contextlevel_coursecategory_instanceid(): void { */ public function test_validation_contextlevel_course_instanceid(): void { $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'contextlevel' => 'course', 'instanceid' => 3]; + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'contextlevel' => 'course', 'instanceid' => 3]; $helper->validate_and_clean_args(); $this->expectOutputString(''); } @@ -457,7 +525,7 @@ public function test_validation_contextlevel_course_instanceid(): void { */ public function test_validation_contextlevel_module_instanceid(): void { $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'contextlevel' => 'module', 'instanceid' => 3]; + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'contextlevel' => 'module', 'instanceid' => 3]; $helper->validate_and_clean_args(); $this->expectOutputString(''); } @@ -479,7 +547,7 @@ public function test_validation_contextlevel_missing(): void { */ public function test_validation_contextlevel_manifestpath(): void { $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'manifestpath' => 'path/subpath']; + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'manifestpath' => 'path/subpath']; $helper->validate_and_clean_args(); $this->expectOutputString(''); } @@ -490,7 +558,7 @@ public function test_validation_contextlevel_manifestpath(): void { */ public function test_validation_contextlevel_wrong(): void { $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'contextlevel' => 'lama']; + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'contextlevel' => 'lama']; $helper->validate_and_clean_args(); $this->expectOutputRegex('/Contextlevel should be/'); } @@ -501,7 +569,7 @@ public function test_validation_contextlevel_wrong(): void { */ public function test_validation_ignorecat(): void { $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'manifestpath' => 'path/subpath', 'ignorecat' => '/hello/']; + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'manifestpath' => 'path/subpath', 'ignorecat' => '/hello/']; $helper->validate_and_clean_args(); $this->expectOutputString(''); $this->assertEquals('/hello/', $helper->processedoptions['ignorecat']); @@ -513,7 +581,7 @@ public function test_validation_ignorecat(): void { */ public function test_validation_ignorecat_error(): void { $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'manifestpath' => 'path/subpath', 'ignorecat' => '/hello']; + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'manifestpath' => 'path/subpath', 'ignorecat' => '/hello']; @$helper->validate_and_clean_args(); $this->expectOutputRegex('/problem with your regular expression/'); } @@ -524,9 +592,20 @@ public function test_validation_ignorecat_error(): void { */ public function test_validation_ignorecat_replace(): void { $helper = new fake_cli_helper([]); - $helper->processedoptions = ['token' => 'X', 'manifestpath' => 'path/subpath', 'ignorecat' => '//hello\//']; + $helper->processedoptions = ['token' => 'X', 'usegit' => true, 'manifestpath' => 'path/subpath', 'ignorecat' => '//hello\//']; $helper->validate_and_clean_args(); $this->expectOutputString(''); $this->assertEquals('/hello\//', $helper->processedoptions['ignorecat']); } + + /** + * Validation + * @covers \gitsync\cli_helper\validate_and_clean_args() + */ + public function test_usegit(): void { + $helper = new fake_cli_helper([]); + $helper->processedoptions = ['token' => 'X', 'manifestpath' => 'path/subpath']; + $helper->validate_and_clean_args(); + $this->expectOutputRegex('/^\nAre you using Git?/s'); + } } diff --git a/tests/create_repo_test.php b/tests/create_repo_test.php index 0ef2d07..701df9f 100644 --- a/tests/create_repo_test.php +++ b/tests/create_repo_test.php @@ -189,11 +189,7 @@ public function test_temp_file_creation(): void { $tempfile = fopen($this->createrepo->tempfilepath, 'r'); $firstline = json_decode(fgets($tempfile)); $this->assertEquals('1', $firstline->questionbankentryid); - $this->assertEquals($firstline->contextlevel, '50'); $this->assertEquals($this->rootpath . '/top/One.xml', $firstline->filepath); - $this->assertEquals($firstline->coursename, 'Course 1'); - $this->assertEquals($firstline->modulename, 'Module 1'); - $this->assertEquals($firstline->coursecategory, null); $this->assertEquals($firstline->version, '10'); $this->assertEquals($firstline->format, 'xml'); } diff --git a/tests/export_quiz_test.php b/tests/export_quiz_test.php index 68277f5..75d2b7a 100644 --- a/tests/export_quiz_test.php +++ b/tests/export_quiz_test.php @@ -29,30 +29,6 @@ use advanced_testcase; use org\bovigo\vfs\vfsStream; -/** - * Allows testing of errors that lead to an exit. - */ -class fake_export_cli_helper extends cli_helper { - /** - * Override so ignored during testing - * - * @return void - */ - public static function call_exit():void { - return; - } - - /** - * Override so ignored during testing - * - * @return void - */ - public static function handle_abort():void { - return; - } -} - - /** * Test the CLI script for exporting a repo from Moodle. * @group qbank_gitsync diff --git a/tests/import_repo_test.php b/tests/import_repo_test.php index 841d479..d6668b9 100644 --- a/tests/import_repo_test.php +++ b/tests/import_repo_test.php @@ -451,12 +451,8 @@ function() { $this->assertEquals(4, count(file($this->importrepo->tempfilepath))); $tempfile = fopen($this->importrepo->tempfilepath, 'r'); $firstline = json_decode(fgets($tempfile)); - $this->assertStringContainsString('3500', $firstline->questionbankentryid); - $this->assertEquals($firstline->contextlevel, '10'); $this->assertStringContainsString($this->rootpath . '/top/cat-', $firstline->filepath); - $this->assertEquals($firstline->coursename, 'Course 1'); - $this->assertEquals($firstline->modulename, 'Test 1'); - $this->assertEquals($firstline->coursecategory, 'Cat 1'); + $this->assertEquals($firstline->version, '2'); $this->assertEquals($firstline->format, 'xml'); } @@ -597,11 +593,8 @@ function() { $tempfile = fopen($this->importrepo->tempfilepath, 'r'); $firstline = json_decode(fgets($tempfile)); $this->assertStringContainsString('3500', $firstline->questionbankentryid); - $this->assertEquals($firstline->contextlevel, '10'); $this->assertStringContainsString($this->rootpath . '/top/cat-', $firstline->filepath); - $this->assertEquals($firstline->coursename, 'Course 1'); - $this->assertEquals($firstline->modulename, 'Test 1'); - $this->assertEquals($firstline->coursecategory, 'Cat 1'); + $this->assertEquals($firstline->version, '2'); $this->assertEquals($firstline->format, 'xml'); $this->assertEquals($this->importrepo->listpostsettings["qcategoryname"], 'top/cat 2/subcat 2_1'); } @@ -786,7 +779,7 @@ public function test_manifest_file(): void { $this->assertArrayHasKey('/top/cat-2/subcat-2_1/Fourth-Question.xml', $manifestentries); $context = $manifestcontents->context; - $this->assertEquals($context->contextlevel, '10'); + $this->assertEquals($context->contextlevel, '70'); $this->assertEquals($context->coursename, 'Course 1'); $this->assertEquals($context->modulename, 'Module 1'); $this->assertEquals($context->coursecategory, ''); @@ -834,7 +827,7 @@ public function test_manifest_file_with_subdirectory(): void { $this->assertArrayHasKey('/top/cat-2/subcat-2_1/Fourth-Question.xml', $manifestentries); $context = $manifestcontents->context; - $this->assertEquals($context->contextlevel, '10'); + $this->assertEquals($context->contextlevel, '70'); $this->assertEquals($context->coursename, 'Course 1'); $this->assertEquals($context->modulename, 'Module 1'); $this->assertEquals($context->coursecategory, ''); @@ -880,7 +873,7 @@ public function test_manifest_file_with_subdirectory_and_ignore(): void { $this->assertArrayHasKey('/top/cat-2/Second-Question.xml', $manifestentries); $context = $manifestcontents->context; - $this->assertEquals($context->contextlevel, '10'); + $this->assertEquals($context->contextlevel, '70'); $this->assertEquals($context->coursename, 'Course 1'); $this->assertEquals($context->modulename, 'Module 1'); $this->assertEquals($context->coursecategory, ''); @@ -927,7 +920,7 @@ public function test_manifest_file_with_ignore(): void { $this->assertArrayHasKey('/top/cat-1/First-Question.xml', $manifestentries); $context = $manifestcontents->context; - $this->assertEquals($context->contextlevel, '10'); + $this->assertEquals($context->contextlevel, '70'); $this->assertEquals($context->coursename, 'Course 1'); $this->assertEquals($context->modulename, 'Module 1'); $this->assertEquals($context->coursecategory, '');