diff --git a/admin/tool/lp/tests/externallib_test.php b/admin/tool/lp/tests/externallib_test.php index 338bdebdcb617..ec76951b8ccea 100644 --- a/admin/tool/lp/tests/externallib_test.php +++ b/admin/tool/lp/tests/externallib_test.php @@ -13,22 +13,10 @@ // // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * External learning plans webservice API tests. - * - * @package tool_lp - * @copyright 2015 Damyon Wiese - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -defined('MOODLE_INTERNAL') || die(); -global $CFG; - -require_once($CFG->dirroot . '/webservice/tests/helpers.php'); +namespace tool_lp; use core_competency\api; -use tool_lp\external; use core_competency\invalid_persistent_exception; use core_competency\plan; use core_competency\related_competency; @@ -38,6 +26,13 @@ use core_competency\template; use core_competency\template_competency; use core_competency\course_competency_settings; +use externallib_advanced_testcase; + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; + +require_once($CFG->dirroot . '/webservice/tests/helpers.php'); /** * External learning plans webservice API tests. @@ -46,24 +41,24 @@ * @copyright 2015 Damyon Wiese * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class tool_lp_external_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { - /** @var stdClass $creator User with enough permissions to create insystem context. */ + /** @var \stdClass $creator User with enough permissions to create insystem context. */ protected $creator = null; - /** @var stdClass $catcreator User with enough permissions to create incategory context. */ + /** @var \stdClass $catcreator User with enough permissions to create incategory context. */ protected $catcreator = null; - /** @var stdClass $category Category */ + /** @var \stdClass $category Category */ protected $category = null; - /** @var stdClass $category Category */ + /** @var \stdClass $category Category */ protected $othercategory = null; - /** @var stdClass $user User with enough permissions to view insystem context */ + /** @var \stdClass $user User with enough permissions to view insystem context */ protected $user = null; - /** @var stdClass $catuser User with enough permissions to view incategory context */ + /** @var \stdClass $catuser User with enough permissions to view incategory context */ protected $catuser = null; /** @var int Creator role id */ @@ -87,8 +82,8 @@ protected function setUp(): void { $catcreator = $this->getDataGenerator()->create_user(); $category = $this->getDataGenerator()->create_category(); $othercategory = $this->getDataGenerator()->create_category(); - $syscontext = context_system::instance(); - $catcontext = context_coursecat::instance($category->id); + $syscontext = \context_system::instance(); + $catcontext = \context_coursecat::instance($category->id); // Fetching default authenticated user role. $authrole = $DB->get_record('role', array('id' => $CFG->defaultuserroleid)); @@ -157,33 +152,33 @@ public function test_search_users_by_capability() { // First we search with no capability assigned. $this->setUser($ux); $result = external::search_users('yyylan', 'moodle/competency:planmanage'); - $result = external_api::clean_returnvalue(external::search_users_returns(), $result); + $result = \external_api::clean_returnvalue(external::search_users_returns(), $result); $this->assertCount(0, $result['users']); $this->assertEquals(0, $result['count']); // Now we assign a different capability. - $usercontext = context_user::instance($u1->id); - $systemcontext = context_system::instance(); + $usercontext = \context_user::instance($u1->id); + $systemcontext = \context_system::instance(); $customrole = $this->assignUserCapability('moodle/competency:planview', $usercontext->id); $result = external::search_users('yyylan', 'moodle/competency:planmanage'); - $result = external_api::clean_returnvalue(external::search_users_returns(), $result); + $result = \external_api::clean_returnvalue(external::search_users_returns(), $result); $this->assertCount(0, $result['users']); $this->assertEquals(0, $result['count']); // Now we assign a matching capability in the same role. - $usercontext = context_user::instance($u1->id); + $usercontext = \context_user::instance($u1->id); $this->assignUserCapability('moodle/competency:planmanage', $usercontext->id, $customrole); $result = external::search_users('yyylan', 'moodle/competency:planmanage'); - $result = external_api::clean_returnvalue(external::search_users_returns(), $result); + $result = \external_api::clean_returnvalue(external::search_users_returns(), $result); $this->assertCount(1, $result['users']); $this->assertEquals(1, $result['count']); // Now assign another role with the same capability (test duplicates). role_assign($this->creatorrole, $ux->id, $usercontext->id); $result = external::search_users('yyylan', 'moodle/competency:planmanage'); - $result = external_api::clean_returnvalue(external::search_users_returns(), $result); + $result = \external_api::clean_returnvalue(external::search_users_returns(), $result); $this->assertCount(1, $result['users']); $this->assertEquals(1, $result['count']); @@ -192,7 +187,7 @@ public function test_search_users_by_capability() { role_assign($this->creatorrole, $ux2->id, $systemcontext->id); $this->setUser($ux2); $result = external::search_users('yyylan', 'moodle/competency:planmanage'); - $result = external_api::clean_returnvalue(external::search_users_returns(), $result); + $result = \external_api::clean_returnvalue(external::search_users_returns(), $result); $this->assertCount(1, $result['users']); $this->assertEquals(1, $result['count']); @@ -201,7 +196,7 @@ public function test_search_users_by_capability() { role_assign($this->creatorrole, $ux3->id, $usercontext->id); $this->setUser($ux3); $result = external::search_users('yyylan', 'moodle/competency:planmanage'); - $result = external_api::clean_returnvalue(external::search_users_returns(), $result); + $result = \external_api::clean_returnvalue(external::search_users_returns(), $result); $this->assertCount(1, $result['users']); $this->assertEquals(1, $result['count']); @@ -211,14 +206,14 @@ public function test_search_users_by_capability() { // Now add a prevent override (will change nothing because we still have an ALLOW). assign_capability('moodle/competency:planmanage', CAP_PREVENT, $customrole, $usercontext->id); $result = external::search_users('yyylan', 'moodle/competency:planmanage'); - $result = external_api::clean_returnvalue(external::search_users_returns(), $result); + $result = \external_api::clean_returnvalue(external::search_users_returns(), $result); $this->assertCount(1, $result['users']); $this->assertEquals(1, $result['count']); // Now change to a prohibit override (should prevent access). assign_capability('moodle/competency:planmanage', CAP_PROHIBIT, $customrole, $usercontext->id); $result = external::search_users('yyylan', 'moodle/competency:planmanage'); - $result = external_api::clean_returnvalue(external::search_users_returns(), $result); + $result = \external_api::clean_returnvalue(external::search_users_returns(), $result); $this->assertCount(1, $result['users']); $this->assertEquals(1, $result['count']); @@ -237,10 +232,10 @@ public function test_search_users_by_capability_the_comeback() { $slave2 = $dg->create_user(array('lastname' => 'MOODLER')); $slave3 = $dg->create_user(array('lastname' => 'MOODLER')); - $syscontext = context_system::instance(); - $slave1context = context_user::instance($slave1->id); - $slave2context = context_user::instance($slave2->id); - $slave3context = context_user::instance($slave3->id); + $syscontext = \context_system::instance(); + $slave1context = \context_user::instance($slave1->id); + $slave2context = \context_user::instance($slave2->id); + $slave3context = \context_user::instance($slave3->id); // Creating a role giving the site config. $roleid = $dg->create_role(); @@ -306,19 +301,19 @@ public function test_search_users() { // We need to give the user the capability we are searching for on each of the test users. $this->setAdminUser(); - $usercontext = context_user::instance($u1->id); + $usercontext = \context_user::instance($u1->id); $dummyrole = $this->assignUserCapability('moodle/competency:planmanage', $usercontext->id); - $usercontext = context_user::instance($u2->id); + $usercontext = \context_user::instance($u2->id); $this->assignUserCapability('moodle/competency:planmanage', $usercontext->id, $dummyrole); - $usercontext = context_user::instance($u3->id); + $usercontext = \context_user::instance($u3->id); $this->assignUserCapability('moodle/competency:planmanage', $usercontext->id, $dummyrole); $this->setUser($ux); - $usercontext = context_user::instance($u1->id); + $usercontext = \context_user::instance($u1->id); $this->assignUserCapability('moodle/competency:planmanage', $usercontext->id, $dummyrole); - $usercontext = context_user::instance($u2->id); + $usercontext = \context_user::instance($u2->id); $this->assignUserCapability('moodle/competency:planmanage', $usercontext->id, $dummyrole); - $usercontext = context_user::instance($u3->id); + $usercontext = \context_user::instance($u3->id); $this->assignUserCapability('moodle/competency:planmanage', $usercontext->id, $dummyrole); $this->setAdminUser(); @@ -326,14 +321,14 @@ public function test_search_users() { // No identity fields. $CFG->showuseridentity = ''; $result = external::search_users('cats', 'moodle/competency:planmanage'); - $result = external_api::clean_returnvalue(external::search_users_returns(), $result); + $result = \external_api::clean_returnvalue(external::search_users_returns(), $result); $this->assertCount(0, $result['users']); $this->assertEquals(0, $result['count']); // Filter by name. $CFG->showuseridentity = ''; $result = external::search_users('dyyylan', 'moodle/competency:planmanage'); - $result = external_api::clean_returnvalue(external::search_users_returns(), $result); + $result = \external_api::clean_returnvalue(external::search_users_returns(), $result); $this->assertCount(2, $result['users']); $this->assertEquals(2, $result['count']); $this->assertEquals($u2->id, $result['users'][0]['id']); @@ -342,7 +337,7 @@ public function test_search_users() { // Filter by institution and name. $CFG->showuseridentity = 'institution'; $result = external::search_users('bob', 'moodle/competency:planmanage'); - $result = external_api::clean_returnvalue(external::search_users_returns(), $result); + $result = \external_api::clean_returnvalue(external::search_users_returns(), $result); $this->assertCount(2, $result['users']); $this->assertEquals(2, $result['count']); $this->assertEquals($u1->id, $result['users'][0]['id']); @@ -351,7 +346,7 @@ public function test_search_users() { // Filter by id number. $CFG->showuseridentity = 'idnumber'; $result = external::search_users('cats', 'moodle/competency:planmanage'); - $result = external_api::clean_returnvalue(external::search_users_returns(), $result); + $result = \external_api::clean_returnvalue(external::search_users_returns(), $result); $this->assertCount(1, $result['users']); $this->assertEquals(1, $result['count']); $this->assertEquals($u1->id, $result['users'][0]['id']); @@ -365,7 +360,7 @@ public function test_search_users() { // Filter by email. $CFG->showuseridentity = 'email'; $result = external::search_users('yyy', 'moodle/competency:planmanage'); - $result = external_api::clean_returnvalue(external::search_users_returns(), $result); + $result = \external_api::clean_returnvalue(external::search_users_returns(), $result); $this->assertCount(3, $result['users']); $this->assertEquals(3, $result['count']); $this->assertEquals($u2->id, $result['users'][0]['id']); @@ -378,7 +373,7 @@ public function test_search_users() { // Filter by any. $CFG->showuseridentity = 'idnumber,email,phone1,phone2,department,institution'; $result = external::search_users('yyy', 'moodle/competency:planmanage'); - $result = external_api::clean_returnvalue(external::search_users_returns(), $result); + $result = \external_api::clean_returnvalue(external::search_users_returns(), $result); $this->assertCount(3, $result['users']); $this->assertEquals(3, $result['count']); $this->assertArrayHasKey('idnumber', $result['users'][0]); @@ -394,12 +389,12 @@ public function test_search_users() { // Only names are included. $result = external::search_users('fish'); - $result = external_api::clean_returnvalue(external::search_users_returns(), $result); + $result = \external_api::clean_returnvalue(external::search_users_returns(), $result); $this->assertCount(0, $result['users']); $this->assertEquals(0, $result['count']); $result = external::search_users('bob', 'moodle/competency:planmanage'); - $result = external_api::clean_returnvalue(external::search_users_returns(), $result); + $result = \external_api::clean_returnvalue(external::search_users_returns(), $result); $this->assertCount(1, $result['users']); $this->assertEquals(1, $result['count']); $this->assertEquals($u1->id, $result['users'][0]['id']); diff --git a/admin/tool/mobile/tests/externallib_test.php b/admin/tool/mobile/tests/externallib_test.php index 8b3491c666f13..a4a730c1e995b 100644 --- a/admin/tool/mobile/tests/externallib_test.php +++ b/admin/tool/mobile/tests/externallib_test.php @@ -14,15 +14,9 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Moodle Mobile admin tool external functions tests. - * - * @package tool_mobile - * @category external - * @copyright 2016 Juan Leyva - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @since Moodle 3.1 - */ +namespace tool_mobile; + +use externallib_advanced_testcase; defined('MOODLE_INTERNAL') || die(); @@ -32,9 +26,6 @@ require_once($CFG->dirroot . '/admin/tool/mobile/tests/fixtures/output/mobile.php'); require_once($CFG->dirroot . '/webservice/lib.php'); -use tool_mobile\external; -use tool_mobile\api; - /** * Moodle Mobile admin tool external functions tests. * @@ -43,7 +34,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 3.1 */ -class tool_mobile_external_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * Test get_plugins_supporting_mobile. @@ -51,7 +42,7 @@ class tool_mobile_external_testcase extends externallib_advanced_testcase { */ public function test_get_plugins_supporting_mobile() { $result = external::get_plugins_supporting_mobile(); - $result = external_api::clean_returnvalue(external::get_plugins_supporting_mobile_returns(), $result); + $result = \external_api::clean_returnvalue(external::get_plugins_supporting_mobile_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertArrayHasKey('plugins', $result); $this->assertTrue(is_array($result['plugins'])); @@ -62,10 +53,10 @@ public function test_get_public_config() { $this->resetAfterTest(true); $result = external::get_public_config(); - $result = external_api::clean_returnvalue(external::get_public_config_returns(), $result); + $result = \external_api::clean_returnvalue(external::get_public_config_returns(), $result); // Test default values. - $context = context_system::instance(); + $context = \context_system::instance(); list($authinstructions, $notusedformat) = external_format_text($CFG->auth_instructions, FORMAT_MOODLE, $context->id); list($maintenancemessage, $notusedformat) = external_format_text($CFG->maintenance_message, FORMAT_MOODLE, $context->id); @@ -125,7 +116,7 @@ public function test_get_public_config() { $irecord = $issuer->to_record(); $irecord->clientid = 'mock'; $irecord->clientsecret = 'mock'; - core\oauth2\api::update_issuer($irecord); + \core\oauth2\api::update_issuer($irecord); set_config('hostname', 'localhost', 'auth_cas'); set_config('auth_logo', 'http://invalidurl.com//invalid/', 'auth_cas'); @@ -153,7 +144,7 @@ public function test_get_public_config() { } $result = external::get_public_config(); - $result = external_api::clean_returnvalue(external::get_public_config_returns(), $result); + $result = \external_api::clean_returnvalue(external::get_public_config_returns(), $result); // First check providers. $identityproviders = $result['identityproviders']; unset($result['identityproviders']); @@ -172,7 +163,7 @@ public function test_get_public_config() { $newurl = 'validimage.png'; set_config('auth_logo', $newurl, 'auth_cas'); $result = external::get_public_config(); - $result = external_api::clean_returnvalue(external::get_public_config_returns(), $result); + $result = \external_api::clean_returnvalue(external::get_public_config_returns(), $result); $this->assertStringContainsString($newurl, $result['identityproviders'][1]['iconurl']); } @@ -190,13 +181,13 @@ public function test_get_config() { set_config('supportemail', 'test@test.com'); $result = external::get_config(); - $result = external_api::clean_returnvalue(external::get_config_returns(), $result); + $result = \external_api::clean_returnvalue(external::get_config_returns(), $result); // SITE summary is null in phpunit which gets transformed to an empty string by format_text. - list($sitesummary, $unused) = external_format_text($SITE->summary, $SITE->summaryformat, context_system::instance()->id); + list($sitesummary, $unused) = external_format_text($SITE->summary, $SITE->summaryformat, \context_system::instance()->id); // Test default values. - $context = context_system::instance(); + $context = \context_system::instance(); $expected = array( array('name' => 'fullname', 'value' => $SITE->fullname), array('name' => 'shortname', 'value' => $SITE->shortname), @@ -251,7 +242,7 @@ public function test_get_config() { array_splice($expected, 11); $result = external::get_config('frontpagesettings'); - $result = external_api::clean_returnvalue(external::get_config_returns(), $result); + $result = \external_api::clean_returnvalue(external::get_config_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertEquals($expected, $result['settings']); } @@ -277,7 +268,7 @@ public function test_get_autologin_key() { $_GET['wstoken'] = $token->token; // Mock parameters. // Fake the app. - core_useragent::instance(true, 'Mozilla/5.0 (Linux; Android 7.1.1; Moto G Play Build/NPIS26.48-43-2; wv) ' . + \core_useragent::instance(true, 'Mozilla/5.0 (Linux; Android 7.1.1; Moto G Play Build/NPIS26.48-43-2; wv) ' . 'AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.99 Mobile Safari/537.36 MoodleMobile'); // Even if we force the password change for the current user we should be able to retrieve the key. @@ -285,9 +276,9 @@ public function test_get_autologin_key() { $this->setCurrentTimeStart(); $result = external::get_autologin_key($token->privatetoken); - $result = external_api::clean_returnvalue(external::get_autologin_key_returns(), $result); + $result = \external_api::clean_returnvalue(external::get_autologin_key_returns(), $result); // Validate the key. - $this->assertEquals(32, core_text::strlen($result['key'])); + $this->assertEquals(32, \core_text::strlen($result['key'])); $key = $DB->get_record('user_private_key', array('value' => $result['key'])); $this->assertEquals($USER->id, $key->userid); $this->assertTimeCurrent($key->validuntil - api::LOGIN_KEY_TTL); @@ -308,7 +299,7 @@ public function test_get_autologin_key_missing_ws() { $this->resetAfterTest(true); // Fake the app. - core_useragent::instance(true, 'Mozilla/5.0 (Linux; Android 7.1.1; Moto G Play Build/NPIS26.48-43-2; wv) ' . + \core_useragent::instance(true, 'Mozilla/5.0 (Linux; Android 7.1.1; Moto G Play Build/NPIS26.48-43-2; wv) ' . 'AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.99 Mobile Safari/537.36 MoodleMobile'); // Need to disable webservices to verify that's checked. @@ -328,7 +319,7 @@ public function test_get_autologin_key_missing_https() { global $CFG; // Fake the app. - core_useragent::instance(true, 'Mozilla/5.0 (Linux; Android 7.1.1; Moto G Play Build/NPIS26.48-43-2; wv) ' . + \core_useragent::instance(true, 'Mozilla/5.0 (Linux; Android 7.1.1; Moto G Play Build/NPIS26.48-43-2; wv) ' . 'AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.99 Mobile Safari/537.36 MoodleMobile'); // Need to simulate a non HTTPS site here. @@ -352,7 +343,7 @@ public function test_get_autologin_key_missing_admin() { $this->setAdminUser(); // Fake the app. - core_useragent::instance(true, 'Mozilla/5.0 (Linux; Android 7.1.1; Moto G Play Build/NPIS26.48-43-2; wv) ' . + \core_useragent::instance(true, 'Mozilla/5.0 (Linux; Android 7.1.1; Moto G Play Build/NPIS26.48-43-2; wv) ' . 'AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.99 Mobile Safari/537.36 MoodleMobile'); $this->expectException('moodle_exception'); @@ -376,17 +367,17 @@ public function test_get_autologin_key_missing_locked() { $_GET['wstoken'] = $token->token; // Mock parameters. // Fake the app. - core_useragent::instance(true, 'Mozilla/5.0 (Linux; Android 7.1.1; Moto G Play Build/NPIS26.48-43-2; wv) ' . + \core_useragent::instance(true, 'Mozilla/5.0 (Linux; Android 7.1.1; Moto G Play Build/NPIS26.48-43-2; wv) ' . 'AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.99 Mobile Safari/537.36 MoodleMobile'); $result = external::get_autologin_key($token->privatetoken); - $result = external_api::clean_returnvalue(external::get_autologin_key_returns(), $result); + $result = \external_api::clean_returnvalue(external::get_autologin_key_returns(), $result); // Mock last time request. $mocktime = time() - 7 * MINSECS; set_user_preference('tool_mobile_autologin_request_last', $mocktime, $USER); $result = external::get_autologin_key($token->privatetoken); - $result = external_api::clean_returnvalue(external::get_autologin_key_returns(), $result); + $result = \external_api::clean_returnvalue(external::get_autologin_key_returns(), $result); // Change min time between requests to 30 seconds. set_config('autologinmintimebetweenreq', 30, 'tool_mobile'); @@ -395,7 +386,7 @@ public function test_get_autologin_key_missing_locked() { $mocktime = time() - MINSECS; set_user_preference('tool_mobile_autologin_request_last', $mocktime, $USER); $result = external::get_autologin_key($token->privatetoken); // All good, we were expecint 30 seconds or more. - $result = external_api::clean_returnvalue(external::get_autologin_key_returns(), $result); + $result = \external_api::clean_returnvalue(external::get_autologin_key_returns(), $result); // We just requested one token, we must wait. $this->expectException('moodle_exception'); @@ -424,7 +415,7 @@ public function test_get_content() { $paramval = 16; $result = external::get_content('tool_mobile', 'test_view', array(array('name' => 'param1', 'value' => $paramval))); - $result = external_api::clean_returnvalue(external::get_content_returns(), $result); + $result = \external_api::clean_returnvalue(external::get_content_returns(), $result); $this->assertCount(1, $result['templates']); $this->assertCount(1, $result['otherdata']); $this->assertCount(2, $result['restrict']['users']); @@ -448,7 +439,7 @@ public function test_get_content_disabled() { $paramval = 16; $result = external::get_content('tool_mobile', 'test_view_disabled', array(array('name' => 'param1', 'value' => $paramval))); - $result = external_api::clean_returnvalue(external::get_content_returns(), $result); + $result = \external_api::clean_returnvalue(external::get_content_returns(), $result); $this->assertTrue($result['disabled']); } @@ -505,7 +496,7 @@ public function test_call_external_functions() { $token = external_generate_token_for_current_user($service); $_POST['wstoken'] = $token->token; - // Workaround for external_api::call_external_function requiring sesskey. + // Workaround for \external_api::call_external_function requiring sesskey. $_POST['sesskey'] = sesskey(); // Call some functions. @@ -531,21 +522,21 @@ public function test_call_external_functions() { $result = external::call_external_functions($requests); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(external::call_external_functions_returns(), $result); + $result = \external_api::clean_returnvalue(external::call_external_functions_returns(), $result); // Only 3 responses, the 4th request is not executed because the 3rd throws an exception. $this->assertCount(3, $result['responses']); $this->assertFalse($result['responses'][0]['error']); - $coursedata = external_api::clean_returnvalue( - core_course_external::get_courses_by_field_returns(), - core_course_external::get_courses_by_field('id', $course->id)); + $coursedata = \external_api::clean_returnvalue( + \core_course_external::get_courses_by_field_returns(), + \core_course_external::get_courses_by_field('id', $course->id)); $this->assertEquals(json_encode($coursedata), $result['responses'][0]['data']); $this->assertFalse($result['responses'][1]['error']); - $userdata = external_api::clean_returnvalue( - core_user_external::get_users_by_field_returns(), - core_user_external::get_users_by_field('id', [$user1->id])); + $userdata = \external_api::clean_returnvalue( + \core_user_external::get_users_by_field_returns(), + \core_user_external::get_users_by_field('id', [$user1->id])); $this->assertEquals(json_encode($userdata), $result['responses'][1]['data']); $this->assertTrue($result['responses'][2]['error']); @@ -641,11 +632,11 @@ public function test_get_tokens_for_qr_login() { $token = external_generate_token_for_current_user($service); // Fake the app. - core_useragent::instance(true, 'Mozilla/5.0 (Linux; Android 7.1.1; Moto G Play Build/NPIS26.48-43-2; wv) ' . + \core_useragent::instance(true, 'Mozilla/5.0 (Linux; Android 7.1.1; Moto G Play Build/NPIS26.48-43-2; wv) ' . 'AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.99 Mobile Safari/537.36 MoodleMobile'); $result = external::get_tokens_for_qr_login($qrloginkey, $USER->id); - $result = external_api::clean_returnvalue(external::get_tokens_for_qr_login_returns(), $result); + $result = \external_api::clean_returnvalue(external::get_tokens_for_qr_login_returns(), $result); $this->assertEmpty($result['warnings']); $this->assertEquals($token->token, $result['token']); @@ -665,7 +656,7 @@ public function test_get_tokens_for_qr_login_missing_enableqr() { $this->resetAfterTest(true); $this->setAdminUser(); - set_config('qrcodetype', tool_mobile\api::QR_CODE_DISABLED, 'tool_mobile'); + set_config('qrcodetype', api::QR_CODE_DISABLED, 'tool_mobile'); $this->expectExceptionMessage(get_string('qrcodedisabled', 'tool_mobile')); $result = external::get_tokens_for_qr_login('', $USER->id); @@ -682,7 +673,7 @@ public function test_get_tokens_for_qr_login_missing_ws() { $this->setUser($user); // Fake the app. - core_useragent::instance(true, 'Mozilla/5.0 (Linux; Android 7.1.1; Moto G Play Build/NPIS26.48-43-2; wv) ' . + \core_useragent::instance(true, 'Mozilla/5.0 (Linux; Android 7.1.1; Moto G Play Build/NPIS26.48-43-2; wv) ' . 'AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.99 Mobile Safari/537.36 MoodleMobile'); // Need to disable webservices to verify that's checked. @@ -702,7 +693,7 @@ public function test_get_tokens_for_qr_login_missing_https() { global $CFG, $USER; // Fake the app. - core_useragent::instance(true, 'Mozilla/5.0 (Linux; Android 7.1.1; Moto G Play Build/NPIS26.48-43-2; wv) ' . + \core_useragent::instance(true, 'Mozilla/5.0 (Linux; Android 7.1.1; Moto G Play Build/NPIS26.48-43-2; wv) ' . 'AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.99 Mobile Safari/537.36 MoodleMobile'); // Need to simulate a non HTTPS site here. @@ -726,7 +717,7 @@ public function test_get_tokens_for_qr_login_missing_admin() { $this->setAdminUser(); // Fake the app. - core_useragent::instance(true, 'Mozilla/5.0 (Linux; Android 7.1.1; Moto G Play Build/NPIS26.48-43-2; wv) ' . + \core_useragent::instance(true, 'Mozilla/5.0 (Linux; Android 7.1.1; Moto G Play Build/NPIS26.48-43-2; wv) ' . 'AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.99 Mobile Safari/537.36 MoodleMobile'); $this->expectException('moodle_exception'); @@ -758,7 +749,7 @@ public function test_validate_subscription_key_valid() { set_config('sitesubscriptionkey', json_encode($sitesubscriptionkey), 'tool_mobile'); $result = external::validate_subscription_key($sitesubscriptionkey['key']); - $result = external_api::clean_returnvalue(external::validate_subscription_key_returns(), $result); + $result = \external_api::clean_returnvalue(external::validate_subscription_key_returns(), $result); $this->assertEmpty($result['warnings']); $this->assertTrue($result['validated']); } @@ -773,13 +764,13 @@ public function test_validate_subscription_key_invalid_key_first() { set_config('sitesubscriptionkey', json_encode($sitesubscriptionkey), 'tool_mobile'); $result = external::validate_subscription_key('fakekey'); - $result = external_api::clean_returnvalue(external::validate_subscription_key_returns(), $result); + $result = \external_api::clean_returnvalue(external::validate_subscription_key_returns(), $result); $this->assertEmpty($result['warnings']); $this->assertFalse($result['validated']); // The valid one has been invalidated because the previous attempt. $result = external::validate_subscription_key($sitesubscriptionkey['key']); - $result = external_api::clean_returnvalue(external::validate_subscription_key_returns(), $result); + $result = \external_api::clean_returnvalue(external::validate_subscription_key_returns(), $result); $this->assertEmpty($result['warnings']); $this->assertFalse($result['validated']); } @@ -791,7 +782,7 @@ public function test_validate_subscription_key_invalid_key() { $this->resetAfterTest(true); $result = external::validate_subscription_key('fakekey'); - $result = external_api::clean_returnvalue(external::validate_subscription_key_returns(), $result); + $result = \external_api::clean_returnvalue(external::validate_subscription_key_returns(), $result); $this->assertEmpty($result['warnings']); $this->assertFalse($result['validated']); } @@ -806,7 +797,7 @@ public function test_validate_subscription_key_outdated() { set_config('sitesubscriptionkey', json_encode($sitesubscriptionkey), 'tool_mobile'); $result = external::validate_subscription_key($sitesubscriptionkey['key']); - $result = external_api::clean_returnvalue(external::validate_subscription_key_returns(), $result); + $result = \external_api::clean_returnvalue(external::validate_subscription_key_returns(), $result); $this->assertEmpty($result['warnings']); $this->assertFalse($result['validated']); } diff --git a/admin/tool/monitor/tests/generator_test.php b/admin/tool/monitor/tests/generator_test.php index c2672a8c67f39..d17694b1d40b3 100644 --- a/admin/tool/monitor/tests/generator_test.php +++ b/admin/tool/monitor/tests/generator_test.php @@ -14,16 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * PHPUnit data generator tests. - * - * @package tool_monitor - * @category test - * @copyright 2014 onwards Simey Lameze - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -defined('MOODLE_INTERNAL') || die(); +namespace tool_monitor; /** * PHPUnit data generator test case. @@ -34,7 +25,7 @@ * @copyright 2014 onwards Simey Lameze * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class tool_monitor_generator_testcase extends advanced_testcase { +class generator_test extends \advanced_testcase { /** * Set up method. @@ -55,7 +46,7 @@ public function test_create_rule() { $rulegenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor'); - $record = new stdClass(); + $record = new \stdClass(); $record->courseid = $course->id; $record->userid = $user->id; @@ -77,7 +68,7 @@ public function test_create_subscription() { $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor'); $rule = $monitorgenerator->create_rule(); - $record = new stdClass(); + $record = new \stdClass(); $record->courseid = $course->id; $record->userid = $user->id; $record->ruleid = $rule->id; diff --git a/admin/tool/policy/tests/externallib_test.php b/admin/tool/policy/tests/externallib_test.php index bec7faab688f2..05dd2fc07151d 100644 --- a/admin/tool/policy/tests/externallib_test.php +++ b/admin/tool/policy/tests/externallib_test.php @@ -14,13 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * External policy webservice API tests. - * - * @package tool_policy - * @copyright 2018 Sara Arjona - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace tool_policy; + +use externallib_advanced_testcase; +use tool_mobile\external as external_mobile; defined('MOODLE_INTERNAL') || die(); @@ -30,10 +27,6 @@ require_once($CFG->dirroot . '/webservice/tests/helpers.php'); require_once($CFG->dirroot . '/user/externallib.php'); -use tool_policy\api; -use tool_policy\external; -use tool_mobile\external as external_mobile; - /** * External policy webservice API tests. * @@ -41,7 +34,7 @@ * @copyright 2018 Sara Arjona * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class tool_policy_external_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * Setup function- we will create some policy docs. @@ -73,8 +66,8 @@ public function setUp(): void { $this->parent = $this->getDataGenerator()->create_user(); $this->adult = $this->getDataGenerator()->create_user(); - $syscontext = context_system::instance(); - $childcontext = context_user::instance($this->child->id); + $syscontext = \context_system::instance(); + $childcontext = \context_user::instance($this->child->id); $roleminorid = create_role('Digital minor', 'digiminor', 'Not old enough to accept site policies themselves'); $roleparentid = create_role('Parent', 'parent', 'Can accept policies on behalf of their child'); @@ -94,21 +87,21 @@ public function test_get_policy_version() { // View current policy version. $result = external::get_policy_version($this->policy2->get('id')); - $result = external_api::clean_returnvalue(external::get_policy_version_returns(), $result); + $result = \external_api::clean_returnvalue(external::get_policy_version_returns(), $result); $this->assertCount(1, $result['result']); $this->assertEquals($this->policy1->get('name'), $result['result']['policy']['name']); $this->assertEquals($this->policy1->get('content'), $result['result']['policy']['content']); // View draft policy version. $result = external::get_policy_version($this->policy3->get('id')); - $result = external_api::clean_returnvalue(external::get_policy_version_returns(), $result); + $result = \external_api::clean_returnvalue(external::get_policy_version_returns(), $result); $this->assertCount(0, $result['result']); $this->assertCount(1, $result['warnings']); $this->assertEquals(array_pop($result['warnings'])['warningcode'], 'errorusercantviewpolicyversion'); // Add test for non existing versionid. $result = external::get_policy_version(999); - $result = external_api::clean_returnvalue(external::get_policy_version_returns(), $result); + $result = \external_api::clean_returnvalue(external::get_policy_version_returns(), $result); $this->assertCount(0, $result['result']); $this->assertCount(1, $result['warnings']); $this->assertEquals(array_pop($result['warnings'])['warningcode'], 'errorpolicyversionnotfound'); @@ -116,7 +109,7 @@ public function test_get_policy_version() { // View previous non-accepted version in behalf of a child. $this->setUser($this->parent); $result = external::get_policy_version($this->policy1->get('id'), $this->child->id); - $result = external_api::clean_returnvalue(external::get_policy_version_returns(), $result); + $result = \external_api::clean_returnvalue(external::get_policy_version_returns(), $result); $this->assertCount(0, $result['result']); $this->assertCount(1, $result['warnings']); $this->assertEquals(array_pop($result['warnings'])['warningcode'], 'errorusercantviewpolicyversion'); @@ -124,7 +117,7 @@ public function test_get_policy_version() { // Let the parent accept the policy on behalf of her child and view it again. api::accept_policies($this->policy1->get('id'), $this->child->id); $result = external::get_policy_version($this->policy1->get('id'), $this->child->id); - $result = external_api::clean_returnvalue(external::get_policy_version_returns(), $result); + $result = \external_api::clean_returnvalue(external::get_policy_version_returns(), $result); $this->assertCount(1, $result['result']); $this->assertEquals($this->policy1->get('name'), $result['result']['policy']['name']); $this->assertEquals($this->policy1->get('content'), $result['result']['policy']['content']); @@ -132,7 +125,7 @@ public function test_get_policy_version() { // Only parent is able to view the child policy version accepted by her child. $this->setUser($this->adult); $result = external::get_policy_version($this->policy1->get('id'), $this->child->id); - $result = external_api::clean_returnvalue(external::get_policy_version_returns(), $result); + $result = \external_api::clean_returnvalue(external::get_policy_version_returns(), $result); $this->assertCount(0, $result['result']); $this->assertCount(1, $result['warnings']); $this->assertEquals(array_pop($result['warnings'])['warningcode'], 'errorusercantviewpolicyversion'); @@ -148,9 +141,9 @@ public function test_get_config_with_site_policy_handler() { // Set the handler for the site policy, make sure it substitutes link to the sitepolicy. $CFG->sitepolicyhandler = 'tool_policy'; - $sitepolicymanager = new core_privacy\local\sitepolicy\manager(); + $sitepolicymanager = new \core_privacy\local\sitepolicy\manager(); $result = external_mobile::get_config(); - $result = external_api::clean_returnvalue(external_mobile::get_config_returns(), $result); + $result = \external_api::clean_returnvalue(external_mobile::get_config_returns(), $result); $toolsitepolicy = $sitepolicymanager->get_embed_url(); foreach (array_values($result['settings']) as $r) { if ($r['name'] == 'sitepolicy') { @@ -173,25 +166,25 @@ public function test_agree_site_policy_with_handler() { // Set mock site policy handler. See function tool_phpunit_site_policy_handler() below. $CFG->sitepolicyhandler = 'tool_policy'; $this->assertEquals(0, $USER->policyagreed); - $sitepolicymanager = new core_privacy\local\sitepolicy\manager(); + $sitepolicymanager = new \core_privacy\local\sitepolicy\manager(); // Make sure user can not login. $toolconsentpage = $sitepolicymanager->get_redirect_url(); - $this->expectException('moodle_exception'); + $this->expectException(\moodle_exception::class); $this->expectExceptionMessage(get_string('sitepolicynotagreed', 'error', $toolconsentpage->out())); - core_user_external::validate_context(context_system::instance()); + \core_user_external::validate_context(\context_system::instance()); // Call WS to agree to the site policy. It will call tool_policy handler. - $result = core_user_external::agree_site_policy(); - $result = external_api::clean_returnvalue(core_user_external::agree_site_policy_returns(), $result); + $result = \core_user_external::agree_site_policy(); + $result = \external_api::clean_returnvalue(\core_user_external::agree_site_policy_returns(), $result); $this->assertTrue($result['status']); $this->assertCount(0, $result['warnings']); $this->assertEquals(1, $USER->policyagreed); $this->assertEquals(1, $DB->get_field('user', 'policyagreed', array('id' => $USER->id))); // Try again, we should get a warning. - $result = core_user_external::agree_site_policy(); - $result = external_api::clean_returnvalue(core_user_external::agree_site_policy_returns(), $result); + $result = \core_user_external::agree_site_policy(); + $result = \external_api::clean_returnvalue(\core_user_external::agree_site_policy_returns(), $result); $this->assertFalse($result['status']); $this->assertCount(1, $result['warnings']); $this->assertEquals('alreadyagreed', $result['warnings'][0]['warningcode']); @@ -205,8 +198,8 @@ public function test_checkcanaccept_with_handler() { $this->resetAfterTest(true); $CFG->sitepolicyhandler = 'tool_policy'; - $syscontext = context_system::instance(); - $sitepolicymanager = new core_privacy\local\sitepolicy\manager(); + $syscontext = \context_system::instance(); + $sitepolicymanager = new \core_privacy\local\sitepolicy\manager(); $adult = $this->getDataGenerator()->create_user(); @@ -218,15 +211,15 @@ public function test_checkcanaccept_with_handler() { // Default user can accept policies. $this->setUser($adult); $result = external_mobile::get_config(); - $result = external_api::clean_returnvalue(external_mobile::get_config_returns(), $result); + $result = \external_api::clean_returnvalue(external_mobile::get_config_returns(), $result); $toolsitepolicy = $sitepolicymanager->accept(); $this->assertTrue($toolsitepolicy); // Child user can not accept policies. $this->setUser($child); $result = external_mobile::get_config(); - $result = external_api::clean_returnvalue(external_mobile::get_config_returns(), $result); - $this->expectException('required_capability_exception'); + $result = \external_api::clean_returnvalue(external_mobile::get_config_returns(), $result); + $this->expectException(\required_capability_exception::class); $sitepolicymanager->accept(); } } diff --git a/admin/tool/templatelibrary/tests/externallib_test.php b/admin/tool/templatelibrary/tests/externallib_test.php index aba224e5b9034..87144cddef32f 100644 --- a/admin/tool/templatelibrary/tests/externallib_test.php +++ b/admin/tool/templatelibrary/tests/externallib_test.php @@ -14,14 +14,16 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +namespace tool_templatelibrary; + +use externallib_advanced_testcase; + defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->dirroot . '/webservice/tests/helpers.php'); -use tool_templatelibrary\external; - /** * External learning plans webservice API tests. * @@ -29,7 +31,7 @@ * @copyright 2015 Damyon Wiese * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class tool_templatelibrary_external_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * Test list all. diff --git a/backup/tests/externallib_test.php b/backup/tests/externallib_test.php index df2b1539c4e77..d3e9d342650fa 100644 --- a/backup/tests/externallib_test.php +++ b/backup/tests/externallib_test.php @@ -14,14 +14,11 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Backup webservice tests. - * - * @package core_backup - * @copyright 2020 onward The Moodle Users Association - * @author Matt Porritt - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace core_backup; + +use backup; +use core_backup_external; +use externallib_advanced_testcase; defined('MOODLE_INTERNAL') || die(); @@ -39,7 +36,7 @@ * @author Matt Porritt * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class backup_external_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * Set up tasks for all tests. @@ -93,7 +90,7 @@ public function test_get_copy_progress() { $returnvalue = core_backup_external::get_copy_progress($params); // We need to execute the return values cleaning process to simulate the web service server. - $returnvalue = external_api::clean_returnvalue(core_backup_external::get_copy_progress_returns(), $returnvalue); + $returnvalue = \external_api::clean_returnvalue(core_backup_external::get_copy_progress_returns(), $returnvalue); $this->assertEquals(\backup::STATUS_AWAITING, $returnvalue[0]['status']); $this->assertEquals(0, $returnvalue[0]['progress']); @@ -114,7 +111,7 @@ public function test_get_copy_progress() { $params = array('copies' => $copydetails); $returnvalue = core_backup_external::get_copy_progress($params); - $returnvalue = external_api::clean_returnvalue(core_backup_external::get_copy_progress_returns(), $returnvalue); + $returnvalue = \external_api::clean_returnvalue(core_backup_external::get_copy_progress_returns(), $returnvalue); $this->assertEquals(\backup::STATUS_FINISHED_OK, $returnvalue[0]['status']); $this->assertEquals(1, $returnvalue[0]['progress']); @@ -163,7 +160,7 @@ public function test_submit_copy_form() { $returnvalue = core_backup_external::submit_copy_form($jsonformdata); - $returnjson = external_api::clean_returnvalue(core_backup_external::submit_copy_form_returns(), $returnvalue); + $returnjson = \external_api::clean_returnvalue(core_backup_external::submit_copy_form_returns(), $returnvalue); $copyids = json_decode($returnjson, true); $backuprec = $DB->get_record('backup_controllers', array('backupid' => $copyids['backupid'])); diff --git a/blocks/online_users/tests/generator_test.php b/blocks/online_users/tests/generator_test.php index 1ffcaba723d85..ad5a7dfa079e3 100644 --- a/blocks/online_users/tests/generator_test.php +++ b/blocks/online_users/tests/generator_test.php @@ -14,17 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * PHPUnit data generator tests - * - * @package block_online_users - * @category phpunit - * @copyright 2012 Petr Skoda {@link http://skodak.org} - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -defined('MOODLE_INTERNAL') || die(); - +namespace block_online_users; /** * PHPUnit data generator testcase @@ -34,7 +24,7 @@ * @copyright 2012 Petr Skoda {@link http://skodak.org} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class block_online_users_generator_testcase extends advanced_testcase { +class generator_test extends \advanced_testcase { public function test_generator() { global $DB; diff --git a/blocks/recentlyaccesseditems/tests/externallib_test.php b/blocks/recentlyaccesseditems/tests/externallib_test.php index b6d6b5e94396a..0d98f1e90ec3f 100644 --- a/blocks/recentlyaccesseditems/tests/externallib_test.php +++ b/blocks/recentlyaccesseditems/tests/externallib_test.php @@ -14,15 +14,9 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * External block functions unit tests - * - * @package block_recentlyaccesseditems - * @category external - * @copyright 2018 Victor Deniz - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @since Moodle 3.6 - */ +namespace block_recentlyaccesseditems; + +use externallib_advanced_testcase; defined('MOODLE_INTERNAL') || die(); @@ -39,7 +33,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 3.6 */ -class block_recentlyaccesseditems_externallib_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * Test the get_recent_items function. @@ -76,7 +70,7 @@ public function test_get_recent_items() { // Student access all forums. foreach ($forum as $module) { - $event = \mod_forum\event\course_module_viewed::create(array('context' => context_module::instance($module->cmid), + $event = \mod_forum\event\course_module_viewed::create(array('context' => \context_module::instance($module->cmid), 'objectid' => $module->id)); $event->trigger(); $this->waitForSecond(); @@ -88,7 +82,7 @@ public function test_get_recent_items() { // Student access all assignments. foreach ($chat as $module) { - $event = \mod_chat\event\course_module_viewed::create(array('context' => context_module::instance($module->cmid), + $event = \mod_chat\event\course_module_viewed::create(array('context' => \context_module::instance($module->cmid), 'objectid' => $module->id)); $event->trigger(); $this->waitForSecond(); @@ -114,4 +108,4 @@ public function test_get_recent_items() { $result = \block_recentlyaccesseditems\external::get_recent_items(); $this->assertCount((count($forum) + count($chat)) - 3, $result); } -} \ No newline at end of file +} diff --git a/blocks/tests/externallib_test.php b/blocks/tests/externallib_test.php index f4bc6aba9ba2e..8291879622d18 100644 --- a/blocks/tests/externallib_test.php +++ b/blocks/tests/externallib_test.php @@ -14,15 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * External block functions unit tests - * - * @package core_block - * @category external - * @copyright 2017 Juan Leyva - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @since Moodle 3.3 - */ +namespace core_block; + +use core_block_external; +use externallib_advanced_testcase; defined('MOODLE_INTERNAL') || die(); @@ -40,7 +35,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 3.0 */ -class core_block_externallib_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * Test get_course_blocks @@ -55,8 +50,8 @@ public function test_get_course_blocks() { $studentrole = $DB->get_record('role', array('shortname' => 'student')); $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id); - $page = new moodle_page(); - $page->set_context(context_course::instance($course->id)); + $page = new \moodle_page(); + $page->set_context(\context_course::instance($course->id)); $page->set_pagelayout('course'); $course->format = course_get_format($course)->get_format(); $page->set_pagetype('course-view-' . $course->format); @@ -68,7 +63,7 @@ public function test_get_course_blocks() { // Check for the new block. $result = core_block_external::get_course_blocks($course->id); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(core_block_external::get_course_blocks_returns(), $result); + $result = \external_api::clean_returnvalue(core_block_external::get_course_blocks_returns(), $result); // Expect the new block. $this->assertCount(1, $result['blocks']); @@ -85,8 +80,8 @@ public function test_get_course_blocks_site_home() { $user = $this->getDataGenerator()->create_user(); - $page = new moodle_page(); - $page->set_context(context_course::instance(SITEID)); + $page = new \moodle_page(); + $page->set_context(\context_course::instance(SITEID)); $page->set_pagelayout('frontpage'); $page->set_pagetype('site-index'); $page->blocks->load_blocks(); @@ -97,7 +92,7 @@ public function test_get_course_blocks_site_home() { // Check for the new block. $result = core_block_external::get_course_blocks(SITEID); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(core_block_external::get_course_blocks_returns(), $result); + $result = \external_api::clean_returnvalue(core_block_external::get_course_blocks_returns(), $result); // Expect the new block. $this->assertCount(1, $result['blocks']); @@ -124,7 +119,7 @@ public function test_get_course_blocks_overrides() { // Try default blocks. $result = core_block_external::get_course_blocks($course->id); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(core_block_external::get_course_blocks_returns(), $result); + $result = \external_api::clean_returnvalue(core_block_external::get_course_blocks_returns(), $result); // Expect 4 default blocks. $this->assertCount(4, $result['blocks']); @@ -151,13 +146,13 @@ public function test_get_course_blocks_contents() { $course = $this->getDataGenerator()->create_course(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id); - $coursecontext = context_course::instance($course->id); + $coursecontext = \context_course::instance($course->id); // Create a HTML block. $title = 'Some course info'; $body = 'Some course info

Some contents

'; $bodyformat = FORMAT_MOODLE; - $page = new moodle_page(); + $page = new \moodle_page(); $page->set_context($coursecontext); $page->set_pagelayout('course'); $course->format = course_get_format($course)->get_format(); @@ -168,7 +163,7 @@ public function test_get_course_blocks_contents() { $this->setUser($user); // Re-create the page. - $page = new moodle_page(); + $page = new \moodle_page(); $page->set_context($coursecontext); $page->set_pagelayout('course'); $course->format = course_get_format($course)->get_format(); @@ -192,7 +187,7 @@ public function test_get_course_blocks_contents() { $block->instance_config_save((object) $configdata); $filename = 'img.png'; $filerecord = array( - 'contextid' => context_block::instance($block->instance->id)->id, + 'contextid' => \context_block::instance($block->instance->id)->id, 'component' => 'block_html', 'filearea' => 'content', 'itemid' => 0, @@ -207,7 +202,7 @@ public function test_get_course_blocks_contents() { // Check for the new block. $result = core_block_external::get_course_blocks($course->id, true); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(core_block_external::get_course_blocks_returns(), $result); + $result = \external_api::clean_returnvalue(core_block_external::get_course_blocks_returns(), $result); // Expect the new block. $this->assertCount(1, $result['blocks']); @@ -255,13 +250,13 @@ public function test_get_course_blocks_contents_with_mathjax() { $course = $this->getDataGenerator()->create_course(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id); - $coursecontext = context_course::instance($course->id); + $coursecontext = \context_course::instance($course->id); // Create a HTML block. $title = 'My block $$(a+b)=2$$'; $body = 'My block contents $$(a+b)=2$$'; $bodyformat = FORMAT_MOODLE; - $page = new moodle_page(); + $page = new \moodle_page(); $page->set_context($coursecontext); $page->set_pagelayout('course'); $course->format = course_get_format($course)->get_format(); @@ -272,7 +267,7 @@ public function test_get_course_blocks_contents_with_mathjax() { $this->setUser($user); // Re-create the page. - $page = new moodle_page(); + $page = new \moodle_page(); $page->set_context($coursecontext); $page->set_pagelayout('course'); $course->format = course_get_format($course)->get_format(); @@ -297,10 +292,10 @@ public function test_get_course_blocks_contents_with_mathjax() { // Check for the new block. $result = core_block_external::get_course_blocks($course->id, true); - $result = external_api::clean_returnvalue(core_block_external::get_course_blocks_returns(), $result); + $result = \external_api::clean_returnvalue(core_block_external::get_course_blocks_returns(), $result); // Format the original data. - $sitecontext = context_system::instance(); + $sitecontext = \context_system::instance(); $title = external_format_string($title, $coursecontext->id); list($body, $bodyformat) = external_format_text($body, $bodyformat, $coursecontext->id, 'block_html', 'content'); @@ -341,7 +336,7 @@ public function test_get_dashboard_blocks_default_dashboard() { // Check for the default blocks. $result = core_block_external::get_dashboard_blocks($user->id); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(core_block_external::get_dashboard_blocks_returns(), $result); + $result = \external_api::clean_returnvalue(core_block_external::get_dashboard_blocks_returns(), $result); // Expect all default blocks defined in blocks_add_default_system_blocks(). $this->assertCount(count($alldefaultblocksordered), $result['blocks']); $returnedblocks = array(); @@ -382,10 +377,10 @@ public function test_get_dashboard_blocks_default_dashboard_including_sticky_blo ); // Now, add a sticky block. - $page = new moodle_page(); - $page->set_context(context_system::instance()); + $page = new \moodle_page(); + $page->set_context(\context_system::instance()); $page->set_pagetype('my-index'); - $page->set_url(new moodle_url('/')); + $page->set_url(new \moodle_url('/')); $page->blocks->add_region('side-pre'); $page->blocks->load_blocks(); $page->blocks->add_block('myprofile', 'side-pre', 0, true, '*'); @@ -395,7 +390,7 @@ public function test_get_dashboard_blocks_default_dashboard_including_sticky_blo // Check for the default blocks plus the sticky. $result = core_block_external::get_dashboard_blocks($user->id); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(core_block_external::get_dashboard_blocks_returns(), $result); + $result = \external_api::clean_returnvalue(core_block_external::get_dashboard_blocks_returns(), $result); // Expect all default blocks defined in blocks_add_default_system_blocks() plus sticky one. $this->assertCount(count($alldefaultblocks) + 1, $result['blocks']); $found = false; @@ -431,8 +426,8 @@ public function test_get_dashboard_blocks_custom_user_dashboard() { ); // Add a custom block. - $page = new moodle_page(); - $page->set_context(context_user::instance($user->id)); + $page = new \moodle_page(); + $page->set_context(\context_user::instance($user->id)); $page->set_pagelayout('mydashboard'); $page->set_pagetype('my-index'); $page->blocks->add_region('content'); @@ -446,7 +441,7 @@ public function test_get_dashboard_blocks_custom_user_dashboard() { // Check for the new block as admin for a user. $result = core_block_external::get_dashboard_blocks($user->id); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(core_block_external::get_dashboard_blocks_returns(), $result); + $result = \external_api::clean_returnvalue(core_block_external::get_dashboard_blocks_returns(), $result); // Expect all default blocks defined in blocks_add_default_system_blocks() plus the one we added. $this->assertCount(count($alldefaultblocks) + 1, $result['blocks']); $found = false; @@ -504,7 +499,7 @@ public function test_get_dashboard_blocks_my_courses() { // Check for the default blocks. $result = core_block_external::get_dashboard_blocks($user->id, false, MY_PAGE_COURSES); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(core_block_external::get_dashboard_blocks_returns(), $result); + $result = \external_api::clean_returnvalue(core_block_external::get_dashboard_blocks_returns(), $result); // Expect all default blocks defined in blocks_add_default_system_blocks(). $this->assertCount(count($alldefaultblocksordered), $result['blocks']); $returnedblocks = []; diff --git a/calendar/tests/event/events_test.php b/calendar/tests/event/events_test.php index a375d32c88b3f..edb609a9c155e 100644 --- a/calendar/tests/event/events_test.php +++ b/calendar/tests/event/events_test.php @@ -74,7 +74,7 @@ public function test_calendar_event_created() { $record = new \stdClass(); $record->courseid = 0; $time = time(); - $calevent = core_calendar_externallib_testcase::create_calendar_event('event', $this->user->id, 'user', 0, $time, + $calevent = \core_calendar\externallib_test::create_calendar_event('event', $this->user->id, 'user', 0, $time, $record); // User event. // Capture the event. @@ -96,7 +96,7 @@ public function test_calendar_event_created() { // Now we create a repeated course event. $record = new \stdClass(); $record->courseid = $this->course->id; - $calevent = core_calendar_externallib_testcase::create_calendar_event('course', $this->user->id, 'course', 10, $time, + $calevent = \core_calendar\externallib_test::create_calendar_event('course', $this->user->id, 'course', 10, $time, $record); $events = $sink->get_events(); $sink->close(); @@ -177,7 +177,7 @@ public function test_calendar_event_updated() { $record = new \stdClass(); $record->courseid = 0; $time = time(); - $calevent = core_calendar_externallib_testcase::create_calendar_event('event', $this->user->id, 'user', 0, $time, + $calevent = \core_calendar\externallib_test::create_calendar_event('event', $this->user->id, 'user', 0, $time, $record); // User event. // Catch the events. @@ -203,7 +203,7 @@ public function test_calendar_event_updated() { // Now we create a repeated course event and update it. $record = new \stdClass(); $record->courseid = $this->course->id; - $calevent = core_calendar_externallib_testcase::create_calendar_event('course', $this->user->id, 'course', 10, time(), + $calevent = \core_calendar\externallib_test::create_calendar_event('course', $this->user->id, 'course', 10, time(), $record); $sink->clear(); @@ -234,7 +234,7 @@ public function test_calendar_event_updated_toggle_visibility() { // Create a calendar event. $time = time(); - $calevent = core_calendar_externallib_testcase::create_calendar_event('Some wickedly awesome event yo!', + $calevent = \core_calendar\externallib_test::create_calendar_event('Some wickedly awesome event yo!', $this->user->id, 'user', 0, $time); // Updated the visibility of the calendar event. @@ -327,7 +327,7 @@ public function test_calendar_event_deleted() { $record->courseid = 0; $record->repeatid = 0; $time = time(); - $calevent = core_calendar_externallib_testcase::create_calendar_event('event', $this->user->id, 'user', 0, $time, + $calevent = \core_calendar\externallib_test::create_calendar_event('event', $this->user->id, 'user', 0, $time, $record); // User event. $dbrecord = $DB->get_record('event', array('id' => $calevent->id), '*', MUST_EXIST); @@ -350,7 +350,7 @@ public function test_calendar_event_deleted() { // Now we create a repeated course event and delete it. $record = new \stdClass(); $record->courseid = $this->course->id; - $calevent = core_calendar_externallib_testcase::create_calendar_event('course', $this->user->id, 'course', 10, time(), + $calevent = \core_calendar\externallib_test::create_calendar_event('course', $this->user->id, 'course', 10, time(), $record); $sink->clear(); diff --git a/calendar/tests/externallib_test.php b/calendar/tests/externallib_test.php index c1fe74612b674..febd9a484cc14 100644 --- a/calendar/tests/externallib_test.php +++ b/calendar/tests/externallib_test.php @@ -14,14 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * External calendar functions unit tests - * - * @package core_calendar - * @category external - * @copyright 2012 Ankit Agarwal - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace core_calendar; + +use core_calendar_external; +use externallib_advanced_testcase; defined('MOODLE_INTERNAL') || die(); @@ -38,7 +34,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 2.5 */ -class core_calendar_externallib_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * Tests set up @@ -70,7 +66,7 @@ public static function create_calendar_event($name, $userid = 0, $type = 'user', $prop = (object)$prop; } } else { - $prop = new stdClass(); + $prop = new \stdClass(); } $prop->name = $name; if (empty($prop->eventtype)) { @@ -133,7 +129,7 @@ public static function create_calendar_event($name, $userid = 0, $type = 'user', $prop->priority = $priority; } - $event = new calendar_event($prop); + $event = new \calendar_event($prop); return $event->create($prop); } @@ -174,7 +170,7 @@ public function test_delete_calendar_events() { // Create a few stuff to test with. $user = $this->getDataGenerator()->create_user(); $course = $this->getDataGenerator()->create_course(); - $record = new stdClass(); + $record = new \stdClass(); $record->courseid = $course->id; $group = $this->getDataGenerator()->create_group($record); @@ -182,11 +178,11 @@ public function test_delete_calendar_events() { // Let's create a few events. $siteevent = $this->create_calendar_event('site', $USER->id, 'site'); - $record = new stdClass(); + $record = new \stdClass(); $record->courseid = $course->id; $courseevent = $this->create_calendar_event('course', $USER->id, 'course', 2, time(), $record); $userevent = $this->create_calendar_event('user', $USER->id); - $record = new stdClass(); + $record = new \stdClass(); $record->courseid = $course->id; $record->groupid = $group->id; $groupevent = $this->create_calendar_event('group', $USER->id, 'group', 0, time(), $record); @@ -206,19 +202,19 @@ public function test_delete_calendar_events() { // Let's create a few events. $siteevent = $this->create_calendar_event('site', $USER->id, 'site'); - $record = new stdClass(); + $record = new \stdClass(); $record->courseid = $course->id; $courseevent = $this->create_calendar_event('course', $USER->id, 'course', 3, time(), $record); $userevent = $this->create_calendar_event('user', $user->id); - $record = new stdClass(); + $record = new \stdClass(); $record->courseid = $course->id; $record->groupid = $group->id; $groupevent = $this->create_calendar_event('group', $USER->id, 'group', 0, time(), $record); $this->setuser($user); - $sitecontext = context_system::instance(); - $coursecontext = context_course::instance($course->id); - $usercontext = context_user::instance($user->id); + $sitecontext = \context_system::instance(); + $coursecontext = \context_course::instance($course->id); + $usercontext = \context_user::instance($user->id); $role = $DB->get_record('role', array('shortname' => 'student')); $this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id); @@ -270,11 +266,11 @@ public function test_delete_calendar_events() { // Let us try deleting without caps. $siteevent = $this->create_calendar_event('site', $USER->id, 'site'); - $record = new stdClass(); + $record = new \stdClass(); $record->courseid = $course->id; $courseevent = $this->create_calendar_event('course', $USER->id, 'course', 3, time(), $record); $userevent = $this->create_calendar_event('user', $USER->id); - $record = new stdClass(); + $record = new \stdClass(); $record->courseid = $course->id; $record->groupid = $group->id; $groupevent = $this->create_calendar_event('group', $USER->id, 'group', 0, time(), $record); @@ -287,7 +283,7 @@ public function test_delete_calendar_events() { array('eventid' => $userevent->id, 'repeat' => 0), array('eventid' => $groupevent->id, 'repeat' => 0) ); - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); core_calendar_external::delete_calendar_events($events); } @@ -315,7 +311,7 @@ public function test_get_calendar_events() { $role = $DB->get_record('role', array('shortname' => 'student')); $this->getDataGenerator()->enrol_user($user2->id, $course3->id, $role->id); - $record = new stdClass(); + $record = new \stdClass(); $record->courseid = $course->id; $group = $this->getDataGenerator()->create_group($record); @@ -326,7 +322,7 @@ public function test_get_calendar_events() { // This event will have description with an inline fake image. $draftidfile = file_get_unused_draft_itemid(); - $usercontext = context_course::instance($course->id); + $usercontext = \context_course::instance($course->id); $filerecord = array( 'contextid' => $usercontext->id, 'component' => 'user', @@ -338,7 +334,7 @@ public function test_get_calendar_events() { $fs = get_file_storage(); $fs->create_file_from_string($filerecord, 'img contents'); - $record = new stdClass(); + $record = new \stdClass(); $record->courseid = $course->id; $record->groupid = 0; $record->description = array( @@ -348,12 +344,12 @@ public function test_get_calendar_events() { ); $courseevent = $this->create_calendar_event('course', $USER->id, 'course', 2, time(), $record); - $record = new stdClass(); + $record = new \stdClass(); $record->courseid = 0; $record->groupid = 0; $userevent = $this->create_calendar_event('user', $USER->id, 'user', 0, time(), $record); - $record = new stdClass(); + $record = new \stdClass(); $record->courseid = $course->id; $record->groupid = $group->id; $groupevent = $this->create_calendar_event('group', $USER->id, 'group', 0, time(), $record); @@ -363,19 +359,19 @@ public function test_get_calendar_events() { $options = array ('siteevents' => true, 'userevents' => true); $events = core_calendar_external::get_calendar_events($paramevents, $options); - $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); + $events = \external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); // Check to see if we got all events. $this->assertEquals(5, count($events['events'])); $this->assertEquals(0, count($events['warnings'])); $options = array ('siteevents' => true, 'userevents' => true, 'timeend' => time() + 7*WEEKSECS); $events = core_calendar_external::get_calendar_events($paramevents, $options); - $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); + $events = \external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(5, count($events['events'])); $this->assertEquals(0, count($events['warnings'])); // Expect the same URL in the description of two different events (because they are repeated). - $coursecontext = context_course::instance($course->id); + $coursecontext = \context_course::instance($course->id); $expectedurl = "webservice/pluginfile.php/$coursecontext->id/calendar/event_description/$courseevent->id/fakeimage.png"; $withdescription = 0; foreach ($events['events'] as $event) { @@ -389,78 +385,78 @@ public function test_get_calendar_events() { // Let's play around with caps. // Create user event for the user $user. - $record = new stdClass(); + $record = new \stdClass(); $record->courseid = 0; $record->groupid = 0; $this->create_calendar_event('user', $user->id, 'user', 0, time(), $record); $this->setUser($user); $events = core_calendar_external::get_calendar_events($paramevents, $options); - $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); + $events = \external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(2, count($events['events'])); // site, user. $this->assertEquals(2, count($events['warnings'])); // course, group. $role = $DB->get_record('role', array('shortname' => 'student')); $this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id); $events = core_calendar_external::get_calendar_events($paramevents, $options); - $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); + $events = \external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(4, count($events['events'])); // site, user, both course events. $this->assertEquals(1, count($events['warnings'])); // group. $options = array ('siteevents' => true, 'userevents' => true, 'timeend' => time() + HOURSECS); $events = core_calendar_external::get_calendar_events($paramevents, $options); - $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); + $events = \external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(3, count($events['events'])); // site, user, one course event. $this->assertEquals(1, count($events['warnings'])); // group. groups_add_member($group, $user); $events = core_calendar_external::get_calendar_events($paramevents, $options); - $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); + $events = \external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(4, count($events['events'])); // site, user, group, one course event. $this->assertEquals(0, count($events['warnings'])); $paramevents = array ('courseids' => array($course->id), 'groupids' => array($group->id)); $events = core_calendar_external::get_calendar_events($paramevents, $options); - $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); + $events = \external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(4, count($events['events'])); // site, user, group, one course event. $this->assertEquals(0, count($events['warnings'])); $paramevents = array ('groupids' => array($group->id, 23)); $events = core_calendar_external::get_calendar_events($paramevents, $options); - $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); + $events = \external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(3, count($events['events'])); // site, user, group. $this->assertEquals(1, count($events['warnings'])); $paramevents = array ('courseids' => array(23)); $events = core_calendar_external::get_calendar_events($paramevents, $options); - $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); + $events = \external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(2, count($events['events'])); // site, user. $this->assertEquals(1, count($events['warnings'])); $paramevents = array (); $options = array ('siteevents' => false, 'userevents' => false, 'timeend' => time() + 7*WEEKSECS); $events = core_calendar_external::get_calendar_events($paramevents, $options); - $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); + $events = \external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(0, count($events['events'])); // nothing returned. $this->assertEquals(0, count($events['warnings'])); $paramevents = array ('eventids' => array($siteevent->id, $groupevent->id)); $options = array ('siteevents' => false, 'userevents' => false, 'timeend' => time() + 7*WEEKSECS); $events = core_calendar_external::get_calendar_events($paramevents, $options); - $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); + $events = \external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(2, count($events['events'])); // site, group. $this->assertEquals(0, count($events['warnings'])); $paramevents = array ('eventids' => array($siteevent->id)); $events = core_calendar_external::get_calendar_events($paramevents, $options); - $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); + $events = \external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(1, count($events['events'])); // site. $this->assertEquals(0, count($events['warnings'])); // Try getting a course event by its id. $paramevents = array ('eventids' => array($courseevent->id)); $events = core_calendar_external::get_calendar_events($paramevents, $options); - $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); + $events = \external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(1, count($events['events'])); $this->assertEquals(0, count($events['warnings'])); @@ -473,7 +469,7 @@ public function test_get_calendar_events() { $paramevents = array ('courseids' => array($course->id)); $options = array ('siteevents' => true, 'userevents' => true, 'timeend' => time() + WEEKSECS); $events = core_calendar_external::get_calendar_events($paramevents, $options); - $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); + $events = \external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertCount(5, $events['events']); @@ -481,22 +477,22 @@ public function test_get_calendar_events() { set_coursemodule_visible($assign->cmid, 0); // Empty all the caches that may be affected by this change. accesslib_clear_all_caches_for_unit_testing(); - course_modinfo::clear_instance_cache(); + \course_modinfo::clear_instance_cache(); $events = core_calendar_external::get_calendar_events($paramevents, $options); - $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); + $events = \external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); // Expect one less. $this->assertCount(4, $events['events']); // Create some category events. $this->setAdminUser(); - $record = new stdClass(); + $record = new \stdClass(); $record->courseid = 0; $record->categoryid = $category->id; $record->timestart = time() - DAYSECS; $catevent1 = $this->create_calendar_event('category a', $USER->id, 'category', 0, time(), $record); - $record = new stdClass(); + $record = new \stdClass(); $record->courseid = 0; $record->categoryid = $category2->id; $record->timestart = time() + DAYSECS; @@ -507,7 +503,7 @@ public function test_get_calendar_events() { $paramevents = array('categoryids' => array($category2b->id)); $options = array('timeend' => time() + 7 * WEEKSECS, 'userevents' => false, 'siteevents' => false); $events = core_calendar_external::get_calendar_events($paramevents, $options); - $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); + $events = \external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); // Should be just one, since there's just one category event of the course I am enrolled (course3 - cat2b). $this->assertEquals(1, count($events['events'])); @@ -519,7 +515,7 @@ public function test_get_calendar_events() { $paramevents = array('courseids' => array($course3->id)); $options = array('timeend' => time() + 7 * WEEKSECS, 'userevents' => false, 'siteevents' => false); $events = core_calendar_external::get_calendar_events($paramevents, $options); - $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); + $events = \external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(1, count($events['events'])); $this->assertEquals($catevent2->id, $events['events'][0]['id']); $this->assertEquals(0, count($events['warnings'])); @@ -529,7 +525,7 @@ public function test_get_calendar_events() { $paramevents = array('categoryids' => array($category2->id, $category->id)); $options = array('timeend' => time() + 7 * WEEKSECS, 'userevents' => false, 'siteevents' => false); $events = core_calendar_external::get_calendar_events($paramevents, $options); - $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); + $events = \external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(1, count($events['events'])); $this->assertEquals($catevent2->id, $events['events'][0]['id']); $this->assertEquals(0, count($events['warnings'])); @@ -539,7 +535,7 @@ public function test_get_calendar_events() { $paramevents = array('categoryids' => array($category->id, $category2->id, $category2b->id)); $options = array('timeend' => time() + 7 * WEEKSECS, 'userevents' => false, 'siteevents' => false); $events = core_calendar_external::get_calendar_events($paramevents, $options); - $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); + $events = \external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(2, count($events['events'])); $this->assertEquals(0, count($events['warnings'])); $this->assertEquals($catevent1->id, $events['events'][0]['id']); @@ -569,10 +565,10 @@ public function test_get_calendar_events_with_mathjax() { // Now call the WebService. $events = core_calendar_external::get_calendar_events(); - $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); + $events = \external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); // Format the original data. - $sitecontext = context_system::instance(); + $sitecontext = \context_system::instance(); $siteevent->name = $siteevent->format_external_name(); list($siteevent->description, $siteevent->descriptionformat) = $siteevent->format_external_text(); @@ -596,7 +592,7 @@ public function test_core_create_calendar_events() { // Create a few stuff to test with. $user = $this->getDataGenerator()->create_user(); $course = $this->getDataGenerator()->create_course(); - $record = new stdClass(); + $record = new \stdClass(); $record->courseid = $course->id; $group = $this->getDataGenerator()->create_group($record); @@ -610,7 +606,7 @@ public function test_core_create_calendar_events() { array('name' => 'user') ); $eventsret = core_calendar_external::create_calendar_events($events); - $eventsret = external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret); + $eventsret = \external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret); // Check to see if things were created properly. $aftercount = $DB->count_records('event'); @@ -618,8 +614,8 @@ public function test_core_create_calendar_events() { $this->assertEquals(5, count($eventsret['events'])); $this->assertEquals(0, count($eventsret['warnings'])); - $sitecontext = context_system::instance(); - $coursecontext = context_course::instance($course->id); + $sitecontext = \context_system::instance(); + $coursecontext = \context_course::instance($course->id); $this->setUser($user); $prevcount = $aftercount; @@ -634,7 +630,7 @@ public function test_core_create_calendar_events() { $this->assignUserCapability('moodle/calendar:manageentries', $coursecontext->id, $role->id); $this->assignUserCapability('moodle/calendar:managegroupentries', $coursecontext->id, $role->id); $eventsret = core_calendar_external::create_calendar_events($events); - $eventsret = external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret); + $eventsret = \external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret); // Check to see if things were created properly. $aftercount = $DB->count_records('event'); $this->assertEquals($prevcount + 4, $aftercount); @@ -645,7 +641,7 @@ public function test_core_create_calendar_events() { $this->setGuestUser(); $prevcount = $DB->count_records('event'); $eventsret = core_calendar_external::create_calendar_events($events); - $eventsret = external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret); + $eventsret = \external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret); $aftercount = $DB->count_records('event'); $this->assertEquals($prevcount, $aftercount); $this->assertEquals(0, count($eventsret['events'])); @@ -656,7 +652,7 @@ public function test_core_create_calendar_events() { $this->unassignUserCapability('moodle/calendar:managegroupentries', $coursecontext->id, $role->id); $prevcount = $DB->count_records('event'); $eventsret = core_calendar_external::create_calendar_events($events); - $eventsret = external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret); + $eventsret = \external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret); $aftercount = $DB->count_records('event'); $this->assertEquals($prevcount + 1, $aftercount); // User event. $this->assertEquals(1, count($eventsret['events'])); @@ -698,7 +694,7 @@ public function test_get_calendar_action_events_by_timesort_after_time() { $event8 = $this->create_calendar_event('Event 8', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 8])); $result = core_calendar_external::get_calendar_action_events_by_timesort(5); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_timesort_returns(), $result ); @@ -713,7 +709,7 @@ public function test_get_calendar_action_events_by_timesort_after_time() { $this->assertEquals($event8->id, $result['lastid']); $result = core_calendar_external::get_calendar_action_events_by_timesort(9); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_timesort_returns(), $result ); @@ -725,7 +721,7 @@ public function test_get_calendar_action_events_by_timesort_after_time() { // Requesting action events on behalf of another user. $this->setAdminUser(); $result = core_calendar_external::get_calendar_action_events_by_timesort(5, null, 0, 20, false, $user->id); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_timesort_returns(), $result ); @@ -775,7 +771,7 @@ public function test_get_calendar_action_events_by_timesort_before_time() { $event8 = $this->create_calendar_event('Event 8', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 9])); $result = core_calendar_external::get_calendar_action_events_by_timesort(null, 5); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_timesort_returns(), $result ); @@ -790,7 +786,7 @@ public function test_get_calendar_action_events_by_timesort_before_time() { $this->assertEquals($event4->id, $result['lastid']); $result = core_calendar_external::get_calendar_action_events_by_timesort(null, 1); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_timesort_returns(), $result ); @@ -803,7 +799,7 @@ public function test_get_calendar_action_events_by_timesort_before_time() { $this->setAdminUser(); $result = core_calendar_external::get_calendar_action_events_by_timesort(null, 5, 0, 20, false, $user->id); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_timesort_returns(), $result ); @@ -852,7 +848,7 @@ public function test_get_calendar_events_override() { $paramevents = array('courseids' => array($course->id)); $options = array ('siteevents' => true, 'userevents' => true); $events = core_calendar_external::get_calendar_events($paramevents, $options); - $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); + $events = \external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(1, count($events['events'])); $this->assertEquals(0, count($events['warnings'])); $this->assertEquals('Base event', $events['events'][0]['name']); @@ -860,7 +856,7 @@ public function test_get_calendar_events_override() { // Retrieve events for the first student - both events are returned. $this->setUser($user); $events = core_calendar_external::get_calendar_events($paramevents, $options); - $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); + $events = \external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(2, count($events['events'])); $this->assertEquals(0, count($events['warnings'])); $this->assertEquals('Base event', $events['events'][0]['name']); @@ -870,7 +866,7 @@ public function test_get_calendar_events_override() { $this->setUser($teacher); $paramevents = ['eventids' => [$event2->id]]; $events = core_calendar_external::get_calendar_events($paramevents, $options); - $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); + $events = \external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(1, count($events['events'])); $this->assertEquals(0, count($events['warnings'])); $this->assertEquals('User event', $events['events'][0]['name']); @@ -879,7 +875,7 @@ public function test_get_calendar_events_override() { $this->setUser($anotheruser); $paramevents = ['eventids' => [$event2->id, $event1->id]]; $events = core_calendar_external::get_calendar_events($paramevents, $options); - $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); + $events = \external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(0, count($events['events'])); $this->assertEquals(0, count($events['warnings'])); } @@ -918,7 +914,7 @@ public function test_get_calendar_action_events_by_timesort_time_range() { $event8 = $this->create_calendar_event('Event 8', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 8])); $result = core_calendar_external::get_calendar_action_events_by_timesort(3, 6); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_timesort_returns(), $result ); @@ -933,7 +929,7 @@ public function test_get_calendar_action_events_by_timesort_time_range() { $this->assertEquals($event6->id, $result['lastid']); $result = core_calendar_external::get_calendar_action_events_by_timesort(10, 15); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_timesort_returns(), $result ); @@ -978,7 +974,7 @@ public function test_get_calendar_action_events_by_timesort_time_limit_offset() $event8 = $this->create_calendar_event('Event 8', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 8])); $result = core_calendar_external::get_calendar_action_events_by_timesort(2, 7, $event3->id, 2); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_timesort_returns(), $result ); @@ -991,7 +987,7 @@ public function test_get_calendar_action_events_by_timesort_time_limit_offset() $this->assertEquals($event5->id, $result['lastid']); $result = core_calendar_external::get_calendar_action_events_by_timesort(2, 7, $event5->id, 2); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_timesort_returns(), $result ); @@ -1004,7 +1000,7 @@ public function test_get_calendar_action_events_by_timesort_time_limit_offset() $this->assertEquals($event7->id, $result['lastid']); $result = core_calendar_external::get_calendar_action_events_by_timesort(2, 7, $event7->id, 2); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_timesort_returns(), $result ); @@ -1085,7 +1081,7 @@ public function test_get_calendar_action_events_by_timesort_for_other_users() { // Student1 will see an exception if he/she trying to view student2's data. $this->setUser($user1); - $this->expectException(required_capability_exception::class); + $this->expectException(\required_capability_exception::class); $this->expectExceptionMessage('error/nopermission'); $result = core_calendar_external::get_calendar_action_events_by_timesort(0, null, 0, 20, true, $user2->id); } @@ -1132,7 +1128,7 @@ public function test_get_calendar_action_events_by_course_after_time() { } $result = core_calendar_external::get_calendar_action_events_by_course($course1->id, 5); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_course_returns(), $result ); @@ -1145,7 +1141,7 @@ public function test_get_calendar_action_events_by_course_after_time() { $this->assertEquals('Event 8', $result[3]['name']); $result = core_calendar_external::get_calendar_action_events_by_course($course1->id, 9); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_course_returns(), $result ); @@ -1196,7 +1192,7 @@ public function test_get_calendar_action_events_by_course_before_time() { } $result = core_calendar_external::get_calendar_action_events_by_course($course1->id, null, 5); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_course_returns(), $result ); @@ -1209,7 +1205,7 @@ public function test_get_calendar_action_events_by_course_before_time() { $this->assertEquals('Event 4', $result[3]['name']); $result = core_calendar_external::get_calendar_action_events_by_course($course1->id, null, 1); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_course_returns(), $result ); @@ -1260,7 +1256,7 @@ public function test_get_calendar_action_events_by_course_time_range() { } $result = core_calendar_external::get_calendar_action_events_by_course($course1->id, 3, 6); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_course_returns(), $result ); @@ -1273,7 +1269,7 @@ public function test_get_calendar_action_events_by_course_time_range() { $this->assertEquals('Event 6', $result[3]['name']); $result = core_calendar_external::get_calendar_action_events_by_course($course1->id, 10, 15); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_course_returns(), $result ); @@ -1326,7 +1322,7 @@ public function test_get_calendar_action_events_by_course_time_limit_offset() { $result = core_calendar_external::get_calendar_action_events_by_course( $course1->id, 2, 7, $records[2]->id, 2); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_course_returns(), $result ); @@ -1338,7 +1334,7 @@ public function test_get_calendar_action_events_by_course_time_limit_offset() { $result = core_calendar_external::get_calendar_action_events_by_course( $course1->id, 2, 7, $records[4]->id, 2); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_course_returns(), $result ); @@ -1350,7 +1346,7 @@ public function test_get_calendar_action_events_by_course_time_limit_offset() { $result = core_calendar_external::get_calendar_action_events_by_course( $course1->id, 2, 7, $records[6]->id, 2); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_course_returns(), $result ); @@ -1393,7 +1389,7 @@ public function test_get_calendar_action_events_by_course_with_search() { // No result found for fake search. $result = core_calendar_external::get_calendar_action_events_by_course($course->id, null, null, 0, 20, 'Fake search'); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_course_returns(), $result ); @@ -1402,7 +1398,7 @@ public function test_get_calendar_action_events_by_course_with_search() { // Search for event name called 'Event 1'. $result = core_calendar_external::get_calendar_action_events_by_course($course->id, null, null, 0, 20, 'Event 1'); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_course_returns(), $result ); @@ -1412,7 +1408,7 @@ public function test_get_calendar_action_events_by_course_with_search() { // Search for activity type called 'assign'. $result = core_calendar_external::get_calendar_action_events_by_course($course->id, null, null, 0, 20, 'assign'); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_course_returns(), $result ); @@ -1483,7 +1479,7 @@ public function test_get_action_events_by_courses() { } $result = core_calendar_external::get_calendar_action_events_by_courses([], 1); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_courses_returns(), $result ); @@ -1492,7 +1488,7 @@ public function test_get_action_events_by_courses() { $this->assertEmpty($result); $result = core_calendar_external::get_calendar_action_events_by_courses([$course1->id], 3); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_courses_returns(), $result ); @@ -1502,7 +1498,7 @@ public function test_get_action_events_by_courses() { $this->assertEmpty($groupedbycourse[$course1->id]); $result = core_calendar_external::get_calendar_action_events_by_courses([$course1->id], 1); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_courses_returns(), $result ); @@ -1514,7 +1510,7 @@ public function test_get_action_events_by_courses() { $result = core_calendar_external::get_calendar_action_events_by_courses( [$course1->id, $course2->id], 1); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_courses_returns(), $result ); @@ -1530,7 +1526,7 @@ public function test_get_action_events_by_courses() { $result = core_calendar_external::get_calendar_action_events_by_courses( [$course1->id, $course2->id], 2, 4); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_courses_returns(), $result ); @@ -1545,7 +1541,7 @@ public function test_get_action_events_by_courses() { $result = core_calendar_external::get_calendar_action_events_by_courses( [$course1->id, $course2->id], 1, null, 1); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_courses_returns(), $result ); @@ -1619,7 +1615,7 @@ public function test_get_action_events_by_courses_with_search() { // No result found for fake search. $result = core_calendar_external::get_calendar_action_events_by_courses([$course1->id, $course2->id, $course3->id], 1, null, 20, 'Fake search'); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_courses_returns(), $result ); @@ -1632,7 +1628,7 @@ public function test_get_action_events_by_courses_with_search() { // Search for event name called 'Event 1'. $result = core_calendar_external::get_calendar_action_events_by_courses([$course1->id, $course2->id, $course3->id], 1, null, 20, 'Event 1'); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_courses_returns(), $result ); @@ -1647,7 +1643,7 @@ public function test_get_action_events_by_courses_with_search() { // Search for activity type called 'assign'. $result = core_calendar_external::get_calendar_action_events_by_courses([$course1->id, $course2->id, $course3->id], 1, null, 20, 'assign'); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::get_calendar_action_events_by_courses_returns(), $result ); @@ -1683,7 +1679,7 @@ public function test_delete_calendar_events_for_modules() { ]; } - $this->expectException('moodle_exception'); + $this->expectException(\moodle_exception::class); core_calendar_external::delete_calendar_events($params); } @@ -1696,9 +1692,9 @@ public function test_update_event_start_day() { $user = $generator->create_user(); $roleid = $generator->create_role(); $context = \context_system::instance(); - $originalstarttime = new DateTimeImmutable('2017-01-1T15:00:00+08:00'); - $newstartdate = new DateTimeImmutable('2018-02-2T10:00:00+08:00'); - $expected = new DateTimeImmutable('2018-02-2T15:00:00+08:00'); + $originalstarttime = new \DateTimeImmutable('2017-01-1T15:00:00+08:00'); + $newstartdate = new \DateTimeImmutable('2018-02-2T10:00:00+08:00'); + $expected = new \DateTimeImmutable('2018-02-2T15:00:00+08:00'); $generator->role_assign($roleid, $user->id, $context->id); assign_capability('moodle/calendar:manageownentries', CAP_ALLOW, $roleid, $context, true); @@ -1719,7 +1715,7 @@ public function test_update_event_start_day() { ); $result = core_calendar_external::update_event_start_day($event->id, $newstartdate->getTimestamp()); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::update_event_start_day_returns(), $result ); @@ -1736,9 +1732,9 @@ public function test_update_event_start_day_no_permission() { $user = $generator->create_user(); $roleid = $generator->create_role(); $context = \context_system::instance(); - $originalstarttime = new DateTimeImmutable('2017-01-1T15:00:00+08:00'); - $newstartdate = new DateTimeImmutable('2018-02-2T10:00:00+08:00'); - $expected = new DateTimeImmutable('2018-02-2T15:00:00+08:00'); + $originalstarttime = new \DateTimeImmutable('2017-01-1T15:00:00+08:00'); + $newstartdate = new \DateTimeImmutable('2018-02-2T10:00:00+08:00'); + $expected = new \DateTimeImmutable('2018-02-2T15:00:00+08:00'); $generator->role_assign($roleid, $user->id, $context->id); assign_capability('moodle/calendar:manageownentries', CAP_ALLOW, $roleid, $context, true); @@ -1759,9 +1755,9 @@ public function test_update_event_start_day_no_permission() { ); assign_capability('moodle/calendar:manageownentries', CAP_PROHIBIT, $roleid, $context, true); - $this->expectException('moodle_exception'); + $this->expectException(\moodle_exception::class); $result = core_calendar_external::update_event_start_day($event->id, $newstartdate->getTimestamp()); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::update_event_start_day_returns(), $result ); @@ -1778,9 +1774,9 @@ public function test_update_event_start_day_module_event() { $moduleinstance = $plugingenerator->create_instance(['course' => $course->id]); $roleid = $generator->create_role(); $context = \context_course::instance($course->id); - $originalstarttime = new DateTimeImmutable('2017-01-1T15:00:00+08:00'); - $newstartdate = new DateTimeImmutable('2018-02-2T10:00:00+08:00'); - $expected = new DateTimeImmutable('2018-02-2T15:00:00+08:00'); + $originalstarttime = new \DateTimeImmutable('2017-01-1T15:00:00+08:00'); + $newstartdate = new \DateTimeImmutable('2018-02-2T10:00:00+08:00'); + $expected = new \DateTimeImmutable('2018-02-2T15:00:00+08:00'); $generator->role_assign($roleid, $user->id, $context->id); $generator->enrol_user($user->id, $course->id); @@ -1803,9 +1799,9 @@ public function test_update_event_start_day_module_event() { ); assign_capability('moodle/calendar:manageentries', CAP_ALLOW, $roleid, $context, true); - $this->expectException('moodle_exception'); + $this->expectException(\moodle_exception::class); $result = core_calendar_external::update_event_start_day($event->id, $newstartdate->getTimestamp()); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::update_event_start_day_returns(), $result ); @@ -1817,9 +1813,9 @@ public function test_update_event_start_day_module_event() { */ public function test_submit_create_update_form_validation_error() { $user = $this->getDataGenerator()->create_user(); - $timestart = new DateTime(); - $interval = new DateInterval("P1D"); // One day. - $timedurationuntil = new DateTime(); + $timestart = new \DateTime(); + $interval = new \DateInterval("P1D"); // One day. + $timedurationuntil = new \DateTime(); $timedurationuntil->sub($interval); $formdata = [ 'id' => 0, @@ -1858,7 +1854,7 @@ public function test_submit_create_update_form_validation_error() { $this->resetAfterTest(true); $this->setUser($user); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::submit_create_update_form_returns(), core_calendar_external::submit_create_update_form($querystring) ); @@ -1875,9 +1871,9 @@ public function test_submit_create_update_form_create_user_event() { $user = $generator->create_user(); $roleid = $generator->create_role(); $context = \context_system::instance(); - $timestart = new DateTime(); - $interval = new DateInterval("P1D"); // One day. - $timedurationuntil = new DateTime(); + $timestart = new \DateTime(); + $interval = new \DateInterval("P1D"); // One day. + $timedurationuntil = new \DateTime(); $timedurationuntil->add($interval); $formdata = [ 'id' => 0, @@ -1920,7 +1916,7 @@ public function test_submit_create_update_form_create_user_event() { $this->resetAfterTest(true); $this->setUser($user); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::submit_create_update_form_returns(), core_calendar_external::submit_create_update_form($querystring) ); @@ -1940,9 +1936,9 @@ public function test_submit_create_update_form_create_user_event_no_permission() $user = $generator->create_user(); $roleid = $generator->create_role(); $context = \context_system::instance(); - $timestart = new DateTime(); - $interval = new DateInterval("P1D"); // One day. - $timedurationuntil = new DateTime(); + $timestart = new \DateTime(); + $interval = new \DateInterval("P1D"); // One day. + $timedurationuntil = new \DateTime(); $timedurationuntil->add($interval); $formdata = [ 'id' => 0, @@ -1984,8 +1980,9 @@ public function test_submit_create_update_form_create_user_event_no_permission() $this->resetAfterTest(true); $this->setUser($user); - $this->expectException(moodle_exception::class); - external_api::clean_returnvalue( + $this->expectException(\moodle_exception::class); + + \external_api::clean_returnvalue( core_calendar_external::submit_create_update_form_returns(), core_calendar_external::submit_create_update_form($querystring) ); @@ -1998,11 +1995,11 @@ public function test_submit_create_update_form_create_user_event_no_permission() public function test_submit_create_update_form_create_site_event() { $generator = $this->getDataGenerator(); $user = $generator->create_user(); - $context = context_system::instance(); + $context = \context_system::instance(); $roleid = $generator->create_role(); - $timestart = new DateTime(); - $interval = new DateInterval("P1D"); // One day. - $timedurationuntil = new DateTime(); + $timestart = new \DateTime(); + $interval = new \DateInterval("P1D"); // One day. + $timedurationuntil = new \DateTime(); $timedurationuntil->add($interval); $formdata = [ 'id' => 0, @@ -2046,7 +2043,7 @@ public function test_submit_create_update_form_create_site_event() { $this->resetAfterTest(true); $this->setUser($user); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::submit_create_update_form_returns(), core_calendar_external::submit_create_update_form($querystring) ); @@ -2064,11 +2061,11 @@ public function test_submit_create_update_form_create_site_event() { public function test_submit_create_update_form_create_site_event_no_permission() { $generator = $this->getDataGenerator(); $user = $generator->create_user(); - $context = context_course::instance(SITEID); + $context = \context_course::instance(SITEID); $roleid = $generator->create_role(); - $timestart = new DateTime(); - $interval = new DateInterval("P1D"); // One day. - $timedurationuntil = new DateTime(); + $timestart = new \DateTime(); + $interval = new \DateInterval("P1D"); // One day. + $timedurationuntil = new \DateTime(); $timedurationuntil->add($interval); $formdata = [ 'id' => 0, @@ -2111,7 +2108,7 @@ public function test_submit_create_update_form_create_site_event_no_permission() $this->resetAfterTest(true); $this->setUser($user); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::submit_create_update_form_returns(), core_calendar_external::submit_create_update_form($querystring) ); @@ -2127,11 +2124,11 @@ public function test_submit_create_update_form_create_course_event() { $generator = $this->getDataGenerator(); $user = $generator->create_user(); $course = $generator->create_course(); - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $roleid = $generator->create_role(); - $timestart = new DateTime(); - $interval = new DateInterval("P1D"); // One day. - $timedurationuntil = new DateTime(); + $timestart = new \DateTime(); + $interval = new \DateInterval("P1D"); // One day. + $timedurationuntil = new \DateTime(); $timedurationuntil->add($interval); $formdata = [ 'id' => 0, @@ -2177,7 +2174,7 @@ public function test_submit_create_update_form_create_course_event() { $this->resetAfterTest(true); $this->setUser($user); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::submit_create_update_form_returns(), core_calendar_external::submit_create_update_form($querystring) ); @@ -2197,11 +2194,11 @@ public function test_submit_create_update_form_create_course_event_no_permission $generator = $this->getDataGenerator(); $user = $generator->create_user(); $course = $generator->create_course(); - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $roleid = $generator->create_role(); - $timestart = new DateTime(); - $interval = new DateInterval("P1D"); // One day. - $timedurationuntil = new DateTime(); + $timestart = new \DateTime(); + $interval = new \DateInterval("P1D"); // One day. + $timedurationuntil = new \DateTime(); $timedurationuntil->add($interval); $formdata = [ 'id' => 0, @@ -2246,7 +2243,7 @@ public function test_submit_create_update_form_create_course_event_no_permission $this->resetAfterTest(true); $this->setUser($user); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::submit_create_update_form_returns(), core_calendar_external::submit_create_update_form($querystring) ); @@ -2263,11 +2260,11 @@ public function test_submit_create_update_form_create_course_event_not_enrolled( $user = $generator->create_user(); $course = $generator->create_course(); $course2 = $generator->create_course(); - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $roleid = $generator->create_role(); - $timestart = new DateTime(); - $interval = new DateInterval("P1D"); // One day. - $timedurationuntil = new DateTime(); + $timestart = new \DateTime(); + $interval = new \DateInterval("P1D"); // One day. + $timedurationuntil = new \DateTime(); $timedurationuntil->add($interval); $formdata = [ 'id' => 0, @@ -2312,7 +2309,7 @@ public function test_submit_create_update_form_create_course_event_not_enrolled( $this->resetAfterTest(true); $this->setUser($user); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::submit_create_update_form_returns(), core_calendar_external::submit_create_update_form($querystring) ); @@ -2329,11 +2326,11 @@ public function test_submit_create_update_form_create_group_event_group_member_m $user = $generator->create_user(); $course = $generator->create_course(); $group = $generator->create_group(array('courseid' => $course->id)); - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $roleid = $generator->create_role(); - $timestart = new DateTime(); - $interval = new DateInterval("P1D"); // One day. - $timedurationuntil = new DateTime(); + $timestart = new \DateTime(); + $interval = new \DateInterval("P1D"); // One day. + $timedurationuntil = new \DateTime(); $timedurationuntil->add($interval); $formdata = [ 'id' => 0, @@ -2381,7 +2378,7 @@ public function test_submit_create_update_form_create_group_event_group_member_m $this->resetAfterTest(true); $this->setUser($user); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::submit_create_update_form_returns(), core_calendar_external::submit_create_update_form($querystring) ); @@ -2402,11 +2399,11 @@ public function test_submit_create_update_form_create_group_event_group_member_m $user = $generator->create_user(); $course = $generator->create_course(); $group = $generator->create_group(array('courseid' => $course->id)); - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $roleid = $generator->create_role(); - $timestart = new DateTime(); - $interval = new DateInterval("P1D"); // One day. - $timedurationuntil = new DateTime(); + $timestart = new \DateTime(); + $interval = new \DateInterval("P1D"); // One day. + $timedurationuntil = new \DateTime(); $timedurationuntil->add($interval); $formdata = [ 'id' => 0, @@ -2455,7 +2452,7 @@ public function test_submit_create_update_form_create_group_event_group_member_m $this->resetAfterTest(true); $this->setUser($user); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::submit_create_update_form_returns(), core_calendar_external::submit_create_update_form($querystring) ); @@ -2476,11 +2473,11 @@ public function test_submit_create_update_form_create_group_event_access_all_gro $user = $generator->create_user(); $course = $generator->create_course(); $group = $generator->create_group(array('courseid' => $course->id)); - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $roleid = $generator->create_role(); - $timestart = new DateTime(); - $interval = new DateInterval("P1D"); // One day. - $timedurationuntil = new DateTime(); + $timestart = new \DateTime(); + $interval = new \DateInterval("P1D"); // One day. + $timedurationuntil = new \DateTime(); $timedurationuntil->add($interval); $formdata = [ 'id' => 0, @@ -2528,7 +2525,7 @@ public function test_submit_create_update_form_create_group_event_access_all_gro $this->resetAfterTest(true); $this->setUser($user); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::submit_create_update_form_returns(), core_calendar_external::submit_create_update_form($querystring) ); @@ -2550,11 +2547,11 @@ public function test_submit_create_update_form_create_group_event_non_member_no_ $user = $generator->create_user(); $course = $generator->create_course(); $group = $generator->create_group(array('courseid' => $course->id)); - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $roleid = $generator->create_role(); - $timestart = new DateTime(); - $interval = new DateInterval("P1D"); // One day. - $timedurationuntil = new DateTime(); + $timestart = new \DateTime(); + $interval = new \DateInterval("P1D"); // One day. + $timedurationuntil = new \DateTime(); $timedurationuntil->add($interval); $formdata = [ 'id' => 0, @@ -2601,7 +2598,7 @@ public function test_submit_create_update_form_create_group_event_non_member_no_ $this->resetAfterTest(true); $this->setUser($user); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_calendar_external::submit_create_update_form_returns(), core_calendar_external::submit_create_update_form($querystring) ); @@ -2623,13 +2620,13 @@ public function test_get_calendar_monthly_view_no_course_permission() { $course = $generator->create_course(); $generator->enrol_user($user1->id, $course->id, 'student'); $name = 'Course Event (course' . $course->id . ')'; - $record = new stdClass(); + $record = new \stdClass(); $record->courseid = $course->id; $courseevent = $this->create_calendar_event($name, $USER->id, 'course', 0, time(), $record); - $timestart = new DateTime(); + $timestart = new \DateTime(); // Admin can load the course. - $data = external_api::clean_returnvalue( + $data = \external_api::clean_returnvalue( core_calendar_external::get_calendar_monthly_view_returns(), core_calendar_external::get_calendar_monthly_view($timestart->format('Y'), $timestart->format('n'), $course->id, null, false, true, $timestart->format('j')) @@ -2637,7 +2634,7 @@ public function test_get_calendar_monthly_view_no_course_permission() { $this->assertEquals($data['courseid'], $course->id); // User enrolled in the course can load the course calendar. $this->setUser($user1); - $data = external_api::clean_returnvalue( + $data = \external_api::clean_returnvalue( core_calendar_external::get_calendar_monthly_view_returns(), core_calendar_external::get_calendar_monthly_view($timestart->format('Y'), $timestart->format('n'), $course->id, null, false, true, $timestart->format('j')) @@ -2645,8 +2642,8 @@ public function test_get_calendar_monthly_view_no_course_permission() { $this->assertEquals($data['courseid'], $course->id); // User not enrolled in the course cannot load the course calendar. $this->setUser($user2); - $this->expectException('require_login_exception'); - $data = external_api::clean_returnvalue( + $this->expectException(\require_login_exception::class); + $data = \external_api::clean_returnvalue( core_calendar_external::get_calendar_monthly_view_returns(), core_calendar_external::get_calendar_monthly_view($timestart->format('Y'), $timestart->format('n'), $course->id, null, false, false, $timestart->format('j')) @@ -2660,8 +2657,8 @@ public function test_get_calendar_monthly_view_with_day_provided() { $this->resetAfterTest(); $this->setAdminUser(); - $timestart = new DateTime(); - $data = external_api::clean_returnvalue( + $timestart = new \DateTime(); + $data = \external_api::clean_returnvalue( core_calendar_external::get_calendar_monthly_view_returns(), core_calendar_external::get_calendar_monthly_view($timestart->format('Y'), $timestart->format('n'), SITEID, null, false, true, $timestart->format('j')) @@ -2683,13 +2680,13 @@ public function test_get_calendar_day_view_no_course_permission() { $course = $generator->create_course(); $generator->enrol_user($user1->id, $course->id, 'student'); $name = 'Course Event (course' . $course->id . ')'; - $record = new stdClass(); + $record = new \stdClass(); $record->courseid = $course->id; $courseevent = $this->create_calendar_event($name, $USER->id, 'course', 0, time(), $record); - $timestart = new DateTime(); + $timestart = new \DateTime(); // Admin can load the course. - $data = external_api::clean_returnvalue( + $data = \external_api::clean_returnvalue( core_calendar_external::get_calendar_day_view_returns(), core_calendar_external::get_calendar_day_view($timestart->format('Y'), $timestart->format('n'), $timestart->format('j'), $course->id, null) @@ -2697,7 +2694,7 @@ public function test_get_calendar_day_view_no_course_permission() { $this->assertEquals($data['courseid'], $course->id); // User enrolled in the course can load the course calendar. $this->setUser($user1); - $data = external_api::clean_returnvalue( + $data = \external_api::clean_returnvalue( core_calendar_external::get_calendar_day_view_returns(), core_calendar_external::get_calendar_day_view($timestart->format('Y'), $timestart->format('n'), $timestart->format('j'), $course->id, null) @@ -2705,8 +2702,8 @@ public function test_get_calendar_day_view_no_course_permission() { $this->assertEquals($data['courseid'], $course->id); // User not enrolled in the course cannot load the course calendar. $this->setUser($user2); - $this->expectException('require_login_exception'); - $data = external_api::clean_returnvalue( + $this->expectException(\require_login_exception::class); + $data = \external_api::clean_returnvalue( core_calendar_external::get_calendar_day_view_returns(), core_calendar_external::get_calendar_day_view($timestart->format('Y'), $timestart->format('n'), $timestart->format('j'), $course->id, null) @@ -2727,27 +2724,27 @@ public function test_get_calendar_upcoming_view_no_course_permission() { $course = $generator->create_course(); $generator->enrol_user($user1->id, $course->id, 'student'); $name = 'Course Event (course' . $course->id . ')'; - $record = new stdClass(); + $record = new \stdClass(); $record->courseid = $course->id; $courseevent = $this->create_calendar_event($name, $USER->id, 'course', 0, time(), $record); // Admin can load the course. - $data = external_api::clean_returnvalue( + $data = \external_api::clean_returnvalue( core_calendar_external::get_calendar_upcoming_view_returns(), core_calendar_external::get_calendar_upcoming_view($course->id, null) ); $this->assertEquals($data['courseid'], $course->id); // User enrolled in the course can load the course calendar. $this->setUser($user1); - $data = external_api::clean_returnvalue( + $data = \external_api::clean_returnvalue( core_calendar_external::get_calendar_upcoming_view_returns(), core_calendar_external::get_calendar_upcoming_view($course->id, null) ); $this->assertEquals($data['courseid'], $course->id); // User not enrolled in the course cannot load the course calendar. $this->setUser($user2); - $this->expectException('require_login_exception'); - $data = external_api::clean_returnvalue( + $this->expectException(\require_login_exception::class); + $data = \external_api::clean_returnvalue( core_calendar_external::get_calendar_upcoming_view_returns(), core_calendar_external::get_calendar_upcoming_view($course->id, null) ); @@ -2767,27 +2764,27 @@ public function test_get_calendar_event_by_id_no_course_permission() { $course = $generator->create_course(); $generator->enrol_user($user1->id, $course->id, 'student'); $name = 'Course Event (course' . $course->id . ')'; - $record = new stdClass(); + $record = new \stdClass(); $record->courseid = $course->id; $courseevent = $this->create_calendar_event($name, $USER->id, 'course', 0, time(), $record); // Admin can load the course event. - $data = external_api::clean_returnvalue( + $data = \external_api::clean_returnvalue( core_calendar_external::get_calendar_event_by_id_returns(), core_calendar_external::get_calendar_event_by_id($courseevent->id) ); $this->assertEquals($data['event']['id'], $courseevent->id); // User enrolled in the course can load the course event. $this->setUser($user1); - $data = external_api::clean_returnvalue( + $data = \external_api::clean_returnvalue( core_calendar_external::get_calendar_event_by_id_returns(), core_calendar_external::get_calendar_event_by_id($courseevent->id) ); $this->assertEquals($data['event']['id'], $courseevent->id); // User not enrolled in the course cannot load the course event. $this->setUser($user2); - $this->expectException(moodle_exception::class); - $data = external_api::clean_returnvalue( + $this->expectException(\moodle_exception::class); + $data = \external_api::clean_returnvalue( core_calendar_external::get_calendar_event_by_id_returns(), core_calendar_external::get_calendar_event_by_id($courseevent->id) ); @@ -2799,7 +2796,7 @@ public function test_get_calendar_event_by_id_no_course_permission() { * @return array */ public function test_get_calendar_event_by_id_prevent_read_other_users_events_data_provider(): array { - $syscontext = context_system::instance(); + $syscontext = \context_system::instance(); $managerrole = 'manager'; return [ [true, false, $syscontext, $managerrole, true], @@ -2822,7 +2819,7 @@ public function test_get_calendar_event_by_id_prevent_read_other_users_events_da * @param bool $expectexception Should the test throw exception */ public function test_get_calendar_event_by_id_prevent_read_other_users_events( - bool $isadminevent, bool $isadmin, ?stdClass $readerrolecontext, + bool $isadminevent, bool $isadmin, ?\stdClass $readerrolecontext, ?string $readerrolename, bool $expectexception) { global $USER, $DB; @@ -2836,7 +2833,7 @@ public function test_get_calendar_event_by_id_prevent_read_other_users_events( $this->setUser($user); } $userevent = $this->create_calendar_event('user event', $USER->id, 'user', 0, time()); - $results = external_api::clean_returnvalue( + $results = \external_api::clean_returnvalue( core_calendar_external::get_calendar_event_by_id_returns(), core_calendar_external::get_calendar_event_by_id($userevent->id) ); @@ -2856,9 +2853,9 @@ public function test_get_calendar_event_by_id_prevent_read_other_users_events( if ($expectexception) { // Setup if exception is expected for the test. - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); } - external_api::clean_returnvalue( + \external_api::clean_returnvalue( core_calendar_external::get_calendar_event_by_id_returns(), core_calendar_external::get_calendar_event_by_id($userevent->id) ); @@ -2870,7 +2867,7 @@ public function test_get_calendar_event_by_id_prevent_read_other_users_events( * @return array */ public function test_edit_or_delete_other_users_events_data_provider(): array { - $syscontext = context_system::instance(); + $syscontext = \context_system::instance(); $managerrole = 'manager'; return [ [false, false, $syscontext, $managerrole, false], @@ -2892,7 +2889,7 @@ public function test_edit_or_delete_other_users_events_data_provider(): array { * @param bool $expectexception Whether the test should throw an exception or not. */ public function test_delete_other_users_events(bool $isadmin, bool $isadminevent, - ?stdClass $writerrolecontext, ?string $writerrolename, bool $expectexception) { + ?\stdClass $writerrolecontext, ?string $writerrolename, bool $expectexception) { global $DB, $USER; $this->resetAfterTest(); @@ -2919,7 +2916,7 @@ public function test_delete_other_users_events(bool $isadmin, bool $isadminevent } if ($expectexception) { - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); } $events = [ ['eventid' => $userevent->id, 'repeat' => 0] @@ -2939,7 +2936,7 @@ public function test_delete_other_users_events(bool $isadmin, bool $isadminevent * @param bool $expectexception Whether the test should throw an exception or not. */ public function test_edit_other_users_events(bool $isadmin, bool $isadminevent, - ?stdClass $writerrolecontext, ?string $writerrolename, bool $expectexception) { + ?\stdClass $writerrolecontext, ?string $writerrolename, bool $expectexception) { global $DB, $USER; $this->resetAfterTest(); @@ -2992,7 +2989,7 @@ public function test_edit_other_users_events(bool $isadmin, bool $isadminevent, $USER->ignoresesskey = true; if ($expectexception) { - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); } core_calendar_external::submit_create_update_form($querystring); } @@ -3009,7 +3006,7 @@ public function test_get_calendar_events_hidden_category() { $user1 = $generator->create_user(); $category = $generator->create_category(['visible' => 0]); $name = 'Category Event (category: ' . $category->id . ')'; - $record = new stdClass(); + $record = new \stdClass(); $record->categoryid = $category->id; $categoryevent = $this->create_calendar_event($name, $USER->id, 'category', 0, time(), $record); @@ -3018,14 +3015,14 @@ public function test_get_calendar_events_hidden_category() { ]; $options = []; // Admin can load the category event. - $data = external_api::clean_returnvalue( + $data = \external_api::clean_returnvalue( core_calendar_external::get_calendar_events_returns(), core_calendar_external::get_calendar_events($events, $options) ); $this->assertEquals($data['events'][0]['id'], $categoryevent->id); // User with no special permission to see hidden categories will not see the event. $this->setUser($user1); - $data = external_api::clean_returnvalue( + $data = \external_api::clean_returnvalue( core_calendar_external::get_calendar_events_returns(), core_calendar_external::get_calendar_events($events, $options) ); @@ -3043,7 +3040,7 @@ public function test_get_calendar_access_information_for_admins() { $CFG->calendar_adminseesall = 1; - $data = external_api::clean_returnvalue( + $data = \external_api::clean_returnvalue( core_calendar_external::get_calendar_access_information_returns(), core_calendar_external::get_calendar_access_information() ); @@ -3059,7 +3056,7 @@ public function test_get_calendar_access_information_for_authenticated_users() { $this->resetAfterTest(true); $this->setUser($this->getDataGenerator()->create_user()); - $data = external_api::clean_returnvalue( + $data = \external_api::clean_returnvalue( core_calendar_external::get_calendar_access_information_returns(), core_calendar_external::get_calendar_access_information() ); @@ -3082,7 +3079,7 @@ public function test_get_calendar_access_information_for_student_users() { $this->setUser($user); - $data = external_api::clean_returnvalue( + $data = \external_api::clean_returnvalue( core_calendar_external::get_calendar_access_information_returns(), core_calendar_external::get_calendar_access_information($course->id) ); @@ -3106,7 +3103,7 @@ public function test_get_calendar_access_information_for_teacher_users() { $this->setUser($user); - $data = external_api::clean_returnvalue( + $data = \external_api::clean_returnvalue( core_calendar_external::get_calendar_access_information_returns(), core_calendar_external::get_calendar_access_information($course->id) ); @@ -3123,7 +3120,7 @@ public function test_get_allowed_event_types_for_admins() { $this->resetAfterTest(true); $this->setAdminUser(); $CFG->calendar_adminseesall = 1; - $data = external_api::clean_returnvalue( + $data = \external_api::clean_returnvalue( core_calendar_external::get_allowed_event_types_returns(), core_calendar_external::get_allowed_event_types() ); @@ -3135,7 +3132,7 @@ public function test_get_allowed_event_types_for_admins() { public function test_get_allowed_event_types_for_authenticated_users() { $this->resetAfterTest(true); $this->setUser($this->getDataGenerator()->create_user()); - $data = external_api::clean_returnvalue( + $data = \external_api::clean_returnvalue( core_calendar_external::get_allowed_event_types_returns(), core_calendar_external::get_allowed_event_types() ); @@ -3152,7 +3149,7 @@ public function test_get_allowed_event_types_for_student_users() { $role = $DB->get_record('role', array('shortname' => 'student')); $this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id); $this->setUser($user); - $data = external_api::clean_returnvalue( + $data = \external_api::clean_returnvalue( core_calendar_external::get_allowed_event_types_returns(), core_calendar_external::get_allowed_event_types($course->id) ); @@ -3170,7 +3167,7 @@ public function test_get_allowed_event_types_for_teacher_users() { $this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id); $this->getDataGenerator()->create_group(['courseid' => $course->id]); $this->setUser($user); - $data = external_api::clean_returnvalue( + $data = \external_api::clean_returnvalue( core_calendar_external::get_allowed_event_types_returns(), core_calendar_external::get_allowed_event_types($course->id) ); @@ -3184,8 +3181,8 @@ public function test_get_timestamps_string_keys() { $this->resetAfterTest(true); $this->setAdminUser(); - $time1 = new DateTime('2018-12-30 00:00:00'); - $time2 = new DateTime('2019-03-27 23:59:00'); + $time1 = new \DateTime('2018-12-30 00:00:00'); + $time2 = new \DateTime('2019-03-27 23:59:00'); $dates = [ [ @@ -3227,8 +3224,8 @@ public function test_get_timestamps_no_keys() { $this->resetAfterTest(true); $this->setAdminUser(); - $time1 = new DateTime('2018-12-30 00:00:00'); - $time2 = new DateTime('2019-03-27 23:59:00'); + $time1 = new \DateTime('2018-12-30 00:00:00'); + $time2 = new \DateTime('2019-03-27 23:59:00'); $dates = [ [ diff --git a/cohort/tests/externallib_test.php b/cohort/tests/externallib_test.php index 101a58d94e90c..83e2a9727478d 100644 --- a/cohort/tests/externallib_test.php +++ b/cohort/tests/externallib_test.php @@ -14,14 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * External cohort API - * - * @package core_cohort - * @category external - * @copyright MediaTouch 2000 srl - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace core_cohort; + +use core_cohort_external; +use externallib_advanced_testcase; defined('MOODLE_INTERNAL') || die(); @@ -30,7 +26,15 @@ require_once($CFG->dirroot . '/webservice/tests/helpers.php'); require_once($CFG->dirroot . '/cohort/externallib.php'); -class core_cohort_externallib_testcase extends externallib_advanced_testcase { +/** + * External cohort API + * + * @package core_cohort + * @category external + * @copyright MediaTouch 2000 srl + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class externallib_test extends externallib_advanced_testcase { /** * Test create_cohorts @@ -42,7 +46,7 @@ public function test_create_cohorts() { set_config('allowcohortthemes', 1); - $contextid = context_system::instance()->id; + $contextid = \context_system::instance()->id; $category = $this->getDataGenerator()->create_category(); $cohort1 = array( @@ -80,7 +84,7 @@ public function test_create_cohorts() { // Call the external function. $this->setCurrentTimeStart(); $createdcohorts = core_cohort_external::create_cohorts(array($cohort1, $cohort2)); - $createdcohorts = external_api::clean_returnvalue(core_cohort_external::create_cohorts_returns(), $createdcohorts); + $createdcohorts = \external_api::clean_returnvalue(core_cohort_external::create_cohorts_returns(), $createdcohorts); // Check we retrieve the good total number of created cohorts + no error on capability. $this->assertEquals(2, count($createdcohorts)); @@ -97,7 +101,7 @@ public function test_create_cohorts() { // As $CFG->allowcohortthemes is enabled, theme must be initialised. $this->assertEquals($dbcohort->theme, $cohort1['theme']); } else if ($createdcohort['idnumber'] == $cohort2['idnumber']) { - $this->assertEquals($dbcohort->contextid, context_system::instance()->id); + $this->assertEquals($dbcohort->contextid, \context_system::instance()->id); $this->assertEquals($dbcohort->name, $cohort2['name']); $this->assertEquals($dbcohort->description, $cohort2['description']); $this->assertEquals($dbcohort->visible, $cohort2['visible']); @@ -113,7 +117,7 @@ public function test_create_cohorts() { // Call when $CFG->allowcohortthemes is disabled. set_config('allowcohortthemes', 0); $createdcohorts = core_cohort_external::create_cohorts(array($cohort4)); - $createdcohorts = external_api::clean_returnvalue(core_cohort_external::create_cohorts_returns(), $createdcohorts); + $createdcohorts = \external_api::clean_returnvalue(core_cohort_external::create_cohorts_returns(), $createdcohorts); foreach ($createdcohorts as $createdcohort) { $dbcohort = $DB->get_record('cohort', array('id' => $createdcohort['id'])); if ($createdcohort['idnumber'] == $cohort4['idnumber']) { @@ -129,7 +133,7 @@ public function test_create_cohorts() { // Call without required capability. $this->unassignUserCapability('moodle/cohort:manage', $contextid, $roleid); - $this->expectException(required_capability_exception::class); + $this->expectException(\required_capability_exception::class); $createdcohorts = core_cohort_external::create_cohorts(array($cohort3)); } @@ -161,7 +165,7 @@ public function test_delete_cohorts() { $cohort1 = self::getDataGenerator()->create_cohort(); $cohort2 = self::getDataGenerator()->create_cohort(); $this->unassignUserCapability('moodle/cohort:manage', $contextid, $roleid); - $this->expectException(required_capability_exception::class); + $this->expectException(\required_capability_exception::class); core_cohort_external::delete_cohorts(array($cohort1->id, $cohort2->id)); } @@ -185,13 +189,13 @@ public function test_get_cohorts() { $cohort1 = self::getDataGenerator()->create_cohort($cohort1); $cohort2 = self::getDataGenerator()->create_cohort(); - $context = context_system::instance(); + $context = \context_system::instance(); $roleid = $this->assignUserCapability('moodle/cohort:view', $context->id); // Call the external function. $returnedcohorts = core_cohort_external::get_cohorts(array( $cohort1->id, $cohort2->id)); - $returnedcohorts = external_api::clean_returnvalue(core_cohort_external::get_cohorts_returns(), $returnedcohorts); + $returnedcohorts = \external_api::clean_returnvalue(core_cohort_external::get_cohorts_returns(), $returnedcohorts); // Check we retrieve the good total number of enrolled cohorts + no error on capability. $this->assertEquals(2, count($returnedcohorts)); @@ -211,7 +215,7 @@ public function test_get_cohorts() { // Call the external function. $returnedcohorts = core_cohort_external::get_cohorts(array( $cohort1->id, $cohort2->id)); - $returnedcohorts = external_api::clean_returnvalue(core_cohort_external::get_cohorts_returns(), $returnedcohorts); + $returnedcohorts = \external_api::clean_returnvalue(core_cohort_external::get_cohorts_returns(), $returnedcohorts); // Check we retrieve the good total number of enrolled cohorts + no error on capability. $this->assertEquals(2, count($returnedcohorts)); @@ -220,7 +224,7 @@ public function test_get_cohorts() { set_config('allowcohortthemes', 0); $returnedcohorts = core_cohort_external::get_cohorts(array( $cohort1->id)); - $returnedcohorts = external_api::clean_returnvalue(core_cohort_external::get_cohorts_returns(), $returnedcohorts); + $returnedcohorts = \external_api::clean_returnvalue(core_cohort_external::get_cohorts_returns(), $returnedcohorts); foreach ($returnedcohorts as $enrolledcohort) { if ($enrolledcohort['idnumber'] == $cohort1->idnumber) { $this->assertNull($enrolledcohort['theme']); @@ -249,7 +253,7 @@ public function test_update_cohorts() { 'theme' => 'classic' ); - $context = context_system::instance(); + $context = \context_system::instance(); $roleid = $this->assignUserCapability('moodle/cohort:manage', $context->id); // Call the external function. @@ -287,7 +291,7 @@ public function test_update_cohorts() { // Call without required capability. $this->unassignUserCapability('moodle/cohort:manage', $context->id, $roleid); - $this->expectException(required_capability_exception::class); + $this->expectException(\required_capability_exception::class); core_cohort_external::update_cohorts(array($cohort1)); } @@ -308,7 +312,7 @@ public function test_update_cohorts_invalid_id_param() { try { core_cohort_external::update_cohorts(array($cohort1)); $this->fail('Expecting invalid_parameter_exception exception, none occured'); - } catch (invalid_parameter_exception $e1) { + } catch (\invalid_parameter_exception $e1) { $this->assertStringContainsString('Invalid external api parameter: the value is "THIS IS NOT AN ID"', $e1->debuginfo); } @@ -316,7 +320,7 @@ public function test_update_cohorts_invalid_id_param() { try { core_cohort_external::update_cohorts(array($cohort1)); $this->fail('Expecting invalid_parameter_exception exception, none occured'); - } catch (invalid_parameter_exception $e2) { + } catch (\invalid_parameter_exception $e2) { $this->assertStringContainsString('Invalid external api parameter: the value is "9.999"', $e2->debuginfo); } } @@ -335,8 +339,8 @@ public function test_update_cohorts_missing_dest() { $category2 = self::getDataGenerator()->create_category(array( 'name' => 'Test category 2' )); - $context1 = context_coursecat::instance($category1->id); - $context2 = context_coursecat::instance($category2->id); + $context1 = \context_coursecat::instance($category1->id); + $context2 = \context_coursecat::instance($category2->id); $cohort = array( 'contextid' => $context1->id, @@ -358,7 +362,7 @@ public function test_update_cohorts_missing_dest() { // Call the external function. // Should fail because we don't have permission on the dest category - $this->expectException(required_capability_exception::class); + $this->expectException(\required_capability_exception::class); core_cohort_external::update_cohorts(array($cohortupdate)); } @@ -376,8 +380,8 @@ public function test_update_cohorts_missing_src() { $category2 = self::getDataGenerator()->create_category(array( 'name' => 'Test category 2' )); - $context1 = context_coursecat::instance($category1->id); - $context2 = context_coursecat::instance($category2->id); + $context1 = \context_coursecat::instance($category1->id); + $context2 = \context_coursecat::instance($category2->id); $cohort = array( 'contextid' => $context1->id, @@ -399,7 +403,7 @@ public function test_update_cohorts_missing_src() { // Call the external function. // Should fail because we don't have permission on the src category - $this->expectException(required_capability_exception::class); + $this->expectException(\required_capability_exception::class); core_cohort_external::update_cohorts(array($cohortupdate)); } @@ -411,7 +415,7 @@ public function test_add_cohort_members() { $this->resetAfterTest(true); // Reset all changes automatically after this test. - $contextid = context_system::instance()->id; + $contextid = \context_system::instance()->id; $cohort = array( 'contextid' => $contextid, @@ -433,7 +437,7 @@ public function test_add_cohort_members() { // Call the external function. $addcohortmembers = core_cohort_external::add_cohort_members(array($cohort1)); - $addcohortmembers = external_api::clean_returnvalue(core_cohort_external::add_cohort_members_returns(), $addcohortmembers); + $addcohortmembers = \external_api::clean_returnvalue(core_cohort_external::add_cohort_members_returns(), $addcohortmembers); // Check we retrieve the good total number of created cohorts + no error on capability. $this->assertEquals(1, count($addcohortmembers)); @@ -450,7 +454,7 @@ public function test_add_cohort_members() { 'usertype' => array('type' => 'id', 'value' => '2') ); $this->unassignUserCapability('moodle/cohort:assign', $contextid, $roleid); - $this->expectException(required_capability_exception::class); + $this->expectException(\required_capability_exception::class); $addcohortmembers = core_cohort_external::add_cohort_members(array($cohort2)); } @@ -467,7 +471,7 @@ public function test_delete_cohort_members() { $cohort2 = self::getDataGenerator()->create_cohort(); $user2 = self::getDataGenerator()->create_user(); - $context = context_system::instance(); + $context = \context_system::instance(); $roleid = $this->assignUserCapability('moodle/cohort:assign', $context->id); $cohortaddmember1 = array( @@ -475,14 +479,14 @@ public function test_delete_cohort_members() { 'usertype' => array('type' => 'id', 'value' => $user1->id) ); $cohortmembers1 = core_cohort_external::add_cohort_members(array($cohortaddmember1)); - $cohortmembers1 = external_api::clean_returnvalue(core_cohort_external::add_cohort_members_returns(), $cohortmembers1); + $cohortmembers1 = \external_api::clean_returnvalue(core_cohort_external::add_cohort_members_returns(), $cohortmembers1); $cohortaddmember2 = array( 'cohorttype' => array('type' => 'id', 'value' => $cohort2->id), 'usertype' => array('type' => 'id', 'value' => $user2->id) ); $cohortmembers2 = core_cohort_external::add_cohort_members(array($cohortaddmember2)); - $cohortmembers2 = external_api::clean_returnvalue(core_cohort_external::add_cohort_members_returns(), $cohortmembers2); + $cohortmembers2 = \external_api::clean_returnvalue(core_cohort_external::add_cohort_members_returns(), $cohortmembers2); // Check we retrieve no cohorts + no error on capability. $this->assertEquals(2, $DB->count_records_select('cohort_members', ' ((cohortid = :idcohort1 AND userid = :iduser1) @@ -507,7 +511,7 @@ public function test_delete_cohort_members() { // Call without required capability. $this->unassignUserCapability('moodle/cohort:assign', $context->id, $roleid); - $this->expectException(required_capability_exception::class); + $this->expectException(\required_capability_exception::class); core_cohort_external::delete_cohort_members(array($cohortdel1, $cohortdel2)); } @@ -526,9 +530,9 @@ public function test_search_cohorts() { $category = $this->getDataGenerator()->create_category(); $othercategory = $this->getDataGenerator()->create_category(); $course = $this->getDataGenerator()->create_course(); - $syscontext = context_system::instance(); - $catcontext = context_coursecat::instance($category->id); - $coursecontext = context_course::instance($course->id); + $syscontext = \context_system::instance(); + $catcontext = \context_coursecat::instance($category->id); + $coursecontext = \context_course::instance($course->id); // Fetching default authenticated user role. $authrole = $DB->get_record('role', array('id' => $CFG->defaultuserroleid)); @@ -553,10 +557,10 @@ public function test_search_cohorts() { // Enrol user in the course. $this->getDataGenerator()->enrol_user($courseuser->id, $course->id, 'courserole'); - $syscontext = array('contextid' => context_system::instance()->id); - $catcontext = array('contextid' => context_coursecat::instance($category->id)->id); - $othercatcontext = array('contextid' => context_coursecat::instance($othercategory->id)->id); - $coursecontext = array('contextid' => context_course::instance($course->id)->id); + $syscontext = array('contextid' => \context_system::instance()->id); + $catcontext = array('contextid' => \context_coursecat::instance($category->id)->id); + $othercatcontext = array('contextid' => \context_coursecat::instance($othercategory->id)->id); + $coursecontext = array('contextid' => \context_course::instance($course->id)->id); $cohort1 = $this->getDataGenerator()->create_cohort(array_merge($syscontext, array('name' => 'Cohortsearch 1'))); $cohort2 = $this->getDataGenerator()->create_cohort(array_merge($catcontext, array('name' => 'Cohortsearch 2'))); @@ -567,7 +571,7 @@ public function test_search_cohorts() { try { $result = core_cohort_external::search_cohorts("Cohortsearch", $syscontext, 'parents'); $this->fail('Invalid permissions in system'); - } catch (required_capability_exception $e) { + } catch (\required_capability_exception $e) { // All good. } @@ -576,7 +580,7 @@ public function test_search_cohorts() { try { $result = core_cohort_external::search_cohorts("Cohortsearch", $catcontext, 'parents'); $this->fail('Invalid permissions in category'); - } catch (required_capability_exception $e) { + } catch (\required_capability_exception $e) { // All good. } @@ -618,7 +622,7 @@ public function test_search_cohorts() { try { $result = core_cohort_external::search_cohorts("Cohortsearch", $syscontext, 'invalid'); $this->fail('Invalid parameter includes'); - } catch (coding_exception $e) { + } catch (\coding_exception $e) { // All good. } } diff --git a/comment/tests/externallib_test.php b/comment/tests/externallib_test.php index ea79b8bdded1b..4465b1ec47ed9 100644 --- a/comment/tests/externallib_test.php +++ b/comment/tests/externallib_test.php @@ -14,15 +14,11 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * External comment functions unit tests - * - * @package core_comment - * @category external - * @copyright 2015 Juan Leyva - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @since Moodle 2.9 - */ +namespace core_comment; + +use comment_exception; +use core_comment_external; +use externallib_advanced_testcase; defined('MOODLE_INTERNAL') || die(); @@ -39,7 +35,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 2.9 */ -class core_comment_externallib_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * Tests set up @@ -71,7 +67,7 @@ protected function setup_course_and_users_basic() { $this->getDataGenerator()->enrol_user($teacher1->id, $course1->id, $teacherrole->id); // Create a database module instance. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course1->id; $record->name = "Mod data test"; $record->intro = "Some intro of some sort"; @@ -80,7 +76,7 @@ protected function setup_course_and_users_basic() { $module1 = $this->getDataGenerator()->create_module('data', $record); $field = data_get_field_new('text', $module1); - $fielddetail = new stdClass(); + $fielddetail = new \stdClass(); $fielddetail->name = 'Name'; $fielddetail->description = 'Some name'; @@ -125,7 +121,7 @@ public function test_get_comments() { ] ]; $result = core_comment_external::add_comments($inputdata); - $result = external_api::clean_returnvalue(core_comment_external::add_comments_returns(), $result); + $result = \external_api::clean_returnvalue(core_comment_external::add_comments_returns(), $result); $ids = array_column($result, 'id'); // Verify we can get the comments. @@ -136,7 +132,7 @@ public function test_get_comments() { $area = 'database_entry'; $page = 0; $result = core_comment_external::get_comments($contextlevel, $instanceid, $component, $itemid, $area, $page); - $result = external_api::clean_returnvalue(core_comment_external::get_comments_returns(), $result); + $result = \external_api::clean_returnvalue(core_comment_external::get_comments_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertCount(2, $result['comments']); @@ -153,7 +149,7 @@ public function test_get_comments() { // Test sort direction and pagination. $CFG->commentsperpage = 1; $result = core_comment_external::get_comments($contextlevel, $instanceid, $component, $itemid, $area, $page, 'ASC'); - $result = external_api::clean_returnvalue(core_comment_external::get_comments_returns(), $result); + $result = \external_api::clean_returnvalue(core_comment_external::get_comments_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertCount(1, $result['comments']); // Only one per page. @@ -163,7 +159,7 @@ public function test_get_comments() { // Next page. $result = core_comment_external::get_comments($contextlevel, $instanceid, $component, $itemid, $area, $page + 1, 'ASC'); - $result = external_api::clean_returnvalue(core_comment_external::get_comments_returns(), $result); + $result = \external_api::clean_returnvalue(core_comment_external::get_comments_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertCount(1, $result['comments']); @@ -239,7 +235,7 @@ public function test_add_comments_single() { 'area' => 'database_entry' ] ]); - $result = external_api::clean_returnvalue(core_comment_external::add_comments_returns(), $result); + $result = \external_api::clean_returnvalue(core_comment_external::add_comments_returns(), $result); // Verify the result contains 1 result having the correct structure. $this->assertCount(1, $result); @@ -322,7 +318,7 @@ public function test_add_comments_multiple_all_valid() { ] ]; $result = core_comment_external::add_comments($inputdata); - $result = external_api::clean_returnvalue(core_comment_external::add_comments_returns(), $result); + $result = \external_api::clean_returnvalue(core_comment_external::add_comments_returns(), $result); // Two comments should have been created. $this->assertCount(2, $result); @@ -394,14 +390,14 @@ public function test_delete_comments_own_user() { 'area' => 'database_entry' ] ]); - $result = external_api::clean_returnvalue(core_comment_external::add_comments_returns(), $result); + $result = \external_api::clean_returnvalue(core_comment_external::add_comments_returns(), $result); // Delete those comments we just created. $result = core_comment_external::delete_comments([ $result[0]['id'], $result[1]['id'] ]); - $result = external_api::clean_returnvalue(core_comment_external::delete_comments_returns(), $result); + $result = \external_api::clean_returnvalue(core_comment_external::delete_comments_returns(), $result); $this->assertEquals([], $result); } @@ -423,7 +419,7 @@ public function test_delete_comment_other_student() { 'area' => 'database_entry' ] ]); - $result = external_api::clean_returnvalue(core_comment_external::add_comments_returns(), $result); + $result = \external_api::clean_returnvalue(core_comment_external::add_comments_returns(), $result); // Now, as student 2, try to delete the comment made by student 1. Verify we can't. $this->setUser($student2); @@ -449,12 +445,12 @@ public function test_delete_comments_as_teacher() { 'area' => 'database_entry' ] ]); - $result = external_api::clean_returnvalue(core_comment_external::add_comments_returns(), $result); + $result = \external_api::clean_returnvalue(core_comment_external::add_comments_returns(), $result); // Verify teachers can delete the comment. $this->setUser($teacher1); $result = core_comment_external::delete_comments([$result[0]['id']]); - $result = external_api::clean_returnvalue(core_comment_external::delete_comments_returns(), $result); + $result = \external_api::clean_returnvalue(core_comment_external::delete_comments_returns(), $result); $this->assertEquals([], $result); } } diff --git a/competency/tests/generator_test.php b/competency/tests/generator_test.php index 633bc9481be57..d520dc147c605 100644 --- a/competency/tests/generator_test.php +++ b/competency/tests/generator_test.php @@ -14,30 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Tool LP data generator tests. - * - * @package core_competency - * @category test - * @copyright 2015 Frédéric Massart - FMCorz.net - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -use core_competency\competency; -use core_competency\competency_framework; -use core_competency\course_competency; -use core_competency\course_module_competency; -use core_competency\plan; -use core_competency\related_competency; -use core_competency\template; -use core_competency\template_cohort; -use core_competency\template_competency; -use core_competency\user_competency; -use core_competency\user_competency_plan; -use core_competency\plan_competency; -use core_competency\evidence; - -defined('MOODLE_INTERNAL') || die(); +namespace core_competency; /** * Tool LP data generator testcase. @@ -47,7 +24,7 @@ * @copyright 2015 Frédéric Massart - FMCorz.net * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class core_competency_generator_testcase extends advanced_testcase { +class generator_test extends \advanced_testcase { public function test_create_framework() { $this->resetAfterTest(true); diff --git a/completion/tests/externallib_test.php b/completion/tests/externallib_test.php index 098efb361d7da..c293e1de1e563 100644 --- a/completion/tests/externallib_test.php +++ b/completion/tests/externallib_test.php @@ -14,15 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * External completion functions unit tests - * - * @package core_completion - * @category external - * @copyright 2015 Juan Leyva - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @since Moodle 2.9 - */ +namespace core_completion; + +use core_completion_external; +use externallib_advanced_testcase; defined('MOODLE_INTERNAL') || die(); @@ -39,7 +34,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 2.9 */ -class core_completion_externallib_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * Test update_activity_completion_status_manually @@ -63,7 +58,7 @@ public function test_update_activity_completion_status_manually() { $result = core_completion_external::update_activity_completion_status_manually($data->cmid, true); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_completion_external::update_activity_completion_status_manually_returns(), $result); // Check in DB. @@ -71,14 +66,14 @@ public function test_update_activity_completion_status_manually() { array('coursemoduleid' => $data->cmid))); // Check using the API. - $completion = new completion_info($course); + $completion = new \completion_info($course); $completiondata = $completion->get_data($cm); $this->assertEquals(1, $completiondata->completionstate); $this->assertTrue($result['status']); $result = core_completion_external::update_activity_completion_status_manually($data->cmid, false); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_completion_external::update_activity_completion_status_manually_returns(), $result); $this->assertEquals(0, $DB->get_field('course_modules_completion', 'completionstate', @@ -103,7 +98,7 @@ public function test_get_activities_completion_status() { $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1, 'groupmode' => SEPARATEGROUPS, 'groupmodeforce' => 1)); - availability_completion\condition::wipe_static_cache(); + \availability_completion\condition::wipe_static_cache(); $data = $this->getDataGenerator()->create_module('data', ['course' => $course->id], @@ -152,12 +147,12 @@ public function test_get_activities_completion_status() { $this->setUser($student); // Forum complete. - $completion = new completion_info($course); + $completion = new \completion_info($course); $completion->update_state($cmforum, COMPLETION_COMPLETE); $result = core_completion_external::get_activities_completion_status($course->id, $student->id); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_completion_external::get_activities_completion_status_returns(), $result); // We added 6 activities, but only 4 with completion enabled and one of those is hidden. @@ -235,7 +230,7 @@ public function test_get_activities_completion_status() { $this->setUser($teacher); $result = core_completion_external::get_activities_completion_status($course->id, $student->id); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_completion_external::get_activities_completion_status_returns(), $result); $this->assertCount($numberofcompletions, $result['statuses']); @@ -245,7 +240,7 @@ public function test_get_activities_completion_status() { $result = core_completion_external::get_activities_completion_status($course->id, $student->id); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_completion_external::get_activities_completion_status_returns(), $result); // Check forum has been overriden by the teacher. @@ -261,12 +256,12 @@ public function test_get_activities_completion_status() { // Teacher should see his own completion status. // Forum complete for teacher. - $completion = new completion_info($course); + $completion = new \completion_info($course); $completion->update_state($cmforum, COMPLETION_COMPLETE); $result = core_completion_external::get_activities_completion_status($course->id, $teacher->id); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_completion_external::get_activities_completion_status_returns(), $result); $this->assertCount($numberofcompletions, $result['statuses']); @@ -290,14 +285,14 @@ public function test_get_activities_completion_status() { $this->assertEquals(5, $activitiesfound); // Change teacher role capabilities (disable access all groups). - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $teacherrole->id, $context); accesslib_clear_all_caches_for_unit_testing(); try { $result = core_completion_external::get_activities_completion_status($course->id, $student->id); $this->fail('Exception expected due to groups permissions.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('accessdenied', $e->errorcode); } @@ -305,7 +300,7 @@ public function test_get_activities_completion_status() { groups_add_member($group1->id, $teacher->id); $result = core_completion_external::get_activities_completion_status($course->id, $student->id); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_completion_external::get_activities_completion_status_returns(), $result); $this->assertCount($numberofcompletions, $result['statuses']); } @@ -336,34 +331,34 @@ public function test_override_activity_completion_status() { // Manually complete the data activity as the student. $this->setUser($student); - $completion = new completion_info($course); + $completion = new \completion_info($course); $completion->update_state($cmdata, COMPLETION_COMPLETE); // Test overriding the status of the manual-completion-activity 'incomplete'. $this->setUser($teacher); $result = core_completion_external::override_activity_completion_status($student->id, $data->cmid, COMPLETION_INCOMPLETE); - $result = external_api::clean_returnvalue(core_completion_external::override_activity_completion_status_returns(), $result); + $result = \external_api::clean_returnvalue(core_completion_external::override_activity_completion_status_returns(), $result); $this->assertEquals($result['state'], COMPLETION_INCOMPLETE); $completiondata = $completion->get_data($cmdata, false, $student->id); $this->assertEquals(COMPLETION_INCOMPLETE, $completiondata->completionstate); // Test overriding the status of the manual-completion-activity back to 'complete'. $result = core_completion_external::override_activity_completion_status($student->id, $data->cmid, COMPLETION_COMPLETE); - $result = external_api::clean_returnvalue(core_completion_external::override_activity_completion_status_returns(), $result); + $result = \external_api::clean_returnvalue(core_completion_external::override_activity_completion_status_returns(), $result); $this->assertEquals($result['state'], COMPLETION_COMPLETE); $completiondata = $completion->get_data($cmdata, false, $student->id); $this->assertEquals(COMPLETION_COMPLETE, $completiondata->completionstate); // Test overriding the status of the auto-completion-activity to 'complete'. $result = core_completion_external::override_activity_completion_status($student->id, $forum->cmid, COMPLETION_COMPLETE); - $result = external_api::clean_returnvalue(core_completion_external::override_activity_completion_status_returns(), $result); + $result = \external_api::clean_returnvalue(core_completion_external::override_activity_completion_status_returns(), $result); $this->assertEquals($result['state'], COMPLETION_COMPLETE); $completionforum = $completion->get_data($cmforum, false, $student->id); $this->assertEquals(COMPLETION_COMPLETE, $completionforum->completionstate); // Test overriding the status of the auto-completion-activity to 'incomplete'. $result = core_completion_external::override_activity_completion_status($student->id, $forum->cmid, COMPLETION_INCOMPLETE); - $result = external_api::clean_returnvalue(core_completion_external::override_activity_completion_status_returns(), $result); + $result = \external_api::clean_returnvalue(core_completion_external::override_activity_completion_status_returns(), $result); $this->assertEquals($result['state'], COMPLETION_INCOMPLETE); $completionforum = $completion->get_data($cmforum, false, $student->id); $this->assertEquals(COMPLETION_INCOMPLETE, $completionforum->completionstate); @@ -389,7 +384,7 @@ public function test_override_status_user_without_capability() { $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id); $teacherrole = $DB->get_record('role', ['shortname' => 'teacher']); $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id); - $coursecontext = context_course::instance($course->id); + $coursecontext = \context_course::instance($course->id); // Create an activity with automatic completion (a forum). $forum = $this->getDataGenerator()->create_module('forum', ['course' => $course->id], @@ -447,11 +442,11 @@ public function test_get_course_completion_status() { groups_add_member($group2->id, $teacher->id); // Set completion rules. - $completion = new completion_info($course); + $completion = new \completion_info($course); // Loop through each criteria type and run its update_config() method. - $criteriadata = new stdClass(); + $criteriadata = new \stdClass(); $criteriadata->id = $course->id; $criteriadata->criteria_activity = array(); // Some activities. @@ -475,12 +470,12 @@ public function test_get_course_completion_status() { 'course' => $course->id, 'criteriatype' => null ); - $aggregation = new completion_aggregation($aggdata); + $aggregation = new \completion_aggregation($aggdata); $aggregation->setMethod(COMPLETION_AGGREGATION_ALL); $aggregation->save(); $aggdata['criteriatype'] = COMPLETION_CRITERIA_TYPE_ACTIVITY; - $aggregation = new completion_aggregation($aggdata); + $aggregation = new \completion_aggregation($aggdata); $aggregation->setMethod(COMPLETION_AGGREGATION_ALL); $aggregation->save(); @@ -488,7 +483,7 @@ public function test_get_course_completion_status() { $result = core_completion_external::get_course_completion_status($course->id, $student->id); // We need to execute the return values cleaning process to simulate the web service server. - $studentresult = external_api::clean_returnvalue( + $studentresult = \external_api::clean_returnvalue( core_completion_external::get_course_completion_status_returns(), $result); // 3 different criteria. @@ -505,20 +500,20 @@ public function test_get_course_completion_status() { $this->setUser($teacher); $result = core_completion_external::get_course_completion_status($course->id, $student->id); // We need to execute the return values cleaning process to simulate the web service server. - $teacherresult = external_api::clean_returnvalue( + $teacherresult = \external_api::clean_returnvalue( core_completion_external::get_course_completion_status_returns(), $result); $this->assertEquals($studentresult, $teacherresult); // Change teacher role capabilities (disable access al goups). - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $teacherrole->id, $context); accesslib_clear_all_caches_for_unit_testing(); try { $result = core_completion_external::get_course_completion_status($course->id, $student->id); $this->fail('Exception expected due to groups permissions.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('accessdenied', $e->errorcode); } @@ -526,7 +521,7 @@ public function test_get_course_completion_status() { groups_add_member($group1->id, $teacher->id); $result = core_completion_external::get_course_completion_status($course->id, $student->id); // We need to execute the return values cleaning process to simulate the web service server. - $teacherresult = external_api::clean_returnvalue( + $teacherresult = \external_api::clean_returnvalue( core_completion_external::get_course_completion_status_returns(), $result); $this->assertEquals($studentresult, $teacherresult); @@ -552,9 +547,9 @@ public function test_mark_course_self_completed() { $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id); // Set completion rules. - $completion = new completion_info($course); + $completion = new \completion_info($course); - $criteriadata = new stdClass(); + $criteriadata = new \stdClass(); $criteriadata->id = $course->id; $criteriadata->criteria_activity = array(); @@ -569,7 +564,7 @@ public function test_mark_course_self_completed() { 'course' => $course->id, 'criteriatype' => null ); - $aggregation = new completion_aggregation($aggdata); + $aggregation = new \completion_aggregation($aggdata); $aggregation->setMethod(COMPLETION_AGGREGATION_ALL); $aggregation->save(); @@ -577,7 +572,7 @@ public function test_mark_course_self_completed() { $result = core_completion_external::mark_course_self_completed($course->id); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_completion_external::mark_course_self_completed_returns(), $result); // We expect a valid result. @@ -585,7 +580,7 @@ public function test_mark_course_self_completed() { $result = core_completion_external::get_course_completion_status($course->id, $student->id); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_completion_external::get_course_completion_status_returns(), $result); // Course must be completed. @@ -594,7 +589,7 @@ public function test_mark_course_self_completed() { try { $result = core_completion_external::mark_course_self_completed($course->id); $this->fail('Exception expected due course already self completed.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('useralreadymarkedcomplete', $e->errorcode); } diff --git a/customfield/tests/generator_test.php b/customfield/tests/generator_test.php index c5335c37d9fef..20f17b078f7b9 100644 --- a/customfield/tests/generator_test.php +++ b/customfield/tests/generator_test.php @@ -14,16 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * core_customfield test data generator test. - * - * @package core_customfield - * @category test - * @copyright 2018 Ruslan Kabalin - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -defined('MOODLE_INTERNAL') || die(); +namespace core_customfield; /** * core_customfield test data generator testcase. @@ -33,13 +24,13 @@ * @copyright 2018 Ruslan Kabalin * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class core_customfield_generator_testcase extends advanced_testcase { +class generator_test extends \advanced_testcase { /** * Get generator * @return core_customfield_generator */ - protected function get_generator(): core_customfield_generator { + protected function get_generator(): \core_customfield_generator { return $this->getDataGenerator()->get_plugin_generator('core_customfield'); } @@ -69,7 +60,7 @@ public function test_create_field() { $this->assertInstanceOf('\core_customfield\field_controller', $field); $this->assertTrue(\core_customfield\field::record_exists($field->get('id'))); - $category = core_customfield\category_controller::create($category->get('id')); + $category = \core_customfield\category_controller::create($category->get('id')); $category = \core_customfield\api::get_categories_with_fields($category->get('component'), $category->get('area'), $category->get('itemid'))[$category->get('id')]; $this->assertCount(1, $category->get_fields()); diff --git a/enrol/manual/tests/externallib_test.php b/enrol/manual/tests/externallib_test.php index 6b224dd36b1fb..91b773d50041a 100644 --- a/enrol/manual/tests/externallib_test.php +++ b/enrol/manual/tests/externallib_test.php @@ -14,6 +14,18 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +namespace enrol_manual; + +use enrol_manual_external; +use externallib_advanced_testcase; + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; + +require_once($CFG->dirroot . '/webservice/tests/helpers.php'); +require_once($CFG->dirroot . '/enrol/manual/externallib.php'); + /** * Enrol manual external PHPunit tests * @@ -23,15 +35,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 2.4 */ - -defined('MOODLE_INTERNAL') || die(); - -global $CFG; - -require_once($CFG->dirroot . '/webservice/tests/helpers.php'); -require_once($CFG->dirroot . '/enrol/manual/externallib.php'); - -class enrol_manual_externallib_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * Test get_enrolled_users @@ -49,8 +53,8 @@ public function test_enrol_users() { $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); - $context1 = context_course::instance($course1->id); - $context2 = context_course::instance($course2->id); + $context1 = \context_course::instance($course1->id); + $context2 = \context_course::instance($course2->id); $instance1 = $DB->get_record('enrol', array('courseid' => $course1->id, 'enrol' => 'manual'), '*', MUST_EXIST); $instance2 = $DB->get_record('enrol', array('courseid' => $course2->id, 'enrol' => 'manual'), '*', MUST_EXIST); @@ -83,7 +87,7 @@ public function test_enrol_users() { array('roleid' => 3, 'userid' => $user1->id, 'courseid' => $course1->id), )); $this->fail('Exception expected if not having capability to enrol'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertInstanceOf('required_capability_exception', $e); $this->assertSame('nopermissions', $e->errorcode); } @@ -96,7 +100,7 @@ public function test_enrol_users() { array('roleid' => 1, 'userid' => $user1->id, 'courseid' => $course1->id), )); $this->fail('Exception expected if not allowed to assign role.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertSame('wsusercannotassign', $e->errorcode); } $this->assertEquals(0, $DB->count_records('user_enrolments')); @@ -110,7 +114,7 @@ public function test_enrol_users() { array('roleid' => 3, 'userid' => $user1->id, 'courseid' => $course2->id), )); $this->fail('Exception expected if course does not have manual instance'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertSame('wsnoinstance', $e->errorcode); } } @@ -131,7 +135,7 @@ public function test_unenrol_user_single() { $enrol = enrol_get_plugin('manual'); // Create a course. $course = self::getDataGenerator()->create_course(); - $coursecontext = context_course::instance($course->id); + $coursecontext = \context_course::instance($course->id); $enrolinstance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', MUST_EXIST); // Set the capability for the user. $roleid = $this->assignUserCapability('enrol/manual:enrol', $coursecontext); @@ -164,7 +168,7 @@ public function test_unenrol_user_multiple() { $this->setUser($user); // Log this user in. // Create a course. $course = self::getDataGenerator()->create_course(); - $coursecontext = context_course::instance($course->id); + $coursecontext = \context_course::instance($course->id); $enrolinstance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', MUST_EXIST); // Set the capability for the user. $roleid = $this->assignUserCapability('enrol/manual:enrol', $coursecontext); @@ -203,7 +207,7 @@ public function test_unenrol_user_error_no_capability() { $this->setUser($user); // Log this user in. // Create a course. $course = self::getDataGenerator()->create_course(); - $coursecontext = context_course::instance($course->id); + $coursecontext = \context_course::instance($course->id); $enrolinstance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', MUST_EXIST); $enrol = enrol_get_plugin('manual'); // Create a student and enrol them into the course. @@ -216,8 +220,8 @@ public function test_unenrol_user_error_no_capability() { array('userid' => $student->id, 'courseid' => $course->id), )); $this->fail('Exception expected: User cannot log in to the course'); - } catch (Exception $ex) { - $this->assertTrue($ex instanceof require_login_exception); + } catch (\Exception $ex) { + $this->assertTrue($ex instanceof \require_login_exception); } // Set the capability for the course, then try again. $roleid = $this->assignUserCapability('moodle/course:view', $coursecontext); @@ -226,8 +230,8 @@ public function test_unenrol_user_error_no_capability() { array('userid' => $student->id, 'courseid' => $course->id), )); $this->fail('Exception expected: User cannot log in to the course'); - } catch (Exception $ex) { - $this->assertTrue($ex instanceof required_capability_exception); + } catch (\Exception $ex) { + $this->assertTrue($ex instanceof \required_capability_exception); } // Assign unenrol capability. $this->assignUserCapability('enrol/manual:unenrol', $coursecontext, $roleid); @@ -251,7 +255,7 @@ public function test_unenrol_user_error_not_exist() { $enrol = enrol_get_plugin('manual'); // Create a course. $course = self::getDataGenerator()->create_course(); - $coursecontext = context_course::instance($course->id); + $coursecontext = \context_course::instance($course->id); $enrolinstance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', MUST_EXIST); // Set the capability for the user. $roleid = $this->assignUserCapability('enrol/manual:enrol', $coursecontext); @@ -267,8 +271,8 @@ public function test_unenrol_user_error_not_exist() { array('userid' => $student->id + 1, 'courseid' => $course->id), )); $this->fail('Exception expected: invalid student id'); - } catch (Exception $ex) { - $this->assertTrue($ex instanceof invalid_parameter_exception); + } catch (\Exception $ex) { + $this->assertTrue($ex instanceof \invalid_parameter_exception); } $DB->delete_records('enrol', array('id' => $enrolinstance->id)); try { @@ -276,8 +280,8 @@ public function test_unenrol_user_error_not_exist() { array('userid' => $student->id + 1, 'courseid' => $course->id), )); $this->fail('Exception expected: invalid student id'); - } catch (Exception $ex) { - $this->assertTrue($ex instanceof moodle_exception); + } catch (\Exception $ex) { + $this->assertTrue($ex instanceof \moodle_exception); } } } diff --git a/enrol/self/tests/externallib_test.php b/enrol/self/tests/externallib_test.php index 662120252bd8e..21c5d6ede2643 100644 --- a/enrol/self/tests/externallib_test.php +++ b/enrol/self/tests/externallib_test.php @@ -14,14 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Self enrol external PHPunit tests - * - * @package enrol_self - * @copyright 2013 Rajesh Taneja - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @since Moodle 2.6 - */ +namespace enrol_self; + +use enrol_self_external; +use externallib_advanced_testcase; defined('MOODLE_INTERNAL') || die(); @@ -30,7 +26,15 @@ require_once($CFG->dirroot . '/webservice/tests/helpers.php'); require_once($CFG->dirroot . '/enrol/self/externallib.php'); -class enrol_self_external_testcase extends externallib_advanced_testcase { +/** + * Self enrol external PHPunit tests + * + * @package enrol_self + * @copyright 2013 Rajesh Taneja + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 2.6 + */ +class externallib_test extends externallib_advanced_testcase { /** * Test get_instance_info @@ -47,7 +51,7 @@ public function test_get_instance_info() { $studentrole = $DB->get_record('role', array('shortname'=>'student')); $this->assertNotEmpty($studentrole); - $coursedata = new stdClass(); + $coursedata = new \stdClass(); $coursedata->visible = 0; $course = self::getDataGenerator()->create_course($coursedata); @@ -72,7 +76,7 @@ public function test_get_instance_info() { $this->setAdminUser(); $instanceinfo1 = enrol_self_external::get_instance_info($instanceid1); - $instanceinfo1 = external_api::clean_returnvalue(enrol_self_external::get_instance_info_returns(), $instanceinfo1); + $instanceinfo1 = \external_api::clean_returnvalue(enrol_self_external::get_instance_info_returns(), $instanceinfo1); $this->assertEquals($instanceid1, $instanceinfo1['id']); $this->assertEquals($course->id, $instanceinfo1['courseid']); @@ -82,7 +86,7 @@ public function test_get_instance_info() { $this->assertFalse(isset($instanceinfo1['enrolpassword'])); $instanceinfo2 = enrol_self_external::get_instance_info($instanceid2); - $instanceinfo2 = external_api::clean_returnvalue(enrol_self_external::get_instance_info_returns(), $instanceinfo2); + $instanceinfo2 = \external_api::clean_returnvalue(enrol_self_external::get_instance_info_returns(), $instanceinfo2); $this->assertEquals($instanceid2, $instanceinfo2['id']); $this->assertEquals($course->id, $instanceinfo2['courseid']); $this->assertEquals('self', $instanceinfo2['type']); @@ -91,7 +95,7 @@ public function test_get_instance_info() { $this->assertFalse(isset($instanceinfo2['enrolpassword'])); $instanceinfo3 = enrol_self_external::get_instance_info($instanceid3); - $instanceinfo3 = external_api::clean_returnvalue(enrol_self_external::get_instance_info_returns(), $instanceinfo3); + $instanceinfo3 = \external_api::clean_returnvalue(enrol_self_external::get_instance_info_returns(), $instanceinfo3); $this->assertEquals($instanceid3, $instanceinfo3['id']); $this->assertEquals($course->id, $instanceinfo3['courseid']); $this->assertEquals('self', $instanceinfo3['type']); @@ -104,7 +108,7 @@ public function test_get_instance_info() { $this->setUser($user); try { enrol_self_external::get_instance_info($instanceid3); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('coursehidden', $e->errorcode); } } @@ -127,8 +131,8 @@ public function test_enrol_user() { $user3 = self::getDataGenerator()->create_user(); $user4 = self::getDataGenerator()->create_user(); - $context1 = context_course::instance($course1->id); - $context2 = context_course::instance($course2->id); + $context1 = \context_course::instance($course1->id); + $context2 = \context_course::instance($course2->id); $selfplugin = enrol_get_plugin('self'); $studentrole = $DB->get_record('role', array('shortname' => 'student')); @@ -147,7 +151,7 @@ public function test_enrol_user() { // Self enrol me. $result = enrol_self_external::enrol_user($course1->id); - $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); + $result = \external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); self::assertTrue($result['status']); self::assertEquals(1, $DB->count_records('user_enrolments', array('enrolid' => $instance1->id))); @@ -160,7 +164,7 @@ public function test_enrol_user() { // Try instance not enabled. try { enrol_self_external::enrol_user($course2->id); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { self::assertEquals('canntenrol', $e->errorcode); } @@ -169,14 +173,14 @@ public function test_enrol_user() { // Try not passing a key. $result = enrol_self_external::enrol_user($course2->id); - $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); + $result = \external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); self::assertFalse($result['status']); self::assertCount(1, $result['warnings']); self::assertEquals('4', $result['warnings'][0]['warningcode']); // Try passing an invalid key. $result = enrol_self_external::enrol_user($course2->id, 'invalidkey'); - $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); + $result = \external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); self::assertFalse($result['status']); self::assertCount(1, $result['warnings']); self::assertEquals('4', $result['warnings'][0]['warningcode']); @@ -184,14 +188,14 @@ public function test_enrol_user() { // Try passing an invalid key with hint. $selfplugin->set_config('showhint', true); $result = enrol_self_external::enrol_user($course2->id, 'invalidkey'); - $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); + $result = \external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); self::assertFalse($result['status']); self::assertCount(1, $result['warnings']); self::assertEquals('3', $result['warnings'][0]['warningcode']); // Everything correct, now. $result = enrol_self_external::enrol_user($course2->id, 'abcdef'); - $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); + $result = \external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); self::assertTrue($result['status']); self::assertEquals(1, $DB->count_records('user_enrolments', array('enrolid' => $instance2->id))); @@ -208,14 +212,14 @@ public function test_enrol_user() { self::setUser($user2); // Try passing and invalid key for group. $result = enrol_self_external::enrol_user($course2->id, 'invalidkey'); - $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); + $result = \external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); self::assertFalse($result['status']); self::assertCount(1, $result['warnings']); self::assertEquals('2', $result['warnings'][0]['warningcode']); // Now, everything ok. $result = enrol_self_external::enrol_user($course2->id, 'zyx'); - $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); + $result = \external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); self::assertTrue($result['status']); self::assertEquals(2, $DB->count_records('user_enrolments', array('enrolid' => $instance2->id))); @@ -232,20 +236,20 @@ public function test_enrol_user() { self::setUser($user3); $result = enrol_self_external::enrol_user($course2->id, 'invalidkey'); - $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); + $result = \external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); self::assertFalse($result['status']); self::assertCount(2, $result['warnings']); // Now, everything ok. $result = enrol_self_external::enrol_user($course2->id, 'zyx'); - $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); + $result = \external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); self::assertTrue($result['status']); self::assertTrue(is_enrolled($context2, $user3)); // Now test passing an instance id. self::setUser($user4); $result = enrol_self_external::enrol_user($course2->id, 'abcdef', $instance3id); - $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); + $result = \external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); self::assertTrue($result['status']); self::assertTrue(is_enrolled($context2, $user3)); self::assertCount(0, $result['warnings']); diff --git a/enrol/tests/externallib_test.php b/enrol/tests/externallib_test.php index 333a54a7ca578..45289cb53d1c3 100644 --- a/enrol/tests/externallib_test.php +++ b/enrol/tests/externallib_test.php @@ -14,6 +14,12 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +namespace core_enrol; + +use core_enrol_external; +use enrol_user_enrolment_form; +use externallib_advanced_testcase; + defined('MOODLE_INTERNAL') || die(); global $CFG; @@ -30,7 +36,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 2.4 */ -class core_enrol_externallib_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * dataProvider for test_get_enrolled_users_visibility(). @@ -256,7 +262,7 @@ public function test_get_enrolled_users_visibility($settings, $results) { // Create the course and the users. $course = $this->getDataGenerator()->create_course(array('groupmode' => $settings['coursegroupmode'])); - $coursecontext = context_course::instance($course->id); + $coursecontext = \context_course::instance($course->id); $user0 = $this->getDataGenerator()->create_user(array('username' => 'user0')); // A user without group. $user1 = $this->getDataGenerator()->create_user(array('username' => 'user1')); // User for group 1. $user2 = $this->getDataGenerator()->create_user(array('username' => 'user2')); // Two users for group 2. @@ -342,7 +348,7 @@ public function test_get_enrolled_users_visibility($settings, $results) { $enrolledusers = core_enrol_external::get_enrolled_users($course->id, $options); // We need to execute the return values cleaning process to simulate the web service server. - $enrolledusers = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_returns(), $enrolledusers); + $enrolledusers = \external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_returns(), $enrolledusers); // We are only interested in ids to check visibility. $viewed = array(); @@ -365,7 +371,7 @@ public function test_get_enrolled_users_active_suspended() { // Create the course and the users. $course = $this->getDataGenerator()->create_course(); - $coursecontext = context_course::instance($course->id); + $coursecontext = \context_course::instance($course->id); $user0 = $this->getDataGenerator()->create_user(['username' => 'user0active']); $user1 = $this->getDataGenerator()->create_user(['username' => 'user1active']); $user2 = $this->getDataGenerator()->create_user(['username' => 'user2active']); @@ -399,7 +405,7 @@ public function test_get_enrolled_users_active_suspended() { $suspendedusers = core_enrol_external::get_enrolled_users($course->id, $options); // We need to execute the return values cleaning process to simulate the web service server. - $suspendedusers = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_returns(), $suspendedusers); + $suspendedusers = \external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_returns(), $suspendedusers); $this->assertCount(2, $suspendedusers); foreach ($suspendedusers as $suspendeduser) { @@ -414,7 +420,7 @@ public function test_get_enrolled_users_active_suspended() { $activeusers = core_enrol_external::get_enrolled_users($course->id, $options); // We need to execute the return values cleaning process to simulate the web service server. - $activeusers = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_returns(), $activeusers); + $activeusers = \external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_returns(), $activeusers); $this->assertCount(4, $activeusers); foreach ($activeusers as $activeuser) { @@ -428,7 +434,7 @@ public function test_get_enrolled_users_active_suspended() { $allusers = core_enrol_external::get_enrolled_users($course->id, $options); // We need to execute the return values cleaning process to simulate the web service server. - $allusers = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_returns(), $allusers); + $allusers = \external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_returns(), $allusers); $this->assertCount(6, $allusers); // Active and suspended. Test exception is thrown. @@ -475,8 +481,8 @@ public function test_get_users_courses() { $course1 = self::getDataGenerator()->create_course($coursedata1); $course2 = self::getDataGenerator()->create_course($coursedata2); $courses = array($course1, $course2); - $contexts = array ($course1->id => context_course::instance($course1->id), - $course2->id => context_course::instance($course2->id)); + $contexts = array ($course1->id => \context_course::instance($course1->id), + $course2->id => \context_course::instance($course2->id)); $student = $this->getDataGenerator()->create_user(); $otherstudent = $this->getDataGenerator()->create_user(); @@ -496,15 +502,15 @@ public function test_get_users_courses() { // Force completion, setting at least one criteria. require_once($CFG->dirroot.'/completion/criteria/completion_criteria_self.php'); - $criteriadata = new stdClass(); + $criteriadata = new \stdClass(); $criteriadata->id = $course1->id; // Self completion. $criteriadata->criteria_self = 1; - $criterion = new completion_criteria_self(); + $criterion = new \completion_criteria_self(); $criterion->update_config($criteriadata); - $ccompletion = new completion_completion(array('course' => $course1->id, 'userid' => $student->id)); + $ccompletion = new \completion_completion(array('course' => $course1->id, 'userid' => $student->id)); $ccompletion->mark_complete(); // Set course hidden and favourited. @@ -517,7 +523,7 @@ public function test_get_users_courses() { $enrolledincourses = core_enrol_external::get_users_courses($student->id, true); // We need to execute the return values cleaning process to simulate the web service server. - $enrolledincourses = external_api::clean_returnvalue(core_enrol_external::get_users_courses_returns(), $enrolledincourses); + $enrolledincourses = \external_api::clean_returnvalue(core_enrol_external::get_users_courses_returns(), $enrolledincourses); // Check we retrieve the good total number of enrolled users. $this->assertEquals(2, count($enrolledincourses)); @@ -566,7 +572,7 @@ public function test_get_users_courses() { // Check that returnusercount works correctly. $enrolledincourses = core_enrol_external::get_users_courses($student->id, false); - $enrolledincourses = external_api::clean_returnvalue(core_enrol_external::get_users_courses_returns(), $enrolledincourses); + $enrolledincourses = \external_api::clean_returnvalue(core_enrol_external::get_users_courses_returns(), $enrolledincourses); foreach ($enrolledincourses as $courseenrol) { $this->assertFalse(isset($courseenrol['enrolledusercount'])); } @@ -575,7 +581,7 @@ public function test_get_users_courses() { $this->setAdminUser(); $enrolledincourses = core_enrol_external::get_users_courses($student->id, true); - $enrolledincourses = external_api::clean_returnvalue(core_enrol_external::get_users_courses_returns(), $enrolledincourses); + $enrolledincourses = \external_api::clean_returnvalue(core_enrol_external::get_users_courses_returns(), $enrolledincourses); $this->assertEquals(2, count($enrolledincourses)); foreach ($enrolledincourses as $courseenrol) { if ($courseenrol['id'] == $course1->id) { @@ -598,7 +604,7 @@ public function test_get_users_courses() { $this->setUser($otherstudent); $enrolledincourses = core_enrol_external::get_users_courses($student->id, true); - $enrolledincourses = external_api::clean_returnvalue(core_enrol_external::get_users_courses_returns(), $enrolledincourses); + $enrolledincourses = \external_api::clean_returnvalue(core_enrol_external::get_users_courses_returns(), $enrolledincourses); $this->assertEquals(1, count($enrolledincourses)); $this->assertEquals($timenow, $enrolledincourses[0]['lastaccess']); // I can see this, not hidden. @@ -607,7 +613,7 @@ public function test_get_users_courses() { // Change some global profile visibility fields. $CFG->hiddenuserfields = 'lastaccess'; $enrolledincourses = core_enrol_external::get_users_courses($student->id, true); - $enrolledincourses = external_api::clean_returnvalue(core_enrol_external::get_users_courses_returns(), $enrolledincourses); + $enrolledincourses = \external_api::clean_returnvalue(core_enrol_external::get_users_courses_returns(), $enrolledincourses); $this->assertEquals(0, $enrolledincourses[0]['lastaccess']); // I can't see this, hidden by global setting. } @@ -621,7 +627,7 @@ public function test_get_users_courses_can_view_participants(): void { $this->resetAfterTest(); $course = $this->getDataGenerator()->create_course(); - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $user1 = $this->getDataGenerator()->create_and_enrol($course, 'student'); $user2 = $this->getDataGenerator()->create_and_enrol($course, 'student'); @@ -709,7 +715,7 @@ public function test_get_users_courses_with_mathjax() { ]; $course = self::getDataGenerator()->create_course($coursedata); - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); // Enrol a student in the course. $student = $this->getDataGenerator()->create_user(); @@ -722,7 +728,7 @@ public function test_get_users_courses_with_mathjax() { $enrolledincourses = core_enrol_external::get_users_courses($student->id, true); // We need to execute the return values cleaning process to simulate the web service server. - $enrolledincourses = external_api::clean_returnvalue(core_enrol_external::get_users_courses_returns(), $enrolledincourses); + $enrolledincourses = \external_api::clean_returnvalue(core_enrol_external::get_users_courses_returns(), $enrolledincourses); // Check that the amount of courses is the right one. $this->assertCount(1, $enrolledincourses); @@ -760,7 +766,7 @@ public function test_get_course_enrolment_methods() { $this->assertNotEmpty($studentrole); $course1 = self::getDataGenerator()->create_course(); - $coursedata = new stdClass(); + $coursedata = new \stdClass(); $coursedata->visible = 0; $course2 = self::getDataGenerator()->create_course($coursedata); @@ -783,7 +789,7 @@ public function test_get_course_enrolment_methods() { // Check if information is returned. $enrolmentmethods = core_enrol_external::get_course_enrolment_methods($course1->id); - $enrolmentmethods = external_api::clean_returnvalue(core_enrol_external::get_course_enrolment_methods_returns(), + $enrolmentmethods = \external_api::clean_returnvalue(core_enrol_external::get_course_enrolment_methods_returns(), $enrolmentmethods); // Enrolment information is currently returned by self enrolment plugin, so count == 1. // This should be changed as we implement get_enrol_info() for other enrolment plugins. @@ -801,7 +807,7 @@ public function test_get_course_enrolment_methods() { 'customint6' => 1, 'password' => 'test')); $enrolmentmethods = core_enrol_external::get_course_enrolment_methods($course2->id); - $enrolmentmethods = external_api::clean_returnvalue(core_enrol_external::get_course_enrolment_methods_returns(), + $enrolmentmethods = \external_api::clean_returnvalue(core_enrol_external::get_course_enrolment_methods_returns(), $enrolmentmethods); $this->assertCount(1, $enrolmentmethods); @@ -816,7 +822,7 @@ public function test_get_course_enrolment_methods() { $this->setUser($user); try { core_enrol_external::get_course_enrolment_methods($course2->id); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('coursehidden', $e->errorcode); } } @@ -826,7 +832,7 @@ public function get_enrolled_users_setup($capability) { $this->resetAfterTest(true); - $return = new stdClass(); + $return = new \stdClass(); $return->course = self::getDataGenerator()->create_course(); $return->user1 = self::getDataGenerator()->create_user(); @@ -835,7 +841,7 @@ public function get_enrolled_users_setup($capability) { $this->setUser($return->user3); // Set the required capabilities by the external function. - $return->context = context_course::instance($return->course->id); + $return->context = \context_course::instance($return->course->id); $return->roleid = $this->assignUserCapability($capability, $return->context->id); $this->assignUserCapability('moodle/user:viewdetails', $return->context->id, $return->roleid); @@ -859,7 +865,7 @@ public function test_get_enrolled_users_without_parameters() { $enrolledusers = core_enrol_external::get_enrolled_users($data->course->id); // We need to execute the return values cleaning process to simulate the web service server. - $enrolledusers = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_returns(), $enrolledusers); + $enrolledusers = \external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_returns(), $enrolledusers); // Check the result set. $this->assertEquals(3, count($enrolledusers)); @@ -881,7 +887,7 @@ public function test_get_enrolled_users_with_parameters() { )); // We need to execute the return values cleaning process to simulate the web service server. - $enrolledusers = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_returns(), $enrolledusers); + $enrolledusers = \external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_returns(), $enrolledusers); // Check the result set, we should only get the 3rd result, which is $user3. $this->assertCount(1, $enrolledusers); @@ -902,7 +908,7 @@ public function test_get_enrolled_users_including_lastcourseaccess() { // Call the external function. $enrolledusers = core_enrol_external::get_enrolled_users($data->course->id); // We need to execute the return values cleaning process to simulate the web service server. - $enrolledusers = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_returns(), $enrolledusers); + $enrolledusers = \external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_returns(), $enrolledusers); // Check the result set. $this->assertEquals(3, count($enrolledusers)); @@ -921,7 +927,7 @@ public function test_get_enrolled_users_including_lastcourseaccess() { $DB->insert_record('user_lastaccess', $lastaccess); $enrolledusers = core_enrol_external::get_enrolled_users($data->course->id); - $enrolledusers = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_returns(), $enrolledusers); + $enrolledusers = \external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_returns(), $enrolledusers); // Check the result set. $this->assertEquals(3, count($enrolledusers)); @@ -940,7 +946,7 @@ public function test_get_enrolled_users_without_capability() { // Call without required capability. $this->unassignUserCapability($capability, $data->context->id, $data->roleid); - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); $categories = core_enrol_external::get_enrolled_users($data->course->id); } @@ -949,11 +955,11 @@ public function get_enrolled_users_with_capability_setup($capability) { $this->resetAfterTest(true); - $return = new stdClass(); + $return = new \stdClass(); // Create the course and fetch its context. $return->course = self::getDataGenerator()->create_course(); - $context = context_course::instance($return->course->id); + $context = \context_course::instance($return->course->id); // Create one teacher, and two students. $return->teacher = self::getDataGenerator()->create_user(); @@ -1003,7 +1009,7 @@ public function test_get_enrolled_users_with_capability_without_parameters() { ); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_with_capability_returns(), $result); + $result = \external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_with_capability_returns(), $result); // Check an array containing the expected user for the course capability is returned. $expecteduserlist = $result[0]; @@ -1036,7 +1042,7 @@ public function test_get_enrolled_users_with_capability_with_parameters () { ); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_with_capability_returns(), $result); + $result = \external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_with_capability_returns(), $result); // Check an array containing the expected user for the course capability is returned. $expecteduserlist = $result[0]['users']; @@ -1064,7 +1070,7 @@ public function test_get_enrolled_users_with_capability_including_lastcourseacce $result = core_enrol_external::get_enrolled_users_with_capability($parameters, array()); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_with_capability_returns(), $result); + $result = \external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_with_capability_returns(), $result); // Check an array containing the expected user for the course capability is returned. $expecteduserlist = $result[0]; @@ -1086,7 +1092,7 @@ public function test_get_enrolled_users_with_capability_including_lastcourseacce $result = core_enrol_external::get_enrolled_users_with_capability($parameters, array()); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_with_capability_returns(), $result); + $result = \external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_with_capability_returns(), $result); // Check the result set. $expecteduserlist = $result[0]; @@ -1099,10 +1105,10 @@ public function test_get_enrolled_users_with_capability_including_lastcourseacce * dataProvider for test_submit_user_enrolment_form(). */ public function submit_user_enrolment_form_provider() { - $now = new DateTime(); + $now = new \DateTime(); $nextmonth = clone($now); - $nextmonth->add(new DateInterval('P1M')); + $nextmonth->add(new \DateInterval('P1M')); return [ 'Invalid data' => [ @@ -1228,7 +1234,7 @@ public function test_submit_user_enrolment_form($customdata, $expectedresult, $v $querystring = http_build_query($formdata, '', '&'); - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( core_enrol_external::submit_user_enrolment_form_returns(), core_enrol_external::submit_user_enrolment_form($querystring) ); @@ -1293,13 +1299,13 @@ public function test_unenerol_user_enrolment() { // Invalid data by passing invalid ueid. $data = core_enrol_external::unenrol_user_enrolment(101010); - $data = external_api::clean_returnvalue(core_enrol_external::unenrol_user_enrolment_returns(), $data); + $data = \external_api::clean_returnvalue(core_enrol_external::unenrol_user_enrolment_returns(), $data); $this->assertFalse($data['result']); $this->assertNotEmpty($data['errors']); // Valid data. $data = core_enrol_external::unenrol_user_enrolment($ueid); - $data = external_api::clean_returnvalue(core_enrol_external::unenrol_user_enrolment_returns(), $data); + $data = \external_api::clean_returnvalue(core_enrol_external::unenrol_user_enrolment_returns(), $data); $this->assertTrue($data['result']); $this->assertEmpty($data['errors']); diff --git a/files/tests/externallib_test.php b/files/tests/externallib_test.php index a627077a824bd..9b1c7928056a9 100644 --- a/files/tests/externallib_test.php +++ b/files/tests/externallib_test.php @@ -14,6 +14,18 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +namespace core_files; + +use core_files_external; +use core_files\external\delete\draft; +use core_files\external\get\unused_draft; + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; + +require_once($CFG->dirroot . '/webservice/tests/helpers.php'); +require_once($CFG->dirroot . '/files/externallib.php'); /** * PHPunit tests for external files API. @@ -24,14 +36,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 2.6 */ -defined('MOODLE_INTERNAL') || die(); - -global $CFG; - -require_once($CFG->dirroot . '/webservice/tests/helpers.php'); -require_once($CFG->dirroot . '/files/externallib.php'); - -class core_files_externallib_testcase extends advanced_testcase { +class externallib_test extends \advanced_testcase { /* * Test core_files_external::upload(). @@ -42,7 +47,7 @@ public function test_upload() { $this->resetAfterTest(); $this->setAdminUser(); - $context = context_user::instance($USER->id); + $context = \context_user::instance($USER->id); $contextid = $context->id; $component = "user"; $filearea = "draft"; @@ -61,7 +66,7 @@ public function test_upload() { // Call the api to create a file. $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent, $contextlevel, $instanceid); - $fileinfo = external_api::clean_returnvalue(core_files_external::upload_returns(), $fileinfo); + $fileinfo = \external_api::clean_returnvalue(core_files_external::upload_returns(), $fileinfo); // Get the created draft item id. $itemid = $fileinfo['itemid']; @@ -77,7 +82,7 @@ public function test_upload() { // Call the api to create a file. $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent, $contextlevel, $instanceid); - $fileinfo = external_api::clean_returnvalue(core_files_external::upload_returns(), $fileinfo); + $fileinfo = \external_api::clean_returnvalue(core_files_external::upload_returns(), $fileinfo); $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename); $this->assertNotEmpty($file); @@ -90,7 +95,7 @@ public function test_upload() { $this->assertEmpty($file); $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent, $contextlevel, $instanceid); - $fileinfo = external_api::clean_returnvalue(core_files_external::upload_returns(), $fileinfo); + $fileinfo = \external_api::clean_returnvalue(core_files_external::upload_returns(), $fileinfo); $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename); $this->assertNotEmpty($file); @@ -108,7 +113,7 @@ public function test_upload_param_component() { $this->resetAfterTest(); $this->setAdminUser(); - $context = context_user::instance($USER->id); + $context = \context_user::instance($USER->id); $contextid = $context->id; $component = "backup"; $filearea = "draft"; @@ -133,7 +138,7 @@ public function test_upload_param_area() { $this->resetAfterTest(); $this->setAdminUser(); - $context = context_user::instance($USER->id); + $context = \context_user::instance($USER->id); $contextid = $context->id; $component = "user"; $filearea = "draft"; @@ -147,7 +152,7 @@ public function test_upload_param_area() { // Make sure the file is created. $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent, 'user', $USER->id); - $fileinfo = external_api::clean_returnvalue(core_files_external::upload_returns(), $fileinfo); + $fileinfo = \external_api::clean_returnvalue(core_files_external::upload_returns(), $fileinfo); $browser = get_file_browser(); $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename); $this->assertNotEmpty($file); @@ -167,7 +172,7 @@ public function test_get_files() { // Create a course. $course = $this->getDataGenerator()->create_course(); - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $record->name = "Mod data upload test"; $record->intro = "Some intro of some sort"; @@ -178,7 +183,7 @@ public function test_get_files() { // Create a new field in the database activity. $field = data_get_field_new('file', $module); // Add more detail about the field. - $fielddetail = new stdClass(); + $fielddetail = new \stdClass(); $fielddetail->d = $module->id; $fielddetail->mode = 'add'; $fielddetail->type = 'file'; @@ -200,8 +205,8 @@ public function test_get_files() { // Insert the information about the file. $contentid = $DB->insert_record('data_content', $datacontent); // Required information for uploading a file. - $context = context_module::instance($module->cmid); - $usercontext = context_user::instance($USER->id); + $context = \context_module::instance($module->cmid); + $usercontext = \context_user::instance($USER->id); $component = 'mod_data'; $filearea = 'content'; $itemid = $contentid; @@ -228,10 +233,10 @@ public function test_get_files() { // The first time is with a valid context ID. $filename = ''; $testfilelisting = core_files_external::get_files($context->id, $component, $filearea, $itemid, '/', $filename); - $testfilelisting = external_api::clean_returnvalue(core_files_external::get_files_returns(), $testfilelisting); + $testfilelisting = \external_api::clean_returnvalue(core_files_external::get_files_returns(), $testfilelisting); // With the information that we have provided we should get an object exactly like the one below. - $coursecontext = context_course::instance($course->id); + $coursecontext = \context_course::instance($course->id); $testdata = array(); $testdata['parents'] = array(); $testdata['parents']['0'] = array('contextid' => 1, @@ -289,7 +294,7 @@ public function test_get_files() { $contextlevel = 'module'; $instanceid = $module->cmid; $testfilelisting = core_files_external::get_files($nocontext, $component, $filearea, $itemid, '/', $filename, $modified, $contextlevel, $instanceid); - $testfilelisting = external_api::clean_returnvalue(core_files_external::get_files_returns(), $testfilelisting); + $testfilelisting = \external_api::clean_returnvalue(core_files_external::get_files_returns(), $testfilelisting); $this->assertEquals($testfilelisting, $testdata); } @@ -305,7 +310,7 @@ public function test_delete_draft_files() { // Add files to user draft area. $draftitemid = file_get_unused_draft_itemid(); - $context = context_user::instance($USER->id); + $context = \context_user::instance($USER->id); $filerecordinline = array( 'contextid' => $context->id, 'component' => 'user', @@ -325,12 +330,12 @@ public function test_delete_draft_files() { // Check two files were created (one file and one directory). $files = core_files_external::get_files($context->id, 'user', 'draft', $draftitemid, '/', ''); - $files = external_api::clean_returnvalue(core_files_external::get_files_returns(), $files); + $files = \external_api::clean_returnvalue(core_files_external::get_files_returns(), $files); $this->assertCount(2, $files['files']); // Check the folder has one file. $files = core_files_external::get_files($context->id, 'user', 'draft', $draftitemid, '/fakefolder/', ''); - $files = external_api::clean_returnvalue(core_files_external::get_files_returns(), $files); + $files = \external_api::clean_returnvalue(core_files_external::get_files_returns(), $files); $this->assertCount(1, $files['files']); // Delete a file and a folder. @@ -338,12 +343,12 @@ public function test_delete_draft_files() { ['filepath' => '/', 'filename' => 'faketxt.txt'], ['filepath' => '/fakefolder/', 'filename' => ''], ]; - $paths = core_files\external\delete\draft::execute($draftitemid, $filestodelete); - $paths = external_api::clean_returnvalue(core_files\external\delete\draft::execute_returns(), $paths); + $paths = draft::execute($draftitemid, $filestodelete); + $paths = \external_api::clean_returnvalue(draft::execute_returns(), $paths); // Check everything was deleted. $files = core_files_external::get_files($context->id, 'user', 'draft', $draftitemid, '/', ''); - $files = external_api::clean_returnvalue(core_files_external::get_files_returns(), $files); + $files = \external_api::clean_returnvalue(core_files_external::get_files_returns(), $files); $this->assertCount(0, $files['files']); } @@ -357,8 +362,8 @@ public function test_get_unused_draft_itemid() { $this->setAdminUser(); // Add files to user draft area. - $result = core_files\external\get\unused_draft::execute(); - $result = external_api::clean_returnvalue(core_files\external\get\unused_draft::execute_returns(), $result); + $result = unused_draft::execute(); + $result = \external_api::clean_returnvalue(unused_draft::execute_returns(), $result); $filerecordinline = [ 'contextid' => $result['contextid'], @@ -377,10 +382,10 @@ public function test_get_unused_draft_itemid() { $filerecordinline['filename'] = 'fakeimage.png'; $fs->create_file_from_string($filerecordinline, 'img...'); - $context = context_user::instance($USER->id); + $context = \context_user::instance($USER->id); // Check two files were created (one file and one directory). $files = core_files_external::get_files($context->id, 'user', 'draft', $result['itemid'], '/', ''); - $files = external_api::clean_returnvalue(core_files_external::get_files_returns(), $files); + $files = \external_api::clean_returnvalue(core_files_external::get_files_returns(), $files); $this->assertCount(2, $files['files']); } } diff --git a/filter/activitynames/tests/filter_test.php b/filter/activitynames/tests/filter_test.php index 98776b746099c..55875a2737a0f 100644 --- a/filter/activitynames/tests/filter_test.php +++ b/filter/activitynames/tests/filter_test.php @@ -23,10 +23,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -defined('MOODLE_INTERNAL') || die(); - -global $CFG; -require_once($CFG->dirroot . '/filter/activitynames/filter.php'); // Include the code to test. +namespace filter_activitynames; /** * Test case for the activity names auto-linking filter. @@ -34,14 +31,14 @@ * @copyright 2018 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class filter_activitynames_filter_testcase extends advanced_testcase { +class filter_test extends \advanced_testcase { public function test_links() { $this->resetAfterTest(true); // Create a test course. $course = $this->getDataGenerator()->create_course(); - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); // Create two pages that will be linked to. $page1 = $this->getDataGenerator()->create_module('page', @@ -79,7 +76,7 @@ public function test_links_activity_named_hyphen() { // Create a test course. $course = $this->getDataGenerator()->create_course(); - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); // Work around an issue with the activity names filter which maintains a static cache // of activities for current course ID. We can re-build the cache by switching user. @@ -109,8 +106,8 @@ public function test_cache() { // Create a test courses. $course1 = $this->getDataGenerator()->create_course(); $course2 = $this->getDataGenerator()->create_course(); - $context1 = context_course::instance($course1->id); - $context2 = context_course::instance($course2->id); + $context1 = \context_course::instance($course1->id); + $context2 = \context_course::instance($course2->id); // Create page 1. $page1 = $this->getDataGenerator()->create_module('page', diff --git a/filter/algebra/tests/filter_test.php b/filter/algebra/tests/filter_test.php index fbb8a7bdb709e..8834f13fefb86 100644 --- a/filter/algebra/tests/filter_test.php +++ b/filter/algebra/tests/filter_test.php @@ -23,6 +23,10 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +namespace filter_algebra; + +use filter_algebra; + defined('MOODLE_INTERNAL') || die(); global $CFG; @@ -40,13 +44,13 @@ * @copyright 2012 Tim Hunt * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class filter_algebra_testcase extends basic_testcase { +class filter_test extends \basic_testcase { protected $filter; protected function setUp(): void { parent::setUp(); - $this->filter = new filter_algebra(context_system::instance(), array()); + $this->filter = new filter_algebra(\context_system::instance(), array()); } function test_algebra_filter_no_algebra() { diff --git a/filter/data/tests/filter_test.php b/filter/data/tests/filter_test.php index 50b97f21b1e07..c9e54656805fb 100644 --- a/filter/data/tests/filter_test.php +++ b/filter/data/tests/filter_test.php @@ -23,10 +23,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -defined('MOODLE_INTERNAL') || die(); - -global $CFG; -require_once($CFG->dirroot . '/filter/data/filter.php'); +namespace filter_data; /** * Tests for filter_data. @@ -35,7 +32,7 @@ * @copyright 2015 David Monllao * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class filter_data_filter_testcase extends advanced_testcase { +class filter_test extends \advanced_testcase { /** * Tests that the filter applies the required changes. @@ -46,17 +43,17 @@ public function test_filter() { $this->resetAfterTest(true); $this->setAdminUser(); - filter_manager::reset_caches(); + \filter_manager::reset_caches(); filter_set_global_state('data', TEXTFILTER_ON); $course1 = $this->getDataGenerator()->create_course(); - $coursecontext1 = context_course::instance($course1->id); + $coursecontext1 = \context_course::instance($course1->id); $course2 = $this->getDataGenerator()->create_course(); - $coursecontext2 = context_course::instance($course2->id); + $coursecontext2 = \context_course::instance($course2->id); - $sitecontext = context_course::instance(SITEID); + $sitecontext = \context_course::instance(SITEID); $site = get_site(); $this->add_simple_database_instance($site, array('SiteEntry')); @@ -83,7 +80,7 @@ public function test_filter() { /** * Adds a database instance to the provided course + a text field + adds all attached entries. * - * @param stdClass $course + * @param \stdClass $course * @param array $entries A list of entry names. * @return void */ @@ -95,7 +92,7 @@ protected function add_simple_database_instance($course, $entries = false) { // A database field. $field = data_get_field_new('text', $database); - $fielddetail = new stdClass(); + $fielddetail = new \stdClass(); $fielddetail->d = $database->id; $fielddetail->mode = 'add'; $fielddetail->type = 'text'; diff --git a/filter/displayh5p/tests/filter_test.php b/filter/displayh5p/tests/filter_test.php index 386fad0befa5d..9609c68c3e0e6 100644 --- a/filter/displayh5p/tests/filter_test.php +++ b/filter/displayh5p/tests/filter_test.php @@ -23,6 +23,10 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +namespace filter_displayh5p; + +use filter_displayh5p; + defined('MOODLE_INTERNAL') || die(); global $CFG; @@ -34,7 +38,7 @@ * @copyright 2019 Victor Deniz * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class filter_displayh5p_testcase extends advanced_testcase { +class filter_test extends \advanced_testcase { public function setUp(): void { parent::setUp(); diff --git a/filter/emoticon/tests/filter_test.php b/filter/emoticon/tests/filter_test.php index d9bdfdfde7cb1..7046390c47800 100644 --- a/filter/emoticon/tests/filter_test.php +++ b/filter/emoticon/tests/filter_test.php @@ -23,6 +23,10 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +namespace filter_emoticon; + +use filter_emoticon; + defined('MOODLE_INTERNAL') || die(); global $CFG; @@ -31,7 +35,7 @@ /** * Skype icons filter testcase. */ -class filter_emoticon_testcase extends advanced_testcase { +class filter_test extends \advanced_testcase { /** * Test that filter ignores nolink/pre element, and processes remaining text @@ -185,7 +189,7 @@ public function test_filter_emptyemoticons() { // Empty the emoticons array. $CFG->emoticons = null; - $filter = new filter_emoticon(context_system::instance(), array('originalformat' => FORMAT_HTML)); + $filter = new filter_emoticon(\context_system::instance(), array('originalformat' => FORMAT_HTML)); $input = '(grr)'; $expected = '(grr)'; @@ -205,7 +209,7 @@ public function __construct() { parent::$emoticontexts = array(); parent::$emoticonimgs = array(); // Use this context for filtering. - $this->context = context_system::instance(); + $this->context = \context_system::instance(); // Define FORMAT_HTML as only one filtering in DB. set_config('formats', implode(',', array(FORMAT_HTML)), 'filter_emoticon'); } diff --git a/filter/glossary/tests/filter_test.php b/filter/glossary/tests/filter_test.php index a5441bbf9986b..4fae44c91d6f4 100644 --- a/filter/glossary/tests/filter_test.php +++ b/filter/glossary/tests/filter_test.php @@ -23,15 +23,12 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -defined('MOODLE_INTERNAL') || die(); - -global $CFG; -require_once($CFG->dirroot . '/filter/glossary/filter.php'); // Include the code to test. +namespace filter_glossary; /** * Test case for glossary. */ -class filter_glossary_filter_testcase extends advanced_testcase { +class filter_test extends \advanced_testcase { public function test_link_to_entry_with_alias() { global $CFG; @@ -43,7 +40,7 @@ public function test_link_to_entry_with_alias() { // Create a test course. $course = $this->getDataGenerator()->create_course(); - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); // Create a glossary. $glossary = $this->getDataGenerator()->create_module('glossary', @@ -84,7 +81,7 @@ public function test_longest_link_used() { // Create a test course. $course = $this->getDataGenerator()->create_course(); - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); // Create a glossary. $glossary = $this->getDataGenerator()->create_module('glossary', @@ -121,7 +118,7 @@ public function test_link_to_category() { // Create a test course. $course = $this->getDataGenerator()->create_course(); - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); // Create a glossary. $glossary = $this->getDataGenerator()->create_module('glossary', @@ -159,7 +156,7 @@ public function test_ampersands() { // Create a test course. $course = $this->getDataGenerator()->create_course(); - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); // Create a glossary. $glossary = $this->getDataGenerator()->create_module('glossary', @@ -205,7 +202,7 @@ public function test_brackets() { // Create a test course. $course = $this->getDataGenerator()->create_course(); - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); // Create a glossary. $glossary = $this->getDataGenerator()->create_module('glossary', @@ -249,7 +246,7 @@ public function test_exclude_excludes_link_to_entry_with_alias() { // Create a test course. $course = $this->getDataGenerator()->create_course(); - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); // Create a glossary. $glossary = $this->getDataGenerator()->create_module('glossary', @@ -288,7 +285,7 @@ public function test_exclude_does_not_exclude_categories() { // Create a test course. $course = $this->getDataGenerator()->create_course(); - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); // Create a glossary. $glossary = $this->getDataGenerator()->create_module('glossary', diff --git a/filter/mathjaxloader/tests/filter_test.php b/filter/mathjaxloader/tests/filter_test.php index 06eeb7989c3b8..3b14e7ee86123 100644 --- a/filter/mathjaxloader/tests/filter_test.php +++ b/filter/mathjaxloader/tests/filter_test.php @@ -23,6 +23,10 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +namespace filter_mathjaxloader; + +use filter_mathjaxloader; + defined('MOODLE_INTERNAL') || die(); global $CFG; @@ -34,7 +38,7 @@ * @copyright 2017 David Mudrak * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class filter_mathjaxloader_filter_testcase extends advanced_testcase { +class filter_test extends \advanced_testcase { /** * Test the functionality of {@link filter_mathjaxloader::map_language_code()}. @@ -46,7 +50,7 @@ class filter_mathjaxloader_filter_testcase extends advanced_testcase { */ public function test_map_language_code($moodlelangcode, $mathjaxlangcode) { - $filter = new filter_mathjaxloader(context_system::instance(), []); + $filter = new filter_mathjaxloader(\context_system::instance(), []); $this->assertEquals($mathjaxlangcode, $filter->map_language_code($moodlelangcode)); } diff --git a/filter/mediaplugin/tests/filter_test.php b/filter/mediaplugin/tests/filter_test.php index c7e7cc57cd170..79766e9cf9f61 100644 --- a/filter/mediaplugin/tests/filter_test.php +++ b/filter/mediaplugin/tests/filter_test.php @@ -23,13 +23,17 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +namespace filter_mediaplugin; + +use filter_mediaplugin; + defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->dirroot . '/filter/mediaplugin/filter.php'); // Include the code to test -class filter_mediaplugin_testcase extends advanced_testcase { +class filter_test extends \advanced_testcase { function test_filter_mediaplugin_link() { $this->resetAfterTest(true); diff --git a/filter/multilang/tests/filter_test.php b/filter/multilang/tests/filter_test.php index 65d8ce3771606..e00ea99af5250 100644 --- a/filter/multilang/tests/filter_test.php +++ b/filter/multilang/tests/filter_test.php @@ -23,8 +23,9 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -defined('MOODLE_INTERNAL') || die(); +namespace filter_multilang; +use filter_multilang; /** * Tests for filter_multilang. @@ -32,7 +33,7 @@ * @copyright 2019 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class filter_multilang_filter_testcase extends advanced_testcase { +class filter_test extends \advanced_testcase { public function setUp(): void { parent::setUp(); @@ -142,7 +143,7 @@ public function test_filtering($expectedoutput, $input, $targetlang, $parentlang $this->setup_parent_language($child, $parent); } - $filtered = format_text($input, FORMAT_HTML, array('context' => context_system::instance())); + $filtered = format_text($input, FORMAT_HTML, array('context' => \context_system::instance())); $this->assertEquals($expectedoutput, $filtered); } } diff --git a/filter/tex/tests/filter_test.php b/filter/tex/tests/filter_test.php index 02dc2b1b2182f..a9e1e751e4922 100644 --- a/filter/tex/tests/filter_test.php +++ b/filter/tex/tests/filter_test.php @@ -22,6 +22,10 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +namespace filter_tex; + +use filter_tex; + defined('MOODLE_INTERNAL') || die(); global $CFG; @@ -36,14 +40,14 @@ * @copyright 2014 Damyon Wiese * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class filter_tex_testcase extends advanced_testcase { +class filter_test extends \advanced_testcase { protected $filter; protected function setUp(): void { parent::setUp(); $this->resetAfterTest(true); - $this->filter = new filter_tex(context_system::instance(), array()); + $this->filter = new filter_tex(\context_system::instance(), array()); } function run_with_delimiters($start, $end, $filtershouldrun) { diff --git a/filter/urltolink/tests/filter_test.php b/filter/urltolink/tests/filter_test.php index 1dca5b7f74a2e..800453419014e 100644 --- a/filter/urltolink/tests/filter_test.php +++ b/filter/urltolink/tests/filter_test.php @@ -23,13 +23,17 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +namespace filter_urltolink; + +use filter_urltolink; + defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->dirroot . '/filter/urltolink/filter.php'); // Include the code to test -class filter_urltolink_filter_testcase extends basic_testcase { +class filter_test extends \basic_testcase { function get_convert_urls_into_links_test_cases() { // Create a 4095 and 4096 long URLs. diff --git a/grade/report/overview/tests/externallib_test.php b/grade/report/overview/tests/externallib_test.php index 592c0192307bb..72f5566e7e9eb 100644 --- a/grade/report/overview/tests/externallib_test.php +++ b/grade/report/overview/tests/externallib_test.php @@ -14,14 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Overview grade report functions unit tests - * - * @package gradereport_overview - * @category external - * @copyright 2015 Juan Leyva - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace gradereport_overview; + +use externallib_advanced_testcase; +use gradereport_overview_external; defined('MOODLE_INTERNAL') || die(); @@ -37,7 +33,7 @@ * @copyright 2015 Juan Leyva * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class gradereport_overview_externallib_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * Set up for every test @@ -91,7 +87,7 @@ public function test_get_course_grades_student() { // A user can see his own grades in both courses. $this->setUser($this->student1); $studentgrades = gradereport_overview_external::get_course_grades(); - $studentgrades = external_api::clean_returnvalue(gradereport_overview_external::get_course_grades_returns(), $studentgrades); + $studentgrades = \external_api::clean_returnvalue(gradereport_overview_external::get_course_grades_returns(), $studentgrades); $this->assertCount(0, $studentgrades['warnings']); $this->assertCount(2, $studentgrades['grades']); @@ -110,7 +106,7 @@ public function test_get_course_grades_student() { // Second student, no grade in one course. $this->setUser($this->student2); $studentgrades = gradereport_overview_external::get_course_grades(); - $studentgrades = external_api::clean_returnvalue(gradereport_overview_external::get_course_grades_returns(), $studentgrades); + $studentgrades = \external_api::clean_returnvalue(gradereport_overview_external::get_course_grades_returns(), $studentgrades); $this->assertCount(0, $studentgrades['warnings']); $this->assertCount(2, $studentgrades['grades']); @@ -136,7 +132,7 @@ public function test_get_course_grades_admin() { $this->setAdminUser(); $studentgrades = gradereport_overview_external::get_course_grades($this->student1->id); - $studentgrades = external_api::clean_returnvalue(gradereport_overview_external::get_course_grades_returns(), $studentgrades); + $studentgrades = \external_api::clean_returnvalue(gradereport_overview_external::get_course_grades_returns(), $studentgrades); $this->assertCount(0, $studentgrades['warnings']); $this->assertCount(2, $studentgrades['grades']); foreach ($studentgrades['grades'] as $grade) { @@ -150,13 +146,13 @@ public function test_get_course_grades_admin() { } $studentgrades = gradereport_overview_external::get_course_grades($this->student2->id); - $studentgrades = external_api::clean_returnvalue(gradereport_overview_external::get_course_grades_returns(), $studentgrades); + $studentgrades = \external_api::clean_returnvalue(gradereport_overview_external::get_course_grades_returns(), $studentgrades); $this->assertCount(0, $studentgrades['warnings']); $this->assertCount(2, $studentgrades['grades']); // Admins don't see grades. $studentgrades = gradereport_overview_external::get_course_grades(); - $studentgrades = external_api::clean_returnvalue(gradereport_overview_external::get_course_grades_returns(), $studentgrades); + $studentgrades = \external_api::clean_returnvalue(gradereport_overview_external::get_course_grades_returns(), $studentgrades); $this->assertCount(0, $studentgrades['warnings']); $this->assertCount(0, $studentgrades['grades']); } @@ -169,7 +165,7 @@ public function test_get_course_grades_teacher() { $this->setUser($this->teacher); $studentgrades = gradereport_overview_external::get_course_grades(); - $studentgrades = external_api::clean_returnvalue(gradereport_overview_external::get_course_grades_returns(), $studentgrades); + $studentgrades = \external_api::clean_returnvalue(gradereport_overview_external::get_course_grades_returns(), $studentgrades); $this->assertCount(0, $studentgrades['warnings']); $this->assertCount(0, $studentgrades['grades']); } @@ -196,26 +192,26 @@ public function test_view_grade_report() { $this->setUser($this->student1); $result = gradereport_overview_external::view_grade_report($this->course1->id); - $result = external_api::clean_returnvalue(gradereport_overview_external::view_grade_report_returns(), $result); + $result = \external_api::clean_returnvalue(gradereport_overview_external::view_grade_report_returns(), $result); $events = $sink->get_events(); $this->assertCount(1, $events); $event = reset($events); // Check the event details are correct. $this->assertInstanceOf('\gradereport_overview\event\grade_report_viewed', $event); - $this->assertEquals(context_course::instance($this->course1->id), $event->get_context()); + $this->assertEquals(\context_course::instance($this->course1->id), $event->get_context()); $this->assertEquals($USER->id, $event->get_data()['relateduserid']); $this->setUser($this->teacher); $result = gradereport_overview_external::view_grade_report($this->course1->id, $this->student1->id); - $result = external_api::clean_returnvalue(gradereport_overview_external::view_grade_report_returns(), $result); + $result = \external_api::clean_returnvalue(gradereport_overview_external::view_grade_report_returns(), $result); $events = $sink->get_events(); $event = reset($events); $sink->close(); // Check the event details are correct. $this->assertInstanceOf('\gradereport_overview\event\grade_report_viewed', $event); - $this->assertEquals(context_course::instance($this->course1->id), $event->get_context()); + $this->assertEquals(\context_course::instance($this->course1->id), $event->get_context()); $this->assertEquals($this->student1->id, $event->get_data()['relateduserid']); } diff --git a/grade/report/user/tests/externallib_test.php b/grade/report/user/tests/externallib_test.php index b781d42c9edc3..9928e1917e769 100644 --- a/grade/report/user/tests/externallib_test.php +++ b/grade/report/user/tests/externallib_test.php @@ -14,14 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * User grade report functions unit tests - * - * @package gradereport_user - * @category external - * @copyright 2015 Juan Leyva - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace gradereport_user; + +use externallib_advanced_testcase; +use gradereport_user_external; defined('MOODLE_INTERNAL') || die(); @@ -38,7 +34,7 @@ * @copyright 2015 Juan Leyva * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class gradereport_user_externallib_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * Loads some data to be used by the different tests @@ -62,7 +58,7 @@ private function load_data($s1grade, $s2grade) { $teacher = $this->getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id); - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $teacherrole->id, $context); accesslib_clear_all_caches_for_unit_testing(); @@ -100,7 +96,7 @@ public function test_get_grades_table_teacher() { $this->setUser($teacher); $studentgrades = gradereport_user_external::get_grades_table($course->id); - $studentgrades = external_api::clean_returnvalue(gradereport_user_external::get_grades_table_returns(), $studentgrades); + $studentgrades = \external_api::clean_returnvalue(gradereport_user_external::get_grades_table_returns(), $studentgrades); // No warnings returned. $this->assertCount(0, $studentgrades['warnings']); @@ -132,7 +128,7 @@ public function test_get_grades_table_student() { // A user can see his own grades. $this->setUser($student1); $studentgrade = gradereport_user_external::get_grades_table($course->id, $student1->id); - $studentgrade = external_api::clean_returnvalue(gradereport_user_external::get_grades_table_returns(), $studentgrade); + $studentgrade = \external_api::clean_returnvalue(gradereport_user_external::get_grades_table_returns(), $studentgrade); // No warnings returned. $this->assertTrue(count($studentgrade['warnings']) == 0); @@ -161,7 +157,7 @@ public function test_get_grades_table_permissions() { try { $studentgrade = gradereport_user_external::get_grades_table($course->id, $student1->id); $this->fail('Exception expected due to not perissions to view other user grades.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('notingroup', $e->errorcode); } } @@ -183,33 +179,33 @@ public function test_view_grade_report() { $this->setUser($student1); $result = gradereport_user_external::view_grade_report($course->id); - $result = external_api::clean_returnvalue(gradereport_user_external::view_grade_report_returns(), $result); + $result = \external_api::clean_returnvalue(gradereport_user_external::view_grade_report_returns(), $result); $events = $sink->get_events(); $this->assertCount(1, $events); $event = reset($events); // Check the event details are correct. $this->assertInstanceOf('\gradereport_user\event\grade_report_viewed', $event); - $this->assertEquals(context_course::instance($course->id), $event->get_context()); + $this->assertEquals(\context_course::instance($course->id), $event->get_context()); $this->assertEquals($USER->id, $event->get_data()['relateduserid']); $this->setUser($teacher); $result = gradereport_user_external::view_grade_report($course->id, $student1->id); - $result = external_api::clean_returnvalue(gradereport_user_external::view_grade_report_returns(), $result); + $result = \external_api::clean_returnvalue(gradereport_user_external::view_grade_report_returns(), $result); $events = $sink->get_events(); $event = reset($events); $sink->close(); // Check the event details are correct. $this->assertInstanceOf('\gradereport_user\event\grade_report_viewed', $event); - $this->assertEquals(context_course::instance($course->id), $event->get_context()); + $this->assertEquals(\context_course::instance($course->id), $event->get_context()); $this->assertEquals($student1->id, $event->get_data()['relateduserid']); $this->setUser($student2); try { $studentgrade = gradereport_user_external::view_grade_report($course->id, $student1->id); $this->fail('Exception expected due to not permissions to view other user grades.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('nopermissiontoviewgrades', $e->errorcode); } } @@ -240,7 +236,7 @@ public function test_get_grade_items_teacher() { grade_set_setting($course->id, 'report_user_showaverage', 1); $studentgrades = gradereport_user_external::get_grade_items($course->id); - $studentgrades = external_api::clean_returnvalue(gradereport_user_external::get_grade_items_returns(), $studentgrades); + $studentgrades = \external_api::clean_returnvalue(gradereport_user_external::get_grade_items_returns(), $studentgrades); // No warnings returned. $this->assertCount(0, $studentgrades['warnings']); @@ -298,13 +294,13 @@ public function test_get_grade_items_teacher() { $this->assertEquals(70, $studentgrades['usergrades'][0]['gradeitems'][1]['averageformatted']); // Now, override and lock a grade. - $gradegrade = grade_grade::fetch(['itemid' => $studentgrades['usergrades'][0]['gradeitems'][0]['id'], + $gradegrade = \grade_grade::fetch(['itemid' => $studentgrades['usergrades'][0]['gradeitems'][0]['id'], 'userid' => $studentgrades['usergrades'][0]['userid']]); $gradegrade->set_overridden(true); $gradegrade->set_locked(1); $studentgrades = gradereport_user_external::get_grade_items($course->id); - $studentgrades = external_api::clean_returnvalue(gradereport_user_external::get_grade_items_returns(), $studentgrades); + $studentgrades = \external_api::clean_returnvalue(gradereport_user_external::get_grade_items_returns(), $studentgrades); // No warnings returned. $this->assertCount(0, $studentgrades['warnings']); @@ -337,7 +333,7 @@ public function test_get_grade_items_student() { $this->setUser($student1); $studentgrades = gradereport_user_external::get_grade_items($course->id, $student1->id); - $studentgrades = external_api::clean_returnvalue(gradereport_user_external::get_grade_items_returns(), $studentgrades); + $studentgrades = \external_api::clean_returnvalue(gradereport_user_external::get_grade_items_returns(), $studentgrades); // No warnings returned. $this->assertCount(0, $studentgrades['warnings']); @@ -379,11 +375,11 @@ public function test_get_grade_items_student() { $studentgrades['usergrades'][0]['gradeitems'][0]['idnumber']); // Hide one grade for the user. - $gradegrade = new grade_grade(array('userid' => $student1->id, + $gradegrade = new \grade_grade(array('userid' => $student1->id, 'itemid' => $studentgrades['usergrades'][0]['gradeitems'][0]['id']), true); $gradegrade->set_hidden(1); $studentgrades = gradereport_user_external::get_grade_items($course->id, $student1->id); - $studentgrades = external_api::clean_returnvalue(gradereport_user_external::get_grade_items_returns(), $studentgrades); + $studentgrades = \external_api::clean_returnvalue(gradereport_user_external::get_grade_items_returns(), $studentgrades); // Check we get only the course final grade. $this->assertCount(1, $studentgrades['usergrades']); diff --git a/group/tests/externallib_test.php b/group/tests/externallib_test.php index b04853cbd3285..76ae85aaa317e 100644 --- a/group/tests/externallib_test.php +++ b/group/tests/externallib_test.php @@ -14,15 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Group external PHPunit tests - * - * @package core_group - * @category external - * @copyright 2012 Jerome Mouneyrac - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @since Moodle 2.4 - */ +namespace core_group; + +use core_group_external; +use externallib_advanced_testcase; defined('MOODLE_INTERNAL') || die(); @@ -32,7 +27,16 @@ require_once($CFG->dirroot . '/group/externallib.php'); require_once($CFG->dirroot . '/group/lib.php'); -class core_group_externallib_testcase extends externallib_advanced_testcase { +/** + * Group external PHPunit tests + * + * @package core_group + * @category external + * @copyright 2012 Jerome Mouneyrac + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 2.4 + */ +class externallib_test extends externallib_advanced_testcase { /** * Test create_groups @@ -66,7 +70,7 @@ public function test_create_groups() { $group4['description'] = 'Group Test 4 description'; // Set the required capabilities by the external function - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $roleid = $this->assignUserCapability('moodle/course:managegroups', $context->id); $this->assignUserCapability('moodle/course:view', $context->id, $roleid); @@ -74,7 +78,7 @@ public function test_create_groups() { $groups = core_group_external::create_groups(array($group1, $group2)); // We need to execute the return values cleaning process to simulate the web service server. - $groups = external_api::clean_returnvalue(core_group_external::create_groups_returns(), $groups); + $groups = \external_api::clean_returnvalue(core_group_external::create_groups_returns(), $groups); // Checks against DB values $this->assertEquals(2, count($groups)); @@ -93,7 +97,7 @@ public function test_create_groups() { $groupcourseid = $group2['courseid']; break; default: - throw new moodle_exception('unknowgroupname'); + throw new \moodle_exception('unknowgroupname'); break; } $this->assertEquals($dbgroup->description, $groupdescription); @@ -103,7 +107,7 @@ public function test_create_groups() { try { $froups = core_group_external::create_groups(array($group3)); $this->fail('Exception expected due to already existing idnumber.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertInstanceOf('moodle_exception', $e); $this->assertEquals(get_string('idnumbertaken', 'error'), $e->getMessage()); } @@ -139,7 +143,7 @@ public function test_update_groups() { $group2data['idnumber'] = 'TEST2'; // Set the required capabilities by the external function. - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $roleid = $this->assignUserCapability('moodle/course:managegroups', $context->id); $this->assignUserCapability('moodle/course:view', $context->id, $roleid); @@ -170,7 +174,7 @@ public function test_update_groups() { $groupdescription = $group2data['description']; break; default: - throw new moodle_exception('unknowngroupname'); + throw new \moodle_exception('unknowngroupname'); break; } $this->assertEquals($dbgroup->description, $groupdescription); @@ -181,7 +185,7 @@ public function test_update_groups() { try { $groups = core_group_external::update_groups(array($group1data)); $this->fail('Exception expected due to already existing idnumber.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertInstanceOf('moodle_exception', $e); $this->assertEquals(get_string('idnumbertaken', 'error'), $e->getMessage()); } @@ -218,7 +222,7 @@ public function test_get_groups() { $group2 = self::getDataGenerator()->create_group($group2data); // Set the required capabilities by the external function - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $roleid = $this->assignUserCapability('moodle/course:managegroups', $context->id); $this->assignUserCapability('moodle/course:view', $context->id, $roleid); @@ -226,7 +230,7 @@ public function test_get_groups() { $groups = core_group_external::get_groups(array($group1->id, $group2->id)); // We need to execute the return values cleaning process to simulate the web service server. - $groups = external_api::clean_returnvalue(core_group_external::get_groups_returns(), $groups); + $groups = \external_api::clean_returnvalue(core_group_external::get_groups_returns(), $groups); // Checks against DB values $this->assertEquals(2, count($groups)); @@ -245,7 +249,7 @@ public function test_get_groups() { $groupcourseid = $group2->courseid; break; default: - throw new moodle_exception('unknowgroupname'); + throw new \moodle_exception('unknowgroupname'); break; } $this->assertEquals($dbgroup->description, $groupdescription); @@ -286,7 +290,7 @@ public function test_delete_groups() { $group3 = self::getDataGenerator()->create_group($group3data); // Set the required capabilities by the external function - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $roleid = $this->assignUserCapability('moodle/course:managegroups', $context->id); $this->assignUserCapability('moodle/course:view', $context->id, $roleid); @@ -335,7 +339,7 @@ public function test_create_update_groupings() { try { $groupings = core_group_external::create_groupings(array($grouping1data)); $this->fail('Exception expected due to already existing idnumber.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertInstanceOf('moodle_exception', $e); $this->assertEquals(get_string('idnumbertaken', 'error'), $e->getMessage()); } @@ -361,7 +365,7 @@ public function test_create_update_groupings() { try { $groupings = core_group_external::update_groupings(array($grouping2data)); $this->fail('Exception expected due to already existing idnumber.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertInstanceOf('moodle_exception', $e); $this->assertEquals(get_string('idnumbertaken', 'error'), $e->getMessage()); } @@ -386,14 +390,14 @@ public function test_get_groupings() { $grouping = self::getDataGenerator()->create_grouping($groupingdata); // Set the required capabilities by the external function. - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $roleid = $this->assignUserCapability('moodle/course:managegroups', $context->id); $this->assignUserCapability('moodle/course:view', $context->id, $roleid); // Call the external function without specifying the optional parameter. $groupings = core_group_external::get_groupings(array($grouping->id)); // We need to execute the return values cleaning process to simulate the web service server. - $groupings = external_api::clean_returnvalue(core_group_external::get_groupings_returns(), $groupings); + $groupings = \external_api::clean_returnvalue(core_group_external::get_groupings_returns(), $groupings); $this->assertEquals(1, count($groupings)); @@ -417,7 +421,7 @@ public function test_get_groupings() { // Call the external function specifying that groups are returned. $groupings = core_group_external::get_groupings(array($grouping->id), true); // We need to execute the return values cleaning process to simulate the web service server. - $groupings = external_api::clean_returnvalue(core_group_external::get_groupings_returns(), $groupings); + $groupings = \external_api::clean_returnvalue(core_group_external::get_groupings_returns(), $groupings); $this->assertEquals(1, count($groupings)); $this->assertEquals(2, count($groupings[0]['groups'])); foreach ($groupings[0]['groups'] as $group) { @@ -436,7 +440,7 @@ public function test_get_groupings() { $groupcourseid = $group2->courseid; break; default: - throw new moodle_exception('unknowgroupname'); + throw new \moodle_exception('unknowgroupname'); break; } $this->assertEquals($dbgroup->description, $groupdescription); @@ -475,7 +479,7 @@ public function test_delete_groupings() { $grouping3 = self::getDataGenerator()->create_grouping($groupingdata3); // Set the required capabilities by the external function. - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $roleid = $this->assignUserCapability('moodle/course:managegroups', $context->id); $this->assignUserCapability('moodle/course:view', $context->id, $roleid); @@ -559,7 +563,7 @@ public function test_get_course_user_groups() { $this->setUser($student1); $groups = core_group_external::get_course_user_groups($course->id, $student1->id); - $groups = external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups); + $groups = \external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups); // Check that I see my groups. $this->assertCount(2, $groups['groups']); $this->assertEquals($course->id, $groups['groups'][0]['courseid']); @@ -567,7 +571,7 @@ public function test_get_course_user_groups() { // Check that I only see my groups inside the given grouping. $groups = core_group_external::get_course_user_groups($course->id, $student1->id, $grouping->id); - $groups = external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups); + $groups = \external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups); // Check that I see my groups in the grouping. $this->assertCount(1, $groups['groups']); $this->assertEquals($group1->id, $groups['groups'][0]['id']); @@ -575,13 +579,13 @@ public function test_get_course_user_groups() { // Check optional parameters (all student 1 courses and current user). $groups = core_group_external::get_course_user_groups(); - $groups = external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups); + $groups = \external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups); // Check that I see my groups in all my courses. $this->assertCount(3, $groups['groups']); $this->setUser($student2); $groups = core_group_external::get_course_user_groups($course->id, $student2->id); - $groups = external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups); + $groups = \external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups); // Check that I see my groups. $this->assertCount(1, $groups['groups']); @@ -591,17 +595,17 @@ public function test_get_course_user_groups() { $this->setUser($teacher); $groups = core_group_external::get_course_user_groups($course->id, $student1->id); - $groups = external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups); + $groups = \external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups); // Check that a teacher can see student groups in given course. $this->assertCount(2, $groups['groups']); $groups = core_group_external::get_course_user_groups($course->id, $student2->id); - $groups = external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups); + $groups = \external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups); // Check that a teacher can see student groups in given course. $this->assertCount(1, $groups['groups']); $groups = core_group_external::get_course_user_groups(0, $student1->id); - $groups = external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups); + $groups = \external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups); // Check that a teacher can see student groups in all the user courses if the teacher is enrolled in the course. $this->assertCount(2, $groups['groups']); // Teacher only see groups in first course. $this->assertCount(1, $groups['warnings']); // Enrolment warnings. @@ -610,7 +614,7 @@ public function test_get_course_user_groups() { // Enrol teacher in second course. $this->getDataGenerator()->enrol_user($teacher->id, $anothercourse->id, $teacherrole->id); $groups = core_group_external::get_course_user_groups(0, $student1->id); - $groups = external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups); + $groups = \external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups); // Check that a teacher can see student groups in all the user courses if the teacher is enrolled in the course. $this->assertCount(3, $groups['groups']); @@ -619,20 +623,20 @@ public function test_get_course_user_groups() { // Student can's see other students group. $groups = core_group_external::get_course_user_groups($course->id, $student2->id); - $groups = external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups); + $groups = \external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups); $this->assertCount(1, $groups['warnings']); $this->assertEquals('cannotmanagegroups', $groups['warnings'][0]['warningcode']); // Not enrolled course. $groups = core_group_external::get_course_user_groups($emptycourse->id, $student2->id); - $groups = external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups); + $groups = \external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups); $this->assertCount(1, $groups['warnings']); $this->assertEquals('1', $groups['warnings'][0]['warningcode']); $this->setUser($teacher); // Check user checking not enrolled in given course. $groups = core_group_external::get_course_user_groups($emptycourse->id, $student1->id); - $groups = external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups); + $groups = \external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups); $this->assertCount(1, $groups['warnings']); $this->assertEquals('notenrolled', $groups['warnings'][0]['warningcode']); } @@ -689,19 +693,19 @@ public function test_get_activity_allowed_groups() { // First try possible errors. try { $data = core_group_external::get_activity_allowed_groups($cm2->id); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } try { $data = core_group_external::get_activity_allowed_groups($cm3->id); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } // Retrieve my groups. $groups = core_group_external::get_activity_allowed_groups($cm1->id); - $groups = external_api::clean_returnvalue(core_group_external::get_activity_allowed_groups_returns(), $groups); + $groups = \external_api::clean_returnvalue(core_group_external::get_activity_allowed_groups_returns(), $groups); $this->assertCount(2, $groups['groups']); $this->assertFalse($groups['canaccessallgroups']); @@ -718,20 +722,20 @@ public function test_get_activity_allowed_groups() { $this->setUser($teacher); // Retrieve other users groups. $groups = core_group_external::get_activity_allowed_groups($cm1->id, $student->id); - $groups = external_api::clean_returnvalue(core_group_external::get_activity_allowed_groups_returns(), $groups); + $groups = \external_api::clean_returnvalue(core_group_external::get_activity_allowed_groups_returns(), $groups); $this->assertCount(2, $groups['groups']); // We are checking the $student passed as parameter so this will return false. $this->assertFalse($groups['canaccessallgroups']); // Check warnings. Trying to get groups for a user not enrolled in course. $groups = core_group_external::get_activity_allowed_groups($cm1->id, $otherstudent->id); - $groups = external_api::clean_returnvalue(core_group_external::get_activity_allowed_groups_returns(), $groups); + $groups = \external_api::clean_returnvalue(core_group_external::get_activity_allowed_groups_returns(), $groups); $this->assertCount(1, $groups['warnings']); $this->assertFalse($groups['canaccessallgroups']); // Checking teacher groups. $groups = core_group_external::get_activity_allowed_groups($cm1->id); - $groups = external_api::clean_returnvalue(core_group_external::get_activity_allowed_groups_returns(), $groups); + $groups = \external_api::clean_returnvalue(core_group_external::get_activity_allowed_groups_returns(), $groups); $this->assertCount(2, $groups['groups']); // Teachers by default can access all groups. $this->assertTrue($groups['canaccessallgroups']); @@ -766,18 +770,18 @@ public function test_get_activity_groupmode() { $this->setUser($student); $data = core_group_external::get_activity_groupmode($cm1->id); - $data = external_api::clean_returnvalue(core_group_external::get_activity_groupmode_returns(), $data); + $data = \external_api::clean_returnvalue(core_group_external::get_activity_groupmode_returns(), $data); $this->assertEquals(VISIBLEGROUPS, $data['groupmode']); try { $data = core_group_external::get_activity_groupmode($cm2->id); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } try { $data = core_group_external::get_activity_groupmode($cm3->id); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } @@ -814,7 +818,7 @@ public function test_add_group_members() { $this->assertEquals(0, $memberstotal); // Set the required capabilities by the external function. - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $roleid = $this->assignUserCapability('moodle/course:managegroups', $context->id); $this->assignUserCapability('moodle/course:view', $context->id, $roleid); @@ -877,7 +881,7 @@ public function test_delete_group_members() { $this->assertEquals(3, $memberstotal); // Set the required capabilities by the external function. - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $roleid = $this->assignUserCapability('moodle/course:managegroups', $context->id); $this->assignUserCapability('moodle/course:view', $context->id, $roleid); diff --git a/lib/testing/tests/generator_test.php b/lib/testing/tests/testing_generator_test.php similarity index 95% rename from lib/testing/tests/generator_test.php rename to lib/testing/tests/testing_generator_test.php index a48e4c6cd9080..9402d05d9e065 100644 --- a/lib/testing/tests/generator_test.php +++ b/lib/testing/tests/testing_generator_test.php @@ -14,17 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Data generators tests - * - * @package core - * @category phpunit - * @copyright 2012 Petr Skoda {@link http://skodak.org} - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -defined('MOODLE_INTERNAL') || die(); - +namespace core; /** * Test data generator @@ -34,7 +24,7 @@ * @copyright 2012 Petr Skoda {@link http://skodak.org} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class core_test_generator_testcase extends advanced_testcase { +class testing_generator_test extends \advanced_testcase { public function test_get_plugin_generator_good_case() { $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); $this->assertInstanceOf('core_question_generator', $generator); @@ -56,7 +46,7 @@ public function test_get_default_generator() { * Test plugin generator, with no component directory. */ public function test_get_plugin_generator_no_component_dir() { - $this->expectException(coding_exception::class); + $this->expectException(\coding_exception::class); $this->expectExceptionMessage('Component core_completion does not support generators yet. Missing tests/generator/lib.php.'); $generator = $this->getDataGenerator()->get_plugin_generator('core_completion'); } @@ -72,8 +62,8 @@ public function test_create_user() { $this->setCurrentTimeStart(); $user = $generator->create_user(); $this->assertEquals($count + 1, $DB->count_records('user')); - $this->assertSame($user->username, core_user::clean_field($user->username, 'username')); - $this->assertSame($user->email, core_user::clean_field($user->email, 'email')); + $this->assertSame($user->username, \core_user::clean_field($user->username, 'username')); + $this->assertSame($user->email, \core_user::clean_field($user->email, 'email')); $this->assertNotEmpty($user->firstnamephonetic); $this->assertNotEmpty($user->lastnamephonetic); $this->assertNotEmpty($user->alternatename); @@ -155,7 +145,7 @@ public function test_create() { $count = $DB->count_records('cohort'); $cohort = $generator->create_cohort(); $this->assertEquals($count+1, $DB->count_records('cohort')); - $this->assertEquals(context_system::instance()->id, $cohort->contextid); + $this->assertEquals(\context_system::instance()->id, $cohort->contextid); $this->assertMatchesRegularExpression('/^Cohort \d/', $cohort->name); $this->assertSame('', $cohort->idnumber); $this->assertMatchesRegularExpression("/^Description for '{$cohort->name}' \\n/", $cohort->description); @@ -180,7 +170,7 @@ public function test_create() { $this->assertEquals($course->id, $section->course); $course = $generator->create_course(array('tags' => 'Cat, Dog')); - $this->assertEquals(array('Cat', 'Dog'), array_values(core_tag_tag::get_item_tags_array('core', 'course', $course->id))); + $this->assertEquals(array('Cat', 'Dog'), array_values(\core_tag_tag::get_item_tags_array('core', 'course', $course->id))); $scale = $generator->create_scale(); $this->assertNotEmpty($scale); @@ -210,7 +200,7 @@ public function test_create_module() { $page = $generator->create_module('page', array('course' => $SITE->id, 'tags' => 'Cat, Dog')); $this->assertEquals(array('Cat', 'Dog'), - array_values(core_tag_tag::get_item_tags_array('core', 'course_modules', $page->cmid))); + array_values(\core_tag_tag::get_item_tags_array('core', 'course_modules', $page->cmid))); // Prepare environment to generate modules with all possible options. @@ -226,7 +216,7 @@ public function test_create_module() { $course = $generator->create_course(array('enablecompletion' => true)); // Create new grading category in this course. - $grade_category = new grade_category(); + $grade_category = new \grade_category(); $grade_category->courseid = $course->id; $grade_category->fullname = 'Grade category'; $grade_category->insert(); @@ -295,7 +285,7 @@ public function test_create_module() { $featuregrade); // We need id of the grading item for the second module to create availability dependency in the 3rd module. - $gradingitem = grade_item::fetch(array('courseid'=>$course->id, 'itemtype'=>'mod', 'itemmodule' => 'assign', 'iteminstance' => $m3->id)); + $gradingitem = \grade_item::fetch(array('courseid'=>$course->id, 'itemtype'=>'mod', 'itemmodule' => 'assign', 'iteminstance' => $m3->id)); // Now prepare option to create the 4th module with an availability condition. $optionsavailability = array( @@ -335,7 +325,7 @@ public function test_create_module() { $this->assertEquals($featurecompletionautomatic['completionview'], $cm3->completionview); $this->assertEquals($featurecompletionautomatic['completionpassgrade'], $cm3->completionpassgrade); $this->assertEquals(0, $cm3->completiongradeitemnumber); // Zero instead of default null since 'completionusegrade' was set. - $gradingitem = grade_item::fetch(array('courseid'=>$course->id, 'itemtype'=>'mod', 'itemmodule' => 'assign', 'iteminstance' => $m3->id)); + $gradingitem = \grade_item::fetch(array('courseid'=>$course->id, 'itemtype'=>'mod', 'itemmodule' => 'assign', 'iteminstance' => $m3->id)); $this->assertEquals(0, $gradingitem->grademin); $this->assertEquals($featuregrade['grade'], $gradingitem->grademax); $this->assertEquals($featuregrade['gradecat'], $gradingitem->categoryid); @@ -379,9 +369,9 @@ public function test_enrol_user() { $course2 = $this->getDataGenerator()->create_course(); $course3 = $this->getDataGenerator()->create_course(); - $context1 = context_course::instance($course1->id); - $context2 = context_course::instance($course2->id); - $context3 = context_course::instance($course3->id); + $context1 = \context_course::instance($course1->id); + $context2 = \context_course::instance($course2->id); + $context3 = \context_course::instance($course3->id); $user1 = $this->getDataGenerator()->create_user(); $user2 = $this->getDataGenerator()->create_user(); diff --git a/lib/tests/externallib_test.php b/lib/tests/externallib_test.php index 3803427c8a1c2..68fb84280d9a8 100644 --- a/lib/tests/externallib_test.php +++ b/lib/tests/externallib_test.php @@ -14,6 +14,13 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +namespace core; + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->libdir . '/externallib.php'); + /** * Unit tests for /lib/externallib.php. * @@ -22,14 +29,7 @@ * @copyright 2009 Petr Skoda {@link http://skodak.org} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ - -defined('MOODLE_INTERNAL') || die(); - -global $CFG; -require_once($CFG->libdir . '/externallib.php'); - - -class core_externallib_testcase extends advanced_testcase { +class externallib_test extends \advanced_testcase { protected $DB; public function setUp(): void { @@ -54,7 +54,7 @@ public function test_external_settings() { $currentfile = $settings->get_file(); $currentfileurl = $settings->get_fileurl(); - $this->assertInstanceOf('external_settings', $settings); + $this->assertInstanceOf(\external_settings::class, $settings); // Check apis. $settings->set_file('plugin.php'); @@ -75,9 +75,9 @@ public function test_external_settings() { public function test_validate_params() { $params = array('text'=>'aaa', 'someid'=>'6'); - $description = new external_function_parameters(array('someid' => new external_value(PARAM_INT, 'Some int value'), - 'text' => new external_value(PARAM_ALPHA, 'Some text value'))); - $result = external_api::validate_parameters($description, $params); + $description = new \external_function_parameters(array('someid' => new \external_value(PARAM_INT, 'Some int value'), + 'text' => new \external_value(PARAM_ALPHA, 'Some text value'))); + $result = \external_api::validate_parameters($description, $params); $this->assertCount(2, $result); reset($result); $this->assertSame('someid', key($result)); @@ -85,9 +85,9 @@ public function test_validate_params() { $this->assertSame('aaa', $result['text']); $params = array('someids'=>array('1', 2, 'a'=>'3'), 'scalar'=>666); - $description = new external_function_parameters(array('someids' => new external_multiple_structure(new external_value(PARAM_INT, 'Some ID')), - 'scalar' => new external_value(PARAM_ALPHANUM, 'Some text value'))); - $result = external_api::validate_parameters($description, $params); + $description = new \external_function_parameters(array('someids' => new \external_multiple_structure(new \external_value(PARAM_INT, 'Some ID')), + 'scalar' => new \external_value(PARAM_ALPHANUM, 'Some text value'))); + $result = \external_api::validate_parameters($description, $params); $this->assertCount(2, $result); reset($result); $this->assertSame('someids', key($result)); @@ -95,9 +95,9 @@ public function test_validate_params() { $this->assertSame('666', $result['scalar']); $params = array('text'=>'aaa'); - $description = new external_function_parameters(array('someid' => new external_value(PARAM_INT, 'Some int value', false), - 'text' => new external_value(PARAM_ALPHA, 'Some text value'))); - $result = external_api::validate_parameters($description, $params); + $description = new \external_function_parameters(array('someid' => new \external_value(PARAM_INT, 'Some int value', false), + 'text' => new \external_value(PARAM_ALPHA, 'Some text value'))); + $result = \external_api::validate_parameters($description, $params); $this->assertCount(2, $result); reset($result); $this->assertSame('someid', key($result)); @@ -105,9 +105,9 @@ public function test_validate_params() { $this->assertSame('aaa', $result['text']); $params = array('text'=>'aaa'); - $description = new external_function_parameters(array('someid' => new external_value(PARAM_INT, 'Some int value', false, 6), - 'text' => new external_value(PARAM_ALPHA, 'Some text value'))); - $result = external_api::validate_parameters($description, $params); + $description = new \external_function_parameters(array('someid' => new \external_value(PARAM_INT, 'Some int value', false, 6), + 'text' => new \external_value(PARAM_ALPHA, 'Some text value'))); + $result = \external_api::validate_parameters($description, $params); $this->assertCount(2, $result); reset($result); $this->assertSame('someid', key($result)); @@ -116,14 +116,14 @@ public function test_validate_params() { } public function test_external_format_text() { - $settings = external_settings::get_instance(); + $settings = \external_settings::get_instance(); $currentraw = $settings->get_raw(); $currentfilter = $settings->get_filter(); $settings->set_raw(true); $settings->set_filter(false); - $context = context_system::instance(); + $context = \context_system::instance(); $test = '$$ \pi $$'; $testformat = FORMAT_MARKDOWN; @@ -163,7 +163,7 @@ public function test_external_format_text() { $test = '

Text

'; $testformat = FORMAT_HTML; $correct = array('

Text

', FORMAT_HTML); - $options = new StdClass(); + $options = new \stdClass(); $options->allowid = false; // Function external_format_text should work with context id or context instance. $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct); @@ -172,7 +172,7 @@ public function test_external_format_text() { $test = '

Text

'."\n".'Newline'; $testformat = FORMAT_MOODLE; $correct = array('

Text

Newline', FORMAT_HTML); - $options = new StdClass(); + $options = new \stdClass(); $options->newlines = false; // Function external_format_text should work with context id or context instance. $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct); @@ -181,7 +181,7 @@ public function test_external_format_text() { $test = '

Text

'; $testformat = FORMAT_MOODLE; $correct = array('
'.$test.'
', FORMAT_HTML); - $options = new StdClass(); + $options = new \stdClass(); $options->para = true; // Function external_format_text should work with context id or context instance. $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct); @@ -190,7 +190,7 @@ public function test_external_format_text() { $test = '

Text

'; $testformat = FORMAT_MOODLE; $correct = array($test, FORMAT_HTML); - $options = new StdClass(); + $options = new \stdClass(); $options->context = $context; // Function external_format_text should work with context id or context instance. $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct); @@ -202,19 +202,19 @@ public function test_external_format_text() { public function test_external_format_string() { $this->resetAfterTest(); - $settings = external_settings::get_instance(); + $settings = \external_settings::get_instance(); $currentraw = $settings->get_raw(); $currentfilter = $settings->get_filter(); // Enable multilang filter to on content and heading. filter_set_global_state('multilang', TEXTFILTER_ON); filter_set_applies_to_strings('multilang', 1); - $filtermanager = filter_manager::instance(); + $filtermanager = \filter_manager::instance(); $filtermanager->reset_caches(); $settings->set_raw(true); $settings->set_filter(true); - $context = context_system::instance(); + $context = \context_system::instance(); $test = 'ENFR ' . '

there

!'; @@ -261,18 +261,18 @@ public function test_external_format_string() { */ public function test_clean_returnvalue_return_php_type() { - $returndesc = new external_single_structure( + $returndesc = new \external_single_structure( array( - 'value' => new external_value(PARAM_RAW, 'Some text', VALUE_OPTIONAL, null, NULL_NOT_ALLOWED) + 'value' => new \external_value(PARAM_RAW, 'Some text', VALUE_OPTIONAL, null, NULL_NOT_ALLOWED) ) ); // Check return type on exception because the external values does not allow NULL values. $testdata = array('value' => null); try { - $cleanedvalue = external_api::clean_returnvalue($returndesc, $testdata); - } catch (moodle_exception $e) { - $this->assertInstanceOf('invalid_response_exception', $e); + $cleanedvalue = \external_api::clean_returnvalue($returndesc, $testdata); + } catch (\moodle_exception $e) { + $this->assertInstanceOf(\invalid_response_exception::class, $e); $this->assertStringContainsString('of PHP type "NULL"', $e->debuginfo); } } @@ -283,32 +283,32 @@ public function test_clean_returnvalue_return_php_type() { public function test_clean_returnvalue() { // Build some return value decription. - $returndesc = new external_multiple_structure( - new external_single_structure( + $returndesc = new \external_multiple_structure( + new \external_single_structure( array( - 'object' => new external_single_structure( - array('value1' => new external_value(PARAM_INT, 'this is a int'))), - 'value2' => new external_value(PARAM_TEXT, 'some text', VALUE_OPTIONAL)) + 'object' => new \external_single_structure( + array('value1' => new \external_value(PARAM_INT, 'this is a int'))), + 'value2' => new \external_value(PARAM_TEXT, 'some text', VALUE_OPTIONAL)) )); // Clean an object (it should be cast into an array). - $object = new stdClass(); + $object = new \stdClass(); $object->value1 = 1; $singlestructure['object'] = $object; $singlestructure['value2'] = 'Some text'; $testdata = array($singlestructure); - $cleanedvalue = external_api::clean_returnvalue($returndesc, $testdata); + $cleanedvalue = \external_api::clean_returnvalue($returndesc, $testdata); $cleanedsinglestructure = array_pop($cleanedvalue); $this->assertSame($object->value1, $cleanedsinglestructure['object']['value1']); $this->assertSame($singlestructure['value2'], $cleanedsinglestructure['value2']); // Missing VALUE_OPTIONAL. - $object = new stdClass(); + $object = new \stdClass(); $object->value1 = 1; - $singlestructure = new stdClass(); + $singlestructure = new \stdClass(); $singlestructure->object = $object; $testdata = array($singlestructure); - $cleanedvalue = external_api::clean_returnvalue($returndesc, $testdata); + $cleanedvalue = \external_api::clean_returnvalue($returndesc, $testdata); $cleanedsinglestructure = array_pop($cleanedvalue); $this->assertSame($object->value1, $cleanedsinglestructure['object']['value1']); $this->assertArrayNotHasKey('value2', $cleanedsinglestructure); @@ -321,7 +321,7 @@ public function test_clean_returnvalue() { $singlestructure['value2'] = 'Some text'; $singlestructure['unknownvalue'] = 'Some text to ignore'; $testdata = array($singlestructure); - $cleanedvalue = external_api::clean_returnvalue($returndesc, $testdata); + $cleanedvalue = \external_api::clean_returnvalue($returndesc, $testdata); $cleanedsinglestructure = array_pop($cleanedvalue); $this->assertSame($object['value1'], $cleanedsinglestructure['object']['value1']); $this->assertSame($singlestructure['value2'], $cleanedsinglestructure['value2']); @@ -334,15 +334,15 @@ public function test_clean_returnvalue() { $singlestructure['value2'] = 'Some text'; $testdata = array($singlestructure); $this->expectException('invalid_response_exception'); - $cleanedvalue = external_api::clean_returnvalue($returndesc, $testdata); + $cleanedvalue = \external_api::clean_returnvalue($returndesc, $testdata); } /* - * Test external_api::get_context_from_params(). + * Test \external_api::get_context_from_params(). */ public function test_get_context_from_params() { $this->resetAfterTest(true); $course = $this->getDataGenerator()->create_course(); - $realcontext = context_course::instance($course->id); + $realcontext = \context_course::instance($course->id); // Use context id. $fetchedcontext = test_exernal_api::get_context_wrapper(array("contextid" => $realcontext->id)); @@ -356,26 +356,26 @@ public function test_get_context_from_params() { try { $fetchedcontext = test_exernal_api::get_context_wrapper(array("contextid" => 0)); $this->fail('Exception expected from get_context_wrapper()'); - } catch (moodle_exception $e) { - $this->assertInstanceOf('invalid_parameter_exception', $e); + } catch (\moodle_exception $e) { + $this->assertInstanceOf(\invalid_parameter_exception::class, $e); } try { $fetchedcontext = test_exernal_api::get_context_wrapper(array("instanceid" => 0)); $this->fail('Exception expected from get_context_wrapper()'); - } catch (moodle_exception $e) { - $this->assertInstanceOf('invalid_parameter_exception', $e); + } catch (\moodle_exception $e) { + $this->assertInstanceOf(\invalid_parameter_exception::class, $e); } try { $fetchedcontext = test_exernal_api::get_context_wrapper(array("contextid" => null)); $this->fail('Exception expected from get_context_wrapper()'); - } catch (moodle_exception $e) { - $this->assertInstanceOf('invalid_parameter_exception', $e); + } catch (\moodle_exception $e) { + $this->assertInstanceOf(\invalid_parameter_exception::class, $e); } // Tests for context with instanceid equal to 0 (System context). - $realcontext = context_system::instance(); + $realcontext = \context_system::instance(); $fetchedcontext = test_exernal_api::get_context_wrapper(array("contextlevel" => "system", "instanceid" => 0)); $this->assertEquals($realcontext, $fetchedcontext); @@ -385,7 +385,7 @@ public function test_get_context_from_params() { } /* - * Test external_api::get_context()_from_params parameter validation. + * Test \external_api::get_context()_from_params parameter validation. */ public function test_get_context_params() { global $USER; @@ -396,7 +396,7 @@ public function test_get_context_params() { } /* - * Test external_api::get_context()_from_params parameter validation. + * Test \external_api::get_context()_from_params parameter validation. */ public function test_get_context_params2() { global $USER; @@ -407,7 +407,7 @@ public function test_get_context_params2() { } /* - * Test external_api::get_context()_from_params parameter validation. + * Test \external_api::get_context()_from_params parameter validation. */ public function test_get_context_params3() { global $USER; @@ -437,14 +437,14 @@ public function all_external_info_provider() { * @dataProvider all_external_info_provider */ public function test_all_external_info($f) { - $desc = external_api::external_function_info($f); + $desc = \external_api::external_function_info($f); $this->assertNotEmpty($desc->name); $this->assertNotEmpty($desc->classname); $this->assertNotEmpty($desc->methodname); $this->assertEquals($desc->component, clean_param($desc->component, PARAM_COMPONENT)); - $this->assertInstanceOf('external_function_parameters', $desc->parameters_desc); + $this->assertInstanceOf(\external_function_parameters::class, $desc->parameters_desc); if ($desc->returns_desc != null) { - $this->assertInstanceOf('external_description', $desc->returns_desc); + $this->assertInstanceOf(\external_description::class, $desc->returns_desc); } } @@ -459,7 +459,7 @@ public function test_validate_courses() { $courseids = array($c1->id, $c2->id, $c3->id); $this->setAdminUser(); - list($courses, $warnings) = external_util::validate_courses($courseids); + list($courses, $warnings) = \external_util::validate_courses($courseids); $this->assertEmpty($warnings); $this->assertCount(3, $courses); $this->assertArrayHasKey($c1->id, $courses); @@ -470,7 +470,7 @@ public function test_validate_courses() { $this->assertEquals($c3->id, $courses[$c3->id]->id); $this->setUser($u1); - list($courses, $warnings) = external_util::validate_courses($courseids); + list($courses, $warnings) = \external_util::validate_courses($courseids); $this->assertCount(2, $warnings); $this->assertEquals($c2->id, $warnings[0]['itemid']); $this->assertEquals($c3->id, $warnings[1]['itemid']); @@ -495,7 +495,7 @@ public function test_validate_courses_keepfails() { $courseids = array($c1->id, $c2->id, $c3->id); $this->setUser($u1); - list($courses, $warnings) = external_util::validate_courses($courseids, [], false, true); + list($courses, $warnings) = \external_util::validate_courses($courseids, [], false, true); $this->assertCount(2, $warnings); $this->assertEquals($c2->id, $warnings[0]['itemid']); $this->assertEquals($c3->id, $warnings[1]['itemid']); @@ -523,7 +523,7 @@ public function test_validate_courses_prefetch() { $courses = array($c2->id => $c2, $c3->id => $c3, $c4->id => $c4); $this->setUser($u1); - list($courses, $warnings) = external_util::validate_courses($courseids, $courses); + list($courses, $warnings) = \external_util::validate_courses($courseids, $courses); $this->assertCount(2, $courses); $this->assertCount(1, $warnings); $this->assertArrayHasKey($c1->id, $courses); @@ -544,7 +544,7 @@ public function test_call_external_function() { $this->setAdminUser(); $category = $this->getDataGenerator()->create_category(); $params = array( - 'contextid' => context_coursecat::instance($category->id)->id, + 'contextid' => \context_coursecat::instance($category->id)->id, 'name' => 'aaagrrryyy', 'idnumber' => '', 'description' => '' @@ -555,7 +555,7 @@ public function test_call_external_function() { $beforepage = $PAGE; $beforecourse = $COURSE; $params = array('cohortids' => array($cohort1->id, $cohort2->id)); - $result = external_api::call_external_function('core_cohort_get_cohorts', $params); + $result = \external_api::call_external_function('core_cohort_get_cohorts', $params); $this->assertSame($beforepage, $PAGE); $this->assertSame($beforecourse, $COURSE); @@ -566,7 +566,7 @@ public function test_call_external_function() { $beforepage = $PAGE; $beforecourse = $COURSE; $params = array('courseid' => $course->id, 'options' => array()); - $result = external_api::call_external_function('core_enrol_get_enrolled_users', $params); + $result = \external_api::call_external_function('core_enrol_get_enrolled_users', $params); $this->assertSame($beforepage, $PAGE); $this->assertSame($beforecourse, $COURSE); @@ -575,7 +575,7 @@ public function test_call_external_function() { require_once($CFG->dirroot . '/lib/tests/fixtures/test_external_function_throwable.php'); // Call our test function. - $result = test_external_function_throwable::call_external_function('core_throw_exception', array(), false); + $result = \test_external_function_throwable::call_external_function('core_throw_exception', array(), false); $this->assertTrue($result['error']); $this->assertArrayHasKey('exception', $result); @@ -583,7 +583,7 @@ public function test_call_external_function() { } /** - * Text external_util::get_area_files + * Text \external_util::get_area_files */ public function test_external_util_get_area_files() { global $CFG, $DB; @@ -622,7 +622,7 @@ public function test_external_util_get_area_files() { 'isexternalfile' => false, ); // Get all the files for the area. - $files = external_util::get_area_files($context, $component, $filearea, false); + $files = \external_util::get_area_files($context, $component, $filearea, false); $this->assertEquals($expectedfiles, $files); $DB->method('get_in_or_equal')->willReturn([ @@ -631,7 +631,7 @@ public function test_external_util_get_area_files() { ]); // Get just the file indicated by $itemid. - $files = external_util::get_area_files($context, $component, $filearea, $itemid); + $files = \external_util::get_area_files($context, $component, $filearea, $itemid); $this->assertEquals($expectedfiles, $files); } @@ -641,7 +641,7 @@ public function test_external_util_get_area_files() { */ public function test_external_files() { - $description = new external_files(); + $description = new \external_files(); // First check that the expected default values and keys are returned. $expectedkeys = array_flip(array('filename', 'filepath', 'filesize', 'fileurl', 'timemodified', 'mimetype', @@ -686,7 +686,7 @@ public function test_user_created_tokens_duration() { /* * Just a wrapper to access protected apis for testing */ -class test_exernal_api extends external_api { +class test_exernal_api extends \external_api { public static function get_context_wrapper($params) { return self::get_context_from_params($params); diff --git a/message/output/airnotifier/tests/externallib_test.php b/message/output/airnotifier/tests/externallib_test.php index a4fbbf8524501..ef6fc065f8387 100644 --- a/message/output/airnotifier/tests/externallib_test.php +++ b/message/output/airnotifier/tests/externallib_test.php @@ -14,14 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * External airnotifier functions unit tests - * - * @package message_airnotifier - * @category external - * @copyright 2012 Jerome Mouneyrac - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace message_airnotifier; + +use externallib_advanced_testcase; +use message_airnotifier_external; defined('MOODLE_INTERNAL') || die(); @@ -37,7 +33,7 @@ * @copyright 2012 Jerome Mouneyrac * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class message_airnotifier_external_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * Tests set up @@ -60,7 +56,7 @@ public function test_is_system_configured() { // In a clean installation, it should be not configured. $configured = message_airnotifier_external::is_system_configured(); - $configured = external_api::clean_returnvalue(message_airnotifier_external::is_system_configured_returns(), $configured); + $configured = \external_api::clean_returnvalue(message_airnotifier_external::is_system_configured_returns(), $configured); $this->assertEquals(0, $configured); // Fake configuration. @@ -69,7 +65,7 @@ public function test_is_system_configured() { $DB->set_field('message_processors', 'enabled', 1, array('name' => 'airnotifier')); $configured = message_airnotifier_external::is_system_configured(); - $configured = external_api::clean_returnvalue(message_airnotifier_external::is_system_configured_returns(), $configured); + $configured = \external_api::clean_returnvalue(message_airnotifier_external::is_system_configured_returns(), $configured); $this->assertEquals(1, $configured); } @@ -95,7 +91,7 @@ public function test_are_notification_preferences_configured() { $preferences = message_airnotifier_external::are_notification_preferences_configured($params); $returnsdescription = message_airnotifier_external::are_notification_preferences_configured_returns(); - $preferences = external_api::clean_returnvalue($returnsdescription, $preferences); + $preferences = \external_api::clean_returnvalue($returnsdescription, $preferences); $expected = array( array( @@ -111,7 +107,7 @@ public function test_are_notification_preferences_configured() { // Now, remove one user. delete_user($user2); $preferences = message_airnotifier_external::are_notification_preferences_configured($params); - $preferences = external_api::clean_returnvalue($returnsdescription, $preferences); + $preferences = \external_api::clean_returnvalue($returnsdescription, $preferences); $this->assertEquals(1, count($preferences['users'])); $this->assertEquals($expected, $preferences['users']); $this->assertEquals(2, count($preferences['warnings'])); @@ -119,13 +115,13 @@ public function test_are_notification_preferences_configured() { // Now, remove one user1 preference (the user still has one prefernce for airnotifier). unset_user_preference('message_provider_moodle_instantmessage_loggedin', $user1); $preferences = message_airnotifier_external::are_notification_preferences_configured($params); - $preferences = external_api::clean_returnvalue($returnsdescription, $preferences); + $preferences = \external_api::clean_returnvalue($returnsdescription, $preferences); $this->assertEquals($expected, $preferences['users']); // Delete the last user1 preference. unset_user_preference('message_provider_moodle_instantmessage_loggedoff', $user1); $preferences = message_airnotifier_external::are_notification_preferences_configured($params); - $preferences = external_api::clean_returnvalue($returnsdescription, $preferences); + $preferences = \external_api::clean_returnvalue($returnsdescription, $preferences); $expected = array( array( 'userid' => $user1->id, @@ -147,7 +143,7 @@ public function test_get_user_devices() { // System not configured. $devices = message_airnotifier_external::get_user_devices(''); - $devices = external_api::clean_returnvalue(message_airnotifier_external::get_user_devices_returns(), $devices); + $devices = \external_api::clean_returnvalue(message_airnotifier_external::get_user_devices_returns(), $devices); $this->assertCount(1, $devices['warnings']); $this->assertEquals('systemnotconfigured', $devices['warnings'][0]['warningcode']); @@ -158,7 +154,7 @@ public function test_get_user_devices() { // Get devices. $devices = message_airnotifier_external::get_user_devices(''); - $devices = external_api::clean_returnvalue(message_airnotifier_external::get_user_devices_returns(), $devices); + $devices = \external_api::clean_returnvalue(message_airnotifier_external::get_user_devices_returns(), $devices); $this->assertCount(0, $devices['warnings']); // No devices, unfortunatelly we cannot create devices (we can't mock airnotifier server). $this->assertCount(0, $devices['devices']); @@ -213,13 +209,13 @@ public function test_enable_device() { // Disable and enable. $result = message_airnotifier_external::enable_device($airnotifierdevid, false); - $result = external_api::clean_returnvalue(message_airnotifier_external::enable_device_returns(), $result); + $result = \external_api::clean_returnvalue(message_airnotifier_external::enable_device_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertTrue($result['success']); $this->assertEquals(0, $DB->get_field('message_airnotifier_devices', 'enable', array('id' => $airnotifierdevid))); $result = message_airnotifier_external::enable_device($airnotifierdevid, true); - $result = external_api::clean_returnvalue(message_airnotifier_external::enable_device_returns(), $result); + $result = \external_api::clean_returnvalue(message_airnotifier_external::enable_device_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertTrue($result['success']); $this->assertEquals(1, $DB->get_field('message_airnotifier_devices', 'enable', array('id' => $airnotifierdevid))); diff --git a/message/output/popup/tests/externallib_test.php b/message/output/popup/tests/externallib_test.php index 26da55f692cb2..5e5d4b337298b 100644 --- a/message/output/popup/tests/externallib_test.php +++ b/message/output/popup/tests/externallib_test.php @@ -14,13 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * External message popup functions unit tests - * - * @package message_popup - * @copyright 2016 Ryan Wyllie - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace message_popup; + +use message_popup_external; +use message_popup_test_helper; defined('MOODLE_INTERNAL') || die(); @@ -33,10 +30,11 @@ /** * Class for external message popup functions unit tests. * + * @package message_popup * @copyright 2016 Ryan Wyllie * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class message_popup_externallib_testcase extends advanced_testcase { +class externallib_test extends \advanced_testcase { use message_popup_test_helper; /** diff --git a/message/tests/externallib_test.php b/message/tests/externallib_test.php index f716e85703ccb..faeb839187269 100644 --- a/message/tests/externallib_test.php +++ b/message/tests/externallib_test.php @@ -14,14 +14,11 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * External message functions unit tests - * - * @package core_message - * @category external - * @copyright 2012 Jerome Mouneyrac - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace core_message; + +use core_message\tests\helper as testhelper; +use core_message_external; +use externallib_advanced_testcase; defined('MOODLE_INTERNAL') || die(); @@ -30,9 +27,15 @@ require_once($CFG->dirroot . '/webservice/tests/helpers.php'); require_once($CFG->dirroot . '/message/externallib.php'); -use \core_message\tests\helper as testhelper; - -class core_message_externallib_testcase extends externallib_advanced_testcase { +/** + * External message functions unit tests + * + * @package core_message + * @category external + * @copyright 2012 Jerome Mouneyrac + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class externallib_test extends externallib_advanced_testcase { /** * Tests set up @@ -50,8 +53,8 @@ protected function setUp(): void { * sent from a user to another. We should stop using it once {@link message_send()} will support * transactions. This is not clean at all, this is just used to add rows to the table. * - * @param stdClass $userfrom user object of the one sending the message. - * @param stdClass $userto user object of the one receiving the message. + * @param \stdClass $userfrom user object of the one sending the message. + * @param \stdClass $userto user object of the one receiving the message. * @param string $message message to send. * @param int $notification is the message a notification. * @param int $time the time the message was sent @@ -64,7 +67,7 @@ protected function send_message($userfrom, $userto, $message = 'Hello world!', $ } if ($notification) { - $record = new stdClass(); + $record = new \stdClass(); $record->useridfrom = $userfrom->id; $record->useridto = $userto->id; $record->subject = 'No subject'; @@ -87,7 +90,7 @@ protected function send_message($userfrom, $userto, $message = 'Hello world!', $ } // Ok, send the message. - $record = new stdClass(); + $record = new \stdClass(); $record->useridfrom = $userfrom->id; $record->conversationid = $conversationid; $record->subject = 'No subject'; @@ -122,7 +125,7 @@ public function test_send_instant_messages() { $messages = array($message1); $sentmessages = core_message_external::send_instant_messages($messages); - $sentmessages = external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages); + $sentmessages = \external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages); $this->assertEquals( get_string('usercantbemessaged', 'message', fullname(\core_user::get_user($message1['touserid']))), array_pop($sentmessages)['errormessage'] @@ -134,7 +137,7 @@ public function test_send_instant_messages() { // Send message again. Now it should work properly. $sentmessages = core_message_external::send_instant_messages($messages); // We need to execute the return values cleaning process to simulate the web service server. - $sentmessages = external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages); + $sentmessages = \external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages); $sentmessage = reset($sentmessages); @@ -183,7 +186,7 @@ public function test_send_instant_messages_long_text() { \core_message\api::add_contact($user1->id, $user2->id); $sentmessages = core_message_external::send_instant_messages($messages); - $sentmessages = external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages); + $sentmessages = \external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages); $this->assertEquals( get_string('errormessagetoolong', 'message'), array_pop($sentmessages)['errormessage'] @@ -216,7 +219,7 @@ public function test_send_instant_messages_blocked_user() { $messages = array($message1); $sentmessages = core_message_external::send_instant_messages($messages); - $sentmessages = external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages); + $sentmessages = \external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages); $sentmessage = reset($sentmessages); @@ -252,7 +255,7 @@ public function test_send_instant_messages_block_non_contacts() { $messages = array($message1); $sentmessages = core_message_external::send_instant_messages($messages); - $sentmessages = external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages); + $sentmessages = \external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages); $sentmessage = reset($sentmessages); @@ -290,7 +293,7 @@ public function test_send_instant_messages_block_non_contacts_but_am_contact() { $messages = array($message1); $sentmessages = core_message_external::send_instant_messages($messages); - $sentmessages = external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages); + $sentmessages = \external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages); $sentmessage = reset($sentmessages); @@ -328,7 +331,7 @@ public function test_send_instant_messages_no_capability() { $this->setUser($user1); // Unset the required capabilities by the external function. - $contextid = context_system::instance()->id; + $contextid = \context_system::instance()->id; $userrole = $DB->get_record('role', array('shortname' => 'user')); $this->unassignUserCapability('moodle/site:sendmessage', $contextid, $userrole->id); @@ -439,7 +442,7 @@ public function test_get_contact_requests() { \core_message\api::create_contact_request($user3->id, $user1->id); $requests = core_message_external::get_contact_requests($user1->id); - $requests = external_api::clean_returnvalue(core_message_external::get_contact_requests_returns(), $requests); + $requests = \external_api::clean_returnvalue(core_message_external::get_contact_requests_returns(), $requests); $this->assertCount(1, $requests); @@ -474,7 +477,7 @@ public function test_get_contact_requests_blocked_sender() { // Verify we don't see the contact request from the blocked user User2 in the requests for User1. $this->setUser($user1); $requests = core_message_external::get_contact_requests($user1->id); - $requests = external_api::clean_returnvalue(core_message_external::get_contact_requests_returns(), $requests); + $requests = \external_api::clean_returnvalue(core_message_external::get_contact_requests_returns(), $requests); $this->assertCount(0, $requests); } @@ -490,7 +493,7 @@ public function test_get_contact_requests_no_requests() { $this->setUser($user1); $requests = core_message_external::get_contact_requests($user1->id); - $requests = external_api::clean_returnvalue(core_message_external::get_contact_requests_returns(), $requests); + $requests = \external_api::clean_returnvalue(core_message_external::get_contact_requests_returns(), $requests); $this->assertEmpty($requests); } @@ -511,7 +514,7 @@ public function test_get_contact_requests_with_limits() { \core_message\api::create_contact_request($user3->id, $user1->id); $requests = core_message_external::get_contact_requests($user1->id, 0, 1); - $requests = external_api::clean_returnvalue(core_message_external::get_contact_requests_returns(), $requests); + $requests = \external_api::clean_returnvalue(core_message_external::get_contact_requests_returns(), $requests); $this->assertCount(1, $requests); } @@ -569,21 +572,21 @@ public function test_get_received_contact_requests_count() { $this->setUser($user1); $contactrequestnumber = core_message_external::get_received_contact_requests_count($user1->id); - $contactrequestnumber = external_api::clean_returnvalue( + $contactrequestnumber = \external_api::clean_returnvalue( core_message_external::get_received_contact_requests_count_returns(), $contactrequestnumber); $this->assertEquals(0, $contactrequestnumber); \core_message\api::create_contact_request($user2->id, $user1->id); $contactrequestnumber = core_message_external::get_received_contact_requests_count($user1->id); - $contactrequestnumber = external_api::clean_returnvalue( + $contactrequestnumber = \external_api::clean_returnvalue( core_message_external::get_received_contact_requests_count_returns(), $contactrequestnumber); $this->assertEquals(1, $contactrequestnumber); \core_message\api::create_contact_request($user3->id, $user1->id); $contactrequestnumber = core_message_external::get_received_contact_requests_count($user1->id); - $contactrequestnumber = external_api::clean_returnvalue( + $contactrequestnumber = \external_api::clean_returnvalue( core_message_external::get_received_contact_requests_count_returns(), $contactrequestnumber); $this->assertEquals(2, $contactrequestnumber); @@ -591,7 +594,7 @@ public function test_get_received_contact_requests_count() { // Web service should ignore sent requests. $contactrequestnumber = core_message_external::get_received_contact_requests_count($user1->id); - $contactrequestnumber = external_api::clean_returnvalue( + $contactrequestnumber = \external_api::clean_returnvalue( core_message_external::get_received_contact_requests_count_returns(), $contactrequestnumber); $this->assertEquals(2, $contactrequestnumber); } @@ -613,7 +616,7 @@ public function test_get_received_contact_requests_count_blocked_sender() { // Verify we don't see the contact request from the blocked user User2 in the count for User1. $this->setUser($user1); $contactrequestnumber = core_message_external::get_received_contact_requests_count($user1->id); - $contactrequestnumber = external_api::clean_returnvalue( + $contactrequestnumber = \external_api::clean_returnvalue( core_message_external::get_received_contact_requests_count_returns(), $contactrequestnumber); $this->assertEquals(0, $contactrequestnumber); } @@ -673,7 +676,7 @@ public function test_create_contact_request() { $CFG->messagingallusers = 1; $return = core_message_external::create_contact_request($user1->id, $user2->id); - $return = external_api::clean_returnvalue(core_message_external::create_contact_request_returns(), $return); + $return = \external_api::clean_returnvalue(core_message_external::create_contact_request_returns(), $return); $this->assertEquals([], $return['warnings']); $request = $DB->get_records('message_contact_requests'); @@ -704,7 +707,7 @@ public function test_create_contact_request_not_allowed() { $CFG->messagingallusers = 0; $return = core_message_external::create_contact_request($user1->id, $user2->id); - $return = external_api::clean_returnvalue(core_message_external::create_contact_request_returns(), $return); + $return = \external_api::clean_returnvalue(core_message_external::create_contact_request_returns(), $return); $warning = reset($return['warnings']); @@ -772,7 +775,7 @@ public function test_confirm_contact_request() { $this->setUser($user2); $return = core_message_external::confirm_contact_request($user1->id, $user2->id); - $return = external_api::clean_returnvalue(core_message_external::confirm_contact_request_returns(), $return); + $return = \external_api::clean_returnvalue(core_message_external::confirm_contact_request_returns(), $return); $this->assertEquals(array(), $return); $this->assertEquals(0, $DB->count_records('message_contact_requests')); @@ -845,7 +848,7 @@ public function test_decline_contact_request() { $this->setUser($user2); $return = core_message_external::decline_contact_request($user1->id, $user2->id); - $return = external_api::clean_returnvalue(core_message_external::decline_contact_request_returns(), $return); + $return = \external_api::clean_returnvalue(core_message_external::decline_contact_request_returns(), $return); $this->assertEquals(array(), $return); $this->assertEquals(0, $DB->count_records('message_contact_requests')); @@ -910,7 +913,7 @@ public function test_mute_conversations() { // Muting a conversation. $return = core_message_external::mute_conversations($user1->id, [$conversation->id]); - $return = external_api::clean_returnvalue(core_message_external::mute_conversations_returns(), $return); + $return = \external_api::clean_returnvalue(core_message_external::mute_conversations_returns(), $return); $this->assertEquals(array(), $return); // Get list of muted conversations. @@ -922,7 +925,7 @@ public function test_mute_conversations() { // Muting a conversation that is already muted. $return = core_message_external::mute_conversations($user1->id, [$conversation->id]); - $return = external_api::clean_returnvalue(core_message_external::mute_conversations_returns(), $return); + $return = \external_api::clean_returnvalue(core_message_external::mute_conversations_returns(), $return); $this->assertEquals(array(), $return); $this->assertEquals(1, $DB->count_records('message_conversation_actions')); @@ -995,14 +998,14 @@ public function test_unmute_conversations() { // Unmuting a conversation. $return = core_message_external::unmute_conversations($user1->id, [$conversation->id]); - $return = external_api::clean_returnvalue(core_message_external::unmute_conversations_returns(), $return); + $return = \external_api::clean_returnvalue(core_message_external::unmute_conversations_returns(), $return); $this->assertEquals(array(), $return); $this->assertEquals(0, $DB->count_records('message_conversation_actions')); // Unmuting a conversation which is already unmuted. $return = core_message_external::unmute_conversations($user1->id, [$conversation->id]); - $return = external_api::clean_returnvalue(core_message_external::unmute_conversations_returns(), $return); + $return = \external_api::clean_returnvalue(core_message_external::unmute_conversations_returns(), $return); $this->assertEquals(array(), $return); $this->assertEquals(0, $DB->count_records('message_conversation_actions')); @@ -1069,7 +1072,7 @@ public function test_block_user() { // Blocking a user. $return = core_message_external::block_user($user1->id, $user2->id); - $return = external_api::clean_returnvalue(core_message_external::block_user_returns(), $return); + $return = \external_api::clean_returnvalue(core_message_external::block_user_returns(), $return); $this->assertEquals(array(), $return); // Get list of blocked users. @@ -1080,7 +1083,7 @@ public function test_block_user() { // Blocking a user who is already blocked. $return = core_message_external::block_user($user1->id, $user2->id); - $return = external_api::clean_returnvalue(core_message_external::block_user_returns(), $return); + $return = \external_api::clean_returnvalue(core_message_external::block_user_returns(), $return); $this->assertEquals(array(), $return); $this->assertEquals(1, $DB->count_records('message_users_blocked')); @@ -1100,11 +1103,11 @@ public function test_block_user_when_ineffective() { $this->setUser($user1); $authenticateduser = $DB->get_record('role', array('shortname' => 'user')); - assign_capability('moodle/site:messageanyuser', CAP_ALLOW, $authenticateduser->id, context_system::instance(), true); + assign_capability('moodle/site:messageanyuser', CAP_ALLOW, $authenticateduser->id, \context_system::instance(), true); // Blocking a user. $return = core_message_external::block_user($user1->id, $user2->id); - $return = external_api::clean_returnvalue(core_message_external::block_user_returns(), $return); + $return = \external_api::clean_returnvalue(core_message_external::block_user_returns(), $return); $this->assertEquals(array(), $return); $this->assertEquals(0, $DB->count_records('message_users_blocked')); @@ -1168,14 +1171,14 @@ public function test_unblock_user() { // Unblocking a user. $return = core_message_external::unblock_user($user1->id, $user2->id); - $return = external_api::clean_returnvalue(core_message_external::unblock_user_returns(), $return); + $return = \external_api::clean_returnvalue(core_message_external::unblock_user_returns(), $return); $this->assertEquals(array(), $return); $this->assertEquals(0, $DB->count_records('message_users_blocked')); // Unblocking a user who is already unblocked. $return = core_message_external::unblock_user($user1->id, $user2->id); - $return = external_api::clean_returnvalue(core_message_external::unblock_user_returns(), $return); + $return = \external_api::clean_returnvalue(core_message_external::unblock_user_returns(), $return); $this->assertEquals(array(), $return); $this->assertEquals(0, $DB->count_records('message_users_blocked')); @@ -1231,27 +1234,27 @@ public function test_search_contacts() { $course1 = $this->getDataGenerator()->create_course(); $course2 = $this->getDataGenerator()->create_course(); - $user1 = new stdClass(); + $user1 = new \stdClass(); $user1->firstname = 'X'; $user1->lastname = 'X'; $user1 = $this->getDataGenerator()->create_user($user1); $this->getDataGenerator()->enrol_user($user1->id, $course1->id); $this->getDataGenerator()->enrol_user($user1->id, $course2->id); - $user2 = new stdClass(); + $user2 = new \stdClass(); $user2->firstname = 'Eric'; $user2->lastname = 'Cartman'; $user2 = self::getDataGenerator()->create_user($user2); - $user3 = new stdClass(); + $user3 = new \stdClass(); $user3->firstname = 'Stan'; $user3->lastname = 'Marsh'; $user3 = self::getDataGenerator()->create_user($user3); self::getDataGenerator()->enrol_user($user3->id, $course1->id); - $user4 = new stdClass(); + $user4 = new \stdClass(); $user4->firstname = 'Kyle'; $user4->lastname = 'Broflovski'; $user4 = self::getDataGenerator()->create_user($user4); - $user5 = new stdClass(); + $user5 = new \stdClass(); $user5->firstname = 'Kenny'; $user5->lastname = 'McCormick'; $user5 = self::getDataGenerator()->create_user($user5); @@ -1260,31 +1263,31 @@ public function test_search_contacts() { $this->setUser($user1); $results = core_message_external::search_contacts('r'); - $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results); + $results = \external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results); $this->assertCount(5, $results); // Users 2 through 5 + admin $results = core_message_external::search_contacts('r', true); - $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results); + $results = \external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results); $this->assertCount(2, $results); $results = core_message_external::search_contacts('Kyle', false); - $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results); + $results = \external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results); $this->assertCount(1, $results); $result = reset($results); $this->assertEquals($user4->id, $result['id']); $results = core_message_external::search_contacts('y', false); - $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results); + $results = \external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results); $this->assertCount(2, $results); $results = core_message_external::search_contacts('y', true); - $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results); + $results = \external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results); $this->assertCount(1, $results); $result = reset($results); $this->assertEquals($user5->id, $result['id']); // Empty query, will throw an exception. - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); $results = core_message_external::search_contacts(''); } @@ -1318,51 +1321,51 @@ public function test_get_messages() { $this->setUser($user1); // Get unread conversations from user1 to user2. $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', MESSAGE_GET_UNREAD, true, 0, 0); - $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); + $messages = \external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); $this->assertCount(1, $messages['messages']); $this->assertEquals($im1, $messages['messages'][0]['id']); // Get read conversations from user1 to user2. $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', MESSAGE_GET_READ, true, 0, 0); - $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); + $messages = \external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); $this->assertCount(1, $messages['messages']); $this->assertEquals($im6, $messages['messages'][0]['id']); // Get both read and unread conversations from user1 to user2. $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', MESSAGE_GET_READ_AND_UNREAD, true, 0, 0); - $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); + $messages = \external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); $this->assertCount(2, $messages['messages']); // Delete an unread message. \core_message\api::delete_message($user1->id, $im1); $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', MESSAGE_GET_UNREAD, true, 0, 0); - $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); + $messages = \external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); $this->assertCount(0, $messages['messages']); $this->setUser($user2); // Get unread conversations from any user to user2. $messages = core_message_external::get_messages($user2->id, 0, 'conversations', MESSAGE_GET_UNREAD, true, 0, 0); - $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); + $messages = \external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); $this->assertCount(2, $messages['messages']); // Conversations from user3 to user2. $messages = core_message_external::get_messages($user2->id, $user3->id, 'conversations', MESSAGE_GET_UNREAD, true, 0, 0); - $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); + $messages = \external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); $this->assertCount(1, $messages['messages']); // Delete the message. \core_message\api::delete_message($user2->id, $im4); $messages = core_message_external::get_messages($user2->id, $user3->id, 'conversations', MESSAGE_GET_UNREAD, true, 0, 0); - $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); + $messages = \external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); $this->assertCount(0, $messages['messages']); $this->setUser($user3); // Get unread notifications received by user3. $messages = core_message_external::get_messages($user3->id, 0, 'notifications', MESSAGE_GET_UNREAD, true, 0, 0); - $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); + $messages = \external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); $this->assertCount(0, $messages['messages']); // Now, create some notifications... @@ -1399,7 +1402,7 @@ public function test_get_messages() { $message->fullmessagehtml = markdown_to_html($message->fullmessage); $message->smallmessage = $message->subject; $message->contexturlname = $course->fullname; - $message->contexturl = (string)new moodle_url('/course/view.php', array('id' => $course->id)); + $message->contexturl = (string)new \moodle_url('/course/view.php', array('id' => $course->id)); message_send($message); $message = new \core\message\message(); @@ -1415,10 +1418,10 @@ public function test_get_messages() { $message->fullmessagehtml = markdown_to_html($message->fullmessage); $message->smallmessage = $message->subject; $message->contexturlname = $course->fullname; - $message->contexturl = (string)new moodle_url('/course/view.php', array('id' => $course->id)); + $message->contexturl = (string)new \moodle_url('/course/view.php', array('id' => $course->id)); message_send($message); - $userfrom = core_user::get_noreply_user(); + $userfrom = \core_user::get_noreply_user(); $userfrom->maildisplay = true; $eventdata = new \core\message\message(); $eventdata->courseid = $course->id; @@ -1451,32 +1454,32 @@ public function test_get_messages() { $this->setUser($user1); // Get unread notifications from any user to user1. $messages = core_message_external::get_messages($user1->id, 0, 'notifications', MESSAGE_GET_UNREAD, true, 0, 0); - $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); + $messages = \external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); $this->assertCount(3, $messages['messages']); // Get one unread notifications from any user to user1. $messages = core_message_external::get_messages($user1->id, 0, 'notifications', MESSAGE_GET_UNREAD, true, 0, 1); - $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); + $messages = \external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); $this->assertCount(1, $messages['messages']); // Get read notifications from any user to user1. $messages = core_message_external::get_messages($user1->id, 0, 'notifications', MESSAGE_GET_READ, true, 0, 0); - $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); + $messages = \external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); $this->assertCount(0, $messages['messages']); // Get unread both type of messages from any user to user1. $messages = core_message_external::get_messages($user1->id, 0, 'both', MESSAGE_GET_UNREAD, true, 0, 0); - $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); + $messages = \external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); $this->assertCount(4, $messages['messages']); // Get unread notifications from no-reply-user to user1. $messages = core_message_external::get_messages($user1->id, $userfrom->id, 'notifications', MESSAGE_GET_UNREAD, true, 0, 0); - $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); + $messages = \external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); $this->assertCount(1, $messages['messages']); // Get notifications send by user1 to any user. $messages = core_message_external::get_messages(0, $user1->id, 'notifications', MESSAGE_GET_UNREAD, true, 0, 0); - $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); + $messages = \external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); $this->assertCount(1, $messages['messages']); // Check we receive custom data as a unserialisable json. $this->assertObjectHasAttribute('datakey', json_decode($messages['messages'][0]['customdata'])); @@ -1489,7 +1492,7 @@ public function test_get_messages() { $CFG->messaging = 0; $messages = core_message_external::get_messages(0, $user1->id, 'both', MESSAGE_GET_UNREAD, true, 0, 0); - $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); + $messages = \external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); $this->assertCount(1, $messages['warnings']); // Test exceptions. @@ -1498,7 +1501,7 @@ public function test_get_messages() { try { $messages = core_message_external::get_messages(0, $user1->id, 'conversations', MESSAGE_GET_UNREAD, true, 0, 0); $this->fail('Exception expected due messaging disabled.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('disabled', $e->errorcode); } @@ -1508,7 +1511,7 @@ public function test_get_messages() { try { $messages = core_message_external::get_messages(0, 0, 'conversations', MESSAGE_GET_UNREAD, true, 0, 0); $this->fail('Exception expected due invalid users.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('accessdenied', $e->errorcode); } @@ -1516,7 +1519,7 @@ public function test_get_messages() { try { $messages = core_message_external::get_messages(2500, 0, 'conversations', MESSAGE_GET_UNREAD, true, 0, 0); $this->fail('Exception expected due invalid users.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('invaliduser', $e->errorcode); } @@ -1525,7 +1528,7 @@ public function test_get_messages() { try { $messages = core_message_external::get_messages(0, $user1->id, 'conversations', MESSAGE_GET_UNREAD, true, 0, 0); $this->fail('Exception expected due invalid user.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('accessdenied', $e->errorcode); } @@ -1549,7 +1552,7 @@ public function test_get_messages_useridto_all() { // Get messages sent from user 1. $messages = core_message_external::get_messages(0, $user1->id, 'conversations', MESSAGE_GET_UNREAD, false, 0, 0); - $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); + $messages = \external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); // Confirm the data is correct. $messages = $messages['messages']; @@ -1583,7 +1586,7 @@ public function test_get_messages_useridfrom_all() { // Get messages sent to user 1. $messages = core_message_external::get_messages($user1->id, 0, 'conversations', MESSAGE_GET_UNREAD, false, 0, 0); - $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); + $messages = \external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); // Confirm the data is correct. $messages = $messages['messages']; @@ -1628,19 +1631,19 @@ public function test_get_blocked_users() { // Retrieve the list of blocked users. $this->setUser($user1); $blockedusers = core_message_external::get_blocked_users($user1->id); - $blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers); + $blockedusers = \external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers); $this->assertCount(0, $blockedusers['users']); // Block the $userblocked and retrieve again the list. \core_message\api::block_user($user1->id, $userblocked->id); $blockedusers = core_message_external::get_blocked_users($user1->id); - $blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers); + $blockedusers = \external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers); $this->assertCount(1, $blockedusers['users']); // Remove the $userblocked and check that the list now is empty. delete_user($userblocked); $blockedusers = core_message_external::get_blocked_users($user1->id); - $blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers); + $blockedusers = \external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers); $this->assertCount(0, $blockedusers['users']); } @@ -1671,7 +1674,7 @@ public function test_mark_message_read() { $messageids = array(); foreach ($lastmessages as $m) { $messageid = core_message_external::mark_message_read($m->id, time()); - $messageids[] = external_api::clean_returnvalue(core_message_external::mark_message_read_returns(), $messageid); + $messageids[] = \external_api::clean_returnvalue(core_message_external::mark_message_read_returns(), $messageid); } // Retrieve all messages sent (they are currently read). @@ -1688,7 +1691,7 @@ public function test_mark_message_read() { try { $messageid = core_message_external::mark_message_read(1337, time()); $this->fail('Exception expected due invalid messageid.'); - } catch (dml_missing_record_exception $e) { + } catch (\dml_missing_record_exception $e) { $this->assertEquals('invalidrecordunknown', $e->errorcode); } @@ -1698,7 +1701,7 @@ public function test_mark_message_read() { try { $messageid = core_message_external::mark_message_read($messageid, time()); $this->fail('Exception expected due invalid messageid.'); - } catch (invalid_parameter_exception $e) { + } catch (\invalid_parameter_exception $e) { $this->assertEquals('invalidparameter', $e->errorcode); } } @@ -1730,7 +1733,7 @@ public function test_mark_notification_read() { $notificationids = array(); foreach ($lastnotifications as $n) { $notificationid = core_message_external::mark_notification_read($n->id, time()); - $notificationids[] = external_api::clean_returnvalue(core_message_external::mark_notification_read_returns(), + $notificationids[] = \external_api::clean_returnvalue(core_message_external::mark_notification_read_returns(), $notificationid); } @@ -1748,7 +1751,7 @@ public function test_mark_notification_read() { try { $notificationid = core_message_external::mark_notification_read(1337, time()); $this->fail('Exception expected due invalid notificationid.'); - } catch (dml_missing_record_exception $e) { + } catch (\dml_missing_record_exception $e) { $this->assertEquals('invalidrecord', $e->errorcode); } @@ -1758,7 +1761,7 @@ public function test_mark_notification_read() { try { $notificationid = core_message_external::mark_notification_read($notificationid, time()); $this->fail('Exception expected due invalid notificationid.'); - } catch (invalid_parameter_exception $e) { + } catch (\invalid_parameter_exception $e) { $this->assertEquals('invalidparameter', $e->errorcode); } } @@ -1791,7 +1794,7 @@ public function test_delete_message() { // Delete a message not read, as a user from. $result = core_message_external::delete_message($m1to2, $user1->id, false); - $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result); $this->assertTrue($result['status']); $this->assertCount(0, $result['warnings']); $mua = $DB->get_record('message_user_actions', array('messageid' => $m1to2, 'userid' => $user1->id)); @@ -1799,21 +1802,21 @@ public function test_delete_message() { // Try to delete the same message again. $result = core_message_external::delete_message($m1to2, $user1->id, false); - $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result); $this->assertFalse($result['status']); // Try to delete a message that does not belong to me. try { $messageid = core_message_external::delete_message($m2to3, $user3->id, false); $this->fail('Exception expected due invalid messageid.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('You do not have permission to delete this message', $e->errorcode); } $this->setUser($user3); // Delete a message not read, as a user to. $result = core_message_external::delete_message($m2to3, $user3->id, false); - $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result); $this->assertTrue($result['status']); $this->assertCount(0, $result['warnings']); $this->assertTrue($DB->record_exists('message_user_actions', array('messageid' => $m2to3, 'userid' => $user3->id, @@ -1823,7 +1826,7 @@ public function test_delete_message() { $message = $DB->get_record('messages', ['id' => $m3to2]); \core_message\api::mark_message_as_read($user3->id, $message, time()); $result = core_message_external::delete_message($m3to2, $user3->id); - $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result); $this->assertTrue($result['status']); $this->assertCount(0, $result['warnings']); $this->assertTrue($DB->record_exists('message_user_actions', array('messageid' => $m3to2, 'userid' => $user3->id, @@ -1833,7 +1836,7 @@ public function test_delete_message() { try { $result = core_message_external::delete_message(-1, $user1->id); $this->fail('Exception expected due invalid messageid.'); - } catch (dml_missing_record_exception $e) { + } catch (\dml_missing_record_exception $e) { $this->assertEquals('invalidrecord', $e->errorcode); } @@ -1841,7 +1844,7 @@ public function test_delete_message() { try { $result = core_message_external::delete_message($m1to2, -1, false); $this->fail('Exception expected due invalid user.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('invaliduser', $e->errorcode); } @@ -1850,14 +1853,14 @@ public function test_delete_message() { try { $result = core_message_external::delete_message($m1to2, $user2->id, false); $this->fail('Exception expected due invalid user.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('userdeleted', $e->errorcode); } // Now, as an admin, try to delete any message. $this->setAdminUser(); $result = core_message_external::delete_message($m3to4, $user4->id, false); - $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result); $this->assertTrue($result['status']); $this->assertCount(0, $result['warnings']); $this->assertTrue($DB->record_exists('message_user_actions', array('messageid' => $m3to4, 'userid' => $user4->id, @@ -1981,7 +1984,7 @@ public function test_get_user_notification_preferences() { set_user_preference('message_provider_mod_assign_assign_notification_loggedoff', 'email', $user); $prefs = core_message_external::get_user_notification_preferences(); - $prefs = external_api::clean_returnvalue(core_message_external::get_user_notification_preferences_returns(), $prefs); + $prefs = \external_api::clean_returnvalue(core_message_external::get_user_notification_preferences_returns(), $prefs); // Check processors. $this->assertGreaterThanOrEqual(2, count($prefs['preferences']['processors'])); $this->assertEquals($user->id, $prefs['preferences']['userid']); @@ -2038,7 +2041,7 @@ public function test_message_search_users_messagingallusers_disabled() { // Create some users. $users = []; foreach (range(1, 8) as $i) { - $user = new stdClass(); + $user = new \stdClass(); $user->firstname = ($i == 4) ? 'User' : 'User search'; // Ensure the fourth user won't match the search term. $user->lastname = $i; $user = $this->getDataGenerator()->create_user($user); @@ -2080,7 +2083,7 @@ public function test_message_search_users_messagingallusers_disabled() { // Perform a search with $CFG->messagingallusers disabled. set_config('messagingallusers', 0); $result = core_message_external::message_search_users($users[1]->id, 'search'); - $result = external_api::clean_returnvalue(core_message_external::message_search_users_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::message_search_users_returns(), $result); // Confirm that we returns contacts and non-contacts. $this->assertArrayHasKey('contacts', $result); @@ -2134,7 +2137,7 @@ public function test_message_search_users_messagingallusers_enabled() { // Create some users. $users = []; foreach (range(1, 9) as $i) { - $user = new stdClass(); + $user = new \stdClass(); $user->firstname = ($i == 4) ? 'User' : 'User search'; // Ensure the fourth user won't match the search term. $user->lastname = $i; $user = $this->getDataGenerator()->create_user($user); @@ -2176,7 +2179,7 @@ public function test_message_search_users_messagingallusers_enabled() { // Perform a search with $CFG->messagingallusers enabled. set_config('messagingallusers', 1); $result = core_message_external::message_search_users($users[1]->id, 'search'); - $result = external_api::clean_returnvalue(core_message_external::message_search_users_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::message_search_users_returns(), $result); // Confirm that we returns contacts and non-contacts. $this->assertArrayHasKey('contacts', $result); @@ -2224,11 +2227,11 @@ public function test_message_search_users_self_conversations() { $this->resetAfterTest(); // Create some users. - $user1 = new stdClass(); + $user1 = new \stdClass(); $user1->firstname = 'User'; $user1->lastname = 'One'; $user1 = $this->getDataGenerator()->create_user($user1); - $user2 = new stdClass(); + $user2 = new \stdClass(); $user2->firstname = 'User'; $user2->lastname = 'Two'; $user2 = $this->getDataGenerator()->create_user($user2); @@ -2240,7 +2243,7 @@ public function test_message_search_users_self_conversations() { // Perform a search as user1. $this->setUser($user1); $result = core_message_external::message_search_users($user1->id, 'One'); - $result = external_api::clean_returnvalue(core_message_external::message_search_users_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::message_search_users_returns(), $result); // Check results are empty. $this->assertCount(0, $result['contacts']); @@ -2254,11 +2257,11 @@ public function test_message_search_users_with_empty_result() { $this->resetAfterTest(); // Create some users, but make sure neither will match the search term. - $user1 = new stdClass(); + $user1 = new \stdClass(); $user1->firstname = 'User'; $user1->lastname = 'One'; $user1 = $this->getDataGenerator()->create_user($user1); - $user2 = new stdClass(); + $user2 = new \stdClass(); $user2->firstname = 'User'; $user2->lastname = 'Two'; $user2 = $this->getDataGenerator()->create_user($user2); @@ -2266,7 +2269,7 @@ public function test_message_search_users_with_empty_result() { // Perform a search as user1. $this->setUser($user1); $result = core_message_external::message_search_users($user1->id, 'search'); - $result = external_api::clean_returnvalue(core_message_external::message_search_users_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::message_search_users_returns(), $result); // Check results are empty. $this->assertCount(0, $result['contacts']); @@ -2282,7 +2285,7 @@ public function test_message_search_users_limit_offset() { // Create 20 users. $users = []; foreach (range(1, 20) as $i) { - $user = new stdClass(); + $user = new \stdClass(); $user->firstname = "User search"; $user->lastname = $i; $user = $this->getDataGenerator()->create_user($user); @@ -2307,7 +2310,7 @@ public function test_message_search_users_limit_offset() { // Search using a limit of 3. // This tests the case where we have more results than the limit for both contacts and non-contacts. $result = core_message_external::message_search_users($users[1]->id, 'search', 0, 3); - $result = external_api::clean_returnvalue(core_message_external::message_search_users_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::message_search_users_returns(), $result); $contacts = $result['contacts']; $noncontacts = $result['noncontacts']; @@ -2327,7 +2330,7 @@ public function test_message_search_users_limit_offset() { // Now, offset to get the next batch of results. // We expect to see 2 contacts, and 3 non-contacts. $result = core_message_external::message_search_users($users[1]->id, 'search', 3, 3); - $result = external_api::clean_returnvalue(core_message_external::message_search_users_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::message_search_users_returns(), $result); $contacts = $result['contacts']; $noncontacts = $result['noncontacts']; $this->assertCount(2, $contacts); @@ -2342,7 +2345,7 @@ public function test_message_search_users_limit_offset() { // Now, offset to get the next batch of results. // We expect to see 0 contacts, and 2 non-contacts. $result = core_message_external::message_search_users($users[1]->id, 'search', 6, 3); - $result = external_api::clean_returnvalue(core_message_external::message_search_users_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::message_search_users_returns(), $result); $contacts = $result['contacts']; $noncontacts = $result['noncontacts']; $this->assertCount(0, $contacts); @@ -2362,7 +2365,7 @@ public function test_message_search_users_with_cap() { // Create some users. $users = []; foreach (range(1, 8) as $i) { - $user = new stdClass(); + $user = new \stdClass(); $user->firstname = ($i == 4) ? 'User' : 'User search'; // Ensure the fourth user won't match the search term. $user->lastname = $i; $user = $this->getDataGenerator()->create_user($user); @@ -2386,12 +2389,12 @@ public function test_message_search_users_with_cap() { // Grant the authenticated user role the capability 'user:viewdetails' at site context. $authenticatedrole = $DB->get_record('role', ['shortname' => 'user'], '*', MUST_EXIST); - assign_capability('moodle/user:viewdetails', CAP_ALLOW, $authenticatedrole->id, context_system::instance()); + assign_capability('moodle/user:viewdetails', CAP_ALLOW, $authenticatedrole->id, \context_system::instance()); // Perform a search with $CFG->messagingallusers disabled. set_config('messagingallusers', 0); $result = core_message_external::message_search_users($users[1]->id, 'search'); - $result = external_api::clean_returnvalue(core_message_external::message_search_users_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::message_search_users_returns(), $result); $contacts = $result['contacts']; $noncontacts = $result['noncontacts']; @@ -2473,7 +2476,7 @@ public function test_messagearea_search_messages() { $result = core_message_external::data_for_messagearea_search_messages($user1->id, 'o'); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_messages_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_messages_returns(), $result); // Confirm the data is correct. $messages = $result['contacts']; @@ -2531,7 +2534,7 @@ public function test_messagearea_search_messages_as_other_user() { $result = core_message_external::data_for_messagearea_search_messages($user1->id, 'o'); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_messages_returns(), + $result = \external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_messages_returns(), $result); // Confirm the data is correct. @@ -2616,22 +2619,22 @@ public function test_get_user_contacts() { // Set as the user. $this->setUser($user1); - $user2 = new stdClass(); + $user2 = new \stdClass(); $user2->firstname = 'User'; $user2->lastname = 'A'; $user2 = self::getDataGenerator()->create_user($user2); - $user3 = new stdClass(); + $user3 = new \stdClass(); $user3->firstname = 'User'; $user3->lastname = 'B'; $user3 = self::getDataGenerator()->create_user($user3); - $user4 = new stdClass(); + $user4 = new \stdClass(); $user4->firstname = 'User'; $user4->lastname = 'C'; $user4 = self::getDataGenerator()->create_user($user4); - $user5 = new stdClass(); + $user5 = new \stdClass(); $user5->firstname = 'User'; $user5->lastname = 'D'; $user5 = self::getDataGenerator()->create_user($user5); @@ -2645,7 +2648,7 @@ public function test_get_user_contacts() { $result = core_message_external::get_user_contacts($user1->id); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(core_message_external::get_user_contacts_returns(), + $result = \external_api::clean_returnvalue(core_message_external::get_user_contacts_returns(), $result); // Confirm the data is correct. @@ -2681,22 +2684,22 @@ public function test_get_user_contacts_as_other_user() { // Create some users. $user1 = self::getDataGenerator()->create_user(); - $user2 = new stdClass(); + $user2 = new \stdClass(); $user2->firstname = 'User'; $user2->lastname = 'A'; $user2 = self::getDataGenerator()->create_user($user2); - $user3 = new stdClass(); + $user3 = new \stdClass(); $user3->firstname = 'User'; $user3->lastname = 'B'; $user3 = self::getDataGenerator()->create_user($user3); - $user4 = new stdClass(); + $user4 = new \stdClass(); $user4->firstname = 'User'; $user4->lastname = 'C'; $user4 = self::getDataGenerator()->create_user($user4); - $user5 = new stdClass(); + $user5 = new \stdClass(); $user5->firstname = 'User'; $user5->lastname = 'D'; $user5 = self::getDataGenerator()->create_user($user5); @@ -2710,7 +2713,7 @@ public function test_get_user_contacts_as_other_user() { $result = core_message_external::get_user_contacts($user1->id); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(core_message_external::get_user_contacts_returns(), + $result = \external_api::clean_returnvalue(core_message_external::get_user_contacts_returns(), $result); // Confirm the data is correct. @@ -2786,7 +2789,7 @@ public function test_get_user_contacts_no_results() { $this->setUser($user1); $requests = core_message_external::get_user_contacts($user1->id); - $requests = external_api::clean_returnvalue(core_message_external::get_user_contacts_returns(), $requests); + $requests = \external_api::clean_returnvalue(core_message_external::get_user_contacts_returns(), $requests); $this->assertEmpty($requests); } @@ -2824,7 +2827,7 @@ public function test_get_conversation_messages() { $result = core_message_external::get_conversation_messages($user1->id, $conversation->id); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(core_message_external::get_conversation_messages_returns(), + $result = \external_api::clean_returnvalue(core_message_external::get_conversation_messages_returns(), $result); // Check the results are correct. @@ -2897,7 +2900,7 @@ public function test_get_conversation_messages_timefrom() { $result = core_message_external::get_conversation_messages($user1->id, $conversation->id, 0, 0, false, $time - 3); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(core_message_external::get_conversation_messages_returns(), + $result = \external_api::clean_returnvalue(core_message_external::get_conversation_messages_returns(), $result); // Check the results are correct. @@ -2953,7 +2956,7 @@ public function test_get_conversation_messages_as_other_user() { $result = core_message_external::get_conversation_messages($user1->id, $conversation->id); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(core_message_external::get_conversation_messages_returns(), + $result = \external_api::clean_returnvalue(core_message_external::get_conversation_messages_returns(), $result); // Check the results are correct. @@ -3225,7 +3228,7 @@ public function test_get_unread_conversations_count() { $result = core_message_external::get_unread_conversations_count($user1->id); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(core_message_external::get_unread_conversations_count_returns(), + $result = \external_api::clean_returnvalue(core_message_external::get_unread_conversations_count_returns(), $result); $this->assertEquals(3, $result); @@ -3265,7 +3268,7 @@ public function test_get_unread_conversations_count_as_other_user() { $result = core_message_external::get_unread_conversations_count($user1->id); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(core_message_external::get_unread_conversations_count_returns(), + $result = \external_api::clean_returnvalue(core_message_external::get_unread_conversations_count_returns(), $result); $this->assertEquals(3, $result); @@ -3472,7 +3475,7 @@ public function test_get_message_processor() { $result = core_message_external::get_message_processor($user1->id, 'popup'); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(core_message_external::get_message_processor_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::get_message_processor_returns(), $result); $this->assertNotEmpty($result['systemconfigured']); $this->assertNotEmpty($result['userconfigured']); @@ -3496,7 +3499,7 @@ public function test_get_user_message_preferences() { set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_SITE, $user); $prefs = core_message_external::get_user_message_preferences(); - $prefs = external_api::clean_returnvalue(core_message_external::get_user_message_preferences_returns(), $prefs); + $prefs = \external_api::clean_returnvalue(core_message_external::get_user_message_preferences_returns(), $prefs); $this->assertEquals($user->id, $prefs['preferences']['userid']); // Check components. @@ -3590,7 +3593,7 @@ public function test_set_favourite_conversations_basic() { $result = core_message_external::set_favourite_conversations($user1->id, [$conversation1, $conversation2]); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(core_message_external::set_favourite_conversations_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::set_favourite_conversations_returns(), $result); $this->assertCount(0, $result); } @@ -3708,7 +3711,7 @@ public function test_unset_favourite_conversations_basic() { $result = core_message_external::unset_favourite_conversations($user1->id, [$conversation1, $conversation2]); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(core_message_external::unset_favourite_conversations_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::unset_favourite_conversations_returns(), $result); $this->assertCount(0, $result); } @@ -3845,7 +3848,7 @@ public function test_get_conversations_no_restrictions() { // Get all conversations for user1. $result = core_message_external::get_conversations($user1->id); - $result = external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); $conversations = $result['conversations']; $selfconversation = \core_message\api::get_self_conversation($user1->id); @@ -3927,7 +3930,7 @@ public function test_get_conversations_message_format() { // Verify the format of the html message. $expectedmessagetext = message_format_message_text($message); $result = core_message_external::get_conversations($user1->id); - $result = external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); $conversations = $result['conversations']; $messages = $conversations[0]['messages']; $this->assertEquals($expectedmessagetext, $messages[0]['text']); @@ -3948,7 +3951,7 @@ public function test_get_conversations_limit_offset() { // Get all conversations for user1. $result = core_message_external::get_conversations($user1->id, 0, 1); - $result = external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); $conversations = $result['conversations']; // Verify the first conversation. @@ -3958,21 +3961,21 @@ public function test_get_conversations_limit_offset() { // Verify the next conversation. $result = core_message_external::get_conversations($user1->id, 1, 1); - $result = external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); $conversations = $result['conversations']; $this->assertCount(1, $conversations); $this->assertEquals($gc2->id, $conversations[0]['id']); // Verify the next conversation. $result = core_message_external::get_conversations($user1->id, 2, 1); - $result = external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); $conversations = $result['conversations']; $this->assertCount(1, $conversations); $this->assertEquals($ic2->id, $conversations[0]['id']); // Skip one and get both empty conversations. $result = core_message_external::get_conversations($user1->id, 4, 2); - $result = external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); $conversations = $result['conversations']; $this->assertCount(2, $conversations); $this->assertEquals($gc5->id, $conversations[0]['id']); @@ -4001,14 +4004,14 @@ public function test_get_conversations_type_filter() { // Verify we can ask for only individual conversations. $result = core_message_external::get_conversations($user1->id, 0, 20, \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL); - $result = external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); $conversations = $result['conversations']; $this->assertCount(2, $conversations); // Verify we can ask for only group conversations. $result = core_message_external::get_conversations($user1->id, 0, 20, \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP); - $result = external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); $conversations = $result['conversations']; $this->assertCount(4, $conversations); @@ -4039,7 +4042,7 @@ public function test_get_conversations_self_conversations() { // Verify this conversation is returned by the method. $this->setUser($user1); $result = core_message_external::get_conversations($user1->id, 0, 20); - $result = external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); $conversations = $result['conversations']; $this->assertCount(1, $conversations); } @@ -4064,7 +4067,7 @@ public function test_get_conversations_deleted_user() { // Group conversations are also present, albeit with less members. delete_user($user2); $result = core_message_external::get_conversations($user1->id); - $result = external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); $conversations = $result['conversations']; $this->assertCount(7, $conversations); $this->assertEquals($gc3->id, $conversations[0]['id']); @@ -4081,7 +4084,7 @@ public function test_get_conversations_deleted_user() { // This user will still be present in the members array, as will the message in the messages array. delete_user($user4); $result = core_message_external::get_conversations($user1->id); - $result = external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); $conversations = $result['conversations']; $this->assertCount(7, $conversations); $this->assertEquals($gc2->id, $conversations[1]['id']); @@ -4095,7 +4098,7 @@ public function test_get_conversations_deleted_user() { // Group conversations are also present, albeit with less members. delete_user($user3); $result = core_message_external::get_conversations($user1->id); - $result = external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); $conversations = $result['conversations']; $this->assertCount(7, $conversations); $this->assertEquals($gc3->id, $conversations[0]['id']); @@ -4153,7 +4156,7 @@ public function test_get_conversations_deleted_user_from_database() { delete_user($user2); $DB->delete_records('user', ['id' => $user2->id]); $result = core_message_external::get_conversations($user1->id, 0, 20, 1, false); - $result = external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); $conversation = $result['conversations']; @@ -4183,13 +4186,13 @@ public function test_get_conversations_favourite_conversations() { // Try to get ONLY favourite conversations, when no favourites exist. $result = core_message_external::get_conversations($user1->id, 0, 20, null, true); - $result = external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); $conversations = $result['conversations']; $this->assertEquals([], $conversations); // Try to get NO favourite conversations, when no favourites exist. $result = core_message_external::get_conversations($user1->id, 0, 20, null, false); - $result = external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); $conversations = $result['conversations']; // Consider first conversations is self-conversation. $this->assertCount(7, $conversations); @@ -4201,7 +4204,7 @@ public function test_get_conversations_favourite_conversations() { // Get the conversations, first with no restrictions, confirming the favourite status of the conversations. $result = core_message_external::get_conversations($user1->id); - $result = external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); $conversations = $result['conversations']; $this->assertCount(7, $conversations); foreach ($conversations as $conv) { @@ -4212,7 +4215,7 @@ public function test_get_conversations_favourite_conversations() { // Now, get ONLY favourite conversations. $result = core_message_external::get_conversations($user1->id, 0, 20, null, true); - $result = external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); $conversations = $result['conversations']; $this->assertCount(3, $conversations); foreach ($conversations as $conv) { @@ -4229,7 +4232,7 @@ public function test_get_conversations_favourite_conversations() { // And NO favourite conversations. $result = core_message_external::get_conversations($user1->id, 0, 20, null, false); - $result = external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); $conversations = $result['conversations']; $this->assertCount(4, $conversations); foreach ($conversations as $conv) { @@ -4267,7 +4270,7 @@ public function test_get_conversations_group_linked() { $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id)); $result = core_message_external::get_conversations($user1->id, 0, 20, null, false); - $result = external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); $conversations = $result['conversations']; $this->assertEquals(2, $conversations[0]['membercount']); @@ -4278,7 +4281,7 @@ public function test_get_conversations_group_linked() { // Now, disable the conversation linked to the group and verify it's no longer returned. $DB->set_field('message_conversations', 'enabled', 0, ['id' => $conversations[0]['id']]); $result = core_message_external::get_conversations($user1->id, 0, 20, null, false); - $result = external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); $conversations = $result['conversations']; $this->assertCount(0, $conversations); } @@ -4317,7 +4320,7 @@ public function test_get_conversations_group_with_mathjax() { // Call the WebService. $result = core_message_external::get_conversations($user1->id, 0, 20, null, false); - $result = external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); $conversations = $result['conversations']; // Format original data. @@ -4370,7 +4373,7 @@ public function test_get_conversations_user_in_group_and_individual_chat() { $this->setUser($user2); $result = core_message_external::get_conversations($user2->id); - $result = external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); $conversations = $result['conversations']; $groupconversation = array_shift($conversations); @@ -4424,7 +4427,7 @@ public function test_get_conversations_some_muted() { $this->setUser($user1); $result = core_message_external::get_conversations($user1->id); - $result = external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result); $conversations = $result['conversations']; usort($conversations, function($first, $second){ @@ -4478,7 +4481,7 @@ public function test_get_conversation_members_wrong_user() { public function test_get_conversation_members() { $this->resetAfterTest(); - $lastaccess = new stdClass(); + $lastaccess = new \stdClass(); $lastaccess->lastaccess = time(); $user1 = self::getDataGenerator()->create_user($lastaccess); @@ -4512,7 +4515,7 @@ public function test_get_conversation_members() { $this->setAdminUser(); $members = core_message_external::get_conversation_members($user1->id, $conversationid, false); - external_api::clean_returnvalue(core_message_external::get_conversation_members_returns(), $members); + \external_api::clean_returnvalue(core_message_external::get_conversation_members_returns(), $members); // Sort them by id. ksort($members); @@ -4556,7 +4559,7 @@ public function test_get_conversation_members() { public function test_get_conversation_members_with_contact_requests() { $this->resetAfterTest(); - $lastaccess = new stdClass(); + $lastaccess = new \stdClass(); $lastaccess->lastaccess = time(); $user1 = self::getDataGenerator()->create_user($lastaccess); @@ -4590,7 +4593,7 @@ public function test_get_conversation_members_with_contact_requests() { $this->setAdminUser(); $members = core_message_external::get_conversation_members($user1->id, $conversationid, true); - external_api::clean_returnvalue(core_message_external::get_conversation_members_returns(), $members); + \external_api::clean_returnvalue(core_message_external::get_conversation_members_returns(), $members); // Sort them by id. ksort($members); @@ -4710,7 +4713,7 @@ public function test_send_messages_to_conversation_individual() { $sink = $this->redirectMessages(); $writtenmessages = core_message_external::send_messages_to_conversation($ic1->id, $messages); - external_api::clean_returnvalue(core_message_external::send_messages_to_conversation_returns(), $writtenmessages); + \external_api::clean_returnvalue(core_message_external::send_messages_to_conversation_returns(), $writtenmessages); $this->assertCount(2, $writtenmessages); $this->assertObjectHasAttribute('id', $writtenmessages[0]); @@ -4762,7 +4765,7 @@ public function test_send_messages_to_conversation_group() { $sink = $this->redirectMessages(); $writtenmessages = core_message_external::send_messages_to_conversation($gc2->id, $messages); - external_api::clean_returnvalue(core_message_external::send_messages_to_conversation_returns(), $writtenmessages); + \external_api::clean_returnvalue(core_message_external::send_messages_to_conversation_returns(), $writtenmessages); $this->assertCount(2, $writtenmessages); $this->assertObjectHasAttribute('id', $writtenmessages[0]); @@ -4867,7 +4870,7 @@ public function test_send_messages_to_conversation_long_text() { ], ]; - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); $writtenmessages = core_message_external::send_messages_to_conversation($gc2->id, $messages); } @@ -4895,7 +4898,7 @@ public function test_get_conversation_no_conversation() { $this->expectException('moodle_exception'); $conv = core_message_external::get_conversation($user1->id, $conversationid + 1); - external_api::clean_returnvalue(core_message_external::get_conversation_returns(), $conv); + \external_api::clean_returnvalue(core_message_external::get_conversation_returns(), $conv); } /** @@ -4923,13 +4926,13 @@ public function test_get_conversation_favourited() { // Get the conversation for user1 and confirm it's favourited. $this->setUser($user1); $conv = core_message_external::get_conversation($user1->id, $conversation->id); - $conv = external_api::clean_returnvalue(core_message_external::get_conversation_returns(), $conv); + $conv = \external_api::clean_returnvalue(core_message_external::get_conversation_returns(), $conv); $this->assertTrue($conv['isfavourite']); // Get the conversation for user2 and confirm it's NOT favourited. $this->setUser($user2); $conv = core_message_external::get_conversation($user2->id, $conversation->id); - $conv = external_api::clean_returnvalue(core_message_external::get_conversation_returns(), $conv); + $conv = \external_api::clean_returnvalue(core_message_external::get_conversation_returns(), $conv); $this->assertFalse($conv['isfavourite']); } @@ -4971,13 +4974,13 @@ public function test_get_conversation_favourited_group_linked() { // Get the conversation for user1 and confirm it's favourited. $this->setUser($user1); $conv = core_message_external::get_conversation($user1->id, $conversationrecord->id); - $conv = external_api::clean_returnvalue(core_message_external::get_conversation_returns(), $conv); + $conv = \external_api::clean_returnvalue(core_message_external::get_conversation_returns(), $conv); $this->assertTrue($conv['isfavourite']); // Get the conversation for user2 and confirm it's NOT favourited. $this->setUser($user2); $conv = core_message_external::get_conversation($user2->id, $conversationrecord->id); - $conv = external_api::clean_returnvalue(core_message_external::get_conversation_returns(), $conv); + $conv = \external_api::clean_returnvalue(core_message_external::get_conversation_returns(), $conv); $this->assertFalse($conv['isfavourite']); } @@ -5004,7 +5007,7 @@ public function test_get_conversation_no_messages() { $this->setUser($user1); $conv = core_message_external::get_conversation($user1->id, $conversationid); - external_api::clean_returnvalue(core_message_external::get_conversation_returns(), $conv); + \external_api::clean_returnvalue(core_message_external::get_conversation_returns(), $conv); $conv = (array) $conv; $this->assertEquals($conversationid, $conv['id']); @@ -5080,7 +5083,7 @@ public function test_get_conversation_with_messages() { 0, true ); - external_api::clean_returnvalue(core_message_external::get_conversation_returns(), $conv); + \external_api::clean_returnvalue(core_message_external::get_conversation_returns(), $conv); $conv = (array) $conv; $this->assertEquals(false, $conv['isread']); @@ -5105,7 +5108,7 @@ public function test_get_conversation_with_messages() { 0, false ); - external_api::clean_returnvalue(core_message_external::get_conversation_returns(), $conv); + \external_api::clean_returnvalue(core_message_external::get_conversation_returns(), $conv); $conv = (array) $conv; $this->assertCount(3, $conv['messages']); @@ -5128,7 +5131,7 @@ public function test_get_conversation_with_messages() { 1, true ); - external_api::clean_returnvalue(core_message_external::get_conversation_returns(), $conv); + \external_api::clean_returnvalue(core_message_external::get_conversation_returns(), $conv); $conv = (array) $conv; $this->assertCount(1, $conv['messages']); @@ -5585,7 +5588,7 @@ public function test_get_conversation_counts( } $counts = core_message_external::get_conversation_counts(...$arguments); - $counts = external_api::clean_returnvalue(core_message_external::get_conversation_counts_returns(), $counts); + $counts = \external_api::clean_returnvalue(core_message_external::get_conversation_counts_returns(), $counts); $this->assertEquals($expectedcounts['favourites'], $counts['favourites']); $this->assertEquals($expectedcounts['types'][\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL], @@ -5671,7 +5674,7 @@ public function test_get_unread_conversation_counts( } $counts = core_message_external::get_unread_conversation_counts(...$arguments); - $counts = external_api::clean_returnvalue(core_message_external::get_unread_conversation_counts_returns(), $counts); + $counts = \external_api::clean_returnvalue(core_message_external::get_unread_conversation_counts_returns(), $counts); $this->assertEquals($expectedunreadcounts['favourites'], $counts['favourites']); $this->assertEquals($expectedunreadcounts['types'][\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL], @@ -5700,13 +5703,13 @@ public function test_delete_message_for_all_users() { // User1 deletes the first message for all users of group conversation. // First, we have to allow user1 (Teacher) can delete messages for all users. $editingteacher = $DB->get_record('role', ['shortname' => 'editingteacher']); - assign_capability('moodle/site:deleteanymessage', CAP_ALLOW, $editingteacher->id, context_system::instance()); + assign_capability('moodle/site:deleteanymessage', CAP_ALLOW, $editingteacher->id, \context_system::instance()); $this->setUser($user1); // Now, user1 deletes message for all users. $return = core_message_external::delete_message_for_all_users($messageid1, $user1->id); - $return = external_api::clean_returnvalue(core_message_external::delete_message_for_all_users_returns(), $return); + $return = \external_api::clean_returnvalue(core_message_external::delete_message_for_all_users_returns(), $return); // Check if everything is ok. $this->assertEquals(array(), $return); @@ -5788,7 +5791,7 @@ public function test_delete_message_for_all_users_private_conversation() { // First, we have to allow user1 (Teacher) can delete messages for all users. $editingteacher = $DB->get_record('role', ['shortname' => 'editingteacher']); - assign_capability('moodle/site:deleteanymessage', CAP_ALLOW, $editingteacher->id, context_system::instance()); + assign_capability('moodle/site:deleteanymessage', CAP_ALLOW, $editingteacher->id, \context_system::instance()); $this->setUser($user1); @@ -5833,7 +5836,7 @@ public function test_get_conversation_messages_timefrom_higher_than_last_timecre $result = core_message_external::get_conversation_messages($user1->id, $conversation->id, 0, 0, '', $time + 5); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(core_message_external::get_conversation_messages_returns(), $result); + $result = \external_api::clean_returnvalue(core_message_external::get_conversation_messages_returns(), $result); // Check the results are correct. $this->assertEquals($conversation->id, $result['id']); @@ -5857,7 +5860,7 @@ protected function create_delete_message_test_data() { // Create a course and enrol the users. $course = $this->getDataGenerator()->create_course(); - $coursecontext = context_course::instance($course->id); + $coursecontext = \context_course::instance($course->id); $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'editingteacher'); $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student'); $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'student'); @@ -5876,7 +5879,7 @@ protected function create_delete_message_test_data() { 'core_group', 'groups', $group1->id, - context_course::instance($course->id)->id + \context_course::instance($course->id)->id ); // Create and individual conversation. diff --git a/mod/assign/tests/externallib_test.php b/mod/assign/tests/externallib_test.php index 29636b3f17c0d..919e650306f54 100644 --- a/mod/assign/tests/externallib_test.php +++ b/mod/assign/tests/externallib_test.php @@ -14,6 +14,13 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +namespace mod_assign; + +use core_user_external; +use externallib_advanced_testcase; +use mod_assign_external; +use mod_assign_testable_assign; + defined('MOODLE_INTERNAL') || die(); global $CFG; @@ -30,7 +37,7 @@ * @copyright 2012 Paul Charsley * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_assign_external_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * Test get_grades @@ -58,9 +65,9 @@ public function test_get_grades() { $enrolid = $DB->insert_record('enrol', $manualenroldata); // Create a teacher and give them capabilities. - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3); - $context = context_module::instance($assign->cmid); + $context = \context_module::instance($assign->cmid); $this->assignUserCapability('mod/assign:viewgrades', $context->id, $roleid); // Create the teacher's enrolment record. @@ -72,7 +79,7 @@ public function test_get_grades() { // Create a student and give them 2 grades (for 2 attempts). $student = self::getDataGenerator()->create_user(); - $submission = new stdClass(); + $submission = new \stdClass(); $submission->assignment = $assign->id; $submission->userid = $student->id; $submission->status = ASSIGN_SUBMISSION_STATUS_NEW; @@ -83,7 +90,7 @@ public function test_get_grades() { $submission->timemodified = time(); $DB->insert_record('assign_submission', $submission); - $grade = new stdClass(); + $grade = new \stdClass(); $grade->assignment = $assign->id; $grade->userid = $student->id; $grade->timecreated = time(); @@ -93,7 +100,7 @@ public function test_get_grades() { $grade->attemptnumber = 0; $DB->insert_record('assign_grades', $grade); - $submission = new stdClass(); + $submission = new \stdClass(); $submission->assignment = $assign->id; $submission->userid = $student->id; $submission->status = ASSIGN_SUBMISSION_STATUS_NEW; @@ -104,7 +111,7 @@ public function test_get_grades() { $submission->timemodified = time(); $DB->insert_record('assign_submission', $submission); - $grade = new stdClass(); + $grade = new \stdClass(); $grade->assignment = $assign->id; $grade->userid = $student->id; $grade->timecreated = time(); @@ -118,7 +125,7 @@ public function test_get_grades() { $result = mod_assign_external::get_grades($assignmentids); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(mod_assign_external::get_grades_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_grades_returns(), $result); // Check that the correct grade information for the student is returned. $this->assertEquals(1, count($result['assignments'])); @@ -144,7 +151,7 @@ public function test_get_assignments() { filter_set_global_state('multilang', TEXTFILTER_ON); filter_set_applies_to_strings('multilang', 1); // Set WS filtering. - $wssettings = external_settings::get_instance(); + $wssettings = \external_settings::get_instance(); $wssettings->set_filter(true); $category = self::getDataGenerator()->create_category(array( @@ -181,7 +188,7 @@ public function test_get_assignments() { )); // Add a file as assignment attachment. - $context = context_module::instance($assign1->cmid); + $context = \context_module::instance($assign1->cmid); $filerecord = array('component' => 'mod_assign', 'filearea' => 'intro', 'contextid' => $context->id, 'itemid' => 0, 'filename' => 'intro.txt', 'filepath' => '/'); $fs = get_file_storage(); @@ -195,9 +202,9 @@ public function test_get_assignments() { )); // Create the user and give them capabilities. - $context = context_course::instance($course1->id); + $context = \context_course::instance($course1->id); $roleid = $this->assignUserCapability('moodle/course:view', $context->id); - $context = context_module::instance($assign1->cmid); + $context = \context_module::instance($assign1->cmid); $this->assignUserCapability('mod/assign:view', $context->id, $roleid); // Create the user enrolment record. @@ -217,7 +224,7 @@ public function test_get_assignments() { $result = mod_assign_external::get_assignments(); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result); // Check the course and assignment are returned. $this->assertEquals(1, count($result['courses'])); @@ -248,7 +255,7 @@ public function test_get_assignments() { $result = mod_assign_external::get_assignments(array($course1->id)); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result); $this->assertEquals(1, count($result['courses'])); $course = $result['courses'][0]; @@ -267,7 +274,7 @@ public function test_get_assignments() { $result = mod_assign_external::get_assignments(array($course2->id)); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result); $this->assertEquals(0, count($result['courses'])); $this->assertEquals(1, count($result['warnings'])); @@ -275,18 +282,18 @@ public function test_get_assignments() { // Test with non-enrolled user, but with view capabilities. $this->setAdminUser(); $result = mod_assign_external::get_assignments(); - $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result); $this->assertEquals(0, count($result['courses'])); $this->assertEquals(0, count($result['warnings'])); // Expect no courses, because we are not using the special flag. $result = mod_assign_external::get_assignments(array($course1->id)); - $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result); $this->assertCount(0, $result['courses']); // Now use the special flag to return courses where you are not enroled in. $result = mod_assign_external::get_assignments(array($course1->id), array(), true); - $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result); $this->assertCount(1, $result['courses']); $course = $result['courses'][0]; @@ -334,7 +341,7 @@ public function test_get_assignments_with_submissionstatement() { $result = mod_assign_external::get_assignments(); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result); // Check that the amount of courses and assignments is right. $this->assertCount(1, $result['courses']); @@ -382,7 +389,7 @@ public function test_get_submissions() { // First attempt. $student = self::getDataGenerator()->create_user(); $teacher = self::getDataGenerator()->create_user(); - $submission = new stdClass(); + $submission = new \stdClass(); $submission->assignment = $assign1->id; $submission->userid = $student->id; $submission->timecreated = time(); @@ -393,7 +400,7 @@ public function test_get_submissions() { $sid = $DB->insert_record('assign_submission', $submission); // Second attempt. - $submission = new stdClass(); + $submission = new \stdClass(); $submission->assignment = $assign1->id; $submission->userid = $student->id; $submission->timecreated = time(); @@ -404,7 +411,7 @@ public function test_get_submissions() { $sid = $DB->insert_record('assign_submission', $submission); $submission->id = $sid; - $onlinetextsubmission = new stdClass(); + $onlinetextsubmission = new \stdClass(); $onlinetextsubmission->onlinetext = "

online test text

"; $onlinetextsubmission->onlineformat = 1; $onlinetextsubmission->submission = $submission->id; @@ -418,7 +425,7 @@ public function test_get_submissions() { $assignmentids[] = $assign1->id; $result = mod_assign_external::get_submissions($assignmentids); - $result = external_api::clean_returnvalue(mod_assign_external::get_submissions_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_submissions_returns(), $result); // Check the online text submission is NOT returned because the student is not yet enrolled in the course. $this->assertEquals(1, count($result['assignments'])); @@ -431,7 +438,7 @@ public function test_get_submissions() { $this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id); $result = mod_assign_external::get_submissions($assignmentids); - $result = external_api::clean_returnvalue(mod_assign_external::get_submissions_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_submissions_returns(), $result); $this->assertEquals(1, count($result['assignments'])); $assignment = $result['assignments'][0]; @@ -445,13 +452,13 @@ public function test_get_submissions() { // Test locking the context. set_config('contextlocking', 1); - $context = context_course::instance($course1->id); + $context = \context_course::instance($course1->id); $context->set_locked(true); $this->setUser($teacher); $assignmentids[] = $assign1->id; $result = mod_assign_external::get_submissions($assignmentids); - $result = external_api::clean_returnvalue(mod_assign_external::get_submissions_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_submissions_returns(), $result); $this->assertEquals(1, count($result['assignments'])); } @@ -471,11 +478,11 @@ public function test_get_submissions_group_submission() { $student = $result['student']; $teacher = $result['teacher']; $course = $result['course']; - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $teacherrole = $DB->get_record('role', array('shortname' => 'teacher')); $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id)); $cm = get_coursemodule_from_instance('assign', $assignmodule->id); - $context = context_module::instance($cm->id); + $context = \context_module::instance($cm->id); $assign = new mod_assign_testable_assign($context, $cm, $course); groups_add_member($group, $student); @@ -488,7 +495,7 @@ public function test_get_submissions_group_submission() { $assignmentids[] = $assignmodule->id; $result = mod_assign_external::get_submissions($assignmentids); - $result = external_api::clean_returnvalue(mod_assign_external::get_submissions_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_submissions_returns(), $result); $this->assertEquals(1, count($result['assignments'])); $assignment = $result['assignments'][0]; @@ -568,7 +575,7 @@ public function test_get_submissions_group_submission_attemptnumber() { $this->setUser($teacher); $result = mod_assign_external::get_submissions([$assignmodule->id]); - $result = external_api::clean_returnvalue(mod_assign_external::get_submissions_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_submissions_returns(), $result); $this->assertEquals(1, count($result['assignments'])); [$assignment] = $result['assignments']; @@ -607,9 +614,9 @@ public function test_get_user_flags() { $enrolid = $DB->insert_record('enrol', $manualenroldata); // Create a teacher and give them capabilities. - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3); - $context = context_module::instance($assign->cmid); + $context = \context_module::instance($assign->cmid); $this->assignUserCapability('mod/assign:grade', $context->id, $roleid); // Create the teacher's enrolment record. @@ -620,7 +627,7 @@ public function test_get_user_flags() { // Create a student and give them a user flag record. $student = self::getDataGenerator()->create_user(); - $userflag = new stdClass(); + $userflag = new \stdClass(); $userflag->assignment = $assign->id; $userflag->userid = $student->id; $userflag->locked = 0; @@ -635,7 +642,7 @@ public function test_get_user_flags() { $result = mod_assign_external::get_user_flags($assignmentids); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(mod_assign_external::get_user_flags_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_user_flags_returns(), $result); // Check that the correct user flag information for the student is returned. $this->assertEquals(1, count($result['assignments'])); @@ -678,9 +685,9 @@ public function test_get_user_mappings() { $enrolid = $DB->insert_record('enrol', $manualenroldata); // Create a teacher and give them capabilities. - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3); - $context = context_module::instance($assign->cmid); + $context = \context_module::instance($assign->cmid); $this->assignUserCapability('mod/assign:revealidentities', $context->id, $roleid); // Create the teacher's enrolment record. @@ -691,7 +698,7 @@ public function test_get_user_mappings() { // Create a student and give them a user mapping record. $student = self::getDataGenerator()->create_user(); - $mapping = new stdClass(); + $mapping = new \stdClass(); $mapping->assignment = $assign->id; $mapping->userid = $student->id; @@ -701,7 +708,7 @@ public function test_get_user_mappings() { $result = mod_assign_external::get_user_mappings($assignmentids); // We need to execute the return values cleaning process to simulate the web service server. - $result = external_api::clean_returnvalue(mod_assign_external::get_user_mappings_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_user_mappings_returns(), $result); // Check that the correct user mapping information for the student is returned. $this->assertEquals(1, count($result['assignments'])); @@ -728,9 +735,9 @@ public function test_lock_submissions() { $params['assignsubmission_onlinetext_enabled'] = 1; $instance = $generator->create_instance($params); $cm = get_coursemodule_from_instance('assign', $instance->id); - $context = context_module::instance($cm->id); + $context = \context_module::instance($cm->id); - $assign = new assign($context, $cm, $course); + $assign = new \assign($context, $cm, $course); $student1 = self::getDataGenerator()->create_user(); $student2 = self::getDataGenerator()->create_user(); @@ -745,7 +752,7 @@ public function test_lock_submissions() { // Simulate a submission. $this->setUser($student1); $submission = $assign->get_user_submission($student1->id, true); - $data = new stdClass(); + $data = new \stdClass(); $data->onlinetext_editor = array( 'itemid' => file_get_unused_draft_itemid(), 'text' => 'Submission text', @@ -757,20 +764,20 @@ public function test_lock_submissions() { $this->setUser($teacher); $students = array($student1->id, $student2->id); $result = mod_assign_external::lock_submissions($instance->id, $students); - $result = external_api::clean_returnvalue(mod_assign_external::lock_submissions_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::lock_submissions_returns(), $result); // Check for 0 warnings. $this->assertEquals(0, count($result)); $this->setUser($student2); $submission = $assign->get_user_submission($student2->id, true); - $data = new stdClass(); + $data = new \stdClass(); $data->onlinetext_editor = array( 'itemid' => file_get_unused_draft_itemid(), 'text' => 'Submission text', 'format' => FORMAT_MOODLE); $notices = array(); - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); $assign->save_submission($data, $notices); } @@ -789,9 +796,9 @@ public function test_unlock_submissions() { $params['assignsubmission_onlinetext_enabled'] = 1; $instance = $generator->create_instance($params); $cm = get_coursemodule_from_instance('assign', $instance->id); - $context = context_module::instance($cm->id); + $context = \context_module::instance($cm->id); - $assign = new assign($context, $cm, $course); + $assign = new \assign($context, $cm, $course); $student1 = self::getDataGenerator()->create_user(); $student2 = self::getDataGenerator()->create_user(); @@ -806,7 +813,7 @@ public function test_unlock_submissions() { // Simulate a submission. $this->setUser($student1); $submission = $assign->get_user_submission($student1->id, true); - $data = new stdClass(); + $data = new \stdClass(); $data->onlinetext_editor = array( 'itemid' => file_get_unused_draft_itemid(), 'text' => 'Submission text', @@ -818,20 +825,20 @@ public function test_unlock_submissions() { $this->setUser($teacher); $students = array($student1->id, $student2->id); $result = mod_assign_external::lock_submissions($instance->id, $students); - $result = external_api::clean_returnvalue(mod_assign_external::lock_submissions_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::lock_submissions_returns(), $result); // Check for 0 warnings. $this->assertEquals(0, count($result)); $result = mod_assign_external::unlock_submissions($instance->id, $students); - $result = external_api::clean_returnvalue(mod_assign_external::unlock_submissions_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::unlock_submissions_returns(), $result); // Check for 0 warnings. $this->assertEquals(0, count($result)); $this->setUser($student2); $submission = $assign->get_user_submission($student2->id, true); - $data = new stdClass(); + $data = new \stdClass(); $data->onlinetext_editor = array( 'itemid' => file_get_unused_draft_itemid(), 'text' => 'Submission text', @@ -859,9 +866,9 @@ public function test_submit_for_grading() { $params['requiresubmissionstatement'] = 1; $instance = $generator->create_instance($params); $cm = get_coursemodule_from_instance('assign', $instance->id); - $context = context_module::instance($cm->id); + $context = \context_module::instance($cm->id); - $assign = new assign($context, $cm, $course); + $assign = new \assign($context, $cm, $course); $student1 = self::getDataGenerator()->create_user(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); @@ -871,7 +878,7 @@ public function test_submit_for_grading() { // Simulate a submission. $this->setUser($student1); $submission = $assign->get_user_submission($student1->id, true); - $data = new stdClass(); + $data = new \stdClass(); $data->onlinetext_editor = array( 'itemid' => file_get_unused_draft_itemid(), 'text' => 'Submission text', @@ -880,13 +887,13 @@ public function test_submit_for_grading() { $plugin->save($submission, $data); $result = mod_assign_external::submit_for_grading($instance->id, false); - $result = external_api::clean_returnvalue(mod_assign_external::submit_for_grading_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::submit_for_grading_returns(), $result); // Should be 1 fail because the submission statement was not aceptted. $this->assertEquals(1, count($result)); $result = mod_assign_external::submit_for_grading($instance->id, true); - $result = external_api::clean_returnvalue(mod_assign_external::submit_for_grading_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::submit_for_grading_returns(), $result); // Check for 0 warnings. $this->assertEquals(0, count($result)); @@ -923,9 +930,9 @@ public function test_save_user_extensions() { $params['cutoffdate'] = $now - 10; $instance = $generator->create_instance($params); $cm = get_coursemodule_from_instance('assign', $instance->id); - $context = context_module::instance($cm->id); + $context = \context_module::instance($cm->id); - $assign = new assign($context, $cm, $course); + $assign = new \assign($context, $cm, $course); $student1 = self::getDataGenerator()->create_user(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); @@ -933,34 +940,34 @@ public function test_save_user_extensions() { $this->setUser($student1); $result = mod_assign_external::submit_for_grading($instance->id, true); - $result = external_api::clean_returnvalue(mod_assign_external::submit_for_grading_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::submit_for_grading_returns(), $result); // Check for 0 warnings. $this->assertEquals(1, count($result)); $this->setUser($teacher); $result = mod_assign_external::save_user_extensions($instance->id, array($student1->id), array($now, $tomorrow)); - $result = external_api::clean_returnvalue(mod_assign_external::save_user_extensions_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::save_user_extensions_returns(), $result); $this->assertEquals(1, count($result)); $this->setUser($teacher); $result = mod_assign_external::save_user_extensions($instance->id, array($student1->id), array($yesterday - 10)); - $result = external_api::clean_returnvalue(mod_assign_external::save_user_extensions_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::save_user_extensions_returns(), $result); $this->assertEquals(1, count($result)); $this->setUser($teacher); $result = mod_assign_external::save_user_extensions($instance->id, array($student1->id), array($tomorrow)); - $result = external_api::clean_returnvalue(mod_assign_external::save_user_extensions_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::save_user_extensions_returns(), $result); $this->assertEquals(0, count($result)); $this->setUser($student1); $result = mod_assign_external::submit_for_grading($instance->id, true); - $result = external_api::clean_returnvalue(mod_assign_external::submit_for_grading_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::submit_for_grading_returns(), $result); $this->assertEquals(0, count($result)); $this->setUser($student1); $result = mod_assign_external::save_user_extensions($instance->id, array($student1->id), array($now, $tomorrow)); - $result = external_api::clean_returnvalue(mod_assign_external::save_user_extensions_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::save_user_extensions_returns(), $result); } /** @@ -985,24 +992,24 @@ public function test_reveal_identities() { $params['blindmarking'] = 1; $instance = $generator->create_instance($params); $cm = get_coursemodule_from_instance('assign', $instance->id); - $context = context_module::instance($cm->id); + $context = \context_module::instance($cm->id); - $assign = new assign($context, $cm, $course); + $assign = new \assign($context, $cm, $course); $student1 = self::getDataGenerator()->create_user(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $this->getDataGenerator()->enrol_user($student1->id, $course->id, $studentrole->id); $this->setUser($student1); - $this->expectException(required_capability_exception::class); + $this->expectException(\required_capability_exception::class); $result = mod_assign_external::reveal_identities($instance->id); - $result = external_api::clean_returnvalue(mod_assign_external::reveal_identities_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::reveal_identities_returns(), $result); $this->assertEquals(1, count($result)); $this->assertEquals(true, $assign->is_blind_marking()); $this->setUser($teacher); $result = mod_assign_external::reveal_identities($instance->id); - $result = external_api::clean_returnvalue(mod_assign_external::reveal_identities_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::reveal_identities_returns(), $result); $this->assertEquals(0, count($result)); $this->assertEquals(false, $assign->is_blind_marking()); @@ -1013,11 +1020,11 @@ public function test_reveal_identities() { $params['blindmarking'] = 0; $instance = $generator->create_instance($params); $cm = get_coursemodule_from_instance('assign', $instance->id); - $context = context_module::instance($cm->id); + $context = \context_module::instance($cm->id); - $assign = new assign($context, $cm, $course); + $assign = new \assign($context, $cm, $course); $result = mod_assign_external::reveal_identities($instance->id); - $result = external_api::clean_returnvalue(mod_assign_external::reveal_identities_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::reveal_identities_returns(), $result); $this->assertEquals(1, count($result)); $this->assertEquals(false, $assign->is_blind_marking()); @@ -1040,9 +1047,9 @@ public function test_revert_submissions_to_draft() { $params['submissiondrafts'] = 1; $instance = $generator->create_instance($params); $cm = get_coursemodule_from_instance('assign', $instance->id); - $context = context_module::instance($cm->id); + $context = \context_module::instance($cm->id); - $assign = new assign($context, $cm, $course); + $assign = new \assign($context, $cm, $course); $student1 = self::getDataGenerator()->create_user(); $student2 = self::getDataGenerator()->create_user(); @@ -1057,14 +1064,14 @@ public function test_revert_submissions_to_draft() { // Simulate a submission. $this->setUser($student1); $result = mod_assign_external::submit_for_grading($instance->id, true); - $result = external_api::clean_returnvalue(mod_assign_external::submit_for_grading_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::submit_for_grading_returns(), $result); $this->assertEquals(0, count($result)); // Ready to test. $this->setUser($teacher); $students = array($student1->id, $student2->id); $result = mod_assign_external::revert_submissions_to_draft($instance->id, array($student1->id)); - $result = external_api::clean_returnvalue(mod_assign_external::revert_submissions_to_draft_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::revert_submissions_to_draft_returns(), $result); // Check for 0 warnings. $this->assertEquals(0, count($result)); @@ -1093,9 +1100,9 @@ public function test_save_submission() { $params['assignsubmission_file_maxsizebytes'] = 1024 * 1024; $instance = $generator->create_instance($params); $cm = get_coursemodule_from_instance('assign', $instance->id); - $context = context_module::instance($cm->id); + $context = \context_module::instance($cm->id); - $assign = new assign($context, $cm, $course); + $assign = new \assign($context, $cm, $course); $student1 = self::getDataGenerator()->create_user(); $student2 = self::getDataGenerator()->create_user(); @@ -1109,7 +1116,7 @@ public function test_save_submission() { // Create a file in a draft area. $draftidfile = file_get_unused_draft_itemid(); - $usercontext = context_user::instance($student1->id); + $usercontext = \context_user::instance($student1->id); $filerecord = array( 'contextid' => $usercontext->id, 'component' => 'user', @@ -1145,7 +1152,7 @@ public function test_save_submission() { 'itemid' => $draftidonlinetext); $submissionpluginparams['onlinetext_editor'] = $onlinetexteditorparams; $result = mod_assign_external::save_submission($instance->id, $submissionpluginparams); - $result = external_api::clean_returnvalue(mod_assign_external::save_submission_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::save_submission_returns(), $result); $this->assertEquals(0, count($result)); @@ -1155,7 +1162,7 @@ public function test_save_submission() { $DB->update_record('assign', $instance); $result = mod_assign_external::save_submission($instance->id, $submissionpluginparams); - $result = external_api::clean_returnvalue(mod_assign_external::save_submission_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::save_submission_returns(), $result); $this->assertCount(1, $result); $this->assertEquals(get_string('duedatereached', 'assign'), $result[0]['item']); @@ -1182,9 +1189,9 @@ public function test_save_grade() { $params['assignfeedback_comments_enabled'] = 1; $instance = $generator->create_instance($params); $cm = get_coursemodule_from_instance('assign', $instance->id); - $context = context_module::instance($cm->id); + $context = \context_module::instance($cm->id); - $assign = new assign($context, $cm, $course); + $assign = new \assign($context, $cm, $course); $student1 = self::getDataGenerator()->create_user(); $student2 = self::getDataGenerator()->create_user(); @@ -1201,7 +1208,7 @@ public function test_save_grade() { // Create a file in a draft area. $draftidfile = file_get_unused_draft_itemid(); - $usercontext = context_user::instance($teacher->id); + $usercontext = \context_user::instance($teacher->id); $filerecord = array( 'contextid' => $usercontext->id, 'component' => 'user', @@ -1233,7 +1240,7 @@ public function test_save_grade() { $this->assertNull($result); $result = mod_assign_external::get_grades(array($instance->id)); - $result = external_api::clean_returnvalue(mod_assign_external::get_grades_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_grades_returns(), $result); $this->assertEquals((float)$result['assignments'][0]['grades'][0]['grade'], '50.0'); } @@ -1258,9 +1265,9 @@ public function test_save_grades_with_advanced_grading() { $params['assignfeedback_comments_enabled'] = 0; $instance = $generator->create_instance($params); $cm = get_coursemodule_from_instance('assign', $instance->id); - $context = context_module::instance($cm->id); + $context = \context_module::instance($cm->id); - $assign = new assign($context, $cm, $course); + $assign = new \assign($context, $cm, $course); $student1 = self::getDataGenerator()->create_user(); $student2 = self::getDataGenerator()->create_user(); @@ -1428,9 +1435,9 @@ public function test_save_grades_with_group_submission() { $params['teamsubmissiongroupingid'] = $grouping->id; $instance = $generator->create_instance($params); $cm = get_coursemodule_from_instance('assign', $instance->id); - $context = context_module::instance($cm->id); + $context = \context_module::instance($cm->id); - $assign = new assign($context, $cm, $course); + $assign = new \assign($context, $cm, $course); $student1 = self::getDataGenerator()->create_user(); $student2 = self::getDataGenerator()->create_user(); @@ -1473,9 +1480,9 @@ public function test_save_grades_with_group_submission() { $grades1[] = $student2gradeinfo; // Expect an exception since 2 grades have been submitted for the same team. - $this->expectException(invalid_parameter_exception::class); + $this->expectException(\invalid_parameter_exception::class); $result = mod_assign_external::save_grades($instance->id, true, $grades1); - $result = external_api::clean_returnvalue(mod_assign_external::save_grades_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::save_grades_returns(), $result); $grades2 = array(); $student3gradeinfo = array(); @@ -1496,7 +1503,7 @@ public function test_save_grades_with_group_submission() { $student4gradeinfo['plugindata'] = $feedbackpluginparams; $grades2[] = $student4gradeinfo; $result = mod_assign_external::save_grades($instance->id, true, $grades2); - $result = external_api::clean_returnvalue(mod_assign_external::save_grades_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::save_grades_returns(), $result); // There should be no warnings. $this->assertEquals(0, count($result)); @@ -1537,9 +1544,9 @@ public function test_copy_previous_attempt() { $params['maxattempts'] = 5; $instance = $generator->create_instance($params); $cm = get_coursemodule_from_instance('assign', $instance->id); - $context = context_module::instance($cm->id); + $context = \context_module::instance($cm->id); - $assign = new assign($context, $cm, $course); + $assign = new \assign($context, $cm, $course); $student1 = self::getDataGenerator()->create_user(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); @@ -1552,7 +1559,7 @@ public function test_copy_previous_attempt() { $submissionpluginparams['onlinetext_editor'] = $onlinetexteditorparams; $submissionpluginparams['files_filemanager'] = file_get_unused_draft_itemid(); $result = mod_assign_external::save_submission($instance->id, $submissionpluginparams); - $result = external_api::clean_returnvalue(mod_assign_external::save_submission_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::save_submission_returns(), $result); $this->setUser($teacher); // Add a grade and reopen the attempt. @@ -1575,13 +1582,13 @@ public function test_copy_previous_attempt() { $this->setUser($student1); // Now copy the previous attempt. $result = mod_assign_external::copy_previous_attempt($instance->id); - $result = external_api::clean_returnvalue(mod_assign_external::copy_previous_attempt_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::copy_previous_attempt_returns(), $result); // No warnings. $this->assertEquals(0, count($result)); $this->setUser($teacher); $result = mod_assign_external::get_submissions(array($instance->id)); - $result = external_api::clean_returnvalue(mod_assign_external::get_submissions_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_submissions_returns(), $result); // Check we are now on the second attempt. $this->assertEquals($result['assignments'][0]['submissions'][0]['attemptnumber'], 1); @@ -1616,9 +1623,9 @@ public function test_set_user_flags() { $enrolid = $DB->insert_record('enrol', $manualenroldata); // Create a teacher and give them capabilities. - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3); - $context = context_module::instance($assign->cmid); + $context = \context_module::instance($assign->cmid); $this->assignUserCapability('mod/assign:grade', $context->id, $roleid); // Create the teacher's enrolment record. @@ -1639,7 +1646,7 @@ public function test_set_user_flags() { $createduserflags = mod_assign_external::set_user_flags($assign->id, $userflags); // We need to execute the return values cleaning process to simulate the web service server. - $createduserflags = external_api::clean_returnvalue(mod_assign_external::set_user_flags_returns(), $createduserflags); + $createduserflags = \external_api::clean_returnvalue(mod_assign_external::set_user_flags_returns(), $createduserflags); $this->assertEquals($student->id, $createduserflags[0]['userid']); $createduserflag = $DB->get_record('assign_user_flags', array('id' => $createduserflags[0]['id'])); @@ -1661,7 +1668,7 @@ public function test_set_user_flags() { $updateduserflags = mod_assign_external::set_user_flags($assign->id, $userflags); // We need to execute the return values cleaning process to simulate the web service server. - $updateduserflags = external_api::clean_returnvalue(mod_assign_external::set_user_flags_returns(), $updateduserflags); + $updateduserflags = \external_api::clean_returnvalue(mod_assign_external::set_user_flags_returns(), $updateduserflags); $this->assertEquals($student->id, $updateduserflags[0]['userid']); $updateduserflag = $DB->get_record('assign_user_flags', array('id' => $updateduserflags[0]['id'])); @@ -1687,11 +1694,11 @@ public function test_view_grading_table_invalid_instance() { // Setup test data. $course = $this->getDataGenerator()->create_course(); $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id)); - $context = context_module::instance($assign->cmid); + $context = \context_module::instance($assign->cmid); $cm = get_coursemodule_from_instance('assign', $assign->id); // Test invalid instance id. - $this->expectException(dml_missing_record_exception::class); + $this->expectException(\dml_missing_record_exception::class); mod_assign_external::view_grading_table(0); } @@ -1706,14 +1713,14 @@ public function test_view_grading_table_not_enrolled() { // Setup test data. $course = $this->getDataGenerator()->create_course(); $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id)); - $context = context_module::instance($assign->cmid); + $context = \context_module::instance($assign->cmid); $cm = get_coursemodule_from_instance('assign', $assign->id); // Test not-enrolled user. $user = self::getDataGenerator()->create_user(); $this->setUser($user); - $this->expectException(require_login_exception::class); + $this->expectException(\require_login_exception::class); mod_assign_external::view_grading_table($assign->id); } @@ -1728,7 +1735,7 @@ public function test_view_grading_table_correct() { // Setup test data. $course = $this->getDataGenerator()->create_course(); $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id)); - $context = context_module::instance($assign->cmid); + $context = \context_module::instance($assign->cmid); $cm = get_coursemodule_from_instance('assign', $assign->id); // Test user with full capabilities. @@ -1741,7 +1748,7 @@ public function test_view_grading_table_correct() { $sink = $this->redirectEvents(); $result = mod_assign_external::view_grading_table($assign->id); - $result = external_api::clean_returnvalue(mod_assign_external::view_grading_table_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::view_grading_table_returns(), $result); $events = $sink->get_events(); $this->assertCount(1, $events); @@ -1767,7 +1774,7 @@ public function test_view_grading_table_without_capability() { // Setup test data. $course = $this->getDataGenerator()->create_course(); $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id)); - $context = context_module::instance($assign->cmid); + $context = \context_module::instance($assign->cmid); $cm = get_coursemodule_from_instance('assign', $assign->id); // Test user with no capabilities. @@ -1781,9 +1788,9 @@ public function test_view_grading_table_without_capability() { assign_capability('mod/assign:view', CAP_PROHIBIT, $teacherrole->id, $context->id); // Empty all the caches that may be affected by this change. accesslib_clear_all_caches_for_unit_testing(); - course_modinfo::clear_instance_cache(); + \course_modinfo::clear_instance_cache(); - $this->expectException(require_login_exception::class); + $this->expectException(\require_login_exception::class); $this->expectExceptionMessage('Course or activity not accessible. (Activity is hidden)'); mod_assign_external::view_grading_table($assign->id); } @@ -1798,7 +1805,7 @@ public function test_subplugins_availability() { $this->resetAfterTest(true); // Hide assignment file submissiong plugin. - $pluginmanager = new assign_plugin_manager('assignsubmission'); + $pluginmanager = new \assign_plugin_manager('assignsubmission'); $pluginmanager->hide_plugin('file'); $parameters = mod_assign_external::save_submission_parameters(); @@ -1811,7 +1818,7 @@ public function test_subplugins_availability() { $this->assertEquals(VALUE_OPTIONAL, $parameters->keys['plugindata']->keys['files_filemanager']->required); // Hide feedback file submissiong plugin. - $pluginmanager = new assign_plugin_manager('assignfeedback'); + $pluginmanager = new \assign_plugin_manager('assignfeedback'); $pluginmanager->hide_plugin('file'); $parameters = mod_assign_external::save_grade_parameters(); @@ -1843,14 +1850,14 @@ public function test_view_submission_status() { // Setup test data. $course = $this->getDataGenerator()->create_course(); $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id)); - $context = context_module::instance($assign->cmid); + $context = \context_module::instance($assign->cmid); $cm = get_coursemodule_from_instance('assign', $assign->id); // Test invalid instance id. try { mod_assign_external::view_submission_status(0); $this->fail('Exception expected due to invalid mod_assign instance id.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('invalidrecord', $e->errorcode); } @@ -1860,7 +1867,7 @@ public function test_view_submission_status() { try { mod_assign_external::view_submission_status($assign->id); $this->fail('Exception expected due to not enrolled user.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } @@ -1872,7 +1879,7 @@ public function test_view_submission_status() { $sink = $this->redirectEvents(); $result = mod_assign_external::view_submission_status($assign->id); - $result = external_api::clean_returnvalue(mod_assign_external::view_submission_status_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::view_submission_status_returns(), $result); $events = $sink->get_events(); $this->assertCount(1, $events); @@ -1890,12 +1897,12 @@ public function test_view_submission_status() { // We need a explicit prohibit since this capability is only defined in authenticated user and guest roles. assign_capability('mod/assign:view', CAP_PROHIBIT, $studentrole->id, $context->id); accesslib_clear_all_caches_for_unit_testing(); - course_modinfo::clear_instance_cache(); + \course_modinfo::clear_instance_cache(); try { mod_assign_external::view_submission_status($assign->id); $this->fail('Exception expected due to missing capability.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } } @@ -1932,7 +1939,7 @@ private function create_submission_for_testing_status($submitforgrading = false) $instance = $generator->create_instance($params); $cm = get_coursemodule_from_instance('assign', $instance->id); - $context = context_module::instance($cm->id); + $context = \context_module::instance($cm->id); $assign = new mod_assign_testable_assign($context, $cm, $course); @@ -1956,14 +1963,14 @@ private function create_submission_for_testing_status($submitforgrading = false) // Simulate a submission. $submission = $assign->get_user_submission($student1->id, true); - $data = new stdClass(); + $data = new \stdClass(); $data->onlinetext_editor = array( 'itemid' => file_get_unused_draft_itemid(), 'text' => 'Submission text with a link', 'format' => FORMAT_MOODLE); $draftidfile = file_get_unused_draft_itemid(); - $usercontext = context_user::instance($student1->id); + $usercontext = \context_user::instance($student1->id); $filerecord = array( 'contextid' => $usercontext->id, 'component' => 'user', @@ -1984,7 +1991,7 @@ private function create_submission_for_testing_status($submitforgrading = false) // Now, submit the draft for grading. $notices = array(); - $data = new stdClass; + $data = new \stdClass; $data->userid = $student1->id; $assign->submit_for_grading($data, $notices); } @@ -2005,7 +2012,7 @@ public function test_get_submission_status_in_draft_status() { // We expect debugging because of the $PAGE object, this won't happen in a normal WS request. $this->assertDebuggingCalled(); - $result = external_api::clean_returnvalue(mod_assign_external::get_submission_status_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_submission_status_returns(), $result); // The submission is now in draft mode. $this->assertCount(0, $result['warnings']); @@ -2059,7 +2066,7 @@ public function test_get_submission_status_in_submission_status() { $result = mod_assign_external::get_submission_status($assign->get_instance()->id); // We expect debugging because of the $PAGE object, this won't happen in a normal WS request. $this->assertDebuggingCalled(); - $result = external_api::clean_returnvalue(mod_assign_external::get_submission_status_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_submission_status_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertFalse(isset($result['gradingsummary'])); @@ -2093,7 +2100,7 @@ public function test_get_submission_status_in_submission_status_for_teacher() { $result = mod_assign_external::get_submission_status($assign->get_instance()->id, 0, $g1->id); // We expect debugging because of the $PAGE object, this won't happen in a normal WS request. $this->assertDebuggingCalled(); - $result = external_api::clean_returnvalue(mod_assign_external::get_submission_status_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_submission_status_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertFalse(isset($result['lastattempt'])); @@ -2110,7 +2117,7 @@ public function test_get_submission_status_in_submission_status_for_teacher() { // Second group. $result = mod_assign_external::get_submission_status($assign->get_instance()->id, 0, $g2->id); - $result = external_api::clean_returnvalue(mod_assign_external::get_submission_status_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_submission_status_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertEquals(1, $result['gradingsummary']['participantcount']); $this->assertEquals(0, $result['gradingsummary']['submissionssubmittedcount']); // G2 students didn't submit yet. @@ -2118,18 +2125,18 @@ public function test_get_submission_status_in_submission_status_for_teacher() { // Should not return information for all users (missing access to all groups capability for non-editing teacher). $result = mod_assign_external::get_submission_status($assign->get_instance()->id); - $result = external_api::clean_returnvalue(mod_assign_external::get_submission_status_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_submission_status_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertFalse(isset($result['gradingsummary'])); // Should return all participants if we grant accessallgroups capability to the normal teacher role. - $context = context_course::instance($assign->get_instance()->course); + $context = \context_course::instance($assign->get_instance()->course); $teacherrole = $DB->get_record('role', array('shortname' => 'teacher')); assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $teacherrole->id, $context->id, true); accesslib_clear_all_caches_for_unit_testing(); $result = mod_assign_external::get_submission_status($assign->get_instance()->id); - $result = external_api::clean_returnvalue(mod_assign_external::get_submission_status_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_submission_status_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertEquals(2, $result['gradingsummary']['participantcount']); $this->assertEquals(0, $result['gradingsummary']['submissiondraftscount']); @@ -2140,7 +2147,7 @@ public function test_get_submission_status_in_submission_status_for_teacher() { list($assign, $instance, $student1, $student2, $teacher, $g1, $g2) = $this->create_submission_for_testing_status(false); $this->setUser($teacher); $result = mod_assign_external::get_submission_status($assign->get_instance()->id, 0, $g1->id); - $result = external_api::clean_returnvalue(mod_assign_external::get_submission_status_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_submission_status_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertEquals(1, $result['gradingsummary']['participantcount']); $this->assertEquals(1, $result['gradingsummary']['submissiondraftscount']); // We have a draft submission. @@ -2183,7 +2190,7 @@ public function test_get_submission_status_in_reopened_status() { $result = mod_assign_external::get_submission_status($assign->get_instance()->id); // We expect debugging because of the $PAGE object, this won't happen in a normal WS request. $this->assertDebuggingCalled(); - $result = external_api::clean_returnvalue(mod_assign_external::get_submission_status_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_submission_status_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertFalse(isset($result['gradingsummary'])); @@ -2253,7 +2260,7 @@ public function test_get_submission_status_access_control() { $this->setUser($student2); // Access control test. - $this->expectException(required_capability_exception::class); + $this->expectException(\required_capability_exception::class); mod_assign_external::get_submission_status($assign->get_instance()->id, $student1->id); } @@ -2269,7 +2276,7 @@ public function test_get_submission_status_hidden_grader() { // Grade the assign for the student1. $this->setUser($teacher); - $data = new stdClass(); + $data = new \stdClass(); $data->grade = '50.0'; $data->assignfeedbackcomments_editor = ['text' => '']; $assign->testable_apply_grade_to_user($data, $student1->id, 0); @@ -2281,7 +2288,7 @@ public function test_get_submission_status_hidden_grader() { // We expect debugging because of the $PAGE object, this won't happen in a normal WS request. $this->assertDebuggingCalled(); - $result = external_api::clean_returnvalue(mod_assign_external::get_submission_status_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_submission_status_returns(), $result); $this->assertTrue(isset($result['feedback'])); $this->assertTrue(isset($result['feedback']['grade'])); @@ -2299,7 +2306,7 @@ public function test_get_submission_status_hidden_grader() { // Check that the student cannot see the grader anymore. $result = mod_assign_external::get_submission_status($assign->get_instance()->id); - $result = external_api::clean_returnvalue(mod_assign_external::get_submission_status_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_submission_status_returns(), $result); $this->assertTrue(isset($result['feedback'])); $this->assertTrue(isset($result['feedback']['grade'])); @@ -2309,7 +2316,7 @@ public function test_get_submission_status_hidden_grader() { $this->setUser($teacher); $result = mod_assign_external::get_submission_status($assign->get_instance()->id, $student1->id); - $result = external_api::clean_returnvalue(mod_assign_external::get_submission_status_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_submission_status_returns(), $result); $this->assertTrue(isset($result['feedback'])); $this->assertTrue(isset($result['feedback']['grade'])); @@ -2335,7 +2342,7 @@ public function test_get_submission_status_with_override() { $result = mod_assign_external::get_submission_status($assign->get_instance()->id); // We expect debugging because of the $PAGE object, this won't happen in a normal WS request. $this->assertDebuggingCalled(); - $result = external_api::clean_returnvalue(mod_assign_external::get_submission_status_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_submission_status_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertFalse(isset($result['gradingsummary'])); @@ -2356,7 +2363,7 @@ public function test_get_submission_status_with_override() { $this->setUser($student2); $result = mod_assign_external::get_submission_status($assign->get_instance()->id); - $result = external_api::clean_returnvalue(mod_assign_external::get_submission_status_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_submission_status_returns(), $result); // The submission is now in draft mode. $this->assertCount(0, $result['warnings']); @@ -2380,7 +2387,7 @@ public function test_get_submission_status_with_override() { */ public function test_get_participant_no_assignment() { $this->resetAfterTest(true); - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); mod_assign_external::get_participant('-1', '-1', false); } @@ -2396,13 +2403,13 @@ public function test_get_participant_no_view_capability() { $assign = $result['assign']; $student = $result['student']; $course = $result['course']; - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $this->setUser($student); assign_capability('mod/assign:view', CAP_PROHIBIT, $studentrole->id, $context->id, true); - $this->expectException(require_login_exception::class); + $this->expectException(\require_login_exception::class); mod_assign_external::get_participant($assign->id, $student->id, false); } @@ -2419,7 +2426,7 @@ public function test_get_participant_no_grade_capability() { $student = $result['student']; $teacher = $result['teacher']; $course = $result['course']; - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $teacherrole = $DB->get_record('role', array('shortname' => 'teacher')); $this->setUser($teacher); @@ -2427,7 +2434,7 @@ public function test_get_participant_no_grade_capability() { assign_capability('mod/assign:grade', CAP_PROHIBIT, $teacherrole->id, $context->id, true); accesslib_clear_all_caches_for_unit_testing(); - $this->expectException(required_capability_exception::class); + $this->expectException(\required_capability_exception::class); mod_assign_external::get_participant($assign->id, $student->id, false); } @@ -2445,9 +2452,9 @@ public function test_get_participant_no_participant() { $this->setUser($teacher); - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); $result = mod_assign_external::get_participant($assign->id, $student->id, false); - $result = external_api::clean_returnvalue(mod_assign_external::get_participant_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_participant_returns(), $result); } /** @@ -2463,13 +2470,13 @@ public function test_get_participant_blind_marking() { $student = $result['student']; $teacher = $result['teacher']; $course = $result['course']; - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $teacherrole = $DB->get_record('role', array('shortname' => 'teacher')); $this->setUser($teacher); $result = mod_assign_external::get_participant($assign->id, $student->id, true); - $result = external_api::clean_returnvalue(mod_assign_external::get_participant_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_participant_returns(), $result); $this->assertEquals($student->id, $result['id']); $this->assertFalse(fullname($student) == $result['fullname']); $this->assertFalse($result['submitted']); @@ -2493,21 +2500,21 @@ public function test_get_participant_no_user() { $student = $result['student']; $teacher = $result['teacher']; $course = $result['course']; - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $teacherrole = $DB->get_record('role', array('shortname' => 'teacher')); // Create an assign instance to save a submission. set_config('submissionreceipts', 0, 'assign'); $cm = get_coursemodule_from_instance('assign', $assignmodule->id); - $context = context_module::instance($cm->id); + $context = \context_module::instance($cm->id); - $assign = new assign($context, $cm, $course); + $assign = new \assign($context, $cm, $course); $this->setUser($student); // Simulate a submission. - $data = new stdClass(); + $data = new \stdClass(); $data->onlinetext_editor = array( 'itemid' => file_get_unused_draft_itemid(), 'text' => 'Student submission text', @@ -2517,14 +2524,14 @@ public function test_get_participant_no_user() { $notices = array(); $assign->save_submission($data, $notices); - $data = new stdClass; + $data = new \stdClass; $data->userid = $student->id; $assign->submit_for_grading($data, array()); $this->setUser($teacher); $result = mod_assign_external::get_participant($assignmodule->id, $student->id, false); - $result = external_api::clean_returnvalue(mod_assign_external::get_participant_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_participant_returns(), $result); $this->assertEquals($student->id, $result['id']); $this->assertEquals(fullname($student), $result['fullname']); $this->assertTrue($result['submitted']); @@ -2548,13 +2555,13 @@ public function test_get_participant_full_details() { $student = $result['student']; $teacher = $result['teacher']; $course = $result['course']; - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $teacherrole = $DB->get_record('role', array('shortname' => 'teacher')); $this->setUser($teacher); $result = mod_assign_external::get_participant($assign->id, $student->id, true); - $result = external_api::clean_returnvalue(mod_assign_external::get_participant_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_participant_returns(), $result); // Check some of the extended properties we get when requesting the user. $this->assertEquals($student->id, $result['id']); // We should get user infomation back. @@ -2582,11 +2589,11 @@ public function test_get_participant_group_submission() { $student = $result['student']; $teacher = $result['teacher']; $course = $result['course']; - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $teacherrole = $DB->get_record('role', array('shortname' => 'teacher')); $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id)); $cm = get_coursemodule_from_instance('assign', $assignmodule->id); - $context = context_module::instance($cm->id); + $context = \context_module::instance($cm->id); $assign = new mod_assign_testable_assign($context, $cm, $course); groups_add_member($group, $student); @@ -2595,7 +2602,7 @@ public function test_get_participant_group_submission() { $submission = $assign->get_group_submission($student->id, $group->id, true); $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED; $assign->testable_update_submission($submission, $student->id, true, false); - $data = new stdClass(); + $data = new \stdClass(); $data->onlinetext_editor = array( 'itemid' => file_get_unused_draft_itemid(), 'text' => 'Submission text', @@ -2606,7 +2613,7 @@ public function test_get_participant_group_submission() { $this->setUser($teacher); $result = mod_assign_external::get_participant($assignmodule->id, $student->id, false); - $result = external_api::clean_returnvalue(mod_assign_external::get_participant_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_participant_returns(), $result); // Check some of the extended properties we get when not requesting a summary. $this->assertEquals($student->id, $result['id']); $this->assertEquals($group->id, $result['groupid']); @@ -2633,8 +2640,8 @@ public function test_get_participant_relative_dates(array $courseconfig, array $ $assignconfig['course'] = $course->id; $instance = $generator->create_instance($assignconfig); $cm = get_coursemodule_from_instance('assign', $instance->id); - $context = context_module::instance($cm->id); - $assign = new assign($context, $cm, $course); + $context = \context_module::instance($cm->id); + $assign = new \assign($context, $cm, $course); $user = $this->getDataGenerator()->create_and_enrol($course, ...array_values($enrolconfig)); @@ -2642,7 +2649,7 @@ public function test_get_participant_relative_dates(array $courseconfig, array $ $this->setUser($teacher); $result = mod_assign_external::get_participant($assign->get_instance()->id, $user->id, false); - $result = external_api::clean_returnvalue(mod_assign_external::get_participant_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::get_participant_returns(), $result); foreach ($expectedproperties as $propertyname => $propertyval) { $this->assertEquals($propertyval, $result[$propertyname]); @@ -2695,17 +2702,17 @@ public function test_list_participants_user_info_with_special_characters() { $student->department = 'Arts & Sciences & \' " ¢ £ © € ¥ ® < >'; $student->institution = 'University of Awesome People & \' " ¢ £ © € ¥ ® < >'; // Assert that we have valid user data. - $this->assertTrue(core_user::validate($student)); + $this->assertTrue(\core_user::validate($student)); // Update the user record. $DB->update_record('user', $student); $this->setUser($teacher); $participants = mod_assign_external::list_participants($assignment->id, 0, '', 0, 0, false, true, true); - $participants = external_api::clean_returnvalue(mod_assign_external::list_participants_returns(), $participants); + $participants = \external_api::clean_returnvalue(mod_assign_external::list_participants_returns(), $participants); $this->assertCount(1, $participants); // Asser that we have a valid response data. - $response = external_api::clean_returnvalue(mod_assign_external::list_participants_returns(), $participants); + $response = \external_api::clean_returnvalue(mod_assign_external::list_participants_returns(), $participants); $this->assertEquals($response, $participants); // Check participant data. @@ -2720,7 +2727,7 @@ public function test_list_participants_user_info_with_special_characters() { $this->assertArrayHasKey('enrolledcourses', $participant); $participants = mod_assign_external::list_participants($assignment->id, 0, '', 0, 0, false, false, true); - $participants = external_api::clean_returnvalue(mod_assign_external::list_participants_returns(), $participants); + $participants = \external_api::clean_returnvalue(mod_assign_external::list_participants_returns(), $participants); // Check that the list of courses the participant is enrolled is not returned. $participant = $participants[0]; $this->assertArrayNotHasKey('enrolledcourses', $participant); @@ -2745,14 +2752,14 @@ public function test_list_participants_returns_user_property_types() { // Check if key exists in user properties and the description has a type attribute. if (in_array($key, $userproperties) && isset($desc->type)) { try { - // The core_user::get_property_type() method might throw a coding_exception since + // The \core_user::get_property_type() method might throw a coding_exception since // core_user_external::user_description() might contain properties that are not yet included in // core_user's $propertiescache. - $propertytype = core_user::get_property_type($key); + $propertytype = \core_user::get_property_type($key); // Assert that user-related property types match those of the defined in core_user. $this->assertEquals($propertytype, $desc->type); - } catch (coding_exception $e) { + } catch (\coding_exception $e) { // All good. } } @@ -2780,7 +2787,7 @@ private function create_assign_with_student_and_teacher($params = array()) { $assign = $this->getDataGenerator()->create_module('assign', $params); $cm = get_coursemodule_from_instance('assign', $assign->id); - $context = context_module::instance($cm->id); + $context = \context_module::instance($cm->id); $student = $this->getDataGenerator()->create_user(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); @@ -2816,16 +2823,16 @@ public function test_view_assign() { $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1)); $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id), array('completion' => 2, 'completionview' => 1)); - $context = context_module::instance($assign->cmid); + $context = \context_module::instance($assign->cmid); $cm = get_coursemodule_from_instance('assign', $assign->id); $result = mod_assign_external::view_assign($assign->id); - $result = external_api::clean_returnvalue(mod_assign_external::view_assign_returns(), $result); + $result = \external_api::clean_returnvalue(mod_assign_external::view_assign_returns(), $result); $this->assertTrue($result['status']); $this->assertEmpty($result['warnings']); // Check completion status. - $completion = new completion_info($course); + $completion = new \completion_info($course); $completiondata = $completion->get_data($cm); $this->assertEquals(1, $completiondata->completionstate); } diff --git a/mod/book/tests/externallib_test.php b/mod/book/tests/externallib_test.php index e363ec7a01089..aec10eee9a604 100644 --- a/mod/book/tests/externallib_test.php +++ b/mod/book/tests/externallib_test.php @@ -14,15 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * External mod_book functions unit tests - * - * @package mod_book - * @category external - * @copyright 2015 Juan Leyva - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @since Moodle 3.0 - */ +namespace mod_book; + +use externallib_advanced_testcase; +use mod_book_external; defined('MOODLE_INTERNAL') || die(); @@ -39,7 +34,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 3.0 */ -class mod_book_external_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * Test view_book @@ -57,14 +52,14 @@ public function test_view_book() { $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id)); $chapterhidden = $bookgenerator->create_chapter(array('bookid' => $book->id, 'hidden' => 1)); - $context = context_module::instance($book->cmid); + $context = \context_module::instance($book->cmid); $cm = get_coursemodule_from_instance('book', $book->id); // Test invalid instance id. try { mod_book_external::view_book(0); $this->fail('Exception expected due to invalid mod_book instance id.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('invalidrecord', $e->errorcode); } @@ -74,7 +69,7 @@ public function test_view_book() { try { mod_book_external::view_book($book->id, 0); $this->fail('Exception expected due to not enrolled user.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } @@ -86,7 +81,7 @@ public function test_view_book() { $sink = $this->redirectEvents(); $result = mod_book_external::view_book($book->id, 0); - $result = external_api::clean_returnvalue(mod_book_external::view_book_returns(), $result); + $result = \external_api::clean_returnvalue(mod_book_external::view_book_returns(), $result); $events = $sink->get_events(); $this->assertCount(2, $events); @@ -105,7 +100,7 @@ public function test_view_book() { $this->assertEquals($chapter->id, $event->objectid); $result = mod_book_external::view_book($book->id, $chapter->id); - $result = external_api::clean_returnvalue(mod_book_external::view_book_returns(), $result); + $result = \external_api::clean_returnvalue(mod_book_external::view_book_returns(), $result); $events = $sink->get_events(); // We expect a total of 3 events. @@ -115,7 +110,7 @@ public function test_view_book() { try { mod_book_external::view_book($book->id, $chapterhidden->id); $this->fail('Exception expected due to missing capability.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('errorchapter', $e->errorcode); } @@ -127,7 +122,7 @@ public function test_view_book() { try { mod_book_external::view_book($book->id, 0); $this->fail('Exception expected due to missing capability.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('nopermissions', $e->errorcode); } @@ -161,7 +156,7 @@ public function test_get_books_by_courses() { $books = mod_book_external::get_books_by_courses(); // We need to execute the return values cleaning process to simulate the web service server. - $books = external_api::clean_returnvalue(mod_book_external::get_books_by_courses_returns(), $books); + $books = \external_api::clean_returnvalue(mod_book_external::get_books_by_courses_returns(), $books); $this->assertCount(1, $books['books']); $this->assertEquals('First Book', $books['books'][0]['name']); // We see 10 fields. @@ -173,7 +168,7 @@ public function test_get_books_by_courses() { // Student1 is not enrolled in course2. The webservice will return a warning! $books = mod_book_external::get_books_by_courses(array($course2->id)); // We need to execute the return values cleaning process to simulate the web service server. - $books = external_api::clean_returnvalue(mod_book_external::get_books_by_courses_returns(), $books); + $books = \external_api::clean_returnvalue(mod_book_external::get_books_by_courses_returns(), $books); $this->assertCount(0, $books['books']); $this->assertEquals(1, $books['warnings'][0]['warningcode']); @@ -182,7 +177,7 @@ public function test_get_books_by_courses() { // As Admin we can see this book. $books = mod_book_external::get_books_by_courses(array($course2->id)); // We need to execute the return values cleaning process to simulate the web service server. - $books = external_api::clean_returnvalue(mod_book_external::get_books_by_courses_returns(), $books); + $books = \external_api::clean_returnvalue(mod_book_external::get_books_by_courses_returns(), $books); $this->assertCount(1, $books['books']); $this->assertEquals('Second Book', $books['books'][0]['name']); @@ -195,7 +190,7 @@ public function test_get_books_by_courses() { self::getDataGenerator()->enrol_user($student1->id, $course2->id, $studentrole->id); $this->setUser($student1); $books = mod_book_external::get_books_by_courses(); - $books = external_api::clean_returnvalue(mod_book_external::get_books_by_courses_returns(), $books); + $books = \external_api::clean_returnvalue(mod_book_external::get_books_by_courses_returns(), $books); $this->assertCount(2, $books['books']); } diff --git a/mod/book/tests/generator_test.php b/mod/book/tests/generator_test.php index 4796cbff8ffd0..0debdff5cd897 100644 --- a/mod/book/tests/generator_test.php +++ b/mod/book/tests/generator_test.php @@ -14,22 +14,16 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Genarator tests. - * - * @package mod_book - * @copyright 2013 Frédéric Massart - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace mod_book; /** - * Genarator tests class. + * Generator tests class. * * @package mod_book * @copyright 2013 Frédéric Massart * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_book_generator_testcase extends advanced_testcase { +class generator_test extends \advanced_testcase { public function test_create_instance() { global $DB; @@ -68,7 +62,7 @@ public function test_create_chapter() { $this->assertEquals('Oops', $DB->get_field_select('book_chapters', 'title', 'id = :id', array('id' => $chapter->id))); $this->assertEquals('Yay!', $DB->get_field_select('book_chapters', 'content', 'id = :id', array('id' => $chapter->id))); $this->assertEquals(array('Cats', 'mice'), - array_values(core_tag_tag::get_item_tags_array('mod_book', 'book_chapters', $chapter->id))); + array_values(\core_tag_tag::get_item_tags_array('mod_book', 'book_chapters', $chapter->id))); $chapter = $bookgenerator->create_content($book); $this->assertEquals(3, $DB->count_records('book_chapters', array('bookid' => $book->id))); diff --git a/mod/chat/tests/externallib_test.php b/mod/chat/tests/externallib_test.php index 050753baaa6c4..9d7db19807676 100644 --- a/mod/chat/tests/externallib_test.php +++ b/mod/chat/tests/externallib_test.php @@ -14,15 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * External mod_chat functions unit tests - * - * @package mod_chat - * @category external - * @copyright 2015 Juan Leyva - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @since Moodle 3.0 - */ +namespace mod_chat; + +use externallib_advanced_testcase; +use mod_chat_external; defined('MOODLE_INTERNAL') || die(); @@ -39,7 +34,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 3.0 */ -class mod_chat_external_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * Test login user @@ -60,7 +55,7 @@ public function test_login_user() { $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id); $result = mod_chat_external::login_user($chat->id); - $result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result); + $result = \external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result); // Test session started. $sid = $DB->get_field('chat_users', 'sid', array('userid' => $user->id, 'chatid' => $chat->id)); @@ -90,15 +85,15 @@ public function test_get_chat_users() { $this->getDataGenerator()->enrol_user($user2->id, $course->id, $studentrole->id); $result = mod_chat_external::login_user($chat->id); - $result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result); + $result = \external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result); $this->setUser($user2); $result = mod_chat_external::login_user($chat->id); - $result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result); + $result = \external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result); // Get users. $result = mod_chat_external::get_chat_users($result['chatsid']); - $result = external_api::clean_returnvalue(mod_chat_external::get_chat_users_returns(), $result); + $result = \external_api::clean_returnvalue(mod_chat_external::get_chat_users_returns(), $result); // Check correct users. $this->assertCount(2, $result['users']); @@ -131,16 +126,16 @@ public function test_send_get_chat_message() { $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id); $result = mod_chat_external::login_user($chat->id); - $result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result); + $result = \external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result); $chatsid = $result['chatsid']; $result = mod_chat_external::send_chat_message($chatsid, 'hello!'); - $result = external_api::clean_returnvalue(mod_chat_external::send_chat_message_returns(), $result); + $result = \external_api::clean_returnvalue(mod_chat_external::send_chat_message_returns(), $result); // Test messages received. $result = mod_chat_external::get_chat_latest_messages($chatsid, 0); - $result = external_api::clean_returnvalue(mod_chat_external::get_chat_latest_messages_returns(), $result); + $result = \external_api::clean_returnvalue(mod_chat_external::get_chat_latest_messages_returns(), $result); foreach ($result['messages'] as $message) { // Ommit system messages, like user just joined in. @@ -163,14 +158,14 @@ public function test_view_chat() { $this->setAdminUser(); $course = $this->getDataGenerator()->create_course(); $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id)); - $context = context_module::instance($chat->cmid); + $context = \context_module::instance($chat->cmid); $cm = get_coursemodule_from_instance('chat', $chat->id); // Test invalid instance id. try { mod_chat_external::view_chat(0); $this->fail('Exception expected due to invalid mod_chat instance id.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('invalidrecord', $e->errorcode); } @@ -180,7 +175,7 @@ public function test_view_chat() { try { mod_chat_external::view_chat($chat->id); $this->fail('Exception expected due to not enrolled user.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } @@ -192,7 +187,7 @@ public function test_view_chat() { $sink = $this->redirectEvents(); $result = mod_chat_external::view_chat($chat->id); - $result = external_api::clean_returnvalue(mod_chat_external::view_chat_returns(), $result); + $result = \external_api::clean_returnvalue(mod_chat_external::view_chat_returns(), $result); $events = $sink->get_events(); $this->assertCount(1, $events); @@ -214,7 +209,7 @@ public function test_view_chat() { try { mod_chat_external::view_chat($chat->id); $this->fail('Exception expected due to missing capability.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('nopermissions', $e->errorcode); } } @@ -251,7 +246,7 @@ public function test_get_chats_by_courses() { $chats = mod_chat_external::get_chats_by_courses(); // We need to execute the return values cleaning process to simulate the web service server. - $chats = external_api::clean_returnvalue(mod_chat_external::get_chats_by_courses_returns(), $chats); + $chats = \external_api::clean_returnvalue(mod_chat_external::get_chats_by_courses_returns(), $chats); $this->assertCount(1, $chats['chats']); $this->assertEquals('First Chat', $chats['chats'][0]['name']); // We see 12 fields. @@ -263,7 +258,7 @@ public function test_get_chats_by_courses() { // Student1 is not enrolled in course2. The webservice will return a warning! $chats = mod_chat_external::get_chats_by_courses(array($course2->id)); // We need to execute the return values cleaning process to simulate the web service server. - $chats = external_api::clean_returnvalue(mod_chat_external::get_chats_by_courses_returns(), $chats); + $chats = \external_api::clean_returnvalue(mod_chat_external::get_chats_by_courses_returns(), $chats); $this->assertCount(0, $chats['chats']); $this->assertEquals(1, $chats['warnings'][0]['warningcode']); @@ -272,7 +267,7 @@ public function test_get_chats_by_courses() { // As Admin we can see this chat. $chats = mod_chat_external::get_chats_by_courses(array($course2->id)); // We need to execute the return values cleaning process to simulate the web service server. - $chats = external_api::clean_returnvalue(mod_chat_external::get_chats_by_courses_returns(), $chats); + $chats = \external_api::clean_returnvalue(mod_chat_external::get_chats_by_courses_returns(), $chats); $this->assertCount(1, $chats['chats']); $this->assertEquals('Second Chat', $chats['chats'][0]['name']); @@ -286,7 +281,7 @@ public function test_get_chats_by_courses() { self::getDataGenerator()->enrol_user($student1->id, $course2->id, $studentrole->id); $this->setUser($student1); $chats = mod_chat_external::get_chats_by_courses(); - $chats = external_api::clean_returnvalue(mod_chat_external::get_chats_by_courses_returns(), $chats); + $chats = \external_api::clean_returnvalue(mod_chat_external::get_chats_by_courses_returns(), $chats); $this->assertCount(2, $chats['chats']); } @@ -305,7 +300,7 @@ public function test_get_sessions_empty_chat() { $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id)); $result = mod_chat_external::get_sessions($chat->id); - $result = external_api::clean_returnvalue(mod_chat_external::get_sessions_returns(), $result); + $result = \external_api::clean_returnvalue(mod_chat_external::get_sessions_returns(), $result); $this->assertEmpty($result['sessions']); $this->assertEmpty($result['warnings']); } @@ -326,7 +321,7 @@ public function test_get_sessions_no_permissions_for_student() { $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id, 'studentlogs' => 0)); // The admin has permissions to check logs. $result = mod_chat_external::get_sessions($chat->id); - $result = external_api::clean_returnvalue(mod_chat_external::get_sessions_returns(), $result); + $result = \external_api::clean_returnvalue(mod_chat_external::get_sessions_returns(), $result); $this->assertEmpty($result['sessions']); $this->assertEmpty($result['warnings']); @@ -362,20 +357,20 @@ public function test_get_sessions_not_completed_session() { // Start a chat and send just one message. $result = mod_chat_external::login_user($chat->id); - $result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result); + $result = \external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result); $chatsid = $result['chatsid']; $result = mod_chat_external::send_chat_message($chatsid, 'hello!'); - $result = external_api::clean_returnvalue(mod_chat_external::send_chat_message_returns(), $result); + $result = \external_api::clean_returnvalue(mod_chat_external::send_chat_message_returns(), $result); // Check session is not marked as completed so it is not returned. $result = mod_chat_external::get_sessions($chat->id); - $result = external_api::clean_returnvalue(mod_chat_external::get_sessions_returns(), $result); + $result = \external_api::clean_returnvalue(mod_chat_external::get_sessions_returns(), $result); $this->assertEmpty($result['sessions']); $this->assertEmpty($result['warnings']); // Pass showall parameter to indicate that we want not completed sessions. $result = mod_chat_external::get_sessions($chat->id, 0, true); - $result = external_api::clean_returnvalue(mod_chat_external::get_sessions_returns(), $result); + $result = \external_api::clean_returnvalue(mod_chat_external::get_sessions_returns(), $result); $this->assertCount(1, $result['sessions']); // One session. $this->assertFalse($result['sessions'][0]['iscomplete']); // Session not complete. $this->assertEmpty($result['warnings']); @@ -403,16 +398,16 @@ public function test_get_sessions_completed_session() { // Start a chat and completeit. $this->setUser($user1); $result = mod_chat_external::login_user($chat->id); - $result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result); + $result = \external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result); $chatsid = $result['chatsid']; $result = mod_chat_external::send_chat_message($chatsid, 'hello!'); - $result = external_api::clean_returnvalue(mod_chat_external::send_chat_message_returns(), $result); + $result = \external_api::clean_returnvalue(mod_chat_external::send_chat_message_returns(), $result); $this->setUser($user2); $result = mod_chat_external::login_user($chat->id); - $result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result); + $result = \external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result); $chatsid = $result['chatsid']; $result = mod_chat_external::send_chat_message($chatsid, 'hello to you!'); - $result = external_api::clean_returnvalue(mod_chat_external::send_chat_message_returns(), $result); + $result = \external_api::clean_returnvalue(mod_chat_external::send_chat_message_returns(), $result); // Need to change first messages and last message times to mark the session completed. // We receive 4 messages (2 system messages that indicates user joined and the 2 messages sent by the users). $messages = $DB->get_records('chat_messages', array('chatid' => $chat->id)); @@ -425,7 +420,7 @@ public function test_get_sessions_completed_session() { } // Check session is completed. $result = mod_chat_external::get_sessions($chat->id); - $result = external_api::clean_returnvalue(mod_chat_external::get_sessions_returns(), $result); + $result = \external_api::clean_returnvalue(mod_chat_external::get_sessions_returns(), $result); $this->assertCount(1, $result['sessions']); // One session. $this->assertTrue($result['sessions'][0]['iscomplete']); // Session complete. // The session started when user1 entered the chat. @@ -455,26 +450,26 @@ public function test_get_session_messages() { // Start a chat and send a few messages. $this->setUser($user1); $result = mod_chat_external::login_user($chat->id); - $result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result); + $result = \external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result); $chatsid = $result['chatsid']; mod_chat_external::send_chat_message($chatsid, 'hello!'); mod_chat_external::send_chat_message($chatsid, 'bye bye!'); $this->setUser($user2); $result = mod_chat_external::login_user($chat->id); - $result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result); + $result = \external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result); $chatsid = $result['chatsid']; mod_chat_external::send_chat_message($chatsid, 'greetings!'); // Pass showall parameter to indicate that we want not completed sessions. $result = mod_chat_external::get_sessions($chat->id, 0, true); - $result = external_api::clean_returnvalue(mod_chat_external::get_sessions_returns(), $result); + $result = \external_api::clean_returnvalue(mod_chat_external::get_sessions_returns(), $result); $this->assertCount(1, $result['sessions']); // One session. $sessionstart = $result['sessions'][0]['sessionstart']; $sessionend = $result['sessions'][0]['sessionend']; $result = mod_chat_external::get_session_messages($chat->id, $sessionstart, $sessionend); - $result = external_api::clean_returnvalue(mod_chat_external::get_session_messages_returns(), $result); + $result = \external_api::clean_returnvalue(mod_chat_external::get_session_messages_returns(), $result); $this->assertCount(5, $result['messages']); // 2 system + 3 personal messages. $found = 0; foreach ($result['messages'] as $message) { diff --git a/mod/chat/tests/generator_test.php b/mod/chat/tests/generator_test.php index 5a6d23540efb2..6a4a7f37eafa0 100644 --- a/mod/chat/tests/generator_test.php +++ b/mod/chat/tests/generator_test.php @@ -14,22 +14,16 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Genarator tests. - * - * @package mod_chat - * @copyright 2013 Frédéric Massart - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace mod_chat; /** - * Genarator tests class. + * Generator tests class. * * @package mod_chat * @copyright 2013 Frédéric Massart * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_chat_generator_testcase extends advanced_testcase { +class generator_test extends \advanced_testcase { public function test_create_instance() { global $DB; diff --git a/mod/choice/tests/externallib_test.php b/mod/choice/tests/externallib_test.php index 2919495e87fa8..561ec87fdb972 100644 --- a/mod/choice/tests/externallib_test.php +++ b/mod/choice/tests/externallib_test.php @@ -14,14 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * External choice functions unit tests - * - * @package mod_choice - * @category external - * @copyright 2015 Costantino Cito - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace mod_choice; + +use externallib_advanced_testcase; +use mod_choice_external; defined('MOODLE_INTERNAL') || die(); @@ -38,7 +34,7 @@ * @copyright 2015 Costantino Cito * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_choice_externallib_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * Test get_choice_results @@ -49,7 +45,7 @@ public function test_get_choice_results() { $this->resetAfterTest(true); $course = self::getDataGenerator()->create_course(); - $params = new stdClass(); + $params = new \stdClass(); $params->course = $course->id; $params->option = array('fried rice', 'spring rolls', 'sweet and sour pork', 'satay beef', 'gyouza'); $params->name = 'First Choice Activity'; @@ -75,7 +71,7 @@ public function test_get_choice_results() { choice_user_submit_response($myanswer, $choice, $student1->id, $course, $cm); $results = mod_choice_external::get_choice_results($choice->id); // We need to execute the return values cleaning process to simulate the web service server. - $results = external_api::clean_returnvalue(mod_choice_external::get_choice_results_returns(), $results); + $results = \external_api::clean_returnvalue(mod_choice_external::get_choice_results_returns(), $results); // Create an array with optionID as Key. $resultsarr = array(); @@ -91,7 +87,7 @@ public function test_get_choice_results() { $this->setUser($student2); $results = mod_choice_external::get_choice_results($choice->id); // We need to execute the return values cleaning process to simulate the web service server. - $results = external_api::clean_returnvalue(mod_choice_external::get_choice_results_returns(), $results); + $results = \external_api::clean_returnvalue(mod_choice_external::get_choice_results_returns(), $results); // We do not retrieve any response! foreach ($results['options'] as $option) { $this->assertCount(0, $option['userresponses']); @@ -107,7 +103,7 @@ public function test_get_choice_results() { $results = mod_choice_external::get_choice_results($choice->id); // We need to execute the return values cleaning process to simulate the web service server. - $results = external_api::clean_returnvalue(mod_choice_external::get_choice_results_returns(), $results); + $results = \external_api::clean_returnvalue(mod_choice_external::get_choice_results_returns(), $results); // We do not retrieve any response (activity is still open). foreach ($results['options'] as $option) { $this->assertCount(0, $option['userresponses']); @@ -119,7 +115,7 @@ public function test_get_choice_results() { // Now as Stundent2 we will see results! $results = mod_choice_external::get_choice_results($choice->id); // We need to execute the return values cleaning process to simulate the web service server. - $results = external_api::clean_returnvalue(mod_choice_external::get_choice_results_returns(), $results); + $results = \external_api::clean_returnvalue(mod_choice_external::get_choice_results_returns(), $results); // Create an array with optionID as Key. $resultsarr = array(); foreach ($results['options'] as $option) { @@ -135,7 +131,7 @@ public function test_get_choice_results() { $DB->update_record('choice', $choice); $results = mod_choice_external::get_choice_results($choice->id); // We need to execute the return values cleaning process to simulate the web service server. - $results = external_api::clean_returnvalue(mod_choice_external::get_choice_results_returns(), $results); + $results = \external_api::clean_returnvalue(mod_choice_external::get_choice_results_returns(), $results); // Create an array with optionID as Key. $resultsarr = array(); // Does not show any user response! @@ -181,7 +177,7 @@ public function test_get_choice_options() { $results = mod_choice_external::get_choice_options($choice->id); // We need to execute the return values cleaning process to simulate the web service server. - $results = external_api::clean_returnvalue(mod_choice_external::get_choice_options_returns(), $results); + $results = \external_api::clean_returnvalue(mod_choice_external::get_choice_options_returns(), $results); // We should retrieve all options. $this->assertCount(count($possibleoptions), $results['options']); @@ -192,7 +188,7 @@ public function test_get_choice_options() { $results = mod_choice_external::get_choice_options($choice->id); // We need to execute the return values cleaning process to simulate the web service server. - $results = external_api::clean_returnvalue(mod_choice_external::get_choice_options_returns(), $results); + $results = \external_api::clean_returnvalue(mod_choice_external::get_choice_options_returns(), $results); // We should retrieve no options. $this->assertCount(0, $results['options']); $this->assertEquals($notopenyet, $results['warnings'][0]['warningcode']); @@ -202,7 +198,7 @@ public function test_get_choice_options() { $DB->update_record('choice', $choice); $results = mod_choice_external::get_choice_options($choice->id); // We need to execute the return values cleaning process to simulate the web service server. - $results = external_api::clean_returnvalue(mod_choice_external::get_choice_options_returns(), $results); + $results = \external_api::clean_returnvalue(mod_choice_external::get_choice_options_returns(), $results); // We should retrieve all options. $this->assertCount(count($possibleoptions), $results['options']); @@ -229,7 +225,7 @@ public function test_get_choice_options() { $results = mod_choice_external::get_choice_options($choice->id); // We need to execute the return values cleaning process to simulate the web service server. - $results = external_api::clean_returnvalue(mod_choice_external::get_choice_options_returns(), $results); + $results = \external_api::clean_returnvalue(mod_choice_external::get_choice_options_returns(), $results); // We should retrieve all options. $this->assertCount(count($possibleoptions), $results['options']); foreach ($results['options'] as $option) { @@ -249,7 +245,7 @@ public function test_get_choice_options() { $DB->update_record('choice', $choice); $results = mod_choice_external::get_choice_options($choice->id); // We need to execute the return values cleaning process to simulate the web service server. - $results = external_api::clean_returnvalue(mod_choice_external::get_choice_options_returns(), $results); + $results = \external_api::clean_returnvalue(mod_choice_external::get_choice_options_returns(), $results); // We should retrieve no options. $this->assertCount(0, $results['options']); $this->assertEquals($expired, $results['warnings'][0]['warningcode']); @@ -265,7 +261,7 @@ public function test_submit_choice_response() { $this->resetAfterTest(true); $course = self::getDataGenerator()->create_course(); - $params = new stdClass(); + $params = new \stdClass(); $params->course = $course->id; $params->option = array('fried rice', 'spring rolls', 'sweet and sour pork', 'satay beef', 'gyouza'); $params->name = 'First Choice Activity'; @@ -286,7 +282,7 @@ public function test_submit_choice_response() { $myresponse = $options[2]; $results = mod_choice_external::submit_choice_response($choice->id, array($myresponse)); // We need to execute the return values cleaning process to simulate the web service server. - $results = external_api::clean_returnvalue(mod_choice_external::submit_choice_response_returns(), $results); + $results = \external_api::clean_returnvalue(mod_choice_external::submit_choice_response_returns(), $results); $myanswers = $DB->get_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $student1->id)); $myanswer = reset($myanswers); $this->assertEquals($results['answers'][0]['id'], $myanswer->id); @@ -306,14 +302,14 @@ public function test_view_choice() { // Setup test data. $course = $this->getDataGenerator()->create_course(); $choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id)); - $context = context_module::instance($choice->cmid); + $context = \context_module::instance($choice->cmid); $cm = get_coursemodule_from_instance('choice', $choice->id); // Test invalid instance id. try { mod_choice_external::view_choice(0); $this->fail('Exception expected due to invalid mod_choice instance id.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('invalidcoursemodule', $e->errorcode); } @@ -323,7 +319,7 @@ public function test_view_choice() { try { mod_choice_external::view_choice($choice->id); $this->fail('Exception expected due to not enrolled user.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } @@ -335,7 +331,7 @@ public function test_view_choice() { $sink = $this->redirectEvents(); $result = mod_choice_external::view_choice($choice->id); - $result = external_api::clean_returnvalue(mod_choice_external::view_choice_returns(), $result); + $result = \external_api::clean_returnvalue(mod_choice_external::view_choice_returns(), $result); $events = $sink->get_events(); $this->assertCount(1, $events); @@ -381,7 +377,7 @@ public function test_get_choices_by_courses() { $this->setUser($student1); $choices = mod_choice_external::get_choices_by_courses(array()); - $choices = external_api::clean_returnvalue(mod_choice_external::get_choices_by_courses_returns(), $choices); + $choices = \external_api::clean_returnvalue(mod_choice_external::get_choices_by_courses_returns(), $choices); $this->assertCount(1, $choices['choices']); $this->assertEquals('First IMSCP', $choices['choices'][0]['name']); // As Student you cannot see some IMSCP properties like 'section'. @@ -390,7 +386,7 @@ public function test_get_choices_by_courses() { // Student1 is not enrolled in this Course. // The webservice will give a warning! $choices = mod_choice_external::get_choices_by_courses(array($course2->id)); - $choices = external_api::clean_returnvalue(mod_choice_external::get_choices_by_courses_returns(), $choices); + $choices = \external_api::clean_returnvalue(mod_choice_external::get_choices_by_courses_returns(), $choices); $this->assertCount(0, $choices['choices']); $this->assertEquals(1, $choices['warnings'][0]['warningcode']); @@ -398,7 +394,7 @@ public function test_get_choices_by_courses() { $this->setAdminUser(); // As Admin we can see this IMSCP. $choices = mod_choice_external::get_choices_by_courses(array($course2->id)); - $choices = external_api::clean_returnvalue(mod_choice_external::get_choices_by_courses_returns(), $choices); + $choices = \external_api::clean_returnvalue(mod_choice_external::get_choices_by_courses_returns(), $choices); $this->assertCount(1, $choices['choices']); $this->assertEquals('Second IMSCP', $choices['choices'][0]['name']); // As an Admin you can see some IMSCP properties like 'section'. @@ -406,13 +402,13 @@ public function test_get_choices_by_courses() { // Now, prohibit capabilities. $this->setUser($student1); - $contextcourse1 = context_course::instance($course1->id); + $contextcourse1 = \context_course::instance($course1->id); // Prohibit capability = mod:choice:choose on Course1 for students. assign_capability('mod/choice:choose', CAP_PROHIBIT, $studentrole->id, $contextcourse1->id); accesslib_clear_all_caches_for_unit_testing(); $choices = mod_choice_external::get_choices_by_courses(array($course1->id)); - $choices = external_api::clean_returnvalue(mod_choice_external::get_choices_by_courses_returns(), $choices); + $choices = \external_api::clean_returnvalue(mod_choice_external::get_choices_by_courses_returns(), $choices); $this->assertFalse(isset($choices['choices'][0]['timeopen'])); } @@ -425,7 +421,7 @@ public function test_delete_choice_responses() { $this->resetAfterTest(true); $course = self::getDataGenerator()->create_course(); - $params = new stdClass(); + $params = new \stdClass(); $params->course = $course->id; $params->option = array('fried rice', 'spring rolls', 'sweet and sour pork', 'satay beef', 'gyouza'); $params->name = 'First Choice Activity'; @@ -446,7 +442,7 @@ public function test_delete_choice_responses() { $this->setUser($student); $results = mod_choice_external::submit_choice_response($choice->id, array($options[1], $options[2])); - $results = external_api::clean_returnvalue(mod_choice_external::submit_choice_response_returns(), $results); + $results = \external_api::clean_returnvalue(mod_choice_external::submit_choice_response_returns(), $results); $myresponses = array_keys(choice_get_my_response($choice)); @@ -454,7 +450,7 @@ public function test_delete_choice_responses() { try { mod_choice_external::delete_choice_responses($choice->id, array($myresponses[0], $myresponses[0])); $this->fail('Exception expected due to missing permissions.'); - } catch (required_capability_exception $e) { + } catch (\required_capability_exception $e) { $this->assertEquals('nopermissions', $e->errorcode); } @@ -464,14 +460,14 @@ public function test_delete_choice_responses() { try { mod_choice_external::delete_choice_responses($choice->id, array($myresponses[0], $myresponses[1])); $this->fail('Exception expected due to expired choice.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('expired', $e->errorcode); } // Reset time close. We should be able now to delete all the responses. $DB->set_field('choice', 'timeclose', 0, array('id' => $choice->id)); $results = mod_choice_external::delete_choice_responses($choice->id, array($myresponses[0], $myresponses[1])); - $results = external_api::clean_returnvalue(mod_choice_external::delete_choice_responses_returns(), $results); + $results = \external_api::clean_returnvalue(mod_choice_external::delete_choice_responses_returns(), $results); $this->assertTrue($results['status']); $this->assertCount(0, $results['warnings']); @@ -480,12 +476,12 @@ public function test_delete_choice_responses() { // Submit again the responses. $results = mod_choice_external::submit_choice_response($choice->id, array($options[1], $options[2])); - $results = external_api::clean_returnvalue(mod_choice_external::submit_choice_response_returns(), $results); + $results = \external_api::clean_returnvalue(mod_choice_external::submit_choice_response_returns(), $results); $myresponses = array_keys(choice_get_my_response($choice)); // Delete only one response. $results = mod_choice_external::delete_choice_responses($choice->id, array($myresponses[0])); - $results = external_api::clean_returnvalue(mod_choice_external::delete_choice_responses_returns(), $results); + $results = \external_api::clean_returnvalue(mod_choice_external::delete_choice_responses_returns(), $results); $this->assertTrue($results['status']); $this->assertCount(0, $results['warnings']); // Now, in the DB 1 response still. @@ -494,7 +490,7 @@ public function test_delete_choice_responses() { // Delete the remaining response, passing 2 invalid responses ids. $results = mod_choice_external::delete_choice_responses($choice->id, array($myresponses[1], $myresponses[0] + 2, $myresponses[0] + 3)); - $results = external_api::clean_returnvalue(mod_choice_external::delete_choice_responses_returns(), $results); + $results = \external_api::clean_returnvalue(mod_choice_external::delete_choice_responses_returns(), $results); $this->assertTrue($results['status']); // 2 warnings, 2 invalid responses. $this->assertCount(2, $results['warnings']); @@ -505,7 +501,7 @@ public function test_delete_choice_responses() { $this->setUser($student); // Submit again the responses. $results = mod_choice_external::submit_choice_response($choice->id, array($options[1], $options[2])); - $results = external_api::clean_returnvalue(mod_choice_external::submit_choice_response_returns(), $results); + $results = \external_api::clean_returnvalue(mod_choice_external::submit_choice_response_returns(), $results); $studentresponses = array_keys(choice_get_my_response($choice)); $this->setAdminUser(); @@ -513,7 +509,7 @@ public function test_delete_choice_responses() { $DB->set_field('choice', 'timeclose', time() - DAYSECS, array('id' => $choice->id)); $results = mod_choice_external::delete_choice_responses($choice->id, array($studentresponses[0], $studentresponses[1])); - $results = external_api::clean_returnvalue(mod_choice_external::delete_choice_responses_returns(), $results); + $results = \external_api::clean_returnvalue(mod_choice_external::delete_choice_responses_returns(), $results); $this->assertTrue($results['status']); $this->assertCount(0, $results['warnings']); @@ -522,12 +518,12 @@ public function test_delete_choice_responses() { $this->setUser($student); $DB->set_field('choice', 'timeclose', 0, array('id' => $choice->id)); $results = mod_choice_external::submit_choice_response($choice->id, array($options[1], $options[2])); - $results = external_api::clean_returnvalue(mod_choice_external::submit_choice_response_returns(), $results); + $results = \external_api::clean_returnvalue(mod_choice_external::submit_choice_response_returns(), $results); // Test admin try to delete his own responses (he didn't respond so nothing should be deleted). $this->setAdminUser(); $results = mod_choice_external::delete_choice_responses($choice->id); - $results = external_api::clean_returnvalue(mod_choice_external::delete_choice_responses_returns(), $results); + $results = \external_api::clean_returnvalue(mod_choice_external::delete_choice_responses_returns(), $results); $this->assertFalse($results['status']); $this->assertCount(0, $results['warnings']); $allresponses = choice_get_all_responses($choice); @@ -535,12 +531,12 @@ public function test_delete_choice_responses() { // Now admin submit a couple of responses more. $results = mod_choice_external::submit_choice_response($choice->id, array($options[1], $options[2])); - $results = external_api::clean_returnvalue(mod_choice_external::submit_choice_response_returns(), $results); + $results = \external_api::clean_returnvalue(mod_choice_external::submit_choice_response_returns(), $results); $allresponses = choice_get_all_responses($choice); $this->assertCount(4, $allresponses); // Admin responses are deleted when passing an empty array. $results = mod_choice_external::delete_choice_responses($choice->id); - $results = external_api::clean_returnvalue(mod_choice_external::delete_choice_responses_returns(), $results); + $results = \external_api::clean_returnvalue(mod_choice_external::delete_choice_responses_returns(), $results); $this->assertTrue($results['status']); $this->assertCount(0, $results['warnings']); $allresponses = choice_get_all_responses($choice); @@ -548,7 +544,7 @@ public function test_delete_choice_responses() { // Now admin will delete all the other users responses. $results = mod_choice_external::delete_choice_responses($choice->id, array_keys($allresponses)); - $results = external_api::clean_returnvalue(mod_choice_external::delete_choice_responses_returns(), $results); + $results = \external_api::clean_returnvalue(mod_choice_external::delete_choice_responses_returns(), $results); $this->assertTrue($results['status']); $this->assertCount(0, $results['warnings']); @@ -557,7 +553,7 @@ public function test_delete_choice_responses() { // Admin try do delete an invalid response. $results = mod_choice_external::delete_choice_responses($choice->id, array(-1)); - $results = external_api::clean_returnvalue(mod_choice_external::delete_choice_responses_returns(), $results); + $results = \external_api::clean_returnvalue(mod_choice_external::delete_choice_responses_returns(), $results); $this->assertFalse($results['status']); $this->assertCount(1, $results['warnings']); @@ -569,11 +565,11 @@ public function test_delete_choice_responses() { $DB->set_field('choice', 'allowupdate', 1, array('id' => $choice->id)); $DB->set_field('choice', 'timeclose', 0, array('id' => $choice->id)); $results = mod_choice_external::submit_choice_response($choice->id, array($options[1], $options[2])); - $results = external_api::clean_returnvalue(mod_choice_external::submit_choice_response_returns(), $results); + $results = \external_api::clean_returnvalue(mod_choice_external::submit_choice_response_returns(), $results); // Delete all responses. $results = mod_choice_external::delete_choice_responses($choice->id); - $results = external_api::clean_returnvalue(mod_choice_external::delete_choice_responses_returns(), $results); + $results = \external_api::clean_returnvalue(mod_choice_external::delete_choice_responses_returns(), $results); $this->assertTrue($results['status']); $this->assertCount(0, $results['warnings']); diff --git a/mod/choice/tests/generator_test.php b/mod/choice/tests/generator_test.php index 55de7b998bd36..1e092cfc29bc5 100644 --- a/mod/choice/tests/generator_test.php +++ b/mod/choice/tests/generator_test.php @@ -14,13 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Generator tests. - * - * @package mod_choice - * @copyright 2013 Adrian Greeve - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace mod_choice; /** * Generator tests class. @@ -29,7 +23,7 @@ * @copyright 2013 Adrian Greeve * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_choice_generator_testcase extends advanced_testcase { +class generator_test extends \advanced_testcase { public function test_create_instance() { global $DB; @@ -49,7 +43,7 @@ public function test_create_instance() { $this->assertEquals(2, $DB->count_records('choice', array('course' => $course->id))); $this->assertEquals('One more choice', $DB->get_field_select('choice', 'name', 'id = :id', array('id' => $choice->id))); - $params = new stdClass(); + $params = new \stdClass(); $params->course = $course->id; $params->option = array('fried rice', 'spring rolls', 'sweet and sour pork', 'satay beef', 'gyouza'); $choice = $this->getDataGenerator()->create_module('choice', $params); diff --git a/mod/data/tests/externallib_test.php b/mod/data/tests/externallib_test.php index b796df0fbbb7b..d13618af31019 100644 --- a/mod/data/tests/externallib_test.php +++ b/mod/data/tests/externallib_test.php @@ -14,15 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Database module external functions tests - * - * @package mod_data - * @category external - * @copyright 2015 Juan Leyva - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @since Moodle 2.9 - */ +namespace mod_data; + +use externallib_advanced_testcase; +use mod_data_external; defined('MOODLE_INTERNAL') || die(); @@ -39,7 +34,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 2.9 */ -class mod_data_external_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** @var stdClass Test module context. */ protected $context; @@ -89,12 +84,12 @@ public function setUp(): void { $this->setAdminUser(); // Setup test data. - $course = new stdClass(); + $course = new \stdClass(); $course->groupmode = SEPARATEGROUPS; $course->groupmodeforce = true; $this->course = $this->getDataGenerator()->create_course($course); $this->database = $this->getDataGenerator()->create_module('data', array('course' => $this->course->id)); - $this->context = context_module::instance($this->database->cmid); + $this->context = \context_module::instance($this->database->cmid); $this->cm = get_coursemodule_from_instance('data', $this->database->id); // Create users. @@ -139,7 +134,7 @@ public function test_mod_data_get_databases_by_courses() { $course2 = self::getDataGenerator()->create_course(); // First database. - $record = new stdClass(); + $record = new \stdClass(); $record->introformat = FORMAT_HTML; $record->course = $course1->id; // Set multilang text to check that is properly filtered to "en" only. @@ -148,7 +143,7 @@ public function test_mod_data_get_databases_by_courses() { $database1 = self::getDataGenerator()->create_module('data', $record); // Second database. - $record = new stdClass(); + $record = new \stdClass(); $record->introformat = FORMAT_HTML; $record->course = $course2->id; $database2 = self::getDataGenerator()->create_module('data', $record); @@ -172,11 +167,11 @@ public function test_mod_data_get_databases_by_courses() { $enrol->enrol_user($instance2, $student->id, $studentrole->id); // Enable multilang filter to on content and heading. - filter_manager::reset_caches(); + \filter_manager::reset_caches(); filter_set_global_state('multilang', TEXTFILTER_ON); filter_set_applies_to_strings('multilang', true); // Set WS filtering. - $wssettings = external_settings::get_instance(); + $wssettings = \external_settings::get_instance(); $wssettings->set_filter(true); // Create what we expect to be returned when querying the two courses. @@ -213,12 +208,12 @@ public function test_mod_data_get_databases_by_courses() { // Call the external function passing course ids. $result = mod_data_external::get_databases_by_courses(array($course2->id, $course1->id)); - $result = external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result); $this->assertEquals($expecteddatabases, $result['databases']); // Call the external function without passing course id. $result = mod_data_external::get_databases_by_courses(); - $result = external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result); $this->assertEquals($expecteddatabases, $result['databases']); // Unenrol user from second course and alter expected databases. @@ -227,7 +222,7 @@ public function test_mod_data_get_databases_by_courses() { // Call the external function without passing course id. $result = mod_data_external::get_databases_by_courses(); - $result = external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result); $this->assertEquals($expecteddatabases, $result['databases']); // Call for the second course we unenrolled the user from, expected warning. @@ -248,14 +243,14 @@ public function test_mod_data_get_databases_by_courses() { $expecteddatabases[0][$field] = $database1->{$field}; } $result = mod_data_external::get_databases_by_courses(); - $result = external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result); $this->assertEquals($expecteddatabases, $result['databases']); // Admin should get all the information. self::setAdminUser(); $result = mod_data_external::get_databases_by_courses(array($course1->id)); - $result = external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result); $this->assertEquals($expecteddatabases, $result['databases']); } @@ -306,7 +301,7 @@ public function test_view_database() { $sink = $this->redirectEvents(); $result = mod_data_external::view_database($this->database->id); - $result = external_api::clean_returnvalue(mod_data_external::view_database_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::view_database_returns(), $result); $events = $sink->get_events(); $this->assertCount(1, $events); @@ -336,7 +331,7 @@ public function test_get_data_access_information_student() { $this->setUser($this->student1); $result = mod_data_external::get_data_access_information($this->database->id); - $result = external_api::clean_returnvalue(mod_data_external::get_data_access_information_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_data_access_information_returns(), $result); $this->assertEquals($this->group1->id, $result['groupid']); @@ -365,7 +360,7 @@ public function test_get_data_access_information_teacher() { $this->setUser($this->teacher); $result = mod_data_external::get_data_access_information($this->database->id); - $result = external_api::clean_returnvalue(mod_data_external::get_data_access_information_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_data_access_information_returns(), $result); $this->assertEquals(0, $result['groupid']); @@ -391,7 +386,7 @@ public function test_get_data_access_information_groups() { $this->setUser($this->student1); $result = mod_data_external::get_data_access_information($this->database->id); - $result = external_api::clean_returnvalue(mod_data_external::get_data_access_information_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_data_access_information_returns(), $result); $this->assertEquals($this->group1->id, $result['groupid']); // My group is correctly found. $this->assertFalse($result['canmanageentries']); @@ -405,7 +400,7 @@ public function test_get_data_access_information_groups() { // Check the other course group in visible groups mode. $result = mod_data_external::get_data_access_information($this->database->id, $this->group2->id); - $result = external_api::clean_returnvalue(mod_data_external::get_data_access_information_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_data_access_information_returns(), $result); $this->assertEquals($this->group2->id, $result['groupid']); // The group is correctly found. $this->assertFalse($result['canmanageentries']); @@ -435,7 +430,7 @@ public function populate_database_with_entries() { // Creating test Fields with default parameter values. foreach ($fieldtypes as $fieldtype) { $fieldname = 'field-' . $count; - $record = new StdClass(); + $record = new \stdClass(); $record->name = $fieldname; $record->type = $fieldtype; $record->required = 1; @@ -494,7 +489,7 @@ public function test_get_entries() { // We may expect entries without group also. $this->setUser($this->student1); $result = mod_data_external::get_entries($this->database->id); - $result = external_api::clean_returnvalue(mod_data_external::get_entries_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_entries_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertCount(3, $result['entries']); $this->assertEquals(3, $result['totalcount']); @@ -516,7 +511,7 @@ public function test_get_entries() { // Other user in same group. $this->setUser($this->student2); $result = mod_data_external::get_entries($this->database->id); - $result = external_api::clean_returnvalue(mod_data_external::get_entries_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_entries_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertCount(4, $result['entries']); // I can see my entry not approved yet. $this->assertEquals(4, $result['totalcount']); @@ -524,7 +519,7 @@ public function test_get_entries() { // Now try with the user in the second group that must see only two entries (his group entry and the one without group). $this->setUser($this->student3); $result = mod_data_external::get_entries($this->database->id); - $result = external_api::clean_returnvalue(mod_data_external::get_entries_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_entries_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertCount(2, $result['entries']); $this->assertEquals(2, $result['totalcount']); @@ -540,7 +535,7 @@ public function test_get_entries() { // Now, as teacher we should see all (we have permissions to view all groups). $this->setUser($this->teacher); $result = mod_data_external::get_entries($this->database->id); - $result = external_api::clean_returnvalue(mod_data_external::get_entries_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_entries_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertCount(5, $result['entries']); // I can see the not approved one. $this->assertEquals(5, $result['totalcount']); @@ -556,7 +551,7 @@ public function test_get_entries() { // Basic test passing the parameter (instead having to calculate it). $this->setUser($this->student1); $result = mod_data_external::get_entries($this->database->id, $this->group1->id); - $result = external_api::clean_returnvalue(mod_data_external::get_entries_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_entries_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertCount(3, $result['entries']); $this->assertEquals(3, $result['totalcount']); @@ -564,7 +559,7 @@ public function test_get_entries() { // Test ordering (reverse). $this->setUser($this->student1); $result = mod_data_external::get_entries($this->database->id, $this->group1->id, false, null, 'DESC'); - $result = external_api::clean_returnvalue(mod_data_external::get_entries_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_entries_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertCount(3, $result['entries']); $this->assertEquals(3, $result['totalcount']); @@ -573,14 +568,14 @@ public function test_get_entries() { // Test pagination. $this->setUser($this->student1); $result = mod_data_external::get_entries($this->database->id, $this->group1->id, false, null, null, 0, 1); - $result = external_api::clean_returnvalue(mod_data_external::get_entries_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_entries_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertCount(1, $result['entries']); $this->assertEquals(3, $result['totalcount']); $this->assertEquals($entry11, $result['entries'][0]['id']); $result = mod_data_external::get_entries($this->database->id, $this->group1->id, false, null, null, 1, 1); - $result = external_api::clean_returnvalue(mod_data_external::get_entries_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_entries_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertCount(1, $result['entries']); $this->assertEquals(3, $result['totalcount']); @@ -589,7 +584,7 @@ public function test_get_entries() { // Now test the return contents. data_generate_default_template($this->database, 'listtemplate', 0, false, true); // Generate a default list template. $result = mod_data_external::get_entries($this->database->id, $this->group1->id, true, null, null, 0, 2); - $result = external_api::clean_returnvalue(mod_data_external::get_entries_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_entries_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertCount(2, $result['entries']); $this->assertEquals(3, $result['totalcount']); @@ -615,7 +610,7 @@ public function test_get_entry_visible_groups() { // Check I can see my approved group entries. $this->setUser($this->student1); $result = mod_data_external::get_entry($entry11); - $result = external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertEquals($entry11, $result['entry']['id']); $this->assertTrue($result['entry']['approved']); @@ -623,7 +618,7 @@ public function test_get_entry_visible_groups() { // Entry from other group. $result = mod_data_external::get_entry($entry21); - $result = external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertEquals($entry21, $result['entry']['id']); } @@ -638,7 +633,7 @@ public function test_get_entry_separated_groups() { // Check I can see my approved group entries. $this->setUser($this->student1); $result = mod_data_external::get_entry($entry11); - $result = external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertEquals($entry11, $result['entry']['id']); $this->assertTrue($result['entry']['approved']); @@ -647,7 +642,7 @@ public function test_get_entry_separated_groups() { // Retrieve contents. data_generate_default_template($this->database, 'singletemplate', 0, false, true); $result = mod_data_external::get_entry($entry11, true); - $result = external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertCount(9, $result['entry']['contents']); $this->assertTrue(strpos($result['entryviewcontents'], 'opt1') !== false); @@ -658,7 +653,7 @@ public function test_get_entry_separated_groups() { // This is in my group but I'm not the author. $result = mod_data_external::get_entry($entry12); - $result = external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertEquals($entry12, $result['entry']['id']); $this->assertTrue($result['entry']['approved']); @@ -666,7 +661,7 @@ public function test_get_entry_separated_groups() { $this->setUser($this->student3); $result = mod_data_external::get_entry($entry21); - $result = external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertEquals($entry21, $result['entry']['id']); $this->assertTrue($result['entry']['approved']); @@ -675,19 +670,19 @@ public function test_get_entry_separated_groups() { // As teacher I should be able to see all the entries. $this->setUser($this->teacher); $result = mod_data_external::get_entry($entry11); - $result = external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result); $this->assertEquals($entry11, $result['entry']['id']); $result = mod_data_external::get_entry($entry12); - $result = external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result); $this->assertEquals($entry12, $result['entry']['id']); // This is the not approved one. $result = mod_data_external::get_entry($entry13); - $result = external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result); $this->assertEquals($entry13, $result['entry']['id']); $result = mod_data_external::get_entry($entry21); - $result = external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result); $this->assertEquals($entry21, $result['entry']['id']); // Now, try to get an entry not approved yet. @@ -717,7 +712,7 @@ public function test_get_fields() { $this->setUser($this->student1); $result = mod_data_external::get_fields($this->database->id); - $result = external_api::clean_returnvalue(mod_data_external::get_fields_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_fields_returns(), $result); // Basically compare we retrieve all the fields and the correct values. $fields = $DB->get_records('data_fields', array('dataid' => $this->database->id), 'id'); @@ -733,7 +728,7 @@ public function test_get_fields_database_without_fields() { $this->setUser($this->student1); $result = mod_data_external::get_fields($this->database->id); - $result = external_api::clean_returnvalue(mod_data_external::get_fields_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_fields_returns(), $result); $this->assertEmpty($result['fields']); } @@ -748,19 +743,19 @@ public function test_search_entries() { $this->setUser($this->student1); // Empty search, it should return all the visible entries. $result = mod_data_external::search_entries($this->database->id, 0, false); - $result = external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result); $this->assertCount(3, $result['entries']); $this->assertEquals(3, $result['totalcount']); // Search for something that does not exists. $result = mod_data_external::search_entries($this->database->id, 0, false, 'abc'); - $result = external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result); $this->assertCount(0, $result['entries']); $this->assertEquals(0, $result['totalcount']); // Search by text matching all the entries. $result = mod_data_external::search_entries($this->database->id, 0, false, 'text'); - $result = external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result); $this->assertCount(3, $result['entries']); $this->assertEquals(3, $result['totalcount']); $this->assertEquals(3, $result['maxcount']); @@ -768,7 +763,7 @@ public function test_search_entries() { // Now as the other student I should receive my not approved entry. Apply ordering here. $this->setUser($this->student2); $result = mod_data_external::search_entries($this->database->id, 0, false, 'text', [], DATA_APPROVED, 'ASC'); - $result = external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result); $this->assertCount(4, $result['entries']); $this->assertEquals(4, $result['totalcount']); $this->assertEquals(4, $result['maxcount']); @@ -778,7 +773,7 @@ public function test_search_entries() { // Now as the other group student. $this->setUser($this->student3); $result = mod_data_external::search_entries($this->database->id, 0, false, 'text'); - $result = external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result); $this->assertCount(2, $result['entries']); $this->assertEquals(2, $result['totalcount']); $this->assertEquals(2, $result['maxcount']); @@ -788,7 +783,7 @@ public function test_search_entries() { // Same normal text search as teacher. $this->setUser($this->teacher); $result = mod_data_external::search_entries($this->database->id, 0, false, 'text'); - $result = external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result); $this->assertCount(5, $result['entries']); // I can see all groups and non approved. $this->assertEquals(5, $result['totalcount']); $this->assertEquals(5, $result['maxcount']); @@ -796,7 +791,7 @@ public function test_search_entries() { // Pagination. $this->setUser($this->teacher); $result = mod_data_external::search_entries($this->database->id, 0, false, 'text', [], DATA_TIMEADDED, 'ASC', 0, 2); - $result = external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result); $this->assertCount(2, $result['entries']); // Only 2 per page. $this->assertEquals(5, $result['totalcount']); $this->assertEquals(5, $result['maxcount']); @@ -807,7 +802,7 @@ public function test_search_entries() { ['name' => 'fn', 'value' => json_encode($this->student2->firstname)] ]; $result = mod_data_external::search_entries($this->database->id, 0, false, '', $advsearch); - $result = external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result); $this->assertCount(2, $result['entries']); $this->assertEquals(2, $result['totalcount']); $this->assertEquals(3, $result['maxcount']); @@ -819,7 +814,7 @@ public function test_search_entries() { ['name' => 'f_' . $field->id , 'value' => 'sampleurl'] ]; $result = mod_data_external::search_entries($this->database->id, 0, false, '', $advsearch); - $result = external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result); $this->assertCount(3, $result['entries']); // Found two entries matching this. $this->assertEquals(3, $result['totalcount']); $this->assertEquals(3, $result['maxcount']); @@ -832,7 +827,7 @@ public function test_search_entries() { ['name' => 'ln', 'value' => json_encode($this->student2->lastname)] ]; $result = mod_data_external::search_entries($this->database->id, 0, false, '', $advsearch); - $result = external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result); $this->assertCount(2, $result['entries']); // Only one matching everything. $this->assertEquals(2, $result['totalcount']); $this->assertEquals(3, $result['maxcount']); @@ -844,7 +839,7 @@ public function test_search_entries() { ['name' => 'f_' . $field2->id , 'value' => '98780333'], // Non existent number. ]; $result = mod_data_external::search_entries($this->database->id, 0, false, '', $advsearch); - $result = external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::search_entries_returns(), $result); $this->assertCount(0, $result['entries']); // Only one matching everything. $this->assertEquals(0, $result['totalcount']); $this->assertEquals(3, $result['maxcount']); @@ -860,7 +855,7 @@ public function test_approve_entry() { $this->setUser($this->teacher); $this->assertEquals(0, $DB->get_field('data_records', 'approved', array('id' => $entry13))); $result = mod_data_external::approve_entry($entry13); - $result = external_api::clean_returnvalue(mod_data_external::approve_entry_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::approve_entry_returns(), $result); $this->assertEquals(1, $DB->get_field('data_records', 'approved', array('id' => $entry13))); } @@ -874,7 +869,7 @@ public function test_unapprove_entry() { $this->setUser($this->teacher); $this->assertEquals(1, $DB->get_field('data_records', 'approved', array('id' => $entry11))); $result = mod_data_external::approve_entry($entry11, false); - $result = external_api::clean_returnvalue(mod_data_external::approve_entry_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::approve_entry_returns(), $result); $this->assertEquals(0, $DB->get_field('data_records', 'approved', array('id' => $entry11))); } @@ -899,12 +894,12 @@ public function test_delete_entry_as_teacher() { $this->setUser($this->teacher); $result = mod_data_external::delete_entry($entry11); - $result = external_api::clean_returnvalue(mod_data_external::delete_entry_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::delete_entry_returns(), $result); $this->assertEquals(0, $DB->count_records('data_records', array('id' => $entry11))); // Entry in other group. $result = mod_data_external::delete_entry($entry21); - $result = external_api::clean_returnvalue(mod_data_external::delete_entry_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::delete_entry_returns(), $result); $this->assertEquals(0, $DB->count_records('data_records', array('id' => $entry21))); } @@ -917,7 +912,7 @@ public function test_delete_entry_as_student() { $this->setUser($this->student1); $result = mod_data_external::delete_entry($entry11); - $result = external_api::clean_returnvalue(mod_data_external::delete_entry_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::delete_entry_returns(), $result); $this->assertEquals(0, $DB->count_records('data_records', array('id' => $entry11))); } @@ -1024,11 +1019,11 @@ public function test_add_entry() { ]; } $result = mod_data_external::add_entry($this->database->id, 0, $newentrydata); - $result = external_api::clean_returnvalue(mod_data_external::add_entry_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::add_entry_returns(), $result); $newentryid = $result['newentryid']; $result = mod_data_external::get_entry($newentryid, true); - $result = external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result); $this->assertEquals($this->student1->id, $result['entry']['userid']); $this->assertCount(9, $result['entry']['contents']); foreach ($result['entry']['contents'] as $content) { @@ -1081,7 +1076,7 @@ public function test_add_entry() { // Now, try to add another entry but removing some required data. unset($newentrydata[0]); $result = mod_data_external::add_entry($this->database->id, 0, $newentrydata); - $result = external_api::clean_returnvalue(mod_data_external::add_entry_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::add_entry_returns(), $result); $this->assertEquals(0, $result['newentryid']); $this->assertCount(0, $result['generalnotifications']); $this->assertCount(1, $result['fieldnotifications']); @@ -1094,7 +1089,7 @@ public function test_add_entry() { */ public function test_add_entry_empty_form() { $result = mod_data_external::add_entry($this->database->id, 0, []); - $result = external_api::clean_returnvalue(mod_data_external::add_entry_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::add_entry_returns(), $result); $this->assertEquals(0, $result['newentryid']); $this->assertCount(1, $result['generalnotifications']); $this->assertCount(0, $result['fieldnotifications']); @@ -1219,13 +1214,13 @@ public function test_update_entry() { ]; } $result = mod_data_external::update_entry($entry11, $newentrydata); - $result = external_api::clean_returnvalue(mod_data_external::update_entry_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::update_entry_returns(), $result); $this->assertTrue($result['updated']); $this->assertCount(0, $result['generalnotifications']); $this->assertCount(0, $result['fieldnotifications']); $result = mod_data_external::get_entry($entry11, true); - $result = external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result); $this->assertEquals($this->student1->id, $result['entry']['userid']); $this->assertCount(9, $result['entry']['contents']); foreach ($result['entry']['contents'] as $content) { @@ -1278,7 +1273,7 @@ public function test_update_entry() { // Now, try to update the entry but removing some required data. unset($newentrydata[0]); $result = mod_data_external::update_entry($entry11, $newentrydata); - $result = external_api::clean_returnvalue(mod_data_external::update_entry_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::update_entry_returns(), $result); $this->assertFalse($result['updated']); $this->assertCount(0, $result['generalnotifications']); $this->assertCount(1, $result['fieldnotifications']); @@ -1294,7 +1289,7 @@ public function test_update_entry_empty_data() { $this->setUser($this->student1); $result = mod_data_external::update_entry($entry11, []); - $result = external_api::clean_returnvalue(mod_data_external::update_entry_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::update_entry_returns(), $result); $this->assertFalse($result['updated']); $this->assertCount(1, $result['generalnotifications']); $this->assertCount(9, $result['fieldnotifications']); @@ -1347,7 +1342,7 @@ public function test_get_entry_rating_information() { $this->getDataGenerator()->enrol_user($user2->id, $this->course->id, $this->studentrole->id, 'manual'); // Rate the entry as user1. - $rating1 = new stdClass(); + $rating1 = new \stdClass(); $rating1->contextid = $this->context->id; $rating1->component = 'mod_data'; $rating1->ratingarea = 'entry'; @@ -1360,7 +1355,7 @@ public function test_get_entry_rating_information() { $rating1->id = $DB->insert_record('rating', $rating1); // Rate the entry as user2. - $rating2 = new stdClass(); + $rating2 = new \stdClass(); $rating2->contextid = $this->context->id; $rating2->component = 'mod_data'; $rating2->ratingarea = 'entry'; @@ -1375,7 +1370,7 @@ public function test_get_entry_rating_information() { // As student, retrieve ratings information. $this->setUser($this->student2); $result = mod_data_external::get_entry($entry11); - $result = external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result); $this->assertCount(1, $result['ratinginfo']['ratings']); $this->assertFalse($result['ratinginfo']['ratings'][0]['canviewaggregate']); $this->assertFalse($result['ratinginfo']['canviewall']); @@ -1385,7 +1380,7 @@ public function test_get_entry_rating_information() { // Now, as teacher, I should see the info correctly. $this->setUser($this->teacher); $result = mod_data_external::get_entry($entry11); - $result = external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result); + $result = \external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result); $this->assertCount(1, $result['ratinginfo']['ratings']); $this->assertTrue($result['ratinginfo']['ratings'][0]['canviewaggregate']); $this->assertTrue($result['ratinginfo']['canviewall']); diff --git a/mod/data/tests/generator_test.php b/mod/data/tests/generator_test.php index d89c7729d7c40..2b09fbc1c5268 100644 --- a/mod/data/tests/generator_test.php +++ b/mod/data/tests/generator_test.php @@ -14,17 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * PHPUnit data generator tests. - * - * @package mod_data - * @category phpunit - * @copyright 2012 Petr Skoda {@link http://skodak.org} - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -defined('MOODLE_INTERNAL') || die(); - +namespace mod_data; /** * PHPUnit data generator testcase. @@ -34,7 +24,7 @@ * @copyright 2012 Petr Skoda {@link http://skodak.org} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_data_generator_testcase extends advanced_testcase { +class generator_test extends \advanced_testcase { public function test_generator() { global $DB; @@ -59,7 +49,7 @@ public function test_generator() { $this->assertEquals('data', $cm->modname); $this->assertEquals($course->id, $cm->course); - $context = context_module::instance($cm->id); + $context = \context_module::instance($cm->id); $this->assertEquals($data->cmid, $context->instanceid); // test gradebook integration using low level DB access - DO NOT USE IN PLUGIN CODE! @@ -95,7 +85,7 @@ public function test_create_field() { $this->assertEquals('data', $cm->modname); $this->assertEquals($course->id, $cm->course); - $context = context_module::instance($cm->id); + $context = \context_module::instance($cm->id); $this->assertEquals($data->cmid, $context->instanceid); $fieldtypes = array('checkbox', 'date', 'menu', 'multimenu', 'number', 'radiobutton', 'text', 'textarea', 'url'); @@ -107,7 +97,7 @@ public function test_create_field() { // Creating variables dynamically. $fieldname = 'field-' . $count; - $record = new StdClass(); + $record = new \stdClass(); $record->name = $fieldname; $record->type = $fieldtype; @@ -156,7 +146,7 @@ public function test_create_entry() { $this->assertEquals('data', $cm->modname); $this->assertEquals($course->id, $cm->course); - $context = context_module::instance($cm->id); + $context = \context_module::instance($cm->id); $this->assertEquals($data->cmid, $context->instanceid); $fieldtypes = array('checkbox', 'date', 'menu', 'multimenu', 'number', 'radiobutton', 'text', 'textarea', 'url', @@ -169,7 +159,7 @@ public function test_create_entry() { // Creating variables dynamically. $fieldname = 'field-' . $count; - $record = new StdClass(); + $record = new \stdClass(); $record->name = $fieldname; $record->type = $fieldtype; $record->required = 1; @@ -237,6 +227,6 @@ public function test_create_entry() { $this->assertEquals($contents[++$contentstartid]->content, 'http://example.url'); $this->assertEquals($contents[$contentstartid]->content1, 'sampleurl'); $this->assertEquals(array('Cats', 'mice'), - array_values(core_tag_tag::get_item_tags_array('mod_data', 'data_records', $datarecordid))); + array_values(\core_tag_tag::get_item_tags_array('mod_data', 'data_records', $datarecordid))); } } diff --git a/mod/feedback/tests/generator_test.php b/mod/feedback/tests/generator_test.php index bfb3a6c41d5bb..615ed3b3bece3 100644 --- a/mod/feedback/tests/generator_test.php +++ b/mod/feedback/tests/generator_test.php @@ -14,24 +14,16 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Genarator tests. - * - * @package mod_feedback - * @copyright 2013 Ankit Agarwal - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -global $CFG; +namespace mod_feedback; /** - * Genarator tests class. + * Generator tests class. * * @package mod_feedback * @copyright 2013 Ankit Agarwal * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_feedback_generator_testcase extends advanced_testcase { +class generator_test extends \advanced_testcase { public function test_create_instance() { global $DB; @@ -227,4 +219,3 @@ public function test_create_item_pagebreak() { $this->assertCount(2, $records); } } - diff --git a/mod/folder/tests/externallib_test.php b/mod/folder/tests/externallib_test.php index 8b03ddbfe6535..06f4df8717b40 100644 --- a/mod/folder/tests/externallib_test.php +++ b/mod/folder/tests/externallib_test.php @@ -14,15 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * External mod_folder functions unit tests - * - * @package mod_folder - * @category external - * @copyright 2015 Juan Leyva - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @since Moodle 3.0 - */ +namespace mod_folder; + +use externallib_advanced_testcase; +use mod_folder_external; defined('MOODLE_INTERNAL') || die(); @@ -39,7 +34,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 3.0 */ -class mod_folder_external_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * Test view_folder @@ -53,14 +48,14 @@ public function test_view_folder() { // Setup test data. $course = $this->getDataGenerator()->create_course(); $folder = $this->getDataGenerator()->create_module('folder', array('course' => $course->id)); - $context = context_module::instance($folder->cmid); + $context = \context_module::instance($folder->cmid); $cm = get_coursemodule_from_instance('folder', $folder->id); // Test invalid instance id. try { mod_folder_external::view_folder(0); $this->fail('Exception expected due to invalid mod_folder instance id.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('invalidrecord', $e->errorcode); } @@ -70,7 +65,7 @@ public function test_view_folder() { try { mod_folder_external::view_folder($folder->id); $this->fail('Exception expected due to not enrolled user.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } @@ -82,7 +77,7 @@ public function test_view_folder() { $sink = $this->redirectEvents(); $result = mod_folder_external::view_folder($folder->id); - $result = external_api::clean_returnvalue(mod_folder_external::view_folder_returns(), $result); + $result = \external_api::clean_returnvalue(mod_folder_external::view_folder_returns(), $result); $events = $sink->get_events(); $this->assertCount(1, $events); @@ -101,12 +96,12 @@ public function test_view_folder() { assign_capability('mod/folder:view', CAP_PROHIBIT, $studentrole->id, $context->id); // Empty all the caches that may be affected by this change. accesslib_clear_all_caches_for_unit_testing(); - course_modinfo::clear_instance_cache(); + \course_modinfo::clear_instance_cache(); try { mod_folder_external::view_folder($folder->id); $this->fail('Exception expected due to missing capability.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } } @@ -129,13 +124,13 @@ public function test_mod_folder_get_folders_by_courses() { self::setUser($student); // First folder. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course1->id; $record->forcedownload = 1; $folder1 = self::getDataGenerator()->create_module('folder', $record); // Second folder. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course2->id; $record->forcedownload = 0; $folder2 = self::getDataGenerator()->create_module('folder', $record); @@ -184,14 +179,14 @@ public function test_mod_folder_get_folders_by_courses() { // Call the external function passing course ids. $result = mod_folder_external::get_folders_by_courses(array($course2->id, $course1->id)); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedfolders, $result['folders']); $this->assertCount(0, $result['warnings']); // Call the external function without passing course id. $result = mod_folder_external::get_folders_by_courses(); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedfolders, $result['folders']); $this->assertCount(0, $result['warnings']); @@ -199,7 +194,7 @@ public function test_mod_folder_get_folders_by_courses() { // Add a file to the intro. $fileintroname = "fileintro.txt"; $filerecordinline = array( - 'contextid' => context_module::instance($folder2->cmid)->id, + 'contextid' => \context_module::instance($folder2->cmid)->id, 'component' => 'mod_folder', 'filearea' => 'intro', 'itemid' => 0, @@ -211,7 +206,7 @@ public function test_mod_folder_get_folders_by_courses() { $fs->create_file_from_string($filerecordinline, 'image contents (not really)'); $result = mod_folder_external::get_folders_by_courses(array($course2->id, $course1->id)); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertCount(1, $result['folders'][0]['introfiles']); $this->assertEquals($fileintroname, $result['folders'][0]['introfiles'][0]['filename']); @@ -222,7 +217,7 @@ public function test_mod_folder_get_folders_by_courses() { // Call the external function without passing course id. $result = mod_folder_external::get_folders_by_courses(); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedfolders, $result['folders']); diff --git a/mod/folder/tests/generator_test.php b/mod/folder/tests/generator_test.php index f97af26cf323e..4744a8fb110f6 100644 --- a/mod/folder/tests/generator_test.php +++ b/mod/folder/tests/generator_test.php @@ -14,24 +14,17 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * mod_folder generator tests - * - * @package mod_folder - * @category test - * @copyright 2013 Marina Glancy - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace mod_folder; /** - * Genarator tests class for mod_folder. + * Generator tests class for mod_folder. * * @package mod_folder * @category test * @copyright 2013 Marina Glancy * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_folder_generator_testcase extends advanced_testcase { +class generator_test extends \advanced_testcase { public function test_create_instance() { global $DB, $USER; @@ -57,7 +50,7 @@ public function test_create_instance() { 'course' => $course->id, 'files' => file_get_unused_draft_itemid() ); - $usercontext = context_user::instance($USER->id); + $usercontext = \context_user::instance($USER->id); $filerecord = array('component' => 'user', 'filearea' => 'draft', 'contextid' => $usercontext->id, 'itemid' => $params['files'], 'filename' => 'file1.txt', 'filepath' => '/'); diff --git a/mod/forum/tests/externallib_test.php b/mod/forum/tests/externallib_test.php index 5ab7b916fbda3..6d293f082b0f6 100644 --- a/mod/forum/tests/externallib_test.php +++ b/mod/forum/tests/externallib_test.php @@ -14,14 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * The module forums external functions unit tests - * - * @package mod_forum - * @category external - * @copyright 2012 Mark Nelson - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace mod_forum; + +use externallib_advanced_testcase; +use mod_forum_external; defined('MOODLE_INTERNAL') || die(); @@ -30,7 +26,15 @@ require_once($CFG->dirroot . '/webservice/tests/helpers.php'); require_once($CFG->dirroot . '/mod/forum/lib.php'); -class mod_forum_external_testcase extends externallib_advanced_testcase { +/** + * The module forums external functions unit tests + * + * @package mod_forum + * @category external + * @copyright 2012 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class externallib_test extends externallib_advanced_testcase { /** * Tests set up @@ -59,9 +63,9 @@ public function tearDown(): void { * @param moodle_url|null $url * @return array */ - protected function get_expected_attachment(stored_file $file, array $values = [], ?moodle_url $url = null): array { + protected function get_expected_attachment(\stored_file $file, array $values = [], ?\moodle_url $url = null): array { if (!$url) { - $url = moodle_url::make_pluginfile_url( + $url = \moodle_url::make_pluginfile_url( $file->get_contextid(), $file->get_component(), $file->get_filearea(), @@ -115,14 +119,14 @@ public function test_mod_forum_get_forums_by_courses() { $course2 = self::getDataGenerator()->create_course(); // First forum. - $record = new stdClass(); + $record = new \stdClass(); $record->introformat = FORMAT_HTML; $record->course = $course1->id; $record->trackingtype = FORUM_TRACKING_FORCED; $forum1 = self::getDataGenerator()->create_module('forum', $record); // Second forum. - $record = new stdClass(); + $record = new \stdClass(); $record->introformat = FORMAT_HTML; $record->course = $course2->id; $record->trackingtype = FORUM_TRACKING_OFF; @@ -130,7 +134,7 @@ public function test_mod_forum_get_forums_by_courses() { $forum2->introfiles = []; // Add discussions to the forums. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course1->id; $record->userid = $user->id; $record->forum = $forum1->id; @@ -142,7 +146,7 @@ public function test_mod_forum_get_forums_by_courses() { $forum1->unreadpostscount = 0; $forum1->introfiles = []; - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course2->id; $record->userid = $user->id; $record->forum = $forum2->id; @@ -174,7 +178,7 @@ public function test_mod_forum_get_forums_by_courses() { // Assign capabilities to view forums for forum 2. $cm2 = get_coursemodule_from_id('forum', $forum2->cmid, 0, false, MUST_EXIST); - $context2 = context_module::instance($cm2->id); + $context2 = \context_module::instance($cm2->id); $newrole = create_role('Role 2', 'role2', 'Role 2 description'); $roleid2 = $this->assignUserCapability('mod/forum:viewdiscussion', $context2->id, $newrole); @@ -188,7 +192,7 @@ public function test_mod_forum_get_forums_by_courses() { // Call the external function passing course ids. $forums = mod_forum_external::get_forums_by_courses(array($course1->id, $course2->id)); - $forums = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums); + $forums = \external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums); $this->assertCount(2, $forums); foreach ($forums as $forum) { $this->assertEquals($expectedforums[$forum['id']], $forum); @@ -196,7 +200,7 @@ public function test_mod_forum_get_forums_by_courses() { // Call the external function without passing course id. $forums = mod_forum_external::get_forums_by_courses(); - $forums = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums); + $forums = \external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums); $this->assertCount(2, $forums); foreach ($forums as $forum) { $this->assertEquals($expectedforums[$forum['id']], $forum); @@ -208,7 +212,7 @@ public function test_mod_forum_get_forums_by_courses() { // Call the external function without passing course id. $forums = mod_forum_external::get_forums_by_courses(); - $forums = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums); + $forums = \external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums); $this->assertCount(1, $forums); $this->assertEquals($expectedforums[$forum1->id], $forums[0]); $this->assertTrue($forums[0]['cancreatediscussions']); @@ -216,12 +220,12 @@ public function test_mod_forum_get_forums_by_courses() { // Change the type of the forum, the user shouldn't be able to add discussions. $DB->set_field('forum', 'type', 'news', array('id' => $forum1->id)); $forums = mod_forum_external::get_forums_by_courses(); - $forums = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums); + $forums = \external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums); $this->assertFalse($forums[0]['cancreatediscussions']); // Call for the second course we unenrolled the user from. $forums = mod_forum_external::get_forums_by_courses(array($course2->id)); - $forums = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums); + $forums = \external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums); $this->assertCount(0, $forums); } @@ -243,7 +247,7 @@ public function test_mod_forum_toggle_favourite_state() { $course1 = self::getDataGenerator()->create_course(); $this->getDataGenerator()->enrol_user($user->id, $course1->id); - $record = new stdClass(); + $record = new \stdClass(); $record->introformat = FORMAT_HTML; $record->course = $course1->id; $record->trackingtype = FORUM_TRACKING_OFF; @@ -251,24 +255,24 @@ public function test_mod_forum_toggle_favourite_state() { $forum1->introfiles = []; // Add discussions to the forums. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course1->id; $record->userid = $user->id; $record->forum = $forum1->id; $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); $response = mod_forum_external::toggle_favourite_state($discussion1->id, 1); - $response = external_api::clean_returnvalue(mod_forum_external::toggle_favourite_state_returns(), $response); + $response = \external_api::clean_returnvalue(mod_forum_external::toggle_favourite_state_returns(), $response); $this->assertTrue($response['userstate']['favourited']); $response = mod_forum_external::toggle_favourite_state($discussion1->id, 0); - $response = external_api::clean_returnvalue(mod_forum_external::toggle_favourite_state_returns(), $response); + $response = \external_api::clean_returnvalue(mod_forum_external::toggle_favourite_state_returns(), $response); $this->assertFalse($response['userstate']['favourited']); $this->setUser(0); try { $response = mod_forum_external::toggle_favourite_state($discussion1->id, 0); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } } @@ -289,7 +293,7 @@ public function test_mod_forum_set_pin_state() { $course1 = self::getDataGenerator()->create_course(); $this->getDataGenerator()->enrol_user($user->id, $course1->id); - $record = new stdClass(); + $record = new \stdClass(); $record->introformat = FORMAT_HTML; $record->course = $course1->id; $record->trackingtype = FORUM_TRACKING_OFF; @@ -297,7 +301,7 @@ public function test_mod_forum_set_pin_state() { $forum1->introfiles = []; // Add discussions to the forums. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course1->id; $record->userid = $user->id; $record->forum = $forum1->id; @@ -305,17 +309,17 @@ public function test_mod_forum_set_pin_state() { try { $response = mod_forum_external::set_pin_state($discussion1->id, 1); - } catch (Exception $e) { + } catch (\Exception $e) { $this->assertEquals('cannotpindiscussions', $e->errorcode); } self::setAdminUser(); $response = mod_forum_external::set_pin_state($discussion1->id, 1); - $response = external_api::clean_returnvalue(mod_forum_external::set_pin_state_returns(), $response); + $response = \external_api::clean_returnvalue(mod_forum_external::set_pin_state_returns(), $response); $this->assertTrue($response['pinned']); $response = mod_forum_external::set_pin_state($discussion1->id, 0); - $response = external_api::clean_returnvalue(mod_forum_external::set_pin_state_returns(), $response); + $response = \external_api::clean_returnvalue(mod_forum_external::set_pin_state_returns(), $response); $this->assertFalse($response['pinned']); } @@ -332,20 +336,20 @@ public function test_mod_forum_get_discussion_posts() { // Set the CFG variable to allow track forums. $CFG->forum_trackreadposts = true; - $urlfactory = mod_forum\local\container::get_url_factory(); - $legacyfactory = mod_forum\local\container::get_legacy_data_mapper_factory(); - $entityfactory = mod_forum\local\container::get_entity_factory(); + $urlfactory = \mod_forum\local\container::get_url_factory(); + $legacyfactory = \mod_forum\local\container::get_legacy_data_mapper_factory(); + $entityfactory = \mod_forum\local\container::get_entity_factory(); // Create course to add the module. $course1 = self::getDataGenerator()->create_course(); // Create a user who can track forums. - $record = new stdClass(); + $record = new \stdClass(); $record->trackforums = true; $user1 = self::getDataGenerator()->create_user($record); // Create a bunch of other users to post. $user2 = self::getDataGenerator()->create_user(); - $user2entity = $entityfactory->get_author_from_stdclass($user2); + $user2entity = $entityfactory->get_author_from_stdClass($user2); $exporteduser2 = [ 'id' => (int) $user2->id, 'fullname' => fullname($user2), @@ -359,7 +363,7 @@ public function test_mod_forum_get_discussion_posts() { $user2->fullname = $exporteduser2['fullname']; $user3 = self::getDataGenerator()->create_user(['fullname' => "Mr Pants 1"]); - $user3entity = $entityfactory->get_author_from_stdclass($user3); + $user3entity = $entityfactory->get_author_from_stdClass($user3); $exporteduser3 = [ 'id' => (int) $user3->id, 'fullname' => fullname($user3), @@ -377,42 +381,42 @@ public function test_mod_forum_get_discussion_posts() { self::setUser($user1); // Forum with tracking off. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course1->id; $record->trackingtype = FORUM_TRACKING_OFF; // Display word count. Otherwise, word and char counts will be set to null by the forum post exporter. $record->displaywordcount = true; $forum1 = self::getDataGenerator()->create_module('forum', $record); - $forum1context = context_module::instance($forum1->cmid); + $forum1context = \context_module::instance($forum1->cmid); // Forum with tracking enabled. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course1->id; $forum2 = self::getDataGenerator()->create_module('forum', $record); $forum2cm = get_coursemodule_from_id('forum', $forum2->cmid); - $forum2context = context_module::instance($forum2->cmid); + $forum2context = \context_module::instance($forum2->cmid); // Add discussions to the forums. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course1->id; $record->userid = $user1->id; $record->forum = $forum1->id; $discussion1 = $forumgenerator->create_discussion($record); - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course1->id; $record->userid = $user2->id; $record->forum = $forum1->id; $discussion2 = $forumgenerator->create_discussion($record); - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course1->id; $record->userid = $user2->id; $record->forum = $forum2->id; $discussion3 = $forumgenerator->create_discussion($record); // Add 2 replies to the discussion 1 from different users. - $record = new stdClass(); + $record = new \stdClass(); $record->discussion = $discussion1->id; $record->parent = $discussion1->firstpost; $record->userid = $user2->id; @@ -521,7 +525,7 @@ public function test_mod_forum_get_discussion_posts() { 'edit' => null, 'delete' =>null, 'split' => null, - 'reply' => (new moodle_url('/mod/forum/post.php#mformforum', [ + 'reply' => (new \moodle_url('/mod/forum/post.php#mformforum', [ 'reply' => $discussion1reply2->id ]))->out(false), 'export' => null, @@ -582,7 +586,7 @@ public function test_mod_forum_get_discussion_posts() { 'edit' => null, 'delete' =>null, 'split' => null, - 'reply' => (new moodle_url('/mod/forum/post.php#mformforum', [ + 'reply' => (new \moodle_url('/mod/forum/post.php#mformforum', [ 'reply' => $discussion1reply1->id ]))->out(false), 'export' => null, @@ -594,7 +598,7 @@ public function test_mod_forum_get_discussion_posts() { // Test a discussion with two additional posts (total 3 posts). $posts = mod_forum_external::get_discussion_posts($discussion1->id, 'modified', 'DESC', true); - $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); + $posts = \external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); $this->assertEquals(3, count($posts['posts'])); // Unset the initial discussion post. @@ -604,7 +608,7 @@ public function test_mod_forum_get_discussion_posts() { // Check we receive the unread count correctly on tracked forum. forum_tp_count_forum_unread_posts($forum2cm, $course1, true); // Reset static cache. $result = mod_forum_external::get_forums_by_courses(array($course1->id)); - $result = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $result); + $result = \external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $result); foreach ($result as $f) { if ($f['id'] == $forum2->id) { $this->assertEquals(1, $f['unreadpostscount']); @@ -613,31 +617,31 @@ public function test_mod_forum_get_discussion_posts() { // Test discussion without additional posts. There should be only one post (the one created by the discussion). $posts = mod_forum_external::get_discussion_posts($discussion2->id, 'modified', 'DESC'); - $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); + $posts = \external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); $this->assertEquals(1, count($posts['posts'])); // Test discussion tracking on not tracked forum. $result = mod_forum_external::view_forum_discussion($discussion1->id); - $result = external_api::clean_returnvalue(mod_forum_external::view_forum_discussion_returns(), $result); + $result = \external_api::clean_returnvalue(mod_forum_external::view_forum_discussion_returns(), $result); $this->assertTrue($result['status']); $this->assertEmpty($result['warnings']); // Test posts have not been marked as read. $posts = mod_forum_external::get_discussion_posts($discussion1->id, 'modified', 'DESC'); - $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); + $posts = \external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); foreach ($posts['posts'] as $post) { $this->assertNull($post['unread']); } // Test discussion tracking on tracked forum. $result = mod_forum_external::view_forum_discussion($discussion3->id); - $result = external_api::clean_returnvalue(mod_forum_external::view_forum_discussion_returns(), $result); + $result = \external_api::clean_returnvalue(mod_forum_external::view_forum_discussion_returns(), $result); $this->assertTrue($result['status']); $this->assertEmpty($result['warnings']); // Test posts have been marked as read. $posts = mod_forum_external::get_discussion_posts($discussion3->id, 'modified', 'DESC'); - $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); + $posts = \external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); foreach ($posts['posts'] as $post) { $this->assertFalse($post['unread']); } @@ -645,7 +649,7 @@ public function test_mod_forum_get_discussion_posts() { // Check we receive 0 unread posts. forum_tp_count_forum_unread_posts($forum2cm, $course1, true); // Reset static cache. $result = mod_forum_external::get_forums_by_courses(array($course1->id)); - $result = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $result); + $result = \external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $result); foreach ($result as $f) { if ($f['id'] == $forum2->id) { $this->assertEquals(0, $f['unreadpostscount']); @@ -678,7 +682,7 @@ public function test_mod_forum_get_discussion_posts_deleted() { $forum1 = self::getDataGenerator()->create_module('forum', (object) [ 'course' => $course1->id, ]); - $forum1context = context_module::instance($forum1->cmid); + $forum1context = \context_module::instance($forum1->cmid); // Add discussions to the forum. $discussion = $generator->create_discussion((object) [ @@ -716,7 +720,7 @@ public function test_mod_forum_get_discussion_posts_deleted() { // Test where some posts have been marked as deleted. $posts = mod_forum_external::get_discussion_posts($discussion->id, 'modified', 'DESC'); - $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); + $posts = \external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); $deletedsubject = get_string('forumsubjectdeleted', 'mod_forum'); $deletedmessage = get_string('forumbodydeleted', 'mod_forum'); @@ -761,7 +765,7 @@ public function test_mod_forum_get_discussion_posts_inline_attachments() { $draftidinlineattach = file_get_unused_draft_itemid(); $draftidattach = file_get_unused_draft_itemid(); self::setUser($user); - $usercontext = context_user::instance($user->id); + $usercontext = \context_user::instance($user->id); $filepath = '/'; $filearea = 'draft'; $component = 'user'; @@ -795,14 +799,14 @@ public function test_mod_forum_get_discussion_posts_inline_attachments() { -1, $options); $posts = mod_forum_external::get_discussion_posts($discussion['discussionid'], 'modified', 'DESC'); - $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); + $posts = \external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); $post = $posts['posts'][0]; $this->assertCount(0, $post['messageinlinefiles']); $this->assertEmpty($post['messageinlinefiles']); $posts = mod_forum_external::get_discussion_posts($discussion['discussionid'], 'modified', 'DESC', true); - $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); + $posts = \external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); $post = $posts['posts'][0]; $this->assertCount(1, $post['messageinlinefiles']); $this->assertEquals('fakeimage.png', $post['messageinlinefiles'][0]['filename']); @@ -816,7 +820,7 @@ public function test_mod_forum_get_discussion_posts_qanda() { $this->resetAfterTest(true); - $record = new stdClass(); + $record = new \stdClass(); $user1 = self::getDataGenerator()->create_user($record); $user2 = self::getDataGenerator()->create_user(); @@ -829,21 +833,21 @@ public function test_mod_forum_get_discussion_posts_qanda() { $this->getDataGenerator()->enrol_user($user2->id, $course1->id); // Forum with tracking off. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course1->id; $record->type = 'qanda'; $forum1 = self::getDataGenerator()->create_module('forum', $record); - $forum1context = context_module::instance($forum1->cmid); + $forum1context = \context_module::instance($forum1->cmid); // Add discussions to the forums. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course1->id; $record->userid = $user2->id; $record->forum = $forum1->id; $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); // Add 1 reply (not the actual user). - $record = new stdClass(); + $record = new \stdClass(); $record->discussion = $discussion1->id; $record->parent = $discussion1->firstpost; $record->userid = $user2->id; @@ -851,18 +855,18 @@ public function test_mod_forum_get_discussion_posts_qanda() { // We still see only the original post. $posts = mod_forum_external::get_discussion_posts($discussion1->id, 'modified', 'DESC'); - $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); + $posts = \external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); $this->assertEquals(1, count($posts['posts'])); // Add a new reply, the user is going to be able to see only the original post and their new post. - $record = new stdClass(); + $record = new \stdClass(); $record->discussion = $discussion1->id; $record->parent = $discussion1->firstpost; $record->userid = $user1->id; $discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); $posts = mod_forum_external::get_discussion_posts($discussion1->id, 'modified', 'DESC'); - $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); + $posts = \external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); $this->assertEquals(2, count($posts['posts'])); // Now, we can fake the time of the user post, so he can se the rest of the discussion posts. @@ -870,7 +874,7 @@ public function test_mod_forum_get_discussion_posts_qanda() { $DB->update_record('forum_posts', $discussion1reply2); $posts = mod_forum_external::get_discussion_posts($discussion1->id, 'modified', 'DESC'); - $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); + $posts = \external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); $this->assertEquals(3, count($posts['posts'])); } @@ -886,7 +890,7 @@ public function test_mod_forum_get_forum_discussions_paginated() { $CFG->forum_trackreadposts = true; // Create a user who can track forums. - $record = new stdClass(); + $record = new \stdClass(); $record->trackforums = true; $user1 = self::getDataGenerator()->create_user($record); // Create a bunch of other users to post. @@ -901,20 +905,20 @@ public function test_mod_forum_get_forum_discussions_paginated() { $course1 = self::getDataGenerator()->create_course(); // First forum with tracking off. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course1->id; $record->trackingtype = FORUM_TRACKING_OFF; $forum1 = self::getDataGenerator()->create_module('forum', $record); // Add discussions to the forums. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course1->id; $record->userid = $user1->id; $record->forum = $forum1->id; $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); // Add three replies to the discussion 1 from different users. - $record = new stdClass(); + $record = new \stdClass(); $record->discussion = $discussion1->id; $record->parent = $discussion1->firstpost; $record->userid = $user2->id; @@ -945,7 +949,7 @@ public function test_mod_forum_get_forum_discussions_paginated() { // Assign capabilities to view discussions for forum 1. $cm = get_coursemodule_from_id('forum', $forum1->cmid, 0, false, MUST_EXIST); - $context = context_module::instance($cm->id); + $context = \context_module::instance($cm->id); $newrole = create_role('Role 2', 'role2', 'Role 2 description'); $this->assignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole); @@ -989,18 +993,18 @@ public function test_mod_forum_get_forum_discussions_paginated() { // Call the external function passing forum id. $discussions = mod_forum_external::get_forum_discussions_paginated($forum1->id); - $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions); + $discussions = \external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions); $expectedreturn = array( 'discussions' => array($expecteddiscussions), 'warnings' => array() ); // Wait the theme to be loaded (the external_api call does that) to generate the user profiles. - $userpicture = new user_picture($user1); + $userpicture = new \user_picture($user1); $userpicture->size = 1; // Size f1. $expectedreturn['discussions'][0]['userpictureurl'] = $userpicture->get_url($PAGE)->out(false); - $userpicture = new user_picture($user4); + $userpicture = new \user_picture($user4); $userpicture->size = 1; // Size f1. $expectedreturn['discussions'][0]['usermodifiedpictureurl'] = $userpicture->get_url($PAGE)->out(false); @@ -1011,7 +1015,7 @@ public function test_mod_forum_get_forum_discussions_paginated() { try { mod_forum_external::get_forum_discussions_paginated($forum1->id); $this->fail('Exception expected due to missing capability.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('noviewdiscussionspermission', $e->errorcode); } @@ -1022,13 +1026,13 @@ public function test_mod_forum_get_forum_discussions_paginated() { try { mod_forum_external::get_forum_discussions_paginated($forum1->id); $this->fail('Exception expected due to being unenrolled from the course.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } $this->setAdminUser(); $discussions = mod_forum_external::get_forum_discussions_paginated($forum1->id); - $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions); + $discussions = \external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions); $this->assertTrue($discussions['discussions'][0]['canlock']); } @@ -1046,13 +1050,13 @@ public function test_mod_forum_get_forum_discussions_paginated_qanda() { $user2 = self::getDataGenerator()->create_user(); // First forum with tracking off. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $record->type = 'qanda'; $forum = self::getDataGenerator()->create_module('forum', $record); // Add discussions to the forums. - $discussionrecord = new stdClass(); + $discussionrecord = new \stdClass(); $discussionrecord->course = $course->id; $discussionrecord->userid = $user2->id; $discussionrecord->forum = $forum->id; @@ -1060,7 +1064,7 @@ public function test_mod_forum_get_forum_discussions_paginated_qanda() { self::setAdminUser(); $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id); - $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions); + $discussions = \external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions); $this->assertCount(1, $discussions['discussions']); $this->assertCount(0, $discussions['warnings']); @@ -1069,7 +1073,7 @@ public function test_mod_forum_get_forum_discussions_paginated_qanda() { $this->getDataGenerator()->enrol_user($user1->id, $course->id); $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id); - $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions); + $discussions = \external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions); $this->assertCount(1, $discussions['discussions']); $this->assertCount(0, $discussions['warnings']); @@ -1088,7 +1092,7 @@ public function test_mod_forum_get_forum_discussions() { $CFG->forum_trackreadposts = true; // Create a user who can track forums. - $record = new stdClass(); + $record = new \stdClass(); $record->trackforums = true; $user1 = self::getDataGenerator()->create_user($record); // Create a bunch of other users to post. @@ -1103,20 +1107,20 @@ public function test_mod_forum_get_forum_discussions() { $course1 = self::getDataGenerator()->create_course(); // First forum with tracking off. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course1->id; $record->trackingtype = FORUM_TRACKING_OFF; $forum1 = self::getDataGenerator()->create_module('forum', $record); // Add discussions to the forums. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course1->id; $record->userid = $user1->id; $record->forum = $forum1->id; $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); // Add three replies to the discussion 1 from different users. - $record = new stdClass(); + $record = new \stdClass(); $record->discussion = $discussion1->id; $record->parent = $discussion1->firstpost; $record->userid = $user2->id; @@ -1147,7 +1151,7 @@ public function test_mod_forum_get_forum_discussions() { // Assign capabilities to view discussions for forum 1. $cm = get_coursemodule_from_id('forum', $forum1->cmid, 0, false, MUST_EXIST); - $context = context_module::instance($cm->id); + $context = \context_module::instance($cm->id); $newrole = create_role('Role 2', 'role2', 'Role 2 description'); $this->assignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole); @@ -1193,18 +1197,18 @@ public function test_mod_forum_get_forum_discussions() { // Call the external function passing forum id. $discussions = mod_forum_external::get_forum_discussions($forum1->id); - $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions); + $discussions = \external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions); $expectedreturn = array( 'discussions' => array($expecteddiscussions), 'warnings' => array() ); // Wait the theme to be loaded (the external_api call does that) to generate the user profiles. - $userpicture = new user_picture($user1); + $userpicture = new \user_picture($user1); $userpicture->size = 2; // Size f2. $expectedreturn['discussions'][0]['userpictureurl'] = $userpicture->get_url($PAGE)->out(false); - $userpicture = new user_picture($user4); + $userpicture = new \user_picture($user4); $userpicture->size = 2; // Size f2. $expectedreturn['discussions'][0]['usermodifiedpictureurl'] = $userpicture->get_url($PAGE)->out(false); @@ -1214,7 +1218,7 @@ public function test_mod_forum_get_forum_discussions() { $t = mod_forum_external::toggle_favourite_state($discussion1->id, 1); $expectedreturn['discussions'][0]['starred'] = true; $discussions = mod_forum_external::get_forum_discussions($forum1->id); - $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions); + $discussions = \external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions); $this->assertEquals($expectedreturn, $discussions); // Call without required view discussion capability. @@ -1222,7 +1226,7 @@ public function test_mod_forum_get_forum_discussions() { try { mod_forum_external::get_forum_discussions($forum1->id); $this->fail('Exception expected due to missing capability.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('noviewdiscussionspermission', $e->errorcode); } @@ -1233,13 +1237,13 @@ public function test_mod_forum_get_forum_discussions() { try { mod_forum_external::get_forum_discussions($forum1->id); $this->fail('Exception expected due to being unenrolled from the course.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } $this->setAdminUser(); $discussions = mod_forum_external::get_forum_discussions($forum1->id); - $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions); + $discussions = \external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions); $this->assertTrue($discussions['discussions'][0]['canlock']); } @@ -1255,7 +1259,7 @@ public function test_mod_forum_get_forum_discussions_sorting() { $CFG->forum_trackreadposts = true; // Create a user who can track forums. - $record = new stdClass(); + $record = new \stdClass(); $record->trackforums = true; $user1 = self::getDataGenerator()->create_user($record); // Create a bunch of other users to post. @@ -1283,19 +1287,19 @@ public function test_mod_forum_get_forum_discussions_sorting() { $enrol->enrol_user($instance1, $user1->id); // First forum with tracking off. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course1->id; $record->trackingtype = FORUM_TRACKING_OFF; $forum1 = self::getDataGenerator()->create_module('forum', $record); // Assign capabilities to view discussions for forum 1. $cm = get_coursemodule_from_id('forum', $forum1->cmid, 0, false, MUST_EXIST); - $context = context_module::instance($cm->id); + $context = \context_module::instance($cm->id); $newrole = create_role('Role 2', 'role2', 'Role 2 description'); $this->assignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole); // Add discussions to the forums. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course1->id; $record->userid = $user1->id; $record->forum = $forum1->id; @@ -1303,7 +1307,7 @@ public function test_mod_forum_get_forum_discussions_sorting() { sleep(1); // Add three replies to the discussion 1 from different users. - $record = new stdClass(); + $record = new \stdClass(); $record->discussion = $discussion1->id; $record->parent = $discussion1->firstpost; $record->userid = $user2->id; @@ -1320,7 +1324,7 @@ public function test_mod_forum_get_forum_discussions_sorting() { sleep(1); // Create discussion2. - $record2 = new stdClass(); + $record2 = new \stdClass(); $record2->course = $course1->id; $record2->userid = $user1->id; $record2->forum = $forum1->id; @@ -1328,7 +1332,7 @@ public function test_mod_forum_get_forum_discussions_sorting() { sleep(1); // Add one reply to the discussion 2. - $record2 = new stdClass(); + $record2 = new \stdClass(); $record2->discussion = $discussion2->id; $record2->parent = $discussion2->firstpost; $record2->userid = $user2->id; @@ -1336,7 +1340,7 @@ public function test_mod_forum_get_forum_discussions_sorting() { sleep(1); // Create discussion 3. - $record3 = new stdClass(); + $record3 = new \stdClass(); $record3->course = $course1->id; $record3->userid = $user1->id; $record3->forum = $forum1->id; @@ -1344,7 +1348,7 @@ public function test_mod_forum_get_forum_discussions_sorting() { sleep(1); // Add two replies to the discussion 3. - $record3 = new stdClass(); + $record3 = new \stdClass(); $record3->discussion = $discussion3->id; $record3->parent = $discussion3->firstpost; $record3->userid = $user2->id; @@ -1357,7 +1361,7 @@ public function test_mod_forum_get_forum_discussions_sorting() { // Call the external function passing forum id. $discussions = mod_forum_external::get_forum_discussions($forum1->id); - $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions); + $discussions = \external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions); // Discussions should be ordered by last post date in descending order by default. $this->assertEquals($discussions['discussions'][0]['discussion'], $discussion3->id); $this->assertEquals($discussions['discussions'][1]['discussion'], $discussion2->id); @@ -1368,7 +1372,7 @@ public function test_mod_forum_get_forum_discussions_sorting() { // Call the external function passing forum id and sort order parameter. $discussions = mod_forum_external::get_forum_discussions($forum1->id, $discussionlistvault::SORTORDER_LASTPOST_ASC); - $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions); + $discussions = \external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions); // Discussions should be ordered by last post date in ascending order. $this->assertEquals($discussions['discussions'][0]['discussion'], $discussion1->id); $this->assertEquals($discussions['discussions'][1]['discussion'], $discussion2->id); @@ -1376,7 +1380,7 @@ public function test_mod_forum_get_forum_discussions_sorting() { // Call the external function passing forum id and sort order parameter. $discussions = mod_forum_external::get_forum_discussions($forum1->id, $discussionlistvault::SORTORDER_CREATED_DESC); - $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions); + $discussions = \external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions); // Discussions should be ordered by discussion creation date in descending order. $this->assertEquals($discussions['discussions'][0]['discussion'], $discussion3->id); $this->assertEquals($discussions['discussions'][1]['discussion'], $discussion2->id); @@ -1384,7 +1388,7 @@ public function test_mod_forum_get_forum_discussions_sorting() { // Call the external function passing forum id and sort order parameter. $discussions = mod_forum_external::get_forum_discussions($forum1->id, $discussionlistvault::SORTORDER_CREATED_ASC); - $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions); + $discussions = \external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions); // Discussions should be ordered by discussion creation date in ascending order. $this->assertEquals($discussions['discussions'][0]['discussion'], $discussion1->id); $this->assertEquals($discussions['discussions'][1]['discussion'], $discussion2->id); @@ -1392,7 +1396,7 @@ public function test_mod_forum_get_forum_discussions_sorting() { // Call the external function passing forum id and sort order parameter. $discussions = mod_forum_external::get_forum_discussions($forum1->id, $discussionlistvault::SORTORDER_REPLIES_DESC); - $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions); + $discussions = \external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions); // Discussions should be ordered by the number of replies in descending order. $this->assertEquals($discussions['discussions'][0]['discussion'], $discussion1->id); $this->assertEquals($discussions['discussions'][1]['discussion'], $discussion3->id); @@ -1400,7 +1404,7 @@ public function test_mod_forum_get_forum_discussions_sorting() { // Call the external function passing forum id and sort order parameter. $discussions = mod_forum_external::get_forum_discussions($forum1->id, $discussionlistvault::SORTORDER_REPLIES_ASC); - $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions); + $discussions = \external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions); // Discussions should be ordered by the number of replies in ascending order. $this->assertEquals($discussions['discussions'][0]['discussion'], $discussion2->id); $this->assertEquals($discussions['discussions'][1]['discussion'], $discussion3->id); @@ -1412,7 +1416,7 @@ public function test_mod_forum_get_forum_discussions_sorting() { // Call the external function passing forum id. $discussions = mod_forum_external::get_forum_discussions($forum1->id); - $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions); + $discussions = \external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions); // Discussions should be ordered by last post date in descending order by default. // Pinned discussions should be at the top of the list. $this->assertEquals($discussions['discussions'][0]['discussion'], $discussion2->id); @@ -1421,7 +1425,7 @@ public function test_mod_forum_get_forum_discussions_sorting() { // Call the external function passing forum id and sort order parameter. $discussions = mod_forum_external::get_forum_discussions($forum1->id, $discussionlistvault::SORTORDER_LASTPOST_ASC); - $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions); + $discussions = \external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions); // Discussions should be ordered by last post date in ascending order. // Pinned discussions should be at the top of the list. $this->assertEquals($discussions['discussions'][0]['discussion'], $discussion2->id); @@ -1446,14 +1450,14 @@ public function test_add_discussion_post() { $course = self::getDataGenerator()->create_course(array('groupmode' => VISIBLEGROUPS, 'groupmodeforce' => 0)); // Forum with tracking off. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $forum = self::getDataGenerator()->create_module('forum', $record); $cm = get_coursemodule_from_id('forum', $forum->cmid, 0, false, MUST_EXIST); - $forumcontext = context_module::instance($forum->cmid); + $forumcontext = \context_module::instance($forum->cmid); // Add discussions to the forums. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $record->userid = $user->id; $record->forum = $forum->id; @@ -1464,7 +1468,7 @@ public function test_add_discussion_post() { try { mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...'); $this->fail('Exception expected due to being unenrolled from the course.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } @@ -1472,10 +1476,10 @@ public function test_add_discussion_post() { $this->getDataGenerator()->enrol_user($otheruser->id, $course->id); $createdpost = mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...'); - $createdpost = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $createdpost); + $createdpost = \external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $createdpost); $posts = mod_forum_external::get_discussion_posts($discussion->id, 'modified', 'ASC'); - $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); + $posts = \external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); // We receive the discussion and the post. $this->assertEquals(2, count($posts['posts'])); @@ -1499,7 +1503,7 @@ public function test_add_discussion_post() { foreach ($formats as $format) { $createdpost = mod_forum_external::add_discussion_post($discussion->firstpost, 'with some format', 'some formatted here...', $options, $format); - $createdpost = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $createdpost); + $createdpost = \external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $createdpost); $dbformat = $DB->get_field('forum_posts', 'messageformat', ['id' => $createdpost['postid']]); $this->assertEquals($format, $dbformat); } @@ -1510,7 +1514,7 @@ public function test_add_discussion_post() { $options = [['name' => 'topreferredformat', 'value' => true]]; $createdpost = mod_forum_external::add_discussion_post($discussion->firstpost, 'interesting subject', 'with some https://example.com link', $options, FORMAT_MOODLE); - $createdpost = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $createdpost); + $createdpost = \external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $createdpost); $dbpost = $DB->get_record('forum_posts', ['id' => $createdpost['postid']]); // Format HTML and content converted, we should get. $this->assertEquals(FORMAT_HTML, $dbpost->messageformat); @@ -1521,7 +1525,7 @@ public function test_add_discussion_post() { $draftidinlineattach = file_get_unused_draft_itemid(); $draftidattach = file_get_unused_draft_itemid(); self::setUser($user); - $usercontext = context_user::instance($user->id); + $usercontext = \context_user::instance($user->id); $filepath = '/'; $filearea = 'draft'; $component = 'user'; @@ -1551,10 +1555,10 @@ public function test_add_discussion_post() { . '" alt="inlineimage">.'; $createdpost = mod_forum_external::add_discussion_post($discussion->firstpost, 'new post inline attachment', $dummytext, $options); - $createdpost = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $createdpost); + $createdpost = \external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $createdpost); $posts = mod_forum_external::get_discussion_posts($discussion->id, 'modified', 'ASC'); - $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); + $posts = \external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); // We receive the discussion and the post. // Can't guarantee order of posts during tests. $postfound = false; @@ -1585,7 +1589,7 @@ public function test_add_discussion_post() { try { mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...'); $this->fail('Exception expected due to invalid permissions for posting.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('nopostforum', $e->errorcode); } } @@ -1608,13 +1612,13 @@ public function test_add_discussion_post_subscribe_discussion() { $this->getDataGenerator()->enrol_user($user->id, $course->id); // Forum with tracking off. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $forum = self::getDataGenerator()->create_module('forum', $record); $cm = get_coursemodule_from_id('forum', $forum->cmid, 0, false, MUST_EXIST); // Add discussions to the forums. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $record->userid = $admin->id; $record->forum = $forum->id; @@ -1629,7 +1633,7 @@ public function test_add_discussion_post_subscribe_discussion() { mod_forum_external::add_discussion_post($discussion1->firstpost, 'some subject', 'some text here...'); $posts = mod_forum_external::get_discussion_posts($discussion1->id, 'modified', 'ASC'); - $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); + $posts = \external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); // We receive the discussion and the post. $this->assertEquals(2, count($posts['posts'])); // The user should be subscribed to the discussion after adding a discussion post. @@ -1642,7 +1646,7 @@ public function test_add_discussion_post_subscribe_discussion() { mod_forum_external::add_discussion_post($discussion1->firstpost, 'some subject 1', 'some text here 1...'); $posts = mod_forum_external::get_discussion_posts($discussion1->id, 'modified', 'ASC'); - $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); + $posts = \external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); // We receive the discussion and the post. $this->assertEquals(3, count($posts['posts'])); // The user should still be subscribed to the discussion after adding a discussion post. @@ -1653,7 +1657,7 @@ public function test_add_discussion_post_subscribe_discussion() { mod_forum_external::add_discussion_post($discussion2->firstpost, 'some subject 2', 'some text here 2...'); $posts = mod_forum_external::get_discussion_posts($discussion2->id, 'modified', 'ASC'); - $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); + $posts = \external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); // We receive the discussion and the post. $this->assertEquals(2, count($posts['posts'])); // The user should still not be subscribed to the discussion after adding a discussion post. @@ -1669,7 +1673,7 @@ public function test_add_discussion_post_subscribe_discussion() { $options); $posts = mod_forum_external::get_discussion_posts($discussion2->id, 'modified', 'ASC'); - $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); + $posts = \external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); // We receive the discussion and the post. $this->assertEquals(3, count($posts['posts'])); // The user should now be subscribed to the discussion after adding a discussion post. @@ -1690,7 +1694,7 @@ public function test_add_discussion() { $user2 = self::getDataGenerator()->create_user(); // First forum with tracking off. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $record->type = 'news'; $forum = self::getDataGenerator()->create_module('forum', $record); @@ -1701,16 +1705,16 @@ public function test_add_discussion() { try { mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...'); $this->fail('Exception expected due to invalid permissions.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('cannotcreatediscussion', $e->errorcode); } self::setAdminUser(); $createddiscussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...'); - $createddiscussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $createddiscussion); + $createddiscussion = \external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $createddiscussion); $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id); - $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions); + $discussions = \external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions); $this->assertCount(1, $discussions['discussions']); $this->assertCount(0, $discussions['warnings']); @@ -1725,7 +1729,7 @@ public function test_add_discussion() { 'value' => true))); $discussion3 = mod_forum_external::add_discussion($forum->id, 'the non pinnedsubject', 'some 3 text here...'); $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id); - $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions); + $discussions = \external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions); $this->assertCount(3, $discussions['discussions']); $this->assertEquals($discussion2pinned['discussionid'], $discussions['discussions'][0]['discussion']); @@ -1737,7 +1741,7 @@ public function test_add_discussion() { $draftidinlineattach = file_get_unused_draft_itemid(); $draftidattach = file_get_unused_draft_itemid(); - $usercontext = context_user::instance($USER->id); + $usercontext = \context_user::instance($USER->id); $filepath = '/'; $filearea = 'draft'; $component = 'user'; @@ -1767,10 +1771,10 @@ public function test_add_discussion() { array('name' => 'attachmentsid', 'value' => $draftidattach)); $createddiscussion = mod_forum_external::add_discussion($forum->id, 'the inline attachment subject', $dummytext, -1, $options); - $createddiscussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $createddiscussion); + $createddiscussion = \external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $createddiscussion); $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id); - $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions); + $discussions = \external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions); $this->assertCount(4, $discussions['discussions']); $this->assertCount(0, $createddiscussion['warnings']); @@ -1805,7 +1809,7 @@ public function test_add_discussion_in_course_with_groups() { $this->getDataGenerator()->enrol_user($user->id, $course->id); // Forum forcing separate gropus. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $forum = self::getDataGenerator()->create_module('forum', $record, array('groupmode' => SEPARATEGROUPS)); @@ -1816,14 +1820,14 @@ public function test_add_discussion_in_course_with_groups() { try { mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...'); $this->fail('Exception expected due to invalid group permissions.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('cannotcreatediscussion', $e->errorcode); } try { mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...', 0); $this->fail('Exception expected due to invalid group permissions.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('cannotcreatediscussion', $e->errorcode); } @@ -1834,7 +1838,7 @@ public function test_add_discussion_in_course_with_groups() { try { mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...', $group->id); $this->fail('Exception expected due to invalid group permissions.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('cannotcreatediscussion', $e->errorcode); } @@ -1845,16 +1849,16 @@ public function test_add_discussion_in_course_with_groups() { try { mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...', $group->id + 1); $this->fail('Exception expected due to invalid group.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('cannotcreatediscussion', $e->errorcode); } // Nost add the discussion using a valid group. $discussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...', $group->id); - $discussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $discussion); + $discussion = \external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $discussion); $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id); - $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions); + $discussions = \external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions); $this->assertCount(1, $discussions['discussions']); $this->assertCount(0, $discussions['warnings']); @@ -1863,10 +1867,10 @@ public function test_add_discussion_in_course_with_groups() { // Now add a discussions without indicating a group. The function should guess the correct group. $discussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...'); - $discussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $discussion); + $discussion = \external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $discussion); $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id); - $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions); + $discussions = \external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions); $this->assertCount(2, $discussions['discussions']); $this->assertCount(0, $discussions['warnings']); @@ -1879,10 +1883,10 @@ public function test_add_discussion_in_course_with_groups() { // Now add a discussions without indicating a group. The function should guess the correct group (the first one). $discussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...'); - $discussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $discussion); + $discussion = \external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $discussion); $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id); - $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions); + $discussions = \external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions); $this->assertCount(3, $discussions['discussions']); $this->assertCount(0, $discussions['warnings']); @@ -1905,12 +1909,12 @@ public function test_set_lock_state() { $studentrole = $DB->get_record('role', array('shortname' => 'student')); // First forum with tracking off. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $record->type = 'news'; $forum = self::getDataGenerator()->create_module('forum', $record); - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $record->userid = $user->id; $record->forum = $forum->id; @@ -1924,20 +1928,20 @@ public function test_set_lock_state() { try { $result = mod_forum_external::set_lock_state($forum->id, $discussion->id, 0); $this->fail('Exception expected due to missing capability.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('errorcannotlock', $e->errorcode); } // Set the lock. self::setAdminUser(); $result = mod_forum_external::set_lock_state($forum->id, $discussion->id, 0); - $result = external_api::clean_returnvalue(mod_forum_external::set_lock_state_returns(), $result); + $result = \external_api::clean_returnvalue(mod_forum_external::set_lock_state_returns(), $result); $this->assertTrue($result['locked']); $this->assertNotEquals(0, $result['times']['locked']); // Unset the lock. $result = mod_forum_external::set_lock_state($forum->id, $discussion->id, time()); - $result = external_api::clean_returnvalue(mod_forum_external::set_lock_state_returns(), $result); + $result = \external_api::clean_returnvalue(mod_forum_external::set_lock_state_returns(), $result); $this->assertFalse($result['locked']); $this->assertEquals('0', $result['times']['locked']); } @@ -1955,7 +1959,7 @@ public function test_can_add_discussion() { $user = self::getDataGenerator()->create_user(); // First forum with tracking off. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $record->type = 'news'; $forum = self::getDataGenerator()->create_module('forum', $record); @@ -1965,7 +1969,7 @@ public function test_can_add_discussion() { $this->getDataGenerator()->enrol_user($user->id, $course->id); $result = mod_forum_external::can_add_discussion($forum->id); - $result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result); + $result = \external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result); $this->assertFalse($result['status']); $this->assertFalse($result['canpindiscussions']); $this->assertTrue($result['cancreateattachment']); @@ -1973,7 +1977,7 @@ public function test_can_add_discussion() { // Disable attachments. $DB->set_field('forum', 'maxattachments', 0, array('id' => $forum->id)); $result = mod_forum_external::can_add_discussion($forum->id); - $result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result); + $result = \external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result); $this->assertFalse($result['status']); $this->assertFalse($result['canpindiscussions']); $this->assertFalse($result['cancreateattachment']); @@ -1981,7 +1985,7 @@ public function test_can_add_discussion() { self::setAdminUser(); $result = mod_forum_external::can_add_discussion($forum->id); - $result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result); + $result = \external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result); $this->assertTrue($result['status']); $this->assertTrue($result['canpindiscussions']); $this->assertTrue($result['cancreateattachment']); @@ -2006,12 +2010,12 @@ public function test_can_add_discussion_after_cutoff() { $this->getDataGenerator()->enrol_user($user->id, $course->id); $result = mod_forum_external::can_add_discussion($forum->id); - $result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result); + $result = \external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result); $this->assertFalse($result['status']); self::setAdminUser(); $result = mod_forum_external::can_add_discussion($forum->id); - $result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result); + $result = \external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result); $this->assertTrue($result['status']); } @@ -2040,16 +2044,16 @@ public function test_mod_forum_get_discussion_rating_information() { $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id, 'manual'); // Create the forum. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; // Set Aggregate type = Average of ratings. $record->assessed = RATING_AGGREGATE_AVERAGE; $record->scale = 100; $forum = self::getDataGenerator()->create_module('forum', $record); - $context = context_module::instance($forum->cmid); + $context = \context_module::instance($forum->cmid); // Add discussion to the forum. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $record->userid = $user1->id; $record->forum = $forum->id; @@ -2059,7 +2063,7 @@ public function test_mod_forum_get_discussion_rating_information() { $post = $DB->get_record('forum_posts', array('discussion' => $discussion->id)); // Rate the discussion as user2. - $rating1 = new stdClass(); + $rating1 = new \stdClass(); $rating1->contextid = $context->id; $rating1->component = 'mod_forum'; $rating1->ratingarea = 'post'; @@ -2072,7 +2076,7 @@ public function test_mod_forum_get_discussion_rating_information() { $rating1->id = $DB->insert_record('rating', $rating1); // Rate the discussion as user3. - $rating2 = new stdClass(); + $rating2 = new \stdClass(); $rating2->contextid = $context->id; $rating2->component = 'mod_forum'; $rating2->ratingarea = 'post'; @@ -2087,7 +2091,7 @@ public function test_mod_forum_get_discussion_rating_information() { // Retrieve the rating for the post as student. $this->setUser($user1); $posts = mod_forum_external::get_discussion_posts($discussion->id, 'id', 'DESC'); - $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); + $posts = \external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); $this->assertCount(1, $posts['ratinginfo']['ratings']); $this->assertTrue($posts['ratinginfo']['ratings'][0]['canviewaggregate']); $this->assertFalse($posts['ratinginfo']['canviewall']); @@ -2098,7 +2102,7 @@ public function test_mod_forum_get_discussion_rating_information() { // Retrieve the rating for the post as teacher. $this->setUser($teacher); $posts = mod_forum_external::get_discussion_posts($discussion->id, 'id', 'DESC'); - $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); + $posts = \external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); $this->assertCount(1, $posts['ratinginfo']['ratings']); $this->assertTrue($posts['ratinginfo']['ratings'][0]['canviewaggregate']); $this->assertTrue($posts['ratinginfo']['canviewall']); @@ -2118,7 +2122,7 @@ public function test_mod_forum_get_forum_access_information() { $student = self::getDataGenerator()->create_user(); $course = self::getDataGenerator()->create_course(); // Create the forum. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $forum = self::getDataGenerator()->create_module('forum', $record); @@ -2127,7 +2131,7 @@ public function test_mod_forum_get_forum_access_information() { self::setUser($student); $result = mod_forum_external::get_forum_access_information($forum->id); - $result = external_api::clean_returnvalue(mod_forum_external::get_forum_access_information_returns(), $result); + $result = \external_api::clean_returnvalue(mod_forum_external::get_forum_access_information_returns(), $result); // Check default values for capabilities. $enabledcaps = array('canviewdiscussion', 'canstartdiscussion', 'canreplypost', 'canviewrating', 'cancreateattachment', @@ -2149,7 +2153,7 @@ public function test_mod_forum_get_forum_access_information() { accesslib_clear_all_caches_for_unit_testing(); $result = mod_forum_external::get_forum_access_information($forum->id); - $result = external_api::clean_returnvalue(mod_forum_external::get_forum_access_information_returns(), $result); + $result = \external_api::clean_returnvalue(mod_forum_external::get_forum_access_information_returns(), $result); unset($result['warnings']); foreach ($result as $capname => $capvalue) { if (in_array($capname, $enabledcaps)) { @@ -2174,11 +2178,11 @@ public function test_add_discussion_post_private() { $course = self::getDataGenerator()->create_course(); // Standard forum. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $forum = self::getDataGenerator()->create_module('forum', $record); $cm = get_coursemodule_from_id('forum', $forum->cmid, 0, false, MUST_EXIST); - $forumcontext = context_module::instance($forum->cmid); + $forumcontext = \context_module::instance($forum->cmid); $generator = self::getDataGenerator()->get_plugin_generator('mod_forum'); // Create an enrol users. @@ -2193,7 +2197,7 @@ public function test_add_discussion_post_private() { // Add a new discussion to the forum. self::setUser($student1); - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $record->userid = $student1->id; $record->forum = $forum->id; @@ -2207,7 +2211,7 @@ public function test_add_discussion_post_private() { 'value' => true, ], ]); - $post = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $post); + $post = \external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $post); $privatereply = $DB->get_record('forum_posts', array('id' => $post['postid'])); $this->assertEquals($student1->id, $privatereply->privatereplyto); // Bump the time of the private reply to ensure order. @@ -2218,28 +2222,28 @@ public function test_add_discussion_post_private() { // The teacher will receive their private reply. self::setUser($teacher1); $posts = mod_forum_external::get_discussion_posts($discussion->id, 'id', 'DESC'); - $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); + $posts = \external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); $this->assertEquals(2, count($posts['posts'])); $this->assertTrue($posts['posts'][0]['isprivatereply']); // Another teacher on the course will also receive the private reply. self::setUser($teacher2); $posts = mod_forum_external::get_discussion_posts($discussion->id, 'id', 'DESC'); - $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); + $posts = \external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); $this->assertEquals(2, count($posts['posts'])); $this->assertTrue($posts['posts'][0]['isprivatereply']); // The student will receive the private reply. self::setUser($student1); $posts = mod_forum_external::get_discussion_posts($discussion->id, 'id', 'DESC'); - $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); + $posts = \external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); $this->assertEquals(2, count($posts['posts'])); $this->assertTrue($posts['posts'][0]['isprivatereply']); // Another student will not receive the private reply. self::setUser($student2); $posts = mod_forum_external::get_discussion_posts($discussion->id, 'id', 'ASC'); - $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); + $posts = \external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); $this->assertEquals(1, count($posts['posts'])); $this->assertFalse($posts['posts'][0]['isprivatereply']); @@ -2271,14 +2275,14 @@ public function test_trusted_text_enabled() { $user1 = self::getDataGenerator()->create_user(); // First forum with tracking off. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $record->type = 'qanda'; $forum = self::getDataGenerator()->create_module('forum', $record); - $context = context_module::instance($forum->cmid); + $context = \context_module::instance($forum->cmid); // Add discussions to the forums. - $discussionrecord = new stdClass(); + $discussionrecord = new \stdClass(); $discussionrecord->course = $course->id; $discussionrecord->userid = $user1->id; $discussionrecord->forum = $forum->id; @@ -2292,7 +2296,7 @@ public function test_trusted_text_enabled() { $discussion2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($discussionrecord); $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id); - $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions); + $discussions = \external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions); $this->assertCount(2, $discussions['discussions']); $this->assertCount(0, $discussions['warnings']); @@ -2305,12 +2309,12 @@ public function test_trusted_text_enabled() { // Get posts now. $posts = mod_forum_external::get_discussion_posts($discussion2->id, 'modified', 'DESC'); - $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); + $posts = \external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); // Admin message is fully trusted. $this->assertEquals($dangeroustext, $posts['posts'][0]['message']); $posts = mod_forum_external::get_discussion_posts($discussion1->id, 'modified', 'ASC'); - $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); + $posts = \external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); // Student message is not trusted. $this->assertEquals($cleantext, $posts['posts'][0]['message']); } @@ -2332,14 +2336,14 @@ public function test_trusted_text_disabled() { $user1 = self::getDataGenerator()->create_user(); // First forum with tracking off. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $record->type = 'qanda'; $forum = self::getDataGenerator()->create_module('forum', $record); - $context = context_module::instance($forum->cmid); + $context = \context_module::instance($forum->cmid); // Add discussions to the forums. - $discussionrecord = new stdClass(); + $discussionrecord = new \stdClass(); $discussionrecord->course = $course->id; $discussionrecord->userid = $user1->id; $discussionrecord->forum = $forum->id; @@ -2353,7 +2357,7 @@ public function test_trusted_text_disabled() { $discussion2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($discussionrecord); $discussions = mod_forum_external::get_forum_discussions($forum->id); - $discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions); + $discussions = \external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions); $this->assertCount(2, $discussions['discussions']); $this->assertCount(0, $discussions['warnings']); @@ -2366,12 +2370,12 @@ public function test_trusted_text_disabled() { // Get posts now. $posts = mod_forum_external::get_discussion_posts($discussion2->id, 'modified', 'ASC'); - $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); + $posts = \external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); // Admin message is not trusted because enabletrusttext is disabled. $this->assertEquals($cleantext, $posts['posts'][0]['message']); $posts = mod_forum_external::get_discussion_posts($discussion1->id, 'modified', 'ASC'); - $posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); + $posts = \external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts); // Student message is not trusted. $this->assertEquals($cleantext, $posts['posts'][0]['message']); } @@ -2391,7 +2395,7 @@ public function test_delete_post_discussion() { self::getDataGenerator()->enrol_user($user->id, $course->id, $role->id); // Add a discussion. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $record->userid = $user->id; $record->forum = $forum->id; @@ -2399,7 +2403,7 @@ public function test_delete_post_discussion() { $this->setUser($user); $result = mod_forum_external::delete_post($discussion->firstpost); - $result = external_api::clean_returnvalue(mod_forum_external::delete_post_returns(), $result); + $result = \external_api::clean_returnvalue(mod_forum_external::delete_post_returns(), $result); $this->assertTrue($result['status']); $this->assertEquals(0, $DB->count_records('forum_posts', array('id' => $discussion->firstpost))); $this->assertEquals(0, $DB->count_records('forum_discussions', array('id' => $discussion->id))); @@ -2420,7 +2424,7 @@ public function test_delete_post_post() { self::getDataGenerator()->enrol_user($user->id, $course->id, $role->id); // Add a discussion. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $record->userid = $user->id; $record->forum = $forum->id; @@ -2428,7 +2432,7 @@ public function test_delete_post_post() { $parentpost = $DB->get_record('forum_posts', array('discussion' => $discussion->id)); // Add a post. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $record->userid = $user->id; $record->forum = $forum->id; @@ -2438,7 +2442,7 @@ public function test_delete_post_post() { $this->setUser($user); $result = mod_forum_external::delete_post($post->id); - $result = external_api::clean_returnvalue(mod_forum_external::delete_post_returns(), $result); + $result = \external_api::clean_returnvalue(mod_forum_external::delete_post_returns(), $result); $this->assertTrue($result['status']); $this->assertEquals(1, $DB->count_records('forum_posts', array('discussion' => $discussion->id))); $this->assertEquals(1, $DB->count_records('forum_discussions', array('id' => $discussion->id))); @@ -2469,7 +2473,7 @@ public function test_delete_post_other_user_post() { $parentpost = $DB->get_record('forum_posts', array('discussion' => $discussion->id)); // Add a post. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $record->userid = $user->id; $record->forum = $forum->id; @@ -2489,18 +2493,18 @@ public function test_mod_forum_get_discussion_posts_by_userid() { global $DB; $this->resetAfterTest(true); - $urlfactory = mod_forum\local\container::get_url_factory(); - $entityfactory = mod_forum\local\container::get_entity_factory(); - $vaultfactory = mod_forum\local\container::get_vault_factory(); + $urlfactory = \mod_forum\local\container::get_url_factory(); + $entityfactory = \mod_forum\local\container::get_entity_factory(); + $vaultfactory = \mod_forum\local\container::get_vault_factory(); $postvault = $vaultfactory->get_post_vault(); - $legacydatamapper = mod_forum\local\container::get_legacy_data_mapper_factory(); + $legacydatamapper = \mod_forum\local\container::get_legacy_data_mapper_factory(); $legacypostmapper = $legacydatamapper->get_post_data_mapper(); // Create course to add the module. $course1 = self::getDataGenerator()->create_course(); $user1 = self::getDataGenerator()->create_user(); - $user1entity = $entityfactory->get_author_from_stdclass($user1); + $user1entity = $entityfactory->get_author_from_stdClass($user1); $exporteduser1 = [ 'id' => (int) $user1->id, 'fullname' => fullname($user1), @@ -2513,7 +2517,7 @@ public function test_mod_forum_get_discussion_posts_by_userid() { ]; // Create a bunch of other users to post. $user2 = self::getDataGenerator()->create_user(); - $user2entity = $entityfactory->get_author_from_stdclass($user2); + $user2entity = $entityfactory->get_author_from_stdClass($user2); $exporteduser2 = [ 'id' => (int) $user2->id, 'fullname' => fullname($user2), @@ -2532,14 +2536,14 @@ public function test_mod_forum_get_discussion_posts_by_userid() { self::setUser($user1); // Forum with tracking off. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course1->id; $forum1 = self::getDataGenerator()->create_module('forum', $record); - $forum1context = context_module::instance($forum1->cmid); + $forum1context = \context_module::instance($forum1->cmid); // Add discussions to the forums. $time = time(); - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course1->id; $record->userid = $user1->id; $record->forum = $forum1->id; @@ -2549,7 +2553,7 @@ public function test_mod_forum_get_discussion_posts_by_userid() { $discussion1firstpost = $discussion1firstpost[$discussion1->firstpost]; $discussion1firstpostobject = $legacypostmapper->to_legacy_object($discussion1firstpost); - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course1->id; $record->userid = $user1->id; $record->forum = $forum1->id; @@ -2560,7 +2564,7 @@ public function test_mod_forum_get_discussion_posts_by_userid() { $discussion2firstpostobject = $legacypostmapper->to_legacy_object($discussion2firstpost); // Add 1 reply to the discussion 1 from a different user. - $record = new stdClass(); + $record = new \stdClass(); $record->discussion = $discussion1->id; $record->parent = $discussion1->firstpost; $record->userid = $user2->id; @@ -2579,7 +2583,7 @@ public function test_mod_forum_get_discussion_posts_by_userid() { $file1 = $fs->create_file_from_string($filerecordinline, 'image contents (not really)'); // Add 1 reply to the discussion 2 from a different user. - $record = new stdClass(); + $record = new \stdClass(); $record->discussion = $discussion2->id; $record->parent = $discussion2->firstpost; $record->userid = $user2->id; @@ -2675,16 +2679,16 @@ public function test_mod_forum_get_discussion_posts_by_userid() { 'viewisolated' => $isolatedurluser->out(false), 'viewparent' => $urlfactory->get_view_post_url_from_post_id( $discussion1reply1->discussion, $discussion1reply1->parent)->out(false), - 'edit' => (new moodle_url('/mod/forum/post.php', [ + 'edit' => (new \moodle_url('/mod/forum/post.php', [ 'edit' => $discussion1reply1->id ]))->out(false), - 'delete' => (new moodle_url('/mod/forum/post.php', [ + 'delete' => (new \moodle_url('/mod/forum/post.php', [ 'delete' => $discussion1reply1->id ]))->out(false), - 'split' => (new moodle_url('/mod/forum/post.php', [ + 'split' => (new \moodle_url('/mod/forum/post.php', [ 'prune' => $discussion1reply1->id ]))->out(false), - 'reply' => (new moodle_url('/mod/forum/post.php#mformforum', [ + 'reply' => (new \moodle_url('/mod/forum/post.php#mformforum', [ 'reply' => $discussion1reply1->id ]))->out(false), 'export' => null, @@ -2740,14 +2744,14 @@ public function test_mod_forum_get_discussion_posts_by_userid() { $discussion1firstpostobject->discussion, $discussion1firstpostobject->id)->out(false), 'viewisolated' => $isolatedurlparent->out(false), 'viewparent' => null, - 'edit' => (new moodle_url('/mod/forum/post.php', [ + 'edit' => (new \moodle_url('/mod/forum/post.php', [ 'edit' => $discussion1firstpostobject->id ]))->out(false), - 'delete' => (new moodle_url('/mod/forum/post.php', [ + 'delete' => (new \moodle_url('/mod/forum/post.php', [ 'delete' => $discussion1firstpostobject->id ]))->out(false), 'split' => null, - 'reply' => (new moodle_url('/mod/forum/post.php#mformforum', [ + 'reply' => (new \moodle_url('/mod/forum/post.php#mformforum', [ 'reply' => $discussion1firstpostobject->id ]))->out(false), 'export' => null, @@ -2818,16 +2822,16 @@ public function test_mod_forum_get_discussion_posts_by_userid() { 'viewisolated' => $isolatedurluser->out(false), 'viewparent' => $urlfactory->get_view_post_url_from_post_id( $discussion2reply1->discussion, $discussion2reply1->parent)->out(false), - 'edit' => (new moodle_url('/mod/forum/post.php', [ + 'edit' => (new \moodle_url('/mod/forum/post.php', [ 'edit' => $discussion2reply1->id ]))->out(false), - 'delete' => (new moodle_url('/mod/forum/post.php', [ + 'delete' => (new \moodle_url('/mod/forum/post.php', [ 'delete' => $discussion2reply1->id ]))->out(false), - 'split' => (new moodle_url('/mod/forum/post.php', [ + 'split' => (new \moodle_url('/mod/forum/post.php', [ 'prune' => $discussion2reply1->id ]))->out(false), - 'reply' => (new moodle_url('/mod/forum/post.php#mformforum', [ + 'reply' => (new \moodle_url('/mod/forum/post.php#mformforum', [ 'reply' => $discussion2reply1->id ]))->out(false), 'export' => null, @@ -2883,14 +2887,14 @@ public function test_mod_forum_get_discussion_posts_by_userid() { $discussion2firstpostobject->discussion, $discussion2firstpostobject->id)->out(false), 'viewisolated' => $isolatedurlparent->out(false), 'viewparent' => null, - 'edit' => (new moodle_url('/mod/forum/post.php', [ + 'edit' => (new \moodle_url('/mod/forum/post.php', [ 'edit' => $discussion2firstpostobject->id ]))->out(false), - 'delete' => (new moodle_url('/mod/forum/post.php', [ + 'delete' => (new \moodle_url('/mod/forum/post.php', [ 'delete' => $discussion2firstpostobject->id ]))->out(false), 'split' => null, - 'reply' => (new moodle_url('/mod/forum/post.php#mformforum', [ + 'reply' => (new \moodle_url('/mod/forum/post.php#mformforum', [ 'reply' => $discussion2firstpostobject->id ]))->out(false), 'export' => null, @@ -2908,7 +2912,7 @@ public function test_mod_forum_get_discussion_posts_by_userid() { // Test discussions with one additional post each (total 2 posts). // Also testing that we get the parent posts too. $discussions = mod_forum_external::get_discussion_posts_by_userid($user2->id, $forum1->cmid, 'modified', 'DESC'); - $discussions = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_by_userid_returns(), $discussions); + $discussions = \external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_by_userid_returns(), $discussions); $this->assertEquals(2, count($discussions['discussions'])); @@ -2935,7 +2939,7 @@ public function test_mod_forum_get_discussion_posts_by_userid() { groups_add_member($group2->id, $teacher->id); self::setUser($teacher); $discussions = mod_forum_external::get_discussion_posts_by_userid($user2->id, $forum1->cmid, 'modified', 'DESC'); - $discussions = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_by_userid_returns(), $discussions); + $discussions = \external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_by_userid_returns(), $discussions); // Discussion is only 1 record (group 2). $this->assertEquals(1, count($discussions['discussions'])); $this->assertEquals($expectedposts['discussions'][1], $discussions['discussions'][0]); @@ -2954,14 +2958,14 @@ public function test_get_discussion_post_discussion() { $role = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST); self::getDataGenerator()->enrol_user($user->id, $course->id, $role->id); // Add a discussion. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $record->userid = $user->id; $record->forum = $forum->id; $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); $this->setUser($user); $result = mod_forum_external::get_discussion_post($discussion->firstpost); - $result = external_api::clean_returnvalue(mod_forum_external::get_discussion_post_returns(), $result); + $result = \external_api::clean_returnvalue(mod_forum_external::get_discussion_post_returns(), $result); $this->assertEquals($discussion->firstpost, $result['post']['id']); $this->assertFalse($result['post']['hasparent']); $this->assertEquals($discussion->message, $result['post']['message']); @@ -2980,14 +2984,14 @@ public function test_get_discussion_post_post() { $role = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST); self::getDataGenerator()->enrol_user($user->id, $course->id, $role->id); // Add a discussion. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $record->userid = $user->id; $record->forum = $forum->id; $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); $parentpost = $DB->get_record('forum_posts', array('discussion' => $discussion->id)); // Add a post. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $record->userid = $user->id; $record->forum = $forum->id; @@ -2996,7 +3000,7 @@ public function test_get_discussion_post_post() { $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); $this->setUser($user); $result = mod_forum_external::get_discussion_post($post->id); - $result = external_api::clean_returnvalue(mod_forum_external::get_discussion_post_returns(), $result); + $result = \external_api::clean_returnvalue(mod_forum_external::get_discussion_post_returns(), $result); $this->assertEquals($post->id, $result['post']['id']); $this->assertTrue($result['post']['hasparent']); $this->assertEquals($post->message, $result['post']['message']); @@ -3024,7 +3028,7 @@ public function test_get_discussion_post_other_user_post() { $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); $parentpost = $DB->get_record('forum_posts', array('discussion' => $discussion->id)); // Add a post. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $record->userid = $user->id; $record->forum = $forum->id; @@ -3034,7 +3038,7 @@ public function test_get_discussion_post_other_user_post() { // Check other user post. $this->setUser($otheruser); $result = mod_forum_external::get_discussion_post($post->id); - $result = external_api::clean_returnvalue(mod_forum_external::get_discussion_post_returns(), $result); + $result = \external_api::clean_returnvalue(mod_forum_external::get_discussion_post_returns(), $result); $this->assertEquals($post->id, $result['post']['id']); $this->assertTrue($result['post']['hasparent']); $this->assertEquals($post->message, $result['post']['message']); @@ -3060,7 +3064,7 @@ public function test_prepare_draft_area_for_post() { $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); $parentpost = $DB->get_record('forum_posts', array('discussion' => $discussion->id)); // Add a post. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $record->userid = $user->id; $record->forum = $forum->id; @@ -3071,7 +3075,7 @@ public function test_prepare_draft_area_for_post() { // Add some files only in the attachment area. $filename = 'faketxt.txt'; $filerecordinline = array( - 'contextid' => context_module::instance($forum->cmid)->id, + 'contextid' => \context_module::instance($forum->cmid)->id, 'component' => 'mod_forum', 'filearea' => 'attachment', 'itemid' => $post->id, @@ -3087,7 +3091,7 @@ public function test_prepare_draft_area_for_post() { // Check attachment area. $result = mod_forum_external::prepare_draft_area_for_post($post->id, 'attachment'); - $result = external_api::clean_returnvalue(mod_forum_external::prepare_draft_area_for_post_returns(), $result); + $result = \external_api::clean_returnvalue(mod_forum_external::prepare_draft_area_for_post_returns(), $result); $this->assertCount(2, $result['files']); $this->assertEquals($filename, $result['files'][0]['filename']); $this->assertCount(5, $result['areaoptions']); @@ -3095,13 +3099,13 @@ public function test_prepare_draft_area_for_post() { // Check again using existing draft item id. $result = mod_forum_external::prepare_draft_area_for_post($post->id, 'attachment', $result['draftitemid']); - $result = external_api::clean_returnvalue(mod_forum_external::prepare_draft_area_for_post_returns(), $result); + $result = \external_api::clean_returnvalue(mod_forum_external::prepare_draft_area_for_post_returns(), $result); $this->assertCount(2, $result['files']); // Keep only certain files in the area. $filestokeep = array(array('filename' => $filename, 'filepath' => '/')); $result = mod_forum_external::prepare_draft_area_for_post($post->id, 'attachment', $result['draftitemid'], $filestokeep); - $result = external_api::clean_returnvalue(mod_forum_external::prepare_draft_area_for_post_returns(), $result); + $result = \external_api::clean_returnvalue(mod_forum_external::prepare_draft_area_for_post_returns(), $result); $this->assertCount(1, $result['files']); $this->assertEquals($filename, $result['files'][0]['filename']); @@ -3110,7 +3114,7 @@ public function test_prepare_draft_area_for_post() { $filerecordinline['filename'] = 'fakeimage.png'; $fs->create_file_from_string($filerecordinline, 'fake image.'); $result = mod_forum_external::prepare_draft_area_for_post($post->id, 'post'); - $result = external_api::clean_returnvalue(mod_forum_external::prepare_draft_area_for_post_returns(), $result); + $result = \external_api::clean_returnvalue(mod_forum_external::prepare_draft_area_for_post_returns(), $result); $this->assertCount(1, $result['files']); $this->assertEquals($filerecordinline['filename'], $result['files'][0]['filename']); $this->assertCount(5, $result['areaoptions']); @@ -3130,7 +3134,7 @@ public function test_update_discussion_post_discussion() { $this->setAdminUser(); // Add a discussion. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $record->userid = $USER->id; $record->forum = $forum->id; @@ -3146,12 +3150,12 @@ public function test_update_discussion_post_discussion() { $result = mod_forum_external::update_discussion_post($discussion->firstpost, $subject, $message, $messageformat, $options); - $result = external_api::clean_returnvalue(mod_forum_external::update_discussion_post_returns(), $result); + $result = \external_api::clean_returnvalue(mod_forum_external::update_discussion_post_returns(), $result); $this->assertTrue($result['status']); // Get the post from WS. $result = mod_forum_external::get_discussion_post($discussion->firstpost); - $result = external_api::clean_returnvalue(mod_forum_external::get_discussion_post_returns(), $result); + $result = \external_api::clean_returnvalue(mod_forum_external::get_discussion_post_returns(), $result); $this->assertEquals($subject, $result['post']['subject']); $this->assertEquals($message, $result['post']['message']); $this->assertEquals($messageformat, $result['post']['messageformat']); @@ -3181,7 +3185,7 @@ public function test_update_discussion_post_post() { $USER->autosubscribe = true; // Add a discussion. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $record->userid = $user->id; $record->forum = $forum->id; @@ -3197,7 +3201,7 @@ public function test_update_discussion_post_post() { $draftidinlineattach = file_get_unused_draft_itemid(); $draftidattach = file_get_unused_draft_itemid(); self::setUser($user); - $usercontext = context_user::instance($user->id); + $usercontext = \context_user::instance($user->id); $filepath = '/'; $filearea = 'draft'; $component = 'user'; @@ -3230,14 +3234,14 @@ public function test_update_discussion_post_post() { ]; $result = mod_forum_external::update_discussion_post($newpost->id, '', $message, $messageformat, $options); - $result = external_api::clean_returnvalue(mod_forum_external::update_discussion_post_returns(), $result); + $result = \external_api::clean_returnvalue(mod_forum_external::update_discussion_post_returns(), $result); $this->assertTrue($result['status']); // Check subscription status. $this->assertFalse(\mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussion->id, $cm)); // Get the post from WS. $result = mod_forum_external::get_discussion_posts($discussion->id, 'modified', 'DESC', true); - $result = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $result); + $result = \external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $result); $found = false; foreach ($result['posts'] as $post) { if ($post['id'] == $newpost->id) { @@ -3269,14 +3273,14 @@ public function test_update_discussion_post_other_user_post() { $this->setAdminUser(); // Add a discussion. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $record->userid = $USER->id; $record->forum = $forum->id; $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); // Add a post. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $record->userid = $USER->id; $record->forum = $forum->id; @@ -3314,7 +3318,7 @@ public function test_update_discussion_post_set_subject_to_zero(): void { ]); // Update discussion post subject. - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( mod_forum_external::update_discussion_post_returns(), mod_forum_external::update_discussion_post($discussion->firstpost, '0') ); @@ -3347,7 +3351,7 @@ public function test_update_discussion_post_set_message_to_zero(): void { ]); // Update discussion post message. - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( mod_forum_external::update_discussion_post_returns(), mod_forum_external::update_discussion_post($discussion->firstpost, '', '0', FORMAT_HTML) ); @@ -3380,7 +3384,7 @@ public function test_update_discussion_post_set_message_format_moodle(): void { ]); // Update discussion post message & messageformat. - $result = external_api::clean_returnvalue( + $result = \external_api::clean_returnvalue( mod_forum_external::update_discussion_post_returns(), mod_forum_external::update_discussion_post($discussion->firstpost, '', 'Update discussion message', FORMAT_MOODLE) ); diff --git a/mod/forum/tests/generator_test.php b/mod/forum/tests/generator_test.php index 0031d05a33128..ba1908310956e 100644 --- a/mod/forum/tests/generator_test.php +++ b/mod/forum/tests/generator_test.php @@ -14,17 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * PHPUnit data generator tests - * - * @package mod_forum - * @category phpunit - * @copyright 2012 Petr Skoda {@link http://skodak.org} - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -defined('MOODLE_INTERNAL') || die(); - +namespace mod_forum; /** * PHPUnit data generator testcase @@ -34,7 +24,7 @@ * @copyright 2012 Petr Skoda {@link http://skodak.org} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_forum_generator_testcase extends advanced_testcase { +class generator_test extends \advanced_testcase { public function setUp(): void { // We must clear the subscription caches. This has to be done both before each test, and after in case of other @@ -72,7 +62,7 @@ public function test_generator() { $this->assertEquals('forum', $cm->modname); $this->assertEquals($course->id, $cm->course); - $context = context_module::instance($cm->id); + $context = \context_module::instance($cm->id); $this->assertEquals($forum->cmid, $context->instanceid); // test gradebook integration using low level DB access - DO NOT USE IN PLUGIN CODE! @@ -99,7 +89,7 @@ public function test_create_discussion() { $course = self::getDataGenerator()->create_course(); // The forum. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $forum = self::getDataGenerator()->create_module('forum', $record); @@ -121,7 +111,7 @@ public function test_create_discussion() { $record['tags'] = array('Cats', 'mice'); $record = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); $this->assertEquals(array('Cats', 'mice'), - array_values(core_tag_tag::get_item_tags_array('mod_forum', 'forum_posts', $record->firstpost))); + array_values(\core_tag_tag::get_item_tags_array('mod_forum', 'forum_posts', $record->firstpost))); } /** @@ -142,7 +132,7 @@ public function test_create_post() { $course = self::getDataGenerator()->create_course(); // The forum. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $forum = self::getDataGenerator()->create_module('forum', $record); @@ -152,7 +142,7 @@ public function test_create_post() { $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); // Add a bunch of replies, changing the userid. - $record = new stdClass(); + $record = new \stdClass(); $record->discussion = $discussion->id; $record->userid = $user2->id; self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); @@ -169,7 +159,7 @@ public function test_create_post() { $record->tags = array('Cats', 'mice'); $record = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); $this->assertEquals(array('Cats', 'mice'), - array_values(core_tag_tag::get_item_tags_array('mod_forum', 'forum_posts', $record->id))); + array_values(\core_tag_tag::get_item_tags_array('mod_forum', 'forum_posts', $record->id))); } public function test_create_content() { @@ -212,6 +202,6 @@ public function test_create_content() { $this->assertEquals($post2->id, $postrecords[$post4->id]->parent); $this->assertEquals(array('Cats', 'mice'), - array_values(core_tag_tag::get_item_tags_array('mod_forum', 'forum_posts', $post5->id))); + array_values(\core_tag_tag::get_item_tags_array('mod_forum', 'forum_posts', $post5->id))); } } diff --git a/mod/glossary/tests/generator_test.php b/mod/glossary/tests/generator_test.php index e2a9a1ac47fd4..5b8d8d9fa5008 100644 --- a/mod/glossary/tests/generator_test.php +++ b/mod/glossary/tests/generator_test.php @@ -14,14 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * mod_glossary generator tests - * - * @package mod_glossary - * @category test - * @copyright 2013 Marina Glancy - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace mod_glossary; /** * Genarator tests class for mod_glossary. @@ -31,7 +24,7 @@ * @copyright 2013 Marina Glancy * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_glossary_generator_testcase extends advanced_testcase { +class generator_test extends \advanced_testcase { public function test_create_instance() { global $DB; @@ -72,7 +65,7 @@ public function test_create_content() { $this->assertEquals($entry2->id, $records[$entry2->id]->id); $this->assertEquals('Custom concept', $records[$entry2->id]->concept); $this->assertEquals(array('Cats', 'mice'), - array_values(core_tag_tag::get_item_tags_array('mod_glossary', 'glossary_entries', $entry2->id))); + array_values(\core_tag_tag::get_item_tags_array('mod_glossary', 'glossary_entries', $entry2->id))); $aliases = $DB->get_records_menu('glossary_alias', array('entryid' => $entry2->id), 'id ASC', 'id, alias'); $this->assertSame(array('alias1', 'alias2'), array_values($aliases)); diff --git a/mod/h5pactivity/tests/generator_test.php b/mod/h5pactivity/tests/generator_test.php index 2c3f19719259c..5e67438bc2d5e 100644 --- a/mod/h5pactivity/tests/generator_test.php +++ b/mod/h5pactivity/tests/generator_test.php @@ -14,14 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * mod_h5pactivity generator tests - * - * @package mod_h5pactivity - * @category test - * @copyright 2020 Ferran Recio - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace mod_h5pactivity; use mod_h5pactivity\local\manager; @@ -33,7 +26,7 @@ * @copyright 2020 Ferran Recio * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_h5pactivity_generator_testcase extends advanced_testcase { +class generator_test extends \advanced_testcase { /** * Test on H5P activity creation. @@ -81,7 +74,7 @@ public function test_create_instance() { 'course' => $course->id, 'packagefile' => file_get_unused_draft_itemid() ]; - $usercontext = context_user::instance($USER->id); + $usercontext = \context_user::instance($USER->id); $filerecord = ['component' => 'user', 'filearea' => 'draft', 'contextid' => $usercontext->id, 'itemid' => $params['packagefile'], 'filename' => 'singlescobasic.zip', 'filepath' => '/']; @@ -106,7 +99,7 @@ public function test_create_file_exception() { 'course' => $course->id, 'packagefilepath' => $CFG->dirroot.'/h5p/tests/fixtures/wrong_file_.xxx' ]; - $this->expectException(coding_exception::class); + $this->expectException(\coding_exception::class); $activity = $this->getDataGenerator()->create_module('h5pactivity', $params); } @@ -134,7 +127,7 @@ public function test_create_attempt(array $tracks, int $attempts, int $results, $this->assertEquals(0, $DB->count_records('h5pactivity_attempts_results')); if ($exception) { - $this->expectException(Exception::class); + $this->expectException(\Exception::class); } foreach ($tracks as $track) { @@ -329,7 +322,7 @@ public function test_create_attempt_exceptions(bool $validmod, bool $validuser) $activity = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course]); $user = $this->getDataGenerator()->create_and_enrol($course, 'student'); - $this->expectException(coding_exception::class); + $this->expectException(\coding_exception::class); $attemptinfo = [ 'attempt' => 1, diff --git a/mod/imscp/tests/externallib_test.php b/mod/imscp/tests/externallib_test.php index 14b5c68c89e48..3900a4e68720a 100644 --- a/mod/imscp/tests/externallib_test.php +++ b/mod/imscp/tests/externallib_test.php @@ -14,15 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * External mod_imscp functions unit tests - * - * @package mod_imscp - * @category external - * @copyright 2015 Juan Leyva - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @since Moodle 3.0 - */ +namespace mod_imscp; + +use externallib_advanced_testcase; +use mod_imscp_external; defined('MOODLE_INTERNAL') || die(); @@ -39,7 +34,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 3.0 */ -class mod_imscp_external_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * Test view_imscp @@ -53,14 +48,14 @@ public function test_view_imscp() { // Setup test data. $course = $this->getDataGenerator()->create_course(); $imscp = $this->getDataGenerator()->create_module('imscp', array('course' => $course->id)); - $context = context_module::instance($imscp->cmid); + $context = \context_module::instance($imscp->cmid); $cm = get_coursemodule_from_instance('imscp', $imscp->id); // Test invalid instance id. try { mod_imscp_external::view_imscp(0); $this->fail('Exception expected due to invalid mod_imscp instance id.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('invalidrecord', $e->errorcode); } @@ -70,7 +65,7 @@ public function test_view_imscp() { try { mod_imscp_external::view_imscp($imscp->id); $this->fail('Exception expected due to not enrolled user.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } @@ -82,7 +77,7 @@ public function test_view_imscp() { $sink = $this->redirectEvents(); $result = mod_imscp_external::view_imscp($imscp->id); - $result = external_api::clean_returnvalue(mod_imscp_external::view_imscp_returns(), $result); + $result = \external_api::clean_returnvalue(mod_imscp_external::view_imscp_returns(), $result); $events = $sink->get_events(); $this->assertCount(1, $events); @@ -101,12 +96,12 @@ public function test_view_imscp() { assign_capability('mod/imscp:view', CAP_PROHIBIT, $studentrole->id, $context->id); // Empty all the caches that may be affected by this change. accesslib_clear_all_caches_for_unit_testing(); - course_modinfo::clear_instance_cache(); + \course_modinfo::clear_instance_cache(); try { mod_imscp_external::view_imscp($imscp->id); $this->fail('Exception expected due to missing capability.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } @@ -142,7 +137,7 @@ public function test_get_imscps_by_courses() { $this->setUser($student1); $imscps = mod_imscp_external::get_imscps_by_courses(array()); - $imscps = external_api::clean_returnvalue(mod_imscp_external::get_imscps_by_courses_returns(), $imscps); + $imscps = \external_api::clean_returnvalue(mod_imscp_external::get_imscps_by_courses_returns(), $imscps); $this->assertCount(1, $imscps['imscps']); $this->assertEquals('First IMSCP', $imscps['imscps'][0]['name']); // As Student you cannot see some IMSCP properties like 'section'. @@ -151,7 +146,7 @@ public function test_get_imscps_by_courses() { // Student1 is not enrolled in this Course. // The webservice will give a warning! $imscps = mod_imscp_external::get_imscps_by_courses(array($course2->id)); - $imscps = external_api::clean_returnvalue(mod_imscp_external::get_imscps_by_courses_returns(), $imscps); + $imscps = \external_api::clean_returnvalue(mod_imscp_external::get_imscps_by_courses_returns(), $imscps); $this->assertCount(0, $imscps['imscps']); $this->assertEquals(1, $imscps['warnings'][0]['warningcode']); @@ -159,7 +154,7 @@ public function test_get_imscps_by_courses() { $this->setAdminUser(); // As Admin we can see this IMSCP. $imscps = mod_imscp_external::get_imscps_by_courses(array($course2->id)); - $imscps = external_api::clean_returnvalue(mod_imscp_external::get_imscps_by_courses_returns(), $imscps); + $imscps = \external_api::clean_returnvalue(mod_imscp_external::get_imscps_by_courses_returns(), $imscps); $this->assertCount(1, $imscps['imscps']); $this->assertEquals('Second IMSCP', $imscps['imscps'][0]['name']); // As an Admin you can see some IMSCP properties like 'section'. @@ -167,15 +162,15 @@ public function test_get_imscps_by_courses() { // Now, prohibit capabilities. $this->setUser($student1); - $contextcourse1 = context_course::instance($course1->id); + $contextcourse1 = \context_course::instance($course1->id); // Prohibit capability = mod:imscp:view on Course1 for students. assign_capability('mod/imscp:view', CAP_PROHIBIT, $studentrole->id, $contextcourse1->id); // Empty all the caches that may be affected by this change. accesslib_clear_all_caches_for_unit_testing(); - course_modinfo::clear_instance_cache(); + \course_modinfo::clear_instance_cache(); $imscps = mod_imscp_external::get_imscps_by_courses(array($course1->id)); - $imscps = external_api::clean_returnvalue(mod_imscp_external::get_imscps_by_courses_returns(), $imscps); + $imscps = \external_api::clean_returnvalue(mod_imscp_external::get_imscps_by_courses_returns(), $imscps); $this->assertCount(0, $imscps['imscps']); } } diff --git a/mod/imscp/tests/generator_test.php b/mod/imscp/tests/generator_test.php index 77c2138df2fc1..2d45d37646924 100644 --- a/mod/imscp/tests/generator_test.php +++ b/mod/imscp/tests/generator_test.php @@ -14,14 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * mod_imscp generator tests - * - * @package mod_imscp - * @category test - * @copyright 2013 Marina Glancy - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace mod_imscp; /** * Genarator tests class for mod_imscp. @@ -31,7 +24,7 @@ * @copyright 2013 Marina Glancy * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_imscp_generator_testcase extends advanced_testcase { +class generator_test extends \advanced_testcase { public function test_create_instance() { global $DB, $CFG, $USER; @@ -66,7 +59,7 @@ public function test_create_instance() { 'course' => $course->id, 'package' => file_get_unused_draft_itemid() ); - $usercontext = context_user::instance($USER->id); + $usercontext = \context_user::instance($USER->id); $filerecord = array('component' => 'user', 'filearea' => 'draft', 'contextid' => $usercontext->id, 'itemid' => $params['package'], 'filename' => 'singlescobasic.zip', 'filepath' => '/'); diff --git a/mod/label/tests/externallib_test.php b/mod/label/tests/externallib_test.php index 0b846db3062d8..95e5544708c07 100644 --- a/mod/label/tests/externallib_test.php +++ b/mod/label/tests/externallib_test.php @@ -14,15 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * External mod_label functions unit tests - * - * @package mod_label - * @category external - * @copyright 2017 Juan Leyva - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @since Moodle 3.3 - */ +namespace mod_label; + +use externallib_advanced_testcase; +use mod_label_external; defined('MOODLE_INTERNAL') || die(); @@ -39,7 +34,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 3.3 */ -class mod_label_external_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * Test test_mod_label_get_labels_by_courses @@ -57,12 +52,12 @@ public function test_mod_label_get_labels_by_courses() { $this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id); // First label. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course1->id; $label1 = self::getDataGenerator()->create_module('label', $record); // Second label. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course2->id; $label2 = self::getDataGenerator()->create_module('label', $record); @@ -111,21 +106,21 @@ public function test_mod_label_get_labels_by_courses() { // Call the external function passing course ids. $result = mod_label_external::get_labels_by_courses(array($course2->id, $course1->id)); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedlabels, $result['labels']); $this->assertCount(0, $result['warnings']); // Call the external function without passing course id. $result = mod_label_external::get_labels_by_courses(); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedlabels, $result['labels']); $this->assertCount(0, $result['warnings']); // Add a file to the intro. $filename = "file.txt"; $filerecordinline = array( - 'contextid' => context_module::instance($label2->cmid)->id, + 'contextid' => \context_module::instance($label2->cmid)->id, 'component' => 'mod_label', 'filearea' => 'intro', 'itemid' => 0, @@ -137,7 +132,7 @@ public function test_mod_label_get_labels_by_courses() { $fs->create_file_from_string($filerecordinline, 'image contents (not really)'); $result = mod_label_external::get_labels_by_courses(array($course2->id, $course1->id)); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertCount(1, $result['labels'][0]['introfiles']); $this->assertEquals($filename, $result['labels'][0]['introfiles'][0]['filename']); @@ -148,7 +143,7 @@ public function test_mod_label_get_labels_by_courses() { // Call the external function without passing course id. $result = mod_label_external::get_labels_by_courses(); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedlabels, $result['labels']); // Call for the second course we unenrolled the user from, expected warning. diff --git a/mod/label/tests/generator_test.php b/mod/label/tests/generator_test.php index 71582d965d0bb..fdb8a197b6a9a 100644 --- a/mod/label/tests/generator_test.php +++ b/mod/label/tests/generator_test.php @@ -14,17 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * PHPUnit label generator tests - * - * @package mod_label - * @category phpunit - * @copyright 2013 Jerome Mouneyrac - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -defined('MOODLE_INTERNAL') || die(); - +namespace mod_label; /** * PHPUnit label generator testcase @@ -34,7 +24,7 @@ * @copyright 2013 Jerome Mouneyrac * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_label_generator_testcase extends advanced_testcase { +class generator_test extends \advanced_testcase { public function test_generator() { global $DB; @@ -59,7 +49,7 @@ public function test_generator() { $this->assertEquals('label', $cm->modname); $this->assertEquals($course->id, $cm->course); - $context = context_module::instance($cm->id); + $context = \context_module::instance($cm->id); $this->assertEquals($label->cmid, $context->instanceid); } } diff --git a/mod/lesson/tests/generator_test.php b/mod/lesson/tests/generator_test.php index c024dbd415925..18d039d3a8e7b 100644 --- a/mod/lesson/tests/generator_test.php +++ b/mod/lesson/tests/generator_test.php @@ -14,14 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * mod_lesson generator tests - * - * @package mod_lesson - * @category test - * @copyright 2013 Marina Glancy - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace mod_lesson; /** * Genarator tests class for mod_lesson. @@ -31,7 +24,7 @@ * @copyright 2013 Marina Glancy * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_lesson_generator_testcase extends advanced_testcase { +class generator_test extends \advanced_testcase { public function test_create_instance() { global $DB; diff --git a/mod/lti/tests/externallib_test.php b/mod/lti/tests/externallib_test.php index 1c10391eaa705..17655a8198cbb 100644 --- a/mod/lti/tests/externallib_test.php +++ b/mod/lti/tests/externallib_test.php @@ -14,15 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * External tool module external functions tests - * - * @package mod_lti - * @category external - * @copyright 2015 Juan Leyva - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @since Moodle 3.0 - */ +namespace mod_lti; + +use mod_lti_external; +use mod_lti_testcase; defined('MOODLE_INTERNAL') || die(); @@ -41,7 +36,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 3.0 */ -class mod_lti_external_testcase extends mod_lti_testcase { +class externallib_test extends mod_lti_testcase { /** * Set up for every test @@ -64,7 +59,7 @@ protected function setup_test_data() { 'lti', ['course' => $course->id, 'toolurl' => 'http://localhost/not/real/tool.php'] ); - $context = context_module::instance($lti->cmid); + $context = \context_module::instance($lti->cmid); $cm = get_coursemodule_from_instance('lti', $lti->id); // Create users. @@ -101,7 +96,7 @@ public function test_mod_lti_get_tool_proxies() { // Fetch all proxies. $proxies = mod_lti_external::get_tool_proxies(false); - $proxies = external_api::clean_returnvalue(mod_lti_external::get_tool_proxies_returns(), $proxies); + $proxies = \external_api::clean_returnvalue(mod_lti_external::get_tool_proxies_returns(), $proxies); $this->assertCount(2, $proxies); $this->assertEqualsCanonicalizing([(array) $proxy, (array) $orphanedproxy], $proxies); @@ -119,7 +114,7 @@ public function test_mod_lti_get_orphaned_tool_proxies() { // Fetch all proxies. $proxies = mod_lti_external::get_tool_proxies(true); - $proxies = external_api::clean_returnvalue(mod_lti_external::get_tool_proxies_returns(), $proxies); + $proxies = \external_api::clean_returnvalue(mod_lti_external::get_tool_proxies_returns(), $proxies); $this->assertCount(1, $proxies); $this->assertEqualsCanonicalizing([(array) $orphanedproxy], $proxies); @@ -137,7 +132,7 @@ public function test_get_tool_launch_data() { ] = $this->setup_test_data(); $result = mod_lti_external::get_tool_launch_data($lti->id); - $result = external_api::clean_returnvalue(mod_lti_external::get_tool_launch_data_returns(), $result); + $result = \external_api::clean_returnvalue(mod_lti_external::get_tool_launch_data_returns(), $result); // Basic test, the function returns what it's expected. self::assertEquals($lti->toolurl, $result['endpoint']); @@ -176,7 +171,7 @@ public function test_mod_lti_get_ltis_by_courses() { $course2 = self::getDataGenerator()->create_course(); // Second lti. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course2->id; $lti2 = self::getDataGenerator()->create_module('lti', $record); @@ -227,14 +222,14 @@ public function test_mod_lti_get_ltis_by_courses() { // Call the external function passing course ids. $result = mod_lti_external::get_ltis_by_courses(array($course2->id, $course->id)); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedltis, $result['ltis']); $this->assertCount(0, $result['warnings']); // Call the external function without passing course id. $result = mod_lti_external::get_ltis_by_courses(); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedltis, $result['ltis']); $this->assertCount(0, $result['warnings']); @@ -244,12 +239,12 @@ public function test_mod_lti_get_ltis_by_courses() { // Call the external function without passing course id. $result = mod_lti_external::get_ltis_by_courses(); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedltis, $result['ltis']); // Call for the second course we unenrolled the user from, expected warning. $result = mod_lti_external::get_ltis_by_courses(array($course2->id)); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertCount(1, $result['warnings']); $this->assertEquals('1', $result['warnings'][0]['warningcode']); $this->assertEquals($course2->id, $result['warnings'][0]['itemid']); @@ -267,27 +262,27 @@ public function test_mod_lti_get_ltis_by_courses() { } $result = mod_lti_external::get_ltis_by_courses(); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedltis, $result['ltis']); // Admin also should get all the information. self::setAdminUser(); $result = mod_lti_external::get_ltis_by_courses(array($course->id)); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedltis, $result['ltis']); // Now, prohibit capabilities. $this->setUser($student); - $contextcourse1 = context_course::instance($course->id); + $contextcourse1 = \context_course::instance($course->id); // Prohibit capability = mod:lti:view on Course1 for students. assign_capability('mod/lti:view', CAP_PROHIBIT, $studentrole->id, $contextcourse1->id); // Empty all the caches that may be affected by this change. accesslib_clear_all_caches_for_unit_testing(); - course_modinfo::clear_instance_cache(); + \course_modinfo::clear_instance_cache(); $ltis = mod_lti_external::get_ltis_by_courses(array($course->id)); - $ltis = external_api::clean_returnvalue(mod_lti_external::get_ltis_by_courses_returns(), $ltis); + $ltis = \external_api::clean_returnvalue(mod_lti_external::get_ltis_by_courses_returns(), $ltis); $this->assertCount(0, $ltis['ltis']); } @@ -295,7 +290,7 @@ public function test_mod_lti_get_ltis_by_courses() { * Test view_lti with an invalid instance id. */ public function test_view_lti_invalid_instanceid() { - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); mod_lti_external::view_lti(0); } @@ -311,7 +306,7 @@ public function test_view_lti_no_enrolment() { $usernotenrolled = self::getDataGenerator()->create_user(); $this->setUser($usernotenrolled); - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); mod_lti_external::view_lti($lti->id); } @@ -332,9 +327,9 @@ public function test_view_lti_no_capability() { assign_capability('mod/lti:view', CAP_PROHIBIT, $studentrole->id, $context->id); // Empty all the caches that may be affected by this change. accesslib_clear_all_caches_for_unit_testing(); - course_modinfo::clear_instance_cache(); + \course_modinfo::clear_instance_cache(); - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); mod_lti_external::view_lti($lti->id); } @@ -357,7 +352,7 @@ public function test_view_lti() { $result = mod_lti_external::view_lti($lti->id); // The value of the result isn't needed but validation is. - external_api::clean_returnvalue(mod_lti_external::view_lti_returns(), $result); + \external_api::clean_returnvalue(mod_lti_external::view_lti_returns(), $result); $events = $sink->get_events(); $this->assertCount(1, $events); @@ -366,7 +361,7 @@ public function test_view_lti() { // Checking that the event contains the expected values. $this->assertInstanceOf('\mod_lti\event\course_module_viewed', $event); $this->assertEquals($context, $event->get_context()); - $moodlelti = new moodle_url('/mod/lti/view.php', array('id' => $cm->id)); + $moodlelti = new \moodle_url('/mod/lti/view.php', array('id' => $cm->id)); $this->assertEquals($moodlelti, $event->get_url()); $this->assertEventContextNotUsed($event); $this->assertNotEmpty($event->get_name()); @@ -379,7 +374,7 @@ public function test_mod_lti_create_tool_proxy() { $this->setAdminUser(); $capabilities = ['AA', 'BB']; $proxy = mod_lti_external::create_tool_proxy('Test proxy', $this->getExternalTestFileUrl('/test.html'), $capabilities, []); - $proxy = (object) external_api::clean_returnvalue(mod_lti_external::create_tool_proxy_returns(), $proxy); + $proxy = (object) \external_api::clean_returnvalue(mod_lti_external::create_tool_proxy_returns(), $proxy); $this->assertEquals('Test proxy', $proxy->name); $this->assertEquals($this->getExternalTestFileUrl('/test.html'), $proxy->regurl); @@ -394,7 +389,7 @@ public function test_mod_lti_create_tool_proxy_duplicateurl() { $this->setAdminUser(); mod_lti_external::create_tool_proxy('Test proxy 1', $this->getExternalTestFileUrl('/test.html'), array(), array()); - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); mod_lti_external::create_tool_proxy('Test proxy 2', $this->getExternalTestFileUrl('/test.html'), array(), array()); } @@ -405,7 +400,7 @@ public function test_mod_lti_create_tool_proxy_without_capability() { $course = $this->getDataGenerator()->create_course(); $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher'); $this->setUser($teacher); - $this->expectException(required_capability_exception::class); + $this->expectException(\required_capability_exception::class); mod_lti_external::create_tool_proxy('Test proxy', $this->getExternalTestFileUrl('/test.html'), array(), array()); } @@ -415,11 +410,11 @@ public function test_mod_lti_create_tool_proxy_without_capability() { public function test_mod_lti_delete_tool_proxy() { $this->setAdminUser(); $proxy = mod_lti_external::create_tool_proxy('Test proxy', $this->getExternalTestFileUrl('/test.html'), array(), array()); - $proxy = (object) external_api::clean_returnvalue(mod_lti_external::create_tool_proxy_returns(), $proxy); + $proxy = (object) \external_api::clean_returnvalue(mod_lti_external::create_tool_proxy_returns(), $proxy); $this->assertNotEmpty(lti_get_tool_proxy($proxy->id)); $proxy = mod_lti_external::delete_tool_proxy($proxy->id); - $proxy = (object) external_api::clean_returnvalue(mod_lti_external::delete_tool_proxy_returns(), $proxy); + $proxy = (object) \external_api::clean_returnvalue(mod_lti_external::delete_tool_proxy_returns(), $proxy); $this->assertEquals('Test proxy', $proxy->name); $this->assertEquals($this->getExternalTestFileUrl('/test.html'), $proxy->regurl); @@ -433,10 +428,10 @@ public function test_mod_lti_delete_tool_proxy() { public function test_mod_lti_get_tool_proxy_registration_request() { $this->setAdminUser(); $proxy = mod_lti_external::create_tool_proxy('Test proxy', $this->getExternalTestFileUrl('/test.html'), array(), array()); - $proxy = (object) external_api::clean_returnvalue(mod_lti_external::create_tool_proxy_returns(), $proxy); + $proxy = (object) \external_api::clean_returnvalue(mod_lti_external::create_tool_proxy_returns(), $proxy); $request = mod_lti_external::get_tool_proxy_registration_request($proxy->id); - $request = external_api::clean_returnvalue(mod_lti_external::get_tool_proxy_registration_request_returns(), + $request = \external_api::clean_returnvalue(mod_lti_external::get_tool_proxy_registration_request_returns(), $request); $this->assertEquals('ToolProxyRegistrationRequest', $request['lti_message_type']); @@ -449,11 +444,11 @@ public function test_mod_lti_get_tool_proxy_registration_request() { public function test_mod_lti_get_tool_types() { $this->setAdminUser(); $proxy = mod_lti_external::create_tool_proxy('Test proxy', $this->getExternalTestFileUrl('/test.html'), array(), array()); - $proxy = (object) external_api::clean_returnvalue(mod_lti_external::create_tool_proxy_returns(), $proxy); + $proxy = (object) \external_api::clean_returnvalue(mod_lti_external::create_tool_proxy_returns(), $proxy); // Create a tool type, associated with that proxy. - $type = new stdClass(); - $data = new stdClass(); + $type = new \stdClass(); + $data = new \stdClass(); $type->state = LTI_TOOL_STATE_CONFIGURED; $type->name = "Test tool"; $type->description = "Example description"; @@ -462,7 +457,7 @@ public function test_mod_lti_get_tool_types() { lti_add_type($type, $data); $types = mod_lti_external::get_tool_types($proxy->id); - $types = external_api::clean_returnvalue(mod_lti_external::get_tool_types_returns(), $types); + $types = \external_api::clean_returnvalue(mod_lti_external::get_tool_types_returns(), $types); $this->assertCount(1, $types); $type = $types[0]; @@ -476,7 +471,7 @@ public function test_mod_lti_get_tool_types() { public function test_mod_lti_create_tool_type() { $this->setAdminUser(); $type = mod_lti_external::create_tool_type($this->getExternalTestFileUrl('/ims_cartridge_basic_lti_link.xml'), '', ''); - $type = external_api::clean_returnvalue(mod_lti_external::create_tool_type_returns(), $type); + $type = \external_api::clean_returnvalue(mod_lti_external::create_tool_type_returns(), $type); $this->assertEquals('Example tool', $type['name']); $this->assertEquals('Example tool description', $type['description']); @@ -494,7 +489,7 @@ public function test_mod_lti_create_tool_type() { * Test create_tool_type failure from non existent file. */ public function test_mod_lti_create_tool_type_nonexistant_file() { - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); mod_lti_external::create_tool_type($this->getExternalTestFileUrl('/doesntexist.xml'), '', ''); } @@ -502,7 +497,7 @@ public function test_mod_lti_create_tool_type_nonexistant_file() { * Test create_tool_type failure from xml that is not a cartridge. */ public function test_mod_lti_create_tool_type_bad_file() { - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); mod_lti_external::create_tool_type($this->getExternalTestFileUrl('/rsstest.xml'), '', ''); } @@ -513,7 +508,7 @@ public function test_mod_lti_create_tool_type_without_capability() { $course = $this->getDataGenerator()->create_course(); $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher'); $this->setUser($teacher); - $this->expectException(required_capability_exception::class); + $this->expectException(\required_capability_exception::class); mod_lti_external::create_tool_type($this->getExternalTestFileUrl('/ims_cartridge_basic_lti_link.xml'), '', ''); } @@ -523,10 +518,10 @@ public function test_mod_lti_create_tool_type_without_capability() { public function test_mod_lti_update_tool_type() { $this->setAdminUser(); $type = mod_lti_external::create_tool_type($this->getExternalTestFileUrl('/ims_cartridge_basic_lti_link.xml'), '', ''); - $type = external_api::clean_returnvalue(mod_lti_external::create_tool_type_returns(), $type); + $type = \external_api::clean_returnvalue(mod_lti_external::create_tool_type_returns(), $type); $type = mod_lti_external::update_tool_type($type['id'], 'New name', 'New description', LTI_TOOL_STATE_PENDING); - $type = external_api::clean_returnvalue(mod_lti_external::update_tool_type_returns(), $type); + $type = \external_api::clean_returnvalue(mod_lti_external::update_tool_type_returns(), $type); $this->assertEquals('New name', $type['name']); $this->assertEquals('New description', $type['description']); @@ -539,11 +534,11 @@ public function test_mod_lti_update_tool_type() { public function test_mod_lti_delete_tool_type() { $this->setAdminUser(); $type = mod_lti_external::create_tool_type($this->getExternalTestFileUrl('/ims_cartridge_basic_lti_link.xml'), '', ''); - $type = external_api::clean_returnvalue(mod_lti_external::create_tool_type_returns(), $type); + $type = \external_api::clean_returnvalue(mod_lti_external::create_tool_type_returns(), $type); $this->assertNotEmpty(lti_get_type($type['id'])); $type = mod_lti_external::delete_tool_type($type['id']); - $type = external_api::clean_returnvalue(mod_lti_external::delete_tool_type_returns(), $type); + $type = \external_api::clean_returnvalue(mod_lti_external::delete_tool_type_returns(), $type); $this->assertEmpty(lti_get_type($type['id'])); } @@ -553,13 +548,13 @@ public function test_mod_lti_delete_tool_type() { public function test_mod_lti_delete_tool_type_without_capability() { $this->setAdminUser(); $type = mod_lti_external::create_tool_type($this->getExternalTestFileUrl('/ims_cartridge_basic_lti_link.xml'), '', ''); - $type = external_api::clean_returnvalue(mod_lti_external::create_tool_type_returns(), $type); + $type = \external_api::clean_returnvalue(mod_lti_external::create_tool_type_returns(), $type); $this->assertNotEmpty(lti_get_type($type['id'])); $course = $this->getDataGenerator()->create_course(); $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher'); $this->setUser($teacher); - $this->expectException(required_capability_exception::class); + $this->expectException(\required_capability_exception::class); mod_lti_external::delete_tool_type($type['id']); } @@ -569,11 +564,11 @@ public function test_mod_lti_delete_tool_type_without_capability() { public function test_mod_lti_is_cartridge() { $this->setAdminUser(); $result = mod_lti_external::is_cartridge($this->getExternalTestFileUrl('/ims_cartridge_basic_lti_link.xml')); - $result = external_api::clean_returnvalue(mod_lti_external::is_cartridge_returns(), $result); + $result = \external_api::clean_returnvalue(mod_lti_external::is_cartridge_returns(), $result); $this->assertTrue($result['iscartridge']); $result = mod_lti_external::is_cartridge($this->getExternalTestFileUrl('/test.html')); - $result = external_api::clean_returnvalue(mod_lti_external::is_cartridge_returns(), $result); + $result = \external_api::clean_returnvalue(mod_lti_external::is_cartridge_returns(), $result); $this->assertFalse($result['iscartridge']); } } diff --git a/mod/lti/tests/generator_test.php b/mod/lti/tests/generator_test.php index d43af2972845b..66418f39c6493 100644 --- a/mod/lti/tests/generator_test.php +++ b/mod/lti/tests/generator_test.php @@ -14,17 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * PHPUnit data generator tests - * - * @package mod_lti - * @category phpunit - * @copyright Copyright (c) 2012 Moodlerooms Inc. (http://www.moodlerooms.com) - * @author Mark Nielsen - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -defined('MOODLE_INTERNAL') || die(); +namespace mod_lti; /** * PHPUnit data generator testcase @@ -35,7 +25,7 @@ * @author Mark Nielsen * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_lti_generator_testcase extends advanced_testcase { +class generator_test extends \advanced_testcase { public function test_generator() { global $DB; @@ -62,7 +52,7 @@ public function test_generator() { $this->assertEquals('lti', $cm->modname); $this->assertEquals($course->id, $cm->course); - $context = context_module::instance($cm->id); + $context = \context_module::instance($cm->id); $this->assertEquals($lti->cmid, $context->instanceid); // Test gradebook integration using low level DB access - DO NOT USE IN PLUGIN CODE! diff --git a/mod/page/tests/externallib_test.php b/mod/page/tests/externallib_test.php index c88fd4616a6be..87ec9f555876f 100644 --- a/mod/page/tests/externallib_test.php +++ b/mod/page/tests/externallib_test.php @@ -14,15 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * External mod_page functions unit tests - * - * @package mod_page - * @category external - * @copyright 2015 Juan Leyva - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @since Moodle 3.0 - */ +namespace mod_page; + +use externallib_advanced_testcase; +use mod_page_external; defined('MOODLE_INTERNAL') || die(); @@ -39,7 +34,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 3.0 */ -class mod_page_external_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * Test view_page @@ -52,14 +47,14 @@ public function test_view_page() { // Setup test data. $course = $this->getDataGenerator()->create_course(); $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id)); - $context = context_module::instance($page->cmid); + $context = \context_module::instance($page->cmid); $cm = get_coursemodule_from_instance('page', $page->id); // Test invalid instance id. try { mod_page_external::view_page(0); $this->fail('Exception expected due to invalid mod_page instance id.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('invalidrecord', $e->errorcode); } @@ -69,7 +64,7 @@ public function test_view_page() { try { mod_page_external::view_page($page->id); $this->fail('Exception expected due to not enrolled user.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } @@ -81,7 +76,7 @@ public function test_view_page() { $sink = $this->redirectEvents(); $result = mod_page_external::view_page($page->id); - $result = external_api::clean_returnvalue(mod_page_external::view_page_returns(), $result); + $result = \external_api::clean_returnvalue(mod_page_external::view_page_returns(), $result); $events = $sink->get_events(); $this->assertCount(1, $events); @@ -100,12 +95,12 @@ public function test_view_page() { assign_capability('mod/page:view', CAP_PROHIBIT, $studentrole->id, $context->id); // Empty all the caches that may be affected by this change. accesslib_clear_all_caches_for_unit_testing(); - course_modinfo::clear_instance_cache(); + \course_modinfo::clear_instance_cache(); try { mod_page_external::view_page($page->id); $this->fail('Exception expected due to missing capability.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } @@ -127,12 +122,12 @@ public function test_mod_page_get_pages_by_courses() { $this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id); // First page. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course1->id; $page1 = self::getDataGenerator()->create_module('page', $record); // Second page. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course2->id; $page2 = self::getDataGenerator()->create_module('page', $record); @@ -186,21 +181,21 @@ public function test_mod_page_get_pages_by_courses() { // Call the external function passing course ids. $result = mod_page_external::get_pages_by_courses(array($course2->id, $course1->id)); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedpages, $result['pages']); $this->assertCount(0, $result['warnings']); // Call the external function without passing course id. $result = mod_page_external::get_pages_by_courses(); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedpages, $result['pages']); $this->assertCount(0, $result['warnings']); // Add a file to the intro. $filename = "file.txt"; $filerecordinline = array( - 'contextid' => context_module::instance($page2->cmid)->id, + 'contextid' => \context_module::instance($page2->cmid)->id, 'component' => 'mod_page', 'filearea' => 'intro', 'itemid' => 0, @@ -212,7 +207,7 @@ public function test_mod_page_get_pages_by_courses() { $fs->create_file_from_string($filerecordinline, 'image contents (not really)'); $result = mod_page_external::get_pages_by_courses(array($course2->id, $course1->id)); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertCount(1, $result['pages'][0]['introfiles']); $this->assertEquals($filename, $result['pages'][0]['introfiles'][0]['filename']); @@ -223,7 +218,7 @@ public function test_mod_page_get_pages_by_courses() { // Call the external function without passing course id. $result = mod_page_external::get_pages_by_courses(); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedpages, $result['pages']); // Call for the second course we unenrolled the user from, expected warning. diff --git a/mod/page/tests/generator_test.php b/mod/page/tests/generator_test.php index da1f6260b8e05..a96cab5558469 100644 --- a/mod/page/tests/generator_test.php +++ b/mod/page/tests/generator_test.php @@ -14,17 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * PHPUnit data generator tests - * - * @package mod_page - * @category phpunit - * @copyright 2012 Petr Skoda {@link http://skodak.org} - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -defined('MOODLE_INTERNAL') || die(); - +namespace mod_page; /** * PHPUnit data generator testcase @@ -34,7 +24,7 @@ * @copyright 2012 Petr Skoda {@link http://skodak.org} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_page_generator_testcase extends advanced_testcase { +class generator_test extends \advanced_testcase { public function test_generator() { global $DB, $SITE; @@ -57,7 +47,7 @@ public function test_generator() { $this->assertEquals('page', $cm->modname); $this->assertEquals($SITE->id, $cm->course); - $context = context_module::instance($cm->id); + $context = \context_module::instance($cm->id); $this->assertEquals($page->cmid, $context->instanceid); } } diff --git a/mod/quiz/tests/generator_test.php b/mod/quiz/tests/generator_test.php index 94e7ca70b9547..2de1d943872e9 100644 --- a/mod/quiz/tests/generator_test.php +++ b/mod/quiz/tests/generator_test.php @@ -14,17 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * PHPUnit data generator tests - * - * @package mod_quiz - * @category phpunit - * @copyright 2012 Matt Petro - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -defined('MOODLE_INTERNAL') || die(); - +namespace mod_quiz; /** * PHPUnit data generator testcase @@ -34,7 +24,7 @@ * @copyright 2012 Matt Petro * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_quiz_generator_testcase extends advanced_testcase { +class generator_test extends \advanced_testcase { public function test_generator() { global $DB, $SITE; @@ -58,7 +48,7 @@ public function test_generator() { $this->assertEquals('quiz', $cm->modname); $this->assertEquals($SITE->id, $cm->course); - $context = context_module::instance($cm->id); + $context = \context_module::instance($cm->id); $this->assertEquals($quiz->cmid, $context->instanceid); $this->assertEqualsWithDelta($createtime, diff --git a/mod/resource/tests/externallib_test.php b/mod/resource/tests/externallib_test.php index 99aff26cedcce..49824f971260e 100644 --- a/mod/resource/tests/externallib_test.php +++ b/mod/resource/tests/externallib_test.php @@ -14,15 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * External mod_resource functions unit tests - * - * @package mod_resource - * @category external - * @copyright 2015 Juan Leyva - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @since Moodle 3.0 - */ +namespace mod_resource; + +use externallib_advanced_testcase; +use mod_resource_external; defined('MOODLE_INTERNAL') || die(); @@ -39,7 +34,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 3.0 */ -class mod_resource_external_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * Test view_resource @@ -53,14 +48,14 @@ public function test_view_resource() { // Setup test data. $course = $this->getDataGenerator()->create_course(); $resource = $this->getDataGenerator()->create_module('resource', array('course' => $course->id)); - $context = context_module::instance($resource->cmid); + $context = \context_module::instance($resource->cmid); $cm = get_coursemodule_from_instance('resource', $resource->id); // Test invalid instance id. try { mod_resource_external::view_resource(0); $this->fail('Exception expected due to invalid mod_resource instance id.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('invalidrecord', $e->errorcode); } @@ -70,7 +65,7 @@ public function test_view_resource() { try { mod_resource_external::view_resource($resource->id); $this->fail('Exception expected due to not enrolled user.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } @@ -82,7 +77,7 @@ public function test_view_resource() { $sink = $this->redirectEvents(); $result = mod_resource_external::view_resource($resource->id); - $result = external_api::clean_returnvalue(mod_resource_external::view_resource_returns(), $result); + $result = \external_api::clean_returnvalue(mod_resource_external::view_resource_returns(), $result); $events = $sink->get_events(); $this->assertCount(1, $events); @@ -101,12 +96,12 @@ public function test_view_resource() { assign_capability('mod/resource:view', CAP_PROHIBIT, $studentrole->id, $context->id); // Empty all the caches that may be affected by this change. accesslib_clear_all_caches_for_unit_testing(); - course_modinfo::clear_instance_cache(); + \course_modinfo::clear_instance_cache(); try { mod_resource_external::view_resource($resource->id); $this->fail('Exception expected due to missing capability.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } @@ -130,12 +125,12 @@ public function test_mod_resource_get_resources_by_courses() { self::setUser($student); // First resource. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course1->id; $resource1 = self::getDataGenerator()->create_module('resource', $record); // Second resource. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course2->id; $resource2 = self::getDataGenerator()->create_module('resource', $record); @@ -187,7 +182,7 @@ public function test_mod_resource_get_resources_by_courses() { // Call the external function passing course ids. $result = mod_resource_external::get_resources_by_courses(array($course2->id, $course1->id)); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); // Remove the contentfiles (to be checked bellow). $result['resources'][0]['contentfiles'] = []; @@ -199,7 +194,7 @@ public function test_mod_resource_get_resources_by_courses() { // Call the external function without passing course id. $result = mod_resource_external::get_resources_by_courses(); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); // Remove the contentfiles (to be checked bellow). $result['resources'][0]['contentfiles'] = []; @@ -212,7 +207,7 @@ public function test_mod_resource_get_resources_by_courses() { // Add a file to the intro. $fileintroname = "fileintro.txt"; $filerecordinline = array( - 'contextid' => context_module::instance($resource2->cmid)->id, + 'contextid' => \context_module::instance($resource2->cmid)->id, 'component' => 'mod_resource', 'filearea' => 'intro', 'itemid' => 0, @@ -224,7 +219,7 @@ public function test_mod_resource_get_resources_by_courses() { $fs->create_file_from_string($filerecordinline, 'image contents (not really)'); $result = mod_resource_external::get_resources_by_courses(array($course2->id, $course1->id)); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); // Check that we receive correctly the files. $this->assertCount(1, $result['resources'][0]['introfiles']); @@ -241,7 +236,7 @@ public function test_mod_resource_get_resources_by_courses() { // Call the external function without passing course id. $result = mod_resource_external::get_resources_by_courses(); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); // Remove the contentfiles (to be checked bellow). $result['resources'][0]['contentfiles'] = []; diff --git a/mod/resource/tests/generator_test.php b/mod/resource/tests/generator_test.php index 49181b3c3e226..464b25b645859 100644 --- a/mod/resource/tests/generator_test.php +++ b/mod/resource/tests/generator_test.php @@ -14,17 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * PHPUnit data generator tests. - * - * @package mod_resource - * @category phpunit - * @copyright 2013 The Open University - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -defined('MOODLE_INTERNAL') || die(); - +namespace mod_resource; /** * PHPUnit data generator testcase. @@ -34,7 +24,7 @@ * @copyright 2013 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_resource_generator_testcase extends advanced_testcase { +class generator_test extends \advanced_testcase { public function test_generator() { global $DB, $SITE; @@ -64,7 +54,7 @@ public function test_generator() { $this->assertEquals($SITE->id, $cm->course); // Check the context is correct. - $context = context_module::instance($cm->id); + $context = \context_module::instance($cm->id); $this->assertEquals($resource->cmid, $context->instanceid); // Check that generated resource module contains a file. diff --git a/mod/scorm/tests/externallib_test.php b/mod/scorm/tests/externallib_test.php index fe8a801ff07a4..153b93bfa9f8a 100644 --- a/mod/scorm/tests/externallib_test.php +++ b/mod/scorm/tests/externallib_test.php @@ -14,15 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * SCORM module external functions tests - * - * @package mod_scorm - * @category external - * @copyright 2015 Juan Leyva - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @since Moodle 3.0 - */ +namespace mod_scorm; + +use externallib_advanced_testcase; +use mod_scorm_external; defined('MOODLE_INTERNAL') || die(); @@ -40,7 +35,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 3.0 */ -class mod_scorm_external_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * Set up for every test @@ -55,7 +50,7 @@ public function setUp(): void { $this->course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1)); $this->scorm = $this->getDataGenerator()->create_module('scorm', array('course' => $this->course->id), array('completion' => 2, 'completionview' => 1)); - $this->context = context_module::instance($this->scorm->cmid); + $this->context = \context_module::instance($this->scorm->cmid); $this->cm = get_coursemodule_from_instance('scorm', $this->scorm->id); // Create users. @@ -79,7 +74,7 @@ public function test_view_scorm() { try { mod_scorm_external::view_scorm(0); $this->fail('Exception expected due to invalid mod_scorm instance id.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('invalidrecord', $e->errorcode); } @@ -89,7 +84,7 @@ public function test_view_scorm() { try { mod_scorm_external::view_scorm($this->scorm->id); $this->fail('Exception expected due to not enrolled user.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } @@ -101,7 +96,7 @@ public function test_view_scorm() { $sink = $this->redirectEvents(); $result = mod_scorm_external::view_scorm($this->scorm->id); - $result = external_api::clean_returnvalue(mod_scorm_external::view_scorm_returns(), $result); + $result = \external_api::clean_returnvalue(mod_scorm_external::view_scorm_returns(), $result); $events = $sink->get_events(); $this->assertCount(1, $events); @@ -125,7 +120,7 @@ public function test_mod_scorm_get_scorm_attempt_count_own_empty() { // Retrieve my attempts (should be 0). $result = mod_scorm_external::get_scorm_attempt_count($this->scorm->id, $this->student->id); - $result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_attempt_count_returns(), $result); + $result = \external_api::clean_returnvalue(mod_scorm_external::get_scorm_attempt_count_returns(), $result); $this->assertEquals(0, $result['attemptscount']); } @@ -140,7 +135,7 @@ public function test_mod_scorm_get_scorm_attempt_count_own_with_complete() { scorm_insert_track($this->student->id, $this->scorm->id, $sco->id, 2, 'cmi.core.lesson_status', 'completed'); $result = mod_scorm_external::get_scorm_attempt_count($this->scorm->id, $this->student->id); - $result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_attempt_count_returns(), $result); + $result = \external_api::clean_returnvalue(mod_scorm_external::get_scorm_attempt_count_returns(), $result); $this->assertEquals(2, $result['attemptscount']); } @@ -155,7 +150,7 @@ public function test_mod_scorm_get_scorm_attempt_count_own_incomplete() { scorm_insert_track($this->student->id, $this->scorm->id, $sco->id, 2, 'cmi.core.credit', '0'); $result = mod_scorm_external::get_scorm_attempt_count($this->scorm->id, $this->student->id, true); - $result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_attempt_count_returns(), $result); + $result = \external_api::clean_returnvalue(mod_scorm_external::get_scorm_attempt_count_returns(), $result); $this->assertEquals(1, $result['attemptscount']); } @@ -170,7 +165,7 @@ public function test_mod_scorm_get_scorm_attempt_count_others_as_teacher() { // I should be able to view the attempts for my students. $result = mod_scorm_external::get_scorm_attempt_count($this->scorm->id, $this->student->id); - $result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_attempt_count_returns(), $result); + $result = \external_api::clean_returnvalue(mod_scorm_external::get_scorm_attempt_count_returns(), $result); $this->assertEquals(1, $result['attemptscount']); } @@ -183,7 +178,7 @@ public function test_mod_scorm_get_scorm_attempt_count_others_as_student() { self::setUser($student2); // I should not be able to view the attempts of another student. - $this->expectException(required_capability_exception::class); + $this->expectException(\required_capability_exception::class); mod_scorm_external::get_scorm_attempt_count($this->scorm->id, $this->student->id); } @@ -192,7 +187,7 @@ public function test_mod_scorm_get_scorm_attempt_count_invalid_instanceid() { self::setUser($this->student); // Test invalid instance id. - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); mod_scorm_external::get_scorm_attempt_count(0, $this->student->id); } @@ -200,7 +195,7 @@ public function test_mod_scorm_get_scorm_attempt_count_invalid_userid() { // As student. self::setUser($this->student); - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); mod_scorm_external::get_scorm_attempt_count($this->scorm->id, -1); } @@ -220,7 +215,7 @@ public function test_mod_scorm_get_scorm_scoes() { $course = self::getDataGenerator()->create_course(); // First scorm, dates restriction. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $record->timeopen = time() + DAYSECS; $record->timeclose = $record->timeopen + DAYSECS; @@ -239,7 +234,7 @@ public function test_mod_scorm_get_scorm_scoes() { try { mod_scorm_external::get_scorm_scoes($scorm->id); $this->fail('Exception expected due to invalid dates.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('notopenyet', $e->errorcode); } @@ -250,14 +245,14 @@ public function test_mod_scorm_get_scorm_scoes() { try { mod_scorm_external::get_scorm_scoes($scorm->id); $this->fail('Exception expected due to invalid dates.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('expired', $e->errorcode); } // Retrieve my scoes, user with permission. self::setUser($teacher); $result = mod_scorm_external::get_scorm_scoes($scorm->id); - $result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_scoes_returns(), $result); + $result = \external_api::clean_returnvalue(mod_scorm_external::get_scorm_scoes_returns(), $result); $this->assertCount(2, $result['scoes']); $this->assertCount(0, $result['warnings']); @@ -289,7 +284,7 @@ public function test_mod_scorm_get_scorm_scoes() { // Use organization. $organization = 'golf_sample_default_org'; $result = mod_scorm_external::get_scorm_scoes($scorm->id, $organization); - $result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_scoes_returns(), $result); + $result = \external_api::clean_returnvalue(mod_scorm_external::get_scorm_scoes_returns(), $result); $this->assertCount(1, $result['scoes']); $this->assertEquals($organization, $result['scoes'][0]['organization']); $this->assertCount(0, $result['warnings']); @@ -298,7 +293,7 @@ public function test_mod_scorm_get_scorm_scoes() { try { mod_scorm_external::get_scorm_scoes(0); $this->fail('Exception expected due to invalid instance id.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('invalidrecord', $e->errorcode); } @@ -313,13 +308,13 @@ public function test_mod_scorm_get_scorm_scoes_complex_package() { // As student. self::setUser($this->student); - $record = new stdClass(); + $record = new \stdClass(); $record->course = $this->course->id; $record->packagefilepath = $CFG->dirroot.'/mod/scorm/tests/packages/complexscorm.zip'; $scorm = self::getDataGenerator()->create_module('scorm', $record); $result = mod_scorm_external::get_scorm_scoes($scorm->id); - $result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_scoes_returns(), $result); + $result = \external_api::clean_returnvalue(mod_scorm_external::get_scorm_scoes_returns(), $result); $this->assertCount(9, $result['scoes']); $this->assertCount(0, $result['warnings']); @@ -363,7 +358,7 @@ public function test_mod_scorm_get_scorm_user_data() { $course = self::getDataGenerator()->create_course(); // First scorm. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $scorm = self::getDataGenerator()->create_module('scorm', $record); @@ -381,7 +376,7 @@ public function test_mod_scorm_get_scorm_user_data() { scorm_insert_track($student1->id, $scorm->id, $sco->id, 2, 'cmi.core.lesson_status', 'completed'); $result = mod_scorm_external::get_scorm_user_data($scorm->id, 1); - $result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_user_data_returns(), $result); + $result = \external_api::clean_returnvalue(mod_scorm_external::get_scorm_user_data_returns(), $result); $this->assertCount(2, $result['data']); // Find our tracking data. $found = 0; @@ -401,7 +396,7 @@ public function test_mod_scorm_get_scorm_user_data() { try { mod_scorm_external::get_scorm_user_data(0, 1); $this->fail('Exception expected due to invalid instance id.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('invalidrecord', $e->errorcode); } } @@ -421,7 +416,7 @@ public function test_mod_scorm_insert_scorm_tracks() { $course = self::getDataGenerator()->create_course(); // First scorm, dates restriction. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $record->timeopen = time() + DAYSECS; $record->timeclose = $record->timeopen + DAYSECS; @@ -453,7 +448,7 @@ public function test_mod_scorm_insert_scorm_tracks() { try { mod_scorm_external::insert_scorm_tracks($sco->id, 1, $tracks); $this->fail('Exception expected due to dates'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('notopenyet', $e->errorcode); } @@ -464,7 +459,7 @@ public function test_mod_scorm_insert_scorm_tracks() { try { mod_scorm_external::insert_scorm_tracks($sco->id, 1, $tracks); $this->fail('Exception expected due to dates'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('expired', $e->errorcode); } @@ -472,7 +467,7 @@ public function test_mod_scorm_insert_scorm_tracks() { try { mod_scorm_external::insert_scorm_tracks(0, 1, $tracks); $this->fail('Exception expected due to invalid sco id.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('cannotfindsco', $e->errorcode); } @@ -482,7 +477,7 @@ public function test_mod_scorm_insert_scorm_tracks() { // Retrieve my tracks. $result = mod_scorm_external::insert_scorm_tracks($sco->id, 1, $tracks); - $result = external_api::clean_returnvalue(mod_scorm_external::insert_scorm_tracks_returns(), $result); + $result = \external_api::clean_returnvalue(mod_scorm_external::insert_scorm_tracks_returns(), $result); $this->assertCount(0, $result['warnings']); $trackids = $DB->get_records('scorm_scoes_track', array('userid' => $student->id, 'scoid' => $sco->id, @@ -512,7 +507,7 @@ public function test_mod_scorm_get_scorm_sco_tracks() { $course = self::getDataGenerator()->create_course(); // First scorm. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $scorm = self::getDataGenerator()->create_module('scorm', $record); @@ -530,7 +525,7 @@ public function test_mod_scorm_get_scorm_sco_tracks() { scorm_insert_track($student->id, $scorm->id, $sco->id, 2, 'cmi.core.lesson_status', 'completed'); $result = mod_scorm_external::get_scorm_sco_tracks($sco->id, $student->id, 1); - $result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_sco_tracks_returns(), $result); + $result = \external_api::clean_returnvalue(mod_scorm_external::get_scorm_sco_tracks_returns(), $result); // 7 default elements + 2 custom ones. $this->assertCount(9, $result['data']['tracks']); $this->assertEquals(1, $result['data']['attempt']); @@ -549,7 +544,7 @@ public function test_mod_scorm_get_scorm_sco_tracks() { // Try invalid attempt. $result = mod_scorm_external::get_scorm_sco_tracks($sco->id, $student->id, 10); - $result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_sco_tracks_returns(), $result); + $result = \external_api::clean_returnvalue(mod_scorm_external::get_scorm_sco_tracks_returns(), $result); $this->assertCount(0, $result['data']['tracks']); $this->assertEquals(10, $result['data']['attempt']); $this->assertCount(1, $result['warnings']); @@ -559,14 +554,14 @@ public function test_mod_scorm_get_scorm_sco_tracks() { try { mod_scorm_external::get_scorm_sco_tracks($sco->id, $otherstudent->id); $this->fail('Exception expected due to invalid instance id.'); - } catch (required_capability_exception $e) { + } catch (\required_capability_exception $e) { $this->assertEquals('nopermissions', $e->errorcode); } self::setUser($teacher); // Ommit the attempt parameter, the function should calculate the last attempt. $result = mod_scorm_external::get_scorm_sco_tracks($sco->id, $student->id); - $result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_sco_tracks_returns(), $result); + $result = \external_api::clean_returnvalue(mod_scorm_external::get_scorm_sco_tracks_returns(), $result); // 7 default elements + 1 custom one. $this->assertCount(8, $result['data']['tracks']); $this->assertEquals(2, $result['data']['attempt']); @@ -575,14 +570,14 @@ public function test_mod_scorm_get_scorm_sco_tracks() { try { mod_scorm_external::get_scorm_sco_tracks(0, 1); $this->fail('Exception expected due to invalid instance id.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('cannotfindsco', $e->errorcode); } // Invalid user. try { mod_scorm_external::get_scorm_sco_tracks($sco->id, 0); $this->fail('Exception expected due to invalid instance id.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('invaliduser', $e->errorcode); } } @@ -607,7 +602,7 @@ public function test_mod_scorm_get_scorms_by_courses() { $course2 = self::getDataGenerator()->create_course(); // First scorm. - $record = new stdClass(); + $record = new \stdClass(); $record->introformat = FORMAT_HTML; $record->course = $course1->id; $record->hidetoc = 2; @@ -616,7 +611,7 @@ public function test_mod_scorm_get_scorms_by_courses() { $scorm1 = self::getDataGenerator()->create_module('scorm', $record); // Second scorm. - $record = new stdClass(); + $record = new \stdClass(); $record->introformat = FORMAT_HTML; $record->course = $course2->id; $scorm2 = self::getDataGenerator()->create_module('scorm', $record); @@ -649,7 +644,7 @@ public function test_mod_scorm_get_scorms_by_courses() { $DB->update_record('scorm', $scorm1); $result = mod_scorm_external::get_scorms_by_courses(array($course1->id)); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertCount(1, $result['warnings']); // Only 'id', 'coursemodule', 'course', 'name', 'intro', 'introformat', 'introfiles'. $this->assertCount(7, $result['scorms'][0]); @@ -660,7 +655,7 @@ public function test_mod_scorm_get_scorms_by_courses() { $DB->update_record('scorm', $scorm1); $result = mod_scorm_external::get_scorms_by_courses(array($course1->id)); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertCount(1, $result['warnings']); // Only 'id', 'coursemodule', 'course', 'name', 'intro', 'introformat', 'introfiles'. $this->assertCount(7, $result['scorms'][0]); @@ -694,15 +689,15 @@ public function test_mod_scorm_get_scorms_by_courses() { $scorm2->groupingid = 0; // SCORM size. The same package is used in both SCORMs. - $scormcontext1 = context_module::instance($scorm1->cmid); - $scormcontext2 = context_module::instance($scorm2->cmid); + $scormcontext1 = \context_module::instance($scorm1->cmid); + $scormcontext2 = \context_module::instance($scorm2->cmid); $fs = get_file_storage(); $packagefile = $fs->get_file($scormcontext1->id, 'mod_scorm', 'package', 0, '/', $scorm1->reference); $packagesize = $packagefile->get_filesize(); - $packageurl1 = moodle_url::make_webservice_pluginfile_url( + $packageurl1 = \moodle_url::make_webservice_pluginfile_url( $scormcontext1->id, 'mod_scorm', 'package', 0, '/', $scorm1->reference)->out(false); - $packageurl2 = moodle_url::make_webservice_pluginfile_url( + $packageurl2 = \moodle_url::make_webservice_pluginfile_url( $scormcontext2->id, 'mod_scorm', 'package', 0, '/', $scorm2->reference)->out(false); $scorm1->packagesize = $packagesize; @@ -739,12 +734,12 @@ public function test_mod_scorm_get_scorms_by_courses() { // Call the external function passing course ids. $result = mod_scorm_external::get_scorms_by_courses(array($course2->id, $course1->id)); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedscorms, $result['scorms']); // Call the external function without passing course id. $result = mod_scorm_external::get_scorms_by_courses(); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedscorms, $result['scorms']); // Unenrol user from second course and alter expected scorms. @@ -753,7 +748,7 @@ public function test_mod_scorm_get_scorms_by_courses() { // Call the external function without passing course id. $result = mod_scorm_external::get_scorms_by_courses(); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedscorms, $result['scorms']); // Call for the second course we unenrolled the user from, expected warning. @@ -780,7 +775,7 @@ public function test_mod_scorm_get_scorms_by_courses() { } $result = mod_scorm_external::get_scorms_by_courses(); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedscorms, $result['scorms']); // Even with the SCORM closed in time teacher should retrieve the info. @@ -792,14 +787,14 @@ public function test_mod_scorm_get_scorms_by_courses() { $expectedscorms[0]['timeclose'] = $scorm1->timeclose; $result = mod_scorm_external::get_scorms_by_courses(); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedscorms, $result['scorms']); // Admin also should get all the information. self::setAdminUser(); $result = mod_scorm_external::get_scorms_by_courses(array($course1->id)); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedscorms, $result['scorms']); } @@ -813,7 +808,7 @@ public function test_launch_sco() { try { mod_scorm_external::launch_sco(0); $this->fail('Exception expected due to invalid mod_scorm instance id.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('invalidrecord', $e->errorcode); } @@ -823,7 +818,7 @@ public function test_launch_sco() { try { mod_scorm_external::launch_sco($this->scorm->id); $this->fail('Exception expected due to not enrolled user.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } @@ -842,7 +837,7 @@ public function test_launch_sco() { } $result = mod_scorm_external::launch_sco($this->scorm->id, $sco->id); - $result = external_api::clean_returnvalue(mod_scorm_external::launch_sco_returns(), $result); + $result = \external_api::clean_returnvalue(mod_scorm_external::launch_sco_returns(), $result); $events = $sink->get_events(); $this->assertCount(3, $events); @@ -860,7 +855,7 @@ public function test_launch_sco() { $this->assertInstanceOf('\core\event\course_module_completion_updated', $event); // Check completion status. - $completion = new completion_info($this->course); + $completion = new \completion_info($this->course); $completiondata = $completion->get_data($this->cm); $this->assertEquals(COMPLETION_VIEWED, $completiondata->completionstate); @@ -868,7 +863,7 @@ public function test_launch_sco() { try { mod_scorm_external::launch_sco($this->scorm->id, -1); $this->fail('Exception expected due to invalid SCO id.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('cannotfindsco', $e->errorcode); } } @@ -884,17 +879,17 @@ public function test_mod_scorm_get_scorm_access_information() { $student = self::getDataGenerator()->create_user(); $course = self::getDataGenerator()->create_course(); // Create the scorm. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course->id; $scorm = self::getDataGenerator()->create_module('scorm', $record); - $context = context_module::instance($scorm->cmid); + $context = \context_module::instance($scorm->cmid); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual'); self::setUser($student); $result = mod_scorm_external::get_scorm_access_information($scorm->id); - $result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_access_information_returns(), $result); + $result = \external_api::clean_returnvalue(mod_scorm_external::get_scorm_access_information_returns(), $result); // Check default values for capabilities. $enabledcaps = array('canskipview', 'cansavetrack', 'canviewscores'); @@ -913,7 +908,7 @@ public function test_mod_scorm_get_scorm_access_information() { accesslib_clear_all_caches_for_unit_testing(); $result = mod_scorm_external::get_scorm_access_information($scorm->id); - $result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_access_information_returns(), $result); + $result = \external_api::clean_returnvalue(mod_scorm_external::get_scorm_access_information_returns(), $result); unset($result['warnings']); foreach ($result as $capname => $capvalue) { if (in_array($capname, $enabledcaps)) { diff --git a/mod/scorm/tests/generator_test.php b/mod/scorm/tests/generator_test.php index f71bc72b0e362..7ba6cefba43f3 100644 --- a/mod/scorm/tests/generator_test.php +++ b/mod/scorm/tests/generator_test.php @@ -14,14 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * mod_scorm generator tests - * - * @package mod_scorm - * @category test - * @copyright 2013 Marina Glancy - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace mod_scorm; /** * Genarator tests class for mod_scorm. @@ -31,7 +24,7 @@ * @copyright 2013 Marina Glancy * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_scorm_generator_testcase extends advanced_testcase { +class generator_test extends \advanced_testcase { public function test_create_instance() { global $DB, $CFG, $USER; @@ -66,7 +59,7 @@ public function test_create_instance() { 'course' => $course->id, 'packagefile' => file_get_unused_draft_itemid() ); - $usercontext = context_user::instance($USER->id); + $usercontext = \context_user::instance($USER->id); $filerecord = array('component' => 'user', 'filearea' => 'draft', 'contextid' => $usercontext->id, 'itemid' => $params['packagefile'], 'filename' => 'singlescobasic.zip', 'filepath' => '/'); diff --git a/mod/survey/tests/externallib_test.php b/mod/survey/tests/externallib_test.php index 1a086ac8ac176..d9bf2fb213d26 100644 --- a/mod/survey/tests/externallib_test.php +++ b/mod/survey/tests/externallib_test.php @@ -14,15 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Survey module external functions tests - * - * @package mod_survey - * @category external - * @copyright 2015 Juan Leyva - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @since Moodle 3.0 - */ +namespace mod_survey; + +use externallib_advanced_testcase; +use mod_survey_external; defined('MOODLE_INTERNAL') || die(); @@ -40,7 +35,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 3.0 */ -class mod_survey_external_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * Set up for every test @@ -53,7 +48,7 @@ public function setUp(): void { // Setup test data. $this->course = $this->getDataGenerator()->create_course(); $this->survey = $this->getDataGenerator()->create_module('survey', array('course' => $this->course->id)); - $this->context = context_module::instance($this->survey->cmid); + $this->context = \context_module::instance($this->survey->cmid); $this->cm = get_coursemodule_from_instance('survey', $this->survey->id); // Create users. @@ -78,7 +73,7 @@ public function test_mod_survey_get_surveys_by_courses() { $course2 = self::getDataGenerator()->create_course(); // Second survey. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course2->id; $survey2 = self::getDataGenerator()->create_module('survey', $record); // Force empty intro. @@ -135,14 +130,14 @@ public function test_mod_survey_get_surveys_by_courses() { // Call the external function passing course ids. $result = mod_survey_external::get_surveys_by_courses(array($course2->id, $this->course->id)); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedsurveys, $result['surveys']); $this->assertCount(0, $result['warnings']); // Call the external function without passing course id. $result = mod_survey_external::get_surveys_by_courses(); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedsurveys, $result['surveys']); $this->assertCount(0, $result['warnings']); @@ -152,7 +147,7 @@ public function test_mod_survey_get_surveys_by_courses() { // Call the external function without passing course id. $result = mod_survey_external::get_surveys_by_courses(); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedsurveys, $result['surveys']); // Call for the second course we unenrolled the user from, expected warning. @@ -171,25 +166,25 @@ public function test_mod_survey_get_surveys_by_courses() { } $result = mod_survey_external::get_surveys_by_courses(); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedsurveys, $result['surveys']); // Admin also should get all the information. self::setAdminUser(); $result = mod_survey_external::get_surveys_by_courses(array($this->course->id)); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedsurveys, $result['surveys']); // Now, prohibit capabilities. $this->setUser($this->student); - $contextcourse1 = context_course::instance($this->course->id); + $contextcourse1 = \context_course::instance($this->course->id); // Prohibit capability = mod/survey:participate on Course1 for students. assign_capability('mod/survey:participate', CAP_PROHIBIT, $this->studentrole->id, $contextcourse1->id); accesslib_clear_all_caches_for_unit_testing(); $surveys = mod_survey_external::get_surveys_by_courses(array($this->course->id)); - $surveys = external_api::clean_returnvalue(mod_survey_external::get_surveys_by_courses_returns(), $surveys); + $surveys = \external_api::clean_returnvalue(mod_survey_external::get_surveys_by_courses_returns(), $surveys); $this->assertFalse(isset($surveys['surveys'][0]['intro'])); } @@ -203,7 +198,7 @@ public function test_view_survey() { try { mod_survey_external::view_survey(0); $this->fail('Exception expected due to invalid mod_survey instance id.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('invalidrecord', $e->errorcode); } @@ -213,7 +208,7 @@ public function test_view_survey() { try { mod_survey_external::view_survey($this->survey->id); $this->fail('Exception expected due to not enrolled user.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } @@ -224,7 +219,7 @@ public function test_view_survey() { $sink = $this->redirectEvents(); $result = mod_survey_external::view_survey($this->survey->id); - $result = external_api::clean_returnvalue(mod_survey_external::view_survey_returns(), $result); + $result = \external_api::clean_returnvalue(mod_survey_external::view_survey_returns(), $result); $this->assertTrue($result['status']); $events = $sink->get_events(); @@ -247,7 +242,7 @@ public function test_view_survey() { try { mod_survey_external::view_survey($this->survey->id); $this->fail('Exception expected due to missing capability.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('nopermissions', $e->errorcode); } @@ -278,7 +273,7 @@ public function test_get_questions() { } $result = mod_survey_external::get_questions($this->survey->id); - $result = external_api::clean_returnvalue(mod_survey_external::get_questions_returns(), $result); + $result = \external_api::clean_returnvalue(mod_survey_external::get_questions_returns(), $result); // Check we receive the same questions. $this->assertCount(0, $result['warnings']); @@ -302,7 +297,7 @@ public function test_get_questions() { try { mod_survey_external::get_questions($this->survey->id); $this->fail('Exception expected due to missing capability.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('nopermissions', $e->errorcode); } } @@ -342,7 +337,7 @@ public function test_submit_answers() { } $result = mod_survey_external::submit_answers($this->survey->id, $realquestions); - $result = external_api::clean_returnvalue(mod_survey_external::submit_answers_returns(), $result); + $result = \external_api::clean_returnvalue(mod_survey_external::submit_answers_returns(), $result); $this->assertTrue($result['status']); $this->assertCount(0, $result['warnings']); @@ -357,7 +352,7 @@ public function test_submit_answers() { try { mod_survey_external::submit_answers($this->survey->id, $realquestions); $this->fail('Exception expected due to answers already submitted.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('alreadysubmitted', $e->errorcode); } @@ -369,7 +364,7 @@ public function test_submit_answers() { try { mod_survey_external::submit_answers($this->survey->id, $realquestions); $this->fail('Exception expected due to missing capability.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('nopermissions', $e->errorcode); } @@ -379,7 +374,7 @@ public function test_submit_answers() { try { mod_survey_external::submit_answers($this->survey->id, $realquestions); $this->fail('Exception expected due to not enrolled user.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } } diff --git a/mod/survey/tests/generator_test.php b/mod/survey/tests/generator_test.php index d98bb91488164..841c497db3ecc 100644 --- a/mod/survey/tests/generator_test.php +++ b/mod/survey/tests/generator_test.php @@ -14,14 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * mod_survey generator tests - * - * @package mod_survey - * @category test - * @copyright 2013 Marina Glancy - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace mod_survey; /** * Genarator tests class for mod_survey. @@ -31,7 +24,7 @@ * @copyright 2013 Marina Glancy * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_survey_generator_testcase extends advanced_testcase { +class generator_test extends \advanced_testcase { public function test_create_instance() { global $DB; @@ -85,11 +78,11 @@ public function test_create_instance_with_template() { $this->getDataGenerator()->create_module('survey', array('course' => $course, 'template' => 87654)); $this->fail('Exception about non-existing numeric template is expected'); - } catch (Exception $e) {} + } catch (\Exception $e) {} try { $this->getDataGenerator()->create_module('survey', array('course' => $course, 'template' => 'nonexistingcode')); $this->fail('Exception about non-existing string template is expected'); - } catch (Exception $e) {} + } catch (\Exception $e) {} } } diff --git a/mod/url/tests/externallib_test.php b/mod/url/tests/externallib_test.php index 1ddb9915ac76c..9b8efa93e4b34 100644 --- a/mod/url/tests/externallib_test.php +++ b/mod/url/tests/externallib_test.php @@ -14,15 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * External mod_url functions unit tests - * - * @package mod_url - * @category external - * @copyright 2015 Juan Leyva - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @since Moodle 3.0 - */ +namespace mod_url; + +use externallib_advanced_testcase; +use mod_url_external; defined('MOODLE_INTERNAL') || die(); @@ -39,7 +34,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 3.0 */ -class mod_url_external_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * Test view_url @@ -52,14 +47,14 @@ public function test_view_url() { // Setup test data. $course = $this->getDataGenerator()->create_course(); $url = $this->getDataGenerator()->create_module('url', array('course' => $course->id)); - $context = context_module::instance($url->cmid); + $context = \context_module::instance($url->cmid); $cm = get_coursemodule_from_instance('url', $url->id); // Test invalid instance id. try { mod_url_external::view_url(0); $this->fail('Exception expected due to invalid mod_url instance id.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('invalidrecord', $e->errorcode); } @@ -69,7 +64,7 @@ public function test_view_url() { try { mod_url_external::view_url($url->id); $this->fail('Exception expected due to not enrolled user.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } @@ -81,7 +76,7 @@ public function test_view_url() { $sink = $this->redirectEvents(); $result = mod_url_external::view_url($url->id); - $result = external_api::clean_returnvalue(mod_url_external::view_url_returns(), $result); + $result = \external_api::clean_returnvalue(mod_url_external::view_url_returns(), $result); $events = $sink->get_events(); $this->assertCount(1, $events); @@ -100,12 +95,12 @@ public function test_view_url() { assign_capability('mod/url:view', CAP_PROHIBIT, $studentrole->id, $context->id); // Empty all the caches that may be affected by this change. accesslib_clear_all_caches_for_unit_testing(); - course_modinfo::clear_instance_cache(); + \course_modinfo::clear_instance_cache(); try { mod_url_external::view_url($url->id); $this->fail('Exception expected due to missing capability.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } @@ -127,12 +122,12 @@ public function test_mod_url_get_urls_by_courses() { $this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id); // First url. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course1->id; $url1 = self::getDataGenerator()->create_module('url', $record); // Second url. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course2->id; $url2 = self::getDataGenerator()->create_module('url', $record); @@ -182,21 +177,21 @@ public function test_mod_url_get_urls_by_courses() { // Call the external function passing course ids. $result = mod_url_external::get_urls_by_courses(array($course2->id, $course1->id)); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedurls, $result['urls']); $this->assertCount(0, $result['warnings']); // Call the external function without passing course id. $result = mod_url_external::get_urls_by_courses(); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedurls, $result['urls']); $this->assertCount(0, $result['warnings']); // Add a file to the intro. $filename = "file.txt"; $filerecordinline = array( - 'contextid' => context_module::instance($url2->cmid)->id, + 'contextid' => \context_module::instance($url2->cmid)->id, 'component' => 'mod_url', 'filearea' => 'intro', 'itemid' => 0, @@ -208,7 +203,7 @@ public function test_mod_url_get_urls_by_courses() { $fs->create_file_from_string($filerecordinline, 'image contents (not really)'); $result = mod_url_external::get_urls_by_courses(array($course2->id, $course1->id)); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertCount(1, $result['urls'][0]['introfiles']); $this->assertEquals($filename, $result['urls'][0]['introfiles'][0]['filename']); @@ -219,7 +214,7 @@ public function test_mod_url_get_urls_by_courses() { // Call the external function without passing course id. $result = mod_url_external::get_urls_by_courses(); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedurls, $result['urls']); // Call for the second course we unenrolled the user from, expected warning. diff --git a/mod/url/tests/generator_test.php b/mod/url/tests/generator_test.php index 3a258d9f1b698..ecb983d246429 100644 --- a/mod/url/tests/generator_test.php +++ b/mod/url/tests/generator_test.php @@ -14,14 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * mod_url generator tests - * - * @package mod_url - * @category test - * @copyright 2013 Marina Glancy - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace mod_url; /** * Genarator tests class for mod_url. @@ -31,7 +24,7 @@ * @copyright 2013 Marina Glancy * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_url_generator_testcase extends advanced_testcase { +class generator_test extends \advanced_testcase { public function test_create_instance() { global $DB; diff --git a/mod/wiki/tests/externallib_test.php b/mod/wiki/tests/externallib_test.php index c91fd936c044f..324f8d69d794d 100644 --- a/mod/wiki/tests/externallib_test.php +++ b/mod/wiki/tests/externallib_test.php @@ -14,15 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Wiki module external functions tests. - * - * @package mod_wiki - * @category external - * @copyright 2015 Dani Palou - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @since Moodle 3.1 - */ +namespace mod_wiki; + +use externallib_advanced_testcase; +use mod_wiki_external; defined('MOODLE_INTERNAL') || die(); @@ -40,7 +35,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 3.1 */ -class mod_wiki_external_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * Set up for every test @@ -53,7 +48,7 @@ public function setUp(): void { // Setup test data. $this->course = $this->getDataGenerator()->create_course(); $this->wiki = $this->getDataGenerator()->create_module('wiki', array('course' => $this->course->id)); - $this->context = context_module::instance($this->wiki->cmid); + $this->context = \context_module::instance($this->wiki->cmid); $this->cm = get_coursemodule_from_instance('wiki', $this->wiki->id); // Create users. @@ -152,7 +147,7 @@ public function test_mod_wiki_get_wikis_by_courses() { $course2 = self::getDataGenerator()->create_course(); // Second wiki. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $course2->id; $wiki2 = self::getDataGenerator()->create_module('wiki', $record); @@ -207,14 +202,14 @@ public function test_mod_wiki_get_wikis_by_courses() { // Call the external function passing course ids. $result = mod_wiki_external::get_wikis_by_courses(array($course2->id, $this->course->id)); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedwikis, $result['wikis']); $this->assertCount(0, $result['warnings']); // Call the external function without passing course id. $result = mod_wiki_external::get_wikis_by_courses(); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedwikis, $result['wikis']); $this->assertCount(0, $result['warnings']); @@ -224,7 +219,7 @@ public function test_mod_wiki_get_wikis_by_courses() { // Call the external function without passing course id. $result = mod_wiki_external::get_wikis_by_courses(); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedwikis, $result['wikis']); // Call for the second course we unenrolled the user from, expected warning. @@ -243,43 +238,43 @@ public function test_mod_wiki_get_wikis_by_courses() { } $result = mod_wiki_external::get_wikis_by_courses(); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedwikis, $result['wikis']); // Admin also should get all the information. self::setAdminUser(); $result = mod_wiki_external::get_wikis_by_courses(array($this->course->id)); - $result = external_api::clean_returnvalue($returndescription, $result); + $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedwikis, $result['wikis']); // Now, prohibit capabilities. $this->setUser($this->student); - $contextcourse1 = context_course::instance($this->course->id); + $contextcourse1 = \context_course::instance($this->course->id); // Default student role allows to view wiki and create pages. $wikis = mod_wiki_external::get_wikis_by_courses(array($this->course->id)); - $wikis = external_api::clean_returnvalue(mod_wiki_external::get_wikis_by_courses_returns(), $wikis); + $wikis = \external_api::clean_returnvalue(mod_wiki_external::get_wikis_by_courses_returns(), $wikis); $this->assertEquals('Test wiki 1', $wikis['wikis'][0]['intro']); $this->assertEquals(1, $wikis['wikis'][0]['cancreatepages']); // Prohibit capability = mod:wiki:viewpage on Course1 for students. assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $contextcourse1->id, true); accesslib_clear_all_caches_for_unit_testing(); - course_modinfo::clear_instance_cache(null); + \course_modinfo::clear_instance_cache(null); $wikis = mod_wiki_external::get_wikis_by_courses(array($this->course->id)); - $wikis = external_api::clean_returnvalue(mod_wiki_external::get_wikis_by_courses_returns(), $wikis); + $wikis = \external_api::clean_returnvalue(mod_wiki_external::get_wikis_by_courses_returns(), $wikis); $this->assertEquals(0, count($wikis['wikis'])); // Prohibit capability = mod:wiki:createpage on Course1 for students. assign_capability('mod/wiki:viewpage', CAP_ALLOW, $this->studentrole->id, $contextcourse1->id, true); assign_capability('mod/wiki:createpage', CAP_PROHIBIT, $this->studentrole->id, $contextcourse1->id); accesslib_clear_all_caches_for_unit_testing(); - course_modinfo::clear_instance_cache(null); + \course_modinfo::clear_instance_cache(null); $wikis = mod_wiki_external::get_wikis_by_courses(array($this->course->id)); - $wikis = external_api::clean_returnvalue(mod_wiki_external::get_wikis_by_courses_returns(), $wikis); + $wikis = \external_api::clean_returnvalue(mod_wiki_external::get_wikis_by_courses_returns(), $wikis); $this->assertFalse($wikis['wikis'][0]['cancreatepages']); } @@ -293,7 +288,7 @@ public function test_view_wiki() { try { mod_wiki_external::view_wiki(0); $this->fail('Exception expected due to invalid mod_wiki instance id.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('incorrectwikiid', $e->errorcode); } @@ -303,7 +298,7 @@ public function test_view_wiki() { try { mod_wiki_external::view_wiki($this->wiki->id); $this->fail('Exception expected due to not enrolled user.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } @@ -314,7 +309,7 @@ public function test_view_wiki() { $sink = $this->redirectEvents(); $result = mod_wiki_external::view_wiki($this->wiki->id); - $result = external_api::clean_returnvalue(mod_wiki_external::view_wiki_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::view_wiki_returns(), $result); $events = $sink->get_events(); $this->assertCount(1, $events); @@ -336,7 +331,7 @@ public function test_view_wiki() { try { mod_wiki_external::view_wiki($this->wiki->id); $this->fail('Exception expected due to missing capability.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('cannotviewpage', $e->errorcode); } @@ -351,7 +346,7 @@ public function test_view_page() { try { mod_wiki_external::view_page(0); $this->fail('Exception expected due to invalid view_page page id.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('incorrectpageid', $e->errorcode); } @@ -361,7 +356,7 @@ public function test_view_page() { try { mod_wiki_external::view_page($this->firstpage->id); $this->fail('Exception expected due to not enrolled user.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } @@ -372,7 +367,7 @@ public function test_view_page() { $sink = $this->redirectEvents(); $result = mod_wiki_external::view_page($this->firstpage->id); - $result = external_api::clean_returnvalue(mod_wiki_external::view_page_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::view_page_returns(), $result); $events = $sink->get_events(); $this->assertCount(1, $events); @@ -394,7 +389,7 @@ public function test_view_page() { try { mod_wiki_external::view_page($this->firstpage->id); $this->fail('Exception expected due to missing capability.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('cannotviewpage', $e->errorcode); } @@ -409,7 +404,7 @@ public function test_get_subwikis() { try { mod_wiki_external::get_subwikis(0); $this->fail('Exception expected due to invalid get_subwikis wiki id.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('incorrectwikiid', $e->errorcode); } @@ -419,7 +414,7 @@ public function test_get_subwikis() { try { mod_wiki_external::get_subwikis($this->wiki->id); $this->fail('Exception expected due to not enrolled user.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } @@ -439,7 +434,7 @@ public function test_get_subwikis() { $expectedsubwikis[] = $expectedsubwiki; $result = mod_wiki_external::get_subwikis($this->wiki->id); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwikis_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwikis_returns(), $result); $this->assertEquals($expectedsubwikis, $result['subwikis']); $this->assertCount(0, $result['warnings']); @@ -451,7 +446,7 @@ public function test_get_subwikis() { try { mod_wiki_external::get_subwikis($this->wiki->id); $this->fail('Exception expected due to missing capability.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('nopermissions', $e->errorcode); } @@ -461,7 +456,7 @@ public function test_get_subwikis() { * Test get_subwiki_pages using an invalid wiki instance. */ public function test_get_subwiki_pages_invalid_instance() { - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); mod_wiki_external::get_subwiki_pages(0); } @@ -473,7 +468,7 @@ public function test_get_subwiki_pages_unenrolled_user() { $usernotenrolled = self::getDataGenerator()->create_user(); $this->setUser($usernotenrolled); - $this->expectException(require_login_exception::class); + $this->expectException(\require_login_exception::class); mod_wiki_external::get_subwiki_pages($this->wiki->id); } @@ -486,7 +481,7 @@ public function test_get_subwiki_pages_hidden_wiki_as_student() { array('course' => $this->course->id, 'visible' => false)); $this->setUser($this->student); - $this->expectException(require_login_exception::class); + $this->expectException(\require_login_exception::class); mod_wiki_external::get_subwiki_pages($hiddenwiki->id); } @@ -495,12 +490,12 @@ public function test_get_subwiki_pages_hidden_wiki_as_student() { */ public function test_get_subwiki_pages_without_viewpage_capability() { // Prohibit capability = mod/wiki:viewpage on the course for students. - $contextcourse = context_course::instance($this->course->id); + $contextcourse = \context_course::instance($this->course->id); assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $contextcourse->id); accesslib_clear_all_caches_for_unit_testing(); $this->setUser($this->student); - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); mod_wiki_external::get_subwiki_pages($this->wiki->id); } @@ -512,7 +507,7 @@ public function test_get_subwiki_pages_invalid_userid() { $indwiki = $this->getDataGenerator()->create_module('wiki', array('course' => $this->course->id, 'wikimode' => 'individual')); - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); mod_wiki_external::get_subwiki_pages($indwiki->id, 0, -10); } @@ -523,7 +518,7 @@ public function test_get_subwiki_pages_invalid_groupid() { // Create testing data. $this->create_collaborative_wikis_with_groups(); - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); mod_wiki_external::get_subwiki_pages($this->wikisep->id, -111); } @@ -536,7 +531,7 @@ public function test_get_subwiki_pages_individual_student_see_other_user() { array('course' => $this->course->id, 'wikimode' => 'individual')); $this->setUser($this->student); - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); mod_wiki_external::get_subwiki_pages($indwiki->id, 0, $this->teacher->id); } @@ -549,7 +544,7 @@ public function test_get_subwiki_pages_collaborative_separate_groups_student_see $this->create_collaborative_wikis_with_groups(); $this->setUser($this->student); - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); mod_wiki_external::get_subwiki_pages($this->wikisep->id, $this->group2->id); } @@ -562,7 +557,7 @@ public function test_get_subwiki_pages_individual_separate_groups_student_see_ot $this->create_individual_wikis_with_groups(); $this->setUser($this->student); - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); mod_wiki_external::get_subwiki_pages($this->wikisepind->id, $this->group2->id, $this->teacher->id); } @@ -575,7 +570,7 @@ public function test_get_subwiki_pages_collaborative_separate_groups_student_see $this->create_collaborative_wikis_with_groups(); $this->setUser($this->student); - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); mod_wiki_external::get_subwiki_pages($this->wikisep->id, 0); } @@ -588,7 +583,7 @@ public function test_get_subwiki_pages_individual_separate_groups_student_see_al $this->create_individual_wikis_with_groups(); $this->setUser($this->student); - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); mod_wiki_external::get_subwiki_pages($this->wikisepind->id, 0, $this->teacher->id); } @@ -613,17 +608,17 @@ public function test_get_subwiki_pages_collaborative() { $expectedpages[] = $expectedfirstpage; $result = mod_wiki_external::get_subwiki_pages($this->wiki->id); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); $this->assertEquals($expectedpages, $result['pages']); // Check that groupid param is ignored since the wiki isn't using groups. $result = mod_wiki_external::get_subwiki_pages($this->wiki->id, 1234); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); $this->assertEquals($expectedpages, $result['pages']); // Check that userid param is ignored since the wiki is collaborative. $result = mod_wiki_external::get_subwiki_pages($this->wiki->id, 1234, 1234); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); $this->assertEquals($expectedpages, $result['pages']); // Add a new page to the wiki and test again. We'll use a custom title so it's returned first if sorted by title. @@ -638,13 +633,13 @@ public function test_get_subwiki_pages_collaborative() { array_unshift($expectedpages, $expectednewpage); // Add page to the beginning since it orders by title by default. $result = mod_wiki_external::get_subwiki_pages($this->wiki->id); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); $this->assertEquals($expectedpages, $result['pages']); // Now we'll order by ID. Since first page was created first it'll have a lower ID. $expectedpages = array($expectedfirstpage, $expectednewpage); $result = mod_wiki_external::get_subwiki_pages($this->wiki->id, 0, 0, array('sortby' => 'id')); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); $this->assertEquals($expectedpages, $result['pages']); // Check that WS doesn't return page content if includecontent is false, it returns the size instead. @@ -658,7 +653,7 @@ public function test_get_subwiki_pages_collaborative() { unset($expectedpages[$i]['contentformat']); } $result = mod_wiki_external::get_subwiki_pages($this->wiki->id, 0, 0, array('sortby' => 'id', 'includecontent' => 0)); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); $this->assertEquals($expectedpages, $result['pages']); } @@ -673,7 +668,7 @@ public function test_get_subwiki_pages_individual() { // Perform a request before creating any page to check that an empty array is returned if subwiki doesn't exist. $result = mod_wiki_external::get_subwiki_pages($indwiki->id); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); $this->assertEquals(array(), $result['pages']); // Create first pages as student and teacher. @@ -691,7 +686,7 @@ public function test_get_subwiki_pages_individual() { $expectedpages = array($expectedteacherpage); $result = mod_wiki_external::get_subwiki_pages($indwiki->id, 0, $this->teacher->id); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); $this->assertEquals($expectedpages, $result['pages']); // Check that the teacher can see the student's pages. @@ -703,19 +698,19 @@ public function test_get_subwiki_pages_individual() { $expectedpages = array($expectedstudentpage); $result = mod_wiki_external::get_subwiki_pages($indwiki->id, 0, $this->student->id); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); $this->assertEquals($expectedpages, $result['pages']); // Now check that student can get his pages. $this->setUser($this->student); $result = mod_wiki_external::get_subwiki_pages($indwiki->id, 0, $this->student->id); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); $this->assertEquals($expectedpages, $result['pages']); // Check that not using userid uses current user. $result = mod_wiki_external::get_subwiki_pages($indwiki->id, 0); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); $this->assertEquals($expectedpages, $result['pages']); } @@ -739,18 +734,18 @@ public function test_get_subwiki_pages_separate_groups_collaborative() { $expectedpages = array($expectedpage); $result = mod_wiki_external::get_subwiki_pages($this->wikisep->id, $this->group1->id); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); $this->assertEquals($expectedpages, $result['pages']); // Let's check that not using groupid returns the same result (current group). $result = mod_wiki_external::get_subwiki_pages($this->wikisep->id); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); $this->assertEquals($expectedpages, $result['pages']); // Check that teacher can view a group pages without belonging to it. $this->setUser($this->teacher); $result = mod_wiki_external::get_subwiki_pages($this->wikisep->id, $this->group1->id); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); $this->assertEquals($expectedpages, $result['pages']); // Check that teacher can get the pages from all participants. @@ -762,7 +757,7 @@ public function test_get_subwiki_pages_separate_groups_collaborative() { $expectedpages = array($expectedpage); $result = mod_wiki_external::get_subwiki_pages($this->wikisep->id, 0); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); $this->assertEquals($expectedpages, $result['pages']); } @@ -786,7 +781,7 @@ public function test_get_subwiki_pages_visible_groups_collaborative() { $expectedpages = array($expectedpage); $result = mod_wiki_external::get_subwiki_pages($this->wikivis->id, $this->group1->id); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); $this->assertEquals($expectedpages, $result['pages']); // Check that with visible groups a student can get the pages of groups he doesn't belong to. @@ -798,7 +793,7 @@ public function test_get_subwiki_pages_visible_groups_collaborative() { $expectedpages = array($expectedpage); $result = mod_wiki_external::get_subwiki_pages($this->wikivis->id, $this->group2->id); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); $this->assertEquals($expectedpages, $result['pages']); // Check that with visible groups a student can get the pages of all participants. @@ -810,7 +805,7 @@ public function test_get_subwiki_pages_visible_groups_collaborative() { $expectedpages = array($expectedpage); $result = mod_wiki_external::get_subwiki_pages($this->wikivis->id, 0); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); $this->assertEquals($expectedpages, $result['pages']); } @@ -833,18 +828,18 @@ public function test_get_subwiki_pages_separate_groups_individual() { $expectedpages = array($expectedpage); $result = mod_wiki_external::get_subwiki_pages($this->wikisepind->id, $this->group1->id, $this->student->id); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); $this->assertEquals($expectedpages, $result['pages']); // Check that not using userid uses current user. $result = mod_wiki_external::get_subwiki_pages($this->wikisepind->id, $this->group1->id); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); $this->assertEquals($expectedpages, $result['pages']); // Check that the teacher can see the student pages. $this->setUser($this->teacher); $result = mod_wiki_external::get_subwiki_pages($this->wikisepind->id, $this->group1->id, $this->student->id); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); $this->assertEquals($expectedpages, $result['pages']); // Check that a student can see pages from another user that belongs to his groups. @@ -857,7 +852,7 @@ public function test_get_subwiki_pages_separate_groups_individual() { $expectedpages = array($expectedpage); $result = mod_wiki_external::get_subwiki_pages($this->wikisepind->id, $this->group1->id, $this->student2->id); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); $this->assertEquals($expectedpages, $result['pages']); } @@ -880,7 +875,7 @@ public function test_get_subwiki_pages_visible_groups_individual() { $expectedpages = array($expectedpage); $result = mod_wiki_external::get_subwiki_pages($this->wikivisind->id, $this->group1->id, $this->student->id); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); $this->assertEquals($expectedpages, $result['pages']); // Check that student can see teacher pages in visible groups, even if the user doesn't belong to the group. @@ -892,7 +887,7 @@ public function test_get_subwiki_pages_visible_groups_individual() { $expectedpages = array($expectedpage); $result = mod_wiki_external::get_subwiki_pages($this->wikivisind->id, $this->group2->id, $this->teacher->id); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); $this->assertEquals($expectedpages, $result['pages']); // Check that with visible groups a student can get the pages of all participants. @@ -904,7 +899,7 @@ public function test_get_subwiki_pages_visible_groups_individual() { $expectedpages = array($expectedpage); $result = mod_wiki_external::get_subwiki_pages($this->wikivisind->id, 0, $this->teacher->id); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result); $this->assertEquals($expectedpages, $result['pages']); } @@ -912,7 +907,7 @@ public function test_get_subwiki_pages_visible_groups_individual() { * Test get_page_contents using an invalid pageid. */ public function test_get_page_contents_invalid_pageid() { - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); mod_wiki_external::get_page_contents(0); } @@ -924,7 +919,7 @@ public function test_get_page_contents_unenrolled_user() { $usernotenrolled = self::getDataGenerator()->create_user(); $this->setUser($usernotenrolled); - $this->expectException(require_login_exception::class); + $this->expectException(\require_login_exception::class); mod_wiki_external::get_page_contents($this->firstpage->id); } @@ -938,7 +933,7 @@ public function test_get_page_contents_hidden_wiki_as_student() { $hiddenpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_page($hiddenwiki); $this->setUser($this->student); - $this->expectException(require_login_exception::class); + $this->expectException(\require_login_exception::class); mod_wiki_external::get_page_contents($hiddenpage->id); } @@ -947,12 +942,12 @@ public function test_get_page_contents_hidden_wiki_as_student() { */ public function test_get_page_contents_without_viewpage_capability() { // Prohibit capability = mod/wiki:viewpage on the course for students. - $contextcourse = context_course::instance($this->course->id); + $contextcourse = \context_course::instance($this->course->id); assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $contextcourse->id); accesslib_clear_all_caches_for_unit_testing(); $this->setUser($this->student); - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); mod_wiki_external::get_page_contents($this->firstpage->id); } @@ -965,7 +960,7 @@ public function test_get_page_contents_separate_groups_student_see_other_group() $this->create_individual_wikis_with_groups(); $this->setUser($this->student); - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); mod_wiki_external::get_page_contents($this->fpsepg2indt->id); } @@ -997,7 +992,7 @@ public function test_get_page_contents() { $expectedpage['tags'][1]['isstandard'] = (bool) $expectedpage['tags'][1]['isstandard']; $result = mod_wiki_external::get_page_contents($this->firstpage->id); - $result = external_api::clean_returnvalue(mod_wiki_external::get_page_contents_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_page_contents_returns(), $result); $this->assertEquals($expectedpage, $result['page']); // Add a new page to the wiki and test with it. @@ -1009,7 +1004,7 @@ public function test_get_page_contents() { $expectedpage['tags'] = array(); $result = mod_wiki_external::get_page_contents($newpage->id); - $result = external_api::clean_returnvalue(mod_wiki_external::get_page_contents_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_page_contents_returns(), $result); $this->assertEquals($expectedpage, $result['page']); } @@ -1040,13 +1035,13 @@ public function test_get_page_contents_with_groups() { ); $result = mod_wiki_external::get_page_contents($this->fpsepg1indstu->id); - $result = external_api::clean_returnvalue(mod_wiki_external::get_page_contents_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_page_contents_returns(), $result); $this->assertEquals($expectedfpsepg1indstu, $result['page']); // Check that teacher can view a group pages without belonging to it. $this->setUser($this->teacher); $result = mod_wiki_external::get_page_contents($this->fpsepg1indstu->id); - $result = external_api::clean_returnvalue(mod_wiki_external::get_page_contents_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_page_contents_returns(), $result); $this->assertEquals($expectedfpsepg1indstu, $result['page']); } @@ -1055,7 +1050,7 @@ public function test_get_page_contents_with_groups() { */ public function test_get_subwiki_files_no_files() { $result = mod_wiki_external::get_subwiki_files($this->wiki->id); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result); $this->assertCount(0, $result['files']); $this->assertCount(0, $result['warnings']); } @@ -1069,7 +1064,7 @@ public function test_get_subwiki_files_separate_groups_student_see_other_group() $this->create_collaborative_wikis_with_groups(); $this->setUser($this->student); - $this->expectException(moodle_exception::class); + $this->expectException(\moodle_exception::class); mod_wiki_external::get_subwiki_files($this->wikisep->id, $this->group2->id); } @@ -1094,13 +1089,13 @@ public function test_get_subwiki_files_collaborative_no_groups() { 'isexternalfile' => false, 'filesize' => strlen($content), 'timemodified' => $file['timemodified'], - 'fileurl' => moodle_url::make_webservice_pluginfile_url($file['contextid'], $file['component'], + 'fileurl' => \moodle_url::make_webservice_pluginfile_url($file['contextid'], $file['component'], $file['filearea'], $file['itemid'], $file['filepath'], $file['filename']), ); // Call the WS and check that it returns this file. $result = mod_wiki_external::get_subwiki_files($this->wiki->id); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result); $this->assertCount(1, $result['files']); $this->assertEquals($expectedfile, $result['files'][0]); @@ -1113,12 +1108,12 @@ public function test_get_subwiki_files_collaborative_no_groups() { $expectedfile['filename'] = $file['filename']; $expectedfile['timemodified'] = $file['timemodified']; $expectedfile['filesize'] = strlen($content); - $expectedfile['fileurl'] = moodle_url::make_webservice_pluginfile_url($file['contextid'], $file['component'], + $expectedfile['fileurl'] = \moodle_url::make_webservice_pluginfile_url($file['contextid'], $file['component'], $file['filearea'], $file['itemid'], $file['filepath'], $file['filename']); // Call the WS and check that it returns both files file. $result = mod_wiki_external::get_subwiki_files($this->wiki->id); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result); $this->assertCount(2, $result['files']); // The new file is returned first because they're returned in alphabetical order. $this->assertEquals($expectedfile, $result['files'][0]); @@ -1135,7 +1130,7 @@ public function test_get_subwiki_files_visible_groups_individual() { // Add a file as subwiki attachment in the student group 1 subwiki. $fs = get_file_storage(); - $contextwiki = context_module::instance($this->wikivisind->cmid); + $contextwiki = \context_module::instance($this->wikivisind->cmid); $file = array('component' => 'mod_wiki', 'filearea' => 'attachments', 'contextid' => $contextwiki->id, 'itemid' => $this->fpvisg1indstu->subwikiid, 'filename' => 'image.jpg', 'filepath' => '/', 'timemodified' => time()); @@ -1149,20 +1144,20 @@ public function test_get_subwiki_files_visible_groups_individual() { 'isexternalfile' => false, 'filesize' => strlen($content), 'timemodified' => $file['timemodified'], - 'fileurl' => moodle_url::make_webservice_pluginfile_url($file['contextid'], $file['component'], + 'fileurl' => \moodle_url::make_webservice_pluginfile_url($file['contextid'], $file['component'], $file['filearea'], $file['itemid'], $file['filepath'], $file['filename']), ); // Call the WS and check that it returns this file. $result = mod_wiki_external::get_subwiki_files($this->wikivisind->id, $this->group1->id); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result); $this->assertCount(1, $result['files']); $this->assertEquals($expectedfile, $result['files'][0]); // Now check that a teacher can see it too. $this->setUser($this->teacher); $result = mod_wiki_external::get_subwiki_files($this->wikivisind->id, $this->group1->id, $this->student->id); - $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result); $this->assertCount(1, $result['files']); $this->assertEquals($expectedfile, $result['files'][0]); } @@ -1193,7 +1188,7 @@ public function test_get_page_for_editing() { ); $result = mod_wiki_external::get_page_for_editing($newpage->id); - $result = external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result); $this->assertEquals($expected, $result['pagesection']); // Set expected result: Section Page content. @@ -1204,7 +1199,7 @@ public function test_get_page_for_editing() { ); $result = mod_wiki_external::get_page_for_editing($newpage->id, 'Title1'); - $result = external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result); $this->assertEquals($expected, $result['pagesection']); } @@ -1228,7 +1223,7 @@ public function test_get_page_locking() { ); $result = mod_wiki_external::get_page_for_editing($newpage->id, 'Title1', true); - $result = external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result); $this->assertEquals($expected, $result['pagesection']); // Test the section is locked. @@ -1236,7 +1231,7 @@ public function test_get_page_locking() { try { mod_wiki_external::get_page_for_editing($newpage->id, 'Title1', true); $this->fail('Exception expected due to not page locking.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('pageislocked', $e->errorcode); } @@ -1244,13 +1239,13 @@ public function test_get_page_locking() { try { mod_wiki_external::get_page_for_editing($newpage->id, null, true); $this->fail('Exception expected due to not page locking.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('pageislocked', $e->errorcode); } // Test the other section is not locked. $result = mod_wiki_external::get_page_for_editing($newpage->id, 'Title2', true); - $result = external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result); $this->assertEquals($expected, $result['pagesection']); // Back to the original user to test version change when editing. @@ -1262,7 +1257,7 @@ public function test_get_page_locking() { 'version' => '2' ); $result = mod_wiki_external::get_page_for_editing($newpage->id, 'Title1', true); - $result = external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result); $this->assertEquals($expected, $result['pagesection']); } @@ -1283,7 +1278,7 @@ public function test_new_page() { // Test on existing subwiki. $result = mod_wiki_external::new_page($pagetitle, $pagecontent, 'html', $this->fpsepg1indstu->subwikiid); - $result = external_api::clean_returnvalue(mod_wiki_external::new_page_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::new_page_returns(), $result); $this->assertIsInt($result['pageid']); $version = wiki_get_current_version($result['pageid']); @@ -1297,7 +1292,7 @@ public function test_new_page() { try { mod_wiki_external::new_page($pagetitle, $pagecontent, 'html', $this->fpsepg1indstu->subwikiid); $this->fail('Exception expected due to creation of an existing page.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('pageexists', $e->errorcode); } @@ -1305,7 +1300,7 @@ public function test_new_page() { $this->getDataGenerator()->create_group_member(array('userid' => $this->student->id, 'groupid' => $this->group2->id)); $result = mod_wiki_external::new_page($pagetitle, $pagecontent, 'html', null, $this->wikisepind->id, $this->student->id, $this->group2->id); - $result = external_api::clean_returnvalue(mod_wiki_external::new_page_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::new_page_returns(), $result); $this->assertIsInt($result['pageid']); $version = wiki_get_current_version($result['pageid']); @@ -1316,7 +1311,7 @@ public function test_new_page() { $this->assertEquals($pagetitle, $page->title); $subwiki = wiki_get_subwiki($page->subwikiid); - $expected = new StdClass(); + $expected = new \stdClass(); $expected->id = $subwiki->id; $expected->wikiid = $this->wikisepind->id; $expected->groupid = $this->group2->id; @@ -1332,7 +1327,7 @@ public function test_new_page() { mod_wiki_external::new_page($pagetitle, $pagecontent, 'html', null, $this->wikisepind->id, $this->studentnotincourse->id, $this->groupnotincourse->id); $this->fail('Exception expected due to creation of an invalid subwiki creation.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('cannoteditpage', $e->errorcode); } @@ -1358,7 +1353,7 @@ public function test_edit_page() { $newpagecontent = $sectioncontent.'

Title2

Text inside section'; $result = mod_wiki_external::edit_page($newpage->id, $newpagecontent); - $result = external_api::clean_returnvalue(mod_wiki_external::edit_page_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::edit_page_returns(), $result); $this->assertIsInt($result['pageid']); $version = wiki_get_current_version($result['pageid']); @@ -1369,7 +1364,7 @@ public function test_edit_page() { $section = 'Title2'; $result = mod_wiki_external::edit_page($newpage->id, $newsectioncontent, $section); - $result = external_api::clean_returnvalue(mod_wiki_external::edit_page_returns(), $result); + $result = \external_api::clean_returnvalue(mod_wiki_external::edit_page_returns(), $result); $this->assertIsInt($result['pageid']); $expected = $sectioncontent . $newsectioncontent; @@ -1386,7 +1381,7 @@ public function test_edit_page() { wiki_set_lock($newpage->id, 1, $section, true); mod_wiki_external::edit_page($newpage->id, $newsectioncontent, $section); $this->fail('Exception expected due to locked section'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('pageislocked', $e->errorcode); } @@ -1397,7 +1392,7 @@ public function test_edit_page() { try { mod_wiki_external::edit_page($newpage->id, $newsectioncontent, $section); $this->fail('Exception expected due to non existing section in the page.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('invalidsection', $e->errorcode); } diff --git a/mod/wiki/tests/generator_test.php b/mod/wiki/tests/generator_test.php index 5fd05216009c1..601b2d1bdcb18 100644 --- a/mod/wiki/tests/generator_test.php +++ b/mod/wiki/tests/generator_test.php @@ -14,14 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * mod_wiki generator tests - * - * @package mod_wiki - * @category test - * @copyright 2013 Marina Glancy - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace mod_wiki; /** * Genarator tests class for mod_wiki. @@ -31,7 +24,7 @@ * @copyright 2013 Marina Glancy * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_wiki_generator_testcase extends advanced_testcase { +class generator_test extends \advanced_testcase { public function test_create_instance() { global $DB; @@ -77,9 +70,9 @@ public function test_create_content() { $this->assertEquals($page3->id, $records[$page3->id]->id); $this->assertEquals('Custom title', $records[$page3->id]->title); $this->assertEquals(array('Cats', 'mice'), - array_values(core_tag_tag::get_item_tags_array('mod_wiki', 'wiki_pages', $page3->id))); + array_values(\core_tag_tag::get_item_tags_array('mod_wiki', 'wiki_pages', $page3->id))); $this->assertEquals(array('Cats', 'dogs'), - array_values(core_tag_tag::get_item_tags_array('mod_wiki', 'wiki_pages', $page4->id))); + array_values(\core_tag_tag::get_item_tags_array('mod_wiki', 'wiki_pages', $page4->id))); } public function test_create_content_individual() { diff --git a/mod/workshop/tests/generator_test.php b/mod/workshop/tests/generator_test.php index d0961fb819f39..efa2b7ca8b5c0 100644 --- a/mod/workshop/tests/generator_test.php +++ b/mod/workshop/tests/generator_test.php @@ -14,14 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * mod_workshop generator tests - * - * @package mod_workshop - * @category test - * @copyright 2013 Marina Glancy - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace mod_workshop; /** * Genarator tests class for mod_workshop. @@ -31,7 +24,7 @@ * @copyright 2013 Marina Glancy * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_workshop_generator_testcase extends advanced_testcase { +class generator_test extends \advanced_testcase { public function test_create_instance() { global $DB; diff --git a/notes/tests/externallib_test.php b/notes/tests/externallib_test.php index 8adf7e07beca7..61ca6245a73f2 100644 --- a/notes/tests/externallib_test.php +++ b/notes/tests/externallib_test.php @@ -23,6 +23,11 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +namespace core_notes; + +use core_notes_external; +use externallib_advanced_testcase; + defined('MOODLE_INTERNAL') || die(); global $CFG; @@ -30,7 +35,7 @@ require_once($CFG->dirroot . '/webservice/tests/helpers.php'); require_once($CFG->dirroot . '/notes/externallib.php'); -class core_notes_externallib_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * Test create_notes @@ -44,7 +49,7 @@ public function test_create_notes() { $course = self::getDataGenerator()->create_course(); // Set the required capabilities by the external function. - $contextid = context_course::instance($course->id)->id; + $contextid = \context_course::instance($course->id)->id; $roleid = $this->assignUserCapability('moodle/notes:manage', $contextid); $this->assignUserCapability('moodle/course:view', $contextid, $roleid); @@ -59,7 +64,7 @@ public function test_create_notes() { $creatednotes = core_notes_external::create_notes($notes); // We need to execute the return values cleaning process to simulate the web service server. - $creatednotes = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes); + $creatednotes = \external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes); $thenote = $DB->get_record('post', array('id' => $creatednotes[0]['noteid'])); @@ -72,7 +77,7 @@ public function test_create_notes() { // Call without required capability. $this->unassignUserCapability('moodle/notes:manage', $contextid, $roleid); - $this->expectException('required_capability_exception'); + $this->expectException('\required_capability_exception'); $creatednotes = core_notes_external::create_notes($notes); } @@ -85,7 +90,7 @@ public function test_delete_notes() { $course = self::getDataGenerator()->create_course(); // Set the required capabilities by the external function. - $contextid = context_course::instance($course->id)->id; + $contextid = \context_course::instance($course->id)->id; $roleid = $this->assignUserCapability('moodle/notes:manage', $contextid); $this->assignUserCapability('moodle/course:view', $contextid, $roleid); @@ -98,11 +103,11 @@ public function test_delete_notes() { $cnote['clientnoteid'] = 4; $cnotes = array($cnote); $creatednotes = core_notes_external::create_notes($cnotes); - $creatednotes = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes); + $creatednotes = \external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes); $dnotes1 = array($creatednotes[0]['noteid']); $deletednotes1 = core_notes_external::delete_notes($dnotes1); - $deletednotes1 = external_api::clean_returnvalue(core_notes_external::delete_notes_returns(), $deletednotes1); + $deletednotes1 = \external_api::clean_returnvalue(core_notes_external::delete_notes_returns(), $deletednotes1); // Confirm that base note data was deleted correctly. $notdeletedcount = $DB->count_records_select('post', 'id = ' . $creatednotes[0]['noteid']); @@ -110,7 +115,7 @@ public function test_delete_notes() { $dnotes2 = array(33); // This note does not exist. $deletednotes2 = core_notes_external::delete_notes($dnotes2); - $deletednotes2 = external_api::clean_returnvalue(core_notes_external::delete_notes_returns(), $deletednotes2); + $deletednotes2 = \external_api::clean_returnvalue(core_notes_external::delete_notes_returns(), $deletednotes2); $this->assertEquals("note", $deletednotes2[0]["item"]); $this->assertEquals(33, $deletednotes2[0]["itemid"]); @@ -119,13 +124,13 @@ public function test_delete_notes() { // Call without required capability. $creatednotes = core_notes_external::create_notes($cnotes); - $creatednotes = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes); + $creatednotes = \external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes); $dnotes3 = array($creatednotes[0]['noteid']); $this->unassignUserCapability('moodle/notes:manage', $contextid, $roleid); - $this->expectException('required_capability_exception'); + $this->expectException('\required_capability_exception'); $deletednotes = core_notes_external::delete_notes($dnotes3); - $deletednotes = external_api::clean_returnvalue(core_notes_external::delete_notes_returns(), $deletednotes); + $deletednotes = \external_api::clean_returnvalue(core_notes_external::delete_notes_returns(), $deletednotes); } public function test_get_notes() { @@ -137,7 +142,7 @@ public function test_get_notes() { $course = self::getDataGenerator()->create_course(); // Set the required capabilities by the external function. - $contextid = context_course::instance($course->id)->id; + $contextid = \context_course::instance($course->id)->id; $roleid = $this->assignUserCapability('moodle/notes:manage', $contextid); $this->assignUserCapability('moodle/notes:view', $contextid, $roleid); $this->assignUserCapability('moodle/course:view', $contextid, $roleid); @@ -154,14 +159,14 @@ public function test_get_notes() { $creatednotes2 = core_notes_external::create_notes($cnotes); $creatednotes3 = core_notes_external::create_notes($cnotes); - $creatednotes1 = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes1); - $creatednotes2 = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes2); - $creatednotes3 = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes3); + $creatednotes1 = \external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes1); + $creatednotes2 = \external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes2); + $creatednotes3 = \external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes3); // Note 33 does not exist. $gnotes = array($creatednotes1[0]['noteid'], $creatednotes2[0]['noteid'], $creatednotes3[0]['noteid'], 33); $getnotes = core_notes_external::get_notes($gnotes); - $getnotes = external_api::clean_returnvalue(core_notes_external::get_notes_returns(), $getnotes); + $getnotes = \external_api::clean_returnvalue(core_notes_external::get_notes_returns(), $getnotes); $this->unassignUserCapability('moodle/notes:manage', $contextid, $roleid); // Confirm that base note data was retrieved correctly. @@ -178,7 +183,7 @@ public function test_get_notes() { // Call without required capability. $this->unassignUserCapability('moodle/notes:view', $contextid, $roleid); - $this->expectException('required_capability_exception'); + $this->expectException('\required_capability_exception'); $creatednotes = core_notes_external::get_notes($gnotes); } @@ -191,7 +196,7 @@ public function test_update_notes() { $course = self::getDataGenerator()->create_course(); // Set the required capabilities by the external function. - $contextid = context_course::instance($course->id)->id; + $contextid = \context_course::instance($course->id)->id; $roleid = $this->assignUserCapability('moodle/notes:manage', $contextid); $this->assignUserCapability('moodle/course:view', $contextid, $roleid); @@ -212,7 +217,7 @@ public function test_update_notes() { $notes1 = array($note1, $note2, $note3); $creatednotes = core_notes_external::create_notes($notes1); - $creatednotes = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes); + $creatednotes = \external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes); $note2 = array(); $note2["id"] = $creatednotes[0]['noteid']; @@ -223,7 +228,7 @@ public function test_update_notes() { $updatednotes = core_notes_external::update_notes($notes2); - $updatednotes = external_api::clean_returnvalue(core_notes_external::update_notes_returns(), $updatednotes); + $updatednotes = \external_api::clean_returnvalue(core_notes_external::update_notes_returns(), $updatednotes); $thenote = $DB->get_record('post', array('id' => $creatednotes[0]['noteid'])); // Confirm that base note data was updated correctly. @@ -232,9 +237,9 @@ public function test_update_notes() { // Call without required capability. $creatednotes = core_notes_external::create_notes($notes1); - $creatednotes = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes); + $creatednotes = \external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes); $this->unassignUserCapability('moodle/notes:manage', $contextid, $roleid); - $this->expectException('required_capability_exception'); + $this->expectException('\required_capability_exception'); $note2 = array(); $note2["id"] = $creatednotes[0]['noteid']; $note2['publishstate'] = 'personal'; @@ -242,7 +247,7 @@ public function test_update_notes() { $note2['format'] = FORMAT_HTML; $notes2 = array($note2); $updatednotes = core_notes_external::update_notes($notes2); - $updatednotes = external_api::clean_returnvalue(core_notes_external::update_notes_returns(), $updatednotes); + $updatednotes = \external_api::clean_returnvalue(core_notes_external::update_notes_returns(), $updatednotes); } /** @@ -305,7 +310,7 @@ public function test_get_course_notes() { // Retrieve notes, normal case. $result = core_notes_external::get_course_notes($course1->id, $student1->id); - $result = external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result); + $result = \external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result); $this->assertEquals($notes1->id, $result['sitenotes'][0]['id']); $this->assertCount(2, $result['coursenotes']); // Teacher can manage only the course notes. @@ -324,12 +329,12 @@ public function test_get_course_notes() { try { $result = core_notes_external::get_course_notes($course2->id, $student1->id); $this->fail('the user is not enrolled in the course'); - } catch (require_login_exception $e) { + } catch (\require_login_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } $result = core_notes_external::get_course_notes(0, $student1->id); - $result = external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result); + $result = \external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result); $this->assertEmpty($result['sitenotes']); // Teacher can't manage system notes. $this->assertFalse($result['canmanagesystemnotes']); @@ -345,7 +350,7 @@ public function test_get_course_notes() { $this->setAdminUser(); $result = core_notes_external::get_course_notes(0, $student1->id); - $result = external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result); + $result = \external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result); $this->assertEquals($notes1->id, $result['sitenotes'][0]['id']); $this->assertCount(1, $result['sitenotes']); // Admin user can manage both system and course notes. @@ -354,7 +359,7 @@ public function test_get_course_notes() { $this->setUser($teacher1); $result = core_notes_external::get_course_notes(0, 0); - $result = external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result); + $result = \external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result); $this->assertEmpty($result['sitenotes']); $this->assertEmpty($result['coursenotes']); $this->assertEmpty($result['personalnotes']); @@ -364,7 +369,7 @@ public function test_get_course_notes() { $this->setUser($teacher2); $result = core_notes_external::get_course_notes($course1->id, $student1->id); - $result = external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result); + $result = \external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result); $this->assertEquals($notes1->id, $result['sitenotes'][0]['id']); foreach ($result['coursenotes'] as $coursenote) { @@ -380,7 +385,7 @@ public function test_get_course_notes() { $this->assertTrue($result['canmanagecoursenotes']); $result = core_notes_external::get_course_notes($course1->id, 0); - $result = external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result); + $result = \external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result); $this->assertEquals($notes1->id, $result['sitenotes'][0]['id']); foreach ($result['coursenotes'] as $coursenote) { @@ -394,7 +399,7 @@ public function test_get_course_notes() { $this->setUser($teacher1); $result = core_notes_external::get_course_notes($course1->id, 0); - $result = external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result); + $result = \external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result); $this->assertEquals($notep1->id, $result['personalnotes'][0]['id']); $this->assertCount(1, $result['personalnotes']); // Teacher can manage only the course notes. @@ -420,7 +425,7 @@ public function test_view_notes() { $student = $this->getDataGenerator()->create_user(); $teacher = $this->getDataGenerator()->create_user(); $course = $this->getDataGenerator()->create_course(); - $coursecontext = context_course::instance($course->id); + $coursecontext = \context_course::instance($course->id); // Enroll students and teachers to course. $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id); @@ -438,10 +443,10 @@ public function test_view_notes() { $sink = $this->redirectEvents(); $result = core_notes_external::view_notes($course->id, $student->id); - $result = external_api::clean_returnvalue(core_notes_external::view_notes_returns(), $result); + $result = \external_api::clean_returnvalue(core_notes_external::view_notes_returns(), $result); $result = core_notes_external::view_notes($course->id); - $result = external_api::clean_returnvalue(core_notes_external::view_notes_returns(), $result); + $result = \external_api::clean_returnvalue(core_notes_external::view_notes_returns(), $result); $events = $sink->get_events(); @@ -458,14 +463,14 @@ public function test_view_notes() { try { core_notes_external::view_notes(0); $this->fail('Exception expected due to invalid permissions at system level.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('nopermissions', $e->errorcode); } try { core_notes_external::view_notes($course->id, $student->id + 100); $this->fail('Exception expected due to invalid user id.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('invaliduser', $e->errorcode); } } diff --git a/notes/tests/generator_test.php b/notes/tests/generator_test.php index 6db5be66cea77..b8eafd0f6b510 100644 --- a/notes/tests/generator_test.php +++ b/notes/tests/generator_test.php @@ -22,7 +22,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -defined('MOODLE_INTERNAL') || die(); +namespace core_notes; /** * Generator tests class. @@ -31,7 +31,7 @@ * @copyright 2013 Ankit Agarwal * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class core_notes_generator_testcase extends advanced_testcase { +class generator_test extends \advanced_testcase { /** Test create_instance method */ public function test_create_instance() { @@ -65,7 +65,7 @@ public function test_create_instance_exceptions() { try { $gen->create_instance(array('courseid' => 2)); $this->fail('A note should not be allowed to be created without associcated userid'); - } catch (coding_exception $e) { + } catch (\coding_exception $e) { $this->assertStringContainsString('Module generator requires $record->userid', $e->getMessage()); } @@ -73,7 +73,7 @@ public function test_create_instance_exceptions() { try { $gen->create_instance(array('userid' => 2)); $this->fail('A note should not be allowed to be created without associcated courseid'); - } catch (coding_exception $e) { + } catch (\coding_exception $e) { $this->assertStringContainsString('Module generator requires $record->courseid', $e->getMessage()); } } diff --git a/question/tests/externallib_test.php b/question/tests/externallib_test.php index 19a54df21befd..71a597e309126 100644 --- a/question/tests/externallib_test.php +++ b/question/tests/externallib_test.php @@ -24,6 +24,11 @@ * @since Moodle 3.1 */ +namespace core_question; + +use core_question_external; +use externallib_advanced_testcase; + defined('MOODLE_INTERNAL') || die(); global $CFG; @@ -40,7 +45,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 3.1 */ -class core_question_external_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * Set up for every test @@ -71,17 +76,17 @@ public function test_core_question_update_flag() { // Create a question category. $cat = $questiongenerator->create_question_category(); - $quba = question_engine::make_questions_usage_by_activity('core_question_update_flag', context_system::instance()); + $quba = \question_engine::make_questions_usage_by_activity('core_question_update_flag', \context_system::instance()); $quba->set_preferred_behaviour('deferredfeedback'); $questiondata = $questiongenerator->create_question('numerical', null, ['category' => $cat->id]); - $question = question_bank::load_question($questiondata->id); + $question = \question_bank::load_question($questiondata->id); $slot = $quba->add_question($question); $qa = $quba->get_question_attempt($slot); self::setUser($this->student); $quba->start_all_questions(); - question_engine::save_questions_usage_by_activity($quba); + \question_engine::save_questions_usage_by_activity($quba); $qubaid = $quba->get_id(); $questionid = $question->id; @@ -98,7 +103,7 @@ public function test_core_question_update_flag() { core_question_external::update_flag($qubaid, $questionid, $qaid, $slot, $checksum, true); $this->fail('Exception expected due to invalid checksum.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('errorsavingflags', $e->errorcode); } } @@ -192,7 +197,7 @@ public function test_get_random_question_summaries_variations( ) { $this->resetAfterTest(); - $context = context_system::instance(); + $context = \context_system::instance(); $categories = []; $questions = []; $tagnames = [ @@ -201,8 +206,8 @@ public function test_get_random_question_summaries_variations( 'subcat', 'foo' ]; - $collid = core_tag_collection::get_default(); - $tags = core_tag_tag::create_if_missing($collid, $tagnames); + $collid = \core_tag_collection::get_default(); + $tags = \core_tag_tag::create_if_missing($collid, $tagnames); $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); // First category and questions. @@ -254,8 +259,8 @@ public function test_get_random_question_summaries_variations( public function test_get_random_question_summaries_invalid_category_id_param() { $this->resetAfterTest(); - $context = context_system::instance(); - $this->expectException('invalid_parameter_exception'); + $context = \context_system::instance(); + $this->expectException('\invalid_parameter_exception'); core_question_external::get_random_question_summaries('invalid value', false, [], $context->id); } @@ -266,8 +271,8 @@ public function test_get_random_question_summaries_invalid_category_id_param() { public function test_get_random_question_summaries_invalid_includesubcategories_param() { $this->resetAfterTest(); - $context = context_system::instance(); - $this->expectException('invalid_parameter_exception'); + $context = \context_system::instance(); + $this->expectException('\invalid_parameter_exception'); core_question_external::get_random_question_summaries(1, 'invalid value', [], $context->id); } @@ -278,8 +283,8 @@ public function test_get_random_question_summaries_invalid_includesubcategories_ public function test_get_random_question_summaries_invalid_tagids_param() { $this->resetAfterTest(); - $context = context_system::instance(); - $this->expectException('invalid_parameter_exception'); + $context = \context_system::instance(); + $this->expectException('\invalid_parameter_exception'); core_question_external::get_random_question_summaries(1, false, ['invalid', 'values'], $context->id); } @@ -290,7 +295,7 @@ public function test_get_random_question_summaries_invalid_tagids_param() { public function test_get_random_question_summaries_invalid_context() { $this->resetAfterTest(); - $this->expectException('invalid_parameter_exception'); + $this->expectException('\invalid_parameter_exception'); core_question_external::get_random_question_summaries(1, false, [1, 2], 'context'); } @@ -303,8 +308,8 @@ public function test_get_random_question_summaries_restricted_context() { $this->resetAfterTest(); $course = $this->getDataGenerator()->create_course(); - $coursecontext = context_course::instance($course->id); - $systemcontext = context_system::instance(); + $coursecontext = \context_course::instance($course->id); + $systemcontext = \context_system::instance(); // Restrict access to external functions for the logged in user to only // the course we just created. External functions should not be allowed // to execute in any contexts above the course context. @@ -316,7 +321,7 @@ public function test_get_random_question_summaries_restricted_context() { // Do this in a try/catch statement to allow the context restriction // to be reset afterwards. core_question_external::get_random_question_summaries(1, false, [], $systemcontext->id); - } catch (Exception $e) { + } catch (\Exception $e) { $this->assertInstanceOf('restricted_context_exception', $e); } // Reset the restriction so that other tests don't fail aftwards. @@ -330,7 +335,7 @@ public function test_get_random_question_summaries_formats_returned_questions() $this->resetAfterTest(); list($category, $questions) = $this->create_category_and_questions(1); - $context = context_system::instance(); + $context = \context_system::instance(); $question = $questions[0]; $expected = (object) [ 'id' => $question->id, @@ -366,7 +371,7 @@ public function test_get_random_question_summaries_with_limit_and_offset() { $tagids = []; $limit = 1; $offset = 0; - $context = context_system::instance(); + $context = \context_system::instance(); list($category, $questions) = $this->create_category_and_questions($numberofquestions); // Sort the questions by id to match the ordering of the result. @@ -411,13 +416,13 @@ public function test_get_random_question_summaries_without_capability() { $generator = $this->getDataGenerator(); $user = $generator->create_user(); $roleid = $generator->create_role(); - $systemcontext = context_system::instance(); + $systemcontext = \context_system::instance(); $numberofquestions = 5; $includesubcategories = false; $tagids = []; - $context = context_system::instance(); + $context = \context_system::instance(); list($category, $questions) = $this->create_category_and_questions($numberofquestions); - $categorycontext = context::instance_by_id($category->contextid); + $categorycontext = \context::instance_by_id($category->contextid); $generator->role_assign($roleid, $user->id, $systemcontext->id); // Prohibit all of the tag capabilities. @@ -459,8 +464,8 @@ protected function create_category_and_questions($questioncount, $tagnames = [], } if (!empty($tagnames) && !empty($questions)) { - $context = context::instance_by_id($category->contextid); - core_tag_tag::set_item_tags('core_question', 'question', $questions[0]->id, $context, $tagnames); + $context = \context::instance_by_id($category->contextid); + \core_tag_tag::set_item_tags('core_question', 'question', $questions[0]->id, $context, $tagnames); } return [$category, $questions]; diff --git a/question/tests/generator_test.php b/question/tests/generator_test.php index 39ce4ac3aa1cd..2fe2db6ff67b0 100644 --- a/question/tests/generator_test.php +++ b/question/tests/generator_test.php @@ -17,14 +17,13 @@ /** * Data generators tests * - * @package moodlecore + * @package core_question * @subpackage questionengine * @copyright 2013 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -defined('MOODLE_INTERNAL') || die(); - +namespace core_question; /** * Test data generator @@ -32,7 +31,7 @@ * @copyright 2013 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class core_question_generator_testcase extends advanced_testcase { +class generator_test extends \advanced_testcase { public function test_create() { global $DB; @@ -70,11 +69,11 @@ public function test_idnumbers_in_categories_and_questions() { $this->assertSame('myquest_3', $quest3->idnumber); // Check idnumbers of questions moved. Note have to use load_question_data or we only get to see old cached data. question_move_questions_to_category([$quest1->id], $qcat1->id); - $this->assertSame('myquest', question_bank::load_question_data($quest1->id)->idnumber); + $this->assertSame('myquest', \question_bank::load_question_data($quest1->id)->idnumber); // Can only change idnumber of quest2 once quest1 has been moved to another category. $quest2 = $generator->update_question($questions[1], null, ['idnumber' => 'myquest']); question_move_questions_to_category([$quest2->id], $qcat1->id); - $this->assertSame('myquest_4', question_bank::load_question_data($quest2->id)->idnumber); + $this->assertSame('myquest_4', \question_bank::load_question_data($quest2->id)->idnumber); // Check can add an idnumber of 0. $quest4 = $generator->create_question('shortanswer', null, ['name' => 'sa1', 'category' => $qcat1->id, 'idnumber' => '0']); $this->assertSame('0', $quest4->idnumber); diff --git a/rating/tests/externallib_test.php b/rating/tests/externallib_test.php index 4ffed3b578009..ec4723354e41f 100644 --- a/rating/tests/externallib_test.php +++ b/rating/tests/externallib_test.php @@ -23,6 +23,11 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +namespace core_rating; + +use core_rating_external; +use externallib_advanced_testcase; + defined('MOODLE_INTERNAL') || die(); global $CFG; @@ -38,7 +43,7 @@ * @copyright 2015 Costantino Cito * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class core_rating_externallib_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /* * Set up for every test @@ -64,7 +69,7 @@ public function setUp(): void { $this->getDataGenerator()->enrol_user($this->teacher3->id, $this->course->id, $this->teacherrole->id); // Create the forum. - $record = new stdClass(); + $record = new \stdClass(); $record->introformat = FORMAT_HTML; $record->course = $this->course->id; // Set Aggregate type = Average of ratings. @@ -72,10 +77,10 @@ public function setUp(): void { $record->scale = 100; $this->forum = self::getDataGenerator()->create_module('forum', $record); - $this->contextid = context_module::instance($this->forum->cmid)->id; + $this->contextid = \context_module::instance($this->forum->cmid)->id; // Add discussion to the forums. - $record = new stdClass(); + $record = new \stdClass(); $record->course = $this->course->id; $record->userid = $this->student1->id; $record->forum = $this->forum->id; @@ -91,7 +96,7 @@ public function test_get_item_ratings() { global $DB; // Rete the discussion as teacher1. - $rating1 = new stdClass(); + $rating1 = new \stdClass(); $rating1->contextid = $this->contextid; $rating1->component = 'mod_forum'; $rating1->ratingarea = 'post'; @@ -104,7 +109,7 @@ public function test_get_item_ratings() { $rating1->id = $DB->insert_record('rating', $rating1); // Rete the discussion as teacher2. - $rating2 = new stdClass(); + $rating2 = new \stdClass(); $rating2->contextid = $this->contextid; $rating2->component = 'mod_forum'; $rating2->ratingarea = 'post'; @@ -124,7 +129,7 @@ public function test_get_item_ratings() { $ratings = core_rating_external::get_item_ratings('module', $this->forum->cmid, 'mod_forum', 'post', $this->post->id, 100, ''); // We need to execute the return values cleaning process to simulate the web service server. - $ratings = external_api::clean_returnvalue(core_rating_external::get_item_ratings_returns(), $ratings); + $ratings = \external_api::clean_returnvalue(core_rating_external::get_item_ratings_returns(), $ratings); $this->assertCount(2, $ratings['ratings']); $indexedratings = array(); @@ -142,14 +147,14 @@ public function test_get_item_ratings() { $ratings = core_rating_external::get_item_ratings('module', $this->forum->cmid, 'mod_forum', 'post', $this->post->id, 100, ''); // We need to execute the return values cleaning process to simulate the web service server. - $ratings = external_api::clean_returnvalue(core_rating_external::get_item_ratings_returns(), $ratings); + $ratings = \external_api::clean_returnvalue(core_rating_external::get_item_ratings_returns(), $ratings); $this->assertCount(2, $ratings['ratings']); // Invalid item. try { $ratings = core_rating_external::get_item_ratings('module', $this->forum->cmid, 'mod_forum', 'post', 0, 100, ''); $this->fail('Exception expected due invalid itemid.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('invalidrecord', $e->errorcode); } @@ -157,7 +162,7 @@ public function test_get_item_ratings() { try { $ratings = core_rating_external::get_item_ratings('module', $this->forum->cmid, 'mod_forum', 'xyz', $this->post->id, 100, ''); $this->fail('Exception expected due invalid rating area.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('invalidratingarea', $e->errorcode); } @@ -165,7 +170,7 @@ public function test_get_item_ratings() { try { $ratings = core_rating_external::get_item_ratings('module', 0, 'mod_forum', 'post', $this->post->id, 100, ''); $this->fail('Exception expected due invalid context.'); - } catch (invalid_parameter_exception $e) { + } catch (\invalid_parameter_exception $e) { $this->assertEquals('invalidparameter', $e->errorcode); } @@ -181,14 +186,14 @@ public function test_get_item_ratings() { $this->setUser($this->teacher1); $ratings = core_rating_external::get_item_ratings('module', $this->forum->cmid, 'mod_forum', 'post', $this->post->id, 100, ''); // We need to execute the return values cleaning process to simulate the web service server. - $ratings = external_api::clean_returnvalue(core_rating_external::get_item_ratings_returns(), $ratings); + $ratings = \external_api::clean_returnvalue(core_rating_external::get_item_ratings_returns(), $ratings); $this->assertCount(2, $ratings['ratings']); $this->setUser($this->teacher3); try { $ratings = core_rating_external::get_item_ratings('module', $this->forum->cmid, 'mod_forum', 'post', $this->post->id, 100, ''); $this->fail('Exception expected due invalid group permissions.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('noviewrate', $e->errorcode); } @@ -204,7 +209,7 @@ public function test_add_rating() { $rating = core_rating_external::add_rating('module', $this->forum->cmid, 'mod_forum', 'post', $this->post->id, 100, 50, $this->student1->id, RATING_AGGREGATE_AVERAGE); // We need to execute the return values cleaning process to simulate the web service server. - $rating = external_api::clean_returnvalue(core_rating_external::add_rating_returns(), $rating); + $rating = \external_api::clean_returnvalue(core_rating_external::add_rating_returns(), $rating); $this->assertTrue($rating['success']); $this->assertEquals(50, $rating['aggregate']); $this->assertEquals(1, $rating['count']); @@ -212,7 +217,7 @@ public function test_add_rating() { // New different rate (it will replace the existing one). $rating = core_rating_external::add_rating('module', $this->forum->cmid, 'mod_forum', 'post', $this->post->id, 100, 100, $this->student1->id, RATING_AGGREGATE_AVERAGE); - $rating = external_api::clean_returnvalue(core_rating_external::add_rating_returns(), $rating); + $rating = \external_api::clean_returnvalue(core_rating_external::add_rating_returns(), $rating); $this->assertTrue($rating['success']); $this->assertEquals(100, $rating['aggregate']); $this->assertEquals(1, $rating['count']); @@ -221,7 +226,7 @@ public function test_add_rating() { $this->setUser($this->teacher2); $rating = core_rating_external::add_rating('module', $this->forum->cmid, 'mod_forum', 'post', $this->post->id, 100, 50, $this->student1->id, RATING_AGGREGATE_AVERAGE); - $rating = external_api::clean_returnvalue(core_rating_external::add_rating_returns(), $rating); + $rating = \external_api::clean_returnvalue(core_rating_external::add_rating_returns(), $rating); $this->assertEquals(75, $rating['aggregate']); $this->assertEquals(2, $rating['count']); diff --git a/report/insights/tests/externallib_test.php b/report/insights/tests/externallib_test.php index 067316fb137bd..0013668b33617 100644 --- a/report/insights/tests/externallib_test.php +++ b/report/insights/tests/externallib_test.php @@ -22,6 +22,8 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +namespace report_insights; + defined('MOODLE_INTERNAL') || die(); require_once(__DIR__ . '/../../../analytics/tests/fixtures/test_indicator_max.php'); @@ -34,7 +36,7 @@ * @copyright 2019 David Monllaó {@link http://www.davidmonllao.com} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class report_insights_external_testcase extends advanced_testcase { +class externallib_test extends \advanced_testcase { /** * test_action_executed diff --git a/repository/tests/generator_test.php b/repository/tests/generator_test.php index ba40bc990cffa..aea74f8cd974c 100644 --- a/repository/tests/generator_test.php +++ b/repository/tests/generator_test.php @@ -17,13 +17,15 @@ /** * Repository generator tests * - * @package repository + * @package core_repository * @category test * @copyright 2013 Frédéric Massart * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -defined('MOODLE_INTERNAL') || die(); +namespace core_repository; + +use repository_exception; /** * Repository generator tests class @@ -33,7 +35,7 @@ * @copyright 2013 Frédéric Massart * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class core_repository_generator_testcase extends advanced_testcase { +class generator_test extends \advanced_testcase { /** * Basic test of creation of repository types. @@ -87,7 +89,7 @@ public function test_create_type_custom_options() { // Single instances. // Note: for single instances repositories enablecourseinstances and enableuserinstances are forced set to 0. - $record = new stdClass(); + $record = new \stdClass(); $record->pluginname = 'Custom Flickr'; $record->api_key = '12345'; $record->secret = '67890'; @@ -111,7 +113,7 @@ public function test_create_type_custom_options() { 'showonloginpage' => 1, ]; $issuer = \core\oauth2\api::create_issuer((object)$params); - $record = new stdClass(); + $record = new \stdClass(); $record->pluginname = 'Custom Dropbox'; $record->dropbox_issuerid = $issuer->get('id'); $record->dropbox_cachelimit = '123'; @@ -125,7 +127,7 @@ public function test_create_type_custom_options() { $DB->get_field('repository_instances', 'name', array('typeid' => $dropbox->id), MUST_EXIST)); // Multiple instances. - $record = new stdClass(); + $record = new \stdClass(); $record->pluginname = 'Custom WebDAV'; $record->enableuserinstances = '0'; $record->enablecourseinstances = '0'; @@ -135,7 +137,7 @@ public function test_create_type_custom_options() { $this->assertEquals($record, $config); $this->assertFalse( $DB->record_exists('repository_instances', array('typeid' => $webdav->id))); - $record = new stdClass(); + $record = new \stdClass(); $record->pluginname = 'Custom Equella'; $record->enableuserinstances = '1'; $record->enablecourseinstances = '0'; @@ -160,7 +162,7 @@ public function test_create_instance() { $block = $this->getDataGenerator()->create_block('online_users'); $type = $this->getDataGenerator()->create_repository_type('webdav'); - $record = new stdClass(); + $record = new \stdClass(); $record->name = 'A WebDAV instance'; $record->webdav_type = '1'; $record->webdav_server = 'localhost'; @@ -174,7 +176,7 @@ public function test_create_instance() { $this->assertEquals(1, $DB->count_records('repository_instances', array('typeid' => $type->id))); $this->assertEquals($record->name, $DB->get_field('repository_instances', 'name', array('id' => $instance->id))); $entries = $DB->get_records('repository_instance_config', array('instanceid' => $instance->id)); - $config = new stdClass(); + $config = new \stdClass(); foreach ($entries as $entry) { $config->{$entry->name} = $entry->value; } @@ -182,21 +184,21 @@ public function test_create_instance() { $this->assertEquals($config, $record); // Course context. - $record = new stdClass(); - $record->contextid = context_course::instance($course->id)->id; + $record = new \stdClass(); + $record->contextid = \context_course::instance($course->id)->id; $instance = $this->getDataGenerator()->create_repository('webdav', $record); $this->assertEquals(2, $DB->count_records('repository_instances', array('typeid' => $type->id))); $this->assertEquals($record->contextid, $instance->contextid); // User context. - $record->contextid = context_user::instance($user->id)->id; + $record->contextid = \context_user::instance($user->id)->id; $instance = $this->getDataGenerator()->create_repository('webdav', $record); $this->assertEquals(3, $DB->count_records('repository_instances', array('typeid' => $type->id))); $this->assertEquals($record->contextid, $instance->contextid); // Invalid context. $this->expectException('coding_exception'); - $record->contextid = context_block::instance($block->id)->id; + $record->contextid = \context_block::instance($block->id)->id; $instance = $this->getDataGenerator()->create_repository('webdav', $record); } diff --git a/user/tests/externallib_test.php b/user/tests/externallib_test.php index fd9f5cb16936c..95812a41baba0 100644 --- a/user/tests/externallib_test.php +++ b/user/tests/externallib_test.php @@ -24,6 +24,12 @@ * @since Moodle 2.4 */ +namespace core_user; + +use core_files_external; +use core_user_external; +use externallib_advanced_testcase; + defined('MOODLE_INTERNAL') || die(); global $CFG; @@ -32,7 +38,7 @@ require_once($CFG->dirroot . '/user/externallib.php'); require_once($CFG->dirroot . '/files/externallib.php'); -class core_user_externallib_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { /** * Test get_users @@ -74,7 +80,7 @@ public function test_get_users() { $generatedusers[$user1->id] = $user1; $generatedusers[$user2->id] = $user2; - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $roleid = $this->assignUserCapability('moodle/user:viewdetails', $context->id); // Enrol the users in the course. @@ -94,7 +100,7 @@ public function test_get_users() { $result = core_user_external::get_users($searchparams); // We need to execute the return values cleaning process to simulate the web service server - $result = external_api::clean_returnvalue(core_user_external::get_users_returns(), $result); + $result = \external_api::clean_returnvalue(core_user_external::get_users_returns(), $result); // Check we retrieve the good total number of enrolled users + no error on capability. $expectedreturnedusers = 1; @@ -162,9 +168,9 @@ public function test_get_users() { // Call the external function. $result = core_user_external::get_users($searchparams); $this->fail('Expecting \'keyalreadyset\' moodle_exception to be thrown.'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('keyalreadyset', $e->errorcode); - } catch (Exception $e) { + } catch (\Exception $e) { $this->fail('Expecting \'keyalreadyset\' moodle_exception to be thrown.'); } } @@ -207,7 +213,7 @@ public function test_get_users_by_field() { $generatedusers[$user1->id] = $user1; $generatedusers[$user2->id] = $user2; - $context = context_course::instance($course->id); + $context = \context_course::instance($course->id); $roleid = $this->assignUserCapability('moodle/user:viewdetails', $context->id); // Enrol the users in the course. @@ -225,7 +231,7 @@ public function test_get_users_by_field() { // Call the external function. $returnedusers = core_user_external::get_users_by_field($fieldtosearch, array($USER->{$fieldtosearch}, $user1->{$fieldtosearch}, $user2->{$fieldtosearch})); - $returnedusers = external_api::clean_returnvalue(core_user_external::get_users_by_field_returns(), $returnedusers); + $returnedusers = \external_api::clean_returnvalue(core_user_external::get_users_by_field_returns(), $returnedusers); // Expected result differ following the searched field // Admin user in the PHPunit framework doesn't have an idnumber. @@ -292,7 +298,7 @@ public function test_get_users_by_field() { // Call the external function. $returnedusers = core_user_external::get_users_by_field('username', array($USER->username, $user1->username, $user2->username)); - $returnedusers = external_api::clean_returnvalue(core_user_external::get_users_by_field_returns(), $returnedusers); + $returnedusers = \external_api::clean_returnvalue(core_user_external::get_users_by_field_returns(), $returnedusers); // Only the own $USER username should be returned $this->assertEquals(1, count($returnedusers)); @@ -303,7 +309,7 @@ public function test_get_users_by_field() { // Call the external function. $returnedusers = core_user_external::get_users_by_field('username', array($USER->username, $user1->username, $user2->username)); - $returnedusers = external_api::clean_returnvalue(core_user_external::get_users_by_field_returns(), $returnedusers); + $returnedusers = \external_api::clean_returnvalue(core_user_external::get_users_by_field_returns(), $returnedusers); // Only the own $USER username should be returned still. $this->assertEquals(1, count($returnedusers)); @@ -314,7 +320,7 @@ public function get_course_user_profiles_setup($capability) { $this->resetAfterTest(true); - $return = new stdClass(); + $return = new \stdClass(); // Create the course and fetch its context. $return->course = self::getDataGenerator()->create_course(); @@ -342,7 +348,7 @@ public function get_course_user_profiles_setup($capability) { } $return->user2 = self::getDataGenerator()->create_user(); - $context = context_course::instance($return->course->id); + $context = \context_course::instance($return->course->id); $return->roleid = $this->assignUserCapability($capability, $context->id); // Enrol the users in the course. @@ -368,7 +374,7 @@ public function test_get_course_user_profiles() { array('userid' => $USER->id, 'courseid' => $data->course->id))); // We need to execute the return values cleaning process to simulate the web service server. - $enrolledusers = external_api::clean_returnvalue(core_user_external::get_course_user_profiles_returns(), $enrolledusers); + $enrolledusers = \external_api::clean_returnvalue(core_user_external::get_course_user_profiles_returns(), $enrolledusers); // Check we retrieve the good total number of enrolled users + no error on capability. $this->assertEquals(1, count($enrolledusers)); @@ -392,7 +398,7 @@ public function test_get_user_course_profile_as_admin() { array('userid' => $data->user1->id, 'courseid' => $data->course->id))); // We need to execute the return values cleaning process to simulate the web service server. - $enrolledusers = external_api::clean_returnvalue(core_user_external::get_course_user_profiles_returns(), $enrolledusers); + $enrolledusers = \external_api::clean_returnvalue(core_user_external::get_course_user_profiles_returns(), $enrolledusers); foreach($enrolledusers as $enrolleduser) { if ($enrolleduser['username'] == $data->user1->username) { @@ -462,7 +468,7 @@ public function test_create_users() { 'auth' => 'oauth2' ); - $context = context_system::instance(); + $context = \context_system::instance(); $roleid = $this->assignUserCapability('moodle/user:create', $context->id); $this->assignUserCapability('moodle/user:editprofile', $context->id, $roleid); @@ -470,7 +476,7 @@ public function test_create_users() { $createdusers = core_user_external::create_users(array($user1, $user2)); // We need to execute the return values cleaning process to simulate the web service server. - $createdusers = external_api::clean_returnvalue(core_user_external::create_users_returns(), $createdusers); + $createdusers = \external_api::clean_returnvalue(core_user_external::create_users_returns(), $createdusers); // Check we retrieve the good total number of created users + no error on capability. $this->assertCount(2, $createdusers); @@ -483,8 +489,8 @@ public function test_create_users() { $this->assertEquals('atto', get_user_preferences('htmleditor', null, $dbuser)); $this->assertEquals(null, get_user_preferences('invalidpreference', null, $dbuser)); // Confirm user interests have been saved. - $interests = core_tag_tag::get_item_tags_array('core', 'user', $createduser['id'], - core_tag_tag::BOTH_STANDARD_AND_NOT, 0, false); + $interests = \core_tag_tag::get_item_tags_array('core', 'user', $createduser['id'], + \core_tag_tag::BOTH_STANDARD_AND_NOT, 0, false); // There should be 3 user interests. $this->assertCount(3, $interests); @@ -530,7 +536,7 @@ public function test_create_users_empty_password() { ]; // This should throw an exception because either password or createpassword param must be passed for auth_manual. - $this->expectException(invalid_parameter_exception::class); + $this->expectException(\invalid_parameter_exception::class); core_user_external::create_users([$user]); } @@ -588,7 +594,7 @@ public function test_create_users_with_same_emails($sameemailallowed, $different if (!$sameemailallowed) { // This should throw an exception when $CFG->allowaccountssameemail is empty. - $this->expectException(invalid_parameter_exception::class); + $this->expectException(\invalid_parameter_exception::class); } // Create our users. @@ -692,7 +698,7 @@ public function test_delete_users() { $this->assertEquals(2, $DB->count_records_select('user', 'deleted = 0 AND (id = :userid1 OR id = :userid2)', array('userid1' => $user1->id, 'userid2' => $user2->id))); - $context = context_system::instance(); + $context = \context_system::instance(); $roleid = $this->assignUserCapability('moodle/user:delete', $context->id); // Call the external function. @@ -720,7 +726,7 @@ public function test_update_users() { $wsuser = self::getDataGenerator()->create_user(); self::setUser($wsuser); - $context = context_user::instance($USER->id); + $context = \context_user::instance($USER->id); $contextid = $context->id; $filename = "reddot.png"; $filecontent = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38" @@ -729,7 +735,7 @@ public function test_update_users() { // Call the files api to create a file. $draftfile = core_files_external::upload($contextid, 'user', 'draft', 0, '/', $filename, $filecontent, null, null); - $draftfile = external_api::clean_returnvalue(core_files_external::upload_returns(), $draftfile); + $draftfile = \external_api::clean_returnvalue(core_files_external::upload_returns(), $draftfile); $draftid = $draftfile['itemid']; @@ -766,7 +772,7 @@ public function test_update_users() { 'interests' => 'badminton, basketball, cooking, ' ); - $context = context_system::instance(); + $context = \context_system::instance(); $roleid = $this->assignUserCapability('moodle/user:update', $context->id); $this->assignUserCapability('moodle/user:editprofile', $context->id, $roleid); @@ -786,7 +792,7 @@ public function test_update_users() { // Call the external function. $returnvalue = core_user_external::update_users(array($user1, $user2, $user3, $user4)); - $returnvalue = external_api::clean_returnvalue(core_user_external::update_users_returns(), $returnvalue); + $returnvalue = \external_api::clean_returnvalue(core_user_external::update_users_returns(), $returnvalue); // Check warnings. $this->assertEquals($user2['id'], $returnvalue['warnings'][0]['itemid']); // Guest user. @@ -821,7 +827,7 @@ public function test_update_users() { $this->assertEquals(null, get_user_preferences('invalidpreference', null, $dbuser)); // Confirm user interests have been saved. - $interests = core_tag_tag::get_item_tags_array('core', 'user', $user1['id'], core_tag_tag::BOTH_STANDARD_AND_NOT, 0, false); + $interests = \core_tag_tag::get_item_tags_array('core', 'user', $user1['id'], \core_tag_tag::BOTH_STANDARD_AND_NOT, 0, false); // There should be 3 user interests. $this->assertCount(3, $interests); @@ -840,7 +846,7 @@ public function test_update_users() { // Updating user with an invalid email. $user5['email'] = 'bogus'; $returnvalue = core_user_external::update_users(array($user5)); - $returnvalue = external_api::clean_returnvalue(core_user_external::update_users_returns(), $returnvalue); + $returnvalue = \external_api::clean_returnvalue(core_user_external::update_users_returns(), $returnvalue); $this->assertEquals('useremailinvalid', $returnvalue['warnings'][0]['warningcode']); $this->assertStringContainsString('Invalid email address', $returnvalue['warnings'][0]['message']); @@ -848,7 +854,7 @@ public function test_update_users() { // Updating user with a duplicate email. $user5['email'] = $user1['email']; $returnvalue = core_user_external::update_users(array($user1, $user5)); - $returnvalue = external_api::clean_returnvalue(core_user_external::update_users_returns(), $returnvalue); + $returnvalue = \external_api::clean_returnvalue(core_user_external::update_users_returns(), $returnvalue); $this->assertEquals('useremailduplicate', $returnvalue['warnings'][0]['warningcode']); $this->assertStringContainsString('Duplicate email address', $returnvalue['warnings'][0]['message']); @@ -856,7 +862,7 @@ public function test_update_users() { // Updating a user that does not exist. $user5['id'] = -1; $returnvalue = core_user_external::update_users(array($user5)); - $returnvalue = external_api::clean_returnvalue(core_user_external::update_users_returns(), $returnvalue); + $returnvalue = \external_api::clean_returnvalue(core_user_external::update_users_returns(), $returnvalue); $this->assertEquals('invaliduserid', $returnvalue['warnings'][0]['warningcode']); $this->assertStringContainsString('Invalid user ID', $returnvalue['warnings'][0]['message']); @@ -866,7 +872,7 @@ public function test_update_users() { user_update_user($user1); // Update user not using webservice. unset($user1['mnethostid']); // The mnet host ID field is not in the allowed field list for the webservice. $returnvalue = core_user_external::update_users(array($user1)); - $returnvalue = external_api::clean_returnvalue(core_user_external::update_users_returns(), $returnvalue); + $returnvalue = \external_api::clean_returnvalue(core_user_external::update_users_returns(), $returnvalue); $this->assertEquals('usernotupdatedremote', $returnvalue['warnings'][0]['warningcode']); $this->assertStringContainsString('User is a remote user', $returnvalue['warnings'][0]['message']); @@ -970,10 +976,10 @@ public function test_add_user_private_files() { $this->resetAfterTest(true); - $context = context_system::instance(); + $context = \context_system::instance(); $roleid = $this->assignUserCapability('moodle/user:manageownfiles', $context->id); - $context = context_user::instance($USER->id); + $context = \context_user::instance($USER->id); $contextid = $context->id; $component = "user"; $filearea = "draft"; @@ -988,7 +994,7 @@ public function test_add_user_private_files() { // Call the files api to create a file. $draftfile = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent, $contextlevel, $instanceid); - $draftfile = external_api::clean_returnvalue(core_files_external::upload_returns(), $draftfile); + $draftfile = \external_api::clean_returnvalue(core_files_external::upload_returns(), $draftfile); $draftid = $draftfile['itemid']; // Make sure the file was created. @@ -1038,14 +1044,14 @@ public function test_add_user_device() { $warnings = core_user_external::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'], $device['version'], $device['pushid'], $device['uuid']); // We need to execute the return values cleaning process to simulate the web service server. - $warnings = external_api::clean_returnvalue(core_user_external::add_user_device_returns(), $warnings); + $warnings = \external_api::clean_returnvalue(core_user_external::add_user_device_returns(), $warnings); $this->assertCount(1, $warnings); // Test update an existing device. $device['pushid'] = 'different than before'; $warnings = core_user_external::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'], $device['version'], $device['pushid'], $device['uuid']); - $warnings = external_api::clean_returnvalue(core_user_external::add_user_device_returns(), $warnings); + $warnings = \external_api::clean_returnvalue(core_user_external::add_user_device_returns(), $warnings); $this->assertEquals(1, $DB->count_records('user_devices')); $updated = $DB->get_record('user_devices', array('pushid' => $device['pushid'])); @@ -1056,7 +1062,7 @@ public function test_add_user_device() { $device['pushid'] = 'new different than before'; $warnings = core_user_external::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'], $device['version'], $device['pushid'], $device['uuid']); - $warnings = external_api::clean_returnvalue(core_user_external::add_user_device_returns(), $warnings); + $warnings = \external_api::clean_returnvalue(core_user_external::add_user_device_returns(), $warnings); $this->assertEquals(2, $DB->count_records('user_devices')); } @@ -1094,24 +1100,24 @@ public function test_remove_user_device() { // Try to remove a device that does not exist. $result = core_user_external::remove_user_device('1234567890'); - $result = external_api::clean_returnvalue(core_user_external::remove_user_device_returns(), $result); + $result = \external_api::clean_returnvalue(core_user_external::remove_user_device_returns(), $result); $this->assertFalse($result['removed']); $this->assertCount(1, $result['warnings']); // Try to remove a device that does not exist for an existing app. $result = core_user_external::remove_user_device('1234567890', $device['appid']); - $result = external_api::clean_returnvalue(core_user_external::remove_user_device_returns(), $result); + $result = \external_api::clean_returnvalue(core_user_external::remove_user_device_returns(), $result); $this->assertFalse($result['removed']); $this->assertCount(1, $result['warnings']); // Remove an existing device for an existing app. This will remove one of the two devices. $result = core_user_external::remove_user_device($device['uuid'], $device['appid']); - $result = external_api::clean_returnvalue(core_user_external::remove_user_device_returns(), $result); + $result = \external_api::clean_returnvalue(core_user_external::remove_user_device_returns(), $result); $this->assertTrue($result['removed']); // Remove all the devices. This must remove the remaining device. $result = core_user_external::remove_user_device($device['uuid']); - $result = external_api::clean_returnvalue(core_user_external::remove_user_device_returns(), $result); + $result = \external_api::clean_returnvalue(core_user_external::remove_user_device_returns(), $result); $this->assertTrue($result['removed']); } @@ -1128,7 +1134,7 @@ public function test_get_user_preferences() { $this->setUser($user); $result = core_user_external::get_user_preferences(); - $result = external_api::clean_returnvalue(core_user_external::get_user_preferences_returns(), $result); + $result = \external_api::clean_returnvalue(core_user_external::get_user_preferences_returns(), $result); $this->assertCount(0, $result['warnings']); // Expect 3, _lastloaded is always returned. $this->assertCount(3, $result['preferences']); @@ -1143,14 +1149,14 @@ public function test_get_user_preferences() { // Retrieve just one preference. $result = core_user_external::get_user_preferences('some_random_text'); - $result = external_api::clean_returnvalue(core_user_external::get_user_preferences_returns(), $result); + $result = \external_api::clean_returnvalue(core_user_external::get_user_preferences_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertCount(1, $result['preferences']); $this->assertEquals('text', $result['preferences'][0]['value']); // Retrieve non-existent preference. $result = core_user_external::get_user_preferences('non_existent'); - $result = external_api::clean_returnvalue(core_user_external::get_user_preferences_returns(), $result); + $result = \external_api::clean_returnvalue(core_user_external::get_user_preferences_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertCount(1, $result['preferences']); $this->assertEquals(null, $result['preferences'][0]['value']); @@ -1158,7 +1164,7 @@ public function test_get_user_preferences() { // Check that as admin we can retrieve all the preferences for any user. $this->setAdminUser(); $result = core_user_external::get_user_preferences('', $user->id); - $result = external_api::clean_returnvalue(core_user_external::get_user_preferences_returns(), $result); + $result = \external_api::clean_returnvalue(core_user_external::get_user_preferences_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertCount(3, $result['preferences']); @@ -1189,7 +1195,7 @@ public function test_update_picture() { $user = self::getDataGenerator()->create_user(); self::setUser($user); - $context = context_user::instance($USER->id); + $context = \context_user::instance($USER->id); $contextid = $context->id; $filename = "reddot.png"; $filecontent = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38" @@ -1201,7 +1207,7 @@ public function test_update_picture() { // Change user profile image. $result = core_user_external::update_picture($draftid); - $result = external_api::clean_returnvalue(core_user_external::update_picture_returns(), $result); + $result = \external_api::clean_returnvalue(core_user_external::update_picture_returns(), $result); $picture = $DB->get_field('user', 'picture', array('id' => $user->id)); // The new revision is in the url for the user. $this->assertStringContainsString($picture, $result['profileimageurl']); @@ -1210,7 +1216,7 @@ public function test_update_picture() { // Delete image. $result = core_user_external::update_picture(0, true); - $result = external_api::clean_returnvalue(core_user_external::update_picture_returns(), $result); + $result = \external_api::clean_returnvalue(core_user_external::update_picture_returns(), $result); $picture = $DB->get_field('user', 'picture', array('id' => $user->id)); // No picture. $this->assertEquals(0, $picture); @@ -1218,13 +1224,13 @@ public function test_update_picture() { // Add again the user profile image (as admin). $this->setAdminUser(); - $context = context_user::instance($USER->id); + $context = \context_user::instance($USER->id); $admincontextid = $context->id; $draftfile = core_files_external::upload($admincontextid, 'user', 'draft', 0, '/', $filename, $filecontent, null, null); $draftid = $draftfile['itemid']; $result = core_user_external::update_picture($draftid, false, $user->id); - $result = external_api::clean_returnvalue(core_user_external::update_picture_returns(), $result); + $result = \external_api::clean_returnvalue(core_user_external::update_picture_returns(), $result); // The new revision is in the url for the user. $picture = $DB->get_field('user', 'picture', array('id' => $user->id)); $this->assertStringContainsString($picture, $result['profileimageurl']); @@ -1270,7 +1276,7 @@ public function test_set_user_preferences_save() { ); $result = core_user_external::set_user_preferences($preferences); - $result = external_api::clean_returnvalue(core_user_external::set_user_preferences_returns(), $result); + $result = \external_api::clean_returnvalue(core_user_external::set_user_preferences_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertCount(2, $result['saved']); @@ -1301,7 +1307,7 @@ public function test_set_user_preferences_save_invalid_pref() { ); $result = core_user_external::set_user_preferences($preferences); - $result = external_api::clean_returnvalue(core_user_external::set_user_preferences_returns(), $result); + $result = \external_api::clean_returnvalue(core_user_external::set_user_preferences_returns(), $result); $this->assertCount(1, $result['warnings']); $this->assertCount(0, $result['saved']); $this->assertEquals('nopermission', $result['warnings'][0]['warningcode']); @@ -1326,7 +1332,7 @@ public function test_set_user_preferences_invalid_user() { ); $result = core_user_external::set_user_preferences($preferences); - $result = external_api::clean_returnvalue(core_user_external::set_user_preferences_returns(), $result); + $result = \external_api::clean_returnvalue(core_user_external::set_user_preferences_returns(), $result); $this->assertCount(1, $result['warnings']); $this->assertCount(0, $result['saved']); $this->assertEquals('invaliduser', $result['warnings'][0]['warningcode']); @@ -1351,7 +1357,7 @@ public function test_set_user_preferences_invalid_preference() { ); $result = core_user_external::set_user_preferences($preferences); - $result = external_api::clean_returnvalue(core_user_external::set_user_preferences_returns(), $result); + $result = \external_api::clean_returnvalue(core_user_external::set_user_preferences_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertCount(1, $result['saved']); // Cleaned valud of the preference was saved. @@ -1405,7 +1411,7 @@ public function test_update_user_preferences_unset() { ); $result = core_user_external::set_user_preferences($preferences); - $result = external_api::clean_returnvalue(core_user_external::set_user_preferences_returns(), $result); + $result = \external_api::clean_returnvalue(core_user_external::set_user_preferences_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertCount(1, $result['saved']); @@ -1431,7 +1437,7 @@ public function test_agree_site_policy() { // Site policy not set. $result = core_user_external::agree_site_policy(); - $result = external_api::clean_returnvalue(core_user_external::agree_site_policy_returns(), $result); + $result = \external_api::clean_returnvalue(core_user_external::agree_site_policy_returns(), $result); $this->assertFalse($result['status']); $this->assertCount(1, $result['warnings']); $this->assertEquals('nositepolicy', $result['warnings'][0]['warningcode']); @@ -1441,7 +1447,7 @@ public function test_agree_site_policy() { $this->assertEquals(0, $USER->policyagreed); $result = core_user_external::agree_site_policy(); - $result = external_api::clean_returnvalue(core_user_external::agree_site_policy_returns(), $result); + $result = \external_api::clean_returnvalue(core_user_external::agree_site_policy_returns(), $result); $this->assertTrue($result['status']); $this->assertCount(0, $result['warnings']); $this->assertEquals(1, $USER->policyagreed); @@ -1449,7 +1455,7 @@ public function test_agree_site_policy() { // Try again, we should get a warning. $result = core_user_external::agree_site_policy(); - $result = external_api::clean_returnvalue(core_user_external::agree_site_policy_returns(), $result); + $result = \external_api::clean_returnvalue(core_user_external::agree_site_policy_returns(), $result); $this->assertFalse($result['status']); $this->assertCount(1, $result['warnings']); $this->assertEquals('alreadyagreed', $result['warnings'][0]['warningcode']); @@ -1463,9 +1469,9 @@ public function test_agree_site_policy() { try { $result = core_user_external::agree_site_policy(); $this->fail('Expecting \'usernotfullysetup\' moodle_exception to be thrown'); - } catch (moodle_exception $e) { + } catch (\moodle_exception $e) { $this->assertEquals('usernotfullysetup', $e->errorcode); - } catch (Exception $e) { + } catch (\Exception $e) { $this->fail('Expecting \'usernotfullysetup\' moodle_exception to be thrown.'); } } @@ -1478,7 +1484,7 @@ public function test_get_private_files_info() { $this->resetAfterTest(true); $user = self::getDataGenerator()->create_user(); $this->setUser($user); - $usercontext = context_user::instance($user->id); + $usercontext = \context_user::instance($user->id); $filerecord = array( 'contextid' => $usercontext->id, @@ -1494,7 +1500,7 @@ public function test_get_private_files_info() { // Get my private files information. $result = core_user_external::get_private_files_info(); - $result = external_api::clean_returnvalue(core_user_external::get_private_files_info_returns(), $result); + $result = \external_api::clean_returnvalue(core_user_external::get_private_files_info_returns(), $result); $this->assertEquals(1, $result['filecount']); $this->assertEquals($file->get_filesize(), $result['filesize']); $this->assertEquals(0, $result['foldercount']); @@ -1503,7 +1509,7 @@ public function test_get_private_files_info() { // As admin, get user information. $this->setAdminUser(); $result = core_user_external::get_private_files_info($user->id); - $result = external_api::clean_returnvalue(core_user_external::get_private_files_info_returns(), $result); + $result = \external_api::clean_returnvalue(core_user_external::get_private_files_info_returns(), $result); $this->assertEquals(1, $result['filecount']); $this->assertEquals($file->get_filesize(), $result['filesize']); $this->assertEquals(0, $result['foldercount']); @@ -1580,7 +1586,7 @@ public function test_external_search_identity() { $CFG->maxusersperpage = 3; $result = \core_user\external\search_identity::execute('Lastt'); - $result = external_api::clean_returnvalue(\core_user\external\search_identity::execute_returns(), $result); + $result = \external_api::clean_returnvalue(\core_user\external\search_identity::execute_returns(), $result); $this->assertEquals(2, count($result['list'])); $this->assertEquals(3, $result['maxusersperpage']); @@ -1597,7 +1603,7 @@ public function test_external_search_identity() { $CFG->maxusersperpage = 2; $result = \core_user\external\search_identity::execute('Firstt'); - $result = external_api::clean_returnvalue(\core_user\external\search_identity::execute_returns(), $result); + $result = \external_api::clean_returnvalue(\core_user\external\search_identity::execute_returns(), $result); $this->assertEquals(2, count($result['list'])); $this->assertEquals(2, $result['maxusersperpage']); @@ -1612,7 +1618,7 @@ public function test_external_search_identity() { $CFG->maxusersperpage = 2; $result = \core_user\external\search_identity::execute('City One'); - $result = external_api::clean_returnvalue(\core_user\external\search_identity::execute_returns(), $result); + $result = \external_api::clean_returnvalue(\core_user\external\search_identity::execute_returns(), $result); $this->assertEquals(0, count($result['list'])); $this->assertEquals(2, $result['maxusersperpage']); @@ -1627,7 +1633,7 @@ public function test_external_search_identity() { } $result = \core_user\external\search_identity::execute('City One'); - $result = external_api::clean_returnvalue(\core_user\external\search_identity::execute_returns(), $result); + $result = \external_api::clean_returnvalue(\core_user\external\search_identity::execute_returns(), $result); $this->assertEquals(2, count($result['list'])); $this->assertEquals(2, $result['maxusersperpage']); @@ -1658,7 +1664,7 @@ public function test_external_search_identity_with_alternativefullnameformat() { 'lastname firstname (lastnamephonetic firstnamephonetic)'; $result = \core_user\external\search_identity::execute('Ak'); - $result = external_api::clean_returnvalue(\core_user\external\search_identity::execute_returns(), $result); + $result = \external_api::clean_returnvalue(\core_user\external\search_identity::execute_returns(), $result); $this->assertEquals(1, count($result['list'])); $this->assertEquals(3, $result['maxusersperpage']); diff --git a/webservice/tests/externallib_test.php b/webservice/tests/externallib_test.php index 1a7480abd32e3..64b69b3fa41a0 100644 --- a/webservice/tests/externallib_test.php +++ b/webservice/tests/externallib_test.php @@ -14,6 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +namespace core_webservice; + +use externallib_advanced_testcase; + defined('MOODLE_INTERNAL') || die(); global $CFG; @@ -28,7 +32,7 @@ * @copyright 2012 Paul Charsley * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class core_webservice_externallib_testcase extends externallib_advanced_testcase { +class externallib_test extends externallib_advanced_testcase { public function setUp(): void { // Calling parent is good, always @@ -58,7 +62,7 @@ public function test_get_site_info() { self::setUser(self::getDataGenerator()->create_user($user)); // Add a web service and token. - $webservice = new stdClass(); + $webservice = new \stdClass(); $webservice->name = 'Test web service'; $webservice->enabled = true; $webservice->restrictedusers = false; @@ -73,7 +77,7 @@ public function test_get_site_info() { 'functionname' => 'core_course_get_contents')); $_POST['wstoken'] = 'testtoken'; - $externaltoken = new stdClass(); + $externaltoken = new \stdClass(); $externaltoken->token = 'testtoken'; $externaltoken->tokentype = 0; $externaltoken->userid = $USER->id; @@ -83,10 +87,10 @@ public function test_get_site_info() { $externaltoken->timecreated = time(); $DB->insert_record('external_tokens', $externaltoken); - $siteinfo = core_webservice_external::get_site_info(); + $siteinfo = \core_webservice_external::get_site_info(); // We need to execute the return values cleaning process to simulate the web service server. - $siteinfo = external_api::clean_returnvalue(core_webservice_external::get_site_info_returns(), $siteinfo); + $siteinfo = \external_api::clean_returnvalue(\core_webservice_external::get_site_info_returns(), $siteinfo); $this->assertEquals('johnd', $siteinfo['username']); $this->assertEquals('John', $siteinfo['firstname']); @@ -141,7 +145,7 @@ public function test_get_site_info() { // Set a fake token for the user admin. $_POST['wstoken'] = 'testtoken'; - $externaltoken = new stdClass(); + $externaltoken = new \stdClass(); $externaltoken->token = 'testtoken'; $externaltoken->tokentype = 0; $externaltoken->userid = $USER->id; @@ -155,10 +159,10 @@ public function test_get_site_info() { $CFG->defaulthomepage = HOMEPAGE_USER; set_user_preference('user_home_page_preference', HOMEPAGE_SITE); - $siteinfo = core_webservice_external::get_site_info(); + $siteinfo = \core_webservice_external::get_site_info(); // We need to execute the return values cleaning process to simulate the web service server. - $siteinfo = external_api::clean_returnvalue(core_webservice_external::get_site_info_returns(), $siteinfo); + $siteinfo = \external_api::clean_returnvalue(\core_webservice_external::get_site_info_returns(), $siteinfo); $this->assertEquals(0, $siteinfo['userquota']); $this->assertEquals(USER_CAN_IGNORE_FILE_SIZE_LIMITS, $siteinfo['usermaxuploadfilesize']); @@ -170,7 +174,7 @@ public function test_get_site_info() { $this->assertFalse(isset($siteinfo['usersessionscount'])); $CFG->limitconcurrentlogins = 1; - $record = new stdClass(); + $record = new \stdClass(); $record->state = 0; $record->sessdata = null; $record->userid = $USER->id; @@ -180,8 +184,8 @@ public function test_get_site_info() { $record->timecreated = time(); $DB->insert_record('sessions', $record); - $siteinfo = core_webservice_external::get_site_info(); - $siteinfo = external_api::clean_returnvalue(core_webservice_external::get_site_info_returns(), $siteinfo); + $siteinfo = \core_webservice_external::get_site_info(); + $siteinfo = \external_api::clean_returnvalue(\core_webservice_external::get_site_info_returns(), $siteinfo); $this->assertEquals($CFG->limitconcurrentlogins, $siteinfo['limitconcurrentlogins']); $this->assertEquals(1, $siteinfo['usersessionscount']); } @@ -198,8 +202,8 @@ public function test_get_site_info_max_int() { $userquota = PHP_INT_MAX . '000'; set_config('userquota', $userquota); - $result = core_webservice_external::get_site_info(); - $result = external_api::clean_returnvalue(core_webservice_external::get_site_info_returns(), $result); + $result = \core_webservice_external::get_site_info(); + $result = \external_api::clean_returnvalue(\core_webservice_external::get_site_info_returns(), $result); $this->assertEquals(PHP_INT_MAX, $result['userquota']); }