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.
This commit is contained in:
Eloy Lafuente (stronk7) 2022-09-11 19:34:26 +02:00
parent c6211a68b4
commit 81f05b3239
39 changed files with 1007 additions and 1119 deletions

View file

@ -14,14 +14,7 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** namespace core;
* 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
*/
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
@ -29,22 +22,28 @@ global $CFG;
require_once($CFG->libdir . '/pagelib.php'); require_once($CFG->libdir . '/pagelib.php');
require_once($CFG->libdir . '/blocklib.php'); require_once($CFG->libdir . '/blocklib.php');
require_once($CFG->dirroot . '/blocks/moodleblock.class.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 $testpage;
protected $blockmanager; protected $blockmanager;
protected $isediting = null; protected $isediting = null;
protected function setUp(): void { protected function setUp(): void {
parent::setUp(); parent::setUp();
$this->testpage = new moodle_page(); $this->testpage = new \moodle_page();
$this->testpage->set_context(context_system::instance()); $this->testpage->set_context(\context_system::instance());
$this->testpage->set_pagetype('phpunit-block-test'); $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 { protected function tearDown(): void {
@ -113,7 +112,7 @@ class core_blocklib_testcase extends advanced_testcase {
// Set up fixture. // Set up fixture.
$this->blockmanager->mark_loaded(); $this->blockmanager->mark_loaded();
// Exercise SUT. // Exercise SUT.
$this->expectException(coding_exception::class); $this->expectException(\coding_exception::class);
$this->blockmanager->add_region('too-late', false); $this->blockmanager->add_region('too-late', false);
} }
@ -168,7 +167,7 @@ class core_blocklib_testcase extends advanced_testcase {
// Set up fixture. // Set up fixture.
$this->blockmanager->mark_loaded(); $this->blockmanager->mark_loaded();
// Exercise SUT. // Exercise SUT.
$this->expectException(coding_exception::class); $this->expectException(\coding_exception::class);
$this->blockmanager->add_region('too-late'); $this->blockmanager->add_region('too-late');
} }
@ -183,7 +182,7 @@ class core_blocklib_testcase extends advanced_testcase {
public function test_cannot_set_unknown_region_as_default() { public function test_cannot_set_unknown_region_as_default() {
// Exercise SUT. // Exercise SUT.
$this->expectException(coding_exception::class); $this->expectException(\coding_exception::class);
$this->blockmanager->set_default_region('a-region-name'); $this->blockmanager->set_default_region('a-region-name');
} }
@ -191,7 +190,7 @@ class core_blocklib_testcase extends advanced_testcase {
// Set up fixture. // Set up fixture.
$this->blockmanager->mark_loaded(); $this->blockmanager->mark_loaded();
// Exercise SUT. // Exercise SUT.
$this->expectException(coding_exception::class); $this->expectException(\coding_exception::class);
$this->blockmanager->set_default_region('too-late'); $this->blockmanager->set_default_region('too-late');
} }
@ -210,13 +209,13 @@ class core_blocklib_testcase extends advanced_testcase {
} }
protected function get_a_page_and_block_manager($regions, $context, $pagetype, $subpage = '') { 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_context($context);
$page->set_pagetype($pagetype); $page->set_pagetype($pagetype);
$page->set_subpage($subpage); $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->add_regions($regions, false);
$blockmanager->set_default_region($regions[0]); $blockmanager->set_default_region($regions[0]);
@ -225,7 +224,7 @@ class core_blocklib_testcase extends advanced_testcase {
protected function get_a_known_block_type() { protected function get_a_known_block_type() {
global $DB; global $DB;
$block = new stdClass; $block = new \stdClass;
$block->name = 'ablocktype'; $block->name = 'ablocktype';
$DB->insert_record('block', $block); $DB->insert_record('block', $block);
return $block->name; return $block->name;
@ -249,7 +248,7 @@ class core_blocklib_testcase extends advanced_testcase {
// Set up fixture. // Set up fixture.
list($page, $blockmanager) = $this->get_a_page_and_block_manager(array('a-region'), 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. // Exercise SUT.
$blockmanager->load_blocks(); $blockmanager->load_blocks();
// Validate. // Validate.
@ -263,7 +262,7 @@ class core_blocklib_testcase extends advanced_testcase {
// Set up fixture. // Set up fixture.
$regionname = 'a-region'; $regionname = 'a-region';
$blockname = $this->get_a_known_block_type(); $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), list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
$context, 'page-type'); $context, 'page-type');
@ -282,7 +281,7 @@ class core_blocklib_testcase extends advanced_testcase {
// Set up fixture. // Set up fixture.
$regionname = 'a-region'; $regionname = 'a-region';
$blockname = $this->get_a_known_block_type(); $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), list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
$context, 'page-type'); $context, 'page-type');
@ -302,7 +301,7 @@ class core_blocklib_testcase extends advanced_testcase {
// Set up fixture. // Set up fixture.
$regionname = 'a-region'; $regionname = 'a-region';
$blockname = $this->get_a_known_block_type(); $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), list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
$context, 'page-type'); $context, 'page-type');
@ -327,7 +326,7 @@ class core_blocklib_testcase extends advanced_testcase {
// Set up fixture. // Set up fixture.
$regionname = 'a-region'; $regionname = 'a-region';
$blockname = $this->get_a_known_block_type(); $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), list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
$context, 'page-type'); $context, 'page-type');
@ -345,9 +344,9 @@ class core_blocklib_testcase extends advanced_testcase {
$this->purge_blocks(); $this->purge_blocks();
// Set up fixture. // Set up fixture.
$syscontext = context_system::instance(); $syscontext = \context_system::instance();
$cat = $this->getDataGenerator()->create_category(array('name' => 'testcategory')); $cat = $this->getDataGenerator()->create_category(array('name' => 'testcategory'));
$fakecontext = context_coursecat::instance($cat->id); $fakecontext = \context_coursecat::instance($cat->id);
$regionname = 'a-region'; $regionname = 'a-region';
$blockname = $this->get_a_known_block_type(); $blockname = $this->get_a_known_block_type();
@ -367,8 +366,8 @@ class core_blocklib_testcase extends advanced_testcase {
$this->purge_blocks(); $this->purge_blocks();
// Set up fixture. // Set up fixture.
$syscontext = context_system::instance(); $syscontext = \context_system::instance();
$childcontext = context_coursecat::instance(1); $childcontext = \context_coursecat::instance(1);
$regionname = 'a-region'; $regionname = 'a-region';
$blockname = $this->get_a_known_block_type(); $blockname = $this->get_a_known_block_type();
@ -388,7 +387,7 @@ class core_blocklib_testcase extends advanced_testcase {
$this->purge_blocks(); $this->purge_blocks();
// Set up fixture. // Set up fixture.
$syscontext = context_system::instance(); $syscontext = \context_system::instance();
$regionname = 'a-region'; $regionname = 'a-region';
$blockname = $this->get_a_known_block_type(); $blockname = $this->get_a_known_block_type();
@ -410,7 +409,7 @@ class core_blocklib_testcase extends advanced_testcase {
// Set up fixture. // Set up fixture.
$regionname = 'a-region'; $regionname = 'a-region';
$blockname = $this->get_a_known_block_type(); $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), list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
$syscontext, 'page-type', 'sub-page'); $syscontext, 'page-type', 'sub-page');
@ -430,7 +429,7 @@ class core_blocklib_testcase extends advanced_testcase {
// Set up fixture. // Set up fixture.
$regionname = 'a-region'; $regionname = 'a-region';
$blockname = $this->get_a_known_block_type(); $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), list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
$syscontext, 'page-type', 'sub-page'); $syscontext, 'page-type', 'sub-page');
@ -450,7 +449,7 @@ class core_blocklib_testcase extends advanced_testcase {
// Set up fixture. // Set up fixture.
$regionname = 'a-region'; $regionname = 'a-region';
$blockname = $this->get_a_known_block_type(); $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), list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
$syscontext, 'page-type', 'sub-page'); $syscontext, 'page-type', 'sub-page');
@ -521,7 +520,7 @@ class core_blocklib_testcase extends advanced_testcase {
$regionname = 'a-region'; $regionname = 'a-region';
$blockname = $this->get_a_known_block_type(); $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), list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
$context, 'page-type'); $context, 'page-type');
@ -572,9 +571,9 @@ class core_blocklib_testcase extends advanced_testcase {
list($insql, $inparams) = $DB->get_in_or_equal($preferences); list($insql, $inparams) = $DB->get_in_or_equal($preferences);
$this->assertEquals(2, $DB->count_records_select('user_preferences', "name $insql", $inparams)); $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[0], IGNORE_MISSING));
$this->assertFalse(context_block::instance($blockids[1], IGNORE_MISSING)); $this->assertFalse(\context_block::instance($blockids[1], IGNORE_MISSING));
context_block::instance($tokeep); // Would throw an exception if it was deleted. \context_block::instance($tokeep); // Would throw an exception if it was deleted.
} }
public function test_create_all_block_instances() { public function test_create_all_block_instances() {
@ -583,7 +582,7 @@ class core_blocklib_testcase extends advanced_testcase {
$this->setAdminUser(); $this->setAdminUser();
$this->resetAfterTest(); $this->resetAfterTest();
$regionname = 'side-pre'; $regionname = 'side-pre';
$context = context_system::instance(); $context = \context_system::instance();
$PAGE->reset_theme_and_output(); $PAGE->reset_theme_and_output();
$CFG->theme = 'boost'; $CFG->theme = 'boost';
@ -668,7 +667,7 @@ class core_blocklib_testcase extends advanced_testcase {
// Set up fixture. // Set up fixture.
$regionname = 'a-region'; $regionname = 'a-region';
$blockname = 'html'; $blockname = 'html';
$context = context_system::instance(); $context = \context_system::instance();
list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname), list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
$context, 'page-type'); $context, 'page-type');
@ -735,8 +734,8 @@ class core_blocklib_testcase extends advanced_testcase {
$regionname = 'a-region'; $regionname = 'a-region';
$blockname = $this->get_a_known_block_type(); $blockname = $this->get_a_known_block_type();
$user = self::getDataGenerator()->create_user(); $user = self::getDataGenerator()->create_user();
$syscontext = context_system::instance(); $syscontext = \context_system::instance();
$usercontext = context_user::instance($user->id); $usercontext = \context_user::instance($user->id);
// Add sitewide 'sticky' blocks. The page is not setup exactly as a site page would be... // 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. // 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'); list($sitepage, $sitebm) = $this->get_a_page_and_block_manager(array($regionname), $syscontext, 'site-index');
@ -772,7 +771,7 @@ class core_blocklib_testcase extends advanced_testcase {
$mybm->load_blocks(); $mybm->load_blocks();
$mybm->reposition_block($sitestickyblock2->id, $regionname, -1); $mybm->reposition_block($sitestickyblock2->id, $regionname, -1);
// Reload the blocks in the managers. // Reload the blocks in the managers.
context_helper::reset_caches(); \context_helper::reset_caches();
$defaultmybm->reset_caches(); $defaultmybm->reset_caches();
$this->assertNull($defaultmybm->get_loaded_blocks()); $this->assertNull($defaultmybm->get_loaded_blocks());
$defaultmybm->load_blocks(); $defaultmybm->load_blocks();
@ -838,7 +837,7 @@ class core_blocklib_testcase extends advanced_testcase {
$this->setAdminUser(); $this->setAdminUser();
$this->resetAfterTest(); $this->resetAfterTest();
$regionname = 'side-pre'; $regionname = 'side-pre';
$context = context_system::instance(); $context = \context_system::instance();
$PAGE->reset_theme_and_output(); $PAGE->reset_theme_and_output();
$CFG->theme = 'boost'; $CFG->theme = 'boost';
@ -864,32 +863,3 @@ class core_blocklib_testcase extends advanced_testcase {
$this->assertEmpty($blocks); $this->assertEmpty($blocks);
} }
} }
/**
* 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() {
}
}

28
lib/tests/fixtures/block_ablocktype.php vendored Normal file
View file

@ -0,0 +1,28 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* 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() {
}
}

View file

@ -0,0 +1,41 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* 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;
}
}

File diff suppressed because it is too large Load diff

View file

@ -14,15 +14,13 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** namespace core;
* 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.
*/
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\*). * Tests for oauth2 apis (\core\oauth2\*).
@ -32,7 +30,7 @@ defined('MOODLE_INTERNAL') || die();
* @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.
* @coversDefaultClass \core\oauth2\api * @coversDefaultClass \core\oauth2\api
*/ */
class core_oauth2_testcase extends advanced_testcase { class oauth2_test extends \advanced_testcase {
/** /**
* Tests the crud operations on oauth2 issuers. * 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() { public function test_create_and_delete_standard_issuers() {
$this->resetAfterTest(); $this->resetAfterTest();
$this->setAdminUser(); $this->setAdminUser();
\core\oauth2\api::create_standard_issuer('google'); api::create_standard_issuer('google');
\core\oauth2\api::create_standard_issuer('facebook'); api::create_standard_issuer('facebook');
\core\oauth2\api::create_standard_issuer('microsoft'); api::create_standard_issuer('microsoft');
\core\oauth2\api::create_standard_issuer('nextcloud', 'https://dummy.local/nextcloud/'); 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[0]->get('name'), 'Google');
$this->assertEquals($issuers[1]->get('name'), 'Facebook'); $this->assertEquals($issuers[1]->get('name'), 'Facebook');
$this->assertEquals($issuers[2]->get('name'), 'Microsoft'); $this->assertEquals($issuers[2]->get('name'), 'Microsoft');
$this->assertEquals($issuers[3]->get('name'), 'Nextcloud'); $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[0]->get('name'), 'Facebook');
$this->assertEquals($issuers[1]->get('name'), 'Google'); $this->assertEquals($issuers[1]->get('name'), 'Google');
$this->assertEquals($issuers[2]->get('name'), 'Microsoft'); $this->assertEquals($issuers[2]->get('name'), 'Microsoft');
$this->assertEquals($issuers[3]->get('name'), 'Nextcloud'); $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[0]->get('name'), 'Facebook');
$this->assertEquals($issuers[1]->get('name'), 'Microsoft'); $this->assertEquals($issuers[1]->get('name'), 'Microsoft');
@ -78,7 +76,7 @@ class core_oauth2_testcase extends advanced_testcase {
$this->setAdminUser(); $this->setAdminUser();
$this->expectException(\moodle_exception::class); $this->expectException(\moodle_exception::class);
\core\oauth2\api::create_standard_issuer('nextcloud'); api::create_standard_issuer('nextcloud');
} }
/** /**
@ -87,31 +85,31 @@ class core_oauth2_testcase extends advanced_testcase {
public function test_getters() { public function test_getters() {
$this->resetAfterTest(); $this->resetAfterTest();
$this->setAdminUser(); $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) { foreach ($same->properties_definition() as $name => $def) {
$this->assertTrue($issuer->get($name) == $same->get($name)); $this->assertTrue($issuer->get($name) == $same->get($name));
} }
$endpoints = \core\oauth2\api::get_endpoints($issuer); $endpoints = api::get_endpoints($issuer);
$same = \core\oauth2\api::get_endpoint($endpoints[0]->get('id')); $same = api::get_endpoint($endpoints[0]->get('id'));
$this->assertEquals($endpoints[0]->get('id'), $same->get('id')); $this->assertEquals($endpoints[0]->get('id'), $same->get('id'));
$this->assertEquals($endpoints[0]->get('name'), $same->get('name')); $this->assertEquals($endpoints[0]->get('name'), $same->get('name'));
$todelete = $endpoints[0]; $todelete = $endpoints[0];
\core\oauth2\api::delete_endpoint($todelete->get('id')); api::delete_endpoint($todelete->get('id'));
$endpoints = \core\oauth2\api::get_endpoints($issuer); $endpoints = api::get_endpoints($issuer);
$this->assertNotEquals($endpoints[0]->get('id'), $todelete->get('id')); $this->assertNotEquals($endpoints[0]->get('id'), $todelete->get('id'));
$userfields = \core\oauth2\api::get_user_field_mappings($issuer); $userfields = api::get_user_field_mappings($issuer);
$same = \core\oauth2\api::get_user_field_mapping($userfields[0]->get('id')); $same = api::get_user_field_mapping($userfields[0]->get('id'));
$this->assertEquals($userfields[0]->get('id'), $same->get('id')); $this->assertEquals($userfields[0]->get('id'), $same->get('id'));
$todelete = $userfields[0]; $todelete = $userfields[0];
\core\oauth2\api::delete_user_field_mapping($todelete->get('id')); api::delete_user_field_mapping($todelete->get('id'));
$userfields = \core\oauth2\api::get_user_field_mappings($issuer); $userfields = api::get_user_field_mappings($issuer);
$this->assertNotEquals($userfields[0]->get('id'), $todelete->get('id')); $this->assertNotEquals($userfields[0]->get('id'), $todelete->get('id'));
} }
@ -144,16 +142,16 @@ class core_oauth2_testcase extends advanced_testcase {
* Tests we can get a logged in oauth client for a system account. * Tests we can get a logged in oauth client for a system account.
* *
* @dataProvider system_oauth_client_provider * @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. * @param int $expiresin The expected expiration time.
*/ */
public function test_get_system_oauth_client($responsedata, $expiresin) { public function test_get_system_oauth_client($responsedata, $expiresin) {
$this->resetAfterTest(); $this->resetAfterTest();
$this->setAdminUser(); $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. // Fake a system account.
$data = (object) [ $data = (object) [
'issuerid' => $issuer->get('id'), 'issuerid' => $issuer->get('id'),
@ -162,17 +160,17 @@ class core_oauth2_testcase extends advanced_testcase {
'email' => 'sys@example.com', 'email' => 'sys@example.com',
'username' => 'sys' 'username' => 'sys'
]; ];
$sys = new \core\oauth2\system_account(0, $data); $sys = new system_account(0, $data);
$sys->create(); $sys->create();
// Fake a response with an access token. // Fake a response with an access token.
$response = json_encode($responsedata); $response = json_encode($responsedata);
curl::mock_response($response); \curl::mock_response($response);
$client = \core\oauth2\api::get_system_oauth_client($issuer); $client = api::get_system_oauth_client($issuer);
$this->assertTrue($client->is_logged_in()); $this->assertTrue($client->is_logged_in());
// Check token expiry. // 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. // 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. // They might differ by a couple of seconds depending on the timing when the token gets actually processed.
@ -190,24 +188,24 @@ class core_oauth2_testcase extends advanced_testcase {
$this->resetAfterTest(); $this->resetAfterTest();
$this->setAdminUser(); $this->setAdminUser();
$issuer = \core\oauth2\api::create_standard_issuer('microsoft'); $issuer = api::create_standard_issuer('microsoft');
$issuerid = $issuer->get('id'); $issuerid = $issuer->get('id');
\core\oauth2\api::enable_issuer($issuerid); api::enable_issuer($issuerid);
$check = \core\oauth2\api::get_issuer($issuer->get('id')); $check = api::get_issuer($issuer->get('id'));
$this->assertTrue((boolean)$check->get('enabled')); $this->assertTrue((boolean)$check->get('enabled'));
\core\oauth2\api::enable_issuer($issuerid); api::enable_issuer($issuerid);
$check = \core\oauth2\api::get_issuer($issuer->get('id')); $check = api::get_issuer($issuer->get('id'));
$this->assertTrue((boolean)$check->get('enabled')); $this->assertTrue((boolean)$check->get('enabled'));
\core\oauth2\api::disable_issuer($issuerid); api::disable_issuer($issuerid);
$check = \core\oauth2\api::get_issuer($issuer->get('id')); $check = api::get_issuer($issuer->get('id'));
$this->assertFalse((boolean)$check->get('enabled')); $this->assertFalse((boolean)$check->get('enabled'));
\core\oauth2\api::enable_issuer($issuerid); api::enable_issuer($issuerid);
$check = \core\oauth2\api::get_issuer($issuer->get('id')); $check = api::get_issuer($issuer->get('id'));
$this->assertTrue((boolean)$check->get('enabled')); $this->assertTrue((boolean)$check->get('enabled'));
} }
@ -218,7 +216,7 @@ class core_oauth2_testcase extends advanced_testcase {
$this->resetAfterTest(); $this->resetAfterTest();
$this->setAdminUser(); $this->setAdminUser();
$issuer = \core\oauth2\api::create_standard_issuer('microsoft'); $issuer = api::create_standard_issuer('microsoft');
$issuer->set('alloweddomains', ''); $issuer->set('alloweddomains', '');
@ -281,10 +279,10 @@ class core_oauth2_testcase extends advanced_testcase {
if ($expectedexception) { if ($expectedexception) {
$this->expectException($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. // Check endpoints have been created.
$endpoints = \core\oauth2\api::get_endpoints($issuer); $endpoints = api::get_endpoints($issuer);
$this->assertNotEmpty($endpoints); $this->assertNotEmpty($endpoints);
$this->assertNotEmpty($issuer->get('image')); $this->assertNotEmpty($issuer->get('image'));
// Check discovery URL. // Check discovery URL.
@ -294,7 +292,7 @@ class core_oauth2_testcase extends advanced_testcase {
$this->assertFalse($issuer->get_endpoint_url('discovery')); $this->assertFalse($issuer->get_endpoint_url('discovery'));
} }
// Check userfield mappings. // Check userfield mappings.
$userfieldmappings = core\oauth2\api::get_user_field_mappings($issuer); $userfieldmappings =api::get_user_field_mappings($issuer);
if ($hasmappingfields) { if ($hasmappingfields) {
$this->assertNotEmpty($userfieldmappings); $this->assertNotEmpty($userfieldmappings);
} else { } else {
@ -373,21 +371,21 @@ class core_oauth2_testcase extends advanced_testcase {
public function test_get_all_issuers() { public function test_get_all_issuers() {
$this->resetAfterTest(); $this->resetAfterTest();
$this->setAdminUser(); $this->setAdminUser();
$googleissuer = core\oauth2\api::create_standard_issuer('google'); $googleissuer = api::create_standard_issuer('google');
core\oauth2\api::create_standard_issuer('facebook'); api::create_standard_issuer('facebook');
core\oauth2\api::create_standard_issuer('microsoft'); api::create_standard_issuer('microsoft');
// Set Google issuer to be shown only on login page. // Set Google issuer to be shown only on login page.
$record = $googleissuer->to_record(); $record = $googleissuer->to_record();
$record->showonloginpage = $googleissuer::LOGINONLY; $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); $this->assertCount(2, $issuers);
$expected = ['Microsoft', 'Facebook']; $expected = ['Microsoft', 'Facebook'];
$this->assertEqualsCanonicalizing($expected, [$issuers[0]->get_display_name(), $issuers[1]->get_display_name()]); $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); $this->assertCount(3, $issuers);
$expected = ['Google', 'Microsoft', 'Facebook']; $expected = ['Google', 'Microsoft', 'Facebook'];
$this->assertEqualsCanonicalizing($expected, $this->assertEqualsCanonicalizing($expected,
@ -400,12 +398,12 @@ class core_oauth2_testcase extends advanced_testcase {
public function test_is_available_for_login() { public function test_is_available_for_login() {
$this->resetAfterTest(); $this->resetAfterTest();
$this->setAdminUser(); $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. // Set Google issuer to be shown only on login page.
$record = $googleissuer->to_record(); $record = $googleissuer->to_record();
$record->showonloginpage = $googleissuer::LOGINONLY; $record->showonloginpage = $googleissuer::LOGINONLY;
core\oauth2\api::update_issuer($record); api::update_issuer($record);
$this->assertFalse($googleissuer->is_available_for_login()); $this->assertFalse($googleissuer->is_available_for_login());
@ -417,13 +415,13 @@ class core_oauth2_testcase extends advanced_testcase {
$this->assertTrue($googleissuer->is_available_for_login()); $this->assertTrue($googleissuer->is_available_for_login());
// Set showonloginpage to service only. // Set showonloginpage to service only.
$googleissuer->set('showonloginpage', \core\oauth2\issuer::SERVICEONLY); $googleissuer->set('showonloginpage', issuer::SERVICEONLY);
$googleissuer->update(); $googleissuer->update();
$this->assertFalse($googleissuer->is_available_for_login()); $this->assertFalse($googleissuer->is_available_for_login());
// Set showonloginpage to everywhere (service and login) and disable issuer. // 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->set('enabled', 0);
$googleissuer->update(); $googleissuer->update();
@ -436,11 +434,11 @@ class core_oauth2_testcase extends advanced_testcase {
$this->assertTrue($googleissuer->is_available_for_login()); $this->assertTrue($googleissuer->is_available_for_login());
// Remove userinfo endpoint from issuer. // Remove userinfo endpoint from issuer.
$endpoint = core\oauth2\endpoint::get_record([ $endpoint = endpoint::get_record([
'issuerid' => $googleissuer->get('id'), 'issuerid' => $googleissuer->get('id'),
'name' => 'userinfo_endpoint' 'name' => 'userinfo_endpoint'
]); ]);
\core\oauth2\api::delete_endpoint($endpoint->get('id')); api::delete_endpoint($endpoint->get('id'));
$this->assertFalse($googleissuer->is_available_for_login()); $this->assertFalse($googleissuer->is_available_for_login());
} }

View file

@ -14,16 +14,9 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** namespace core;
* 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;
use question_bank;
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
@ -39,10 +32,12 @@ require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
/** /**
* Unit tests for (some of) ../questionlib.php. * Unit tests for (some of) ../questionlib.php.
* *
* @package core
* @category test
* @copyright 2006 The Open University * @copyright 2006 The Open University
* @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 core_questionlib_testcase extends advanced_testcase { class questionlib_test extends \advanced_testcase {
/** /**
* Test set up. * Test set up.
@ -81,19 +76,19 @@ class core_questionlib_testcase extends advanced_testcase {
switch ($type) { switch ($type) {
case 'course': case 'course':
$context = context_course::instance($course->id); $context = \context_course::instance($course->id);
break; break;
case 'category': case 'category':
$context = context_coursecat::instance($category->id); $context = \context_coursecat::instance($category->id);
break; break;
case 'system': case 'system':
$context = context_system::instance(); $context = \context_system::instance();
break; break;
default: default:
$context = context_module::instance($quiz->cmid); $context = \context_module::instance($quiz->cmid);
break; break;
} }
@ -171,8 +166,8 @@ class core_questionlib_testcase extends advanced_testcase {
$coursecat2 = $this->getDataGenerator()->create_category(); $coursecat2 = $this->getDataGenerator()->create_category();
// Create a couple of categories and questions. // Create a couple of categories and questions.
$context1 = context_coursecat::instance($coursecat1->id); $context1 = \context_coursecat::instance($coursecat1->id);
$context2 = context_coursecat::instance($coursecat2->id); $context2 = \context_coursecat::instance($coursecat2->id);
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
$questioncat1 = $questiongenerator->create_question_category(array('contextid' => $questioncat1 = $questiongenerator->create_question_category(array('contextid' =>
$context1->id)); $context1->id));
@ -184,10 +179,10 @@ class core_questionlib_testcase extends advanced_testcase {
$question4 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat2->id)); $question4 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat2->id));
// Now lets tag these questions. // 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', $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', $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', $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', $question4->id, $context2, array('tag 7', 'tag 8'));
// Test moving the questions to another category. // Test moving the questions to another category.
question_move_questions_to_category(array($question1->id, $question2->id), $questioncat2->id); question_move_questions_to_category(array($question1->id, $question2->id), $questioncat2->id);
@ -214,7 +209,7 @@ class core_questionlib_testcase extends advanced_testcase {
// Now test moving them back. // Now test moving them back.
question_move_category_to_context($questioncat1->id, $questioncat2->contextid, 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. // 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', $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question',
@ -233,21 +228,21 @@ class core_questionlib_testcase extends advanced_testcase {
$course = $this->getDataGenerator()->create_course(); $course = $this->getDataGenerator()->create_course();
// Create some question categories and questions in this 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)); $questioncat = $questiongenerator->create_question_category(array('contextid' => $coursecontext->id));
$question1 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat->id)); $question1 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat->id));
$question2 = $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. // 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', $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', $question2->id, $coursecontext, array('tag 1', 'tag 2'));
// Create a course that we are going to restore the other course to. // Create a course that we are going to restore the other course to.
$course2 = $this->getDataGenerator()->create_course(); $course2 = $this->getDataGenerator()->create_course();
// Create backup file and save it to the backup location. // Create backup file and save it to the backup location.
$bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE, $bc = new \backup_controller(\backup::TYPE_1COURSE, $course->id, \backup::FORMAT_MOODLE,
backup::INTERACTIVE_NO, backup::MODE_GENERAL, 2); \backup::INTERACTIVE_NO, \backup::MODE_GENERAL, 2);
$bc->execute_plan(); $bc->execute_plan();
$results = $bc->get_results(); $results = $bc->get_results();
$file = $results['backup_destination']; $file = $results['backup_destination'];
@ -257,14 +252,14 @@ class core_questionlib_testcase extends advanced_testcase {
$bc->destroy(); $bc->destroy();
// Now restore the course. // Now restore the course.
$rc = new restore_controller('test-restore-course', $course2->id, backup::INTERACTIVE_NO, $rc = new \restore_controller('test-restore-course', $course2->id, \backup::INTERACTIVE_NO,
backup::MODE_GENERAL, 2, backup::TARGET_NEW_COURSE); \backup::MODE_GENERAL, 2, \backup::TARGET_NEW_COURSE);
$rc->execute_precheck(); $rc->execute_precheck();
$rc->execute_plan(); $rc->execute_plan();
// Get the created question category. // Get the created question category.
$restoredcategory = $DB->get_record_select('question_categories', 'contextid = ? AND parent <> 0', $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. // Check that there are two questions in the restored to course's context.
$this->assertEquals(2, $DB->get_record_sql('SELECT COUNT(q.id) as questioncount $this->assertEquals(2, $DB->get_record_sql('SELECT COUNT(q.id) as questioncount
@ -285,7 +280,7 @@ class core_questionlib_testcase extends advanced_testcase {
global $DB; global $DB;
// Setup. // Setup.
$context = context_system::instance(); $context = \context_system::instance();
$qgen = $this->getDataGenerator()->get_plugin_generator('core_question'); $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
$qcat = $qgen->create_question_category(array('contextid' => $context->id)); $qcat = $qgen->create_question_category(array('contextid' => $context->id));
$q1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id)); $q1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id));
@ -307,7 +302,7 @@ class core_questionlib_testcase extends advanced_testcase {
global $DB; global $DB;
// Setup. // Setup.
$context = context_system::instance(); $context = \context_system::instance();
$qgen = $this->getDataGenerator()->get_plugin_generator('core_question'); $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
$qcat = $qgen->create_question_category(array('contextid' => $context->id)); $qcat = $qgen->create_question_category(array('contextid' => $context->id));
$q1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id)); $q1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id));
@ -488,7 +483,7 @@ class core_questionlib_testcase extends advanced_testcase {
list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions(); 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'), $newcat = question_save_from_deletion(array_column($questions, 'id'),
$context->get_parent_context()->id, $context->get_context_name()); $context->get_parent_context()->id, $context->get_context_name());
@ -516,7 +511,7 @@ class core_questionlib_testcase extends advanced_testcase {
$DB->update_record('quiz', $quiz); $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'), $newcat = question_save_from_deletion(array_column($questions, 'id'),
$context->get_parent_context()->id, $context->get_context_name()); $context->get_parent_context()->id, $context->get_context_name());
@ -564,15 +559,15 @@ class core_questionlib_testcase extends advanced_testcase {
list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
$question1 = $questions[0]; $question1 = $questions[0];
$question2 = $questions[1]; $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', $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', $question2->id, $qcontext, ['baz', 'bop']);
get_question_options($questions, true); get_question_options($questions, true);
foreach ($questions as $question) { 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 = []; $expectedtags = [];
$actualtags = $question->tags; $actualtags = $question->tags;
foreach ($tags as $tag) { foreach ($tags as $tag) {
@ -601,15 +596,15 @@ class core_questionlib_testcase extends advanced_testcase {
list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
$question1 = $questions[0]; $question1 = $questions[0];
$question2 = $questions[1]; $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', $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', $question2->id, $coursecontext, ['baz', 'bop']);
get_question_options($questions, true); get_question_options($questions, true);
foreach ($questions as $question) { 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 = []; $expectedcoursetags = [];
$actualcoursetags = $question->coursetags; $actualcoursetags = $question->coursetags;
foreach ($tags as $tag) { foreach ($tags as $tag) {
@ -640,17 +635,17 @@ class core_questionlib_testcase extends advanced_testcase {
list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('course'); list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('course');
$question1 = $questions[0]; $question1 = $questions[0];
$question2 = $questions[1]; $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 // Create course level tags in the course context that matches the question
// course context. // 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', $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', $question2->id, $coursecontext, ['baz', 'bop']);
get_question_options($questions, true); get_question_options($questions, true);
foreach ($questions as $question) { 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; $actualtagobjects = $question->tagobjects;
sort($tags); sort($tags);
@ -673,18 +668,18 @@ class core_questionlib_testcase extends advanced_testcase {
list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
$question1 = $questions[0]; $question1 = $questions[0];
$question2 = $questions[1]; $question2 = $questions[1];
$qcontext = context::instance_by_id($qcat->contextid); $qcontext = \context::instance_by_id($qcat->contextid);
$coursecontext = context_course::instance($course->id); $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, $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', $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, $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', $question2->id, $coursecontext, ['cbaz', 'cbop']);
get_question_options($questions, true); get_question_options($questions, true);
foreach ($questions as $question) { 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) { $tags = array_filter($alltags, function($tag) use ($qcontext) {
return $tag->taginstancecontextid == $qcontext->id; return $tag->taginstancecontextid == $qcontext->id;
}); });
@ -725,26 +720,26 @@ class core_questionlib_testcase extends advanced_testcase {
list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
$question1 = $questions[0]; $question1 = $questions[0];
$question2 = $questions[1]; $question2 = $questions[1];
$qcontext = context::instance_by_id($qcat->contextid); $qcontext = \context::instance_by_id($qcat->contextid);
$systemcontext = context_system::instance(); $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', $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', $question2->id, $qcontext, ['baz', 'bop']);
$q1tags = core_tag_tag::get_item_tags('core_question', 'question', $question1->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); $q2tags = \core_tag_tag::get_item_tags('core_question', 'question', $question2->id);
$q1tag = array_shift($q1tags); $q1tag = array_shift($q1tags);
$q2tag = array_shift($q2tags); $q2tag = array_shift($q2tags);
// Change two of the tag instances to be a different (non-course) context to the // 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 // question tag context. These tags should then be normalised back to the question
// tag context. // 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); get_question_options($questions, true);
foreach ($questions as $question) { 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. // The database should have been updated with the correct context id.
foreach ($tags as $tag) { foreach ($tags as $tag) {
@ -767,15 +762,15 @@ class core_questionlib_testcase extends advanced_testcase {
list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('course'); list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('course');
$question1 = $questions[0]; $question1 = $questions[0];
$question2 = $questions[1]; $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', $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', $question2->id, $coursecontext, ['baz', 'bop']);
get_question_options($questions, true); get_question_options($questions, true);
foreach ($questions as $question) { 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 // Tags in a course context that matches the question context should
// not be considered course tags. // not be considered course tags.
$this->assertEmpty($question->coursetagobjects); $this->assertEmpty($question->coursetagobjects);
@ -796,16 +791,16 @@ class core_questionlib_testcase extends advanced_testcase {
list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
$question1 = $questions[0]; $question1 = $questions[0];
$question2 = $questions[1]; $question2 = $questions[1];
$coursecontext = context_course::instance($course->id); $coursecontext = \context_course::instance($course->id);
// Create a sibling course. // Create a sibling course.
$siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category]); $siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category]);
$siblingcoursecontext = context_course::instance($siblingcourse->id); $siblingcoursecontext = \context_course::instance($siblingcourse->id);
// Create course tags. // 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', $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', $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', $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', $question2->id, $siblingcoursecontext, ['c2']);
get_question_options($questions, true); get_question_options($questions, true);
@ -829,17 +824,17 @@ class core_questionlib_testcase extends advanced_testcase {
list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
$question1 = $questions[0]; $question1 = $questions[0];
$question2 = $questions[1]; $question2 = $questions[1];
$coursecontext = context_course::instance($course->id); $coursecontext = \context_course::instance($course->id);
// Create a sibling course. // Create a sibling course.
$siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category]); $siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category]);
$siblingcoursecontext = context_course::instance($siblingcourse->id); $siblingcoursecontext = \context_course::instance($siblingcourse->id);
// Create course tags. // 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', $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', $question2->id, $coursecontext, ['bar']);
// Create sibling course tags. These should be filtered out. // 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', $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', $question2->id, $siblingcoursecontext, ['filtered2']);
// Ask to only receive course tags from $course (ignoring $siblingcourse tags). // Ask to only receive course tags from $course (ignoring $siblingcourse tags).
get_question_options($questions, true, [$course]); get_question_options($questions, true, [$course]);
@ -862,21 +857,21 @@ class core_questionlib_testcase extends advanced_testcase {
list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('system'); list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('system');
$question1 = $questions[0]; $question1 = $questions[0];
$question2 = $questions[1]; $question2 = $questions[1];
$qcontext = context::instance_by_id($qcat->contextid); $qcontext = \context::instance_by_id($qcat->contextid);
$newcontext = context_coursecat::instance($category->id); $newcontext = \context_coursecat::instance($category->id);
foreach ($questions as $question) { foreach ($questions as $question) {
$question->contextid = $qcat->contextid; $question->contextid = $qcat->contextid;
} }
// Create tags in the system context. // 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', $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', $question2->id, $qcontext, ['foo', 'bar']);
question_move_question_tags_to_new_context($questions, $newcontext); question_move_question_tags_to_new_context($questions, $newcontext);
foreach ($questions as $question) { 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. // All of the tags should have their context id set to the new context.
foreach ($tags as $tag) { foreach ($tags as $tag) {
@ -894,25 +889,25 @@ class core_questionlib_testcase extends advanced_testcase {
list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('system'); list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('system');
$question1 = $questions[0]; $question1 = $questions[0];
$question2 = $questions[1]; $question2 = $questions[1];
$qcontext = context::instance_by_id($qcat->contextid); $qcontext = \context::instance_by_id($qcat->contextid);
$coursecontext = context_course::instance($course->id); $coursecontext = \context_course::instance($course->id);
$newcontext = context_coursecat::instance($category->id); $newcontext = \context_coursecat::instance($category->id);
foreach ($questions as $question) { foreach ($questions as $question) {
$question->contextid = $qcat->contextid; $question->contextid = $qcat->contextid;
} }
// Create tags in the system context. // 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', $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', $question2->id, $qcontext, ['foo']);
// Create tags in the course context. // 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', $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', $question2->id, $coursecontext, ['ctag']);
question_move_question_tags_to_new_context($questions, $newcontext); question_move_question_tags_to_new_context($questions, $newcontext);
foreach ($questions as $question) { 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) { foreach ($tags as $tag) {
if ($tag->name == 'ctag') { if ($tag->name == 'ctag') {
@ -934,21 +929,21 @@ class core_questionlib_testcase extends advanced_testcase {
list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
$question1 = $questions[0]; $question1 = $questions[0];
$question2 = $questions[1]; $question2 = $questions[1];
$qcontext = context::instance_by_id($qcat->contextid); $qcontext = \context::instance_by_id($qcat->contextid);
$newcontext = context_system::instance(); $newcontext = \context_system::instance();
foreach ($questions as $question) { foreach ($questions as $question) {
$question->contextid = $qcat->contextid; $question->contextid = $qcat->contextid;
} }
// Create tags in the course category context. // 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', $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', $question2->id, $qcontext, ['foo', 'bar']);
question_move_question_tags_to_new_context($questions, $newcontext); question_move_question_tags_to_new_context($questions, $newcontext);
foreach ($questions as $question) { 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. // All of the tags should have their context id set to the new context.
foreach ($tags as $tag) { foreach ($tags as $tag) {
@ -966,25 +961,25 @@ class core_questionlib_testcase extends advanced_testcase {
list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
$question1 = $questions[0]; $question1 = $questions[0];
$question2 = $questions[1]; $question2 = $questions[1];
$qcontext = context::instance_by_id($qcat->contextid); $qcontext = \context::instance_by_id($qcat->contextid);
$coursecontext = context_course::instance($course->id); $coursecontext = \context_course::instance($course->id);
$newcontext = context_system::instance(); $newcontext = \context_system::instance();
foreach ($questions as $question) { foreach ($questions as $question) {
$question->contextid = $qcat->contextid; $question->contextid = $qcat->contextid;
} }
// Create tags in the system context. // 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', $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', $question2->id, $qcontext, ['foo']);
// Create tags in the course context. // 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', $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', $question2->id, $coursecontext, ['ctag']);
question_move_question_tags_to_new_context($questions, $newcontext); question_move_question_tags_to_new_context($questions, $newcontext);
foreach ($questions as $question) { 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) { foreach ($tags as $tag) {
if ($tag->name == 'ctag') { if ($tag->name == 'ctag') {
@ -1006,8 +1001,8 @@ class core_questionlib_testcase extends advanced_testcase {
list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
$question1 = $questions[0]; $question1 = $questions[0];
$question2 = $questions[1]; $question2 = $questions[1];
$qcontext = context::instance_by_id($qcat->contextid); $qcontext = \context::instance_by_id($qcat->contextid);
$coursecontext = context_course::instance($course->id); $coursecontext = \context_course::instance($course->id);
$newcontext = $coursecontext; $newcontext = $coursecontext;
foreach ($questions as $question) { foreach ($questions as $question) {
@ -1015,16 +1010,16 @@ class core_questionlib_testcase extends advanced_testcase {
} }
// Create tags in the system context. // 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', $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', $question2->id, $qcontext, ['foo']);
// Create tags in the course context. // 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', $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', $question2->id, $coursecontext, ['ctag']);
question_move_question_tags_to_new_context($questions, $newcontext); question_move_question_tags_to_new_context($questions, $newcontext);
foreach ($questions as $question) { 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. // Each question should have 2 tags.
$this->assertCount(2, $tags); $this->assertCount(2, $tags);
@ -1047,9 +1042,9 @@ class core_questionlib_testcase extends advanced_testcase {
$siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category]); $siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category]);
$question1 = $questions[0]; $question1 = $questions[0];
$question2 = $questions[1]; $question2 = $questions[1];
$qcontext = context::instance_by_id($qcat->contextid); $qcontext = \context::instance_by_id($qcat->contextid);
$coursecontext = context_course::instance($course->id); $coursecontext = \context_course::instance($course->id);
$siblingcoursecontext = context_course::instance($siblingcourse->id); $siblingcoursecontext = \context_course::instance($siblingcourse->id);
$newcontext = $coursecontext; $newcontext = $coursecontext;
foreach ($questions as $question) { foreach ($questions as $question) {
@ -1057,20 +1052,20 @@ class core_questionlib_testcase extends advanced_testcase {
} }
// Create tags in the system context. // 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', $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', $question2->id, $qcontext, ['foo']);
// Create tags in the target course context. // 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', $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', $question2->id, $coursecontext, ['ctag']);
// Create tags in the sibling course context. These should be deleted as // Create tags in the sibling course context. These should be deleted as
// part of the move. // 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', $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', $question2->id, $siblingcoursecontext, ['stag']);
question_move_question_tags_to_new_context($questions, $newcontext); question_move_question_tags_to_new_context($questions, $newcontext);
foreach ($questions as $question) { 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'. // Each question should have 2 tags, 'foo' and 'ctag'.
$this->assertCount(2, $tags); $this->assertCount(2, $tags);
@ -1094,22 +1089,22 @@ class core_questionlib_testcase extends advanced_testcase {
list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('course'); list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('course');
$question1 = $questions[0]; $question1 = $questions[0];
$question2 = $questions[1]; $question2 = $questions[1];
$qcontext = context::instance_by_id($qcat->contextid); $qcontext = \context::instance_by_id($qcat->contextid);
// Moving up into the course category context. // Moving up into the course category context.
$newcontext = context_coursecat::instance($category->id); $newcontext = \context_coursecat::instance($category->id);
foreach ($questions as $question) { foreach ($questions as $question) {
$question->contextid = $qcat->contextid; $question->contextid = $qcat->contextid;
} }
// Create tags in the course context. // 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', $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', $question2->id, $qcontext, ['foo']);
question_move_question_tags_to_new_context($questions, $newcontext); question_move_question_tags_to_new_context($questions, $newcontext);
foreach ($questions as $question) { 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. // All of the tags should have their context id set to the new context.
foreach ($tags as $tag) { foreach ($tags as $tag) {
@ -1128,27 +1123,27 @@ class core_questionlib_testcase extends advanced_testcase {
$question1 = $questions[0]; $question1 = $questions[0];
$question2 = $questions[1]; $question2 = $questions[1];
$othercategory = $this->getDataGenerator()->create_category(); $othercategory = $this->getDataGenerator()->create_category();
$qcontext = context::instance_by_id($qcat->contextid); $qcontext = \context::instance_by_id($qcat->contextid);
$newcontext = context_coursecat::instance($category->id); $newcontext = \context_coursecat::instance($category->id);
$othercategorycontext = context_coursecat::instance($othercategory->id); $othercategorycontext = \context_coursecat::instance($othercategory->id);
foreach ($questions as $question) { foreach ($questions as $question) {
$question->contextid = $qcat->contextid; $question->contextid = $qcat->contextid;
} }
// Create tags in the system context. // 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', $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', $question2->id, $qcontext, ['foo']);
// Create tags in the other course category context. These should be // Create tags in the other course category context. These should be
// update to the next context id because they represent erroneous data // update to the next context id because they represent erroneous data
// from a time before context id was mandatory in the tag API. // 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', $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', $question2->id, $othercategorycontext, ['bar']);
question_move_question_tags_to_new_context($questions, $newcontext); question_move_question_tags_to_new_context($questions, $newcontext);
foreach ($questions as $question) { 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'. // Each question should have two tags, 'foo' and 'bar'.
$this->assertCount(2, $tags); $this->assertCount(2, $tags);
@ -1169,26 +1164,26 @@ class core_questionlib_testcase extends advanced_testcase {
list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
$question1 = $questions[0]; $question1 = $questions[0];
$question2 = $questions[1]; $question2 = $questions[1];
$qcontext = context::instance_by_id($qcat->contextid); $qcontext = \context::instance_by_id($qcat->contextid);
$coursecontext = context_course::instance($course->id); $coursecontext = \context_course::instance($course->id);
$newcontext = context_module::instance($quiz->cmid); $newcontext = \context_module::instance($quiz->cmid);
foreach ($questions as $question) { foreach ($questions as $question) {
$question->contextid = $qcat->contextid; $question->contextid = $qcat->contextid;
} }
// Create tags in the course category context. // 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', $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', $question2->id, $qcontext, ['foo']);
// Move the questions to the activity context which is a child context of // Move the questions to the activity context which is a child context of
// $coursecontext. // $coursecontext.
core_tag_tag::set_item_tags('core_question', 'question', $question1->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']); \core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['ctag']);
question_move_question_tags_to_new_context($questions, $newcontext); question_move_question_tags_to_new_context($questions, $newcontext);
foreach ($questions as $question) { 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. // Each question should have 2 tags.
$this->assertCount(2, $tags); $this->assertCount(2, $tags);
@ -1208,32 +1203,32 @@ class core_questionlib_testcase extends advanced_testcase {
list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
$question1 = $questions[0]; $question1 = $questions[0];
$question2 = $questions[1]; $question2 = $questions[1];
$qcontext = context::instance_by_id($qcat->contextid); $qcontext = \context::instance_by_id($qcat->contextid);
$coursecontext = context_course::instance($course->id); $coursecontext = \context_course::instance($course->id);
$newcontext = context_module::instance($quiz->cmid); $newcontext = \context_module::instance($quiz->cmid);
$othercourse = $this->getDataGenerator()->create_course(); $othercourse = $this->getDataGenerator()->create_course();
$othercoursecontext = context_course::instance($othercourse->id); $othercoursecontext = \context_course::instance($othercourse->id);
foreach ($questions as $question) { foreach ($questions as $question) {
$question->contextid = $qcat->contextid; $question->contextid = $qcat->contextid;
} }
// Create tags in the course category context. // 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', $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', $question2->id, $qcontext, ['foo']);
// Create tags in the course context. // 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', $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', $question2->id, $coursecontext, ['ctag']);
// Create tags in the other course context. These should be deleted. // 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', $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', $question2->id, $othercoursecontext, ['delete']);
// Move the questions to the activity context which is a child context of // Move the questions to the activity context which is a child context of
// $coursecontext. // $coursecontext.
question_move_question_tags_to_new_context($questions, $newcontext); question_move_question_tags_to_new_context($questions, $newcontext);
foreach ($questions as $question) { 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. // Each question should have 2 tags.
$this->assertCount(2, $tags); $this->assertCount(2, $tags);
@ -1253,21 +1248,21 @@ class core_questionlib_testcase extends advanced_testcase {
list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('course'); list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('course');
$question1 = $questions[0]; $question1 = $questions[0];
$question2 = $questions[1]; $question2 = $questions[1];
$qcontext = context::instance_by_id($qcat->contextid); $qcontext = \context::instance_by_id($qcat->contextid);
$newcontext = context_module::instance($quiz->cmid); $newcontext = \context_module::instance($quiz->cmid);
foreach ($questions as $question) { foreach ($questions as $question) {
$question->contextid = $qcat->contextid; $question->contextid = $qcat->contextid;
} }
// Create tags in the course context. // 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', $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', $question2->id, $qcontext, ['foo']);
question_move_question_tags_to_new_context($questions, $newcontext); question_move_question_tags_to_new_context($questions, $newcontext);
foreach ($questions as $question) { 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) { foreach ($tags as $tag) {
$this->assertEquals($newcontext->id, $tag->taginstancecontextid); $this->assertEquals($newcontext->id, $tag->taginstancecontextid);
@ -1283,21 +1278,21 @@ class core_questionlib_testcase extends advanced_testcase {
list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions(); list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions();
$question1 = $questions[0]; $question1 = $questions[0];
$question2 = $questions[1]; $question2 = $questions[1];
$qcontext = context::instance_by_id($qcat->contextid); $qcontext = \context::instance_by_id($qcat->contextid);
$newcontext = context_course::instance($course->id); $newcontext = \context_course::instance($course->id);
foreach ($questions as $question) { foreach ($questions as $question) {
$question->contextid = $qcat->contextid; $question->contextid = $qcat->contextid;
} }
// Create tags in the activity context. // 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', $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', $question2->id, $qcontext, ['foo']);
question_move_question_tags_to_new_context($questions, $newcontext); question_move_question_tags_to_new_context($questions, $newcontext);
foreach ($questions as $question) { 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) { foreach ($tags as $tag) {
$this->assertEquals($newcontext->id, $tag->taginstancecontextid); $this->assertEquals($newcontext->id, $tag->taginstancecontextid);
@ -1320,33 +1315,33 @@ class core_questionlib_testcase extends advanced_testcase {
$question2 = $questions[1]; $question2 = $questions[1];
$othercategory = $this->getDataGenerator()->create_category(); $othercategory = $this->getDataGenerator()->create_category();
$othercourse = $this->getDataGenerator()->create_course(['category' => $othercategory->id]); $othercourse = $this->getDataGenerator()->create_course(['category' => $othercategory->id]);
$qcontext = context::instance_by_id($qcat->contextid); $qcontext = \context::instance_by_id($qcat->contextid);
$newcontext = context_coursecat::instance($category->id); $newcontext = \context_coursecat::instance($category->id);
$othercategorycontext = context_coursecat::instance($othercategory->id); $othercategorycontext = \context_coursecat::instance($othercategory->id);
$coursecontext = context_course::instance($course->id); $coursecontext = \context_course::instance($course->id);
$othercoursecontext = context_course::instance($othercourse->id); $othercoursecontext = \context_course::instance($othercourse->id);
foreach ($questions as $question) { foreach ($questions as $question) {
$question->contextid = $qcat->contextid; $question->contextid = $qcat->contextid;
} }
// Create tags in the system context. // 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', $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', $question2->id, $qcontext, ['foo']);
// Create tags in the child course context of the new context. // 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', $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', $question2->id, $coursecontext, ['bar']);
// Create tags in the other course context. These should be deleted when // Create tags in the other course context. These should be deleted when
// the question moves to the new course category context because this // the question moves to the new course category context because this
// course belongs to a different category, which means it will no longer // course belongs to a different category, which means it will no longer
// have access to the question. // 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', $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', $question2->id, $othercoursecontext, ['delete']);
question_move_question_tags_to_new_context($questions, $newcontext); question_move_question_tags_to_new_context($questions, $newcontext);
foreach ($questions as $question) { 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'. // Each question should have two tags, 'foo' and 'bar'.
$this->assertCount(2, $tags); $this->assertCount(2, $tags);
@ -1372,14 +1367,14 @@ class core_questionlib_testcase extends advanced_testcase {
list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
$question1 = $questions[0]; $question1 = $questions[0];
$question2 = $questions[1]; $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', $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', $question2->id, $qcontext, ['baz', 'bop']);
foreach ($questions as $question) { 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);
$categorycontext = context::instance_by_id($qcat->contextid); $categorycontext = \context::instance_by_id($qcat->contextid);
$tagobjects = question_sort_tags($tags, $categorycontext); $tagobjects = question_sort_tags($tags, $categorycontext);
$expectedtags = []; $expectedtags = [];
$actualtags = $tagobjects->tags; $actualtags = $tagobjects->tags;
@ -1411,13 +1406,13 @@ class core_questionlib_testcase extends advanced_testcase {
list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
$question1 = $questions[0]; $question1 = $questions[0];
$question2 = $questions[1]; $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', $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', $question2->id, $coursecontext, ['baz', 'bop']);
foreach ($questions as $question) { 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); $tagobjects = question_sort_tags($tags, $qcat);
$expectedtags = []; $expectedtags = [];
@ -1450,19 +1445,19 @@ class core_questionlib_testcase extends advanced_testcase {
list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
$question1 = $questions[0]; $question1 = $questions[0];
$question2 = $questions[1]; $question2 = $questions[1];
$coursecontext = context_course::instance($course->id); $coursecontext = \context_course::instance($course->id);
// Create a sibling course. // Create a sibling course.
$siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category]); $siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category]);
$siblingcoursecontext = context_course::instance($siblingcourse->id); $siblingcoursecontext = \context_course::instance($siblingcourse->id);
// Create course tags. // 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', $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', $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', $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', $question2->id, $siblingcoursecontext, ['c2']);
foreach ($questions as $question) { 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); $tagobjects = question_sort_tags($tags, $qcat);
$this->assertCount(2, $tagobjects->coursetagobjects); $this->assertCount(2, $tagobjects->coursetagobjects);
@ -1485,20 +1480,20 @@ class core_questionlib_testcase extends advanced_testcase {
list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category'); list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
$question1 = $questions[0]; $question1 = $questions[0];
$question2 = $questions[1]; $question2 = $questions[1];
$coursecontext = context_course::instance($course->id); $coursecontext = \context_course::instance($course->id);
// Create a sibling course. // Create a sibling course.
$siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category]); $siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category]);
$siblingcoursecontext = context_course::instance($siblingcourse->id); $siblingcoursecontext = \context_course::instance($siblingcourse->id);
// Create course tags. // 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', $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', $question2->id, $coursecontext, ['bar']);
// Create sibling course tags. These should be filtered out. // 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', $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', $question2->id, $siblingcoursecontext, ['filtered2']);
foreach ($questions as $question) { 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]); $tagobjects = question_sort_tags($tags, $qcat, [$course]);
foreach ($tagobjects->coursetagobjects as $tag) { foreach ($tagobjects->coursetagobjects as $tag) {
@ -1642,7 +1637,7 @@ class core_questionlib_testcase extends advanced_testcase {
$questiongenerator = $generator->get_plugin_generator('core_question'); $questiongenerator = $generator->get_plugin_generator('core_question');
$category = $generator->create_category(); $category = $generator->create_category();
$context = context_coursecat::instance($category->id); $context = \context_coursecat::instance($category->id);
$questioncat = $questiongenerator->create_question_category([ $questioncat = $questiongenerator->create_question_category([
'contextid' => $context->id, 'contextid' => $context->id,
]); ]);
@ -1671,7 +1666,7 @@ class core_questionlib_testcase extends advanced_testcase {
* @param bool $isowner Whether the user to create the question should be the owner or not. * @param bool $isowner Whether the user to create the question should be the owner or not.
* @param bool $expect The expected result. * @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(); $this->resetAfterTest();
// Create the test data. // Create the test data.
@ -1679,7 +1674,7 @@ class core_questionlib_testcase extends advanced_testcase {
$otheruser = $this->getDataGenerator()->create_user(); $otheruser = $this->getDataGenerator()->create_user();
$roleid = $this->getDataGenerator()->create_role(); $roleid = $this->getDataGenerator()->create_role();
$category = $this->getDataGenerator()->create_category(); $category = $this->getDataGenerator()->create_category();
$context = context_coursecat::instance($category->id); $context = \context_coursecat::instance($category->id);
// Assign the user to the role. // Assign the user to the role.
role_assign($roleid, $user->id, $context->id); role_assign($roleid, $user->id, $context->id);
@ -1725,7 +1720,7 @@ class core_questionlib_testcase extends advanced_testcase {
$otheruser = $generator->create_user(); $otheruser = $generator->create_user();
$roleid = $generator->create_role(); $roleid = $generator->create_role();
$category = $generator->create_category(); $category = $generator->create_category();
$context = context_coursecat::instance($category->id); $context = \context_coursecat::instance($category->id);
$questioncat = $questiongenerator->create_question_category([ $questioncat = $questiongenerator->create_question_category([
'contextid' => $context->id, 'contextid' => $context->id,
]); ]);
@ -1771,7 +1766,7 @@ class core_questionlib_testcase extends advanced_testcase {
$otheruser = $generator->create_user(); $otheruser = $generator->create_user();
$roleid = $generator->create_role(); $roleid = $generator->create_role();
$category = $generator->create_category(); $category = $generator->create_category();
$context = context_coursecat::instance($category->id); $context = \context_coursecat::instance($category->id);
$questioncat = $questiongenerator->create_question_category([ $questioncat = $questiongenerator->create_question_category([
'contextid' => $context->id, 'contextid' => $context->id,
]); ]);
@ -1817,7 +1812,7 @@ class core_questionlib_testcase extends advanced_testcase {
$otheruser = $generator->create_user(); $otheruser = $generator->create_user();
$roleid = $generator->create_role(); $roleid = $generator->create_role();
$category = $generator->create_category(); $category = $generator->create_category();
$context = context_coursecat::instance($category->id); $context = \context_coursecat::instance($category->id);
$questioncat = $questiongenerator->create_question_category([ $questioncat = $questiongenerator->create_question_category([
'contextid' => $context->id, 'contextid' => $context->id,
]); ]);
@ -1863,13 +1858,13 @@ class core_questionlib_testcase extends advanced_testcase {
$otheruser = $generator->create_user(); $otheruser = $generator->create_user();
$roleid = $generator->create_role(); $roleid = $generator->create_role();
$category = $generator->create_category(); $category = $generator->create_category();
$context = context_coursecat::instance($category->id); $context = \context_coursecat::instance($category->id);
$questioncat = $questiongenerator->create_question_category([ $questioncat = $questiongenerator->create_question_category([
'contextid' => $context->id, 'contextid' => $context->id,
]); ]);
$newcategory = $generator->create_category(); $newcategory = $generator->create_category();
$newcontext = context_coursecat::instance($newcategory->id); $newcontext = \context_coursecat::instance($newcategory->id);
$newquestioncat = $questiongenerator->create_question_category([ $newquestioncat = $questiongenerator->create_question_category([
'contextid' => $newcontext->id, 'contextid' => $newcontext->id,
]); ]);
@ -1919,7 +1914,7 @@ class core_questionlib_testcase extends advanced_testcase {
$otheruser = $generator->create_user(); $otheruser = $generator->create_user();
$roleid = $generator->create_role(); $roleid = $generator->create_role();
$category = $generator->create_category(); $category = $generator->create_category();
$context = context_coursecat::instance($category->id); $context = \context_coursecat::instance($category->id);
$questioncat = $questiongenerator->create_question_category([ $questioncat = $questiongenerator->create_question_category([
'contextid' => $context->id, 'contextid' => $context->id,
]); ]);
@ -1954,7 +1949,7 @@ class core_questionlib_testcase extends advanced_testcase {
$user = $generator->create_user(); $user = $generator->create_user();
$category = $generator->create_category(); $category = $generator->create_category();
$context = context_coursecat::instance($category->id); $context = \context_coursecat::instance($category->id);
$questioncat = $questiongenerator->create_question_category([ $questioncat = $questiongenerator->create_question_category([
'contextid' => $context->id, 'contextid' => $context->id,
]); ]);
@ -1983,7 +1978,7 @@ class core_questionlib_testcase extends advanced_testcase {
$generator = $this->getDataGenerator(); $generator = $this->getDataGenerator();
$questiongenerator = $generator->get_plugin_generator('core_question'); $questiongenerator = $generator->get_plugin_generator('core_question');
$category = $generator->create_category(); $category = $generator->create_category();
$context = context_coursecat::instance($category->id); $context = \context_coursecat::instance($category->id);
// Create a top category. // Create a top category.
$cat0 = question_get_top_category($context->id, true); $cat0 = question_get_top_category($context->id, true);
// Add sub-categories. // Add sub-categories.
@ -2087,13 +2082,13 @@ class core_questionlib_testcase extends advanced_testcase {
list($category2, $course2, $quiz2, $questioncat2, $questions2) = $this->setup_quiz_and_questions(); list($category2, $course2, $quiz2, $questioncat2, $questions2) = $this->setup_quiz_and_questions();
$questionbankentry1 = get_question_bank_entry($questions1[0]->id); $questionbankentry1 = get_question_bank_entry($questions1[0]->id);
$entry = new stdClass(); $entry = new \stdClass();
$entry->id = $questionbankentry1->id; $entry->id = $questionbankentry1->id;
$entry->idnumber = 1; $entry->idnumber = 1;
$DB->update_record('question_bank_entries', $entry); $DB->update_record('question_bank_entries', $entry);
$questionbankentry2 = get_question_bank_entry($questions2[0]->id); $questionbankentry2 = get_question_bank_entry($questions2[0]->id);
$entry2 = new stdClass(); $entry2 = new \stdClass();
$entry2->id = $questionbankentry2->id; $entry2->id = $questionbankentry2->id;
$entry2->idnumber = 1; $entry2->idnumber = 1;
$DB->update_record('question_bank_entries', $entry2); $DB->update_record('question_bank_entries', $entry2);
@ -2144,7 +2139,7 @@ class core_questionlib_testcase extends advanced_testcase {
global $DB; global $DB;
$this->resetAfterTest(); $this->resetAfterTest();
// Setup. // Setup.
$context = context_system::instance(); $context = \context_system::instance();
$qgen = $this->getDataGenerator()->get_plugin_generator('core_question'); $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
$qcat = $qgen->create_question_category(array('contextid' => $context->id)); $qcat = $qgen->create_question_category(array('contextid' => $context->id));
$q1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id)); $q1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id));
@ -2180,7 +2175,7 @@ class core_questionlib_testcase extends advanced_testcase {
global $DB; global $DB;
$this->resetAfterTest(); $this->resetAfterTest();
// Setup. // Setup.
$context = context_system::instance(); $context = \context_system::instance();
$qgen = $this->getDataGenerator()->get_plugin_generator('core_question'); $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
$qcat = $qgen->create_question_category(array('contextid' => $context->id)); $qcat = $qgen->create_question_category(array('contextid' => $context->id));
$q1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id)); $q1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id));
@ -2209,7 +2204,7 @@ class core_questionlib_testcase extends advanced_testcase {
global $DB; global $DB;
$this->resetAfterTest(); $this->resetAfterTest();
// Setup. // Setup.
$context = context_system::instance(); $context = \context_system::instance();
$qgen = $this->getDataGenerator()->get_plugin_generator('core_question'); $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
$qcat = $qgen->create_question_category(array('contextid' => $context->id)); $qcat = $qgen->create_question_category(array('contextid' => $context->id));
$q1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id)); $q1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id));
@ -2239,7 +2234,7 @@ class core_questionlib_testcase extends advanced_testcase {
global $DB; global $DB;
$this->resetAfterTest(); $this->resetAfterTest();
// Setup. // Setup.
$context = context_system::instance(); $context = \context_system::instance();
$qgen = $this->getDataGenerator()->get_plugin_generator('core_question'); $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
$qcat = $qgen->create_question_category(array('contextid' => $context->id)); $qcat = $qgen->create_question_category(array('contextid' => $context->id));
$q1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id)); $q1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id));

View file

@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core;
/** /**
* Unit tests for format_text defined in weblib.php. * Unit tests for format_text defined in weblib.php.
* *
@ -22,17 +24,7 @@
* @copyright 2015 The Open University * @copyright 2015 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/ */
class weblib_format_text_test extends \advanced_testcase {
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 {
public function test_format_text_format_html() { public function test_format_text_format_html() {
$this->resetAfterTest(); $this->resetAfterTest();

View file

@ -14,23 +14,17 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** namespace core;
* Unit tests for xhprof.
*
* @package core
* @copyright 2019 Brendan Heywood <brendan@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5)
*/
defined('MOODLE_INTERNAL') || die();
/** /**
* Unit tests for the xhprof class. * Unit tests for the xhprof class.
* *
* @package core
* @category test
* @copyright 2019 Brendan Heywood <brendan@catalyst-au.net> * @copyright 2019 Brendan Heywood <brendan@catalyst-au.net>
* @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 core_xhprof_testcase extends advanced_testcase { class xhprof_test extends \advanced_testcase {
/** /**
* Data provider for string matches * Data provider for string matches

View file

@ -14,30 +14,26 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** namespace core;
* Unit tests for xhtml stack.
* use xhtml_container_stack;
* @package core
* @category phpunit
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5)
*/
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
global $CFG; global $CFG;
require_once($CFG->libdir . '/outputlib.php'); require_once($CFG->libdir . '/outputlib.php');
/** /**
* Unit tests for the xhtml_container_stack class. * Unit tests for the xhtml_container_stack class.
* *
* These tests assume that developer debug mode is on which is enforced by our phpunit integration. * These tests assume that developer debug mode is on which is enforced by our phpunit integration.
* *
* @package core
* @category test
* @copyright 2009 Tim Hunt * @copyright 2009 Tim Hunt
* @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 core_xhtml_container_stack_testcase extends advanced_testcase { class xhtml_container_stack_test extends \advanced_testcase {
public function test_push_then_pop() { public function test_push_then_pop() {
// Set up. // Set up.
$stack = new xhtml_container_stack(); $stack = new xhtml_container_stack();

View file

@ -14,14 +14,7 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** namespace core;
* 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
*/
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
@ -31,10 +24,12 @@ require_once($CFG->libdir . '/xmlize.php');
/** /**
* This test compares library against the original xmlize XML importer. * This test compares library against the original xmlize XML importer.
* *
* @package core
* @category test
* @copyright 2017 Kilian Singer {@link http://quantumtechnology.info} * @copyright 2017 Kilian Singer {@link http://quantumtechnology.info}
* @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 core_xmlize_testcase extends basic_testcase { class xmlize_test extends \basic_testcase {
/** /**
* Test an XML import using a valid XML file. * Test an XML import using a valid XML file.
* *

View file

@ -14,18 +14,7 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** namespace quizaccess_seb;
* PHPUnit tests for the access manager.
*
* @package quizaccess_seb
* @author Andrew Madden <andrewmadden@catalyst-au.net>
* @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;
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
@ -34,12 +23,14 @@ require_once(__DIR__ . '/test_helper_trait.php');
/** /**
* PHPUnit tests for the access manager. * PHPUnit tests for the access manager.
* *
* @package quizaccess_seb
* @author Andrew Madden <andrewmadden@catalyst-au.net>
* @copyright 2020 Catalyst IT * @copyright 2020 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
* @covers \quizaccess_seb\access_manager * @covers \quizaccess_seb\access_manager
*/ */
class quizacces_seb_access_manager_testcase extends advanced_testcase { class access_manager_test extends \advanced_testcase {
use quizaccess_seb_test_helper_trait; use \quizaccess_seb_test_helper_trait;
/** /**
* Called before every test. * Called before every test.
@ -99,7 +90,7 @@ class quizacces_seb_access_manager_testcase extends advanced_testcase {
$this->setUser($user); $this->setUser($user);
// Set the bypass SEB check capability to $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(); $accessmanager = $this->get_access_manager();
$this->assertTrue($accessmanager->can_bypass_seb()); $this->assertTrue($accessmanager->can_bypass_seb());

View file

@ -14,15 +14,7 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** namespace quizaccess_seb;
* PHPUnit tests for backup and restore functionality.
*
* @package quizaccess_seb
* @author Dmitrii Metelkin <dmitriim@catalyst-au.net>
* @copyright 2020 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
@ -31,14 +23,16 @@ require_once(__DIR__ . '/test_helper_trait.php');
/** /**
* PHPUnit tests for backup and restore functionality. * PHPUnit tests for backup and restore functionality.
* *
* @package quizaccess_seb
* @author Dmitrii Metelkin <dmitriim@catalyst-au.net>
* @copyright 2020 Catalyst IT * @copyright 2020 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 quizaccess_seb_backup_restore_testcase extends advanced_testcase { class backup_restore_test extends \advanced_testcase {
use quizaccess_seb_test_helper_trait; use \quizaccess_seb_test_helper_trait;
/** @var \quizaccess_seb\template $template A test template. */ /** @var template $template A test template. */
protected $template; protected $template;
/** /**
@ -60,12 +54,12 @@ class quizaccess_seb_backup_restore_testcase extends advanced_testcase {
/** /**
* A helper method to create a quiz with template usage of SEB. * 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() { protected function create_quiz_with_template() {
$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);
$quizsettings = \quizaccess_seb\quiz_settings::get_record(['quizid' => $this->quiz->id]); $quizsettings = quiz_settings::get_record(['quizid' => $this->quiz->id]);
$quizsettings->set('requiresafeexambrowser', \quizaccess_seb\settings_provider::USE_SEB_TEMPLATE); $quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_TEMPLATE);
$quizsettings->set('templateid', $this->template->get('id')); $quizsettings->set('templateid', $this->template->get('id'));
$quizsettings->save(); $quizsettings->save();
@ -95,8 +89,8 @@ class quizaccess_seb_backup_restore_testcase extends advanced_testcase {
$backupid = 'test-seb-backup-restore'; $backupid = 'test-seb-backup-restore';
$bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->quiz->coursemodule, backup::FORMAT_MOODLE, $bc = new \backup_controller(\backup::TYPE_1ACTIVITY, $this->quiz->coursemodule, \backup::FORMAT_MOODLE,
backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->user->id); \backup::INTERACTIVE_NO, \backup::MODE_GENERAL, $this->user->id);
$bc->execute_plan(); $bc->execute_plan();
$results = $bc->get_results(); $results = $bc->get_results();
@ -115,8 +109,8 @@ class quizaccess_seb_backup_restore_testcase extends advanced_testcase {
* @param string $backupid Backup ID to restore. * @param string $backupid Backup ID to restore.
*/ */
protected function restore_quiz($backupid) { protected function restore_quiz($backupid) {
$rc = new restore_controller($backupid, $this->course->id, $rc = new \restore_controller($backupid, $this->course->id,
backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->user->id, backup::TARGET_CURRENT_ADDING); \backup::INTERACTIVE_NO, \backup::MODE_GENERAL, $this->user->id, \backup::TARGET_CURRENT_ADDING);
$this->assertTrue($rc->execute_precheck()); $this->assertTrue($rc->execute_precheck());
$rc->execute_plan(); $rc->execute_plan();
$rc->destroy(); $rc->destroy();
@ -134,11 +128,11 @@ class quizaccess_seb_backup_restore_testcase extends advanced_testcase {
* *
* @param cm_info $newcm Restored course_module object. * @param cm_info $newcm Restored course_module object.
*/ */
protected function validate_backup_restore(cm_info $newcm) { protected function validate_backup_restore(\cm_info $newcm) {
$this->assertEquals(2, quizaccess_seb\quiz_settings::count_records()); $this->assertEquals(2, quiz_settings::count_records());
$actual = \quizaccess_seb\quiz_settings::get_record(['quizid' => $newcm->instance]); $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('templateid'), $actual->get('templateid'));
$this->assertEquals($expected->get('requiresafeexambrowser'), $actual->get('requiresafeexambrowser')); $this->assertEquals($expected->get('requiresafeexambrowser'), $actual->get('requiresafeexambrowser'));
$this->assertEquals($expected->get('showsebdownloadlink'), $actual->get('showsebdownloadlink')); $this->assertEquals($expected->get('showsebdownloadlink'), $actual->get('showsebdownloadlink'));
@ -147,7 +141,7 @@ class quizaccess_seb_backup_restore_testcase extends advanced_testcase {
$this->assertEquals($expected->get('allowedbrowserexamkeys'), $actual->get('allowedbrowserexamkeys')); $this->assertEquals($expected->get('allowedbrowserexamkeys'), $actual->get('allowedbrowserexamkeys'));
// Validate specific SEB config settings. // 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); $name = preg_replace("/^seb_/", "", $name);
$this->assertEquals($expected->get($name), $actual->get($name)); $this->assertEquals($expected->get($name), $actual->get($name));
} }
@ -157,25 +151,25 @@ class quizaccess_seb_backup_restore_testcase extends advanced_testcase {
* Test backup and restore when no seb. * Test backup and restore when no seb.
*/ */
public function test_backup_restore_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->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_NO);
$this->assertEquals(0, quizaccess_seb\quiz_settings::count_records()); $this->assertEquals(0, quiz_settings::count_records());
$this->backup_and_restore_quiz(); $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. * Test backup and restore when manually configured.
*/ */
public function test_backup_restore_manual_config() { 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('showsebdownloadlink', 0);
$expected->set('quitpassword', '123'); $expected->set('quitpassword', '123');
$expected->save(); $expected->save();
$this->assertEquals(1, quizaccess_seb\quiz_settings::count_records()); $this->assertEquals(1, quiz_settings::count_records());
$newcm = $this->backup_and_restore_quiz(); $newcm = $this->backup_and_restore_quiz();
$this->validate_backup_restore($newcm); $this->validate_backup_restore($newcm);
@ -185,15 +179,15 @@ class quizaccess_seb_backup_restore_testcase extends advanced_testcase {
* Test backup and restore when using template. * Test backup and restore when using template.
*/ */
public function test_backup_restore_template_config() { 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(); $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->set('templateid', $template->get('id'));
$expected->save(); $expected->save();
$this->assertEquals(1, quizaccess_seb\quiz_settings::count_records()); $this->assertEquals(1, quiz_settings::count_records());
$newcm = $this->backup_and_restore_quiz(); $newcm = $this->backup_and_restore_quiz();
$this->validate_backup_restore($newcm); $this->validate_backup_restore($newcm);
@ -203,21 +197,21 @@ class quizaccess_seb_backup_restore_testcase extends advanced_testcase {
* Test backup and restore when using uploaded file. * Test backup and restore when using uploaded file.
*/ */
public function test_backup_restore_uploaded_config() { 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 = quiz_settings::get_record(['quizid' => $this->quiz->id]);
$expected->set('requiresafeexambrowser', \quizaccess_seb\settings_provider::USE_SEB_UPLOAD_CONFIG); $expected->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG);
$xml = file_get_contents(__DIR__ . '/fixtures/unencrypted.seb'); $xml = file_get_contents(__DIR__ . '/fixtures/unencrypted.seb');
$this->create_module_test_file($xml, $this->quiz->cmid); $this->create_module_test_file($xml, $this->quiz->cmid);
$expected->save(); $expected->save();
$this->assertEquals(1, quizaccess_seb\quiz_settings::count_records()); $this->assertEquals(1, quiz_settings::count_records());
$newcm = $this->backup_and_restore_quiz(); $newcm = $this->backup_and_restore_quiz();
$this->validate_backup_restore($newcm); $this->validate_backup_restore($newcm);
$expectedfile = \quizaccess_seb\settings_provider::get_module_context_sebconfig_file($this->quiz->cmid); $expectedfile = settings_provider::get_module_context_sebconfig_file($this->quiz->cmid);
$actualfile = \quizaccess_seb\settings_provider::get_module_context_sebconfig_file($newcm->id); $actualfile = settings_provider::get_module_context_sebconfig_file($newcm->id);
$this->assertEquals($expectedfile->get_content(), $actualfile->get_content()); $this->assertEquals($expectedfile->get_content(), $actualfile->get_content());
} }
@ -230,15 +224,15 @@ class quizaccess_seb_backup_restore_testcase extends advanced_testcase {
$this->create_quiz_with_template(); $this->create_quiz_with_template();
$backupid = $this->backup_quiz(); $backupid = $this->backup_quiz();
$this->assertEquals(1, quizaccess_seb\quiz_settings::count_records()); $this->assertEquals(1, quiz_settings::count_records());
$this->assertEquals(1, quizaccess_seb\template::count_records()); $this->assertEquals(1, template::count_records());
$this->change_site(); $this->change_site();
$this->restore_quiz($backupid); $this->restore_quiz($backupid);
// Should see additional setting record, but no new template record. // Should see additional setting record, but no new template record.
$this->assertEquals(2, quizaccess_seb\quiz_settings::count_records()); $this->assertEquals(2, quiz_settings::count_records());
$this->assertEquals(1, quizaccess_seb\template::count_records()); $this->assertEquals(1, template::count_records());
} }
/** /**
@ -249,8 +243,8 @@ class quizaccess_seb_backup_restore_testcase extends advanced_testcase {
$this->create_quiz_with_template(); $this->create_quiz_with_template();
$backupid = $this->backup_quiz(); $backupid = $this->backup_quiz();
$this->assertEquals(1, quizaccess_seb\quiz_settings::count_records()); $this->assertEquals(1, quiz_settings::count_records());
$this->assertEquals(1, quizaccess_seb\template::count_records()); $this->assertEquals(1, template::count_records());
$this->template->set('name', 'New name for template'); $this->template->set('name', 'New name for template');
$this->template->save(); $this->template->save();
@ -259,8 +253,8 @@ class quizaccess_seb_backup_restore_testcase extends advanced_testcase {
$this->restore_quiz($backupid); $this->restore_quiz($backupid);
// Should see additional setting record, and new template record. // Should see additional setting record, and new template record.
$this->assertEquals(2, quizaccess_seb\quiz_settings::count_records()); $this->assertEquals(2, quiz_settings::count_records());
$this->assertEquals(2, quizaccess_seb\template::count_records()); $this->assertEquals(2, template::count_records());
} }
/** /**
@ -273,8 +267,8 @@ class quizaccess_seb_backup_restore_testcase extends advanced_testcase {
$this->create_quiz_with_template(); $this->create_quiz_with_template();
$backupid = $this->backup_quiz(); $backupid = $this->backup_quiz();
$this->assertEquals(1, quizaccess_seb\quiz_settings::count_records()); $this->assertEquals(1, quiz_settings::count_records());
$this->assertEquals(1, quizaccess_seb\template::count_records()); $this->assertEquals(1, template::count_records());
$newxml = file_get_contents($CFG->dirroot . '/mod/quiz/accessrule/seb/tests/fixtures/simpleunencrypted.seb'); $newxml = file_get_contents($CFG->dirroot . '/mod/quiz/accessrule/seb/tests/fixtures/simpleunencrypted.seb');
$this->template->set('content', $newxml); $this->template->set('content', $newxml);
@ -284,8 +278,8 @@ class quizaccess_seb_backup_restore_testcase extends advanced_testcase {
$this->restore_quiz($backupid); $this->restore_quiz($backupid);
// Should see additional setting record, and new template record. // Should see additional setting record, and new template record.
$this->assertEquals(2, quizaccess_seb\quiz_settings::count_records()); $this->assertEquals(2, quiz_settings::count_records());
$this->assertEquals(2, quizaccess_seb\template::count_records()); $this->assertEquals(2, template::count_records());
} }
} }

View file

@ -14,32 +14,23 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace quizaccess_seb;
/** /**
* PHPUnit Tests for config_key class. * PHPUnit Tests for config_key class.
* *
* @package quizaccess_seb * @package quizaccess_seb
* @author Andrew Madden <andrewmadden@catalyst-au.net> * @author Andrew Madden <andrewmadden@catalyst-au.net>
* @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();
/**
* PHPUnit Tests for config_key class.
*
* @copyright 2020 Catalyst IT * @copyright 2020 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 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. * 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() { 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"); $this->expectExceptionMessage("Invalid a PList XML string, representing SEB config");
config_key::generate("<?xml This is some bad xml for sure."); config_key::generate("<?xml This is some bad xml for sure.");
} }

View file

@ -14,26 +14,17 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace quizaccess_seb;
/** /**
* PHPUnit tests for hideif_rule. * PHPUnit tests for hideif_rule.
* *
* @package quizaccess_seb * @package quizaccess_seb
* @author Dmitrii Metelkin <dmitriim@catalyst-au.net> * @author Dmitrii Metelkin <dmitriim@catalyst-au.net>
* @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();
/**
* PHPUnit tests for hideif_rule.
*
* @copyright 2020 Catalyst IT * @copyright 2020 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 quizaccess_seb_hideif_rule_testcase extends advanced_testcase { class hideif_rule_test extends \advanced_testcase {
/** /**
* Test that can get rule data. * Test that can get rule data.

View file

@ -14,26 +14,17 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace quizaccess_seb;
/** /**
* PHPUnit tests for link_generator. * PHPUnit tests for link_generator.
* *
* @package quizaccess_seb * @package quizaccess_seb
* @author Andrew Madden <andrewmadden@catalyst-au.net> * @author Andrew Madden <andrewmadden@catalyst-au.net>
* @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();
/**
* PHPUnit tests for link_generator.
*
* @copyright 2020 Catalyst IT * @copyright 2020 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 quizaccess_seb_link_generator_testcase extends advanced_testcase { class link_generator_test extends \advanced_testcase {
/** /**
* Called before every test. * Called before every test.
@ -95,7 +86,7 @@ class quizaccess_seb_link_generator_testcase extends advanced_testcase {
* Test that link_generator can't not be instantiated with fake course module. * Test that link_generator can't not be instantiated with fake course module.
*/ */
public function test_course_module_does_not_exist() { 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.*/"); $this->expectExceptionMessageMatches("/^Can't find data record in database.*/");
$generator = link_generator::get_link(123456, false); $generator = link_generator::get_link(123456, false);
} }

View file

@ -14,26 +14,17 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace quizaccess_seb;
/** /**
* PHPUnit for property_list class. * PHPUnit for property_list class.
* *
* @package quizaccess_seb * @package quizaccess_seb
* @author Andrew Madden <andrewmadden@catalyst-au.net> * @author Andrew Madden <andrewmadden@catalyst-au.net>
* @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();
/**
* PHPUnit for property_list class.
*
* @copyright 2020 Catalyst IT * @copyright 2020 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 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. * Test that an empty PList with a root dictionary is created.
@ -144,7 +135,7 @@ class quizaccess_seb_property_list_testcase extends advanced_testcase {
. $this->get_plist_xml_footer(); . $this->get_plist_xml_footer();
$plist = new property_list($xml); $plist = new property_list($xml);
$this->expectException(invalid_parameter_exception::class); $this->expectException(\invalid_parameter_exception::class);
$this->expectExceptionMessage($exceptionmessage); $this->expectExceptionMessage($exceptionmessage);
$plist->update_element_value($key, $value); $plist->update_element_value($key, $value);
@ -181,7 +172,7 @@ class quizaccess_seb_property_list_testcase extends advanced_testcase {
. $this->get_plist_xml_footer(); . $this->get_plist_xml_footer();
$plist = new property_list($xml); $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.'); $this->expectExceptionMessage('New array must only contain CFType objects.');
$plist->update_element_array('testDict', [false]); $plist->update_element_array('testDict', [false]);
@ -281,7 +272,7 @@ class quizaccess_seb_property_list_testcase extends advanced_testcase {
$this->assertEquals(42, $plist->get_element_value('number')); $this->assertEquals(42, $plist->get_element_value('number'));
// Type exception. // Type exception.
$this->expectException(TypeError::class); $this->expectException(\TypeError::class);
$plist->set_or_update_value('someKey', 'We really need to pass in CFTypes here'); $plist->set_or_update_value('someKey', 'We really need to pass in CFTypes here');
} }

View file

@ -14,17 +14,7 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** namespace quizaccess_seb;
* PHPUnit tests for quiz_settings class.
*
* @package quizaccess_seb
* @author Andrew Madden <andrewmadden@catalyst-au.net>
* @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;
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
@ -33,11 +23,13 @@ require_once(__DIR__ . '/test_helper_trait.php');
/** /**
* PHPUnit tests for quiz_settings class. * PHPUnit tests for quiz_settings class.
* *
* @package quizaccess_seb
* @author Andrew Madden <andrewmadden@catalyst-au.net>
* @copyright 2020 Catalyst IT * @copyright 2020 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 quizaccess_seb_quiz_settings_testcase extends advanced_testcase { class quiz_settings_test extends \advanced_testcase {
use quizaccess_seb_test_helper_trait; use \quizaccess_seb_test_helper_trait;
/** @var context_module $context Test context. */ /** @var context_module $context Test context. */
protected $context; protected $context;
@ -59,8 +51,8 @@ class quizaccess_seb_quiz_settings_testcase extends advanced_testcase {
'course' => $this->course->id, 'course' => $this->course->id,
'seb_requiresafeexambrowser' => settings_provider::USE_SEB_CONFIG_MANUALLY, 'seb_requiresafeexambrowser' => settings_provider::USE_SEB_CONFIG_MANUALLY,
]); ]);
$this->context = context_module::instance($this->quiz->cmid); $this->context = \context_module::instance($this->quiz->cmid);
$this->url = new moodle_url("/mod/quiz/view.php", ['id' => $this->quiz->cmid]); $this->url = new \moodle_url("/mod/quiz/view.php", ['id' => $this->quiz->cmid]);
} }
/** /**
@ -180,12 +172,12 @@ class quizaccess_seb_quiz_settings_testcase extends advanced_testcase {
/** /**
* Test that different URL filter expressions are turned into config XML. * 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. * @param string $expectedxml SEB Config XML.
* *
* @dataProvider filter_rules_provider * @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); $quizsettings = new quiz_settings(0, $settings);
$config = $quizsettings->get_config(); $config = $quizsettings->get_config();
$this->assertEquals($expectedxml, $config); $this->assertEquals($expectedxml, $config);
@ -221,7 +213,7 @@ class quizaccess_seb_quiz_settings_testcase extends advanced_testcase {
* Test that uploaded seb file gets converted to config string. * Test that uploaded seb file gets converted to config string.
*/ */
public function test_config_file_uploaded_converted_to_config() { 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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
. "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" . "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
. "<plist version=\"1.0\"><dict><key>hashedQuitPassword</key><string>hashedpassword</string>" . "<plist version=\"1.0\"><dict><key>hashedQuitPassword</key><string>hashedpassword</string>"
@ -242,7 +234,7 @@ class quizaccess_seb_quiz_settings_testcase extends advanced_testcase {
$quizsettings = quiz_settings::get_record(['quizid' => $this->quiz->id]); $quizsettings = quiz_settings::get_record(['quizid' => $this->quiz->id]);
$quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG); $quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG);
$cmid = $quizsettings->get('cmid'); $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}"); $this->expectExceptionMessage("No uploaded SEB config file could be found for quiz with cmid: {$cmid}");
$quizsettings->get_config(); $quizsettings->get_config();
} }

View file

@ -14,17 +14,7 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** namespace quizaccess_seb;
* PHPUnit tests for settings_provider.
*
* @package quizaccess_seb
* @author Andrew Madden <andrewmadden@catalyst-au.net>
* @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;
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
@ -33,11 +23,13 @@ require_once(__DIR__ . '/test_helper_trait.php');
/** /**
* PHPUnit tests for settings_provider. * PHPUnit tests for settings_provider.
* *
* @package quizaccess_seb
* @author Andrew Madden <andrewmadden@catalyst-au.net>
* @copyright 2020 Catalyst IT * @copyright 2020 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 quizaccess_seb_settings_provider_testcase extends advanced_testcase { class settings_provider_test extends \advanced_testcase {
use quizaccess_seb_test_helper_trait; use \quizaccess_seb_test_helper_trait;
/** /**
* Mocked quiz form instance. * Mocked quiz form instance.
@ -74,7 +66,7 @@ class quizaccess_seb_settings_provider_testcase extends advanced_testcase {
*/ */
protected function set_up_form_mocks() { protected function set_up_form_mocks() {
if (empty($this->context)) { 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'); $this->mockedquizform = $this->createMock('mod_quiz_mod_form');
@ -166,13 +158,13 @@ class quizaccess_seb_settings_provider_testcase extends advanced_testcase {
/** /**
* Helper method to assert hide if element. * 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 $element Expected element.
* @param string $dependantname Expected dependant element name. * @param string $dependantname Expected dependant element name.
* @param string $condition Expected condition. * @param string $condition Expected condition.
* @param mixed $value Expected value. * @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($element, $hideif->get_element());
$this->assertEquals($dependantname, $hideif->get_dependantname()); $this->assertEquals($dependantname, $hideif->get_dependantname());
$this->assertEquals($condition, $hideif->get_condition()); $this->assertEquals($condition, $hideif->get_condition());
@ -568,7 +560,7 @@ class quizaccess_seb_settings_provider_testcase extends advanced_testcase {
* Test that exception thrown if we try to build capability name from the incorrect setting name. * 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() { 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'); $this->expectExceptionMessage('Incorrect SEB quiz setting broken');
$broken = settings_provider::build_setting_capability_name('broken'); $broken = settings_provider::build_setting_capability_name('broken');
@ -596,7 +588,7 @@ class quizaccess_seb_settings_provider_testcase extends advanced_testcase {
$this->course = $this->getDataGenerator()->create_course(); $this->course = $this->getDataGenerator()->create_course();
$this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]); $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(); $this->set_up_user_and_role();
@ -640,7 +632,7 @@ class quizaccess_seb_settings_provider_testcase extends advanced_testcase {
$this->course = $this->getDataGenerator()->create_course(); $this->course = $this->getDataGenerator()->create_course();
$this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]); $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); $options = settings_provider::get_requiresafeexambrowser_options($this->context);
@ -708,7 +700,7 @@ class quizaccess_seb_settings_provider_testcase extends advanced_testcase {
$this->course = $this->getDataGenerator()->create_course(); $this->course = $this->getDataGenerator()->create_course();
$this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY); $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(); $template = $this->create_template();
@ -735,7 +727,7 @@ class quizaccess_seb_settings_provider_testcase extends advanced_testcase {
$this->course = $this->getDataGenerator()->create_course(); $this->course = $this->getDataGenerator()->create_course();
$this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY); $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. // Setup conflicting permissions.
$template = $this->create_template(); $template = $this->create_template();
@ -768,7 +760,7 @@ class quizaccess_seb_settings_provider_testcase extends advanced_testcase {
$this->course = $this->getDataGenerator()->create_course(); $this->course = $this->getDataGenerator()->create_course();
$this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY); $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(); $user = $this->getDataGenerator()->create_user();
$this->attempt_quiz($this->quiz, $user); $this->attempt_quiz($this->quiz, $user);
@ -798,7 +790,7 @@ class quizaccess_seb_settings_provider_testcase extends advanced_testcase {
$this->course = $this->getDataGenerator()->create_course(); $this->course = $this->getDataGenerator()->create_course();
$this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY); $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(); $template = $this->create_template();
@ -835,7 +827,7 @@ class quizaccess_seb_settings_provider_testcase extends advanced_testcase {
$this->course = $this->getDataGenerator()->create_course(); $this->course = $this->getDataGenerator()->create_course();
$this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY); $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(); $this->set_up_user_and_role();
@ -862,7 +854,7 @@ class quizaccess_seb_settings_provider_testcase extends advanced_testcase {
$this->course = $this->getDataGenerator()->create_course(); $this->course = $this->getDataGenerator()->create_course();
$this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CLIENT_CONFIG); $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(); $this->set_up_user_and_role();
@ -907,7 +899,7 @@ class quizaccess_seb_settings_provider_testcase extends advanced_testcase {
$xml = "This is not a config file."; $xml = "This is not a config file.";
$itemid = $this->create_test_draftarea_file($xml); $itemid = $this->create_test_draftarea_file($xml);
$errors = settings_provider::validate_draftarea_configfile($itemid); $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 @@ class quizaccess_seb_settings_provider_testcase extends advanced_testcase {
$this->resetAfterTest(); $this->resetAfterTest();
$this->course = $this->getDataGenerator()->create_course(); $this->course = $this->getDataGenerator()->create_course();
$this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]); $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(); $this->set_up_user_and_role();
$xml = file_get_contents(__DIR__ . '/fixtures/unencrypted.seb'); $xml = file_get_contents(__DIR__ . '/fixtures/unencrypted.seb');
@ -956,7 +948,7 @@ class quizaccess_seb_settings_provider_testcase extends advanced_testcase {
$this->resetAfterTest(); $this->resetAfterTest();
$this->course = $this->getDataGenerator()->create_course(); $this->course = $this->getDataGenerator()->create_course();
$this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]); $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(); $this->set_up_user_and_role();
$xml = file_get_contents(__DIR__ . '/fixtures/unencrypted.seb'); $xml = file_get_contents(__DIR__ . '/fixtures/unencrypted.seb');
@ -984,7 +976,7 @@ class quizaccess_seb_settings_provider_testcase extends advanced_testcase {
$this->course = $this->getDataGenerator()->create_course(); $this->course = $this->getDataGenerator()->create_course();
$this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY); $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(); $this->set_up_user_and_role();
@ -1026,7 +1018,7 @@ class quizaccess_seb_settings_provider_testcase extends advanced_testcase {
$this->course = $this->getDataGenerator()->create_course(); $this->course = $this->getDataGenerator()->create_course();
$this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]); $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->setAdminUser();
$this->assertTrue(settings_provider::can_configure_seb($this->context)); $this->assertTrue(settings_provider::can_configure_seb($this->context));
@ -1047,7 +1039,7 @@ class quizaccess_seb_settings_provider_testcase extends advanced_testcase {
$this->course = $this->getDataGenerator()->create_course(); $this->course = $this->getDataGenerator()->create_course();
$this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]); $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->setAdminUser();
$this->assertTrue(settings_provider::can_use_seb_template($this->context)); $this->assertTrue(settings_provider::can_use_seb_template($this->context));
@ -1068,7 +1060,7 @@ class quizaccess_seb_settings_provider_testcase extends advanced_testcase {
$this->course = $this->getDataGenerator()->create_course(); $this->course = $this->getDataGenerator()->create_course();
$this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]); $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->setAdminUser();
$this->assertTrue(settings_provider::can_upload_seb_file($this->context)); $this->assertTrue(settings_provider::can_upload_seb_file($this->context));
@ -1089,7 +1081,7 @@ class quizaccess_seb_settings_provider_testcase extends advanced_testcase {
$this->course = $this->getDataGenerator()->create_course(); $this->course = $this->getDataGenerator()->create_course();
$this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]); $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->setAdminUser();
$this->assertTrue(settings_provider::can_change_seb_showsebdownloadlink($this->context)); $this->assertTrue(settings_provider::can_change_seb_showsebdownloadlink($this->context));
@ -1109,7 +1101,7 @@ class quizaccess_seb_settings_provider_testcase extends advanced_testcase {
$this->course = $this->getDataGenerator()->create_course(); $this->course = $this->getDataGenerator()->create_course();
$this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]); $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->setAdminUser();
$this->assertTrue(settings_provider::can_change_seb_allowedbrowserexamkeys($this->context)); $this->assertTrue(settings_provider::can_change_seb_allowedbrowserexamkeys($this->context));
@ -1133,7 +1125,7 @@ class quizaccess_seb_settings_provider_testcase extends advanced_testcase {
$this->course = $this->getDataGenerator()->create_course(); $this->course = $this->getDataGenerator()->create_course();
$this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]); $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->setAdminUser();
$this->assertTrue(settings_provider::can_configure_manually($this->context)); $this->assertTrue(settings_provider::can_configure_manually($this->context));
@ -1171,7 +1163,7 @@ class quizaccess_seb_settings_provider_testcase extends advanced_testcase {
$this->course = $this->getDataGenerator()->create_course(); $this->course = $this->getDataGenerator()->create_course();
$this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY); $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. // Create a template.
$template = $this->create_template(); $template = $this->create_template();
@ -1199,7 +1191,7 @@ class quizaccess_seb_settings_provider_testcase extends advanced_testcase {
$this->course = $this->getDataGenerator()->create_course(); $this->course = $this->getDataGenerator()->create_course();
$this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY); $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. // Save file.
$xml = file_get_contents(__DIR__ . '/fixtures/unencrypted.seb'); $xml = file_get_contents(__DIR__ . '/fixtures/unencrypted.seb');
@ -1230,7 +1222,7 @@ class quizaccess_seb_settings_provider_testcase extends advanced_testcase {
$this->course = $this->getDataGenerator()->create_course(); $this->course = $this->getDataGenerator()->create_course();
$this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY); $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)); $this->assertFalse(settings_provider::is_conflicting_permissions($this->context));
@ -1256,7 +1248,7 @@ class quizaccess_seb_settings_provider_testcase extends advanced_testcase {
* Test filter_plugin_settings helper method. * Test filter_plugin_settings helper method.
*/ */
public function test_filter_plugin_settings() { public function test_filter_plugin_settings() {
$test = new stdClass(); $test = new \stdClass();
$test->one = 'one'; $test->one = 'one';
$test->seb_two = 'two'; $test->seb_two = 'two';
$test->seb_seb_three = 'three'; $test->seb_seb_three = 'three';
@ -1278,7 +1270,7 @@ class quizaccess_seb_settings_provider_testcase extends advanced_testcase {
* @return \stdClass * @return \stdClass
*/ */
protected function get_settings() { protected function get_settings() {
$allsettings = new stdClass(); $allsettings = new \stdClass();
$allsettings->seb_showsebdownloadlink = 0; $allsettings->seb_showsebdownloadlink = 0;
$allsettings->seb_linkquitseb = 2; $allsettings->seb_linkquitseb = 2;
$allsettings->seb_userconfirmquit = 3; $allsettings->seb_userconfirmquit = 3;

View file

@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace quizaccess_seb;
/** /**
* PHPUnit tests for template class. * PHPUnit tests for template class.
* *
@ -22,18 +24,7 @@
* @copyright 2020 Catalyst IT * @copyright 2020 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 template_test extends \advanced_testcase {
use quizaccess_seb\template;
defined('MOODLE_INTERNAL') || die();
/**
* PHPUnit tests for template class.
*
* @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 {
/** /**
* Called before every test. * Called before every test.
@ -49,7 +40,7 @@ class quizaccess_seb_template_testcase extends advanced_testcase {
*/ */
public function test_template_is_saved() { public function test_template_is_saved() {
global $DB; global $DB;
$data = new stdClass(); $data = new \stdClass();
$data->name = 'Test name'; $data->name = 'Test name';
$data->description = 'Test description'; $data->description = 'Test description';
$data->enabled = 1; $data->enabled = 1;
@ -82,7 +73,7 @@ class quizaccess_seb_template_testcase extends advanced_testcase {
$this->expectException(\core\invalid_persistent_exception::class); $this->expectException(\core\invalid_persistent_exception::class);
$this->expectExceptionMessage('Invalid SEB config template'); $this->expectExceptionMessage('Invalid SEB config template');
$data = new stdClass(); $data = new \stdClass();
$data->name = 'Test name'; $data->name = 'Test name';
$data->description = 'Test description'; $data->description = 'Test description';
$data->enabled = 1; $data->enabled = 1;
@ -97,7 +88,7 @@ class quizaccess_seb_template_testcase extends advanced_testcase {
public function test_cannot_delete_template_when_assigned_to_quiz() { public function test_cannot_delete_template_when_assigned_to_quiz() {
global $DB; global $DB;
$data = new stdClass(); $data = new \stdClass();
$data->name = 'Test name'; $data->name = 'Test name';
$data->description = 'Test description'; $data->description = 'Test description';
$data->enabled = 1; $data->enabled = 1;
@ -117,7 +108,7 @@ class quizaccess_seb_template_testcase extends advanced_testcase {
$template->save(); $template->save();
$this->assertTrue($template->can_delete()); $this->assertTrue($template->can_delete());
$DB->insert_record(\quizaccess_seb\quiz_settings::TABLE, (object) [ $DB->insert_record(quiz_settings::TABLE, (object) [
'quizid' => 1, 'quizid' => 1,
'cmid' => 1, 'cmid' => 1,
'templateid' => $template->get('id'), 'templateid' => $template->get('id'),

View file

@ -14,16 +14,17 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** namespace quiz_overview;
* 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
*/
use core_question\local\bank\question_version_status; use core_question\local\bank\question_version_status;
use mod_quiz\external\submit_question_version; use mod_quiz\external\submit_question_version;
use question_engine;
use quiz;
use quiz_attempt;
use quiz_attempts_report;
use quiz_overview_options;
use quiz_overview_report;
use quiz_overview_table;
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
@ -40,10 +41,11 @@ require_once($CFG->dirroot . '/mod/quiz/tests/quiz_question_helper_test_trait.ph
/** /**
* Tests for the quiz overview report. * Tests for the quiz overview report.
* *
* @package quiz_overview
* @copyright 2014 The Open University * @copyright 2014 The Open University
* @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 quiz_overview_report_testcase extends advanced_testcase { class report_test extends \advanced_testcase {
use \quiz_question_helper_test_trait; use \quiz_question_helper_test_trait;
/** /**
@ -153,7 +155,7 @@ class quiz_overview_report_testcase extends advanced_testcase {
$quba->get_question_attempt(1)->manual_grade( $quba->get_question_attempt(1)->manual_grade(
'Comment', $sumgrades, FORMAT_HTML, $timestart + 1200); 'Comment', $sumgrades, FORMAT_HTML, $timestart + 1200);
question_engine::save_questions_usage_by_activity($quba); question_engine::save_questions_usage_by_activity($quba);
$update = new stdClass(); $update = new \stdClass();
$update->id = $attemptobj->get_attemptid(); $update->id = $attemptobj->get_attemptid();
$update->timemodified = $timestart + 1200; $update->timemodified = $timestart + 1200;
$update->sumgrades = $quba->get_total_mark(); $update->sumgrades = $quba->get_total_mark();
@ -165,7 +167,7 @@ class quiz_overview_report_testcase extends advanced_testcase {
// Actually getting the SQL to run is quite hard. Do a minimal set up of // Actually getting the SQL to run is quite hard. Do a minimal set up of
// some objects. // some objects.
$context = context_module::instance($quiz->cmid); $context = \context_module::instance($quiz->cmid);
$cm = get_coursemodule_from_id('quiz', $quiz->cmid); $cm = get_coursemodule_from_id('quiz', $quiz->cmid);
$qmsubselect = quiz_report_qm_filter_select($quiz); $qmsubselect = quiz_report_qm_filter_select($quiz);
$studentsjoins = get_enrolled_with_capabilities_join($context, '', $studentsjoins = get_enrolled_with_capabilities_join($context, '',
@ -186,7 +188,7 @@ class quiz_overview_report_testcase extends advanced_testcase {
$table->download = $isdownloading; // Cannot call the is_downloading API, because it gives errors. $table->download = $isdownloading; // Cannot call the is_downloading API, because it gives errors.
$table->define_columns(array('fullname')); $table->define_columns(array('fullname'));
$table->sortable(true, 'uniqueid'); $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(); $table->setup();
// Run the query. // Run the query.
@ -224,7 +226,7 @@ class quiz_overview_report_testcase extends advanced_testcase {
// Ensure that filtering by initial does not break it. // Ensure that filtering by initial does not break it.
// This involves setting a private properly of the base class, which is // This involves setting a private properly of the base class, which is
// only really possible using reflection :-(. // only really possible using reflection :-(.
$reflectionobject = new ReflectionObject($table); $reflectionobject = new \ReflectionObject($table);
while ($parent = $reflectionobject->getParentClass()) { while ($parent = $reflectionobject->getParentClass()) {
$reflectionobject = $parent; $reflectionobject = $parent;
} }
@ -305,10 +307,10 @@ class quiz_overview_report_testcase extends advanced_testcase {
$generator->enrol_user($student->id, $course->id); $generator->enrol_user($student->id, $course->id);
$generator->enrol_user($student->id, $course->id, null, 'self'); $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); $cm = get_coursemodule_from_id('quiz', $quiz->cmid);
$allowedjoins = get_enrolled_with_capabilities_join($context, '', ['mod/quiz:attempt', 'mod/quiz:reviewmyattempts']); $allowedjoins = get_enrolled_with_capabilities_join($context, '', ['mod/quiz:attempt', 'mod/quiz:reviewmyattempts']);
$quizattemptsreport = new testable_quiz_attempts_report(); $quizattemptsreport = new \testable_quiz_attempts_report();
// Create the new attempt and initialize the question sessions. // Create the new attempt and initialize the question sessions.
$quizobj = quiz::create($quiz->id, $student->id); $quizobj = quiz::create($quiz->id, $student->id);
@ -335,7 +337,7 @@ class quiz_overview_report_testcase extends advanced_testcase {
$course = $this->getDataGenerator()->create_course(); $course = $this->getDataGenerator()->create_course();
$quiz = $this->create_test_quiz($course); $quiz = $this->create_test_quiz($course);
$cm = get_fast_modinfo($course->id)->get_cm($quiz->cmid); $cm = get_fast_modinfo($course->id)->get_cm($quiz->cmid);
$context = context_module::instance($quiz->cmid); $context = \context_module::instance($quiz->cmid);
/** @var core_question_generator $questiongenerator */ /** @var core_question_generator $questiongenerator */
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');

View file

@ -14,15 +14,10 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** namespace quiz_responses;
* Quiz attempt walk through using data from csv file.
* use question_bank;
* @package quiz_statistics use quiz_attempt;
* @category phpunit
* @copyright 2013 The Open University
* @author Jamie Pratt <me@jamiep.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
@ -35,13 +30,13 @@ require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
/** /**
* Quiz attempt walk through using data from csv file. * Quiz attempt walk through using data from csv file.
* *
* @package quiz_statistics * @package quiz_responses
* @category phpunit * @category test
* @copyright 2013 The Open University * @copyright 2013 The Open University
* @author Jamie Pratt <me@jamiep.org> * @author Jamie Pratt <me@jamiep.org>
* @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 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(string $setname, string $test): string { protected function get_full_path_of_csv_file(string $setname, string $test): string {
// Overridden here so that __DIR__ points to the path of this file. // Overridden here so that __DIR__ points to the path of this file.
return __DIR__."/fixtures/{$setname}{$test}.csv"; return __DIR__."/fixtures/{$setname}{$test}.csv";
@ -69,7 +64,7 @@ class quiz_report_responses_from_steps_testcase extends mod_quiz_attempt_walkthr
$responses = $this->explode_dot_separated_keys_to_make_subindexs($responsesfromcsv); $responses = $this->explode_dot_separated_keys_to_make_subindexs($responsesfromcsv);
if (!isset($quizattemptids[$responses['quizattempt']])) { 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); $this->assert_response_test($quizattemptids[$responses['quizattempt']], $responses);
} }
@ -92,7 +87,7 @@ class quiz_report_responses_from_steps_testcase extends mod_quiz_attempt_walkthr
$stepswithsubmit = $qa->get_steps_with_submitted_response_iterator(); $stepswithsubmit = $qa->get_steps_with_submitted_response_iterator();
$step = $stepswithsubmit[$responses['submittedstepno']]; $step = $stepswithsubmit[$responses['submittedstepno']];
if (null === $step) { 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']}!"); "for slot $slot in quizattempt {$responses['quizattempt']}!");
} }
foreach (array('responsesummary', 'fraction', 'state') as $column) { foreach (array('responsesummary', 'fraction', 'state') as $column) {

View file

@ -14,14 +14,9 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** namespace quiz_statistics;
* Unit tests for the quiz_statistics_table class.
* use quiz_statistics_table;
* @package quiz_statistics
* @category test
* @copyright 2018 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
@ -31,16 +26,18 @@ require_once($CFG->dirroot . '/mod/quiz/report/statistics/statistics_table.php')
/** /**
* Class quiz_statistics_statistics_table_testcase * Class quiz_statistics_statistics_table_testcase
* *
* @package quiz_statistics
* @category test
* @copyright 2018 Shamim Rezaie <shamim@moodle.com> * @copyright 2018 Shamim Rezaie <shamim@moodle.com>
* @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 quiz_statistics_statistics_table_testcase extends advanced_testcase { class statistics_table_test extends \advanced_testcase {
public function test_format_percentage() { public function test_format_percentage() {
$table = new quiz_statistics_table(); $table = new quiz_statistics_table();
// The format_percentage method is protected. Use Reflection to call the method. // 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 = $reflector->getMethod('format_percentage');
$method->setAccessible(true); $method->setAccessible(true);
@ -59,7 +56,7 @@ class quiz_statistics_statistics_table_testcase extends advanced_testcase {
$table = new quiz_statistics_table(); $table = new quiz_statistics_table();
// The format_percentage_range method is protected. Use Reflection to call the method. // 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 = $reflector->getMethod('format_percentage_range');
$method->setAccessible(true); $method->setAccessible(true);
@ -78,7 +75,7 @@ class quiz_statistics_statistics_table_testcase extends advanced_testcase {
$table = new quiz_statistics_table(); $table = new quiz_statistics_table();
// The format_range method is protected. Use Reflection to call the method. // 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 = $reflector->getMethod('format_range');
$method->setAccessible(true); $method->setAccessible(true);

View file

@ -18,11 +18,13 @@
* Unit tests for (some of) /question/engine/statistics.php * Unit tests for (some of) /question/engine/statistics.php
* *
* @package quiz_statistics * @package quiz_statistics
* @category phpunit * @category test
* @copyright 2008 Jamie Pratt * @copyright 2008 Jamie Pratt
* @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
*/ */
namespace quiz_statistics;
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
global $CFG; global $CFG;
@ -55,7 +57,7 @@ class testable_question_statistics extends \core_question\statistics\questions\c
*/ */
protected $lateststeps; 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) { public function set_step_data($states) {
$this->lateststeps = $states; $this->lateststeps = $states;
@ -99,7 +101,7 @@ class testable_question_statistics extends \core_question\statistics\questions\c
* @copyright 2008 Jamie Pratt * @copyright 2008 Jamie Pratt
* @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 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. */ /** @var testable_all_calculated_for_qubaid_condition object created to test class. */
protected $qstats; protected $qstats;
@ -181,7 +183,7 @@ class quiz_statistics_question_stats_testcase extends basic_testcase {
while (null !== ($line = array_shift($filecontents))) { while (null !== ($line = array_shift($filecontents))) {
$data = $this->get_fields_from_csv($line); $data = $this->get_fields_from_csv($line);
$arraykey = reset($data); $arraykey = reset($data);
$object = new stdClass(); $object = new \stdClass();
foreach ($keys as $key) { foreach ($keys as $key) {
$value = array_shift($data); $value = array_shift($data);
if ($value !== null) { if ($value !== null) {

View file

@ -14,23 +14,12 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** namespace quiz_statistics;
* Quiz attempt walk through using data from csv file.
* use question_attempt;
* The quiz stats below and the question stats found in qstats00.csv were calculated independently in a spreadsheet which is use question_bank;
* available in open document or excel format here : use question_finder;
* https://github.com/jamiepratt/moodle-quiz-tools/tree/master/statsspreadsheet use quiz_statistics_report;
*
* 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 <me@jamiep.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
@ -43,13 +32,21 @@ require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
/** /**
* Quiz attempt walk through using data from csv file. * 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 * @package quiz_statistics
* @category phpunit * @category test
* @copyright 2013 The Open University * @copyright 2013 The Open University
* @author Jamie Pratt <me@jamiep.org> * @author Jamie Pratt <me@jamiep.org>
* @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 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. * @var quiz_statistics_report object to do stats calculations.
@ -193,7 +190,7 @@ class quiz_report_statistics_from_steps_testcase extends mod_quiz_attempt_walkth
return; 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) { protected function get_response_subpart_and_class_id($question, $subpart, $modelresponse) {
@ -201,7 +198,7 @@ class quiz_report_statistics_from_steps_testcase extends mod_quiz_attempt_walkth
$possibleresponses = $qtypeobj->get_possible_responses($question); $possibleresponses = $qtypeobj->get_possible_responses($question);
$possibleresponsesubpartids = array_keys($possibleresponses); $possibleresponsesubpartids = array_keys($possibleresponses);
if (!isset($possibleresponsesubpartids[$subpart - 1])) { 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]; $subpartid = $possibleresponsesubpartids[$subpart - 1];
@ -373,7 +370,7 @@ class quiz_report_statistics_from_steps_testcase extends mod_quiz_attempt_walkth
// We will create some quiz and question stat calculator instances and some response analyser instances, just in order // 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. // 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. // 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)); $this->assertTimeCurrent($quizcalc->get_last_calculated_time($qubaids));

View file

@ -14,14 +14,11 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** namespace mod_quiz;
* Tests for the quiz_attempt class.
* use question_engine;
* @package mod_quiz use quiz;
* @category test use quiz_attempt;
* @copyright 2014 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
@ -31,10 +28,12 @@ require_once($CFG->dirroot . '/mod/quiz/locallib.php');
/** /**
* Tests for the quiz_attempt class. * Tests for the quiz_attempt class.
* *
* @package mod_quiz
* @category test
* @copyright 2014 Tim Hunt * @copyright 2014 Tim Hunt
* @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_attempt_testcase extends advanced_testcase { class attempt_test extends \advanced_testcase {
/** /**
* Create quiz and attempt data with layout. * Create quiz and attempt data with layout.
@ -90,75 +89,75 @@ class mod_quiz_attempt_testcase extends advanced_testcase {
$url = '/mod/quiz/attempt.php'; $url = '/mod/quiz/attempt.php';
$params = ['attempt' => $attemptid, 'cmid' => $cmid, 'page' => 2]; $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; $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); $questionattempt = $attempt->get_question_attempt(4);
$expecteanchor = $questionattempt->get_outer_question_div_unique_id(); $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));
$questionattempt = $attempt->get_question_attempt(3); $questionattempt = $attempt->get_question_attempt(3);
$expecteanchor = '#' . $questionattempt->get_outer_question_div_unique_id(); $expecteanchor = '#' . $questionattempt->get_outer_question_div_unique_id();
$this->assertEquals(new moodle_url('#'), $attempt->attempt_url(null, 2, 2)); $this->assertEquals(new \moodle_url('#'), $attempt->attempt_url(null, 2, 2));
$this->assertEquals(new moodle_url($expecteanchor), $attempt->attempt_url(3, -1, 1)); $this->assertEquals(new \moodle_url($expecteanchor), $attempt->attempt_url(3, -1, 1));
$questionattempt = $attempt->get_question_attempt(4); $questionattempt = $attempt->get_question_attempt(4);
$expecteanchor = $questionattempt->get_outer_question_div_unique_id(); $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. // Summary page.
$url = '/mod/quiz/summary.php'; $url = '/mod/quiz/summary.php';
unset($params['page']); 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. // Review page.
$url = '/mod/quiz/review.php'; $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; $params['page'] = 1;
$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));
$this->assertEquals(new moodle_url($url, $params, $expecteanchor), $attempt->review_url(4, -1, false)); $this->assertEquals(new \moodle_url($url, $params, $expecteanchor), $attempt->review_url(4, -1, false));
unset($params['page']); 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; $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']); unset($params['page']);
$params['showall'] = 0; $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(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(1, -1, false));
$params['page'] = 1; $params['page'] = 1;
unset($params['showall']); 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; $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));
$this->assertEquals(new moodle_url('#'), $attempt->review_url(null, -1, null, 0)); $this->assertEquals(new \moodle_url('#'), $attempt->review_url(null, -1, null, 0));
$questionattempt = $attempt->get_question_attempt(3); $questionattempt = $attempt->get_question_attempt(3);
$expecteanchor = '#' . $questionattempt->get_outer_question_div_unique_id(); $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); $questionattempt = $attempt->get_question_attempt(4);
$expecteanchor = '#' . $questionattempt->get_outer_question_div_unique_id(); $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($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(null, 2, true, 0));
$questionattempt = $attempt->get_question_attempt(1); $questionattempt = $attempt->get_question_attempt(1);
$expecteanchor = '#' . $questionattempt->get_outer_question_div_unique_id(); $expecteanchor = '#' . $questionattempt->get_outer_question_div_unique_id();
$this->assertEquals(new moodle_url($expecteanchor), $attempt->review_url(1, -1, true, 0)); $this->assertEquals(new \moodle_url($expecteanchor), $attempt->review_url(1, -1, true, 0));
$this->assertEquals(new moodle_url($expecteanchor), $attempt->review_url(1, -1, false, 0)); $this->assertEquals(new \moodle_url($expecteanchor), $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($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(null, 0, false, 0));
$params['page'] = 1; $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. // Setup another attempt.
$attempt = $this->create_quiz_and_attempt_with_layout( $attempt = $this->create_quiz_and_attempt_with_layout(
@ -169,58 +168,58 @@ class mod_quiz_attempt_testcase extends advanced_testcase {
$attemptid = $attempt->get_attempt()->id; $attemptid = $attempt->get_attempt()->id;
$cmid = $attempt->get_cmid(); $cmid = $attempt->get_cmid();
$params = ['attempt' => $attemptid, 'cmid' => $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; $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; $params['page'] = 1;
unset($params['showall']); 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); $questionattempt = $attempt->get_question_attempt(12);
$expecteanchor = $questionattempt->get_outer_question_div_unique_id(); $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; $params['showall'] = 1;
unset($params['page']); 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; $params['page'] = 2;
unset($params['showall']); 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']); 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; $params['page'] = 1;
$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));
$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['page'] = 2; $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));
$this->assertEquals(new moodle_url('#'), $attempt->review_url(null, -1, null, 0)); $this->assertEquals(new \moodle_url('#'), $attempt->review_url(null, -1, null, 0));
$questionattempt = $attempt->get_question_attempt(3); $questionattempt = $attempt->get_question_attempt(3);
$expecteanchor = $questionattempt->get_outer_question_div_unique_id(); $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); $questionattempt = $attempt->get_question_attempt(4);
$expecteanchor = $questionattempt->get_outer_question_div_unique_id(); $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));
$questionattempt = $attempt->get_question_attempt(1); $questionattempt = $attempt->get_question_attempt(1);
$expecteanchor = '#' . $questionattempt->get_outer_question_div_unique_id(); $expecteanchor = '#' . $questionattempt->get_outer_question_div_unique_id();
$this->assertEquals(new moodle_url($expecteanchor), $attempt->review_url(1, -1, true, 0)); $this->assertEquals(new \moodle_url($expecteanchor), $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(null, 2, true, 0));
$params['page'] = 2; $params['page'] = 2;
$questionattempt = $attempt->get_question_attempt(1); $questionattempt = $attempt->get_question_attempt(1);
$expecteanchor = '#' . $questionattempt->get_outer_question_div_unique_id(); $expecteanchor = '#' . $questionattempt->get_outer_question_div_unique_id();
$this->assertEquals(new moodle_url($expecteanchor), $attempt->review_url(1, -1, false, 0)); $this->assertEquals(new \moodle_url($expecteanchor), $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($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(null, 0, false, 0));
$params['page'] = 1; $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));
} }
/** /**

View file

@ -14,15 +14,11 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** namespace mod_quiz;
* Quiz attempt walk through using data from csv file.
* use question_engine;
* @package mod_quiz use quiz;
* @category phpunit use quiz_attempt;
* @copyright 2013 The Open University
* @author Jamie Pratt <me@jamiep.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
@ -33,12 +29,12 @@ require_once($CFG->dirroot . '/mod/quiz/locallib.php');
* Quiz attempt walk through using data from csv file. * Quiz attempt walk through using data from csv file.
* *
* @package mod_quiz * @package mod_quiz
* @category phpunit * @category test
* @copyright 2013 The Open University * @copyright 2013 The Open University
* @author Jamie Pratt <me@jamiep.org> * @author Jamie Pratt <me@jamiep.org>
* @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_attempt_walkthrough_from_csv_testcase extends advanced_testcase { class attempt_walkthrough_from_csv_test extends \advanced_testcase {
protected $files = array('questions', 'steps', 'results'); protected $files = array('questions', 'steps', 'results');
@ -330,7 +326,7 @@ class mod_quiz_attempt_walkthrough_from_csv_testcase extends advanced_testcase {
"Mark for slot $slotno of attempt {$result['quizattempt']}."); "Mark for slot $slotno of attempt {$result['quizattempt']}.");
break; break;
default : 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)); .s($slotfieldname));
} }
} }
@ -360,7 +356,7 @@ class mod_quiz_attempt_walkthrough_from_csv_testcase extends advanced_testcase {
$this->assertEquals($value, $gradebookgrade->grade, "Gradebook grade for attempt {$result['quizattempt']}."); $this->assertEquals($value, $gradebookgrade->grade, "Gradebook grade for attempt {$result['quizattempt']}.");
break; break;
default : default :
throw new coding_exception('Unknown column in csv file '.s($fieldname)); throw new \coding_exception('Unknown column in csv file '.s($fieldname));
} }
} }
} }

View file

@ -14,15 +14,12 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** namespace mod_quiz;
* Quiz attempt walk through tests.
* use question_bank;
* @package mod_quiz use question_engine;
* @category phpunit use quiz;
* @copyright 2013 The Open University use quiz_attempt;
* @author Jamie Pratt <me@jamiep.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
@ -33,12 +30,12 @@ require_once($CFG->dirroot . '/mod/quiz/locallib.php');
* Quiz attempt walk through. * Quiz attempt walk through.
* *
* @package mod_quiz * @package mod_quiz
* @category phpunit * @category test
* @copyright 2013 The Open University * @copyright 2013 The Open University
* @author Jamie Pratt <me@jamiep.org> * @author Jamie Pratt <me@jamiep.org>
* @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_attempt_walkthrough_testcase extends advanced_testcase { class attempt_walkthrough_test extends \advanced_testcase {
/** /**
* Create a quiz with questions and walk through a quiz attempt. * Create a quiz with questions and walk through a quiz attempt.

View file

@ -14,14 +14,11 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** namespace mod_quiz;
* Quiz attempt overdue handling tests
* use mod_quiz_overdue_attempt_updater;
* @package mod_quiz use question_engine;
* @category phpunit use quiz;
* @copyright 2012 Matt Petro
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
@ -32,11 +29,11 @@ require_once($CFG->dirroot.'/group/lib.php');
* Unit tests for quiz attempt overdue handling * Unit tests for quiz attempt overdue handling
* *
* @package mod_quiz * @package mod_quiz
* @category phpunit * @category test
* @copyright 2012 Matt Petro * @copyright 2012 Matt Petro
* @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_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 * Test the functions quiz_update_open_attempts(), get_list_of_overdue_attempts() and
@ -458,12 +455,12 @@ class mod_quiz_attempt_overdue_testcase extends advanced_testcase {
* question usage to store in uniqueid, but they don't have to be * question usage to store in uniqueid, but they don't have to be
* very realistic. * very realistic.
* *
* @param stdClass $quiz * @param \stdClass $quiz
* @return int question usage id. * @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', $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'); $quba->set_preferred_behaviour('deferredfeedback');
question_engine::save_questions_usage_by_activity($quba); question_engine::save_questions_usage_by_activity($quba);
return $quba->get_id(); return $quba->get_id();
@ -578,19 +575,19 @@ class mod_quiz_attempt_overdue_testcase extends advanced_testcase {
try { try {
$result = quiz_create_attempt_handling_errors($attempt->id, 9999); $result = quiz_create_attempt_handling_errors($attempt->id, 9999);
$this->fail('Exception expected due to invalid course module id.'); $this->fail('Exception expected due to invalid course module id.');
} catch (moodle_exception $e) { } catch (\moodle_exception $e) {
$this->assertEquals('invalidcoursemodule', $e->errorcode); $this->assertEquals('invalidcoursemodule', $e->errorcode);
} }
try { try {
quiz_create_attempt_handling_errors(9999, $result->get_cmid()); quiz_create_attempt_handling_errors(9999, $result->get_cmid());
$this->fail('Exception expected due to quiz content change.'); $this->fail('Exception expected due to quiz content change.');
} catch (moodle_exception $e) { } catch (\moodle_exception $e) {
$this->assertEquals('attempterrorcontentchange', $e->errorcode); $this->assertEquals('attempterrorcontentchange', $e->errorcode);
} }
try { try {
quiz_create_attempt_handling_errors(9999); quiz_create_attempt_handling_errors(9999);
$this->fail('Exception expected due to invalid quiz attempt id.'); $this->fail('Exception expected due to invalid quiz attempt id.');
} catch (moodle_exception $e) { } catch (\moodle_exception $e) {
$this->assertEquals('attempterrorinvalid', $e->errorcode); $this->assertEquals('attempterrorinvalid', $e->errorcode);
} }
// Set up as normal user without permission to view preview. // Set up as normal user without permission to view preview.
@ -598,13 +595,13 @@ class mod_quiz_attempt_overdue_testcase extends advanced_testcase {
try { try {
quiz_create_attempt_handling_errors(9999, $result->get_cmid()); quiz_create_attempt_handling_errors(9999, $result->get_cmid());
$this->fail('Exception expected due to quiz content change for user without permission.'); $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); $this->assertEquals('attempterrorcontentchangeforuser', $e->errorcode);
} }
try { try {
quiz_create_attempt_handling_errors($attempt->id, 9999); quiz_create_attempt_handling_errors($attempt->id, 9999);
$this->fail('Exception expected due to invalid course module id for user without permission.'); $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); $this->assertEquals('invalidcoursemodule', $e->errorcode);
} }
} }

View file

@ -14,6 +14,13 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mod_quiz;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/mod/quiz/lib.php');
/** /**
* Unit tests for the calendar event modification callbacks used * Unit tests for the calendar event modification callbacks used
* for dragging and dropping quiz calendar events in the calendar * for dragging and dropping quiz calendar events in the calendar
@ -24,17 +31,7 @@
* @copyright 2017 Ryan Wyllie <ryan@moodle.com> * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/ */
class calendar_event_modified_test extends \advanced_testcase {
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/mod/quiz/lib.php');
/**
* @copyright 2017 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
class mod_quiz_calendar_event_modified_testcase extends advanced_testcase {
/** /**
* Create an instance of the quiz activity. * Create an instance of the quiz activity.
@ -71,7 +68,7 @@ class mod_quiz_calendar_event_modified_testcase extends advanced_testcase {
/** /**
* Create a calendar event for a quiz activity instance. * 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 * @param array $eventproperties Properties to set on the calendar event
* @return calendar_event * @return calendar_event
*/ */
@ -241,11 +238,11 @@ class mod_quiz_calendar_event_modified_testcase extends advanced_testcase {
$generator = $this->getDataGenerator(); $generator = $this->getDataGenerator();
$user = $generator->create_user(); $user = $generator->create_user();
$course = $generator->create_course(); $course = $generator->create_course();
$context = context_course::instance($course->id); $context = \context_course::instance($course->id);
$roleid = $generator->create_role(); $roleid = $generator->create_role();
$now = time(); $now = time();
$timeopen = (new DateTime())->setTimestamp($now); $timeopen = (new \DateTime())->setTimestamp($now);
$newtimeopen = (new DateTime())->setTimestamp($now)->modify('+1 day'); $newtimeopen = (new \DateTime())->setTimestamp($now)->modify('+1 day');
$quiz = $this->create_quiz_instance([ $quiz = $this->create_quiz_instance([
'course' => $course->id, 'course' => $course->id,
'timeopen' => $timeopen->getTimestamp() 'timeopen' => $timeopen->getTimestamp()
@ -285,11 +282,11 @@ class mod_quiz_calendar_event_modified_testcase extends advanced_testcase {
$generator = $this->getDataGenerator(); $generator = $this->getDataGenerator();
$user = $generator->create_user(); $user = $generator->create_user();
$course = $generator->create_course(); $course = $generator->create_course();
$context = context_course::instance($course->id); $context = \context_course::instance($course->id);
$roleid = $generator->create_role(); $roleid = $generator->create_role();
$now = time(); $now = time();
$timeopen = (new DateTime())->setTimestamp($now); $timeopen = (new \DateTime())->setTimestamp($now);
$newtimeopen = (new DateTime())->setTimestamp($now)->modify('+1 day'); $newtimeopen = (new \DateTime())->setTimestamp($now)->modify('+1 day');
$quiz = $this->create_quiz_instance([ $quiz = $this->create_quiz_instance([
'course' => $course->id, 'course' => $course->id,
'timeopen' => $timeopen->getTimestamp() 'timeopen' => $timeopen->getTimestamp()
@ -467,12 +464,12 @@ class mod_quiz_calendar_event_modified_testcase extends advanced_testcase {
$teacher = $generator->create_user(); $teacher = $generator->create_user();
$student = $generator->create_user(); $student = $generator->create_user();
$course = $generator->create_course(); $course = $generator->create_course();
$context = context_course::instance($course->id); $context = \context_course::instance($course->id);
$roleid = $generator->create_role(); $roleid = $generator->create_role();
$now = time(); $now = time();
$timelimit = 600; $timelimit = 600;
$timeopen = (new DateTime())->setTimestamp($now); $timeopen = (new \DateTime())->setTimestamp($now);
$timeclose = (new DateTime())->setTimestamp($now)->modify('+1 day'); $timeclose = (new \DateTime())->setTimestamp($now)->modify('+1 day');
// The new close time being earlier than the time open + time limit should // The new close time being earlier than the time open + time limit should
// result in an update to the quiz attempts. // result in an update to the quiz attempts.
$newtimeclose = $timeopen->getTimestamp() + $timelimit - 10; $newtimeclose = $timeopen->getTimestamp() + $timelimit - 10;

View file

@ -14,16 +14,7 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** namespace mod_quiz;
* Unit tests for the {@link \mod_quiz\local\structure\slot_random} class.
*
* @package mod_quiz
* @category test
* @copyright 2018 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
use mod_quiz\question\bank\qbank_helper; use mod_quiz\question\bank\qbank_helper;
@ -31,10 +22,12 @@ use mod_quiz\question\bank\qbank_helper;
* Class mod_quiz_local_structure_slot_random_test * Class mod_quiz_local_structure_slot_random_test
* Class for tests related to the {@link \mod_quiz\local\structure\slot_random} class. * Class for tests related to the {@link \mod_quiz\local\structure\slot_random} class.
* *
* @package mod_quiz
* @category test
* @copyright 2018 Shamim Rezaie <shamim@moodle.com> * @copyright 2018 Shamim Rezaie <shamim@moodle.com>
* @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_local_structure_slot_random_test extends advanced_testcase { class local_structure_slot_random_test extends \advanced_testcase {
/** /**
* Constructor test. * Constructor test.
*/ */
@ -54,7 +47,7 @@ class mod_quiz_local_structure_slot_random_test extends advanced_testcase {
// Create a random question without adding it to a quiz. // 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. // 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->category = $category->id . ',' . $category->contextid;
$form->includesubcategories = true; $form->includesubcategories = true;
$form->fromtags = []; $form->fromtags = [];
@ -63,22 +56,22 @@ class mod_quiz_local_structure_slot_random_test extends advanced_testcase {
$form->stamp = make_unique_id_code(); $form->stamp = make_unique_id_code();
// Set the filter conditions. // Set the filter conditions.
$filtercondition = new stdClass(); $filtercondition = new \stdClass();
$filtercondition->questioncategoryid = $category->id; $filtercondition->questioncategoryid = $category->id;
$filtercondition->includingsubcategories = 1; $filtercondition->includingsubcategories = 1;
// Slot data. // Slot data.
$randomslotdata = new stdClass(); $randomslotdata = new \stdClass();
$randomslotdata->quizid = $quiz->id; $randomslotdata->quizid = $quiz->id;
$randomslotdata->maxmark = 1; $randomslotdata->maxmark = 1;
$randomslotdata->usingcontextid = context_module::instance($quiz->cmid)->id; $randomslotdata->usingcontextid = \context_module::instance($quiz->cmid)->id;
$randomslotdata->questionscontextid = $category->contextid; $randomslotdata->questionscontextid = $category->contextid;
// Insert the random question to the quiz. // Insert the random question to the quiz.
$randomslot = new \mod_quiz\local\structure\slot_random($randomslotdata); $randomslot = new \mod_quiz\local\structure\slot_random($randomslotdata);
$randomslot->set_filter_condition($filtercondition); $randomslot->set_filter_condition($filtercondition);
$rc = new ReflectionClass('\mod_quiz\local\structure\slot_random'); $rc = new \ReflectionClass('\mod_quiz\local\structure\slot_random');
$rcp = $rc->getProperty('filtercondition'); $rcp = $rc->getProperty('filtercondition');
$rcp->setAccessible(true); $rcp->setAccessible(true);
$record = json_decode($rcp->getValue($randomslot)); $record = json_decode($rcp->getValue($randomslot));
@ -110,15 +103,15 @@ class mod_quiz_local_structure_slot_random_test extends advanced_testcase {
quiz_add_random_questions($quiz, 0, $category->id, 1, false); quiz_add_random_questions($quiz, 0, $category->id, 1, false);
// Set the filter conditions. // Set the filter conditions.
$filtercondition = new stdClass(); $filtercondition = new \stdClass();
$filtercondition->questioncategoryid = $category->id; $filtercondition->questioncategoryid = $category->id;
$filtercondition->includingsubcategories = 1; $filtercondition->includingsubcategories = 1;
// Slot data. // Slot data.
$randomslotdata = new stdClass(); $randomslotdata = new \stdClass();
$randomslotdata->quizid = $quiz->id; $randomslotdata->quizid = $quiz->id;
$randomslotdata->maxmark = 1; $randomslotdata->maxmark = 1;
$randomslotdata->usingcontextid = context_module::instance($quiz->cmid)->id; $randomslotdata->usingcontextid = \context_module::instance($quiz->cmid)->id;
$randomslotdata->questionscontextid = $category->contextid; $randomslotdata->questionscontextid = $category->contextid;
$randomslot = new \mod_quiz\local\structure\slot_random($randomslotdata); $randomslot = new \mod_quiz\local\structure\slot_random($randomslotdata);
@ -147,15 +140,15 @@ class mod_quiz_local_structure_slot_random_test extends advanced_testcase {
quiz_add_random_questions($quiz, 0, $category->id, 1, false); quiz_add_random_questions($quiz, 0, $category->id, 1, false);
// Set the filter conditions. // Set the filter conditions.
$filtercondition = new stdClass(); $filtercondition = new \stdClass();
$filtercondition->questioncategoryid = $category->id; $filtercondition->questioncategoryid = $category->id;
$filtercondition->includingsubcategories = 1; $filtercondition->includingsubcategories = 1;
// Slot data. // Slot data.
$randomslotdata = new stdClass(); $randomslotdata = new \stdClass();
$randomslotdata->quizid = $quiz->id; $randomslotdata->quizid = $quiz->id;
$randomslotdata->maxmark = 1; $randomslotdata->maxmark = 1;
$randomslotdata->usingcontextid = context_module::instance($quiz->cmid)->id; $randomslotdata->usingcontextid = \context_module::instance($quiz->cmid)->id;
$randomslotdata->questionscontextid = $category->contextid; $randomslotdata->questionscontextid = $category->contextid;
$randomslot = new \mod_quiz\local\structure\slot_random($randomslotdata); $randomslot = new \mod_quiz\local\structure\slot_random($randomslotdata);
@ -166,7 +159,7 @@ class mod_quiz_local_structure_slot_random_test extends advanced_testcase {
$randomslot->set_quiz($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 = $rc->getProperty('quiz');
$rcp->setAccessible(true); $rcp->setAccessible(true);
$quizpropery = $rcp->getValue($randomslot); $quizpropery = $rcp->getValue($randomslot);
@ -188,10 +181,10 @@ class mod_quiz_local_structure_slot_random_test extends advanced_testcase {
quiz_add_random_questions($quiz, 0, $category->id, 1, false); quiz_add_random_questions($quiz, 0, $category->id, 1, false);
// Slot data. // Slot data.
$randomslotdata = new stdClass(); $randomslotdata = new \stdClass();
$randomslotdata->quizid = $quiz->id; $randomslotdata->quizid = $quiz->id;
$randomslotdata->maxmark = 1; $randomslotdata->maxmark = 1;
$randomslotdata->usingcontextid = context_module::instance($quiz->cmid)->id; $randomslotdata->usingcontextid = \context_module::instance($quiz->cmid)->id;
$randomslotdata->questionscontextid = $category->contextid; $randomslotdata->questionscontextid = $category->contextid;
$randomslot = new \mod_quiz\local\structure\slot_random($randomslotdata); $randomslot = new \mod_quiz\local\structure\slot_random($randomslotdata);
@ -215,11 +208,11 @@ class mod_quiz_local_structure_slot_random_test extends advanced_testcase {
$this->setAdminUser(); $this->setAdminUser();
list($randomslot, $tags) = $this->setup_for_test_tags(['foo', 'bar']); list($randomslot, $tags) = $this->setup_for_test_tags(['foo', 'bar']);
$filtercondition = new stdClass(); $filtercondition = new \stdClass();
$randomslot->set_tags([$tags['foo'], $tags['bar']]); $randomslot->set_tags([$tags['foo'], $tags['bar']]);
$randomslot->set_filter_condition($filtercondition); $randomslot->set_filter_condition($filtercondition);
$rc = new ReflectionClass('\mod_quiz\local\structure\slot_random'); $rc = new \ReflectionClass('\mod_quiz\local\structure\slot_random');
$rcp = $rc->getProperty('filtercondition'); $rcp = $rc->getProperty('filtercondition');
$rcp->setAccessible(true); $rcp->setAccessible(true);
$tagspropery = $rcp->getValue($randomslot); $tagspropery = $rcp->getValue($randomslot);
@ -237,13 +230,13 @@ class mod_quiz_local_structure_slot_random_test extends advanced_testcase {
list($randomslot, $tags) = $this->setup_for_test_tags(['foo', 'bar', 'baz']); list($randomslot, $tags) = $this->setup_for_test_tags(['foo', 'bar', 'baz']);
// Set tags for the first time. // Set tags for the first time.
$filtercondition = new stdClass(); $filtercondition = new \stdClass();
$randomslot->set_tags([$tags['foo'], $tags['bar']]); $randomslot->set_tags([$tags['foo'], $tags['bar']]);
// Now set the tags again. // Now set the tags again.
$randomslot->set_tags([$tags['baz']]); $randomslot->set_tags([$tags['baz']]);
$randomslot->set_filter_condition($filtercondition); $randomslot->set_filter_condition($filtercondition);
$rc = new ReflectionClass('\mod_quiz\local\structure\slot_random'); $rc = new \ReflectionClass('\mod_quiz\local\structure\slot_random');
$rcp = $rc->getProperty('filtercondition'); $rcp = $rc->getProperty('filtercondition');
$rcp->setAccessible(true); $rcp->setAccessible(true);
$tagspropery = $rcp->getValue($randomslot); $tagspropery = $rcp->getValue($randomslot);
@ -258,11 +251,11 @@ class mod_quiz_local_structure_slot_random_test extends advanced_testcase {
$this->setAdminUser(); $this->setAdminUser();
list($randomslot, $tags) = $this->setup_for_test_tags(['foo', 'bar', 'baz']); list($randomslot, $tags) = $this->setup_for_test_tags(['foo', 'bar', 'baz']);
$filtercondition = new stdClass(); $filtercondition = new \stdClass();
$randomslot->set_tags([$tags['foo'], $tags['bar'], $tags['foo']]); $randomslot->set_tags([$tags['foo'], $tags['bar'], $tags['foo']]);
$randomslot->set_filter_condition($filtercondition); $randomslot->set_filter_condition($filtercondition);
$rc = new ReflectionClass('\mod_quiz\local\structure\slot_random'); $rc = new \ReflectionClass('\mod_quiz\local\structure\slot_random');
$rcp = $rc->getProperty('filtercondition'); $rcp = $rc->getProperty('filtercondition');
$rcp->setAccessible(true); $rcp->setAccessible(true);
$tagspropery = $rcp->getValue($randomslot); $tagspropery = $rcp->getValue($randomslot);
@ -278,11 +271,11 @@ class mod_quiz_local_structure_slot_random_test extends advanced_testcase {
$this->setAdminUser(); $this->setAdminUser();
list($randomslot, $tags) = $this->setup_for_test_tags(['foo', 'bar', 'baz']); list($randomslot, $tags) = $this->setup_for_test_tags(['foo', 'bar', 'baz']);
$filtercondition = new stdClass(); $filtercondition = new \stdClass();
$randomslot->set_tags_by_id([$tags['foo']->id, $tags['bar']->id]); $randomslot->set_tags_by_id([$tags['foo']->id, $tags['bar']->id]);
$randomslot->set_filter_condition($filtercondition); $randomslot->set_filter_condition($filtercondition);
$rc = new ReflectionClass('\mod_quiz\local\structure\slot_random'); $rc = new \ReflectionClass('\mod_quiz\local\structure\slot_random');
$rcp = $rc->getProperty('tags'); $rcp = $rc->getProperty('tags');
$rcp->setAccessible(true); $rcp->setAccessible(true);
$tagspropery = $rcp->getValue($randomslot); $tagspropery = $rcp->getValue($randomslot);
@ -312,7 +305,7 @@ class mod_quiz_local_structure_slot_random_test extends advanced_testcase {
// Now set the tags again. // Now set the tags again.
$randomslot->set_tags_by_id([$tags['baz']->id]); $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 = $rc->getProperty('tags');
$rcp->setAccessible(true); $rcp->setAccessible(true);
$tagspropery = $rcp->getValue($randomslot); $tagspropery = $rcp->getValue($randomslot);
@ -334,7 +327,7 @@ class mod_quiz_local_structure_slot_random_test extends advanced_testcase {
$randomslot->set_tags_by_id([$tags['foo']->id, $tags['bar']->id], $tags['foo']->id); $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 = $rc->getProperty('tags');
$rcp->setAccessible(true); $rcp->setAccessible(true);
$tagspropery = $rcp->getValue($randomslot); $tagspropery = $rcp->getValue($randomslot);
@ -362,14 +355,14 @@ class mod_quiz_local_structure_slot_random_test extends advanced_testcase {
// Create a quiz. // Create a quiz.
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz'); $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
$quiz = $quizgenerator->create_instance(array('course' => $SITE->id, 'questionsperpage' => 3, 'grade' => 100.0)); $quiz = $quizgenerator->create_instance(array('course' => $SITE->id, 'questionsperpage' => 3, 'grade' => 100.0));
$quizcontext = context_module::instance($quiz->cmid); $quizcontext = \context_module::instance($quiz->cmid);
// Create a question category in the system context. // Create a question category in the system context.
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
$category = $questiongenerator->create_question_category(); $category = $questiongenerator->create_question_category();
// Create a random question without adding it to a quiz. // Create a random question without adding it to a quiz.
$form = new stdClass(); $form = new \stdClass();
$form->category = $category->id . ',' . $category->contextid; $form->category = $category->id . ',' . $category->contextid;
$form->includesubcategories = true; $form->includesubcategories = true;
$form->fromtags = []; $form->fromtags = [];
@ -395,12 +388,12 @@ class mod_quiz_local_structure_slot_random_test extends advanced_testcase {
// Set the filter conditions. // Set the filter conditions.
$filtercondition = new stdClass(); $filtercondition = new \stdClass();
$filtercondition->questioncategoryid = $category->id; $filtercondition->questioncategoryid = $category->id;
$filtercondition->includingsubcategories = 1; $filtercondition->includingsubcategories = 1;
// Slot data. // Slot data.
$randomslotdata = new stdClass(); $randomslotdata = new \stdClass();
$randomslotdata->quizid = $quiz->id; $randomslotdata->quizid = $quiz->id;
$randomslotdata->maxmark = 1; $randomslotdata->maxmark = 1;
$randomslotdata->usingcontextid = $quizcontext->id; $randomslotdata->usingcontextid = $quizcontext->id;

View file

@ -17,12 +17,14 @@
/** /**
* Unit tests for the privacy legacy polyfill for quiz access rules. * Unit tests for the privacy legacy polyfill for quiz access rules.
* *
* @package mod_quiz
* @category test
* @copyright 2018 Andrew Nicols <andrew@nicols.co.uk> * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
* @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
*/ */
namespace mod_quiz;
use quiz;
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
global $CFG; global $CFG;
@ -34,7 +36,7 @@ require_once($CFG->dirroot . '/mod/quiz/attemptlib.php');
* @copyright 2018 Andrew Nicols <andrew@nicols.co.uk> * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
* @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 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 * Test that the core_quizaccess\privacy\legacy_polyfill works and that the static _export_quizaccess_user_data can
* be called. * be called.
@ -59,7 +61,7 @@ class core_privacy_legacy_quizaccess_polyfill_test extends advanced_testcase {
* Test the _delete_quizaccess_for_context shim. * Test the _delete_quizaccess_for_context shim.
*/ */
public function test_delete_quizaccess_for_context() { public function test_delete_quizaccess_for_context() {
$context = context_system::instance(); $context = \context_system::instance();
$quiz = $this->createMock(quiz::class); $quiz = $this->createMock(quiz::class);
@ -76,7 +78,7 @@ class core_privacy_legacy_quizaccess_polyfill_test extends advanced_testcase {
* Test the _delete_quizaccess_for_user shim. * Test the _delete_quizaccess_for_user shim.
*/ */
public function test_delete_quizaccess_for_user() { public function test_delete_quizaccess_for_user() {
$context = context_system::instance(); $context = \context_system::instance();
$quiz = $this->createMock(quiz::class); $quiz = $this->createMock(quiz::class);
$user = (object) []; $user = (object) [];
@ -94,7 +96,7 @@ class core_privacy_legacy_quizaccess_polyfill_test extends advanced_testcase {
* Test the _delete_quizaccess_for_users shim. * Test the _delete_quizaccess_for_users shim.
*/ */
public function test_delete_quizaccess_for_users() { public function test_delete_quizaccess_for_users() {
$context = $this->createMock(context_module::class); $context = $this->createMock(\context_module::class);
$user = (object) []; $user = (object) [];
$approveduserlist = new \core_privacy\local\request\approved_userlist($context, 'mod_quiz', [$user]); $approveduserlist = new \core_privacy\local\request\approved_userlist($context, 'mod_quiz', [$user]);

View file

@ -14,6 +14,16 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mod_quiz;
use core_question\local\bank\question_edit_contexts;
use mod_quiz\question\bank\custom_view;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/question/editlib.php');
/** /**
* Unit tests for the quiz's own question bank view class. * Unit tests for the quiz's own question bank view class.
* *
@ -22,20 +32,7 @@
* @copyright 2018 the Open University * @copyright 2018 the Open University
* @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 quiz_question_bank_view_test extends \advanced_testcase {
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/question/editlib.php');
/**
* Unit tests for the quiz's own question bank view class.
*
* @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 {
public function test_viewing_question_bank_should_not_load_individual_questions() { public function test_viewing_question_bank_should_not_load_individual_questions() {
$this->resetAfterTest(); $this->resetAfterTest();
@ -47,21 +44,21 @@ class quiz_question_bank_view_testcase extends advanced_testcase {
// Create a course and a quiz. // Create a course and a quiz.
$course = $generator->create_course(); $course = $generator->create_course();
$quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id)); $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); $cm = get_coursemodule_from_instance('quiz', $quiz->id);
// Create a question in the default category. // Create a question in the default category.
$contexts = new core_question\local\bank\question_edit_contexts($context); $contexts = new question_edit_contexts($context);
$cat = question_make_default_categories($contexts->all()); $cat = question_make_default_categories($contexts->all());
$questiondata = $questiongenerator->create_question('numerical', null, $questiondata = $questiongenerator->create_question('numerical', null,
['name' => 'Example question', 'category' => $cat->id]); ['name' => 'Example question', 'category' => $cat->id]);
// Ensure the question is not in the cache. // Ensure the question is not in the cache.
$cache = cache::make('core', 'questiondata'); $cache = \cache::make('core', 'questiondata');
$cache->delete($questiondata->id); $cache->delete($questiondata->id);
// Generate the view. // 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(); ob_start();
$pagevars = [ $pagevars = [
'qpage' => 0, 'qpage' => 0,

View file

@ -14,31 +14,26 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** namespace mod_quiz;
* 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
*/
use mod_quiz_display_options;
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
global $CFG; global $CFG;
require_once($CFG->dirroot . '/mod/quiz/locallib.php'); require_once($CFG->dirroot . '/mod/quiz/locallib.php');
/** /**
* Unit tests for {@link mod_quiz_display_options}. * Unit tests for {@link mod_quiz_display_options}.
* *
* @package mod_quiz
* @category test
* @copyright 2010 The Open University * @copyright 2010 The Open University
* @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_display_options_testcase extends basic_testcase { class quizdisplayoptions_test extends \basic_testcase {
public function test_num_attempts_access_rule() { public function test_num_attempts_access_rule() {
$quiz = new stdClass(); $quiz = new \stdClass();
$quiz->decimalpoints = 2; $quiz->decimalpoints = 2;
$quiz->questiondecimalpoints = -1; $quiz->questiondecimalpoints = -1;
$quiz->reviewattempt = 0x11110; $quiz->reviewattempt = 0x11110;

View file

@ -14,38 +14,34 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** namespace mod_quiz;
* 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
*/
use mod_quiz_display_options;
use quiz;
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
global $CFG; global $CFG;
require_once($CFG->dirroot . '/mod/quiz/locallib.php'); require_once($CFG->dirroot . '/mod/quiz/locallib.php');
/** /**
* Unit tests for the quiz class * Unit tests for the quiz class
* *
* @package mod_quiz
* @copyright 2008 The Open University * @copyright 2008 The Open University
* @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_class_testcase extends basic_testcase { class quizobj_test extends \basic_testcase {
public function test_cannot_review_message() { public function test_cannot_review_message() {
$quiz = new stdClass(); $quiz = new \stdClass();
$quiz->reviewattempt = 0x10010; $quiz->reviewattempt = 0x10010;
$quiz->timeclose = 0; $quiz->timeclose = 0;
$quiz->attempts = 0; $quiz->attempts = 0;
$cm = new stdClass(); $cm = new \stdClass();
$cm->id = 123; $cm->id = 123;
$quizobj = new quiz($quiz, $cm, new stdClass(), false); $quizobj = new quiz($quiz, $cm, new \stdClass(), false);
$this->assertEquals('', $this->assertEquals('',
$quizobj->cannot_review_message(mod_quiz_display_options::DURING)); $quizobj->cannot_review_message(mod_quiz_display_options::DURING));
@ -58,7 +54,7 @@ class mod_quiz_class_testcase extends basic_testcase {
$closetime = time() + 10000; $closetime = time() + 10000;
$quiz->timeclose = $closetime; $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)), $this->assertEquals(get_string('noreviewuntil', 'quiz', userdate($closetime)),
$quizobj->cannot_review_message(mod_quiz_display_options::LATER_WHILE_OPEN)); $quizobj->cannot_review_message(mod_quiz_display_options::LATER_WHILE_OPEN));

View file

@ -22,19 +22,22 @@
* @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
*/ */
namespace mod_quiz;
use quiz;
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
global $CFG; global $CFG;
require_once($CFG->dirroot . '/mod/quiz/locallib.php'); require_once($CFG->dirroot . '/mod/quiz/locallib.php');
require_once($CFG->dirroot . '/mod/quiz/classes/repaginate.php'); require_once($CFG->dirroot . '/mod/quiz/classes/repaginate.php');
/** /**
* Testable subclass, giving access to the protected methods of {@link \mod_quiz\repaginate} * Testable subclass, giving access to the protected methods of {@link \mod_quiz\repaginate}
* @copyright 2014 The Open Univsersity * @copyright 2014 The Open Univsersity
* @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_repaginate_testable extends \mod_quiz\repaginate { class mod_quiz_repaginate_testable extends repaginate {
public function __construct($quizid = 0, $slots = null) { public function __construct($quizid = 0, $slots = null) {
return parent::__construct($quizid, $slots); return parent::__construct($quizid, $slots);
@ -61,7 +64,7 @@ class mod_quiz_repaginate_testable extends \mod_quiz\repaginate {
* @copyright 2014 The Open University * @copyright 2014 The Open University
* @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_repaginate_test extends advanced_testcase { class repaginate_test extends \advanced_testcase {
/** @var array stores the slots. */ /** @var array stores the slots. */
private $quizslots; private $quizslots;
@ -111,7 +114,7 @@ class mod_quiz_repaginate_test extends advanced_testcase {
// Return the quiz object. // Return the quiz object.
$quizobj = new quiz($quiz, $cm, $SITE); $quizobj = new quiz($quiz, $cm, $SITE);
return \mod_quiz\structure::create_for_quiz($quizobj); return structure::create_for_quiz($quizobj);
} }
/** /**
@ -261,7 +264,7 @@ class mod_quiz_repaginate_test extends advanced_testcase {
public function test_repaginate_the_rest() { public function test_repaginate_the_rest() {
$this->set_quiz_slots(); $this->set_quiz_slots();
$slotfrom = 1; $slotfrom = 1;
$type = \mod_quiz\repaginate::LINK; $type = repaginate::LINK;
$expected = array(); $expected = array();
foreach ($this->quizslots as $slot) { foreach ($this->quizslots as $slot) {
if ($slot->slot > $slotfrom) { if ($slot->slot > $slotfrom) {
@ -281,7 +284,7 @@ class mod_quiz_repaginate_test extends advanced_testcase {
$newslots[$s->id] = $s; $newslots[$s->id] = $s;
} }
$type = \mod_quiz\repaginate::UNLINK; $type = repaginate::UNLINK;
$expected = array(); $expected = array();
foreach ($this->quizslots as $slot) { foreach ($this->quizslots as $slot) {
if ($slot->slot > ($slotfrom - 1)) { if ($slot->slot > ($slotfrom - 1)) {

View file

@ -14,15 +14,9 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** namespace mod_quiz;
* 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
*/
use quiz_attempt;
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
@ -33,13 +27,15 @@ require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
/** /**
* This class contains the test cases for the functions in reportlib.php. * This class contains the test cases for the functions in reportlib.php.
* *
* @package mod_quiz
* @category test
* @copyright 2008 Jamie Pratt me@jamiep.org * @copyright 2008 Jamie Pratt me@jamiep.org
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License * @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() { public function test_quiz_report_index_by_keys() {
$datum = array(); $datum = array();
$object = new stdClass(); $object = new \stdClass();
$object->qid = 3; $object->qid = 3;
$object->aid = 101; $object->aid = 101;
$object->response = ''; $object->response = '';
@ -62,7 +58,7 @@ class mod_quiz_reportlib_testcase extends advanced_testcase {
} }
public function test_quiz_report_scale_summarks_as_percentage() { public function test_quiz_report_scale_summarks_as_percentage() {
$quiz = new stdClass(); $quiz = new \stdClass();
$quiz->sumgrades = 10; $quiz->sumgrades = 10;
$quiz->decimalpoints = 2; $quiz->decimalpoints = 2;
@ -75,13 +71,13 @@ class mod_quiz_reportlib_testcase extends advanced_testcase {
} }
public function test_quiz_report_qm_filter_select_only_one_attempt_allowed() { public function test_quiz_report_qm_filter_select_only_one_attempt_allowed() {
$quiz = new stdClass(); $quiz = new \stdClass();
$quiz->attempts = 1; $quiz->attempts = 1;
$this->assertSame('', quiz_report_qm_filter_select($quiz)); $this->assertSame('', quiz_report_qm_filter_select($quiz));
} }
public function test_quiz_report_qm_filter_select_average() { public function test_quiz_report_qm_filter_select_average() {
$quiz = new stdClass(); $quiz = new \stdClass();
$quiz->attempts = 10; $quiz->attempts = 10;
$quiz->grademethod = QUIZ_GRADEAVERAGE; $quiz->grademethod = QUIZ_GRADEAVERAGE;
$this->assertSame('', quiz_report_qm_filter_select($quiz)); $this->assertSame('', quiz_report_qm_filter_select($quiz));
@ -91,7 +87,7 @@ class mod_quiz_reportlib_testcase extends advanced_testcase {
global $DB; global $DB;
$this->resetAfterTest(); $this->resetAfterTest();
$fakeattempt = new stdClass(); $fakeattempt = new \stdClass();
$fakeattempt->userid = 123; $fakeattempt->userid = 123;
$fakeattempt->quiz = 456; $fakeattempt->quiz = 456;
$fakeattempt->layout = '1,2,0,3,4,0,5'; $fakeattempt->layout = '1,2,0,3,4,0,5';
@ -137,7 +133,7 @@ class mod_quiz_reportlib_testcase extends advanced_testcase {
$fakeattempt->uniqueid = 65; $fakeattempt->uniqueid = 65;
$DB->insert_record('quiz_attempts', $fakeattempt); $DB->insert_record('quiz_attempts', $fakeattempt);
$quiz = new stdClass(); $quiz = new \stdClass();
$quiz->attempts = 10; $quiz->attempts = 10;
$quiz->grademethod = QUIZ_ATTEMPTFIRST; $quiz->grademethod = QUIZ_ATTEMPTFIRST;

View file

@ -14,14 +14,7 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** namespace mod_quiz;
* Restore override tests.
*
* @package mod_quiz
* @author 2019 Nathan Nguyen <nathannguyen@catalyst-au.net>
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
@ -35,7 +28,7 @@ require_once($CFG->libdir . "/phpunit/classes/restore_date_testcase.php");
* @copyright Catalyst IT * @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. * Test restore overrides.

View file

@ -14,17 +14,10 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** namespace mod_quiz;
* 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
*/
use mod_quiz\question\bank\qbank_helper; use mod_quiz\question\bank\qbank_helper;
use mod_quiz\structure; use quiz;
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
@ -34,10 +27,12 @@ require_once($CFG->dirroot . '/mod/quiz/attemptlib.php');
/** /**
* Unit tests for quiz events. * Unit tests for quiz events.
* *
* @package mod_quiz
* @category test
* @copyright 2013 Adrian Greeve * @copyright 2013 Adrian Greeve
* @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_structure_testcase extends advanced_testcase { class structure_test extends \advanced_testcase {
/** /**
* Create a course with an empty quiz. * Create a course with an empty quiz.
@ -92,7 +87,7 @@ class mod_quiz_structure_testcase extends advanced_testcase {
foreach ($layout as $item) { foreach ($layout as $item) {
if (is_string($item)) { if (is_string($item)) {
if (isset($headings[$lastpage + 1])) { 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; $headings[$lastpage + 1] = $item;
@ -100,7 +95,7 @@ class mod_quiz_structure_testcase extends advanced_testcase {
list($name, $page, $qtype) = $item; list($name, $page, $qtype) = $item;
if ($page < 1 || !($page == $lastpage + 1 || if ($page < 1 || !($page == $lastpage + 1 ||
(!isset($headings[$lastpage + 1]) && $page == $lastpage))) { (!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, $q = $questiongenerator->create_question($qtype, null,
array('name' => $name, 'category' => $cat->id)); array('name' => $name, 'category' => $cat->id));
@ -272,7 +267,7 @@ class mod_quiz_structure_testcase extends advanced_testcase {
$sections = $structure->get_sections(); $sections = $structure->get_sections();
$section = reset($sections); $section = reset($sections);
$this->expectException(coding_exception::class); $this->expectException(\coding_exception::class);
$structure->remove_section_heading($section->id); $structure->remove_section_heading($section->id);
} }
@ -447,7 +442,7 @@ class mod_quiz_structure_testcase extends advanced_testcase {
$idtomove = $structure->get_question_in_slot(3)->slotid; $idtomove = $structure->get_question_in_slot(3)->slotid;
$idmoveafter = $structure->get_question_in_slot(2)->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'); $structure->move_slot($idtomove, $idmoveafter, '1');
} }
@ -461,7 +456,7 @@ class mod_quiz_structure_testcase extends advanced_testcase {
$idtomove = $structure->get_question_in_slot(1)->slotid; $idtomove = $structure->get_question_in_slot(1)->slotid;
$idmoveafter = $structure->get_question_in_slot(2)->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'); $structure->move_slot($idtomove, $idmoveafter, '4');
} }
@ -737,7 +732,7 @@ class mod_quiz_structure_testcase extends advanced_testcase {
$structure = structure::create_for_quiz($quizobj); $structure = structure::create_for_quiz($quizobj);
$structure->remove_slot(1); $structure->remove_slot(1);
$this->expectException(coding_exception::class); $this->expectException(\coding_exception::class);
$structure->remove_slot(2); $structure->remove_slot(2);
} }
@ -750,7 +745,7 @@ class mod_quiz_structure_testcase extends advanced_testcase {
)); ));
$structure = 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); $structure->remove_slot(3);
} }
@ -860,7 +855,7 @@ class mod_quiz_structure_testcase extends advanced_testcase {
$structure = structure::create_for_quiz($quizobj); $structure = structure::create_for_quiz($quizobj);
$slotid = $structure->get_question_in_slot(2)->slotid; $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 = structure::create_for_quiz($quizobj); $structure = structure::create_for_quiz($quizobj);
$this->assert_quiz_layout(array( $this->assert_quiz_layout(array(
@ -877,7 +872,7 @@ class mod_quiz_structure_testcase extends advanced_testcase {
$structure = structure::create_for_quiz($quizobj); $structure = structure::create_for_quiz($quizobj);
$slotid = $structure->get_question_in_slot(2)->slotid; $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 = structure::create_for_quiz($quizobj); $structure = structure::create_for_quiz($quizobj);
$this->assert_quiz_layout(array( $this->assert_quiz_layout(array(

View file

@ -14,16 +14,21 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mod_quiz;
use mod_quiz\question\bank\qbank_helper; use mod_quiz\question\bank\qbank_helper;
use quiz;
/** /**
* Test the restore of random question tags. * Test the restore of random question tags.
* *
* @package mod_quiz
* @category test
* @copyright 2018 Shamim Rezaie <shamim@moodle.com> * @copyright 2018 Shamim Rezaie <shamim@moodle.com>
* @author 2021 Safat Shahin <safatshahin@catalyst-au.net> * @author 2021 Safat Shahin <safatshahin@catalyst-au.net>
* @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_tags_testcase extends advanced_testcase { class tags_test extends \advanced_testcase {
public function test_restore_random_question_by_tag() { public function test_restore_random_question_by_tag() {
global $CFG, $USER, $DB; global $CFG, $USER, $DB;
@ -40,9 +45,9 @@ class mod_quiz_tags_testcase extends advanced_testcase {
// Do the restore to new course with default settings. // Do the restore to new course with default settings.
$categoryid = $DB->get_field_sql("SELECT MIN(id) FROM {course_categories}"); $categoryid = $DB->get_field_sql("SELECT MIN(id) FROM {course_categories}");
$newcourseid = restore_dbops::create_new_course('Test fullname', 'Test shortname', $categoryid); $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, $rc = new \restore_controller($backupid, $newcourseid, \backup::INTERACTIVE_NO, \backup::MODE_GENERAL, $USER->id,
backup::TARGET_NEW_COURSE); \backup::TARGET_NEW_COURSE);
$this->assertTrue($rc->execute_precheck()); $this->assertTrue($rc->execute_precheck());
$rc->execute_plan(); $rc->execute_plan();
@ -52,7 +57,7 @@ class mod_quiz_tags_testcase extends advanced_testcase {
$modinfo = get_fast_modinfo($newcourseid); $modinfo = get_fast_modinfo($newcourseid);
$quiz = array_values($modinfo->get_instances_of('quiz'))[0]; $quiz = array_values($modinfo->get_instances_of('quiz'))[0];
$quizobj = quiz::create($quiz->instance); $quizobj = quiz::create($quiz->instance);
$structure = \mod_quiz\structure::create_for_quiz($quizobj); $structure = structure::create_for_quiz($quizobj);
// Are the correct slots returned? // Are the correct slots returned?
$slots = $structure->get_slots(); $slots = $structure->get_slots();
@ -66,13 +71,13 @@ class mod_quiz_tags_testcase extends advanced_testcase {
$question = array_values($questions)[0]; $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); $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); $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); $this->assertNotFalse($tag3);
$slottags = $this->get_tags_for_slot($question->slotid); $slottags = $this->get_tags_for_slot($question->slotid);
@ -80,7 +85,7 @@ class mod_quiz_tags_testcase extends advanced_testcase {
$slottags = explode(',', $slottags); $slottags = explode(',', $slottags);
$this->assertEquals("{$tag2->id},{$tag2->name}", "{$slottags[0]},{$slottags[1]}"); $this->assertEquals("{$tag2->id},{$tag2->name}", "{$slottags[0]},{$slottags[1]}");
$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->category); $this->assertEquals($defaultcategory->id, $question->category);
$randomincludingsubcategories = $DB->get_record('question_set_references', $randomincludingsubcategories = $DB->get_record('question_set_references',
['itemid' => reset($slots)->id, 'component' => 'mod_quiz', 'questionarea' => 'slot']); ['itemid' => reset($slots)->id, 'component' => 'mod_quiz', 'questionarea' => 'slot']);