From 65c883915788a8c187aa398df31d1bed33f37a96 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Sun, 11 Sep 2022 19:34:26 +0200 Subject: [PATCH] MDL-75716 phpunit: Move tests to use correct names and ns (take#5) Applied the following changes to various testcase classes: - Namespaced with component[\level2-API] - Moved to level2-API subdirectory when required. - Fixed incorrect use statements with leading backslash. - Remove file phpdoc block - Remove MOODLE_INTERNAL if not needed. - Changed code to point to global scope when needed. - Fix some relative paths and comments here and there. - All them passing individually. - Complete runs passing too. Special mention to: - In lib/tests/blocklib_test.php 2 helper classes have been moved to tests/fixtures, because they needed to be namespace-free. --- lib/tests/blocklib_test.php | 116 ++--- lib/tests/fixtures/block_ablocktype.php | 28 ++ lib/tests/fixtures/testable_block_manager.php | 41 ++ lib/tests/moodlelib_test.php | 373 +++++++-------- lib/tests/oauth2_test.php | 120 +++-- lib/tests/questionlib_test.php | 429 +++++++++--------- lib/tests/weblib_format_text_test.php | 14 +- lib/tests/xhprof_test.php | 14 +- lib/tests/xhtml_container_stack_test.php | 16 +- lib/tests/xmlize_test.php | 13 +- .../seb/tests/access_manager_test.php | 23 +- .../seb/tests/backup_restore_test.php | 102 ++--- .../accessrule/seb/tests/config_key_test.php | 21 +- .../accessrule/seb/tests/hideif_rule_test.php | 19 +- .../seb/tests/link_generator_test.php | 21 +- .../seb/tests/property_list_test.php | 25 +- .../seb/tests/quiz_settings_test.php | 32 +- .../seb/tests/settings_provider_test.php | 74 ++- .../accessrule/seb/tests/template_test.php | 27 +- .../report/overview/tests/report_test.php | 31 +- .../responses_from_steps_walkthrough_test.php | 23 +- .../tests/statistics_table_test.php | 21 +- .../statistics/tests/statistics_test.php | 10 +- .../stats_from_steps_walkthrough_test.php | 41 +- mod/quiz/tests/attempt_test.php | 109 +++-- .../attempt_walkthrough_from_csv_test.php | 23 +- mod/quiz/tests/attempt_walkthrough_test.php | 19 +- mod/quiz/tests/attempts_test.php | 33 +- .../tests/calendar_event_modified_test.php | 39 +- .../local_structure_slot_random_test.php | 52 +-- ...rivacy_legacy_quizaccess_polyfill_test.php | 14 +- .../tests/quiz_question_bank_view_test.php | 23 +- mod/quiz/tests/quizdisplayoptions_test.php | 17 +- mod/quiz/tests/quizobj_test.php | 22 +- mod/quiz/tests/repaginate_test.php | 15 +- mod/quiz/tests/reportlib_test.php | 26 +- mod/quiz/tests/restore_override_test.php | 17 +- mod/quiz/tests/structure_test.php | 171 ++++--- mod/quiz/tests/tags_test.php | 31 +- 39 files changed, 1067 insertions(+), 1178 deletions(-) create mode 100644 lib/tests/fixtures/block_ablocktype.php create mode 100644 lib/tests/fixtures/testable_block_manager.php diff --git a/lib/tests/blocklib_test.php b/lib/tests/blocklib_test.php index f15ce7b32ad7b..486098b92ddf7 100644 --- a/lib/tests/blocklib_test.php +++ b/lib/tests/blocklib_test.php @@ -14,14 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Tests for the block_manager class in ../blocklib.php. - * - * @package core - * @category phpunit - * @copyright 2009 Tim Hunt - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace core; defined('MOODLE_INTERNAL') || die(); @@ -29,22 +22,28 @@ require_once($CFG->libdir . '/pagelib.php'); require_once($CFG->libdir . '/blocklib.php'); require_once($CFG->dirroot . '/blocks/moodleblock.class.php'); - +require_once(__DIR__ . '/fixtures/block_ablocktype.php'); +require_once(__DIR__ . '/fixtures/testable_block_manager.php'); /** - * Test various block related classes. + * Tests for the block_manager class in ../blocklib.php. + * + * @package core + * @category test + * @copyright 2009 Tim Hunt + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class core_blocklib_testcase extends advanced_testcase { +class blocklib_test extends \advanced_testcase { protected $testpage; protected $blockmanager; protected $isediting = null; protected function setUp(): void { parent::setUp(); - $this->testpage = new moodle_page(); - $this->testpage->set_context(context_system::instance()); + $this->testpage = new \moodle_page(); + $this->testpage->set_context(\context_system::instance()); $this->testpage->set_pagetype('phpunit-block-test'); - $this->blockmanager = new testable_block_manager($this->testpage); + $this->blockmanager = new \testable_block_manager($this->testpage); } protected function tearDown(): void { @@ -113,7 +112,7 @@ public function test_cannot_add_region_after_loaded() { // Set up fixture. $this->blockmanager->mark_loaded(); // Exercise SUT. - $this->expectException(coding_exception::class); + $this->expectException(\coding_exception::class); $this->blockmanager->add_region('too-late', false); } @@ -168,7 +167,7 @@ public function test_cannot_add_custom_region_after_loaded() { // Set up fixture. $this->blockmanager->mark_loaded(); // Exercise SUT. - $this->expectException(coding_exception::class); + $this->expectException(\coding_exception::class); $this->blockmanager->add_region('too-late'); } @@ -183,7 +182,7 @@ public function test_set_default_region() { public function test_cannot_set_unknown_region_as_default() { // Exercise SUT. - $this->expectException(coding_exception::class); + $this->expectException(\coding_exception::class); $this->blockmanager->set_default_region('a-region-name'); } @@ -191,7 +190,7 @@ public function test_cannot_change_default_region_after_loaded() { // Set up fixture. $this->blockmanager->mark_loaded(); // Exercise SUT. - $this->expectException(coding_exception::class); + $this->expectException(\coding_exception::class); $this->blockmanager->set_default_region('too-late'); } @@ -210,13 +209,13 @@ public function test_matching_page_type_patterns() { } protected function get_a_page_and_block_manager($regions, $context, $pagetype, $subpage = '') { - $page = new moodle_page; + $page = new \moodle_page; $page->set_context($context); $page->set_pagetype($pagetype); $page->set_subpage($subpage); - $page->set_url(new moodle_url('/')); + $page->set_url(new \moodle_url('/')); - $blockmanager = new testable_block_manager($page); + $blockmanager = new \testable_block_manager($page); $blockmanager->add_regions($regions, false); $blockmanager->set_default_region($regions[0]); @@ -225,7 +224,7 @@ protected function get_a_page_and_block_manager($regions, $context, $pagetype, $ protected function get_a_known_block_type() { global $DB; - $block = new stdClass; + $block = new \stdClass; $block->name = 'ablocktype'; $DB->insert_record('block', $block); return $block->name; @@ -249,7 +248,7 @@ public function test_empty_initially() { // Set up fixture. list($page, $blockmanager) = $this->get_a_page_and_block_manager(array('a-region'), - context_system::instance(), 'page-type'); + \context_system::instance(), 'page-type'); // Exercise SUT. $blockmanager->load_blocks(); // Validate. @@ -263,7 +262,7 @@ public function test_adding_and_retrieving_one_block() { // Set up fixture. $regionname = 'a-region'; $blockname = $this->get_a_known_block_type(); - $context = context_system::instance(); + $context = \context_system::instance(); list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname), $context, 'page-type'); @@ -282,7 +281,7 @@ public function test_adding_and_retrieving_two_blocks() { // Set up fixture. $regionname = 'a-region'; $blockname = $this->get_a_known_block_type(); - $context = context_system::instance(); + $context = \context_system::instance(); list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname), $context, 'page-type'); @@ -302,7 +301,7 @@ public function test_adding_blocks() { // Set up fixture. $regionname = 'a-region'; $blockname = $this->get_a_known_block_type(); - $context = context_system::instance(); + $context = \context_system::instance(); list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname), $context, 'page-type'); @@ -327,7 +326,7 @@ public function test_block_instances() { // Set up fixture. $regionname = 'a-region'; $blockname = $this->get_a_known_block_type(); - $context = context_system::instance(); + $context = \context_system::instance(); list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname), $context, 'page-type'); @@ -345,9 +344,9 @@ public function test_block_not_included_in_different_context() { $this->purge_blocks(); // Set up fixture. - $syscontext = context_system::instance(); + $syscontext = \context_system::instance(); $cat = $this->getDataGenerator()->create_category(array('name' => 'testcategory')); - $fakecontext = context_coursecat::instance($cat->id); + $fakecontext = \context_coursecat::instance($cat->id); $regionname = 'a-region'; $blockname = $this->get_a_known_block_type(); @@ -367,8 +366,8 @@ public function test_block_included_in_sub_context() { $this->purge_blocks(); // Set up fixture. - $syscontext = context_system::instance(); - $childcontext = context_coursecat::instance(1); + $syscontext = \context_system::instance(); + $childcontext = \context_coursecat::instance(1); $regionname = 'a-region'; $blockname = $this->get_a_known_block_type(); @@ -388,7 +387,7 @@ public function test_block_not_included_on_different_page_type() { $this->purge_blocks(); // Set up fixture. - $syscontext = context_system::instance(); + $syscontext = \context_system::instance(); $regionname = 'a-region'; $blockname = $this->get_a_known_block_type(); @@ -410,7 +409,7 @@ public function test_block_not_included_on_different_sub_page() { // Set up fixture. $regionname = 'a-region'; $blockname = $this->get_a_known_block_type(); - $syscontext = context_system::instance(); + $syscontext = \context_system::instance(); list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname), $syscontext, 'page-type', 'sub-page'); @@ -430,7 +429,7 @@ public function test_block_included_with_explicit_sub_page() { // Set up fixture. $regionname = 'a-region'; $blockname = $this->get_a_known_block_type(); - $syscontext = context_system::instance(); + $syscontext = \context_system::instance(); list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname), $syscontext, 'page-type', 'sub-page'); @@ -450,7 +449,7 @@ public function test_block_included_with_page_type_pattern() { // Set up fixture. $regionname = 'a-region'; $blockname = $this->get_a_known_block_type(); - $syscontext = context_system::instance(); + $syscontext = \context_system::instance(); list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname), $syscontext, 'page-type', 'sub-page'); @@ -521,7 +520,7 @@ public function test_delete_instances() { $regionname = 'a-region'; $blockname = $this->get_a_known_block_type(); - $context = context_system::instance(); + $context = \context_system::instance(); list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname), $context, 'page-type'); @@ -572,9 +571,9 @@ public function test_delete_instances() { list($insql, $inparams) = $DB->get_in_or_equal($preferences); $this->assertEquals(2, $DB->count_records_select('user_preferences', "name $insql", $inparams)); - $this->assertFalse(context_block::instance($blockids[0], IGNORE_MISSING)); - $this->assertFalse(context_block::instance($blockids[1], IGNORE_MISSING)); - context_block::instance($tokeep); // Would throw an exception if it was deleted. + $this->assertFalse(\context_block::instance($blockids[0], IGNORE_MISSING)); + $this->assertFalse(\context_block::instance($blockids[1], IGNORE_MISSING)); + \context_block::instance($tokeep); // Would throw an exception if it was deleted. } public function test_create_all_block_instances() { @@ -583,7 +582,7 @@ public function test_create_all_block_instances() { $this->setAdminUser(); $this->resetAfterTest(); $regionname = 'side-pre'; - $context = context_system::instance(); + $context = \context_system::instance(); $PAGE->reset_theme_and_output(); $CFG->theme = 'boost'; @@ -668,7 +667,7 @@ public function test_block_instance_times() { // Set up fixture. $regionname = 'a-region'; $blockname = 'html'; - $context = context_system::instance(); + $context = \context_system::instance(); list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname), $context, 'page-type'); @@ -735,8 +734,8 @@ public function test_default_dashboard() { $regionname = 'a-region'; $blockname = $this->get_a_known_block_type(); $user = self::getDataGenerator()->create_user(); - $syscontext = context_system::instance(); - $usercontext = context_user::instance($user->id); + $syscontext = \context_system::instance(); + $usercontext = \context_user::instance($user->id); // Add sitewide 'sticky' blocks. The page is not setup exactly as a site page would be... // but it does seem to mean that the bloacks are added correctly. list($sitepage, $sitebm) = $this->get_a_page_and_block_manager(array($regionname), $syscontext, 'site-index'); @@ -772,7 +771,7 @@ public function test_default_dashboard() { $mybm->load_blocks(); $mybm->reposition_block($sitestickyblock2->id, $regionname, -1); // Reload the blocks in the managers. - context_helper::reset_caches(); + \context_helper::reset_caches(); $defaultmybm->reset_caches(); $this->assertNull($defaultmybm->get_loaded_blocks()); $defaultmybm->load_blocks(); @@ -827,32 +826,3 @@ public function test_default_dashboard() { $PAGE = $storedpage; } } - -/** - * Test-specific subclass to make some protected things public. - */ -class testable_block_manager extends block_manager { - /** - * Resets the caches in the block manager. - * This allows blocks to be reloaded correctly. - */ - public function reset_caches() { - $this->birecordsbyregion = null; - $this->blockinstances = array(); - $this->visibleblockcontent = array(); - } - public function mark_loaded() { - $this->birecordsbyregion = array(); - } - public function get_loaded_blocks() { - return $this->birecordsbyregion; - } -} - -/** - * Test-specific subclass to make some protected things public. - */ -class block_ablocktype extends block_base { - public function init() { - } -} diff --git a/lib/tests/fixtures/block_ablocktype.php b/lib/tests/fixtures/block_ablocktype.php new file mode 100644 index 0000000000000..83c83c8703b9f --- /dev/null +++ b/lib/tests/fixtures/block_ablocktype.php @@ -0,0 +1,28 @@ +. + +/** + * Test-specific subclass to make some protected things public. + * + * @package core + * @category test + * @copyright 2022 onwards Eloy Lafuente (stronk7) {@link https://stronk7.com} + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class block_ablocktype extends block_base { + public function init() { + } +} diff --git a/lib/tests/fixtures/testable_block_manager.php b/lib/tests/fixtures/testable_block_manager.php new file mode 100644 index 0000000000000..f96546f38489b --- /dev/null +++ b/lib/tests/fixtures/testable_block_manager.php @@ -0,0 +1,41 @@ +. + +/** + * Test-specific subclass to make some protected things public. + * + * @package core + * @category test + * @copyright 2022 onwards Eloy Lafuente (stronk7) {@link https://stronk7.com} + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class testable_block_manager extends block_manager { + /** + * Resets the caches in the block manager. + * This allows blocks to be reloaded correctly. + */ + public function reset_caches() { + $this->birecordsbyregion = null; + $this->blockinstances = array(); + $this->visibleblockcontent = array(); + } + public function mark_loaded() { + $this->birecordsbyregion = array(); + } + public function get_loaded_blocks() { + return $this->birecordsbyregion; + } +} diff --git a/lib/tests/moodlelib_test.php b/lib/tests/moodlelib_test.php index f16f25a46e847..062a60e6f9807 100644 --- a/lib/tests/moodlelib_test.php +++ b/lib/tests/moodlelib_test.php @@ -14,6 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +namespace core; + +use lang_string; + /** * Unit tests for (some of) ../moodlelib.php. * @@ -23,10 +27,7 @@ * @author T.J.Hunt@open.ac.uk * @author nicolas@moodle.com */ - -defined('MOODLE_INTERNAL') || die(); - -class core_moodlelib_testcase extends advanced_testcase { +class moodlelib_test extends \advanced_testcase { /** * Define a local decimal separator. @@ -168,7 +169,7 @@ public function test_fix_utf8() { $this->assertSame('abc', fix_utf8('abc')); $array = array('do', 're', 'mi'); $this->assertSame($array, fix_utf8($array)); - $object = new stdClass(); + $object = new \stdClass(); $object->a = 'aa'; $object->b = 'bb'; $this->assertEquals($object, fix_utf8($object)); @@ -198,31 +199,31 @@ public function test_optional_param() { try { optional_param('username', 'default_user', null); $this->fail('coding_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('coding_exception', $ex); } try { @optional_param('username', 'default_user'); $this->fail('coding_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('coding_exception', $ex); - } catch (Error $error) { - // PHP 7.1 throws Error even earlier. + } catch (\Error $error) { + // PHP 7.1 throws \Error even earlier. $this->assertMatchesRegularExpression('/Too few arguments to function/', $error->getMessage()); } try { @optional_param('username'); $this->fail('coding_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('coding_exception', $ex); - } catch (Error $error) { - // PHP 7.1 throws Error even earlier. + } catch (\Error $error) { + // PHP 7.1 throws \Error even earlier. $this->assertMatchesRegularExpression('/Too few arguments to function/', $error->getMessage()); } try { optional_param('', 'default_user', PARAM_RAW); $this->fail('coding_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('coding_exception', $ex); } @@ -250,31 +251,31 @@ public function test_optional_param_array() { try { optional_param_array('username', array('a'=>'default_user'), null); $this->fail('coding_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('coding_exception', $ex); } try { @optional_param_array('username', array('a'=>'default_user')); $this->fail('coding_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('coding_exception', $ex); - } catch (Error $error) { - // PHP 7.1 throws Error even earlier. + } catch (\Error $error) { + // PHP 7.1 throws \Error even earlier. $this->assertMatchesRegularExpression('/Too few arguments to function/', $error->getMessage()); } try { @optional_param_array('username'); $this->fail('coding_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('coding_exception', $ex); - } catch (Error $error) { - // PHP 7.1 throws Error even earlier. + } catch (\Error $error) { + // PHP 7.1 throws \Error even earlier. $this->assertMatchesRegularExpression('/Too few arguments to function/', $error->getMessage()); } try { optional_param_array('', array('a'=>'default_user'), PARAM_RAW); $this->fail('coding_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('coding_exception', $ex); } @@ -283,7 +284,7 @@ public function test_optional_param_array() { $_POST['username'] = array('a'=>array('b'=>'post_user')); optional_param_array('username', array('a'=>'default_user'), PARAM_RAW); $this->fail('coding_exception expected'); - } catch (coding_exception $ex) { + } catch (\coding_exception $ex) { $this->assertTrue(true); } @@ -310,7 +311,7 @@ public function test_required_param() { try { $this->assertSame('default_user', required_param('username', PARAM_RAW)); $this->fail('moodle_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('moodle_exception', $ex); } @@ -319,22 +320,22 @@ public function test_required_param() { try { @required_param('username'); $this->fail('coding_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('coding_exception', $ex); - } catch (Error $error) { - // PHP 7.1 throws Error even earlier. + } catch (\Error $error) { + // PHP 7.1 throws \Error even earlier. $this->assertMatchesRegularExpression('/Too few arguments to function/', $error->getMessage()); } try { required_param('username', ''); $this->fail('coding_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('coding_exception', $ex); } try { required_param('', PARAM_RAW); $this->fail('coding_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('coding_exception', $ex); } @@ -359,22 +360,22 @@ public function test_required_param_array() { try { required_param_array('username', null); $this->fail('coding_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('coding_exception', $ex); } try { @required_param_array('username'); $this->fail('coding_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('coding_exception', $ex); - } catch (Error $error) { - // PHP 7.1 throws Error. + } catch (\Error $error) { + // PHP 7.1 throws \Error. $this->assertMatchesRegularExpression('/Too few arguments to function/', $error->getMessage()); } try { required_param_array('', PARAM_RAW); $this->fail('coding_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('coding_exception', $ex); } @@ -383,7 +384,7 @@ public function test_required_param_array() { $_POST['username'] = array('a'=>array('b'=>'post_user')); required_param_array('username', PARAM_RAW); $this->fail('coding_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('coding_exception', $ex); } @@ -392,7 +393,7 @@ public function test_required_param_array() { $_POST['username'] = 'post_user'; required_param_array('username', PARAM_RAW); $this->fail('moodle_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('moodle_exception', $ex); } @@ -407,15 +408,15 @@ public function test_clean_param() { try { clean_param(array('x', 'y'), PARAM_RAW); $this->fail('coding_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('coding_exception', $ex); } try { - $param = new stdClass(); + $param = new \stdClass(); $param->id = 1; clean_param($param, PARAM_RAW); $this->fail('coding_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('coding_exception', $ex); } @@ -423,16 +424,16 @@ public function test_clean_param() { try { clean_param('x', 'xxxxxx'); $this->fail('moodle_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('moodle_exception', $ex); } try { @clean_param('x'); $this->fail('moodle_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('moodle_exception', $ex); - } catch (Error $error) { - // PHP 7.1 throws Error even earlier. + } catch (\Error $error) { + // PHP 7.1 throws \Error even earlier. $this->assertMatchesRegularExpression('/Too few arguments to function/', $error->getMessage()); } } @@ -446,23 +447,23 @@ public function test_clean_param_array() { try { clean_param_array(array('x'), 'xxxxxx'); $this->fail('moodle_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('moodle_exception', $ex); } try { @clean_param_array(array('x')); $this->fail('moodle_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('moodle_exception', $ex); - } catch (Error $error) { - // PHP 7.1 throws Error even earlier. + } catch (\Error $error) { + // PHP 7.1 throws \Error even earlier. $this->assertMatchesRegularExpression('/Too few arguments to function/', $error->getMessage()); } try { clean_param_array(array('x', array('y')), PARAM_RAW); $this->fail('coding_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('coding_exception', $ex); } @@ -841,7 +842,7 @@ public function test_validate_param() { try { $param = validate_param('11a', PARAM_INT); $this->fail('invalid_parameter_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('invalid_parameter_exception', $ex); } @@ -851,7 +852,7 @@ public function test_validate_param() { try { $param = validate_param(null, PARAM_INT, false); $this->fail('invalid_parameter_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('invalid_parameter_exception', $ex); } @@ -861,13 +862,13 @@ public function test_validate_param() { try { $param = validate_param(array(), PARAM_INT); $this->fail('invalid_parameter_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('invalid_parameter_exception', $ex); } try { - $param = validate_param(new stdClass, PARAM_INT); + $param = validate_param(new \stdClass, PARAM_INT); $this->fail('invalid_parameter_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('invalid_parameter_exception', $ex); } @@ -890,31 +891,31 @@ public function test_validate_param() { try { $param = validate_param('1,2', PARAM_FLOAT); $this->fail('invalid_parameter_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('invalid_parameter_exception', $ex); } try { $param = validate_param('', PARAM_FLOAT); $this->fail('invalid_parameter_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('invalid_parameter_exception', $ex); } try { $param = validate_param('.', PARAM_FLOAT); $this->fail('invalid_parameter_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('invalid_parameter_exception', $ex); } try { $param = validate_param('e10', PARAM_FLOAT); $this->fail('invalid_parameter_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('invalid_parameter_exception', $ex); } try { $param = validate_param('abc', PARAM_FLOAT); $this->fail('invalid_parameter_exception expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('invalid_parameter_exception', $ex); } } @@ -1345,7 +1346,7 @@ public function test_check_user_preferences_loaded() { $DB->delete_records('user_preferences', array('userid'=>$otheruserid)); set_cache_flag('userpreferenceschanged', $otheruserid, null); - $user = new stdClass(); + $user = new \stdClass(); $user->id = $otheruserid; // Load. @@ -1394,7 +1395,7 @@ public function test_set_user_preference() { $DB->delete_records('user_preferences', array('userid'=>$otheruserid)); set_cache_flag('userpreferenceschanged', $otheruserid, null); - $user = new stdClass(); + $user = new \stdClass(); $user->id = $otheruserid; set_user_preference('aaa', 'bbb', $otheruserid); @@ -1444,7 +1445,7 @@ public function test_set_user_preference() { try { set_user_preference('_test_long_user_preference', $longvalue); $this->fail('Exception expected - longer than 1333 chars not allowed as preference value'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('coding_exception', $ex); } @@ -1452,37 +1453,37 @@ public function test_set_user_preference() { try { set_user_preference('_test_user_preferences_pref', array()); $this->fail('Exception expected - array not valid preference value'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('coding_exception', $ex); } try { - set_user_preference('_test_user_preferences_pref', new stdClass); + set_user_preference('_test_user_preferences_pref', new \stdClass); $this->fail('Exception expected - class not valid preference value'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('coding_exception', $ex); } try { set_user_preference('_test_user_preferences_pref', 1, array('xx' => 1)); $this->fail('Exception expected - user instance expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('coding_exception', $ex); } try { set_user_preference('_test_user_preferences_pref', 1, 'abc'); $this->fail('Exception expected - user instance expected'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('coding_exception', $ex); } try { set_user_preference('', 1); $this->fail('Exception expected - invalid name accepted'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('coding_exception', $ex); } try { set_user_preference('1', 1); $this->fail('Exception expected - invalid name accepted'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('coding_exception', $ex); } } @@ -1517,7 +1518,7 @@ public function test_get_extra_user_fields_essentials() { $this->resetAfterTest(); $this->setAdminUser(); - $context = context_system::instance(); + $context = \context_system::instance(); // No fields. $CFG->showuseridentity = ''; @@ -1558,10 +1559,10 @@ protected function environment_for_get_extra_user_fields_tests() { $CFG->showuseridentity = 'idnumber,country,city'; $CFG->hiddenuserfields = 'country,city'; - $env = new stdClass(); + $env = new \stdClass(); $env->course = $this->getDataGenerator()->create_course(); - $env->coursecontext = context_course::instance($env->course->id); + $env->coursecontext = \context_course::instance($env->course->id); $env->teacherrole = $DB->get_record('role', array('shortname' => 'teacher')); $env->studentrole = $DB->get_record('role', array('shortname' => 'student')); @@ -1590,7 +1591,7 @@ public function test_get_extra_user_fields_no_access() { $this->setUser($env->student); $this->assertEquals(array(), get_extra_user_fields($env->coursecontext)); - $this->assertEquals(array(), get_extra_user_fields(context_system::instance())); + $this->assertEquals(array(), get_extra_user_fields(\context_system::instance())); $this->assertDebuggingCalledCount(2); } @@ -1607,7 +1608,7 @@ public function test_get_extra_user_fields_course_only_access() { $this->setUser($env->teacher); $this->assertEquals(array('idnumber', 'country', 'city'), get_extra_user_fields($env->coursecontext)); - $this->assertEquals(array(), get_extra_user_fields(context_system::instance())); + $this->assertEquals(array(), get_extra_user_fields(\context_system::instance())); $this->assertDebuggingCalledCount(2); } @@ -1641,7 +1642,7 @@ public function test_get_extra_user_fields_anywhere_access() { $this->setUser($env->manager); $this->assertEquals(array('idnumber', 'country', 'city'), get_extra_user_fields($env->coursecontext)); - $this->assertEquals(array('idnumber', 'country', 'city'), get_extra_user_fields(context_system::instance())); + $this->assertEquals(array('idnumber', 'country', 'city'), get_extra_user_fields(\context_system::instance())); $this->assertDebuggingCalledCount(2); } @@ -1658,7 +1659,7 @@ public function test_get_extra_user_fields_schismatic_access() { $this->setUser($env->manager); assign_capability('moodle/user:viewhiddendetails', CAP_PREVENT, $env->managerrole->id, SYSCONTEXTID, true); - $this->assertEquals(array('idnumber'), get_extra_user_fields(context_system::instance())); + $this->assertEquals(array('idnumber'), get_extra_user_fields(\context_system::instance())); // Note that inside the course, the manager can still see the hidden identifiers as this is currently // controlled by a separate capability for legacy reasons. $this->assertEquals(array('idnumber', 'country', 'city'), get_extra_user_fields($env->coursecontext)); @@ -1680,7 +1681,7 @@ public function test_get_extra_user_fields_hard_to_prevent_access() { assign_capability('moodle/user:viewhiddendetails', CAP_PREVENT, $env->managerrole->id, SYSCONTEXTID, true); assign_capability('moodle/course:viewhiddenuserfields', CAP_PREVENT, $env->managerrole->id, SYSCONTEXTID, true); - $this->assertEquals(array('idnumber'), get_extra_user_fields(context_system::instance())); + $this->assertEquals(array('idnumber'), get_extra_user_fields(\context_system::instance())); $this->assertEquals(array('idnumber'), get_extra_user_fields($env->coursecontext)); $this->assertDebuggingCalledCount(2); @@ -1697,7 +1698,7 @@ public function test_get_extra_user_fields_sql() { $this->setAdminUser(); - $context = context_system::instance(); + $context = \context_system::instance(); // No fields. $CFG->showuseridentity = ''; @@ -1744,8 +1745,8 @@ public function test_some_moodle_special_dst() { // In Europe/Tallinn it was 2013/04/08 05:00:00. $expectation = '2013/04/08 05:00:00'; - $phpdt = DateTime::createFromFormat('U', $stamp, new DateTimeZone('UTC')); - $phpdt->setTimezone(new DateTimeZone('Europe/Tallinn')); + $phpdt = \DateTime::createFromFormat('U', $stamp, new \DateTimeZone('UTC')); + $phpdt->setTimezone(new \DateTimeZone('Europe/Tallinn')); $phpres = $phpdt->format('Y/m/d H:i:s'); // PHP result. $moodleres = userdate($stamp, '%Y/%m/%d %H:%M:%S', 'Europe/Tallinn', false); // Moodle result. $this->assertSame($expectation, $phpres); @@ -1753,8 +1754,8 @@ public function test_some_moodle_special_dst() { // In St. Johns it was 2013/04/07 23:30:00. $expectation = '2013/04/07 23:30:00'; - $phpdt = DateTime::createFromFormat('U', $stamp, new DateTimeZone('UTC')); - $phpdt->setTimezone(new DateTimeZone('America/St_Johns')); + $phpdt = \DateTime::createFromFormat('U', $stamp, new \DateTimeZone('UTC')); + $phpdt->setTimezone(new \DateTimeZone('America/St_Johns')); $phpres = $phpdt->format('Y/m/d H:i:s'); // PHP result. $moodleres = userdate($stamp, '%Y/%m/%d %H:%M:%S', 'America/St_Johns', false); // Moodle result. $this->assertSame($expectation, $phpres); @@ -1764,8 +1765,8 @@ public function test_some_moodle_special_dst() { // In Europe/Tallinn it was 2013/11/08 04:00:00. $expectation = '2013/11/08 04:00:00'; - $phpdt = DateTime::createFromFormat('U', $stamp, new DateTimeZone('UTC')); - $phpdt->setTimezone(new DateTimeZone('Europe/Tallinn')); + $phpdt = \DateTime::createFromFormat('U', $stamp, new \DateTimeZone('UTC')); + $phpdt->setTimezone(new \DateTimeZone('Europe/Tallinn')); $phpres = $phpdt->format('Y/m/d H:i:s'); // PHP result. $moodleres = userdate($stamp, '%Y/%m/%d %H:%M:%S', 'Europe/Tallinn', false); // Moodle result. $this->assertSame($expectation, $phpres); @@ -1773,8 +1774,8 @@ public function test_some_moodle_special_dst() { // In St. Johns it was 2013/11/07 22:30:00. $expectation = '2013/11/07 22:30:00'; - $phpdt = DateTime::createFromFormat('U', $stamp, new DateTimeZone('UTC')); - $phpdt->setTimezone(new DateTimeZone('America/St_Johns')); + $phpdt = \DateTime::createFromFormat('U', $stamp, new \DateTimeZone('UTC')); + $phpdt->setTimezone(new \DateTimeZone('America/St_Johns')); $phpres = $phpdt->format('Y/m/d H:i:s'); // PHP result. $moodleres = userdate($stamp, '%Y/%m/%d %H:%M:%S', 'America/St_Johns', false); // Moodle result. $this->assertSame($expectation, $phpres); @@ -1884,10 +1885,10 @@ public function test_userdate() { $actualoutputhtml = userdate_htmltime($vals['time'], '%A, %d %B %Y, %I:%M %p', $vals['timezone']); // On different systems case of AM PM changes so compare case insensitive. - $vals['expectedoutput'] = core_text::strtolower($vals['expectedoutput']); - $vals['expectedoutputhtml'] = core_text::strtolower($vals['expectedoutputhtml']); - $actualoutput = core_text::strtolower($actualoutput); - $actualoutputhtml = core_text::strtolower($actualoutputhtml); + $vals['expectedoutput'] = \core_text::strtolower($vals['expectedoutput']); + $vals['expectedoutputhtml'] = \core_text::strtolower($vals['expectedoutputhtml']); + $actualoutput = \core_text::strtolower($actualoutput); + $actualoutputhtml = \core_text::strtolower($actualoutputhtml); $this->assertSame($vals['expectedoutput'], $actualoutput, "Expected: {$vals['expectedoutput']} => Actual: {$actualoutput} \ndata: " . var_export($vals, true)); @@ -1902,70 +1903,70 @@ public function test_userdate() { public function test_dst_changes() { // DST switching in Prague. // From 2AM to 3AM in 1989. - $date = new DateTime('1989-03-26T01:59:00+01:00'); + $date = new \DateTime('1989-03-26T01:59:00+01:00'); $this->assertSame('Sunday, 26 March 1989, 01:59', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Europe/Prague')); - $date = new DateTime('1989-03-26T02:01:00+01:00'); + $date = new \DateTime('1989-03-26T02:01:00+01:00'); $this->assertSame('Sunday, 26 March 1989, 03:01', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Europe/Prague')); // From 3AM to 2AM in 1989 - not the same as the west Europe. - $date = new DateTime('1989-09-24T01:59:00+01:00'); + $date = new \DateTime('1989-09-24T01:59:00+01:00'); $this->assertSame('Sunday, 24 September 1989, 02:59', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Europe/Prague')); - $date = new DateTime('1989-09-24T02:01:00+01:00'); + $date = new \DateTime('1989-09-24T02:01:00+01:00'); $this->assertSame('Sunday, 24 September 1989, 02:01', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Europe/Prague')); // From 2AM to 3AM in 2014. - $date = new DateTime('2014-03-30T01:59:00+01:00'); + $date = new \DateTime('2014-03-30T01:59:00+01:00'); $this->assertSame('Sunday, 30 March 2014, 01:59', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Europe/Prague')); - $date = new DateTime('2014-03-30T02:01:00+01:00'); + $date = new \DateTime('2014-03-30T02:01:00+01:00'); $this->assertSame('Sunday, 30 March 2014, 03:01', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Europe/Prague')); // From 3AM to 2AM in 2014. - $date = new DateTime('2014-10-26T01:59:00+01:00'); + $date = new \DateTime('2014-10-26T01:59:00+01:00'); $this->assertSame('Sunday, 26 October 2014, 02:59', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Europe/Prague')); - $date = new DateTime('2014-10-26T02:01:00+01:00'); + $date = new \DateTime('2014-10-26T02:01:00+01:00'); $this->assertSame('Sunday, 26 October 2014, 02:01', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Europe/Prague')); // From 2AM to 3AM in 2020. - $date = new DateTime('2020-03-29T01:59:00+01:00'); + $date = new \DateTime('2020-03-29T01:59:00+01:00'); $this->assertSame('Sunday, 29 March 2020, 01:59', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Europe/Prague')); - $date = new DateTime('2020-03-29T02:01:00+01:00'); + $date = new \DateTime('2020-03-29T02:01:00+01:00'); $this->assertSame('Sunday, 29 March 2020, 03:01', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Europe/Prague')); // From 3AM to 2AM in 2020. - $date = new DateTime('2020-10-25T01:59:00+01:00'); + $date = new \DateTime('2020-10-25T01:59:00+01:00'); $this->assertSame('Sunday, 25 October 2020, 02:59', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Europe/Prague')); - $date = new DateTime('2020-10-25T02:01:00+01:00'); + $date = new \DateTime('2020-10-25T02:01:00+01:00'); $this->assertSame('Sunday, 25 October 2020, 02:01', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Europe/Prague')); // DST switching in NZ. // From 3AM to 2AM in 2015. - $date = new DateTime('2015-04-05T02:59:00+13:00'); + $date = new \DateTime('2015-04-05T02:59:00+13:00'); $this->assertSame('Sunday, 5 April 2015, 02:59', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Pacific/Auckland')); - $date = new DateTime('2015-04-05T03:01:00+13:00'); + $date = new \DateTime('2015-04-05T03:01:00+13:00'); $this->assertSame('Sunday, 5 April 2015, 02:01', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Pacific/Auckland')); // From 2AM to 3AM in 2009. - $date = new DateTime('2015-09-27T01:59:00+12:00'); + $date = new \DateTime('2015-09-27T01:59:00+12:00'); $this->assertSame('Sunday, 27 September 2015, 01:59', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Pacific/Auckland')); - $date = new DateTime('2015-09-27T02:01:00+12:00'); + $date = new \DateTime('2015-09-27T02:01:00+12:00'); $this->assertSame('Sunday, 27 September 2015, 03:01', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Pacific/Auckland')); // DST switching in Perth. // From 3AM to 2AM in 2009. - $date = new DateTime('2008-03-30T01:59:00+08:00'); + $date = new \DateTime('2008-03-30T01:59:00+08:00'); $this->assertSame('Sunday, 30 March 2008, 02:59', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Australia/Perth')); - $date = new DateTime('2008-03-30T02:01:00+08:00'); + $date = new \DateTime('2008-03-30T02:01:00+08:00'); $this->assertSame('Sunday, 30 March 2008, 02:01', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Australia/Perth')); // From 2AM to 3AM in 2009. - $date = new DateTime('2008-10-26T01:59:00+08:00'); + $date = new \DateTime('2008-10-26T01:59:00+08:00'); $this->assertSame('Sunday, 26 October 2008, 01:59', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Australia/Perth')); - $date = new DateTime('2008-10-26T02:01:00+08:00'); + $date = new \DateTime('2008-10-26T02:01:00+08:00'); $this->assertSame('Sunday, 26 October 2008, 03:01', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Australia/Perth')); // DST switching in US. // From 2AM to 3AM in 2014. - $date = new DateTime('2014-03-09T01:59:00-05:00'); + $date = new \DateTime('2014-03-09T01:59:00-05:00'); $this->assertSame('Sunday, 9 March 2014, 01:59', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'America/New_York')); - $date = new DateTime('2014-03-09T02:01:00-05:00'); + $date = new \DateTime('2014-03-09T02:01:00-05:00'); $this->assertSame('Sunday, 9 March 2014, 03:01', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'America/New_York')); // From 3AM to 2AM in 2014. - $date = new DateTime('2014-11-02T01:59:00-04:00'); + $date = new \DateTime('2014-11-02T01:59:00-04:00'); $this->assertSame('Sunday, 2 November 2014, 01:59', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'America/New_York')); - $date = new DateTime('2014-11-02T02:01:00-04:00'); + $date = new \DateTime('2014-11-02T02:01:00-04:00'); $this->assertSame('Sunday, 2 November 2014, 01:01', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'America/New_York')); } @@ -2117,8 +2118,8 @@ public function test_make_timestamp() { ); // On different systems case of AM PM changes so compare case insensitive. - $vals['expectedoutput'] = core_text::strtolower($vals['expectedoutput']); - $actualoutput = core_text::strtolower($actualoutput); + $vals['expectedoutput'] = \core_text::strtolower($vals['expectedoutput']); + $actualoutput = \core_text::strtolower($actualoutput); $this->assertSame($vals['expectedoutput'], $actualoutput, "Expected: {$vals['expectedoutput']} => Actual: {$actualoutput}, @@ -2196,7 +2197,7 @@ public function test_get_string() { // Make sure that object properties that can't be converted don't cause // errors. // Level one: This is as deep as current language processing goes. - $test = new stdClass; + $test = new \stdClass; $test->one = 'here'; $string = get_string('yes', null, $test, true); $this->assertEquals($yesexpected, $string); @@ -2205,8 +2206,8 @@ public function test_get_string() { // errors. // Level two: Language processing doesn't currently reach this deep. // only immediate scalar properties are worked with. - $test = new stdClass; - $test->one = new stdClass; + $test = new \stdClass; + $test->one = new \stdClass; $test->one->two = 'here'; $string = get_string('yes', null, $test, true); $this->assertEquals($yesexpected, $string); @@ -2215,9 +2216,9 @@ public function test_get_string() { // errors. // Level three: It should never ever go this deep, but we're making sure // it doesn't cause any probs anyway. - $test = new stdClass; - $test->one = new stdClass; - $test->one->two = new stdClass; + $test = new \stdClass; + $test->one = new \stdClass; + $test->one->two = new \stdClass; $test->one->two->three = 'here'; $string = get_string('yes', null, $test, true); $this->assertEquals($yesexpected, $string); @@ -2225,7 +2226,7 @@ public function test_get_string() { // Make sure that object properties that can't be converted don't cause // errors and check lang_string properties. // Level one: This is as deep as current language processing goes. - $test = new stdClass; + $test = new \stdClass; $test->one = new lang_string('yes'); $string = get_string('yes', null, $test, true); $this->assertEquals($yesexpected, $string); @@ -2234,8 +2235,8 @@ public function test_get_string() { // errors and check lang_string properties. // Level two: Language processing doesn't currently reach this deep. // only immediate scalar properties are worked with. - $test = new stdClass; - $test->one = new stdClass; + $test = new \stdClass; + $test->one = new \stdClass; $test->one->two = new lang_string('yes'); $string = get_string('yes', null, $test, true); $this->assertEquals($yesexpected, $string); @@ -2244,9 +2245,9 @@ public function test_get_string() { // errors and check lang_string properties. // Level three: It should never ever go this deep, but we're making sure // it doesn't cause any probs anyway. - $test = new stdClass; - $test->one = new stdClass; - $test->one->two = new stdClass; + $test = new \stdClass; + $test->one = new \stdClass; + $test->one->two = new \stdClass; $test->one->two->three = new lang_string('yes'); $string = get_string('yes', null, $test, true); $this->assertEquals($yesexpected, $string); @@ -2254,14 +2255,14 @@ public function test_get_string() { // Make sure that array properties that can't be converted don't cause // errors. $test = array(); - $test['one'] = new stdClass; + $test['one'] = new \stdClass; $test['one']->two = 'here'; $string = get_string('yes', null, $test, true); $this->assertEquals($yesexpected, $string); // Same thing but as above except using an object... this is allowed :P. $string = get_string('yes', null, null, true); - $object = new stdClass; + $object = new \stdClass; $object->$string = 'Yes'; $this->assertEquals($yesexpected, $string); $this->assertEquals($yesexpected, $object->$string); @@ -2307,7 +2308,7 @@ public function test_get_string_limitation() { // This is one of the limitations to the lang_string class. It can't be // used as a key. if (PHP_VERSION_ID >= 80000) { - $this->expectException(TypeError::class); + $this->expectException(\TypeError::class); } else { $this->expectWarning(); } @@ -2491,23 +2492,23 @@ public function test_delete_user() { $this->assertEventContextNotUsed($event); // Try invalid params. - $record = new stdClass(); + $record = new \stdClass(); $record->grrr = 1; try { delete_user($record); $this->fail('Expecting exception for invalid delete_user() $user parameter'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('coding_exception', $ex); } $record->id = 1; try { delete_user($record); $this->fail('Expecting exception for invalid delete_user() $user parameter'); - } catch (moodle_exception $ex) { + } catch (\moodle_exception $ex) { $this->assertInstanceOf('coding_exception', $ex); } - $record = new stdClass(); + $record = new \stdClass(); $record->id = 666; $record->username = 'xx'; $this->assertFalse($DB->record_exists('user', array('id'=>666))); // Any non-existent id is ok. @@ -2565,9 +2566,9 @@ public function test_delete_user_long_username() { // The username for the deleted user shouldn't exceed 100 characters. $usernamedeleted = $DB->get_field('user', 'username', ['id' => $user->id]); - $this->assertEquals(100, core_text::strlen($usernamedeleted)); + $this->assertEquals(100, \core_text::strlen($usernamedeleted)); - $timestrlength = core_text::strlen((string) time()); + $timestrlength = \core_text::strlen((string) time()); // It should start with the user name, and end with the current time. $this->assertStringStartsWith("{$user->username}.{$user->id}@", $usernamedeleted); @@ -2591,12 +2592,12 @@ public function test_delete_user_long_email() { // The username for the deleted user shouldn't exceed 100 characters. $usernamedeleted = $DB->get_field('user', 'username', ['id' => $user->id]); - $this->assertEquals(100, core_text::strlen($usernamedeleted)); + $this->assertEquals(100, \core_text::strlen($usernamedeleted)); - $timestrlength = core_text::strlen((string) time()); + $timestrlength = \core_text::strlen((string) time()); // Max username length is 100 chars. Select up to limit - (length of current time + 1 [period character]) from users email. - $expectedemail = core_text::substr($user->email, 0, 100 - ($timestrlength + 1)); + $expectedemail = \core_text::substr($user->email, 0, 100 - ($timestrlength + 1)); $this->assertMatchesRegularExpression('/^' . preg_quote($expectedemail) . '\.\d{' . $timestrlength . '}$/', $usernamedeleted); } @@ -2606,14 +2607,14 @@ public function test_delete_user_long_email() { */ public function test_convert_to_array() { // Check that normal classes are converted to arrays the same way as (array) would do. - $obj = new stdClass(); + $obj = new \stdClass(); $obj->prop1 = 'hello'; $obj->prop2 = array('first', 'second', 13); $obj->prop3 = 15; $this->assertEquals(convert_to_array($obj), (array)$obj); // Check that context object (with iterator) is converted to array properly. - $obj = context_system::instance(); + $obj = \context_system::instance(); $ar = array( 'id' => $obj->id, 'contextlevel' => $obj->contextlevel, @@ -2679,7 +2680,7 @@ public function test_date_format_string() { // On different systems case of AM PM changes so compare case insensitive. foreach ($tests as $test) { $str = date_format_string(1293876000, $test['str'], $test['tz']); - $this->assertSame(core_text::strtolower($test['expected']), core_text::strtolower($str)); + $this->assertSame(\core_text::strtolower($test['expected']), \core_text::strtolower($str)); } } @@ -2726,7 +2727,7 @@ public function test_get_config() { $this->assertSame('test a', get_config('core', 'phpunit_test_get_config_1')); // Test cache invalidation. - $cache = cache::make('core', 'config'); + $cache = \cache::make('core', 'config'); $this->assertIsArray($cache->get('core')); $this->assertIsArray($cache->get('mod_forum')); set_config('phpunit_test_get_config_1', 'test b'); @@ -2860,7 +2861,7 @@ public function test_validate_internal_user_password() { ); foreach ($validhashes as $password => $hash) { - $user = new stdClass(); + $user = new \stdClass(); $user->auth = 'manual'; $user->password = $hash; // The correct password should be validated. @@ -2881,7 +2882,7 @@ public function test_hash_internal_user_password() { foreach ($passwords as $password) { $hash = hash_internal_user_password($password); $fasthash = hash_internal_user_password($password, true); - $user = new stdClass(); + $user = new \stdClass(); $user->auth = 'manual'; $user->password = $hash; $this->assertTrue(validate_internal_user_password($user, $password)); @@ -2929,7 +2930,7 @@ public function test_update_internal_user_password() { // Verify event information. $this->assertInstanceOf('\core\event\user_password_updated', $event); $this->assertSame($user->id, $event->relateduserid); - $this->assertEquals(context_user::instance($user->id), $event->get_context()); + $this->assertEquals(\context_user::instance($user->id), $event->get_context()); $this->assertEventContextNotUsed($event); // Verify recovery of property 'auth'. @@ -2990,7 +2991,7 @@ public function test_fullname() { $user = $this->getDataGenerator()->create_user($record); // Back up config settings for restore later. - $originalcfg = new stdClass(); + $originalcfg = new \stdClass(); $originalcfg->fullnamedisplay = $CFG->fullnamedisplay; $originalcfg->alternativefullnameformat = $CFG->alternativefullnameformat; @@ -3081,22 +3082,22 @@ public function test_fullname() { // Check to make sure that other characters are left in place. $configarray = array(); - $configarray['0'] = new stdClass(); + $configarray['0'] = new \stdClass(); $configarray['0']->config = 'lastname firstname, middlename'; $configarray['0']->expectedname = "$user->lastname $user->firstname,"; - $configarray['1'] = new stdClass(); + $configarray['1'] = new \stdClass(); $configarray['1']->config = 'lastname firstname + alternatename'; $configarray['1']->expectedname = "$user->lastname $user->firstname + $user->alternatename"; - $configarray['2'] = new stdClass(); + $configarray['2'] = new \stdClass(); $configarray['2']->config = 'firstname aka: alternatename'; $configarray['2']->expectedname = "$user->firstname aka: $user->alternatename"; - $configarray['3'] = new stdClass(); + $configarray['3'] = new \stdClass(); $configarray['3']->config = 'firstname (alternatename)'; $configarray['3']->expectedname = "$user->firstname ($user->alternatename)"; - $configarray['4'] = new stdClass(); + $configarray['4'] = new \stdClass(); $configarray['4']->config = 'firstname [alternatename]'; $configarray['4']->expectedname = "$user->firstname [$user->alternatename]"; - $configarray['5'] = new stdClass(); + $configarray['5'] = new \stdClass(); $configarray['5']->config = 'firstname "lastname"'; $configarray['5']->expectedname = "$user->firstname \"$user->lastname\""; @@ -3111,7 +3112,7 @@ public function test_fullname() { // fullnamedisplay setting is "normal". $CFG->fullnamedisplay = 'firstname lastname'; unset($user); - $user = new stdClass(); + $user = new \stdClass(); $user->firstname = 'Stan'; $user->lastname = 'Lee'; $namedisplay = fullname($user); @@ -3221,7 +3222,7 @@ public function test_complete_user_login() { $this->assertInstanceOf('\core\event\user_loggedin', $event); $this->assertEquals('user', $event->objecttable); $this->assertEquals($user->id, $event->objectid); - $this->assertEquals(context_system::instance()->id, $event->contextid); + $this->assertEquals(\context_system::instance()->id, $event->contextid); $this->assertEventContextNotUsed($event); $user = $DB->get_record('user', array('id'=>$user->id)); @@ -3576,7 +3577,7 @@ public function test_email_to_user_attachment(?string $filedir): void { $filepath = ($filedir ?: $CFG->dataroot) . '/hello.txt'; file_put_contents($filepath, 'Hello'); - $user = core_user::get_support_user(); + $user = \core_user::get_support_user(); $message = 'Test attachment path'; // Create sink to catch all sent e-mails. @@ -3605,7 +3606,7 @@ public function test_email_to_user_attachment(?string $filedir): void { * Test sending an attachment that doesn't exist to email_to_user */ public function test_email_to_user_attachment_missing(): void { - $user = core_user::get_support_user(); + $user = \core_user::get_support_user(); $message = 'Test attachment path'; // Create sink to catch all sent e-mails. @@ -3653,7 +3654,7 @@ public function test_setnew_password_and_mail() { // Test event. $this->assertInstanceOf('\core\event\user_password_updated', $event); $this->assertSame($user->id, $event->relateduserid); - $this->assertEquals(context_user::instance($user->id), $event->get_context()); + $this->assertEquals(\context_user::instance($user->id), $event->get_context()); $this->assertEventContextNotUsed($event); } @@ -3825,7 +3826,7 @@ public function test_username_load_fields_from_object() { $this->resetAfterTest(); // This object represents the information returned from an sql query. - $userinfo = new stdClass(); + $userinfo = new \stdClass(); $userinfo->userid = 1; $userinfo->username = 'loosebruce'; $userinfo->firstname = 'Bruce'; @@ -3840,9 +3841,9 @@ public function test_username_load_fields_from_object() { $userinfo->idnumber = 3982; // Just user name fields. - $user = new stdClass(); + $user = new \stdClass(); $user = username_load_fields_from_object($user, $userinfo); - $expectedarray = new stdClass(); + $expectedarray = new \stdClass(); $expectedarray->firstname = 'Bruce'; $expectedarray->lastname = 'Campbell'; $expectedarray->firstnamephonetic = 'ブルース'; @@ -3852,11 +3853,11 @@ public function test_username_load_fields_from_object() { $this->assertEquals($user, $expectedarray); // User information for showing a picture. - $user = new stdClass(); + $user = new \stdClass(); $additionalfields = explode(',', implode(',', \core_user\fields::get_picture_fields())); $user = username_load_fields_from_object($user, $userinfo, null, $additionalfields); $user->id = $userinfo->userid; - $expectedarray = new stdClass(); + $expectedarray = new \stdClass(); $expectedarray->id = 1; $expectedarray->firstname = 'Bruce'; $expectedarray->lastname = 'Campbell'; @@ -3881,11 +3882,11 @@ public function test_username_load_fields_from_object() { // Return an object with user picture information. - $user = new stdClass(); + $user = new \stdClass(); $additionalfields = explode(',', implode(',', \core_user\fields::get_picture_fields())); $user = username_load_fields_from_object($user, $userinfo, 'author', $additionalfields); $user->id = $userinfo->userid; - $expectedarray = new stdClass(); + $expectedarray = new \stdClass(); $expectedarray->id = 1; $expectedarray->firstname = 'Bruce'; $expectedarray->lastname = 'Campbell'; @@ -4258,7 +4259,7 @@ public function data_can_send_from_real_email_address() { // Test that from display is set to show no one. [ 'email' => 'fromuser@example.com', - 'display' => core_user::MAILDISPLAY_HIDE, + 'display' => \core_user::MAILDISPLAY_HIDE, 'samecourse' => false, 'config' => "example.com\r\ntest.com", 'result' => false @@ -4266,7 +4267,7 @@ public function data_can_send_from_real_email_address() { // Test that from display is set to course members only (course member). [ 'email' => 'fromuser@example.com', - 'display' => core_user::MAILDISPLAY_COURSE_MEMBERS_ONLY, + 'display' => \core_user::MAILDISPLAY_COURSE_MEMBERS_ONLY, 'samecourse' => true, 'config' => "example.com\r\ntest.com", 'result' => true @@ -4274,7 +4275,7 @@ public function data_can_send_from_real_email_address() { // Test that from display is set to course members only (Non course member). [ 'email' => 'fromuser@example.com', - 'display' => core_user::MAILDISPLAY_COURSE_MEMBERS_ONLY, + 'display' => \core_user::MAILDISPLAY_COURSE_MEMBERS_ONLY, 'samecourse' => false, 'config' => "example.com\r\ntest.com", 'result' => false @@ -4282,7 +4283,7 @@ public function data_can_send_from_real_email_address() { // Test that from display is set to show everyone. [ 'email' => 'fromuser@example.com', - 'display' => core_user::MAILDISPLAY_EVERYONE, + 'display' => \core_user::MAILDISPLAY_EVERYONE, 'samecourse' => false, 'config' => "example.com\r\ntest.com", 'result' => true @@ -4290,14 +4291,14 @@ public function data_can_send_from_real_email_address() { // Test a few different config value formats for parsing correctness. [ 'email' => 'fromuser@example.com', - 'display' => core_user::MAILDISPLAY_EVERYONE, + 'display' => \core_user::MAILDISPLAY_EVERYONE, 'samecourse' => false, 'config' => "\n test.com\nexample.com \n", 'result' => true ], [ 'email' => 'fromuser@example.com', - 'display' => core_user::MAILDISPLAY_EVERYONE, + 'display' => \core_user::MAILDISPLAY_EVERYONE, 'samecourse' => false, 'config' => "\r\n example.com \r\n test.com \r\n", 'result' => true @@ -4306,41 +4307,41 @@ public function data_can_send_from_real_email_address() { // Test from email is not in allowed domain. // Test that from display is set to show no one. [ 'email' => 'fromuser@moodle.com', - 'display' => core_user::MAILDISPLAY_HIDE, + 'display' => \core_user::MAILDISPLAY_HIDE, 'samecourse' => false, 'config' => "example.com\r\ntest.com", 'result' => false ], // Test that from display is set to course members only (course member). [ 'email' => 'fromuser@moodle.com', - 'display' => core_user::MAILDISPLAY_COURSE_MEMBERS_ONLY, + 'display' => \core_user::MAILDISPLAY_COURSE_MEMBERS_ONLY, 'samecourse' => true, 'config' => "example.com\r\ntest.com", 'result' => false ], // Test that from display is set to course members only (Non course member. [ 'email' => 'fromuser@moodle.com', - 'display' => core_user::MAILDISPLAY_COURSE_MEMBERS_ONLY, + 'display' => \core_user::MAILDISPLAY_COURSE_MEMBERS_ONLY, 'samecourse' => false, 'config' => "example.com\r\ntest.com", 'result' => false ], // Test that from display is set to show everyone. [ 'email' => 'fromuser@moodle.com', - 'display' => core_user::MAILDISPLAY_EVERYONE, + 'display' => \core_user::MAILDISPLAY_EVERYONE, 'samecourse' => false, 'config' => "example.com\r\ntest.com", 'result' => false ], // Test a few erroneous config value and confirm failure. [ 'email' => 'fromuser@moodle.com', - 'display' => core_user::MAILDISPLAY_EVERYONE, + 'display' => \core_user::MAILDISPLAY_EVERYONE, 'samecourse' => false, 'config' => "\r\n \r\n", 'result' => false ], [ 'email' => 'fromuser@moodle.com', - 'display' => core_user::MAILDISPLAY_EVERYONE, + 'display' => \core_user::MAILDISPLAY_EVERYONE, 'samecourse' => false, 'config' => " \n \n \n ", 'result' => false @@ -4578,7 +4579,7 @@ public function test_unserialize_object(): void { $langstr = new lang_string('no'); $serializedlangstr = serialize($langstr); $unserializedlangstr = unserialize_object($serializedlangstr); - $this->assertInstanceOf(stdClass::class, $unserializedlangstr); + $this->assertInstanceOf(\stdClass::class, $unserializedlangstr); } /** @@ -4612,8 +4613,8 @@ public function test_component_class_callback_method_not_found($default) { public function test_component_class_callback_found_returns_null($default) { require_once(__DIR__ . '/fixtures/component_class_callback_example.php'); - $this->assertSame($default, component_class_callback(test_component_class_callback_example::class, 'method_returns_value', [null], $default)); - $this->assertSame($default, component_class_callback(test_component_class_callback_child_example::class, 'method_returns_value', [null], $default)); + $this->assertSame($default, component_class_callback(\test_component_class_callback_example::class, 'method_returns_value', [null], $default)); + $this->assertSame($default, component_class_callback(\test_component_class_callback_child_example::class, 'method_returns_value', [null], $default)); } /** @@ -4625,8 +4626,8 @@ public function test_component_class_callback_found_returns_null($default) { public function test_component_class_callback_found_returns_value($value) { require_once(__DIR__ . '/fixtures/component_class_callback_example.php'); - $this->assertSame($value, component_class_callback(test_component_class_callback_example::class, 'method_returns_value', [$value], 'This is not the value you were looking for')); - $this->assertSame($value, component_class_callback(test_component_class_callback_child_example::class, 'method_returns_value', [$value], 'This is not the value you were looking for')); + $this->assertSame($value, component_class_callback(\test_component_class_callback_example::class, 'method_returns_value', [$value], 'This is not the value you were looking for')); + $this->assertSame($value, component_class_callback(\test_component_class_callback_child_example::class, 'method_returns_value', [$value], 'This is not the value you were looking for')); } /** @@ -4638,8 +4639,8 @@ public function test_component_class_callback_found_returns_value($value) { public function test_component_class_callback_found_accepts_multiple($params, $count) { require_once(__DIR__ . '/fixtures/component_class_callback_example.php'); - $this->assertSame($count, component_class_callback(test_component_class_callback_example::class, 'method_returns_all_params', $params, 'This is not the value you were looking for')); - $this->assertSame($count, component_class_callback(test_component_class_callback_child_example::class, 'method_returns_all_params', $params, 'This is not the value you were looking for')); + $this->assertSame($count, component_class_callback(\test_component_class_callback_example::class, 'method_returns_all_params', $params, 'This is not the value you were looking for')); + $this->assertSame($count, component_class_callback(\test_component_class_callback_child_example::class, 'method_returns_all_params', $params, 'This is not the value you were looking for')); } /** @@ -4743,7 +4744,7 @@ public function callable_names_provider() { ], 'static_method_of_object' => [ [$this, 'my_foobar_method'], - 'core_moodlelib_testcase::my_foobar_method', + 'core\moodlelib_test::my_foobar_method', ], 'method_of_object' => [ [new lang_string('parentlanguage', 'core_langconfig'), 'my_foobar_method'], @@ -4786,13 +4787,13 @@ public function user_data_provider() { 'email', 's2@example.com', false ], 'Fetch data using a non-existent email, throw exception' => [ - 'email', 's2@example.com', false, dml_missing_record_exception::class + 'email', 's2@example.com', false, \dml_missing_record_exception::class ], 'Multiple accounts with the same email' => [ 'email', 's1@example.com', false, 1 ], 'Multiple accounts with the same email, throw exception' => [ - 'email', 's1@example.com', false, 1, dml_multiple_records_exception::class + 'email', 's1@example.com', false, 1, \dml_multiple_records_exception::class ], 'Fetch data using a valid user ID' => [ 'id', true, true diff --git a/lib/tests/oauth2_test.php b/lib/tests/oauth2_test.php index c2b206a40a5fe..8b707375f9ebf 100644 --- a/lib/tests/oauth2_test.php +++ b/lib/tests/oauth2_test.php @@ -14,15 +14,13 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Tests for oauth2 apis (\core\oauth2\*). - * - * @package core - * @copyright 2017 Damyon Wiese - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. - */ +namespace core; -defined('MOODLE_INTERNAL') || die(); +use core\oauth2\access_token; +use core\oauth2\api; +use core\oauth2\endpoint; +use core\oauth2\issuer; +use core\oauth2\system_account; /** * Tests for oauth2 apis (\core\oauth2\*). @@ -32,7 +30,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. * @coversDefaultClass \core\oauth2\api */ -class core_oauth2_testcase extends advanced_testcase { +class oauth2_test extends \advanced_testcase { /** * Tests the crud operations on oauth2 issuers. @@ -40,30 +38,30 @@ class core_oauth2_testcase extends advanced_testcase { public function test_create_and_delete_standard_issuers() { $this->resetAfterTest(); $this->setAdminUser(); - \core\oauth2\api::create_standard_issuer('google'); - \core\oauth2\api::create_standard_issuer('facebook'); - \core\oauth2\api::create_standard_issuer('microsoft'); - \core\oauth2\api::create_standard_issuer('nextcloud', 'https://dummy.local/nextcloud/'); + api::create_standard_issuer('google'); + api::create_standard_issuer('facebook'); + api::create_standard_issuer('microsoft'); + api::create_standard_issuer('nextcloud', 'https://dummy.local/nextcloud/'); - $issuers = \core\oauth2\api::get_all_issuers(); + $issuers = api::get_all_issuers(); $this->assertEquals($issuers[0]->get('name'), 'Google'); $this->assertEquals($issuers[1]->get('name'), 'Facebook'); $this->assertEquals($issuers[2]->get('name'), 'Microsoft'); $this->assertEquals($issuers[3]->get('name'), 'Nextcloud'); - \core\oauth2\api::move_down_issuer($issuers[0]->get('id')); + api::move_down_issuer($issuers[0]->get('id')); - $issuers = \core\oauth2\api::get_all_issuers(); + $issuers = api::get_all_issuers(); $this->assertEquals($issuers[0]->get('name'), 'Facebook'); $this->assertEquals($issuers[1]->get('name'), 'Google'); $this->assertEquals($issuers[2]->get('name'), 'Microsoft'); $this->assertEquals($issuers[3]->get('name'), 'Nextcloud'); - \core\oauth2\api::delete_issuer($issuers[1]->get('id')); + api::delete_issuer($issuers[1]->get('id')); - $issuers = \core\oauth2\api::get_all_issuers(); + $issuers = api::get_all_issuers(); $this->assertEquals($issuers[0]->get('name'), 'Facebook'); $this->assertEquals($issuers[1]->get('name'), 'Microsoft'); @@ -78,7 +76,7 @@ public function test_create_nextcloud_without_url() { $this->setAdminUser(); $this->expectException(\moodle_exception::class); - \core\oauth2\api::create_standard_issuer('nextcloud'); + api::create_standard_issuer('nextcloud'); } /** @@ -87,31 +85,31 @@ public function test_create_nextcloud_without_url() { public function test_getters() { $this->resetAfterTest(); $this->setAdminUser(); - $issuer = \core\oauth2\api::create_standard_issuer('microsoft'); + $issuer = api::create_standard_issuer('microsoft'); - $same = \core\oauth2\api::get_issuer($issuer->get('id')); + $same = api::get_issuer($issuer->get('id')); foreach ($same->properties_definition() as $name => $def) { $this->assertTrue($issuer->get($name) == $same->get($name)); } - $endpoints = \core\oauth2\api::get_endpoints($issuer); - $same = \core\oauth2\api::get_endpoint($endpoints[0]->get('id')); + $endpoints = api::get_endpoints($issuer); + $same = api::get_endpoint($endpoints[0]->get('id')); $this->assertEquals($endpoints[0]->get('id'), $same->get('id')); $this->assertEquals($endpoints[0]->get('name'), $same->get('name')); $todelete = $endpoints[0]; - \core\oauth2\api::delete_endpoint($todelete->get('id')); - $endpoints = \core\oauth2\api::get_endpoints($issuer); + api::delete_endpoint($todelete->get('id')); + $endpoints = api::get_endpoints($issuer); $this->assertNotEquals($endpoints[0]->get('id'), $todelete->get('id')); - $userfields = \core\oauth2\api::get_user_field_mappings($issuer); - $same = \core\oauth2\api::get_user_field_mapping($userfields[0]->get('id')); + $userfields = api::get_user_field_mappings($issuer); + $same = api::get_user_field_mapping($userfields[0]->get('id')); $this->assertEquals($userfields[0]->get('id'), $same->get('id')); $todelete = $userfields[0]; - \core\oauth2\api::delete_user_field_mapping($todelete->get('id')); - $userfields = \core\oauth2\api::get_user_field_mappings($issuer); + api::delete_user_field_mapping($todelete->get('id')); + $userfields = api::get_user_field_mappings($issuer); $this->assertNotEquals($userfields[0]->get('id'), $todelete->get('id')); } @@ -144,16 +142,16 @@ public function system_oauth_client_provider() { * Tests we can get a logged in oauth client for a system account. * * @dataProvider system_oauth_client_provider - * @param stdClass $responsedata The response data to be mocked. + * @param \stdClass $responsedata The response data to be mocked. * @param int $expiresin The expected expiration time. */ public function test_get_system_oauth_client($responsedata, $expiresin) { $this->resetAfterTest(); $this->setAdminUser(); - $issuer = \core\oauth2\api::create_standard_issuer('microsoft'); + $issuer = api::create_standard_issuer('microsoft'); - $requiredscopes = \core\oauth2\api::get_system_scopes_for_issuer($issuer); + $requiredscopes = api::get_system_scopes_for_issuer($issuer); // Fake a system account. $data = (object) [ 'issuerid' => $issuer->get('id'), @@ -162,17 +160,17 @@ public function test_get_system_oauth_client($responsedata, $expiresin) { 'email' => 'sys@example.com', 'username' => 'sys' ]; - $sys = new \core\oauth2\system_account(0, $data); + $sys = new system_account(0, $data); $sys->create(); // Fake a response with an access token. $response = json_encode($responsedata); - curl::mock_response($response); - $client = \core\oauth2\api::get_system_oauth_client($issuer); + \curl::mock_response($response); + $client = api::get_system_oauth_client($issuer); $this->assertTrue($client->is_logged_in()); // Check token expiry. - $accesstoken = \core\oauth2\access_token::get_record(['issuerid' => $issuer->get('id')]); + $accesstoken = access_token::get_record(['issuerid' => $issuer->get('id')]); // Get the difference between the actual and expected expiry times. // They might differ by a couple of seconds depending on the timing when the token gets actually processed. @@ -190,24 +188,24 @@ public function test_enable_disable_issuer() { $this->resetAfterTest(); $this->setAdminUser(); - $issuer = \core\oauth2\api::create_standard_issuer('microsoft'); + $issuer = api::create_standard_issuer('microsoft'); $issuerid = $issuer->get('id'); - \core\oauth2\api::enable_issuer($issuerid); - $check = \core\oauth2\api::get_issuer($issuer->get('id')); + api::enable_issuer($issuerid); + $check = api::get_issuer($issuer->get('id')); $this->assertTrue((boolean)$check->get('enabled')); - \core\oauth2\api::enable_issuer($issuerid); - $check = \core\oauth2\api::get_issuer($issuer->get('id')); + api::enable_issuer($issuerid); + $check = api::get_issuer($issuer->get('id')); $this->assertTrue((boolean)$check->get('enabled')); - \core\oauth2\api::disable_issuer($issuerid); - $check = \core\oauth2\api::get_issuer($issuer->get('id')); + api::disable_issuer($issuerid); + $check = api::get_issuer($issuer->get('id')); $this->assertFalse((boolean)$check->get('enabled')); - \core\oauth2\api::enable_issuer($issuerid); - $check = \core\oauth2\api::get_issuer($issuer->get('id')); + api::enable_issuer($issuerid); + $check = api::get_issuer($issuer->get('id')); $this->assertTrue((boolean)$check->get('enabled')); } @@ -218,7 +216,7 @@ public function test_issuer_alloweddomains() { $this->resetAfterTest(); $this->setAdminUser(); - $issuer = \core\oauth2\api::create_standard_issuer('microsoft'); + $issuer = api::create_standard_issuer('microsoft'); $issuer->set('alloweddomains', ''); @@ -281,10 +279,10 @@ public function test_create_endpoints_for_standard_issuer(string $type, ?string if ($expectedexception) { $this->expectException($expectedexception); } - $issuer = \core\oauth2\api::create_standard_issuer($type, $baseurl); + $issuer = api::create_standard_issuer($type, $baseurl); // Check endpoints have been created. - $endpoints = \core\oauth2\api::get_endpoints($issuer); + $endpoints = api::get_endpoints($issuer); $this->assertNotEmpty($endpoints); $this->assertNotEmpty($issuer->get('image')); // Check discovery URL. @@ -294,7 +292,7 @@ public function test_create_endpoints_for_standard_issuer(string $type, ?string $this->assertFalse($issuer->get_endpoint_url('discovery')); } // Check userfield mappings. - $userfieldmappings = core\oauth2\api::get_user_field_mappings($issuer); + $userfieldmappings =api::get_user_field_mappings($issuer); if ($hasmappingfields) { $this->assertNotEmpty($userfieldmappings); } else { @@ -373,21 +371,21 @@ public function create_endpoints_for_standard_issuer_provider(): array { public function test_get_all_issuers() { $this->resetAfterTest(); $this->setAdminUser(); - $googleissuer = core\oauth2\api::create_standard_issuer('google'); - core\oauth2\api::create_standard_issuer('facebook'); - core\oauth2\api::create_standard_issuer('microsoft'); + $googleissuer = api::create_standard_issuer('google'); + api::create_standard_issuer('facebook'); + api::create_standard_issuer('microsoft'); // Set Google issuer to be shown only on login page. $record = $googleissuer->to_record(); $record->showonloginpage = $googleissuer::LOGINONLY; - core\oauth2\api::update_issuer($record); + api::update_issuer($record); - $issuers = \core\oauth2\api::get_all_issuers(); + $issuers = api::get_all_issuers(); $this->assertCount(2, $issuers); $expected = ['Microsoft', 'Facebook']; $this->assertEqualsCanonicalizing($expected, [$issuers[0]->get_display_name(), $issuers[1]->get_display_name()]); - $issuers = \core\oauth2\api::get_all_issuers(true); + $issuers = api::get_all_issuers(true); $this->assertCount(3, $issuers); $expected = ['Google', 'Microsoft', 'Facebook']; $this->assertEqualsCanonicalizing($expected, @@ -400,12 +398,12 @@ public function test_get_all_issuers() { public function test_is_available_for_login() { $this->resetAfterTest(); $this->setAdminUser(); - $googleissuer = core\oauth2\api::create_standard_issuer('google'); + $googleissuer = api::create_standard_issuer('google'); // Set Google issuer to be shown only on login page. $record = $googleissuer->to_record(); $record->showonloginpage = $googleissuer::LOGINONLY; - core\oauth2\api::update_issuer($record); + api::update_issuer($record); $this->assertFalse($googleissuer->is_available_for_login()); @@ -417,13 +415,13 @@ public function test_is_available_for_login() { $this->assertTrue($googleissuer->is_available_for_login()); // Set showonloginpage to service only. - $googleissuer->set('showonloginpage', \core\oauth2\issuer::SERVICEONLY); + $googleissuer->set('showonloginpage', issuer::SERVICEONLY); $googleissuer->update(); $this->assertFalse($googleissuer->is_available_for_login()); // Set showonloginpage to everywhere (service and login) and disable issuer. - $googleissuer->set('showonloginpage', \core\oauth2\issuer::EVERYWHERE); + $googleissuer->set('showonloginpage', issuer::EVERYWHERE); $googleissuer->set('enabled', 0); $googleissuer->update(); @@ -436,11 +434,11 @@ public function test_is_available_for_login() { $this->assertTrue($googleissuer->is_available_for_login()); // Remove userinfo endpoint from issuer. - $endpoint = core\oauth2\endpoint::get_record([ + $endpoint = endpoint::get_record([ 'issuerid' => $googleissuer->get('id'), 'name' => 'userinfo_endpoint' ]); - \core\oauth2\api::delete_endpoint($endpoint->get('id')); + api::delete_endpoint($endpoint->get('id')); $this->assertFalse($googleissuer->is_available_for_login()); } diff --git a/lib/tests/questionlib_test.php b/lib/tests/questionlib_test.php index 0ffa10cfe7799..4f48f4b340e0d 100644 --- a/lib/tests/questionlib_test.php +++ b/lib/tests/questionlib_test.php @@ -14,16 +14,9 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Unit tests for (some of) ../questionlib.php. - * - * @package core_question - * @category phpunit - * @copyright 2006 The Open University - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -use core_tag\output\tag; +namespace core; +use question_bank; defined('MOODLE_INTERNAL') || die(); @@ -39,10 +32,12 @@ /** * Unit tests for (some of) ../questionlib.php. * + * @package core_question + * @category test * @copyright 2006 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class core_questionlib_testcase extends advanced_testcase { +class questionlib_test extends \advanced_testcase { /** * Test set up. @@ -81,19 +76,19 @@ public function setup_quiz_and_questions($type = 'module') { switch ($type) { case 'course': - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); break; case 'category': - $context = context_coursecat::instance($category->id); + $context = \context_coursecat::instance($category->id); break; case 'system': - $context = context_system::instance(); + $context = \context_system::instance(); break; default: - $context = context_module::instance($quiz->cmid); + $context = \context_module::instance($quiz->cmid); break; } @@ -159,8 +154,8 @@ public function test_altering_tag_instance_context() { $coursecat2 = $this->getDataGenerator()->create_category(); // Create a couple of categories and questions. - $context1 = context_coursecat::instance($coursecat1->id); - $context2 = context_coursecat::instance($coursecat2->id); + $context1 = \context_coursecat::instance($coursecat1->id); + $context2 = \context_coursecat::instance($coursecat2->id); $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); $questioncat1 = $questiongenerator->create_question_category(array('contextid' => $context1->id)); @@ -172,10 +167,10 @@ public function test_altering_tag_instance_context() { $question4 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat2->id)); // Now lets tag these questions. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $context1, array('tag 1', 'tag 2')); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $context1, array('tag 3', 'tag 4')); - core_tag_tag::set_item_tags('core_question', 'question', $question3->id, $context2, array('tag 5', 'tag 6')); - core_tag_tag::set_item_tags('core_question', 'question', $question4->id, $context2, array('tag 7', 'tag 8')); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $context1, array('tag 1', 'tag 2')); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $context1, array('tag 3', 'tag 4')); + \core_tag_tag::set_item_tags('core_question', 'question', $question3->id, $context2, array('tag 5', 'tag 6')); + \core_tag_tag::set_item_tags('core_question', 'question', $question4->id, $context2, array('tag 7', 'tag 8')); // Test moving the questions to another category. question_move_questions_to_category(array($question1->id, $question2->id), $questioncat2->id); @@ -202,7 +197,7 @@ public function test_altering_tag_instance_context() { // Now test moving them back. question_move_category_to_context($questioncat1->id, $questioncat2->contextid, - context_coursecat::instance($coursecat1->id)->id); + \context_coursecat::instance($coursecat1->id)->id); // Test that all tag_instances are now reset to how they were initially. $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question', @@ -221,22 +216,22 @@ public function test_altering_tag_instance_context() { $course = $this->getDataGenerator()->create_course(); // Create some question categories and questions in this course. - $coursecontext = context_course::instance($course->id); + $coursecontext = \context_course::instance($course->id); $questioncat = $questiongenerator->create_question_category(array('contextid' => $coursecontext->id)); $question1 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat->id)); $question2 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat->id)); // Add some tags to these questions. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, array('tag 1', 'tag 2')); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, array('tag 1', 'tag 2')); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, array('tag 1', 'tag 2')); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, array('tag 1', 'tag 2')); // Create a course that we are going to restore the other course to. $course2 = $this->getDataGenerator()->create_course(); // Create backup file and save it to the backup location. - $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE, - backup::INTERACTIVE_NO, backup::MODE_GENERAL, 2); + $bc = new \backup_controller(\backup::TYPE_1COURSE, $course->id, \backup::FORMAT_MOODLE, + \backup::INTERACTIVE_NO, \backup::MODE_GENERAL, 2); $bc->execute_plan(); $results = $bc->get_results(); $file = $results['backup_destination']; @@ -246,14 +241,14 @@ public function test_altering_tag_instance_context() { $bc->destroy(); // Now restore the course. - $rc = new restore_controller('test-restore-course', $course2->id, backup::INTERACTIVE_NO, - backup::MODE_GENERAL, 2, backup::TARGET_NEW_COURSE); + $rc = new \restore_controller('test-restore-course', $course2->id, \backup::INTERACTIVE_NO, + \backup::MODE_GENERAL, 2, \backup::TARGET_NEW_COURSE); $rc->execute_precheck(); $rc->execute_plan(); // Get the created question category. $restoredcategory = $DB->get_record_select('question_categories', 'contextid = ? AND parent <> 0', - array(context_course::instance($course2->id)->id), '*', MUST_EXIST); + array(\context_course::instance($course2->id)->id), '*', MUST_EXIST); // Check that there are two questions in the restored to course's context. $this->assertEquals(2, $DB->count_records('question', array('category' => $restoredcategory->id))); @@ -268,7 +263,7 @@ public function test_question_delete_question() { global $DB; // Setup. - $context = context_system::instance(); + $context = \context_system::instance(); $qgen = $this->getDataGenerator()->get_plugin_generator('core_question'); $qcat = $qgen->create_question_category(array('contextid' => $context->id)); $q1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id)); @@ -290,7 +285,7 @@ public function test_question_delete_question_broken_data() { global $DB; // Setup. - $context = context_system::instance(); + $context = \context_system::instance(); $qgen = $this->getDataGenerator()->get_plugin_generator('core_question'); $qcat = $qgen->create_question_category(array('contextid' => $context->id)); $q1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id)); @@ -474,7 +469,7 @@ public function test_question_save_from_deletion() { list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions(); - $context = context::instance_by_id($qcat->contextid); + $context = \context::instance_by_id($qcat->contextid); $newcat = question_save_from_deletion(array_column($questions, 'id'), $context->get_parent_context()->id, $context->get_context_name()); @@ -502,7 +497,7 @@ public function test_question_save_from_deletion_quiz_with_long_name() { $DB->update_record('quiz', $quiz); - $context = context::instance_by_id($qcat->contextid); + $context = \context::instance_by_id($qcat->contextid); $newcat = question_save_from_deletion(array_column($questions, 'id'), $context->get_parent_context()->id, $context->get_context_name()); @@ -527,7 +522,7 @@ public function test_question_remove_stale_questions_from_category() { $quiz = $dg->create_module('quiz', ['course' => $course->id]); $qgen = $dg->get_plugin_generator('core_question'); - $context = context_system::instance(); + $context = \context_system::instance(); $qcat1 = $qgen->create_question_category(['contextid' => $context->id]); $q1a = $qgen->create_question('shortanswer', null, ['category' => $qcat1->id]); // Will be hidden. @@ -612,15 +607,15 @@ public function test_get_question_options_includes_question_tags() { list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); $question1 = $questions[0]; $question2 = $questions[1]; - $qcontext = context::instance_by_id($qcat->contextid); + $qcontext = \context::instance_by_id($qcat->contextid); - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo', 'bar']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['baz', 'bop']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo', 'bar']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['baz', 'bop']); get_question_options($questions, true); foreach ($questions as $question) { - $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id); + $tags = \core_tag_tag::get_item_tags('core_question', 'question', $question->id); $expectedtags = []; $actualtags = $question->tags; foreach ($tags as $tag) { @@ -649,15 +644,15 @@ public function test_get_question_options_includes_course_tags() { list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); $question1 = $questions[0]; $question2 = $questions[1]; - $coursecontext = context_course::instance($course->id); + $coursecontext = \context_course::instance($course->id); - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['foo', 'bar']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['baz', 'bop']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['foo', 'bar']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['baz', 'bop']); get_question_options($questions, true); foreach ($questions as $question) { - $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id); + $tags = \core_tag_tag::get_item_tags('core_question', 'question', $question->id); $expectedcoursetags = []; $actualcoursetags = $question->coursetags; foreach ($tags as $tag) { @@ -688,17 +683,17 @@ public function test_get_question_options_course_tags_in_course_question_context list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('course'); $question1 = $questions[0]; $question2 = $questions[1]; - $coursecontext = context_course::instance($course->id); + $coursecontext = \context_course::instance($course->id); // Create course level tags in the course context that matches the question // course context. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['foo', 'bar']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['baz', 'bop']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['foo', 'bar']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['baz', 'bop']); get_question_options($questions, true); foreach ($questions as $question) { - $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id); + $tags = \core_tag_tag::get_item_tags('core_question', 'question', $question->id); $actualtagobjects = $question->tagobjects; sort($tags); @@ -721,18 +716,18 @@ public function test_get_question_options_includes_question_and_course_tags() { list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); $question1 = $questions[0]; $question2 = $questions[1]; - $qcontext = context::instance_by_id($qcat->contextid); - $coursecontext = context_course::instance($course->id); + $qcontext = \context::instance_by_id($qcat->contextid); + $coursecontext = \context_course::instance($course->id); - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo', 'bar']); - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['cfoo', 'cbar']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['baz', 'bop']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['cbaz', 'cbop']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo', 'bar']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['cfoo', 'cbar']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['baz', 'bop']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['cbaz', 'cbop']); get_question_options($questions, true); foreach ($questions as $question) { - $alltags = core_tag_tag::get_item_tags('core_question', 'question', $question->id); + $alltags = \core_tag_tag::get_item_tags('core_question', 'question', $question->id); $tags = array_filter($alltags, function($tag) use ($qcontext) { return $tag->taginstancecontextid == $qcontext->id; }); @@ -773,26 +768,26 @@ public function test_get_question_options_normalises_question_tags() { list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); $question1 = $questions[0]; $question2 = $questions[1]; - $qcontext = context::instance_by_id($qcat->contextid); - $systemcontext = context_system::instance(); + $qcontext = \context::instance_by_id($qcat->contextid); + $systemcontext = \context_system::instance(); - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo', 'bar']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['baz', 'bop']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo', 'bar']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['baz', 'bop']); - $q1tags = core_tag_tag::get_item_tags('core_question', 'question', $question1->id); - $q2tags = core_tag_tag::get_item_tags('core_question', 'question', $question2->id); + $q1tags = \core_tag_tag::get_item_tags('core_question', 'question', $question1->id); + $q2tags = \core_tag_tag::get_item_tags('core_question', 'question', $question2->id); $q1tag = array_shift($q1tags); $q2tag = array_shift($q2tags); // Change two of the tag instances to be a different (non-course) context to the // question tag context. These tags should then be normalised back to the question // tag context. - core_tag_tag::change_instances_context([$q1tag->taginstanceid, $q2tag->taginstanceid], $systemcontext); + \core_tag_tag::change_instances_context([$q1tag->taginstanceid, $q2tag->taginstanceid], $systemcontext); get_question_options($questions, true); foreach ($questions as $question) { - $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id); + $tags = \core_tag_tag::get_item_tags('core_question', 'question', $question->id); // The database should have been updated with the correct context id. foreach ($tags as $tag) { @@ -815,15 +810,15 @@ public function test_get_question_options_includes_course_context_question_tags( list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('course'); $question1 = $questions[0]; $question2 = $questions[1]; - $coursecontext = context_course::instance($course->id); + $coursecontext = \context_course::instance($course->id); - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['foo', 'bar']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['baz', 'bop']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['foo', 'bar']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['baz', 'bop']); get_question_options($questions, true); foreach ($questions as $question) { - $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id); + $tags = \core_tag_tag::get_item_tags('core_question', 'question', $question->id); // Tags in a course context that matches the question context should // not be considered course tags. $this->assertEmpty($question->coursetagobjects); @@ -844,16 +839,16 @@ public function test_get_question_options_includes_multiple_courses_tags() { list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); $question1 = $questions[0]; $question2 = $questions[1]; - $coursecontext = context_course::instance($course->id); + $coursecontext = \context_course::instance($course->id); // Create a sibling course. $siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category]); - $siblingcoursecontext = context_course::instance($siblingcourse->id); + $siblingcoursecontext = \context_course::instance($siblingcourse->id); // Create course tags. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['c1']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['c1']); - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $siblingcoursecontext, ['c2']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $siblingcoursecontext, ['c2']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['c1']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['c1']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $siblingcoursecontext, ['c2']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $siblingcoursecontext, ['c2']); get_question_options($questions, true); @@ -877,17 +872,17 @@ public function test_get_question_options_includes_filter_course_tags() { list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); $question1 = $questions[0]; $question2 = $questions[1]; - $coursecontext = context_course::instance($course->id); + $coursecontext = \context_course::instance($course->id); // Create a sibling course. $siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category]); - $siblingcoursecontext = context_course::instance($siblingcourse->id); + $siblingcoursecontext = \context_course::instance($siblingcourse->id); // Create course tags. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['foo']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['bar']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['foo']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['bar']); // Create sibling course tags. These should be filtered out. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $siblingcoursecontext, ['filtered1']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $siblingcoursecontext, ['filtered2']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $siblingcoursecontext, ['filtered1']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $siblingcoursecontext, ['filtered2']); // Ask to only receive course tags from $course (ignoring $siblingcourse tags). get_question_options($questions, true, [$course]); @@ -910,21 +905,21 @@ public function test_question_move_question_tags_to_new_context_system_to_course list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('system'); $question1 = $questions[0]; $question2 = $questions[1]; - $qcontext = context::instance_by_id($qcat->contextid); - $newcontext = context_coursecat::instance($category->id); + $qcontext = \context::instance_by_id($qcat->contextid); + $newcontext = \context_coursecat::instance($category->id); foreach ($questions as $question) { $question->contextid = $qcat->contextid; } // Create tags in the system context. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo', 'bar']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo', 'bar']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo', 'bar']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo', 'bar']); question_move_question_tags_to_new_context($questions, $newcontext); foreach ($questions as $question) { - $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id); + $tags = \core_tag_tag::get_item_tags('core_question', 'question', $question->id); // All of the tags should have their context id set to the new context. foreach ($tags as $tag) { @@ -942,25 +937,25 @@ public function test_question_move_question_tags_to_new_context_system_to_course list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('system'); $question1 = $questions[0]; $question2 = $questions[1]; - $qcontext = context::instance_by_id($qcat->contextid); - $coursecontext = context_course::instance($course->id); - $newcontext = context_coursecat::instance($category->id); + $qcontext = \context::instance_by_id($qcat->contextid); + $coursecontext = \context_course::instance($course->id); + $newcontext = \context_coursecat::instance($category->id); foreach ($questions as $question) { $question->contextid = $qcat->contextid; } // Create tags in the system context. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']); // Create tags in the course context. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['ctag']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['ctag']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['ctag']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['ctag']); question_move_question_tags_to_new_context($questions, $newcontext); foreach ($questions as $question) { - $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id); + $tags = \core_tag_tag::get_item_tags('core_question', 'question', $question->id); foreach ($tags as $tag) { if ($tag->name == 'ctag') { @@ -982,21 +977,21 @@ public function test_question_move_question_tags_to_new_context_course_cat_to_sy list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); $question1 = $questions[0]; $question2 = $questions[1]; - $qcontext = context::instance_by_id($qcat->contextid); - $newcontext = context_system::instance(); + $qcontext = \context::instance_by_id($qcat->contextid); + $newcontext = \context_system::instance(); foreach ($questions as $question) { $question->contextid = $qcat->contextid; } // Create tags in the course category context. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo', 'bar']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo', 'bar']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo', 'bar']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo', 'bar']); question_move_question_tags_to_new_context($questions, $newcontext); foreach ($questions as $question) { - $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id); + $tags = \core_tag_tag::get_item_tags('core_question', 'question', $question->id); // All of the tags should have their context id set to the new context. foreach ($tags as $tag) { @@ -1014,25 +1009,25 @@ public function test_question_move_question_tags_to_new_context_course_cat_to_sy list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); $question1 = $questions[0]; $question2 = $questions[1]; - $qcontext = context::instance_by_id($qcat->contextid); - $coursecontext = context_course::instance($course->id); - $newcontext = context_system::instance(); + $qcontext = \context::instance_by_id($qcat->contextid); + $coursecontext = \context_course::instance($course->id); + $newcontext = \context_system::instance(); foreach ($questions as $question) { $question->contextid = $qcat->contextid; } // Create tags in the system context. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']); // Create tags in the course context. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['ctag']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['ctag']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['ctag']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['ctag']); question_move_question_tags_to_new_context($questions, $newcontext); foreach ($questions as $question) { - $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id); + $tags = \core_tag_tag::get_item_tags('core_question', 'question', $question->id); foreach ($tags as $tag) { if ($tag->name == 'ctag') { @@ -1054,8 +1049,8 @@ public function test_question_move_question_tags_to_new_context_course_cat_to_co list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); $question1 = $questions[0]; $question2 = $questions[1]; - $qcontext = context::instance_by_id($qcat->contextid); - $coursecontext = context_course::instance($course->id); + $qcontext = \context::instance_by_id($qcat->contextid); + $coursecontext = \context_course::instance($course->id); $newcontext = $coursecontext; foreach ($questions as $question) { @@ -1063,16 +1058,16 @@ public function test_question_move_question_tags_to_new_context_course_cat_to_co } // Create tags in the system context. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']); // Create tags in the course context. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['ctag']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['ctag']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['ctag']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['ctag']); question_move_question_tags_to_new_context($questions, $newcontext); foreach ($questions as $question) { - $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id); + $tags = \core_tag_tag::get_item_tags('core_question', 'question', $question->id); // Each question should have 2 tags. $this->assertCount(2, $tags); @@ -1095,9 +1090,9 @@ public function test_question_move_question_tags_to_new_context_remove_other_cou $siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category]); $question1 = $questions[0]; $question2 = $questions[1]; - $qcontext = context::instance_by_id($qcat->contextid); - $coursecontext = context_course::instance($course->id); - $siblingcoursecontext = context_course::instance($siblingcourse->id); + $qcontext = \context::instance_by_id($qcat->contextid); + $coursecontext = \context_course::instance($course->id); + $siblingcoursecontext = \context_course::instance($siblingcourse->id); $newcontext = $coursecontext; foreach ($questions as $question) { @@ -1105,20 +1100,20 @@ public function test_question_move_question_tags_to_new_context_remove_other_cou } // Create tags in the system context. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']); // Create tags in the target course context. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['ctag']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['ctag']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['ctag']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['ctag']); // Create tags in the sibling course context. These should be deleted as // part of the move. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $siblingcoursecontext, ['stag']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $siblingcoursecontext, ['stag']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $siblingcoursecontext, ['stag']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $siblingcoursecontext, ['stag']); question_move_question_tags_to_new_context($questions, $newcontext); foreach ($questions as $question) { - $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id); + $tags = \core_tag_tag::get_item_tags('core_question', 'question', $question->id); // Each question should have 2 tags, 'foo' and 'ctag'. $this->assertCount(2, $tags); @@ -1142,22 +1137,22 @@ public function test_question_move_question_tags_to_new_context_course_to_course list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('course'); $question1 = $questions[0]; $question2 = $questions[1]; - $qcontext = context::instance_by_id($qcat->contextid); + $qcontext = \context::instance_by_id($qcat->contextid); // Moving up into the course category context. - $newcontext = context_coursecat::instance($category->id); + $newcontext = \context_coursecat::instance($category->id); foreach ($questions as $question) { $question->contextid = $qcat->contextid; } // Create tags in the course context. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']); question_move_question_tags_to_new_context($questions, $newcontext); foreach ($questions as $question) { - $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id); + $tags = \core_tag_tag::get_item_tags('core_question', 'question', $question->id); // All of the tags should have their context id set to the new context. foreach ($tags as $tag) { @@ -1176,27 +1171,27 @@ public function test_question_move_question_tags_to_new_context_orphaned_tag_con $question1 = $questions[0]; $question2 = $questions[1]; $othercategory = $this->getDataGenerator()->create_category(); - $qcontext = context::instance_by_id($qcat->contextid); - $newcontext = context_coursecat::instance($category->id); - $othercategorycontext = context_coursecat::instance($othercategory->id); + $qcontext = \context::instance_by_id($qcat->contextid); + $newcontext = \context_coursecat::instance($category->id); + $othercategorycontext = \context_coursecat::instance($othercategory->id); foreach ($questions as $question) { $question->contextid = $qcat->contextid; } // Create tags in the system context. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']); // Create tags in the other course category context. These should be // update to the next context id because they represent erroneous data // from a time before context id was mandatory in the tag API. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $othercategorycontext, ['bar']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $othercategorycontext, ['bar']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $othercategorycontext, ['bar']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $othercategorycontext, ['bar']); question_move_question_tags_to_new_context($questions, $newcontext); foreach ($questions as $question) { - $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id); + $tags = \core_tag_tag::get_item_tags('core_question', 'question', $question->id); // Each question should have two tags, 'foo' and 'bar'. $this->assertCount(2, $tags); @@ -1217,26 +1212,26 @@ public function test_question_move_question_tags_to_new_context_course_cat_to_ac list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); $question1 = $questions[0]; $question2 = $questions[1]; - $qcontext = context::instance_by_id($qcat->contextid); - $coursecontext = context_course::instance($course->id); - $newcontext = context_module::instance($quiz->cmid); + $qcontext = \context::instance_by_id($qcat->contextid); + $coursecontext = \context_course::instance($course->id); + $newcontext = \context_module::instance($quiz->cmid); foreach ($questions as $question) { $question->contextid = $qcat->contextid; } // Create tags in the course category context. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']); // Move the questions to the activity context which is a child context of // $coursecontext. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['ctag']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['ctag']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['ctag']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['ctag']); question_move_question_tags_to_new_context($questions, $newcontext); foreach ($questions as $question) { - $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id); + $tags = \core_tag_tag::get_item_tags('core_question', 'question', $question->id); // Each question should have 2 tags. $this->assertCount(2, $tags); @@ -1256,32 +1251,32 @@ public function test_question_move_question_tags_to_new_context_course_cat_to_ac list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); $question1 = $questions[0]; $question2 = $questions[1]; - $qcontext = context::instance_by_id($qcat->contextid); - $coursecontext = context_course::instance($course->id); - $newcontext = context_module::instance($quiz->cmid); + $qcontext = \context::instance_by_id($qcat->contextid); + $coursecontext = \context_course::instance($course->id); + $newcontext = \context_module::instance($quiz->cmid); $othercourse = $this->getDataGenerator()->create_course(); - $othercoursecontext = context_course::instance($othercourse->id); + $othercoursecontext = \context_course::instance($othercourse->id); foreach ($questions as $question) { $question->contextid = $qcat->contextid; } // Create tags in the course category context. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']); // Create tags in the course context. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['ctag']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['ctag']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['ctag']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['ctag']); // Create tags in the other course context. These should be deleted. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $othercoursecontext, ['delete']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $othercoursecontext, ['delete']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $othercoursecontext, ['delete']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $othercoursecontext, ['delete']); // Move the questions to the activity context which is a child context of // $coursecontext. question_move_question_tags_to_new_context($questions, $newcontext); foreach ($questions as $question) { - $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id); + $tags = \core_tag_tag::get_item_tags('core_question', 'question', $question->id); // Each question should have 2 tags. $this->assertCount(2, $tags); @@ -1301,21 +1296,21 @@ public function test_question_move_question_tags_to_new_context_course_to_activi list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('course'); $question1 = $questions[0]; $question2 = $questions[1]; - $qcontext = context::instance_by_id($qcat->contextid); - $newcontext = context_module::instance($quiz->cmid); + $qcontext = \context::instance_by_id($qcat->contextid); + $newcontext = \context_module::instance($quiz->cmid); foreach ($questions as $question) { $question->contextid = $qcat->contextid; } // Create tags in the course context. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']); question_move_question_tags_to_new_context($questions, $newcontext); foreach ($questions as $question) { - $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id); + $tags = \core_tag_tag::get_item_tags('core_question', 'question', $question->id); foreach ($tags as $tag) { $this->assertEquals($newcontext->id, $tag->taginstancecontextid); @@ -1331,21 +1326,21 @@ public function test_question_move_question_tags_to_new_context_activity_to_cour list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions(); $question1 = $questions[0]; $question2 = $questions[1]; - $qcontext = context::instance_by_id($qcat->contextid); - $newcontext = context_course::instance($course->id); + $qcontext = \context::instance_by_id($qcat->contextid); + $newcontext = \context_course::instance($course->id); foreach ($questions as $question) { $question->contextid = $qcat->contextid; } // Create tags in the activity context. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']); question_move_question_tags_to_new_context($questions, $newcontext); foreach ($questions as $question) { - $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id); + $tags = \core_tag_tag::get_item_tags('core_question', 'question', $question->id); foreach ($tags as $tag) { $this->assertEquals($newcontext->id, $tag->taginstancecontextid); @@ -1368,33 +1363,33 @@ public function test_question_move_question_tags_to_new_context_system_to_course $question2 = $questions[1]; $othercategory = $this->getDataGenerator()->create_category(); $othercourse = $this->getDataGenerator()->create_course(['category' => $othercategory->id]); - $qcontext = context::instance_by_id($qcat->contextid); - $newcontext = context_coursecat::instance($category->id); - $othercategorycontext = context_coursecat::instance($othercategory->id); - $coursecontext = context_course::instance($course->id); - $othercoursecontext = context_course::instance($othercourse->id); + $qcontext = \context::instance_by_id($qcat->contextid); + $newcontext = \context_coursecat::instance($category->id); + $othercategorycontext = \context_coursecat::instance($othercategory->id); + $coursecontext = \context_course::instance($course->id); + $othercoursecontext = \context_course::instance($othercourse->id); foreach ($questions as $question) { $question->contextid = $qcat->contextid; } // Create tags in the system context. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']); // Create tags in the child course context of the new context. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['bar']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['bar']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['bar']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['bar']); // Create tags in the other course context. These should be deleted when // the question moves to the new course category context because this // course belongs to a different category, which means it will no longer // have access to the question. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $othercoursecontext, ['delete']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $othercoursecontext, ['delete']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $othercoursecontext, ['delete']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $othercoursecontext, ['delete']); question_move_question_tags_to_new_context($questions, $newcontext); foreach ($questions as $question) { - $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id); + $tags = \core_tag_tag::get_item_tags('core_question', 'question', $question->id); // Each question should have two tags, 'foo' and 'bar'. $this->assertCount(2, $tags); @@ -1420,14 +1415,14 @@ public function test_question_sort_tags_includes_question_tags() { list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); $question1 = $questions[0]; $question2 = $questions[1]; - $qcontext = context::instance_by_id($qcat->contextid); + $qcontext = \context::instance_by_id($qcat->contextid); - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo', 'bar']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['baz', 'bop']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo', 'bar']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['baz', 'bop']); foreach ($questions as $question) { - $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id); - $categorycontext = context::instance_by_id($qcat->contextid); + $tags = \core_tag_tag::get_item_tags('core_question', 'question', $question->id); + $categorycontext = \context::instance_by_id($qcat->contextid); $tagobjects = question_sort_tags($tags, $categorycontext); $expectedtags = []; $actualtags = $tagobjects->tags; @@ -1459,13 +1454,13 @@ public function test_question_sort_tags_includes_question_course_tags() { list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); $question1 = $questions[0]; $question2 = $questions[1]; - $coursecontext = context_course::instance($course->id); + $coursecontext = \context_course::instance($course->id); - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['foo', 'bar']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['baz', 'bop']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['foo', 'bar']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['baz', 'bop']); foreach ($questions as $question) { - $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id); + $tags = \core_tag_tag::get_item_tags('core_question', 'question', $question->id); $tagobjects = question_sort_tags($tags, $qcat); $expectedtags = []; @@ -1498,19 +1493,19 @@ public function test_question_sort_tags_includes_multiple_courses_tags() { list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); $question1 = $questions[0]; $question2 = $questions[1]; - $coursecontext = context_course::instance($course->id); + $coursecontext = \context_course::instance($course->id); // Create a sibling course. $siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category]); - $siblingcoursecontext = context_course::instance($siblingcourse->id); + $siblingcoursecontext = \context_course::instance($siblingcourse->id); // Create course tags. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['c1']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['c1']); - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $siblingcoursecontext, ['c2']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $siblingcoursecontext, ['c2']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['c1']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['c1']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $siblingcoursecontext, ['c2']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $siblingcoursecontext, ['c2']); foreach ($questions as $question) { - $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id); + $tags = \core_tag_tag::get_item_tags('core_question', 'question', $question->id); $tagobjects = question_sort_tags($tags, $qcat); $this->assertCount(2, $tagobjects->coursetagobjects); @@ -1533,20 +1528,20 @@ public function test_question_sort_tags_includes_filter_course_tags() { list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); $question1 = $questions[0]; $question2 = $questions[1]; - $coursecontext = context_course::instance($course->id); + $coursecontext = \context_course::instance($course->id); // Create a sibling course. $siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category]); - $siblingcoursecontext = context_course::instance($siblingcourse->id); + $siblingcoursecontext = \context_course::instance($siblingcourse->id); // Create course tags. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['foo']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['bar']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['foo']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['bar']); // Create sibling course tags. These should be filtered out. - core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $siblingcoursecontext, ['filtered1']); - core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $siblingcoursecontext, ['filtered2']); + \core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $siblingcoursecontext, ['filtered1']); + \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $siblingcoursecontext, ['filtered2']); foreach ($questions as $question) { - $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id); + $tags = \core_tag_tag::get_item_tags('core_question', 'question', $question->id); $tagobjects = question_sort_tags($tags, $qcat, [$course]); foreach ($tagobjects->coursetagobjects as $tag) { @@ -1690,7 +1685,7 @@ public function test_question_has_capability_on_broken_question() { $questiongenerator = $generator->get_plugin_generator('core_question'); $category = $generator->create_category(); - $context = context_coursecat::instance($category->id); + $context = \context_coursecat::instance($category->id); $questioncat = $questiongenerator->create_question_category([ 'contextid' => $context->id, ]); @@ -1719,7 +1714,7 @@ public function test_question_has_capability_on_broken_question() { * @param bool $isowner Whether the user to create the question should be the owner or not. * @param bool $expect The expected result. */ - public function test_question_has_capability_on_using_stdclass($capabilities, $capability, $isowner, $expect) { + public function test_question_has_capability_on_using_stdClass($capabilities, $capability, $isowner, $expect) { $this->resetAfterTest(); // Create the test data. @@ -1727,7 +1722,7 @@ public function test_question_has_capability_on_using_stdclass($capabilities, $c $otheruser = $this->getDataGenerator()->create_user(); $roleid = $this->getDataGenerator()->create_role(); $category = $this->getDataGenerator()->create_category(); - $context = context_coursecat::instance($category->id); + $context = \context_coursecat::instance($category->id); // Assign the user to the role. role_assign($roleid, $user->id, $context->id); @@ -1773,7 +1768,7 @@ public function test_question_has_capability_on_using_question_definition($capab $otheruser = $generator->create_user(); $roleid = $generator->create_role(); $category = $generator->create_category(); - $context = context_coursecat::instance($category->id); + $context = \context_coursecat::instance($category->id); $questioncat = $questiongenerator->create_question_category([ 'contextid' => $context->id, ]); @@ -1796,7 +1791,7 @@ public function test_question_has_capability_on_using_question_definition($capab // The question generator does not support setting of the createdby for some reason. $question->createdby = ($isowner) ? $user->id : $otheruser->id; - $fromform = test_question_maker::get_question_form_data($qtype, null); + $fromform = \test_question_maker::get_question_form_data($qtype, null); $fromform = (object) $generator->combine_defaults_and_record((array) $fromform, $overrides); question_bank::get_qtype($qtype)->save_question($question, $fromform); @@ -1824,7 +1819,7 @@ public function test_question_has_capability_on_using_question_id($capabilities, $otheruser = $generator->create_user(); $roleid = $generator->create_role(); $category = $generator->create_category(); - $context = context_coursecat::instance($category->id); + $context = \context_coursecat::instance($category->id); $questioncat = $questiongenerator->create_question_category([ 'contextid' => $context->id, ]); @@ -1847,7 +1842,7 @@ public function test_question_has_capability_on_using_question_id($capabilities, // The question generator does not support setting of the createdby for some reason. $question->createdby = ($isowner) ? $user->id : $otheruser->id; - $fromform = test_question_maker::get_question_form_data($qtype, null); + $fromform = \test_question_maker::get_question_form_data($qtype, null); $fromform = (object) $generator->combine_defaults_and_record((array) $fromform, $overrides); question_bank::get_qtype($qtype)->save_question($question, $fromform); @@ -1875,7 +1870,7 @@ public function test_question_has_capability_on_using_question_string_id($capabi $otheruser = $generator->create_user(); $roleid = $generator->create_role(); $category = $generator->create_category(); - $context = context_coursecat::instance($category->id); + $context = \context_coursecat::instance($category->id); $questioncat = $questiongenerator->create_question_category([ 'contextid' => $context->id, ]); @@ -1898,7 +1893,7 @@ public function test_question_has_capability_on_using_question_string_id($capabi // The question generator does not support setting of the createdby for some reason. $question->createdby = ($isowner) ? $user->id : $otheruser->id; - $fromform = test_question_maker::get_question_form_data($qtype, null); + $fromform = \test_question_maker::get_question_form_data($qtype, null); $fromform = (object) $generator->combine_defaults_and_record((array) $fromform, $overrides); question_bank::get_qtype($qtype)->save_question($question, $fromform); @@ -1926,13 +1921,13 @@ public function test_question_has_capability_on_using_moved_question($capabiliti $otheruser = $generator->create_user(); $roleid = $generator->create_role(); $category = $generator->create_category(); - $context = context_coursecat::instance($category->id); + $context = \context_coursecat::instance($category->id); $questioncat = $questiongenerator->create_question_category([ 'contextid' => $context->id, ]); $newcategory = $generator->create_category(); - $newcontext = context_coursecat::instance($newcategory->id); + $newcontext = \context_coursecat::instance($newcategory->id); $newquestioncat = $questiongenerator->create_question_category([ 'contextid' => $newcontext->id, ]); @@ -1955,7 +1950,7 @@ public function test_question_has_capability_on_using_moved_question($capabiliti // The question generator does not support setting of the createdby for some reason. $question->createdby = ($isowner) ? $user->id : $otheruser->id; - $fromform = test_question_maker::get_question_form_data($qtype, null); + $fromform = \test_question_maker::get_question_form_data($qtype, null); $fromform = (object) $generator->combine_defaults_and_record((array) $fromform, $overrides); question_bank::get_qtype($qtype)->save_question($question, $fromform); @@ -1987,7 +1982,7 @@ public function test_question_has_capability_on_using_question($capabilities, $c $otheruser = $generator->create_user(); $roleid = $generator->create_role(); $category = $generator->create_category(); - $context = context_coursecat::instance($category->id); + $context = \context_coursecat::instance($category->id); $questioncat = $questiongenerator->create_question_category([ 'contextid' => $context->id, ]); @@ -2024,7 +2019,7 @@ public function test_question_has_capability_on_wrong_param_type() { $user = $generator->create_user(); $category = $generator->create_category(); - $context = context_coursecat::instance($category->id); + $context = \context_coursecat::instance($category->id); $questioncat = $questiongenerator->create_question_category([ 'contextid' => $context->id, ]); @@ -2055,7 +2050,7 @@ public function test_question_categorylist_parents() { $generator = $this->getDataGenerator(); $questiongenerator = $generator->get_plugin_generator('core_question'); $category = $generator->create_category(); - $context = context_coursecat::instance($category->id); + $context = \context_coursecat::instance($category->id); // Create a top category. $cat0 = question_get_top_category($context->id, true); // Add sub-categories. @@ -2077,23 +2072,23 @@ public function test_question_get_export_single_question_url() { // Create a question in each place. $questiongenerator = $generator->get_plugin_generator('core_question'); - $courseqcat = $questiongenerator->create_question_category(['contextid' => context_course::instance($course->id)->id]); + $courseqcat = $questiongenerator->create_question_category(['contextid' => \context_course::instance($course->id)->id]); $courseq = $questiongenerator->create_question('truefalse', null, ['category' => $courseqcat->id]); - $quizqcat = $questiongenerator->create_question_category(['contextid' => context_module::instance($quiz->cmid)->id]); + $quizqcat = $questiongenerator->create_question_category(['contextid' => \context_module::instance($quiz->cmid)->id]); $quizq = $questiongenerator->create_question('truefalse', null, ['category' => $quizqcat->id]); $systemqcat = $questiongenerator->create_question_category(); $systemq = $questiongenerator->create_question('truefalse', null, ['category' => $systemqcat->id]); // Verify some URLs. - $this->assertEquals(new moodle_url('/question/exportone.php', + $this->assertEquals(new \moodle_url('/question/exportone.php', ['id' => $courseq->id, 'courseid' => $course->id, 'sesskey' => sesskey()]), question_get_export_single_question_url(question_bank::load_question_data($courseq->id))); - $this->assertEquals(new moodle_url('/question/exportone.php', + $this->assertEquals(new \moodle_url('/question/exportone.php', ['id' => $quizq->id, 'cmid' => $quiz->cmid, 'sesskey' => sesskey()]), question_get_export_single_question_url(question_bank::load_question($quizq->id))); - $this->assertEquals(new moodle_url('/question/exportone.php', + $this->assertEquals(new \moodle_url('/question/exportone.php', ['id' => $systemq->id, 'courseid' => SITEID, 'sesskey' => sesskey()]), question_get_export_single_question_url(question_bank::load_question($systemq->id))); } diff --git a/lib/tests/weblib_format_text_test.php b/lib/tests/weblib_format_text_test.php index 3468b617a7dc4..6aab808bedcda 100644 --- a/lib/tests/weblib_format_text_test.php +++ b/lib/tests/weblib_format_text_test.php @@ -14,6 +14,8 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +namespace core; + /** * Unit tests for format_text defined in weblib.php. * @@ -22,17 +24,7 @@ * @copyright 2015 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU Public License */ - -defined('MOODLE_INTERNAL') || die(); - - -/** - * Unit tests for format_text defined in weblib.php. - * - * @copyright 2015 The Open University - * @license http://www.gnu.org/copyleft/gpl.html GNU Public License - */ -class core_weblib_format_text_testcase extends advanced_testcase { +class weblib_format_text_test extends \advanced_testcase { public function test_format_text_format_html() { $this->resetAfterTest(); diff --git a/lib/tests/xhprof_test.php b/lib/tests/xhprof_test.php index 0a546ebc71014..dac3f2168becc 100644 --- a/lib/tests/xhprof_test.php +++ b/lib/tests/xhprof_test.php @@ -14,23 +14,17 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Unit tests for xhprof. - * - * @package core - * @copyright 2019 Brendan Heywood - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5) - */ - -defined('MOODLE_INTERNAL') || die(); +namespace core; /** * Unit tests for the xhprof class. * + * @package core + * @category test * @copyright 2019 Brendan Heywood * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class core_xhprof_testcase extends advanced_testcase { +class xhprof_test extends \advanced_testcase { /** * Data provider for string matches diff --git a/lib/tests/xhtml_container_stack_test.php b/lib/tests/xhtml_container_stack_test.php index 578a076c03763..ee1373195ef95 100644 --- a/lib/tests/xhtml_container_stack_test.php +++ b/lib/tests/xhtml_container_stack_test.php @@ -14,30 +14,26 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Unit tests for xhtml stack. - * - * @package core - * @category phpunit - * @copyright 2009 Tim Hunt - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5) - */ +namespace core; + +use xhtml_container_stack; defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->libdir . '/outputlib.php'); - /** * Unit tests for the xhtml_container_stack class. * * These tests assume that developer debug mode is on which is enforced by our phpunit integration. * + * @package core + * @category test * @copyright 2009 Tim Hunt * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class core_xhtml_container_stack_testcase extends advanced_testcase { +class xhtml_container_stack_test extends \advanced_testcase { public function test_push_then_pop() { // Set up. $stack = new xhtml_container_stack(); diff --git a/lib/tests/xmlize_test.php b/lib/tests/xmlize_test.php index 554f165275ffd..cfdfb33be72c7 100644 --- a/lib/tests/xmlize_test.php +++ b/lib/tests/xmlize_test.php @@ -14,14 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Test xmlize xml import. - * - * @package core - * @category test - * @copyright 2017 Kilian Singer {@link http://quantumtechnology.info} - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace core; defined('MOODLE_INTERNAL') || die(); @@ -31,10 +24,12 @@ /** * This test compares library against the original xmlize XML importer. * + * @package core + * @category test * @copyright 2017 Kilian Singer {@link http://quantumtechnology.info} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class core_xmlize_testcase extends basic_testcase { +class xmlize_test extends \basic_testcase { /** * Test an XML import using a valid XML file. * diff --git a/mod/quiz/accessrule/seb/tests/access_manager_test.php b/mod/quiz/accessrule/seb/tests/access_manager_test.php index 262ac9a65d785..26c289d6af3dd 100644 --- a/mod/quiz/accessrule/seb/tests/access_manager_test.php +++ b/mod/quiz/accessrule/seb/tests/access_manager_test.php @@ -14,18 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * PHPUnit tests for the access manager. - * - * @package quizaccess_seb - * @author Andrew Madden - * @copyright 2019 Catalyst IT - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -use quizaccess_seb\access_manager; -use quizaccess_seb\quiz_settings; -use quizaccess_seb\settings_provider; +namespace quizaccess_seb; defined('MOODLE_INTERNAL') || die(); @@ -34,11 +23,13 @@ /** * PHPUnit tests for the access manager. * - * @copyright 2020 Catalyst IT + * @package quizaccess_seb + * @author Andrew Madden + * @copyright 2020 Catalyst IT * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class quizacces_seb_access_manager_testcase extends advanced_testcase { - use quizaccess_seb_test_helper_trait; +class access_manager_test extends \advanced_testcase { + use \quizaccess_seb_test_helper_trait; /** * Called before every test. @@ -98,7 +89,7 @@ public function test_user_can_bypass_seb_check() { $this->setUser($user); // Set the bypass SEB check capability to $USER. - $this->assign_user_capability('quizaccess/seb:bypassseb', context_module::instance($this->quiz->cmid)->id); + $this->assign_user_capability('quizaccess/seb:bypassseb', \context_module::instance($this->quiz->cmid)->id); $accessmanager = $this->get_access_manager(); $this->assertTrue($accessmanager->can_bypass_seb()); diff --git a/mod/quiz/accessrule/seb/tests/backup_restore_test.php b/mod/quiz/accessrule/seb/tests/backup_restore_test.php index 049ea298d0e52..aa8abe3ed9587 100644 --- a/mod/quiz/accessrule/seb/tests/backup_restore_test.php +++ b/mod/quiz/accessrule/seb/tests/backup_restore_test.php @@ -14,15 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * PHPUnit tests for backup and restore functionality. - * - * @package quizaccess_seb - * @author Dmitrii Metelkin - * @copyright 2020 Catalyst IT - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - +namespace quizaccess_seb; defined('MOODLE_INTERNAL') || die(); @@ -31,14 +23,16 @@ /** * PHPUnit tests for backup and restore functionality. * - * @copyright 2020 Catalyst IT + * @package quizaccess_seb + * @author Dmitrii Metelkin + * @copyright 2020 Catalyst IT * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class quizaccess_seb_backup_restore_testcase extends advanced_testcase { - use quizaccess_seb_test_helper_trait; +class backup_restore_test extends \advanced_testcase { + use \quizaccess_seb_test_helper_trait; - /** @var \quizaccess_seb\template $template A test template. */ + /** @var template $template A test template. */ protected $template; /** @@ -60,12 +54,12 @@ public function setUp(): void { /** * A helper method to create a quiz with template usage of SEB. * - * @return \quizaccess_seb\quiz_settings + * @return quiz_settings */ protected function create_quiz_with_template() { - $this->quiz = $this->create_test_quiz($this->course, \quizaccess_seb\settings_provider::USE_SEB_CONFIG_MANUALLY); - $quizsettings = \quizaccess_seb\quiz_settings::get_record(['quizid' => $this->quiz->id]); - $quizsettings->set('requiresafeexambrowser', \quizaccess_seb\settings_provider::USE_SEB_TEMPLATE); + $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY); + $quizsettings = quiz_settings::get_record(['quizid' => $this->quiz->id]); + $quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_TEMPLATE); $quizsettings->set('templateid', $this->template->get('id')); $quizsettings->save(); @@ -95,8 +89,8 @@ protected function backup_quiz() { $backupid = 'test-seb-backup-restore'; - $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->quiz->coursemodule, backup::FORMAT_MOODLE, - backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->user->id); + $bc = new \backup_controller(\backup::TYPE_1ACTIVITY, $this->quiz->coursemodule, \backup::FORMAT_MOODLE, + \backup::INTERACTIVE_NO, \backup::MODE_GENERAL, $this->user->id); $bc->execute_plan(); $results = $bc->get_results(); @@ -115,8 +109,8 @@ protected function backup_quiz() { * @param string $backupid Backup ID to restore. */ protected function restore_quiz($backupid) { - $rc = new restore_controller($backupid, $this->course->id, - backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->user->id, backup::TARGET_CURRENT_ADDING); + $rc = new \restore_controller($backupid, $this->course->id, + \backup::INTERACTIVE_NO, \backup::MODE_GENERAL, $this->user->id, \backup::TARGET_CURRENT_ADDING); $this->assertTrue($rc->execute_precheck()); $rc->execute_plan(); $rc->destroy(); @@ -134,11 +128,11 @@ protected function change_site() { * * @param cm_info $newcm Restored course_module object. */ - protected function validate_backup_restore(cm_info $newcm) { - $this->assertEquals(2, quizaccess_seb\quiz_settings::count_records()); - $actual = \quizaccess_seb\quiz_settings::get_record(['quizid' => $newcm->instance]); + protected function validate_backup_restore(\cm_info $newcm) { + $this->assertEquals(2, quiz_settings::count_records()); + $actual = quiz_settings::get_record(['quizid' => $newcm->instance]); - $expected = \quizaccess_seb\quiz_settings::get_record(['quizid' => $this->quiz->id]); + $expected = quiz_settings::get_record(['quizid' => $this->quiz->id]); $this->assertEquals($expected->get('templateid'), $actual->get('templateid')); $this->assertEquals($expected->get('requiresafeexambrowser'), $actual->get('requiresafeexambrowser')); $this->assertEquals($expected->get('showsebdownloadlink'), $actual->get('showsebdownloadlink')); @@ -147,7 +141,7 @@ protected function validate_backup_restore(cm_info $newcm) { $this->assertEquals($expected->get('allowedbrowserexamkeys'), $actual->get('allowedbrowserexamkeys')); // Validate specific SEB config settings. - foreach (\quizaccess_seb\settings_provider::get_seb_config_elements() as $name => $notused) { + foreach (settings_provider::get_seb_config_elements() as $name => $notused) { $name = preg_replace("/^seb_/", "", $name); $this->assertEquals($expected->get($name), $actual->get($name)); } @@ -157,25 +151,25 @@ protected function validate_backup_restore(cm_info $newcm) { * Test backup and restore when no seb. */ public function test_backup_restore_no_seb() { - $this->quiz = $this->create_test_quiz($this->course, \quizaccess_seb\settings_provider::USE_SEB_NO); - $this->assertEquals(0, quizaccess_seb\quiz_settings::count_records()); + $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_NO); + $this->assertEquals(0, quiz_settings::count_records()); $this->backup_and_restore_quiz(); - $this->assertEquals(0, quizaccess_seb\quiz_settings::count_records()); + $this->assertEquals(0, quiz_settings::count_records()); } /** * Test backup and restore when manually configured. */ public function test_backup_restore_manual_config() { - $this->quiz = $this->create_test_quiz($this->course, \quizaccess_seb\settings_provider::USE_SEB_CONFIG_MANUALLY); + $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY); - $expected = \quizaccess_seb\quiz_settings::get_record(['quizid' => $this->quiz->id]); + $expected = quiz_settings::get_record(['quizid' => $this->quiz->id]); $expected->set('showsebdownloadlink', 0); $expected->set('quitpassword', '123'); $expected->save(); - $this->assertEquals(1, quizaccess_seb\quiz_settings::count_records()); + $this->assertEquals(1, quiz_settings::count_records()); $newcm = $this->backup_and_restore_quiz(); $this->validate_backup_restore($newcm); @@ -185,15 +179,15 @@ public function test_backup_restore_manual_config() { * Test backup and restore when using template. */ public function test_backup_restore_template_config() { - $this->quiz = $this->create_test_quiz($this->course, \quizaccess_seb\settings_provider::USE_SEB_CONFIG_MANUALLY); + $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY); - $expected = \quizaccess_seb\quiz_settings::get_record(['quizid' => $this->quiz->id]); + $expected = quiz_settings::get_record(['quizid' => $this->quiz->id]); $template = $this->create_template(); - $expected->set('requiresafeexambrowser', \quizaccess_seb\settings_provider::USE_SEB_TEMPLATE); + $expected->set('requiresafeexambrowser', settings_provider::USE_SEB_TEMPLATE); $expected->set('templateid', $template->get('id')); $expected->save(); - $this->assertEquals(1, quizaccess_seb\quiz_settings::count_records()); + $this->assertEquals(1, quiz_settings::count_records()); $newcm = $this->backup_and_restore_quiz(); $this->validate_backup_restore($newcm); @@ -203,21 +197,21 @@ public function test_backup_restore_template_config() { * Test backup and restore when using uploaded file. */ public function test_backup_restore_uploaded_config() { - $this->quiz = $this->create_test_quiz($this->course, \quizaccess_seb\settings_provider::USE_SEB_CONFIG_MANUALLY); + $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY); - $expected = \quizaccess_seb\quiz_settings::get_record(['quizid' => $this->quiz->id]); - $expected->set('requiresafeexambrowser', \quizaccess_seb\settings_provider::USE_SEB_UPLOAD_CONFIG); + $expected = quiz_settings::get_record(['quizid' => $this->quiz->id]); + $expected->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG); $xml = file_get_contents(__DIR__ . '/fixtures/unencrypted.seb'); $this->create_module_test_file($xml, $this->quiz->cmid); $expected->save(); - $this->assertEquals(1, quizaccess_seb\quiz_settings::count_records()); + $this->assertEquals(1, quiz_settings::count_records()); $newcm = $this->backup_and_restore_quiz(); $this->validate_backup_restore($newcm); - $expectedfile = \quizaccess_seb\settings_provider::get_module_context_sebconfig_file($this->quiz->cmid); - $actualfile = \quizaccess_seb\settings_provider::get_module_context_sebconfig_file($newcm->id); + $expectedfile = settings_provider::get_module_context_sebconfig_file($this->quiz->cmid); + $actualfile = settings_provider::get_module_context_sebconfig_file($newcm->id); $this->assertEquals($expectedfile->get_content(), $actualfile->get_content()); } @@ -230,15 +224,15 @@ public function test_restore_template_to_a_different_site_when_the_same_template $this->create_quiz_with_template(); $backupid = $this->backup_quiz(); - $this->assertEquals(1, quizaccess_seb\quiz_settings::count_records()); - $this->assertEquals(1, quizaccess_seb\template::count_records()); + $this->assertEquals(1, quiz_settings::count_records()); + $this->assertEquals(1, template::count_records()); $this->change_site(); $this->restore_quiz($backupid); // Should see additional setting record, but no new template record. - $this->assertEquals(2, quizaccess_seb\quiz_settings::count_records()); - $this->assertEquals(1, quizaccess_seb\template::count_records()); + $this->assertEquals(2, quiz_settings::count_records()); + $this->assertEquals(1, template::count_records()); } /** @@ -249,8 +243,8 @@ public function test_restore_template_to_a_different_site_when_the_same_content_ $this->create_quiz_with_template(); $backupid = $this->backup_quiz(); - $this->assertEquals(1, quizaccess_seb\quiz_settings::count_records()); - $this->assertEquals(1, quizaccess_seb\template::count_records()); + $this->assertEquals(1, quiz_settings::count_records()); + $this->assertEquals(1, template::count_records()); $this->template->set('name', 'New name for template'); $this->template->save(); @@ -259,8 +253,8 @@ public function test_restore_template_to_a_different_site_when_the_same_content_ $this->restore_quiz($backupid); // Should see additional setting record, and new template record. - $this->assertEquals(2, quizaccess_seb\quiz_settings::count_records()); - $this->assertEquals(2, quizaccess_seb\template::count_records()); + $this->assertEquals(2, quiz_settings::count_records()); + $this->assertEquals(2, template::count_records()); } /** @@ -273,8 +267,8 @@ public function test_restore_template_to_a_different_site_when_the_same_name_but $this->create_quiz_with_template(); $backupid = $this->backup_quiz(); - $this->assertEquals(1, quizaccess_seb\quiz_settings::count_records()); - $this->assertEquals(1, quizaccess_seb\template::count_records()); + $this->assertEquals(1, quiz_settings::count_records()); + $this->assertEquals(1, template::count_records()); $newxml = file_get_contents($CFG->dirroot . '/mod/quiz/accessrule/seb/tests/fixtures/simpleunencrypted.seb'); $this->template->set('content', $newxml); @@ -284,8 +278,8 @@ public function test_restore_template_to_a_different_site_when_the_same_name_but $this->restore_quiz($backupid); // Should see additional setting record, and new template record. - $this->assertEquals(2, quizaccess_seb\quiz_settings::count_records()); - $this->assertEquals(2, quizaccess_seb\template::count_records()); + $this->assertEquals(2, quiz_settings::count_records()); + $this->assertEquals(2, template::count_records()); } } diff --git a/mod/quiz/accessrule/seb/tests/config_key_test.php b/mod/quiz/accessrule/seb/tests/config_key_test.php index 6fbe2b92be9fe..cf21162b42a30 100644 --- a/mod/quiz/accessrule/seb/tests/config_key_test.php +++ b/mod/quiz/accessrule/seb/tests/config_key_test.php @@ -14,32 +14,23 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * PHPUnit Tests for config_key class. - * - * @package quizaccess_seb - * @author Andrew Madden - * @copyright 2019 Catalyst IT - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -use quizaccess_seb\config_key; - -defined('MOODLE_INTERNAL') || die(); +namespace quizaccess_seb; /** * PHPUnit Tests for config_key class. * - * @copyright 2020 Catalyst IT + * @package quizaccess_seb + * @author Andrew Madden + * @copyright 2020 Catalyst IT * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class quizaccess_seb_config_key_testcase extends advanced_testcase { +class config_key_test extends \advanced_testcase { /** * Test that trying to generate the hash key with bad xml will result in an error. */ public function test_config_key_not_generated_with_bad_xml() { - $this->expectException(invalid_parameter_exception::class); + $this->expectException(\invalid_parameter_exception::class); $this->expectExceptionMessage("Invalid a PList XML string, representing SEB config"); config_key::generate(". -/** - * PHPUnit tests for hideif_rule. - * - * @package quizaccess_seb - * @author Dmitrii Metelkin - * @copyright 2019 Catalyst IT - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -use quizaccess_seb\hideif_rule; - -defined('MOODLE_INTERNAL') || die(); +namespace quizaccess_seb; /** * PHPUnit tests for hideif_rule. * - * @copyright 2020 Catalyst IT + * @package quizaccess_seb + * @author Dmitrii Metelkin + * @copyright 2020 Catalyst IT * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class quizaccess_seb_hideif_rule_testcase extends advanced_testcase { +class hideif_rule_test extends \advanced_testcase { /** * Test that can get rule data. diff --git a/mod/quiz/accessrule/seb/tests/link_generator_test.php b/mod/quiz/accessrule/seb/tests/link_generator_test.php index 1cb608ce7b43c..57ff94f2c8136 100644 --- a/mod/quiz/accessrule/seb/tests/link_generator_test.php +++ b/mod/quiz/accessrule/seb/tests/link_generator_test.php @@ -14,26 +14,17 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * PHPUnit tests for link_generator. - * - * @package quizaccess_seb - * @author Andrew Madden - * @copyright 2019 Catalyst IT - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -use quizaccess_seb\link_generator; - -defined('MOODLE_INTERNAL') || die(); +namespace quizaccess_seb; /** * PHPUnit tests for link_generator. * - * @copyright 2020 Catalyst IT + * @package quizaccess_seb + * @author Andrew Madden + * @copyright 2020 Catalyst IT * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class quizaccess_seb_link_generator_testcase extends advanced_testcase { +class link_generator_test extends \advanced_testcase { /** * Called before every test. @@ -95,7 +86,7 @@ public function test_sebs_link_generated() { * Test that link_generator can't not be instantiated with fake course module. */ public function test_course_module_does_not_exist() { - $this->expectException(dml_exception::class); + $this->expectException(\dml_exception::class); $this->expectExceptionMessageMatches("/^Can't find data record in database.*/"); $generator = link_generator::get_link(123456, false); } diff --git a/mod/quiz/accessrule/seb/tests/property_list_test.php b/mod/quiz/accessrule/seb/tests/property_list_test.php index 7337a79ee4158..50eef4998694c 100644 --- a/mod/quiz/accessrule/seb/tests/property_list_test.php +++ b/mod/quiz/accessrule/seb/tests/property_list_test.php @@ -14,26 +14,17 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * PHPUnit for property_list class. - * - * @package quizaccess_seb - * @author Andrew Madden - * @copyright 2019 Catalyst IT - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -use quizaccess_seb\property_list; - -defined('MOODLE_INTERNAL') || die(); +namespace quizaccess_seb; /** * PHPUnit for property_list class. * - * @copyright 2020 Catalyst IT + * @package quizaccess_seb + * @author Andrew Madden + * @copyright 2020 Catalyst IT * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class quizaccess_seb_property_list_testcase extends advanced_testcase { +class property_list_test extends \advanced_testcase { /** * Test that an empty PList with a root dictionary is created. @@ -144,7 +135,7 @@ public function test_updating_element_value_with_bad_data(string $xml, string $k . $this->get_plist_xml_footer(); $plist = new property_list($xml); - $this->expectException(invalid_parameter_exception::class); + $this->expectException(\invalid_parameter_exception::class); $this->expectExceptionMessage($exceptionmessage); $plist->update_element_value($key, $value); @@ -181,7 +172,7 @@ public function test_updating_element_array_if_dictionary_with_bad_data() { . $this->get_plist_xml_footer(); $plist = new property_list($xml); - $this->expectException(invalid_parameter_exception::class); + $this->expectException(\invalid_parameter_exception::class); $this->expectExceptionMessage('New array must only contain CFType objects.'); $plist->update_element_array('testDict', [false]); @@ -281,7 +272,7 @@ public function test_set_or_update_value() { $this->assertEquals(42, $plist->get_element_value('number')); // Type exception. - $this->expectException(TypeError::class); + $this->expectException(\TypeError::class); $plist->set_or_update_value('someKey', 'We really need to pass in CFTypes here'); } diff --git a/mod/quiz/accessrule/seb/tests/quiz_settings_test.php b/mod/quiz/accessrule/seb/tests/quiz_settings_test.php index 705cb5831c995..88b1d3c86be4d 100644 --- a/mod/quiz/accessrule/seb/tests/quiz_settings_test.php +++ b/mod/quiz/accessrule/seb/tests/quiz_settings_test.php @@ -14,17 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * PHPUnit tests for quiz_settings class. - * - * @package quizaccess_seb - * @author Andrew Madden - * @copyright 2019 Catalyst IT - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -use quizaccess_seb\quiz_settings; -use quizaccess_seb\settings_provider; +namespace quizaccess_seb; defined('MOODLE_INTERNAL') || die(); @@ -33,11 +23,13 @@ /** * PHPUnit tests for quiz_settings class. * - * @copyright 2020 Catalyst IT + * @package quizaccess_seb + * @author Andrew Madden + * @copyright 2020 Catalyst IT * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class quizaccess_seb_quiz_settings_testcase extends advanced_testcase { - use quizaccess_seb_test_helper_trait; +class quiz_settings_test extends \advanced_testcase { + use \quizaccess_seb_test_helper_trait; /** @var context_module $context Test context. */ protected $context; @@ -59,8 +51,8 @@ public function setUp(): void { 'course' => $this->course->id, 'seb_requiresafeexambrowser' => settings_provider::USE_SEB_CONFIG_MANUALLY, ]); - $this->context = context_module::instance($this->quiz->cmid); - $this->url = new moodle_url("/mod/quiz/view.php", ['id' => $this->quiz->cmid]); + $this->context = \context_module::instance($this->quiz->cmid); + $this->url = new \moodle_url("/mod/quiz/view.php", ['id' => $this->quiz->cmid]); } /** @@ -176,12 +168,12 @@ public function test_config_key_is_updated_from_quiz_settings() { /** * Test that different URL filter expressions are turned into config XML. * - * @param stdClass $settings Quiz settings + * @param \stdClass $settings Quiz settings * @param string $expectedxml SEB Config XML. * * @dataProvider filter_rules_provider */ - public function test_filter_rules_added_to_config(stdClass $settings, string $expectedxml) { + public function test_filter_rules_added_to_config(\stdClass $settings, string $expectedxml) { $quizsettings = new quiz_settings(0, $settings); $config = $quizsettings->get_config(); $this->assertEquals($expectedxml, $config); @@ -217,7 +209,7 @@ public function test_browser_exam_keys_validation_errors($bek, $expectederrorstr * Test that uploaded seb file gets converted to config string. */ public function test_config_file_uploaded_converted_to_config() { - $url = new moodle_url("/mod/quiz/view.php", ['id' => $this->quiz->cmid]); + $url = new \moodle_url("/mod/quiz/view.php", ['id' => $this->quiz->cmid]); $xml = "\n" . "\n" . "hashedQuitPasswordhashedpassword" @@ -238,7 +230,7 @@ public function test_no_config_file_uploaded() { $quizsettings = quiz_settings::get_record(['quizid' => $this->quiz->id]); $quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG); $cmid = $quizsettings->get('cmid'); - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); $this->expectExceptionMessage("No uploaded SEB config file could be found for quiz with cmid: {$cmid}"); $quizsettings->get_config(); } diff --git a/mod/quiz/accessrule/seb/tests/settings_provider_test.php b/mod/quiz/accessrule/seb/tests/settings_provider_test.php index 1e328b7d85da2..7f63c36393d41 100644 --- a/mod/quiz/accessrule/seb/tests/settings_provider_test.php +++ b/mod/quiz/accessrule/seb/tests/settings_provider_test.php @@ -14,17 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * PHPUnit tests for settings_provider. - * - * @package quizaccess_seb - * @author Andrew Madden - * @copyright 2019 Catalyst IT - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -use quizaccess_seb\quiz_settings; -use quizaccess_seb\settings_provider; +namespace quizaccess_seb; defined('MOODLE_INTERNAL') || die(); @@ -33,11 +23,13 @@ /** * PHPUnit tests for settings_provider. * - * @copyright 2020 Catalyst IT + * @package quizaccess_seb + * @author Andrew Madden + * @copyright 2020 Catalyst IT * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class quizaccess_seb_settings_provider_testcase extends advanced_testcase { - use quizaccess_seb_test_helper_trait; +class settings_provider_test extends \advanced_testcase { + use \quizaccess_seb_test_helper_trait; /** * Mocked quiz form instance. @@ -74,7 +66,7 @@ class quizaccess_seb_settings_provider_testcase extends advanced_testcase { */ protected function set_up_form_mocks() { if (empty($this->context)) { - $this->context = context_module::instance($this->quiz->cmid); + $this->context = \context_module::instance($this->quiz->cmid); } $this->mockedquizform = $this->createMock('mod_quiz_mod_form'); @@ -166,13 +158,13 @@ public function test_setting_types_are_part_of_file_types() { /** * Helper method to assert hide if element. - * @param \quizaccess_seb\hideif_rule $hideif Rule to check. + * @param hideif_rule $hideif Rule to check. * @param string $element Expected element. * @param string $dependantname Expected dependant element name. * @param string $condition Expected condition. * @param mixed $value Expected value. */ - protected function assert_hide_if(\quizaccess_seb\hideif_rule $hideif, $element, $dependantname, $condition, $value) { + protected function assert_hide_if(hideif_rule $hideif, $element, $dependantname, $condition, $value) { $this->assertEquals($element, $hideif->get_element()); $this->assertEquals($dependantname, $hideif->get_dependantname()); $this->assertEquals($condition, $hideif->get_condition()); @@ -568,7 +560,7 @@ public function test_setting_hideifs_are_part_of_file_types() { * Test that exception thrown if we try to build capability name from the incorrect setting name. */ public function test_build_setting_capability_name_incorrect_setting() { - $this->expectException(coding_exception::class); + $this->expectException(\coding_exception::class); $this->expectExceptionMessage('Incorrect SEB quiz setting broken'); $broken = settings_provider::build_setting_capability_name('broken'); @@ -596,7 +588,7 @@ public function test_can_manage_seb_config_setting() { $this->course = $this->getDataGenerator()->create_course(); $this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]); - $this->context = context_module::instance($this->quiz->cmid); + $this->context = \context_module::instance($this->quiz->cmid); $this->set_up_user_and_role(); @@ -640,7 +632,7 @@ public function test_get_requiresafeexambrowser_options($settingcapability) { $this->course = $this->getDataGenerator()->create_course(); $this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]); - $this->context = context_module::instance($this->quiz->cmid); + $this->context = \context_module::instance($this->quiz->cmid); $options = settings_provider::get_requiresafeexambrowser_options($this->context); @@ -708,7 +700,7 @@ public function test_get_requiresafeexambrowser_options_with_conflicting_permiss $this->course = $this->getDataGenerator()->create_course(); $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY); - $this->context = context_module::instance($this->quiz->cmid); + $this->context = \context_module::instance($this->quiz->cmid); $template = $this->create_template(); @@ -735,7 +727,7 @@ public function test_form_elements_are_frozen_if_conflicting_permissions() { $this->course = $this->getDataGenerator()->create_course(); $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY); - $this->context = context_module::instance($this->quiz->cmid); + $this->context = \context_module::instance($this->quiz->cmid); // Setup conflicting permissions. $template = $this->create_template(); @@ -768,7 +760,7 @@ public function test_form_elements_are_locked_when_quiz_attempted_manual() { $this->course = $this->getDataGenerator()->create_course(); $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY); - $this->context = context_module::instance($this->quiz->cmid); + $this->context = \context_module::instance($this->quiz->cmid); $user = $this->getDataGenerator()->create_user(); $this->attempt_quiz($this->quiz, $user); @@ -798,7 +790,7 @@ public function test_form_elements_are_locked_when_quiz_attempted_template() { $this->course = $this->getDataGenerator()->create_course(); $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY); - $this->context = context_module::instance($this->quiz->cmid); + $this->context = \context_module::instance($this->quiz->cmid); $template = $this->create_template(); @@ -835,7 +827,7 @@ public function test_showsebdownloadlink_in_form() { $this->course = $this->getDataGenerator()->create_course(); $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY); - $this->context = context_module::instance($this->quiz->cmid); + $this->context = \context_module::instance($this->quiz->cmid); $this->set_up_user_and_role(); @@ -862,7 +854,7 @@ public function test_allowedbrowserexamkeys_in_form() { $this->course = $this->getDataGenerator()->create_course(); $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CLIENT_CONFIG); - $this->context = context_module::instance($this->quiz->cmid); + $this->context = \context_module::instance($this->quiz->cmid); $this->set_up_user_and_role(); @@ -907,7 +899,7 @@ public function test_validate_draftarea_configfile_failure() { $xml = "This is not a config file."; $itemid = $this->create_test_draftarea_file($xml); $errors = settings_provider::validate_draftarea_configfile($itemid); - $this->assertEquals($errors, new lang_string('fileparsefailed', 'quizaccess_seb')); + $this->assertEquals($errors, new \lang_string('fileparsefailed', 'quizaccess_seb')); } /** @@ -934,7 +926,7 @@ public function test_save_filemanager_sebconfigfile_draftarea() { $this->resetAfterTest(); $this->course = $this->getDataGenerator()->create_course(); $this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]); - $this->context = context_module::instance($this->quiz->cmid); + $this->context = \context_module::instance($this->quiz->cmid); $this->set_up_user_and_role(); $xml = file_get_contents(__DIR__ . '/fixtures/unencrypted.seb'); @@ -956,7 +948,7 @@ public function test_delete_uploaded_config_file() { $this->resetAfterTest(); $this->course = $this->getDataGenerator()->create_course(); $this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]); - $this->context = context_module::instance($this->quiz->cmid); + $this->context = \context_module::instance($this->quiz->cmid); $this->set_up_user_and_role(); $xml = file_get_contents(__DIR__ . '/fixtures/unencrypted.seb'); @@ -984,7 +976,7 @@ public function test_get_module_context_sebconfig_file() { $this->course = $this->getDataGenerator()->create_course(); $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY); - $this->context = context_module::instance($this->quiz->cmid); + $this->context = \context_module::instance($this->quiz->cmid); $this->set_up_user_and_role(); @@ -1026,7 +1018,7 @@ public function test_can_configure_seb() { $this->course = $this->getDataGenerator()->create_course(); $this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]); - $this->context = context_module::instance($this->quiz->cmid); + $this->context = \context_module::instance($this->quiz->cmid); $this->setAdminUser(); $this->assertTrue(settings_provider::can_configure_seb($this->context)); @@ -1047,7 +1039,7 @@ public function test_can_use_seb_template() { $this->course = $this->getDataGenerator()->create_course(); $this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]); - $this->context = context_module::instance($this->quiz->cmid); + $this->context = \context_module::instance($this->quiz->cmid); $this->setAdminUser(); $this->assertTrue(settings_provider::can_use_seb_template($this->context)); @@ -1068,7 +1060,7 @@ public function test_can_upload_seb_file() { $this->course = $this->getDataGenerator()->create_course(); $this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]); - $this->context = context_module::instance($this->quiz->cmid); + $this->context = \context_module::instance($this->quiz->cmid); $this->setAdminUser(); $this->assertTrue(settings_provider::can_upload_seb_file($this->context)); @@ -1089,7 +1081,7 @@ public function test_can_change_seb_showsebdownloadlink() { $this->course = $this->getDataGenerator()->create_course(); $this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]); - $this->context = context_module::instance($this->quiz->cmid); + $this->context = \context_module::instance($this->quiz->cmid); $this->setAdminUser(); $this->assertTrue(settings_provider::can_change_seb_showsebdownloadlink($this->context)); @@ -1109,7 +1101,7 @@ public function test_can_change_seb_allowedbrowserexamkeys() { $this->course = $this->getDataGenerator()->create_course(); $this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]); - $this->context = context_module::instance($this->quiz->cmid); + $this->context = \context_module::instance($this->quiz->cmid); $this->setAdminUser(); $this->assertTrue(settings_provider::can_change_seb_allowedbrowserexamkeys($this->context)); @@ -1133,7 +1125,7 @@ public function test_can_configure_manually($settingcapability) { $this->course = $this->getDataGenerator()->create_course(); $this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]); - $this->context = context_module::instance($this->quiz->cmid); + $this->context = \context_module::instance($this->quiz->cmid); $this->setAdminUser(); $this->assertTrue(settings_provider::can_configure_manually($this->context)); @@ -1171,7 +1163,7 @@ public function test_is_conflicting_permissions_for_manage_templates() { $this->course = $this->getDataGenerator()->create_course(); $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY); - $this->context = context_module::instance($this->quiz->cmid); + $this->context = \context_module::instance($this->quiz->cmid); // Create a template. $template = $this->create_template(); @@ -1199,7 +1191,7 @@ public function test_is_conflicting_permissions_for_upload_seb_file() { $this->course = $this->getDataGenerator()->create_course(); $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY); - $this->context = context_module::instance($this->quiz->cmid); + $this->context = \context_module::instance($this->quiz->cmid); // Save file. $xml = file_get_contents(__DIR__ . '/fixtures/unencrypted.seb'); @@ -1230,7 +1222,7 @@ public function test_is_conflicting_permissions_for_configure_manually($settingc $this->course = $this->getDataGenerator()->create_course(); $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY); - $this->context = context_module::instance($this->quiz->cmid); + $this->context = \context_module::instance($this->quiz->cmid); $this->assertFalse(settings_provider::is_conflicting_permissions($this->context)); @@ -1256,7 +1248,7 @@ public function test_add_prefix() { * Test filter_plugin_settings helper method. */ public function test_filter_plugin_settings() { - $test = new stdClass(); + $test = new \stdClass(); $test->one = 'one'; $test->seb_two = 'two'; $test->seb_seb_three = 'three'; @@ -1278,7 +1270,7 @@ public function test_filter_plugin_settings() { * @return \stdClass */ protected function get_settings() { - $allsettings = new stdClass(); + $allsettings = new \stdClass(); $allsettings->seb_showsebdownloadlink = 0; $allsettings->seb_linkquitseb = 2; $allsettings->seb_userconfirmquit = 3; diff --git a/mod/quiz/accessrule/seb/tests/template_test.php b/mod/quiz/accessrule/seb/tests/template_test.php index 0be6b6d4fa0ce..dbc8f62a8bd2a 100644 --- a/mod/quiz/accessrule/seb/tests/template_test.php +++ b/mod/quiz/accessrule/seb/tests/template_test.php @@ -14,26 +14,17 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * PHPUnit tests for template class. - * - * @package quizaccess_seb - * @author Dmitrii Metelkin - * @copyright 2020 Catalyst IT - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -use quizaccess_seb\template; - -defined('MOODLE_INTERNAL') || die(); +namespace quizaccess_seb; /** * PHPUnit tests for template class. * - * @copyright 2020 Catalyst IT + * @package quizaccess_seb + * @author Dmitrii Metelkin + * @copyright 2020 Catalyst IT * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class quizaccess_seb_template_testcase extends advanced_testcase { +class template_test extends \advanced_testcase { /** * Called before every test. @@ -49,7 +40,7 @@ public function setUp(): void { */ public function test_template_is_saved() { global $DB; - $data = new stdClass(); + $data = new \stdClass(); $data->name = 'Test name'; $data->description = 'Test description'; $data->enabled = 1; @@ -82,7 +73,7 @@ public function test_template_is_not_saved_with_invalid_content() { $this->expectException(\core\invalid_persistent_exception::class); $this->expectExceptionMessage('Invalid SEB config template'); - $data = new stdClass(); + $data = new \stdClass(); $data->name = 'Test name'; $data->description = 'Test description'; $data->enabled = 1; @@ -97,7 +88,7 @@ public function test_template_is_not_saved_with_invalid_content() { public function test_cannot_delete_template_when_assigned_to_quiz() { global $DB; - $data = new stdClass(); + $data = new \stdClass(); $data->name = 'Test name'; $data->description = 'Test description'; $data->enabled = 1; @@ -117,7 +108,7 @@ public function test_cannot_delete_template_when_assigned_to_quiz() { $template->save(); $this->assertTrue($template->can_delete()); - $DB->insert_record(\quizaccess_seb\quiz_settings::TABLE, (object) [ + $DB->insert_record(quiz_settings::TABLE, (object) [ 'quizid' => 1, 'cmid' => 1, 'templateid' => $template->get('id'), diff --git a/mod/quiz/report/overview/tests/report_test.php b/mod/quiz/report/overview/tests/report_test.php index 8f58e721e22bb..3fcf37a5368fe 100644 --- a/mod/quiz/report/overview/tests/report_test.php +++ b/mod/quiz/report/overview/tests/report_test.php @@ -14,13 +14,16 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Tests for the quiz overview report. - * - * @package quiz_overview - * @copyright 2014 The Open University - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace quiz_overview; + +use question_engine; +use quiz; +use quiz_attempt; +use quiz_attempts_report; +use quiz_overview_options; +use quiz_overview_report; +use quiz_overview_table; +use testable_quiz_attempts_report; defined('MOODLE_INTERNAL') || die(); @@ -31,14 +34,14 @@ require_once($CFG->dirroot . '/mod/quiz/report/overview/report.php'); require_once($CFG->dirroot . '/mod/quiz/report/overview/tests/helpers.php'); - /** * Tests for the quiz overview report. * + * @package quiz_overview * @copyright 2014 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class quiz_overview_report_testcase extends advanced_testcase { +class report_test extends \advanced_testcase { /** * Data provider for test_report_sql. @@ -146,7 +149,7 @@ public function test_report_sql($isdownloading) { $quba->get_question_attempt(1)->manual_grade( 'Comment', $sumgrades, FORMAT_HTML, $timestart + 1200); question_engine::save_questions_usage_by_activity($quba); - $update = new stdClass(); + $update = new \stdClass(); $update->id = $attemptobj->get_attemptid(); $update->timemodified = $timestart + 1200; $update->sumgrades = $quba->get_total_mark(); @@ -158,7 +161,7 @@ public function test_report_sql($isdownloading) { // Actually getting the SQL to run is quite hard. Do a minimal set up of // some objects. - $context = context_module::instance($quiz->cmid); + $context = \context_module::instance($quiz->cmid); $cm = get_coursemodule_from_id('quiz', $quiz->cmid); $qmsubselect = quiz_report_qm_filter_select($quiz); $studentsjoins = get_enrolled_with_capabilities_join($context, '', @@ -179,7 +182,7 @@ public function test_report_sql($isdownloading) { $table->download = $isdownloading; // Cannot call the is_downloading API, because it gives errors. $table->define_columns(array('fullname')); $table->sortable(true, 'uniqueid'); - $table->define_baseurl(new moodle_url('/mod/quiz/report.php')); + $table->define_baseurl(new \moodle_url('/mod/quiz/report.php')); $table->setup(); // Run the query. @@ -217,7 +220,7 @@ public function test_report_sql($isdownloading) { // Ensure that filtering by initial does not break it. // This involves setting a private properly of the base class, which is // only really possible using reflection :-(. - $reflectionobject = new ReflectionObject($table); + $reflectionobject = new \ReflectionObject($table); while ($parent = $reflectionobject->getParentClass()) { $reflectionobject = $parent; } @@ -297,7 +300,7 @@ public function test_delete_selected_attempts() { $generator->enrol_user($student->id, $course->id); $generator->enrol_user($student->id, $course->id, null, 'self'); - $context = context_module::instance($quiz->cmid); + $context = \context_module::instance($quiz->cmid); $cm = get_coursemodule_from_id('quiz', $quiz->cmid); $allowedjoins = get_enrolled_with_capabilities_join($context, '', ['mod/quiz:attempt', 'mod/quiz:reviewmyattempts']); $quizattemptsreport = new testable_quiz_attempts_report(); diff --git a/mod/quiz/report/responses/tests/responses_from_steps_walkthrough_test.php b/mod/quiz/report/responses/tests/responses_from_steps_walkthrough_test.php index f4b74f15e0747..8b32981177074 100644 --- a/mod/quiz/report/responses/tests/responses_from_steps_walkthrough_test.php +++ b/mod/quiz/report/responses/tests/responses_from_steps_walkthrough_test.php @@ -14,15 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Quiz attempt walk through using data from csv file. - * - * @package quiz_statistics - * @category phpunit - * @copyright 2013 The Open University - * @author Jamie Pratt - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace quiz_responses; + +use question_bank; +use quiz_attempt; defined('MOODLE_INTERNAL') || die(); @@ -35,13 +30,13 @@ /** * Quiz attempt walk through using data from csv file. * - * @package quiz_statistics - * @category phpunit + * @package quiz_responses + * @category test * @copyright 2013 The Open University * @author Jamie Pratt * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class quiz_report_responses_from_steps_testcase extends mod_quiz_attempt_walkthrough_from_csv_testcase { +class responses_from_steps_walkthrough_test extends \mod_quiz\attempt_walkthrough_from_csv_test { protected function get_full_path_of_csv_file($setname, $test) { // Overridden here so that __DIR__ points to the path of this file. return __DIR__."/fixtures/{$setname}{$test}.csv"; @@ -69,7 +64,7 @@ public function test_walkthrough_from_csv($quizsettings, $csvdata) { $responses = $this->explode_dot_separated_keys_to_make_subindexs($responsesfromcsv); if (!isset($quizattemptids[$responses['quizattempt']])) { - throw new coding_exception("There is no quizattempt {$responses['quizattempt']}!"); + throw new \coding_exception("There is no quizattempt {$responses['quizattempt']}!"); } $this->assert_response_test($quizattemptids[$responses['quizattempt']], $responses); } @@ -92,7 +87,7 @@ protected function assert_response_test($quizattemptid, $responses) { $stepswithsubmit = $qa->get_steps_with_submitted_response_iterator(); $step = $stepswithsubmit[$responses['submittedstepno']]; if (null === $step) { - throw new coding_exception("There is no step no {$responses['submittedstepno']} ". + throw new \coding_exception("There is no step no {$responses['submittedstepno']} ". "for slot $slot in quizattempt {$responses['quizattempt']}!"); } foreach (array('responsesummary', 'fraction', 'state') as $column) { diff --git a/mod/quiz/report/statistics/tests/statistics_table_test.php b/mod/quiz/report/statistics/tests/statistics_table_test.php index 6d86c2036da0b..1169112d54513 100644 --- a/mod/quiz/report/statistics/tests/statistics_table_test.php +++ b/mod/quiz/report/statistics/tests/statistics_table_test.php @@ -14,14 +14,9 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Unit tests for the quiz_statistics_table class. - * - * @package quiz_statistics - * @category test - * @copyright 2018 Shamim Rezaie - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace quiz_statistics; + +use quiz_statistics_table; defined('MOODLE_INTERNAL') || die(); @@ -31,16 +26,18 @@ /** * Class quiz_statistics_statistics_table_testcase * + * @package quiz_statistics + * @category test * @copyright 2018 Shamim Rezaie * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class quiz_statistics_statistics_table_testcase extends advanced_testcase { +class statistics_table_test extends \advanced_testcase { public function test_format_percentage() { $table = new quiz_statistics_table(); // The format_percentage method is protected. Use Reflection to call the method. - $reflector = new ReflectionClass('quiz_statistics_table'); + $reflector = new \ReflectionClass('quiz_statistics_table'); $method = $reflector->getMethod('format_percentage'); $method->setAccessible(true); @@ -59,7 +56,7 @@ public function test_format_percentage_range() { $table = new quiz_statistics_table(); // The format_percentage_range method is protected. Use Reflection to call the method. - $reflector = new ReflectionClass('quiz_statistics_table'); + $reflector = new \ReflectionClass('quiz_statistics_table'); $method = $reflector->getMethod('format_percentage_range'); $method->setAccessible(true); @@ -78,7 +75,7 @@ public function test_format_range() { $table = new quiz_statistics_table(); // The format_range method is protected. Use Reflection to call the method. - $reflector = new ReflectionClass('quiz_statistics_table'); + $reflector = new \ReflectionClass('quiz_statistics_table'); $method = $reflector->getMethod('format_range'); $method->setAccessible(true); diff --git a/mod/quiz/report/statistics/tests/statistics_test.php b/mod/quiz/report/statistics/tests/statistics_test.php index d4bfeaf6dc34b..0706c6d393196 100644 --- a/mod/quiz/report/statistics/tests/statistics_test.php +++ b/mod/quiz/report/statistics/tests/statistics_test.php @@ -18,11 +18,13 @@ * Unit tests for (some of) /question/engine/statistics.php * * @package quiz_statistics - * @category phpunit + * @category test * @copyright 2008 Jamie Pratt * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +namespace quiz_statistics; + defined('MOODLE_INTERNAL') || die(); global $CFG; @@ -55,7 +57,7 @@ class testable_question_statistics extends \core_question\statistics\questions\c */ protected $lateststeps; - protected $statscollectionclassname = 'testable_all_calculated_for_qubaid_condition'; + protected $statscollectionclassname = '\quiz_statistics\testable_all_calculated_for_qubaid_condition'; public function set_step_data($states) { $this->lateststeps = $states; @@ -99,7 +101,7 @@ protected function cache_stats($qubaids) { * @copyright 2008 Jamie Pratt * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class quiz_statistics_question_stats_testcase extends basic_testcase { +class statistics_test extends \basic_testcase { /** @var testable_all_calculated_for_qubaid_condition object created to test class. */ protected $qstats; @@ -181,7 +183,7 @@ public function get_records_from_csv($filename) { while (null !== ($line = array_shift($filecontents))) { $data = $this->get_fields_from_csv($line); $arraykey = reset($data); - $object = new stdClass(); + $object = new \stdClass(); foreach ($keys as $key) { $value = array_shift($data); if ($value !== null) { diff --git a/mod/quiz/report/statistics/tests/stats_from_steps_walkthrough_test.php b/mod/quiz/report/statistics/tests/stats_from_steps_walkthrough_test.php index da2333ed1577f..203b31a88af50 100644 --- a/mod/quiz/report/statistics/tests/stats_from_steps_walkthrough_test.php +++ b/mod/quiz/report/statistics/tests/stats_from_steps_walkthrough_test.php @@ -14,23 +14,12 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Quiz attempt walk through using data from csv file. - * - * The quiz stats below and the question stats found in qstats00.csv were calculated independently in a spreadsheet which is - * available in open document or excel format here : - * https://github.com/jamiepratt/moodle-quiz-tools/tree/master/statsspreadsheet - * - * Similarly the question variant's stats in qstats00.csv are calculated in stats_for_variant_1.xls and stats_for_variant_8.xls - * The calculations in the spreadsheets are the same as for the other question stats but applied just to the attempts where the - * variants appeared. - * - * @package quiz_statistics - * @category phpunit - * @copyright 2013 The Open University - * @author Jamie Pratt - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace quiz_statistics; + +use question_attempt; +use question_bank; +use question_finder; +use quiz_statistics_report; defined('MOODLE_INTERNAL') || die(); @@ -43,13 +32,21 @@ /** * Quiz attempt walk through using data from csv file. * + * The quiz stats below and the question stats found in qstats00.csv were calculated independently in a spreadsheet which is + * available in open document or excel format here : + * https://github.com/jamiepratt/moodle-quiz-tools/tree/master/statsspreadsheet + * + * Similarly the question variant's stats in qstats00.csv are calculated in stats_for_variant_1.xls and stats_for_variant_8.xls + * The calculations in the spreadsheets are the same as for the other question stats but applied just to the attempts where the + * variants appeared. + * * @package quiz_statistics - * @category phpunit + * @category test * @copyright 2013 The Open University * @author Jamie Pratt * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class quiz_report_statistics_from_steps_testcase extends mod_quiz_attempt_walkthrough_from_csv_testcase { +class stats_from_steps_walkthrough_test extends \mod_quiz\attempt_walkthrough_from_csv_test { /** * @var quiz_statistics_report object to do stats calculations. @@ -193,7 +190,7 @@ protected function assert_response_count_equals($question, $qubaids, $expected, return; } } - throw new coding_exception("Expected response '{$expected['actualresponse']}' not found."); + throw new \coding_exception("Expected response '{$expected['actualresponse']}' not found."); } protected function get_response_subpart_and_class_id($question, $subpart, $modelresponse) { @@ -201,7 +198,7 @@ protected function get_response_subpart_and_class_id($question, $subpart, $model $possibleresponses = $qtypeobj->get_possible_responses($question); $possibleresponsesubpartids = array_keys($possibleresponses); if (!isset($possibleresponsesubpartids[$subpart - 1])) { - throw new coding_exception("Subpart '{$subpart}' not found."); + throw new \coding_exception("Subpart '{$subpart}' not found."); } $subpartid = $possibleresponsesubpartids[$subpart - 1]; @@ -373,7 +370,7 @@ protected function check_stats_calculations_and_response_analysis($csvdata, $whi // We will create some quiz and question stat calculator instances and some response analyser instances, just in order // to check the last analysed time then returned. - $quizcalc = new \quiz_statistics\calculator(); + $quizcalc = new calculator(); // Should not be a delay of more than one second between the calculation of stats above and here. $this->assertTimeCurrent($quizcalc->get_last_calculated_time($qubaids)); diff --git a/mod/quiz/tests/attempt_test.php b/mod/quiz/tests/attempt_test.php index a91cbb4a43760..cb9e87fc8997f 100644 --- a/mod/quiz/tests/attempt_test.php +++ b/mod/quiz/tests/attempt_test.php @@ -14,14 +14,11 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Tests for the quiz_attempt class. - * - * @package mod_quiz - * @category test - * @copyright 2014 Tim Hunt - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace mod_quiz; + +use question_engine; +use quiz; +use quiz_attempt; defined('MOODLE_INTERNAL') || die(); @@ -31,10 +28,12 @@ /** * Tests for the quiz_attempt class. * + * @package mod_quiz + * @category test * @copyright 2014 Tim Hunt * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_quiz_attempt_testcase extends advanced_testcase { +class attempt_test extends \advanced_testcase { /** * Create quiz and attempt data with layout. @@ -90,70 +89,70 @@ public function test_attempt_url() { $url = '/mod/quiz/attempt.php'; $params = ['attempt' => $attemptid, 'cmid' => $cmid, 'page' => 2]; - $this->assertEquals(new moodle_url($url, $params), $attempt->attempt_url(null, 2)); + $this->assertEquals(new \moodle_url($url, $params), $attempt->attempt_url(null, 2)); $params['page'] = 1; - $this->assertEquals(new moodle_url($url, $params), $attempt->attempt_url(3)); + $this->assertEquals(new \moodle_url($url, $params), $attempt->attempt_url(3)); $questionattempt = $attempt->get_question_attempt(4); $expecteanchor = $questionattempt->get_outer_question_div_unique_id(); - $this->assertEquals(new moodle_url($url, $params, $expecteanchor), $attempt->attempt_url(4)); + $this->assertEquals(new \moodle_url($url, $params, $expecteanchor), $attempt->attempt_url(4)); - $this->assertEquals(new moodle_url('#'), $attempt->attempt_url(null, 2, 2)); - $this->assertEquals(new moodle_url('#'), $attempt->attempt_url(3, -1, 1)); + $this->assertEquals(new \moodle_url('#'), $attempt->attempt_url(null, 2, 2)); + $this->assertEquals(new \moodle_url('#'), $attempt->attempt_url(3, -1, 1)); $questionattempt = $attempt->get_question_attempt(4); $expecteanchor = $questionattempt->get_outer_question_div_unique_id(); - $this->assertEquals(new moodle_url(null, null, $expecteanchor, null), $attempt->attempt_url(4, -1, 1)); + $this->assertEquals(new \moodle_url(null, null, $expecteanchor, null), $attempt->attempt_url(4, -1, 1)); // Summary page. $url = '/mod/quiz/summary.php'; unset($params['page']); - $this->assertEquals(new moodle_url($url, $params), $attempt->summary_url()); + $this->assertEquals(new \moodle_url($url, $params), $attempt->summary_url()); // Review page. $url = '/mod/quiz/review.php'; - $this->assertEquals(new moodle_url($url, $params), $attempt->review_url()); + $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url()); $params['page'] = 1; - $this->assertEquals(new moodle_url($url, $params), $attempt->review_url(3, -1, false)); - $this->assertEquals(new moodle_url($url, $params, $expecteanchor), $attempt->review_url(4, -1, false)); + $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(3, -1, false)); + $this->assertEquals(new \moodle_url($url, $params, $expecteanchor), $attempt->review_url(4, -1, false)); unset($params['page']); - $this->assertEquals(new moodle_url($url, $params), $attempt->review_url(null, 2, true)); - $this->assertEquals(new moodle_url($url, $params), $attempt->review_url(1, -1, true)); + $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(null, 2, true)); + $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(1, -1, true)); $params['page'] = 2; - $this->assertEquals(new moodle_url($url, $params), $attempt->review_url(null, 2, false)); + $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(null, 2, false)); unset($params['page']); $params['showall'] = 0; - $this->assertEquals(new moodle_url($url, $params), $attempt->review_url(null, 0, false)); - $this->assertEquals(new moodle_url($url, $params), $attempt->review_url(1, -1, false)); + $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(null, 0, false)); + $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(1, -1, false)); $params['page'] = 1; unset($params['showall']); - $this->assertEquals(new moodle_url($url, $params), $attempt->review_url(3, -1, false)); + $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(3, -1, false)); $params['page'] = 2; - $this->assertEquals(new moodle_url($url, $params), $attempt->review_url(null, 2)); - $this->assertEquals(new moodle_url('#'), $attempt->review_url(null, -1, null, 0)); + $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(null, 2)); + $this->assertEquals(new \moodle_url('#'), $attempt->review_url(null, -1, null, 0)); $questionattempt = $attempt->get_question_attempt(3); $expecteanchor = '#' . $questionattempt->get_outer_question_div_unique_id(); - $this->assertEquals(new moodle_url($expecteanchor), $attempt->review_url(3, -1, null, 0)); + $this->assertEquals(new \moodle_url($expecteanchor), $attempt->review_url(3, -1, null, 0)); $questionattempt = $attempt->get_question_attempt(4); $expecteanchor = '#' . $questionattempt->get_outer_question_div_unique_id(); - $this->assertEquals(new moodle_url($expecteanchor), $attempt->review_url(4, -1, null, 0)); - $this->assertEquals(new moodle_url('#'), $attempt->review_url(null, 2, true, 0)); - $this->assertEquals(new moodle_url('#'), $attempt->review_url(1, -1, true, 0)); - $this->assertEquals(new moodle_url($url, $params), $attempt->review_url(null, 2, false, 0)); - $this->assertEquals(new moodle_url('#'), $attempt->review_url(null, 0, false, 0)); - $this->assertEquals(new moodle_url('#'), $attempt->review_url(1, -1, false, 0)); + $this->assertEquals(new \moodle_url($expecteanchor), $attempt->review_url(4, -1, null, 0)); + $this->assertEquals(new \moodle_url('#'), $attempt->review_url(null, 2, true, 0)); + $this->assertEquals(new \moodle_url('#'), $attempt->review_url(1, -1, true, 0)); + $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(null, 2, false, 0)); + $this->assertEquals(new \moodle_url('#'), $attempt->review_url(null, 0, false, 0)); + $this->assertEquals(new \moodle_url('#'), $attempt->review_url(1, -1, false, 0)); $params['page'] = 1; - $this->assertEquals(new moodle_url($url, $params), $attempt->review_url(3, -1, false, 0)); + $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(3, -1, false, 0)); // Setup another attempt. $attempt = $this->create_quiz_and_attempt_with_layout( @@ -164,54 +163,54 @@ public function test_attempt_url() { $attemptid = $attempt->get_attempt()->id; $cmid = $attempt->get_cmid(); $params = ['attempt' => $attemptid, 'cmid' => $cmid]; - $this->assertEquals(new moodle_url($url, $params), $attempt->review_url()); + $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url()); $params['page'] = 2; - $this->assertEquals(new moodle_url($url, $params), $attempt->review_url(null, 2)); + $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(null, 2)); $params['page'] = 1; unset($params['showall']); - $this->assertEquals(new moodle_url($url, $params), $attempt->review_url(11, -1, false)); + $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(11, -1, false)); $questionattempt = $attempt->get_question_attempt(12); $expecteanchor = $questionattempt->get_outer_question_div_unique_id(); - $this->assertEquals(new moodle_url($url, $params, $expecteanchor), $attempt->review_url(12, -1, false)); + $this->assertEquals(new \moodle_url($url, $params, $expecteanchor), $attempt->review_url(12, -1, false)); $params['showall'] = 1; unset($params['page']); - $this->assertEquals(new moodle_url($url, $params), $attempt->review_url(null, 2, true)); + $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(null, 2, true)); - $this->assertEquals(new moodle_url($url, $params), $attempt->review_url(1, -1, true)); + $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(1, -1, true)); $params['page'] = 2; unset($params['showall']); - $this->assertEquals(new moodle_url($url, $params), $attempt->review_url(null, 2, false)); + $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(null, 2, false)); unset($params['page']); - $this->assertEquals(new moodle_url($url, $params), $attempt->review_url(null, 0, false)); + $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(null, 0, false)); $params['page'] = 1; - $this->assertEquals(new moodle_url($url, $params), $attempt->review_url(11, -1, false)); - $this->assertEquals(new moodle_url($url, $params, $expecteanchor), $attempt->review_url(12, -1, false)); + $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(11, -1, false)); + $this->assertEquals(new \moodle_url($url, $params, $expecteanchor), $attempt->review_url(12, -1, false)); $params['page'] = 2; - $this->assertEquals(new moodle_url($url, $params), $attempt->review_url(null, 2)); - $this->assertEquals(new moodle_url('#'), $attempt->review_url(null, -1, null, 0)); + $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(null, 2)); + $this->assertEquals(new \moodle_url('#'), $attempt->review_url(null, -1, null, 0)); $questionattempt = $attempt->get_question_attempt(3); $expecteanchor = $questionattempt->get_outer_question_div_unique_id(); - $this->assertEquals(new moodle_url(null, null, $expecteanchor), $attempt->review_url(3, -1, null, 0)); + $this->assertEquals(new \moodle_url(null, null, $expecteanchor), $attempt->review_url(3, -1, null, 0)); $questionattempt = $attempt->get_question_attempt(4); $expecteanchor = $questionattempt->get_outer_question_div_unique_id(); - $this->assertEquals(new moodle_url(null, null, $expecteanchor), $attempt->review_url(4, -1, null, 0)); + $this->assertEquals(new \moodle_url(null, null, $expecteanchor), $attempt->review_url(4, -1, null, 0)); - $this->assertEquals(new moodle_url('#'), $attempt->review_url(null, 2, true, 0)); - $this->assertEquals(new moodle_url('#'), $attempt->review_url(1, -1, true, 0)); + $this->assertEquals(new \moodle_url('#'), $attempt->review_url(null, 2, true, 0)); + $this->assertEquals(new \moodle_url('#'), $attempt->review_url(1, -1, true, 0)); $params['page'] = 2; - $this->assertEquals(new moodle_url($url, $params), $attempt->review_url(null, 2, false, 0)); - $this->assertEquals(new moodle_url('#'), $attempt->review_url(null, 0, false, 0)); - $this->assertEquals(new moodle_url('#'), $attempt->review_url(1, -1, false, 0)); + $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(null, 2, false, 0)); + $this->assertEquals(new \moodle_url('#'), $attempt->review_url(null, 0, false, 0)); + $this->assertEquals(new \moodle_url('#'), $attempt->review_url(1, -1, false, 0)); $params['page'] = 1; - $this->assertEquals(new moodle_url($url, $params), $attempt->review_url(11, -1, false, 0)); + $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(11, -1, false, 0)); } /** diff --git a/mod/quiz/tests/attempt_walkthrough_from_csv_test.php b/mod/quiz/tests/attempt_walkthrough_from_csv_test.php index 3af909b13f8ac..2b361014cb691 100644 --- a/mod/quiz/tests/attempt_walkthrough_from_csv_test.php +++ b/mod/quiz/tests/attempt_walkthrough_from_csv_test.php @@ -14,15 +14,12 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Quiz attempt walk through using data from csv file. - * - * @package mod_quiz - * @category phpunit - * @copyright 2013 The Open University - * @author Jamie Pratt - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace mod_quiz; + +use question_bank; +use question_engine; +use quiz; +use quiz_attempt; defined('MOODLE_INTERNAL') || die(); @@ -33,12 +30,12 @@ * Quiz attempt walk through using data from csv file. * * @package mod_quiz - * @category phpunit + * @category test * @copyright 2013 The Open University * @author Jamie Pratt * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_quiz_attempt_walkthrough_from_csv_testcase extends advanced_testcase { +class attempt_walkthrough_from_csv_test extends \advanced_testcase { protected $files = array('questions', 'steps', 'results'); @@ -331,7 +328,7 @@ protected function check_attempt_results($result, $attemptobj) { "Mark for slot $slotno of attempt {$result['quizattempt']}."); break; default : - throw new coding_exception('Unknown slots sub field column in csv file ' + throw new \coding_exception('Unknown slots sub field column in csv file ' .s($slotfieldname)); } } @@ -361,7 +358,7 @@ protected function check_attempt_results($result, $attemptobj) { $this->assertEquals($value, $gradebookgrade->grade, "Gradebook grade for attempt {$result['quizattempt']}."); break; default : - throw new coding_exception('Unknown column in csv file '.s($fieldname)); + throw new \coding_exception('Unknown column in csv file '.s($fieldname)); } } } diff --git a/mod/quiz/tests/attempt_walkthrough_test.php b/mod/quiz/tests/attempt_walkthrough_test.php index 1e4338d03ec1f..aadcaac6bacde 100644 --- a/mod/quiz/tests/attempt_walkthrough_test.php +++ b/mod/quiz/tests/attempt_walkthrough_test.php @@ -14,15 +14,12 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Quiz attempt walk through tests. - * - * @package mod_quiz - * @category phpunit - * @copyright 2013 The Open University - * @author Jamie Pratt - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace mod_quiz; + +use question_bank; +use question_engine; +use quiz; +use quiz_attempt; defined('MOODLE_INTERNAL') || die(); @@ -33,12 +30,12 @@ * Quiz attempt walk through. * * @package mod_quiz - * @category phpunit + * @category test * @copyright 2013 The Open University * @author Jamie Pratt * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_quiz_attempt_walkthrough_testcase extends advanced_testcase { +class attempt_walkthrough_test extends \advanced_testcase { /** * Create a quiz with questions and walk through a quiz attempt. diff --git a/mod/quiz/tests/attempts_test.php b/mod/quiz/tests/attempts_test.php index 161be625831f3..52abb66289597 100644 --- a/mod/quiz/tests/attempts_test.php +++ b/mod/quiz/tests/attempts_test.php @@ -14,14 +14,11 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Quiz attempt overdue handling tests - * - * @package mod_quiz - * @category phpunit - * @copyright 2012 Matt Petro - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace mod_quiz; + +use mod_quiz_overdue_attempt_updater; +use question_engine; +use quiz; defined('MOODLE_INTERNAL') || die(); @@ -32,11 +29,11 @@ * Unit tests for quiz attempt overdue handling * * @package mod_quiz - * @category phpunit + * @category test * @copyright 2012 Matt Petro * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_quiz_attempt_overdue_testcase extends advanced_testcase { +class attempts_test extends \advanced_testcase { /** * Test the functions quiz_update_open_attempts(), get_list_of_overdue_attempts() and @@ -458,12 +455,12 @@ public function test_bulk_update_functions() { * question usage to store in uniqueid, but they don't have to be * very realistic. * - * @param stdClass $quiz + * @param \stdClass $quiz * @return int question usage id. */ - protected function usage_id(stdClass $quiz): int { + protected function usage_id(\stdClass $quiz): int { $quba = question_engine::make_questions_usage_by_activity('mod_quiz', - context_module::instance($quiz->cmid)); + \context_module::instance($quiz->cmid)); $quba->set_preferred_behaviour('deferredfeedback'); question_engine::save_questions_usage_by_activity($quba); return $quba->get_id(); @@ -578,19 +575,19 @@ public function test_quiz_create_attempt_handling_errors() { try { $result = quiz_create_attempt_handling_errors($attempt->id, 9999); $this->fail('Exception expected due to invalid course module id.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('invalidcoursemodule', $e->errorcode); } try { quiz_create_attempt_handling_errors(9999, $result->get_cmid()); $this->fail('Exception expected due to quiz content change.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('attempterrorcontentchange', $e->errorcode); } try { quiz_create_attempt_handling_errors(9999); $this->fail('Exception expected due to invalid quiz attempt id.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('attempterrorinvalid', $e->errorcode); } // Set up as normal user without permission to view preview. @@ -598,13 +595,13 @@ public function test_quiz_create_attempt_handling_errors() { try { quiz_create_attempt_handling_errors(9999, $result->get_cmid()); $this->fail('Exception expected due to quiz content change for user without permission.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('attempterrorcontentchangeforuser', $e->errorcode); } try { quiz_create_attempt_handling_errors($attempt->id, 9999); $this->fail('Exception expected due to invalid course module id for user without permission.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('invalidcoursemodule', $e->errorcode); } } diff --git a/mod/quiz/tests/calendar_event_modified_test.php b/mod/quiz/tests/calendar_event_modified_test.php index 4a4cfdf0ffc99..542040123c705 100644 --- a/mod/quiz/tests/calendar_event_modified_test.php +++ b/mod/quiz/tests/calendar_event_modified_test.php @@ -14,16 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Unit tests for the calendar event modification callbacks used - * for dragging and dropping quiz calendar events in the calendar - * UI. - * - * @package mod_quiz - * @category test - * @copyright 2017 Ryan Wyllie - * @license http://www.gnu.org/copyleft/gpl.html GNU Public License - */ +namespace mod_quiz; defined('MOODLE_INTERNAL') || die(); @@ -31,10 +22,16 @@ require_once($CFG->dirroot . '/mod/quiz/lib.php'); /** + * Unit tests for the calendar event modification callbacks used + * for dragging and dropping quiz calendar events in the calendar + * UI. + * + * @package mod_quiz + * @category test * @copyright 2017 Ryan Wyllie * @license http://www.gnu.org/copyleft/gpl.html GNU Public License */ -class mod_quiz_calendar_event_modified_testcase extends advanced_testcase { +class calendar_event_modified_test extends \advanced_testcase { /** * Create an instance of the quiz activity. @@ -71,7 +68,7 @@ protected function create_quiz_instance(array $properties) { /** * Create a calendar event for a quiz activity instance. * - * @param stdClass $quiz The activity instance + * @param \stdClass $quiz The activity instance * @param array $eventproperties Properties to set on the calendar event * @return calendar_event */ @@ -241,11 +238,11 @@ public function test_student_role_cant_update_quiz_activity() { $generator = $this->getDataGenerator(); $user = $generator->create_user(); $course = $generator->create_course(); - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $roleid = $generator->create_role(); $now = time(); - $timeopen = (new DateTime())->setTimestamp($now); - $newtimeopen = (new DateTime())->setTimestamp($now)->modify('+1 day'); + $timeopen = (new \DateTime())->setTimestamp($now); + $newtimeopen = (new \DateTime())->setTimestamp($now)->modify('+1 day'); $quiz = $this->create_quiz_instance([ 'course' => $course->id, 'timeopen' => $timeopen->getTimestamp() @@ -285,11 +282,11 @@ public function test_teacher_role_can_update_quiz_activity() { $generator = $this->getDataGenerator(); $user = $generator->create_user(); $course = $generator->create_course(); - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $roleid = $generator->create_role(); $now = time(); - $timeopen = (new DateTime())->setTimestamp($now); - $newtimeopen = (new DateTime())->setTimestamp($now)->modify('+1 day'); + $timeopen = (new \DateTime())->setTimestamp($now); + $newtimeopen = (new \DateTime())->setTimestamp($now)->modify('+1 day'); $quiz = $this->create_quiz_instance([ 'course' => $course->id, 'timeopen' => $timeopen->getTimestamp() @@ -467,12 +464,12 @@ public function test_core_calendar_event_timestart_updated_update_quiz_attempt() $teacher = $generator->create_user(); $student = $generator->create_user(); $course = $generator->create_course(); - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $roleid = $generator->create_role(); $now = time(); $timelimit = 600; - $timeopen = (new DateTime())->setTimestamp($now); - $timeclose = (new DateTime())->setTimestamp($now)->modify('+1 day'); + $timeopen = (new \DateTime())->setTimestamp($now); + $timeclose = (new \DateTime())->setTimestamp($now)->modify('+1 day'); // The new close time being earlier than the time open + time limit should // result in an update to the quiz attempts. $newtimeclose = $timeopen->getTimestamp() + $timelimit - 10; diff --git a/mod/quiz/tests/local_structure_slot_random_test.php b/mod/quiz/tests/local_structure_slot_random_test.php index 618726206ebdd..a66bf22df0a17 100644 --- a/mod/quiz/tests/local_structure_slot_random_test.php +++ b/mod/quiz/tests/local_structure_slot_random_test.php @@ -14,25 +14,20 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Unit tests for the {@link \mod_quiz\local\structure\slot_random} class. - * - * @package mod_quiz - * @category test - * @copyright 2018 Shamim Rezaie - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace mod_quiz; -defined('MOODLE_INTERNAL') || die(); +use question_bank; /** * Class mod_quiz_local_structure_slot_random_test * Class for tests related to the {@link \mod_quiz\local\structure\slot_random} class. * + * @package mod_quiz + * @category test * @copyright 2018 Shamim Rezaie * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_quiz_local_structure_slot_random_test extends advanced_testcase { +class local_structure_slot_random_test extends \advanced_testcase { /** * Constructor test. */ @@ -52,27 +47,30 @@ public function test_constructor() { // Create a random question without adding it to a quiz. // We don't want to use quiz_add_random_questions because that itself, instantiates an object from the slot_random class. - $form = new stdClass(); + $form = new \stdClass(); $form->category = $category->id . ',' . $category->contextid; $form->includesubcategories = true; $form->fromtags = []; $form->defaultmark = 1; $form->hidden = 1; $form->stamp = make_unique_id_code(); - $question = new stdClass(); + $question = new \stdClass(); $question->qtype = 'random'; $question = question_bank::get_qtype('random')->save_question($question, $form); - $randomslotdata = new stdClass(); + $randomslotdata = new \stdClass(); + $randomslotdata->quizid = $quiz->id; $randomslotdata->questionid = $question->id; $randomslotdata->questioncategoryid = $category->id; $randomslotdata->includingsubcategories = 1; $randomslotdata->maxmark = 1; + $randomslotdata->usingcontextid = \context_module::instance($quiz->cmid)->id; + $randomslotdata->questionscontextid = $category->contextid; $randomslot = new \mod_quiz\local\structure\slot_random($randomslotdata); - $rc = new ReflectionClass('\mod_quiz\local\structure\slot_random'); + $rc = new \ReflectionClass('\mod_quiz\local\structure\slot_random'); $rcp = $rc->getProperty('record'); $rcp->setAccessible(true); $record = $rcp->getValue($randomslot); @@ -103,7 +101,7 @@ public function test_get_quiz_quiz() { // Get the random question's id. It is at the first slot. $questionid = $DB->get_field('quiz_slots', 'questionid', array('quizid' => $quiz->id, 'slot' => 1)); - $randomslotdata = new stdClass(); + $randomslotdata = new \stdClass(); $randomslotdata->quizid = $quiz->id; $randomslotdata->questionid = $questionid; $randomslotdata->questioncategoryid = $category->id; @@ -137,7 +135,7 @@ public function test_set_quiz() { // Get the random question's id. It is at the first slot. $questionid = $DB->get_field('quiz_slots', 'questionid', array('quizid' => $quiz->id, 'slot' => 1)); - $randomslotdata = new stdClass(); + $randomslotdata = new \stdClass(); $randomslotdata->quizid = $quiz->id; $randomslotdata->questionid = $questionid; $randomslotdata->questioncategoryid = $category->id; @@ -151,7 +149,7 @@ public function test_set_quiz() { $randomslot->set_quiz($quiz); - $rc = new ReflectionClass('\mod_quiz\local\structure\slot_random'); + $rc = new \ReflectionClass('\mod_quiz\local\structure\slot_random'); $rcp = $rc->getProperty('quiz'); $rcp->setAccessible(true); $quizpropery = $rcp->getValue($randomslot); @@ -175,7 +173,7 @@ private function setup_for_test_tags($tagnames) { // Get the random question's id. It is at the first slot. $questionid = $DB->get_field('quiz_slots', 'questionid', array('quizid' => $quiz->id, 'slot' => 1)); - $randomslotdata = new stdClass(); + $randomslotdata = new \stdClass(); $randomslotdata->quizid = $quiz->id; $randomslotdata->questionid = $questionid; $randomslotdata->questioncategoryid = $category->id; @@ -205,7 +203,7 @@ public function test_set_tags() { list($randomslot, $tags) = $this->setup_for_test_tags(['foo', 'bar']); $randomslot->set_tags([$tags['foo'], $tags['bar']]); - $rc = new ReflectionClass('\mod_quiz\local\structure\slot_random'); + $rc = new \ReflectionClass('\mod_quiz\local\structure\slot_random'); $rcp = $rc->getProperty('tags'); $rcp->setAccessible(true); $tagspropery = $rcp->getValue($randomslot); @@ -227,7 +225,7 @@ public function test_set_tags_twice() { // Now set the tags again. $randomslot->set_tags([$tags['baz']]); - $rc = new ReflectionClass('\mod_quiz\local\structure\slot_random'); + $rc = new \ReflectionClass('\mod_quiz\local\structure\slot_random'); $rcp = $rc->getProperty('tags'); $rcp->setAccessible(true); $tagspropery = $rcp->getValue($randomslot); @@ -245,7 +243,7 @@ public function test_set_tags_duplicates() { $randomslot->set_tags([$tags['foo'], $tags['bar'], $tags['foo']]); - $rc = new ReflectionClass('\mod_quiz\local\structure\slot_random'); + $rc = new \ReflectionClass('\mod_quiz\local\structure\slot_random'); $rcp = $rc->getProperty('tags'); $rcp->setAccessible(true); $tagspropery = $rcp->getValue($randomslot); @@ -264,7 +262,7 @@ public function test_set_tags_by_id() { $randomslot->set_tags_by_id([$tags['foo']->id, $tags['bar']->id]); - $rc = new ReflectionClass('\mod_quiz\local\structure\slot_random'); + $rc = new \ReflectionClass('\mod_quiz\local\structure\slot_random'); $rcp = $rc->getProperty('tags'); $rcp->setAccessible(true); $tagspropery = $rcp->getValue($randomslot); @@ -294,7 +292,7 @@ public function test_set_tags_by_id_twice() { // Now set the tags again. $randomslot->set_tags_by_id([$tags['baz']->id]); - $rc = new ReflectionClass('\mod_quiz\local\structure\slot_random'); + $rc = new \ReflectionClass('\mod_quiz\local\structure\slot_random'); $rcp = $rc->getProperty('tags'); $rcp->setAccessible(true); $tagspropery = $rcp->getValue($randomslot); @@ -316,7 +314,7 @@ public function test_set_tags_by_id_duplicates() { $randomslot->set_tags_by_id([$tags['foo']->id, $tags['bar']->id], $tags['foo']->id); - $rc = new ReflectionClass('\mod_quiz\local\structure\slot_random'); + $rc = new \ReflectionClass('\mod_quiz\local\structure\slot_random'); $rcp = $rc->getProperty('tags'); $rcp->setAccessible(true); $tagspropery = $rcp->getValue($randomslot); @@ -350,14 +348,14 @@ public function test_insert() { $category = $questiongenerator->create_question_category(); // Create a random question without adding it to a quiz. - $form = new stdClass(); + $form = new \stdClass(); $form->category = $category->id . ',' . $category->contextid; $form->includesubcategories = true; $form->fromtags = []; $form->defaultmark = 1; $form->hidden = 1; $form->stamp = make_unique_id_code(); - $question = new stdClass(); + $question = new \stdClass(); $question->qtype = 'random'; $question = question_bank::get_qtype('random')->save_question($question, $form); @@ -377,7 +375,7 @@ public function test_insert() { ); $bartag = $this->getDataGenerator()->create_tag($tagrecord); - $randomslotdata = new stdClass(); + $randomslotdata = new \stdClass(); $randomslotdata->quizid = $quiz->id; $randomslotdata->questionid = $question->id; $randomslotdata->questioncategoryid = $category->id; diff --git a/mod/quiz/tests/privacy_legacy_quizaccess_polyfill_test.php b/mod/quiz/tests/privacy_legacy_quizaccess_polyfill_test.php index 502d5da54f413..a908760957d83 100644 --- a/mod/quiz/tests/privacy_legacy_quizaccess_polyfill_test.php +++ b/mod/quiz/tests/privacy_legacy_quizaccess_polyfill_test.php @@ -17,12 +17,14 @@ /** * Unit tests for the privacy legacy polyfill for quiz access rules. * - * @package mod_quiz - * @category test * @copyright 2018 Andrew Nicols * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +namespace mod_quiz; + +use quiz; + defined('MOODLE_INTERNAL') || die(); global $CFG; @@ -34,7 +36,7 @@ * @copyright 2018 Andrew Nicols * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class core_privacy_legacy_quizaccess_polyfill_test extends advanced_testcase { +class privacy_legacy_quizaccess_polyfill_test extends \advanced_testcase { /** * Test that the core_quizaccess\privacy\legacy_polyfill works and that the static _export_quizaccess_user_data can * be called. @@ -59,7 +61,7 @@ public function test_export_quizaccess_user_data() { * Test the _delete_quizaccess_for_context shim. */ public function test_delete_quizaccess_for_context() { - $context = context_system::instance(); + $context = \context_system::instance(); $quiz = $this->createMock(quiz::class); @@ -76,7 +78,7 @@ public function test_delete_quizaccess_for_context() { * Test the _delete_quizaccess_for_user shim. */ public function test_delete_quizaccess_for_user() { - $context = context_system::instance(); + $context = \context_system::instance(); $quiz = $this->createMock(quiz::class); $user = (object) []; @@ -94,7 +96,7 @@ public function test_delete_quizaccess_for_user() { * Test the _delete_quizaccess_for_users shim. */ public function test_delete_quizaccess_for_users() { - $context = $this->createMock(context_module::class); + $context = $this->createMock(\context_module::class); $user = (object) []; $approveduserlist = new \core_privacy\local\request\approved_userlist($context, 'mod_quiz', [$user]); diff --git a/mod/quiz/tests/quiz_question_bank_view_test.php b/mod/quiz/tests/quiz_question_bank_view_test.php index 2f99a2b537f5e..43b3b78dda7fb 100644 --- a/mod/quiz/tests/quiz_question_bank_view_test.php +++ b/mod/quiz/tests/quiz_question_bank_view_test.php @@ -14,28 +14,25 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Unit tests for the quiz's own question bank view class. - * - * @package mod_quiz - * @category test - * @copyright 2018 the Open University - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace mod_quiz; + +use mod_quiz\question\bank\custom_view; +use question_edit_contexts; defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->dirroot . '/question/editlib.php'); - /** * Unit tests for the quiz's own question bank view class. * + * @package mod_quiz + * @category test * @copyright 2018 the Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class quiz_question_bank_view_testcase extends advanced_testcase { +class quiz_question_bank_view_test extends \advanced_testcase { public function test_viewing_question_bank_should_not_load_individual_questions() { $this->resetAfterTest(); @@ -47,7 +44,7 @@ public function test_viewing_question_bank_should_not_load_individual_questions( // Create a course and a quiz. $course = $generator->create_course(); $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id)); - $context = context_module::instance($quiz->cmid); + $context = \context_module::instance($quiz->cmid); $cm = get_coursemodule_from_instance('quiz', $quiz->id); // Create a question in the default category. @@ -57,11 +54,11 @@ public function test_viewing_question_bank_should_not_load_individual_questions( ['name' => 'Example question', 'category' => $cat->id]); // Ensure the question is not in the cache. - $cache = cache::make('core', 'questiondata'); + $cache = \cache::make('core', 'questiondata'); $cache->delete($questiondata->id); // Generate the view. - $view = new mod_quiz\question\bank\custom_view($contexts, new moodle_url('/'), $course, $cm, $quiz); + $view = new custom_view($contexts, new \moodle_url('/'), $course, $cm, $quiz); ob_start(); $view->display('editq', 0, 20, $cat->id . ',' . $cat->contextid, false, false, false); $html = ob_get_clean(); diff --git a/mod/quiz/tests/quizdisplayoptions_test.php b/mod/quiz/tests/quizdisplayoptions_test.php index e0b51b0076b42..c3714252ba6fb 100644 --- a/mod/quiz/tests/quizdisplayoptions_test.php +++ b/mod/quiz/tests/quizdisplayoptions_test.php @@ -14,31 +14,26 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Unit tests for the mod_quiz_display_options class. - * - * @package mod_quiz - * @category phpunit - * @copyright 2010 The Open University - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace mod_quiz; +use mod_quiz_display_options; defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->dirroot . '/mod/quiz/locallib.php'); - /** * Unit tests for {@link mod_quiz_display_options}. * + * @package mod_quiz + * @category test * @copyright 2010 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_quiz_display_options_testcase extends basic_testcase { +class quizdisplayoptions_test extends \basic_testcase { public function test_num_attempts_access_rule() { - $quiz = new stdClass(); + $quiz = new \stdClass(); $quiz->decimalpoints = 2; $quiz->questiondecimalpoints = -1; $quiz->reviewattempt = 0x11110; diff --git a/mod/quiz/tests/quizobj_test.php b/mod/quiz/tests/quizobj_test.php index e117b90b9eb31..fa141b2a4d8ae 100644 --- a/mod/quiz/tests/quizobj_test.php +++ b/mod/quiz/tests/quizobj_test.php @@ -14,38 +14,34 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Unit tests for the quiz class. - * - * @package mod_quiz - * @copyright 2008 The Open University - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace mod_quiz; +use mod_quiz_display_options; +use quiz; defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->dirroot . '/mod/quiz/locallib.php'); - /** * Unit tests for the quiz class * + * @package mod_quiz * @copyright 2008 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_quiz_class_testcase extends basic_testcase { +class quizobj_test extends \basic_testcase { public function test_cannot_review_message() { - $quiz = new stdClass(); + $quiz = new \stdClass(); $quiz->reviewattempt = 0x10010; $quiz->timeclose = 0; $quiz->attempts = 0; - $cm = new stdClass(); + $cm = new \stdClass(); $cm->id = 123; - $quizobj = new quiz($quiz, $cm, new stdClass(), false); + $quizobj = new quiz($quiz, $cm, new \stdClass(), false); $this->assertEquals('', $quizobj->cannot_review_message(mod_quiz_display_options::DURING)); @@ -58,7 +54,7 @@ public function test_cannot_review_message() { $closetime = time() + 10000; $quiz->timeclose = $closetime; - $quizobj = new quiz($quiz, $cm, new stdClass(), false); + $quizobj = new quiz($quiz, $cm, new \stdClass(), false); $this->assertEquals(get_string('noreviewuntil', 'quiz', userdate($closetime)), $quizobj->cannot_review_message(mod_quiz_display_options::LATER_WHILE_OPEN)); diff --git a/mod/quiz/tests/repaginate_test.php b/mod/quiz/tests/repaginate_test.php index 9623b5d9bbfb4..51422068c56af 100644 --- a/mod/quiz/tests/repaginate_test.php +++ b/mod/quiz/tests/repaginate_test.php @@ -22,19 +22,22 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +namespace mod_quiz; + +use quiz; + defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->dirroot . '/mod/quiz/locallib.php'); require_once($CFG->dirroot . '/mod/quiz/classes/repaginate.php'); - /** * Testable subclass, giving access to the protected methods of {@link \mod_quiz\repaginate} * @copyright 2014 The Open Univsersity * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_quiz_repaginate_testable extends \mod_quiz\repaginate { +class mod_quiz_repaginate_testable extends repaginate { public function __construct($quizid = 0, $slots = null) { return parent::__construct($quizid, $slots); @@ -61,7 +64,7 @@ public function repaginate_next_slot($nextslotnumber, $type) { * @copyright 2014 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_quiz_repaginate_test extends advanced_testcase { +class repaginate_test extends \advanced_testcase { /** @var array stores the slots. */ private $quizslots; @@ -111,7 +114,7 @@ private function get_quiz_object() { // Return the quiz object. $quizobj = new quiz($quiz, $cm, $SITE); - return \mod_quiz\structure::create_for_quiz($quizobj); + return structure::create_for_quiz($quizobj); } /** @@ -261,7 +264,7 @@ public function test_repaginate_this_slot() { public function test_repaginate_the_rest() { $this->set_quiz_slots(); $slotfrom = 1; - $type = \mod_quiz\repaginate::LINK; + $type = repaginate::LINK; $expected = array(); foreach ($this->quizslots as $slot) { if ($slot->slot > $slotfrom) { @@ -281,7 +284,7 @@ public function test_repaginate_the_rest() { $newslots[$s->id] = $s; } - $type = \mod_quiz\repaginate::UNLINK; + $type = repaginate::UNLINK; $expected = array(); foreach ($this->quizslots as $slot) { if ($slot->slot > ($slotfrom - 1)) { diff --git a/mod/quiz/tests/reportlib_test.php b/mod/quiz/tests/reportlib_test.php index 2cbe51d80bbfe..2c177f7def167 100644 --- a/mod/quiz/tests/reportlib_test.php +++ b/mod/quiz/tests/reportlib_test.php @@ -14,15 +14,9 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Unit tests for (some of) mod/quiz/report/reportlib.php - * - * @package mod_quiz - * @category phpunit - * @copyright 2008 Jamie Pratt me@jamiep.org - * @license http://www.gnu.org/copyleft/gpl.html GNU Public License - */ +namespace mod_quiz; +use quiz_attempt; defined('MOODLE_INTERNAL') || die(); @@ -33,13 +27,15 @@ /** * This class contains the test cases for the functions in reportlib.php. * + * @package mod_quiz + * @category test * @copyright 2008 Jamie Pratt me@jamiep.org * @license http://www.gnu.org/copyleft/gpl.html GNU Public License */ -class mod_quiz_reportlib_testcase extends advanced_testcase { +class reportlib_test extends \advanced_testcase { public function test_quiz_report_index_by_keys() { $datum = array(); - $object = new stdClass(); + $object = new \stdClass(); $object->qid = 3; $object->aid = 101; $object->response = ''; @@ -62,7 +58,7 @@ public function test_quiz_report_index_by_keys() { } public function test_quiz_report_scale_summarks_as_percentage() { - $quiz = new stdClass(); + $quiz = new \stdClass(); $quiz->sumgrades = 10; $quiz->decimalpoints = 2; @@ -75,13 +71,13 @@ public function test_quiz_report_scale_summarks_as_percentage() { } public function test_quiz_report_qm_filter_select_only_one_attempt_allowed() { - $quiz = new stdClass(); + $quiz = new \stdClass(); $quiz->attempts = 1; $this->assertSame('', quiz_report_qm_filter_select($quiz)); } public function test_quiz_report_qm_filter_select_average() { - $quiz = new stdClass(); + $quiz = new \stdClass(); $quiz->attempts = 10; $quiz->grademethod = QUIZ_GRADEAVERAGE; $this->assertSame('', quiz_report_qm_filter_select($quiz)); @@ -91,7 +87,7 @@ public function test_quiz_report_qm_filter_select_first_last_best() { global $DB; $this->resetAfterTest(); - $fakeattempt = new stdClass(); + $fakeattempt = new \stdClass(); $fakeattempt->userid = 123; $fakeattempt->quiz = 456; $fakeattempt->layout = '1,2,0,3,4,0,5'; @@ -137,7 +133,7 @@ public function test_quiz_report_qm_filter_select_first_last_best() { $fakeattempt->uniqueid = 65; $DB->insert_record('quiz_attempts', $fakeattempt); - $quiz = new stdClass(); + $quiz = new \stdClass(); $quiz->attempts = 10; $quiz->grademethod = QUIZ_ATTEMPTFIRST; diff --git a/mod/quiz/tests/restore_override_test.php b/mod/quiz/tests/restore_override_test.php index 7836a9942ecc9..6234639c56144 100644 --- a/mod/quiz/tests/restore_override_test.php +++ b/mod/quiz/tests/restore_override_test.php @@ -14,14 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Restore override tests. - * - * @package mod_quiz - * @author 2019 Nathan Nguyen - * @copyright Catalyst IT - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace mod_quiz; defined('MOODLE_INTERNAL') || die(); @@ -30,12 +23,12 @@ /** * Restore override tests. * - * @package mod_quiz - * @author 2019 Nathan Nguyen + * @package mod_quiz + * @author 2019 Nathan Nguyen * @copyright Catalyst IT - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_quiz_restore_override_testcase extends restore_date_testcase { +class restore_override_test extends \restore_date_testcase { /** * Test restore overrides. diff --git a/mod/quiz/tests/structure_test.php b/mod/quiz/tests/structure_test.php index eb4fe10303807..fc0f803f48748 100644 --- a/mod/quiz/tests/structure_test.php +++ b/mod/quiz/tests/structure_test.php @@ -14,14 +14,9 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Quiz events tests. - * - * @package mod_quiz - * @category test - * @copyright 2013 Adrian Greeve - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace mod_quiz; + +use quiz; defined('MOODLE_INTERNAL') || die(); @@ -31,10 +26,12 @@ /** * Unit tests for quiz events. * - * @copyright 2013 Adrian Greeve - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package mod_quiz + * @category test + * @copyright 2013 Adrian Greeve + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_quiz_structure_testcase extends advanced_testcase { +class structure_test extends \advanced_testcase { /** * Create a course with an empty quiz. @@ -89,7 +86,7 @@ protected function create_test_quiz($layout) { foreach ($layout as $item) { if (is_string($item)) { if (isset($headings[$lastpage + 1])) { - throw new coding_exception('Sections cannot be empty.'); + throw new \coding_exception('Sections cannot be empty.'); } $headings[$lastpage + 1] = $item; @@ -97,7 +94,7 @@ protected function create_test_quiz($layout) { list($name, $page, $qtype) = $item; if ($page < 1 || !($page == $lastpage + 1 || (!isset($headings[$lastpage + 1]) && $page == $lastpage))) { - throw new coding_exception('Page numbers wrong.'); + throw new \coding_exception('Page numbers wrong.'); } $q = $questiongenerator->create_question($qtype, null, array('name' => $name, 'category' => $cat->id)); @@ -108,7 +105,7 @@ protected function create_test_quiz($layout) { } $quizobj = new quiz($quiz, $cm, $course); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); if (isset($headings[1])) { list($heading, $shuffle) = $this->parse_section_name($headings[1]); $sections = $structure->get_sections(); @@ -132,7 +129,7 @@ protected function create_test_quiz($layout) { * @param array $expectedlayout as for $layout in {@link create_test_quiz()}. * @param \mod_quiz\structure $structure the structure to test. */ - protected function assert_quiz_layout($expectedlayout, \mod_quiz\structure $structure) { + protected function assert_quiz_layout($expectedlayout, structure $structure) { $sections = $structure->get_sections(); $slot = 1; @@ -193,7 +190,7 @@ public function test_get_quiz_slots() { array('TF1', 1, 'truefalse'), array('TF2', 1, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); // Are the correct slots returned? $slots = $structure->get_slots(); @@ -204,7 +201,7 @@ public function test_quiz_has_one_section_by_default() { $quizobj = $this->create_test_quiz(array( array('TF1', 1, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $sections = $structure->get_sections(); $this->assertCount(1, $sections); @@ -222,7 +219,7 @@ public function test_get_sections() { 'Heading 2*', array('TF2', 2, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $sections = $structure->get_sections(); $this->assertCount(2, $sections); @@ -245,13 +242,13 @@ public function test_remove_section_heading() { 'Heading 2', array('TF2', 2, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $sections = $structure->get_sections(); $section = end($sections); $structure->remove_section_heading($section->id); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $this->assert_quiz_layout(array( 'Heading 1', array('TF1', 1, 'truefalse'), @@ -264,12 +261,12 @@ public function test_cannot_remove_first_section() { 'Heading 1', array('TF1', 1, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $sections = $structure->get_sections(); $section = reset($sections); - $this->expectException(coding_exception::class); + $this->expectException(\coding_exception::class); $structure->remove_section_heading($section->id); } @@ -279,13 +276,13 @@ public function test_move_slot_to_the_same_place_does_nothing() { array('TF2', 1, 'truefalse'), array('TF3', 2, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $idtomove = $structure->get_question_in_slot(2)->slotid; $idmoveafter = $structure->get_question_in_slot(1)->slotid; $structure->move_slot($idtomove, $idmoveafter, '1'); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $this->assert_quiz_layout(array( array('TF1', 1, 'truefalse'), array('TF2', 1, 'truefalse'), @@ -299,13 +296,13 @@ public function test_move_slot_end_of_one_page_to_start_of_next() { array('TF2', 1, 'truefalse'), array('TF3', 2, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $idtomove = $structure->get_question_in_slot(2)->slotid; $idmoveafter = $structure->get_question_in_slot(2)->slotid; $structure->move_slot($idtomove, $idmoveafter, '2'); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $this->assert_quiz_layout(array( array('TF1', 1, 'truefalse'), array('TF2', 2, 'truefalse'), @@ -318,13 +315,13 @@ public function test_move_last_slot_to_previous_page_emptying_the_last_page() { array('TF1', 1, 'truefalse'), array('TF2', 2, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $idtomove = $structure->get_question_in_slot(2)->slotid; $idmoveafter = $structure->get_question_in_slot(1)->slotid; $structure->move_slot($idtomove, $idmoveafter, '1'); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $this->assert_quiz_layout(array( array('TF1', 1, 'truefalse'), array('TF2', 1, 'truefalse'), @@ -338,13 +335,13 @@ public function test_end_of_one_section_to_start_of_next() { 'Heading', array('TF3', 2, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $idtomove = $structure->get_question_in_slot(2)->slotid; $idmoveafter = $structure->get_question_in_slot(2)->slotid; $structure->move_slot($idtomove, $idmoveafter, '2'); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $this->assert_quiz_layout(array( array('TF1', 1, 'truefalse'), 'Heading', @@ -360,13 +357,13 @@ public function test_start_of_one_section_to_end_of_previous() { array('TF2', 2, 'truefalse'), array('TF3', 2, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $idtomove = $structure->get_question_in_slot(2)->slotid; $idmoveafter = $structure->get_question_in_slot(1)->slotid; $structure->move_slot($idtomove, $idmoveafter, '1'); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $this->assert_quiz_layout(array( array('TF1', 1, 'truefalse'), array('TF2', 1, 'truefalse'), @@ -380,13 +377,13 @@ public function test_move_slot_on_same_page() { array('TF2', 1, 'truefalse'), array('TF3', 1, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $idtomove = $structure->get_question_in_slot(2)->slotid; $idmoveafter = $structure->get_question_in_slot(3)->slotid; $structure->move_slot($idtomove, $idmoveafter, '1'); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $this->assert_quiz_layout(array( array('TF1', 1, 'truefalse'), array('TF3', 1, 'truefalse'), @@ -400,13 +397,13 @@ public function test_move_slot_up_onto_previous_page() { array('TF2', 2, 'truefalse'), array('TF3', 2, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $idtomove = $structure->get_question_in_slot(3)->slotid; $idmoveafter = $structure->get_question_in_slot(1)->slotid; $structure->move_slot($idtomove, $idmoveafter, '1'); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $this->assert_quiz_layout(array( array('TF1', 1, 'truefalse'), array('TF3', 1, 'truefalse'), @@ -420,13 +417,13 @@ public function test_move_slot_emptying_a_page_renumbers_pages() { array('TF2', 2, 'truefalse'), array('TF3', 3, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $idtomove = $structure->get_question_in_slot(2)->slotid; $idmoveafter = $structure->get_question_in_slot(3)->slotid; $structure->move_slot($idtomove, $idmoveafter, '3'); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $this->assert_quiz_layout(array( array('TF1', 1, 'truefalse'), array('TF3', 2, 'truefalse'), @@ -440,11 +437,11 @@ public function test_move_slot_too_small_page_number_detected() { array('TF2', 2, 'truefalse'), array('TF3', 3, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $idtomove = $structure->get_question_in_slot(3)->slotid; $idmoveafter = $structure->get_question_in_slot(2)->slotid; - $this->expectException(coding_exception::class); + $this->expectException(\coding_exception::class); $structure->move_slot($idtomove, $idmoveafter, '1'); } @@ -454,11 +451,11 @@ public function test_move_slot_too_large_page_number_detected() { array('TF2', 2, 'truefalse'), array('TF3', 3, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $idtomove = $structure->get_question_in_slot(1)->slotid; $idmoveafter = $structure->get_question_in_slot(2)->slotid; - $this->expectException(coding_exception::class); + $this->expectException(\coding_exception::class); $structure->move_slot($idtomove, $idmoveafter, '4'); } @@ -470,13 +467,13 @@ public function test_move_slot_within_section() { 'Heading 2', array('TF3', 2, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $idtomove = $structure->get_question_in_slot(1)->slotid; $idmoveafter = $structure->get_question_in_slot(2)->slotid; $structure->move_slot($idtomove, $idmoveafter, '1'); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $this->assert_quiz_layout(array( 'Heading 1', array('TF2', 1, 'truefalse'), @@ -494,13 +491,13 @@ public function test_move_slot_to_new_section() { 'Heading 2', array('TF3', 2, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $idtomove = $structure->get_question_in_slot(2)->slotid; $idmoveafter = $structure->get_question_in_slot(3)->slotid; $structure->move_slot($idtomove, $idmoveafter, '2'); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $this->assert_quiz_layout(array( 'Heading 1', array('TF1', 1, 'truefalse'), @@ -518,12 +515,12 @@ public function test_move_slot_to_start() { array('TF2', 2, 'truefalse'), array('TF3', 2, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $idtomove = $structure->get_question_in_slot(3)->slotid; $structure->move_slot($idtomove, 0, '1'); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $this->assert_quiz_layout(array( 'Heading 1', array('TF3', 1, 'truefalse'), @@ -541,13 +538,13 @@ public function test_move_slot_down_to_start_of_second_section() { 'Heading 2', array('TF3', 2, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $idtomove = $structure->get_question_in_slot(2)->slotid; $idmoveafter = $structure->get_question_in_slot(2)->slotid; $structure->move_slot($idtomove, $idmoveafter, '2'); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $this->assert_quiz_layout(array( 'Heading 1', array('TF1', 1, 'truefalse'), @@ -563,12 +560,12 @@ public function test_move_first_slot_down_to_start_of_page_2() { array('TF1', 1, 'truefalse'), array('TF2', 2, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $idtomove = $structure->get_question_in_slot(1)->slotid; $structure->move_slot($idtomove, 0, '2'); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $this->assert_quiz_layout(array( 'Heading 1', array('TF1', 1, 'truefalse'), @@ -582,12 +579,12 @@ public function test_move_first_slot_to_same_place_on_page_1() { array('TF1', 1, 'truefalse'), array('TF2', 2, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $idtomove = $structure->get_question_in_slot(1)->slotid; $structure->move_slot($idtomove, 0, '1'); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $this->assert_quiz_layout(array( 'Heading 1', array('TF1', 1, 'truefalse'), @@ -601,12 +598,12 @@ public function test_move_first_slot_to_before_page_1() { array('TF1', 1, 'truefalse'), array('TF2', 2, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $idtomove = $structure->get_question_in_slot(1)->slotid; $structure->move_slot($idtomove, 0, ''); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $this->assert_quiz_layout(array( 'Heading 1', array('TF1', 1, 'truefalse'), @@ -624,13 +621,13 @@ public function test_move_slot_up_to_start_of_second_section() { array('TF3', 3, 'truefalse'), array('TF4', 3, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $idtomove = $structure->get_question_in_slot(3)->slotid; $idmoveafter = $structure->get_question_in_slot(1)->slotid; $structure->move_slot($idtomove, $idmoveafter, '2'); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $this->assert_quiz_layout(array( 'Heading 1', array('TF1', 1, 'truefalse'), @@ -652,13 +649,13 @@ public function test_move_slot_does_not_violate_heading_unique_key() { array('TF3', 3, 'truefalse'), array('TF4', 3, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $idtomove = $structure->get_question_in_slot(4)->slotid; $idmoveafter = $structure->get_question_in_slot(1)->slotid; $structure->move_slot($idtomove, $idmoveafter, 1); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $this->assert_quiz_layout(array( 'Heading 1', array('TF1', 1, 'truefalse'), @@ -677,11 +674,11 @@ public function test_quiz_remove_slot() { 'Heading 2', array('TF3', 2, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $structure->remove_slot(2); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $this->assert_quiz_layout(array( array('TF1', 1, 'truefalse'), 'Heading 2', @@ -702,12 +699,12 @@ public function test_quiz_removing_a_random_question_deletes_the_question() { $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); $cat = $questiongenerator->create_question_category(); quiz_add_random_questions($quizobj->get_quiz(), 1, $cat->id, 1, false); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $randomq = $DB->get_record('question', array('qtype' => 'random')); $structure->remove_slot(2); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $this->assert_quiz_layout(array( array('TF1', 1, 'truefalse'), ), $structure); @@ -724,10 +721,10 @@ public function test_cannot_remove_all_slots_in_a_section() { 'Heading 2', array('TF3', 2, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $structure->remove_slot(1); - $this->expectException(coding_exception::class); + $this->expectException(\coding_exception::class); $structure->remove_slot(2); } @@ -738,9 +735,9 @@ public function test_cannot_remove_last_slot_in_a_section() { 'Heading 2', array('TF3', 2, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); - $this->expectException(coding_exception::class); + $this->expectException(\coding_exception::class); $structure->remove_slot(3); } @@ -749,7 +746,7 @@ public function test_can_remove_last_question_in_a_quiz() { 'Heading 1', array('TF1', 1, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $structure->remove_slot(1); @@ -759,7 +756,7 @@ public function test_can_remove_last_question_in_a_quiz() { array('name' => 'TF2', 'category' => $cat->id)); quiz_add_quiz_question($q->id, $quizobj->get_quiz(), 0); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $this->assert_quiz_layout(array( 'Heading 1', @@ -781,7 +778,7 @@ public function test_add_question_updates_headings() { quiz_add_quiz_question($q->id, $quizobj->get_quiz(), 1); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $this->assert_quiz_layout(array( array('TF1', 1, 'truefalse'), array('TF3', 1, 'truefalse'), @@ -807,7 +804,7 @@ public function test_add_question_updates_headings_even_with_one_question_sectio quiz_add_quiz_question($q->id, $quizobj->get_quiz(), 1); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $this->assert_quiz_layout(array( 'Heading 1', array('TF1', 1, 'truefalse'), @@ -833,7 +830,7 @@ public function test_add_question_at_end_does_not_update_headings() { quiz_add_quiz_question($q->id, $quizobj->get_quiz(), 0); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $this->assert_quiz_layout(array( array('TF1', 1, 'truefalse'), 'Heading 2', @@ -847,12 +844,12 @@ public function test_remove_page_break() { array('TF1', 1, 'truefalse'), array('TF2', 2, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $slotid = $structure->get_question_in_slot(2)->slotid; - $slots = $structure->update_page_break($slotid, \mod_quiz\repaginate::LINK); + $slots = $structure->update_page_break($slotid, repaginate::LINK); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $this->assert_quiz_layout(array( array('TF1', 1, 'truefalse'), array('TF2', 1, 'truefalse'), @@ -864,12 +861,12 @@ public function test_add_page_break() { array('TF1', 1, 'truefalse'), array('TF2', 1, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $slotid = $structure->get_question_in_slot(2)->slotid; - $slots = $structure->update_page_break($slotid, \mod_quiz\repaginate::UNLINK); + $slots = $structure->update_page_break($slotid, repaginate::UNLINK); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $this->assert_quiz_layout(array( array('TF1', 1, 'truefalse'), array('TF2', 2, 'truefalse'), @@ -881,21 +878,21 @@ public function test_update_question_dependency() { array('TF1', 1, 'truefalse'), array('TF2', 1, 'truefalse'), )); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); // Test adding a dependency. $slotid = $structure->get_slot_id_for_slot(2); $structure->update_question_dependency($slotid, true); // Having called update page break, we need to reload $structure. - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $this->assertEquals(1, $structure->is_question_dependent_on_previous_slot(2)); // Test removing a dependency. $structure->update_question_dependency($slotid, false); // Having called update page break, we need to reload $structure. - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); $this->assertEquals(0, $structure->is_question_dependent_on_previous_slot(2)); } @@ -1003,8 +1000,8 @@ public function test_get_slot_tags_for_slot($layout, $tagnames, $slotnumber, $ex $this->resetAfterTest(); $quiz = $this->create_test_quiz($layout); - $structure = \mod_quiz\structure::create_for_quiz($quiz); - $collid = core_tag_area::get_collection('core', 'question'); + $structure = structure::create_for_quiz($quiz); + $collid = \core_tag_area::get_collection('core', 'question'); $slottagrecords = []; if (is_null($slotnumber)) { @@ -1019,7 +1016,7 @@ public function test_get_slot_tags_for_slot($layout, $tagnames, $slotnumber, $ex foreach ($tagnames as $index => $slottagnames) { $tagslotnumber = $index + 1; $tagslotid = $structure->get_slot_id_for_slot($tagslotnumber); - $tags = core_tag_tag::create_if_missing($collid, $slottagnames); + $tags = \core_tag_tag::create_if_missing($collid, $slottagnames); $records = array_map(function($tag) use ($tagslotid) { return (object) [ 'slotid' => $tagslotid, @@ -1057,11 +1054,11 @@ public function test_can_add_random_questions() { $noneditingteacher = $generator->create_and_enrol($course, 'teacher'); $this->setUser($teacher); - $structure = \mod_quiz\structure::create_for_quiz($quiz); + $structure = structure::create_for_quiz($quiz); $this->assertTrue($structure->can_add_random_questions()); $this->setUser($noneditingteacher); - $structure = \mod_quiz\structure::create_for_quiz($quiz); + $structure = structure::create_for_quiz($quiz); $this->assertFalse($structure->can_add_random_questions()); } } diff --git a/mod/quiz/tests/tags_test.php b/mod/quiz/tests/tags_test.php index 7781f00ff74c9..9523a9eda88b9 100644 --- a/mod/quiz/tests/tags_test.php +++ b/mod/quiz/tests/tags_test.php @@ -14,24 +14,21 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Unit tests for usage of tags in quizzes. - * - * @package mod_quiz - * @copyright 2018 Shamim Rezaie - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace mod_quiz; -defined('MOODLE_INTERNAL') || die(); +use mod_quiz\question\bank\qbank_helper; +use quiz; /** * Class mod_quiz_tags_testcase * Class for tests related to usage of question tags in quizzes. * + * @package mod_quiz + * @category test * @copyright 2018 Shamim Rezaie * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_quiz_tags_testcase extends advanced_testcase { +class tags_test extends \advanced_testcase { public function test_restore_random_question_by_tag() { global $CFG, $USER, $DB; @@ -48,9 +45,9 @@ public function test_restore_random_question_by_tag() { // Do the restore to new course with default settings. $categoryid = $DB->get_field_sql("SELECT MIN(id) FROM {course_categories}"); - $newcourseid = restore_dbops::create_new_course('Test fullname', 'Test shortname', $categoryid); - $rc = new restore_controller($backupid, $newcourseid, backup::INTERACTIVE_NO, backup::MODE_GENERAL, $USER->id, - backup::TARGET_NEW_COURSE); + $newcourseid = \restore_dbops::create_new_course('Test fullname', 'Test shortname', $categoryid); + $rc = new \restore_controller($backupid, $newcourseid, \backup::INTERACTIVE_NO, \backup::MODE_GENERAL, $USER->id, + \backup::TARGET_NEW_COURSE); $this->assertTrue($rc->execute_precheck()); $rc->execute_plan(); @@ -60,7 +57,7 @@ public function test_restore_random_question_by_tag() { $modinfo = get_fast_modinfo($newcourseid); $quiz = array_values($modinfo->get_instances_of('quiz'))[0]; $quizobj = quiz::create($quiz->instance); - $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $structure = structure::create_for_quiz($quizobj); // Are the correct slots returned? $slots = $structure->get_slots(); @@ -74,13 +71,13 @@ public function test_restore_random_question_by_tag() { $question = array_values($questions)[0]; - $tag1 = core_tag_tag::get_by_name(0, 't1', 'id, name'); + $tag1 = \core_tag_tag::get_by_name(0, 't1', 'id, name'); $this->assertNotFalse($tag1); - $tag2 = core_tag_tag::get_by_name(0, 't2', 'id, name'); + $tag2 = \core_tag_tag::get_by_name(0, 't2', 'id, name'); $this->assertNotFalse($tag2); - $tag3 = core_tag_tag::get_by_name(0, 't3', 'id, name'); + $tag3 = \core_tag_tag::get_by_name(0, 't3', 'id, name'); $this->assertNotFalse($tag3); $slottags = quiz_retrieve_slot_tags($question->slotid); @@ -93,7 +90,7 @@ public function test_restore_random_question_by_tag() { }, $slottags) ); - $defaultcategory = question_get_default_category(context_course::instance($newcourseid)->id); + $defaultcategory = question_get_default_category(\context_course::instance($newcourseid)->id); $this->assertEquals($defaultcategory->id, $question->randomfromcategory); $this->assertEquals(0, $question->randomincludingsubcategories); }