���ѧۧݧ�ӧ�� �ާ֧ߧ֧էا֧� - ���֧էѧܧ�ڧ��ӧѧ�� - /home3/cpr76684/public_html/externallib_test.php.tar
���ѧ٧ѧ�
home3/cpr76684/public_html/Aem/webservice/tests/externallib_test.php 0000644 00000021664 15151252363 0021363 0 ustar 00 <?php // This file is part of Moodle - http://moodle.org/ // // Moodle is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // Moodle is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Moodle. If not, see <http://www.gnu.org/licenses/>. namespace core_webservice; use externallib_advanced_testcase; defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->dirroot . '/webservice/externallib.php'); require_once($CFG->dirroot . '/webservice/tests/helpers.php'); /** * External course functions unit tests * * @package core_webservice * @category external * @copyright 2012 Paul Charsley * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class externallib_test extends externallib_advanced_testcase { public function setUp(): void { // Calling parent is good, always parent::setUp(); // We always need enabled WS for this testcase set_config('enablewebservices', '1'); } public function test_get_site_info() { global $DB, $USER, $CFG, $PAGE; $this->resetAfterTest(true); $maxbytes = 10485760; $userquota = 5242880; set_config('maxbytes', $maxbytes); set_config('userquota', $userquota); // Set current user set_config('allowuserthemes', 1); $user = array(); $user['username'] = 'johnd'; $user['firstname'] = 'John'; $user['lastname'] = 'Doe'; $user['theme'] = 'boost'; self::setUser(self::getDataGenerator()->create_user($user)); // Add a web service and token. $webservice = new \stdClass(); $webservice->name = 'Test web service'; $webservice->enabled = true; $webservice->restrictedusers = false; $webservice->component = 'moodle'; $webservice->timecreated = time(); $webservice->downloadfiles = true; $webservice->uploadfiles = true; $externalserviceid = $DB->insert_record('external_services', $webservice); // Add a function to the service $DB->insert_record('external_services_functions', array('externalserviceid' => $externalserviceid, 'functionname' => 'core_course_get_contents')); $_POST['wstoken'] = 'testtoken'; $externaltoken = new \stdClass(); $externaltoken->token = 'testtoken'; $externaltoken->tokentype = 0; $externaltoken->userid = $USER->id; $externaltoken->externalserviceid = $externalserviceid; $externaltoken->contextid = 1; $externaltoken->creatorid = $USER->id; $externaltoken->timecreated = time(); $DB->insert_record('external_tokens', $externaltoken); $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); $this->assertEquals('johnd', $siteinfo['username']); $this->assertEquals('John', $siteinfo['firstname']); $this->assertEquals('Doe', $siteinfo['lastname']); $this->assertEquals(current_language(), $siteinfo['lang']); $this->assertEquals($USER->id, $siteinfo['userid']); $this->assertEquals(SITEID, $siteinfo['siteid']); $this->assertEquals(true, $siteinfo['downloadfiles']); $this->assertEquals($CFG->release, $siteinfo['release']); $this->assertEquals($CFG->version, $siteinfo['version']); $this->assertEquals('', $siteinfo['mobilecssurl']); $this->assertEquals(count($siteinfo['functions']), 1); $function = array_pop($siteinfo['functions']); $this->assertEquals($function['name'], 'core_course_get_contents'); $this->assertEquals($function['version'], $siteinfo['version']); $this->assertEquals(1, $siteinfo['downloadfiles']); $this->assertEquals(1, $siteinfo['uploadfiles']); foreach ($siteinfo['advancedfeatures'] as $feature) { if ($feature['name'] == 'mnet_dispatcher_mode') { if ($CFG->mnet_dispatcher_mode == 'off') { $this->assertEquals(0, $feature['value']); } else { $this->assertEquals(1, $feature['value']); } } else { $this->assertEquals($CFG->{$feature['name']}, $feature['value']); } } $this->assertEquals($userquota, $siteinfo['userquota']); // We can use the function for the expectation because USER_CAN_IGNORE_FILE_SIZE_LIMITS is // covered below for admin user. This test is for user not allowed to ignore limits. $this->assertEquals(get_max_upload_file_size($maxbytes), $siteinfo['usermaxuploadfilesize']); $this->assertEquals(true, $siteinfo['usercanmanageownfiles']); $userkey = get_user_key('core_files', $USER->id); $this->assertEquals($userkey, $siteinfo['userprivateaccesskey']); $this->assertEquals(HOMEPAGE_MY, $siteinfo['userhomepage']); $this->assertEquals($CFG->calendartype, $siteinfo['sitecalendartype']); if (!empty($USER->calendartype)) { $this->assertEquals($USER->calendartype, $siteinfo['usercalendartype']); } else { $this->assertEquals($CFG->calendartype, $siteinfo['usercalendartype']); } $this->assertFalse($siteinfo['userissiteadmin']); $this->assertEquals($CFG->calendartype, $siteinfo['sitecalendartype']); $this->assertEquals($user['theme'], $siteinfo['theme']); // Now as admin. $this->setAdminUser(); // Set a fake token for the user admin. $_POST['wstoken'] = 'testtoken'; $externaltoken = new \stdClass(); $externaltoken->token = 'testtoken'; $externaltoken->tokentype = 0; $externaltoken->userid = $USER->id; $externaltoken->externalserviceid = $externalserviceid; $externaltoken->contextid = 1; $externaltoken->creatorid = $USER->id; $externaltoken->timecreated = time(); $DB->insert_record('external_tokens', $externaltoken); // Set a home page by user preferences. $CFG->defaulthomepage = HOMEPAGE_USER; set_user_preference('user_home_page_preference', HOMEPAGE_SITE); $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); $this->assertEquals(0, $siteinfo['userquota']); $this->assertEquals(USER_CAN_IGNORE_FILE_SIZE_LIMITS, $siteinfo['usermaxuploadfilesize']); $this->assertEquals(true, $siteinfo['usercanmanageownfiles']); $this->assertTrue($siteinfo['userissiteadmin']); $this->assertEmpty($USER->theme); $this->assertEquals($PAGE->theme->name, $siteinfo['theme']); $this->assertEquals($CFG->limitconcurrentlogins, $siteinfo['limitconcurrentlogins']); $this->assertFalse(isset($siteinfo['usersessionscount'])); $CFG->limitconcurrentlogins = 1; $record = new \stdClass(); $record->state = 0; $record->sessdata = null; $record->userid = $USER->id; $record->timemodified = time(); $record->firstip = $record->lastip = '10.0.0.1'; $record->sid = md5('hokus1'); $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); $this->assertEquals($CFG->limitconcurrentlogins, $siteinfo['limitconcurrentlogins']); $this->assertEquals(1, $siteinfo['usersessionscount']); } /** * Test get_site_info with values > PHP_INT_MAX. We check only userquota since maxbytes require PHP ini changes. */ public function test_get_site_info_max_int() { $this->resetAfterTest(true); self::setUser(self::getDataGenerator()->create_user()); // Check values higher than PHP_INT_MAX. This value may come from settings (as string). $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); $this->assertEquals(PHP_INT_MAX, $result['userquota']); } } home3/cpr76684/public_html/Aem/blocks/tests/externallib_test.php 0000644 00000056046 15152003640 0020475 0 ustar 00 <?php // This file is part of Moodle - http://moodle.org/ // // Moodle is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // Moodle is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Moodle. If not, see <http://www.gnu.org/licenses/>. namespace core_block; use core_block_external; use externallib_advanced_testcase; defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->dirroot . '/webservice/tests/helpers.php'); require_once($CFG->dirroot . '/my/lib.php'); /** * External block functions unit tests * * @package core_block * @category external * @copyright 2015 Juan Leyva <juan@moodle.com> * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 3.0 */ class externallib_test extends externallib_advanced_testcase { /** * Test get_course_blocks */ public function test_get_course_blocks() { global $DB, $FULLME; $this->resetAfterTest(true); $user = $this->getDataGenerator()->create_user(); $course = $this->getDataGenerator()->create_course(); $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->set_pagelayout('course'); $course->format = course_get_format($course)->get_format(); $page->set_pagetype('course-view-' . $course->format); $page->blocks->load_blocks(); $newblock = 'calendar_upcoming'; $page->blocks->add_block_at_end_of_default_region($newblock); $this->setUser($user); // 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); // Expect the new block. $this->assertCount(1, $result['blocks']); $this->assertEquals($newblock, $result['blocks'][0]['name']); } /** * Test get_course_blocks on site home */ public function test_get_course_blocks_site_home() { global $DB, $FULLME; $this->resetAfterTest(true); $user = $this->getDataGenerator()->create_user(); $page = new \moodle_page(); $page->set_context(\context_course::instance(SITEID)); $page->set_pagelayout('frontpage'); $page->set_pagetype('site-index'); $page->blocks->load_blocks(); $newblock = 'calendar_upcoming'; $page->blocks->add_block_at_end_of_default_region($newblock); $this->setUser($user); // 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); // Expect the new block. $this->assertCount(1, $result['blocks']); $this->assertEquals($newblock, $result['blocks'][0]['name']); } /** * Test get_course_blocks */ public function test_get_course_blocks_overrides() { global $DB, $CFG, $FULLME; $this->resetAfterTest(true); $CFG->defaultblocks_override = 'search_forums,course_list:calendar_upcoming,recent_activity'; $user = $this->getDataGenerator()->create_user(); $course = $this->getDataGenerator()->create_course(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id); $this->setUser($user); // 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); // Expect 4 default blocks. $this->assertCount(4, $result['blocks']); $expectedblocks = array('navigation', 'settings', 'search_forums', 'course_list', 'calendar_upcoming', 'recent_activity'); foreach ($result['blocks'] as $block) { if (!in_array($block['name'], $expectedblocks)) { $this->fail("Unexpected block found: " . $block['name']); } } } /** * Test get_course_blocks contents */ public function test_get_course_blocks_contents() { global $DB, $FULLME; $this->resetAfterTest(true); $user = $this->getDataGenerator()->create_user(); $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); // Create a HTML block. $title = 'Some course info'; $body = 'Some course info<br /><p>Some contents</p>'; $bodyformat = FORMAT_MOODLE; $page = new \moodle_page(); $page->set_context($coursecontext); $page->set_pagelayout('course'); $course->format = course_get_format($course)->get_format(); $page->set_pagetype('course-view-' . $course->format); $page->blocks->load_blocks(); $newblock = 'html'; $page->blocks->add_block_at_end_of_default_region($newblock); $this->setUser($user); // Re-create the page. $page = new \moodle_page(); $page->set_context($coursecontext); $page->set_pagelayout('course'); $course->format = course_get_format($course)->get_format(); $page->set_pagetype('course-view-' . $course->format); $page->blocks->load_blocks(); $blocks = $page->blocks->get_blocks_for_region($page->blocks->get_default_region()); $block = end($blocks); $block = block_instance('html', $block->instance); $nonscalar = [ 'something' => true, ]; $configdata = (object) [ 'title' => $title, 'text' => [ 'itemid' => 0, 'text' => $body, 'format' => $bodyformat, ], 'nonscalar' => $nonscalar ]; $block->instance_config_save((object) $configdata); $filename = 'img.png'; $filerecord = array( 'contextid' => \context_block::instance($block->instance->id)->id, 'component' => 'block_html', 'filearea' => 'content', 'itemid' => 0, 'filepath' => '/', 'filename' => $filename, ); // Create an area to upload the file. $fs = get_file_storage(); // Create a file from the string that we made earlier. $file = $fs->create_file_from_string($filerecord, 'some fake content (should be an image).'); // 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); // Expect the new block. $this->assertCount(1, $result['blocks']); $this->assertEquals($title, $result['blocks'][0]['contents']['title']); $this->assertEquals($body, $result['blocks'][0]['contents']['content']); $this->assertEquals(FORMAT_HTML, $result['blocks'][0]['contents']['contentformat']); // Format change for external. $this->assertEquals('', $result['blocks'][0]['contents']['footer']); $this->assertCount(1, $result['blocks'][0]['contents']['files']); $this->assertEquals($newblock, $result['blocks'][0]['name']); $configcounts = 0; foreach ($result['blocks'][0]['configs'] as $config) { if ($config['type'] = 'plugin' && $config['name'] == 'allowcssclasses' && $config['value'] == json_encode('0')) { $configcounts++; } else if ($config['type'] = 'instance' && $config['name'] == 'text' && $config['value'] == json_encode($body)) { $configcounts++; } else if ($config['type'] = 'instance' && $config['name'] == 'title' && $config['value'] == json_encode($title)) { $configcounts++; } else if ($config['type'] = 'instance' && $config['name'] == 'format' && $config['value'] == json_encode('0')) { $configcounts++; } else if ($config['type'] = 'instance' && $config['name'] == 'nonscalar' && $config['value'] == json_encode($nonscalar)) { $configcounts++; } } $this->assertEquals(5, $configcounts); } /** * Test get_course_blocks contents with mathjax. */ public function test_get_course_blocks_contents_with_mathjax() { global $DB, $CFG; require_once($CFG->dirroot . '/lib/externallib.php'); $this->resetAfterTest(true); // Enable MathJax filter in content and headings. $this->configure_filters([ ['name' => 'mathjaxloader', 'state' => TEXTFILTER_ON, 'move' => -1, 'applytostrings' => true], ]); // Create a few stuff to test with. $user = $this->getDataGenerator()->create_user(); $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); // 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->set_context($coursecontext); $page->set_pagelayout('course'); $course->format = course_get_format($course)->get_format(); $page->set_pagetype('course-view-' . $course->format); $page->blocks->load_blocks(); $newblock = 'html'; $page->blocks->add_block_at_end_of_default_region($newblock); $this->setUser($user); // Re-create the page. $page = new \moodle_page(); $page->set_context($coursecontext); $page->set_pagelayout('course'); $course->format = course_get_format($course)->get_format(); $page->set_pagetype('course-view-' . $course->format); $page->blocks->load_blocks(); $blocks = $page->blocks->get_blocks_for_region($page->blocks->get_default_region()); $block = end($blocks); $block = block_instance('html', $block->instance); $nonscalar = [ 'something' => true, ]; $configdata = (object) [ 'title' => $title, 'text' => [ 'itemid' => 0, 'text' => $body, 'format' => $bodyformat, ], 'nonscalar' => $nonscalar ]; $block->instance_config_save((object) $configdata); // 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); // Format the original data. $sitecontext = \context_system::instance(); $title = external_format_string($title, $coursecontext->id); list($body, $bodyformat) = external_format_text($body, $bodyformat, $coursecontext->id, 'block_html', 'content'); // Check that the block data is formatted. $this->assertCount(1, $result['blocks']); $this->assertStringContainsString('<span class="filter_mathjaxloader_equation">', $result['blocks'][0]['contents']['title']); $this->assertStringContainsString('<span class="filter_mathjaxloader_equation">', $result['blocks'][0]['contents']['content']); $this->assertEquals($title, $result['blocks'][0]['contents']['title']); $this->assertEquals($body, $result['blocks'][0]['contents']['content']); } /** * Test user get default dashboard blocks. */ public function test_get_dashboard_blocks_default_dashboard() { global $PAGE, $DB; $this->resetAfterTest(true); $user = $this->getDataGenerator()->create_user(); $PAGE->set_url('/my/index.php'); // Need this because some internal API calls require the $PAGE url to be set. // Force a setting change to check the returned blocks settings. set_config('displaycategories', 0, 'block_myoverview'); $systempage = $DB->get_record('my_pages', array('userid' => null, 'name' => MY_PAGE_DEFAULT, 'private' => true)); // Get the expected default blocks. $alldefaultblocksordered = $DB->get_records_menu( 'block_instances', array('pagetypepattern' => 'my-index', 'subpagepattern' => $systempage->id), 'defaultregion, defaultweight ASC', 'id, blockname' ); $this->setUser($user); // 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); // Expect all default blocks defined in blocks_add_default_system_blocks(). $this->assertCount(count($alldefaultblocksordered), $result['blocks']); $returnedblocks = array(); foreach ($result['blocks'] as $block) { // Check all the returned blocks are in the expected blocks array. $this->assertContains($block['name'], $alldefaultblocksordered); $returnedblocks[] = $block['name']; // Check the configuration returned for this default block. if ($block['name'] == 'myoverview') { // Convert config to associative array to avoid DB sorting randomness. $config = array_column($block['configs'], null, 'name'); $this->assertArrayHasKey('displaycategories', $config); $this->assertEquals(json_encode('0'), $config['displaycategories']['value']); $this->assertEquals('plugin', $config['displaycategories']['type']); } } // Check that we received the blocks in the expected order. $this->assertEquals(array_values($alldefaultblocksordered), $returnedblocks); } /** * Test user get default dashboard blocks including a sticky block. */ public function test_get_dashboard_blocks_default_dashboard_including_sticky_block() { global $PAGE, $DB; $this->resetAfterTest(true); $user = $this->getDataGenerator()->create_user(); $PAGE->set_url('/my/index.php'); // Need this because some internal API calls require the $PAGE url to be set. $systempage = $DB->get_record('my_pages', array('userid' => null, 'name' => MY_PAGE_DEFAULT, 'private' => true)); // Get the expected default blocks. $alldefaultblocks = $DB->get_records_menu( 'block_instances', array('pagetypepattern' => 'my-index', 'subpagepattern' => $systempage->id), '', 'id, blockname' ); // Now, add a sticky block. $page = new \moodle_page(); $page->set_context(\context_system::instance()); $page->set_pagetype('my-index'); $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, '*'); $this->setUser($user); // 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); // Expect all default blocks defined in blocks_add_default_system_blocks() plus sticky one. $this->assertCount(count($alldefaultblocks) + 1, $result['blocks']); $found = false; foreach ($result['blocks'] as $block) { if ($block['name'] == 'myprofile') { $this->assertEquals('side-pre', $block['region']); $found = true; continue; } // Check that the block is in the expected blocks array. $this->assertContains($block['name'], $alldefaultblocks); } $this->assertTrue($found); } /** * Test admin get user's custom dashboard blocks. */ public function test_get_dashboard_blocks_custom_user_dashboard() { global $PAGE, $DB; $this->resetAfterTest(true); $user = $this->getDataGenerator()->create_user(); $PAGE->set_url('/my/index.php'); // Need this because some internal API calls require the $PAGE url to be set. $systempage = $DB->get_record('my_pages', array('userid' => null, 'name' => MY_PAGE_DEFAULT, 'private' => true)); // Get the expected default blocks. $alldefaultblocks = $DB->get_records_menu( 'block_instances', array('pagetypepattern' => 'my-index', 'subpagepattern' => $systempage->id), '', 'id, blockname' ); // Add a custom block. $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'); $currentpage = my_get_page($user->id, MY_PAGE_PRIVATE); $page->set_subpage($currentpage->id); $page->blocks->load_blocks(); $page->blocks->add_block('myprofile', 'content', 0, false); $this->setAdminUser(); // 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); // 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; foreach ($result['blocks'] as $block) { if ($block['name'] == 'myprofile') { $this->assertEquals('content', $block['region']); $found = true; continue; } // Check that the block is in the expected blocks array. $this->assertContains($block['name'], $alldefaultblocks); } $this->assertTrue($found); } /** * Test user tries to get other user blocks not having permission. */ public function test_get_dashboard_blocks_other_user_missing_permissions() { $this->resetAfterTest(true); $user1 = $this->getDataGenerator()->create_user(); $user2 = $this->getDataGenerator()->create_user(); $this->setUser($user1); $this->expectException('moodle_exception'); core_block_external::get_dashboard_blocks($user2->id); } /** * Test user get default dashboard blocks for my courses page. */ public function test_get_dashboard_blocks_my_courses() { global $PAGE, $DB; $this->resetAfterTest(true); $user = $this->getDataGenerator()->create_user(); $PAGE->set_url('/my/index.php'); // Need this because some internal API calls require the $PAGE url to be set. // Force a setting change to check the returned blocks settings. set_config('displaycategories', 0, 'block_myoverview'); $systempage = $DB->get_record('my_pages', ['userid' => null, 'name' => MY_PAGE_COURSES, 'private' => false]); // Get the expected default blocks. $alldefaultblocksordered = $DB->get_records_menu( 'block_instances', ['pagetypepattern' => 'my-index', 'subpagepattern' => $systempage->id], 'defaultregion, defaultweight ASC', 'id, blockname' ); $this->setUser($user); // 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); // Expect all default blocks defined in blocks_add_default_system_blocks(). $this->assertCount(count($alldefaultblocksordered), $result['blocks']); $returnedblocks = []; foreach ($result['blocks'] as $block) { // Check all the returned blocks are in the expected blocks array. $this->assertContains($block['name'], $alldefaultblocksordered); $returnedblocks[] = $block['name']; // Check the configuration returned for this default block. if ($block['name'] == 'myoverview') { // Convert config to associative array to avoid DB sorting randomness. $config = array_column($block['configs'], null, 'name'); $this->assertArrayHasKey('displaycategories', $config); $this->assertEquals(json_encode('0'), $config['displaycategories']['value']); $this->assertEquals('plugin', $config['displaycategories']['type']); } } // Check that we received the blocks in the expected order. $this->assertEquals(array_values($alldefaultblocksordered), $returnedblocks); } /** * Test user passing the wrong page type and getting an exception. */ public function test_get_dashboard_blocks_incorrect_page() { global $PAGE; $this->resetAfterTest(true); $user = $this->getDataGenerator()->create_user(); $PAGE->set_url('/my/index.php'); // Need this because some internal API calls require the $PAGE url to be set. $this->setUser($user); $this->expectException('moodle_exception'); // Check for the default blocks with a fake page, no need to assign as it'll throw. core_block_external::get_dashboard_blocks($user->id, false, 'fakepage'); } } home3/cpr76684/public_html/Aem/admin/tool/lp/tests/externallib_test.php 0000644 00000060376 15152100526 0021702 0 ustar 00 <?php // This file is part of Moodle - http://moodle.org/ // // Moodle is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // Moodle is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Moodle. If not, see <http://www.gnu.org/licenses/>. namespace tool_lp; use core_competency\api; use core_competency\invalid_persistent_exception; use core_competency\plan; use core_competency\related_competency; use core_competency\user_competency; use core_competency\user_competency_plan; use core_competency\plan_competency; 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. * * @package tool_lp * @copyright 2015 Damyon Wiese * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class externallib_test extends externallib_advanced_testcase { /** @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. */ protected $catcreator = null; /** @var \stdClass $category Category */ protected $category = null; /** @var \stdClass $category Category */ protected $othercategory = null; /** @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 */ protected $catuser = null; /** @var int Creator role id */ protected $creatorrole = null; /** @var int User role id */ protected $userrole = null; /** * Setup function- we will create a course and add an assign instance to it. */ protected function setUp(): void { global $DB, $CFG; $this->resetAfterTest(true); // Create some users. $creator = $this->getDataGenerator()->create_user(); $user = $this->getDataGenerator()->create_user(); $catuser = $this->getDataGenerator()->create_user(); $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); // Fetching default authenticated user role. $authrole = $DB->get_record('role', array('id' => $CFG->defaultuserroleid)); // Reset all default authenticated users permissions. unassign_capability('moodle/competency:competencygrade', $authrole->id); unassign_capability('moodle/competency:competencymanage', $authrole->id); unassign_capability('moodle/competency:competencyview', $authrole->id); unassign_capability('moodle/competency:planmanage', $authrole->id); unassign_capability('moodle/competency:planmanagedraft', $authrole->id); unassign_capability('moodle/competency:planmanageown', $authrole->id); unassign_capability('moodle/competency:planview', $authrole->id); unassign_capability('moodle/competency:planviewdraft', $authrole->id); unassign_capability('moodle/competency:planviewown', $authrole->id); unassign_capability('moodle/competency:planviewowndraft', $authrole->id); unassign_capability('moodle/competency:templatemanage', $authrole->id); unassign_capability('moodle/competency:templateview', $authrole->id); unassign_capability('moodle/cohort:manage', $authrole->id); unassign_capability('moodle/competency:coursecompetencyconfigure', $authrole->id); // Creating specific roles. $this->creatorrole = create_role('Creator role', 'lpcreatorrole', 'learning plan creator role description'); $this->userrole = create_role('User role', 'lpuserrole', 'learning plan user role description'); assign_capability('moodle/competency:competencymanage', CAP_ALLOW, $this->creatorrole, $syscontext->id); assign_capability('moodle/competency:coursecompetencyconfigure', CAP_ALLOW, $this->creatorrole, $syscontext->id); assign_capability('moodle/competency:planmanage', CAP_ALLOW, $this->creatorrole, $syscontext->id); assign_capability('moodle/competency:planmanagedraft', CAP_ALLOW, $this->creatorrole, $syscontext->id); assign_capability('moodle/competency:planmanageown', CAP_ALLOW, $this->creatorrole, $syscontext->id); assign_capability('moodle/competency:planview', CAP_ALLOW, $this->creatorrole, $syscontext->id); assign_capability('moodle/competency:planviewdraft', CAP_ALLOW, $this->creatorrole, $syscontext->id); assign_capability('moodle/competency:templatemanage', CAP_ALLOW, $this->creatorrole, $syscontext->id); assign_capability('moodle/competency:competencygrade', CAP_ALLOW, $this->creatorrole, $syscontext->id); assign_capability('moodle/cohort:manage', CAP_ALLOW, $this->creatorrole, $syscontext->id); assign_capability('moodle/competency:competencyview', CAP_ALLOW, $this->userrole, $syscontext->id); assign_capability('moodle/competency:templateview', CAP_ALLOW, $this->userrole, $syscontext->id); assign_capability('moodle/competency:planviewown', CAP_ALLOW, $this->userrole, $syscontext->id); assign_capability('moodle/competency:planviewowndraft', CAP_ALLOW, $this->userrole, $syscontext->id); role_assign($this->creatorrole, $creator->id, $syscontext->id); role_assign($this->creatorrole, $catcreator->id, $catcontext->id); role_assign($this->userrole, $user->id, $syscontext->id); role_assign($this->userrole, $catuser->id, $catcontext->id); $this->creator = $creator; $this->catcreator = $catcreator; $this->user = $user; $this->catuser = $catuser; $this->category = $category; $this->othercategory = $othercategory; accesslib_clear_all_caches_for_unit_testing(); } public function test_search_users_by_capability() { global $CFG; $this->resetAfterTest(true); $dg = $this->getDataGenerator(); $ux = $dg->create_user(); $u1 = $dg->create_user(array('idnumber' => 'Cats', 'firstname' => 'Bob', 'lastname' => 'Dyyylan', 'email' => 'bobbyyy@dyyylan.com', 'phone1' => '123456', 'phone2' => '78910', 'department' => 'Marketing', 'institution' => 'HQ')); // 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); $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(); $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); $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); $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); $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); $this->assertCount(1, $result['users']); $this->assertEquals(1, $result['count']); // Now lets try a different user with only the role at system level. $ux2 = $dg->create_user(); 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); $this->assertCount(1, $result['users']); $this->assertEquals(1, $result['count']); // Now lets try a different user with only the role at user level. $ux3 = $dg->create_user(); 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); $this->assertCount(1, $result['users']); $this->assertEquals(1, $result['count']); // Switch back. $this->setUser($ux); // 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); $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); $this->assertCount(1, $result['users']); $this->assertEquals(1, $result['count']); } /** * Ensures that overrides, as well as system permissions, are respected. */ public function test_search_users_by_capability_the_comeback() { $this->resetAfterTest(); $dg = $this->getDataGenerator(); $master = $dg->create_user(); $manager = $dg->create_user(); $slave1 = $dg->create_user(array('lastname' => 'MOODLER')); $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); // Creating a role giving the site config. $roleid = $dg->create_role(); assign_capability('moodle/site:config', CAP_ALLOW, $roleid, $syscontext->id, true); // Create a role override for slave 2. assign_capability('moodle/site:config', CAP_PROHIBIT, $roleid, $slave2context->id, true); // Assigning the role. // Master -> System context. // Manager -> User context. role_assign($roleid, $master->id, $syscontext); role_assign($roleid, $manager->id, $slave1context); // Flush accesslib. accesslib_clear_all_caches_for_unit_testing(); // Confirm. // Master has system permissions. $this->setUser($master); $this->assertTrue(has_capability('moodle/site:config', $syscontext)); $this->assertTrue(has_capability('moodle/site:config', $slave1context)); $this->assertFalse(has_capability('moodle/site:config', $slave2context)); $this->assertTrue(has_capability('moodle/site:config', $slave3context)); // Manager only has permissions in slave 1. $this->setUser($manager); $this->assertFalse(has_capability('moodle/site:config', $syscontext)); $this->assertTrue(has_capability('moodle/site:config', $slave1context)); $this->assertFalse(has_capability('moodle/site:config', $slave2context)); $this->assertFalse(has_capability('moodle/site:config', $slave3context)); // Now do the test. $this->setUser($master); $result = external::search_users('MOODLER', 'moodle/site:config'); $this->assertCount(2, $result['users']); $this->assertEquals(2, $result['count']); $this->assertArrayHasKey($slave1->id, $result['users']); $this->assertArrayHasKey($slave3->id, $result['users']); $this->setUser($manager); $result = external::search_users('MOODLER', 'moodle/site:config'); $this->assertCount(1, $result['users']); $this->assertEquals(1, $result['count']); $this->assertArrayHasKey($slave1->id, $result['users']); } public function test_search_users() { global $CFG; $this->resetAfterTest(true); $dg = $this->getDataGenerator(); $ux = $dg->create_user(); $u1 = $dg->create_user(array('idnumber' => 'Cats', 'firstname' => 'Bob', 'lastname' => 'Dyyylan', 'email' => 'bobbyyy@dyyylan.com', 'phone1' => '123456', 'phone2' => '78910', 'department' => 'Marketing', 'institution' => 'HQ')); $u2 = $dg->create_user(array('idnumber' => 'Dogs', 'firstname' => 'Alice', 'lastname' => 'Dyyylan', 'email' => 'alyyyson@dyyylan.com', 'phone1' => '33333', 'phone2' => '77777', 'department' => 'Development', 'institution' => 'O2')); $u3 = $dg->create_user(array('idnumber' => 'Fish', 'firstname' => 'Thomas', 'lastname' => 'Xow', 'email' => 'fishyyy@moodle.com', 'phone1' => '77777', 'phone2' => '33333', 'department' => 'Research', 'institution' => 'Bob')); // 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); $dummyrole = $this->assignUserCapability('moodle/competency:planmanage', $usercontext->id); $usercontext = \context_user::instance($u2->id); $this->assignUserCapability('moodle/competency:planmanage', $usercontext->id, $dummyrole); $usercontext = \context_user::instance($u3->id); $this->assignUserCapability('moodle/competency:planmanage', $usercontext->id, $dummyrole); $this->setUser($ux); $usercontext = \context_user::instance($u1->id); $this->assignUserCapability('moodle/competency:planmanage', $usercontext->id, $dummyrole); $usercontext = \context_user::instance($u2->id); $this->assignUserCapability('moodle/competency:planmanage', $usercontext->id, $dummyrole); $usercontext = \context_user::instance($u3->id); $this->assignUserCapability('moodle/competency:planmanage', $usercontext->id, $dummyrole); $this->setAdminUser(); // No identity fields. $CFG->showuseridentity = ''; $result = external::search_users('cats', 'moodle/competency:planmanage'); $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); $this->assertCount(2, $result['users']); $this->assertEquals(2, $result['count']); $this->assertEquals($u2->id, $result['users'][0]['id']); $this->assertEquals($u1->id, $result['users'][1]['id']); // 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); $this->assertCount(2, $result['users']); $this->assertEquals(2, $result['count']); $this->assertEquals($u1->id, $result['users'][0]['id']); $this->assertEquals($u3->id, $result['users'][1]['id']); // 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); $this->assertCount(1, $result['users']); $this->assertEquals(1, $result['count']); $this->assertEquals($u1->id, $result['users'][0]['id']); $this->assertEquals($u1->idnumber, $result['users'][0]['idnumber']); $this->assertEmpty($result['users'][0]['email']); $this->assertEmpty($result['users'][0]['phone1']); $this->assertEmpty($result['users'][0]['phone2']); $this->assertEmpty($result['users'][0]['department']); $this->assertEmpty($result['users'][0]['institution']); // Filter by email. $CFG->showuseridentity = 'email'; $result = external::search_users('yyy', 'moodle/competency:planmanage'); $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']); $this->assertEquals($u2->email, $result['users'][0]['email']); $this->assertEquals($u1->id, $result['users'][1]['id']); $this->assertEquals($u1->email, $result['users'][1]['email']); $this->assertEquals($u3->id, $result['users'][2]['id']); $this->assertEquals($u3->email, $result['users'][2]['email']); // 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); $this->assertCount(3, $result['users']); $this->assertEquals(3, $result['count']); $this->assertArrayHasKey('idnumber', $result['users'][0]); $this->assertArrayHasKey('email', $result['users'][0]); $this->assertArrayHasKey('phone1', $result['users'][0]); $this->assertArrayHasKey('phone2', $result['users'][0]); $this->assertArrayHasKey('department', $result['users'][0]); $this->assertArrayHasKey('institution', $result['users'][0]); // Switch to a user that cannot view identity fields. $this->setUser($ux); $CFG->showuseridentity = 'idnumber,email,phone1,phone2,department,institution'; // Only names are included. $result = external::search_users('fish'); $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); $this->assertCount(1, $result['users']); $this->assertEquals(1, $result['count']); $this->assertEquals($u1->id, $result['users'][0]['id']); $this->assertEmpty($result['users'][0]['idnumber']); $this->assertEmpty($result['users'][0]['email']); $this->assertEmpty($result['users'][0]['phone1']); $this->assertEmpty($result['users'][0]['phone2']); $this->assertEmpty($result['users'][0]['department']); $this->assertEmpty($result['users'][0]['institution']); } public function test_data_for_user_competency_summary_in_plan() { global $CFG; $this->setUser($this->creator); $dg = $this->getDataGenerator(); $lpg = $dg->get_plugin_generator('core_competency'); $f1 = $lpg->create_framework(); $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id'))); $tpl = $lpg->create_template(); $lpg->create_template_competency(array('templateid' => $tpl->get('id'), 'competencyid' => $c1->get('id'))); $plan = $lpg->create_plan(array('userid' => $this->user->id, 'templateid' => $tpl->get('id'), 'name' => 'Evil')); $uc = $lpg->create_user_competency(array('userid' => $this->user->id, 'competencyid' => $c1->get('id'))); $evidence = \core_competency\external::grade_competency_in_plan($plan->get('id'), $c1->get('id'), 1, true); $evidence = \core_competency\external::grade_competency_in_plan($plan->get('id'), $c1->get('id'), 2, true); $summary = external::data_for_user_competency_summary_in_plan($c1->get('id'), $plan->get('id')); $this->assertTrue($summary->usercompetencysummary->cangrade); $this->assertEquals('Evil', $summary->plan->name); $this->assertEquals('B', $summary->usercompetencysummary->usercompetency->gradename); $this->assertEquals('B', $summary->usercompetencysummary->evidence[0]->gradename); $this->assertEquals('A', $summary->usercompetencysummary->evidence[1]->gradename); } public function test_data_for_user_competency_summary() { $this->setUser($this->creator); $dg = $this->getDataGenerator(); $lpg = $dg->get_plugin_generator('core_competency'); $f1 = $lpg->create_framework(); $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id'))); $evidence = \core_competency\external::grade_competency($this->user->id, $c1->get('id'), 1, true); $evidence = \core_competency\external::grade_competency($this->user->id, $c1->get('id'), 2, true); $summary = external::data_for_user_competency_summary($this->user->id, $c1->get('id')); $this->assertTrue($summary->cangrade); $this->assertEquals('B', $summary->usercompetency->gradename); $this->assertEquals('B', $summary->evidence[0]->gradename); $this->assertEquals('A', $summary->evidence[1]->gradename); } public function test_data_for_course_competency_page() { $this->setAdminUser(); $dg = $this->getDataGenerator(); $lpg = $dg->get_plugin_generator('core_competency'); $f1 = $lpg->create_framework(); $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id'))); $course1 = $dg->create_course(array('category' => $this->category->id)); $cc = api::add_competency_to_course($course1->id, $c1->get('id')); $evidence = \core_competency\external::grade_competency($this->user->id, $c1->get('id'), 1, true); $evidence = \core_competency\external::grade_competency($this->user->id, $c1->get('id'), 2, true); $pagegenerator = $this->getDataGenerator()->get_plugin_generator('mod_page'); $page = $pagegenerator->create_instance(array('course' => $course1->id)); $page2 = $pagegenerator->create_instance(array('course' => $course1->id)); $cm = get_coursemodule_from_instance('page', $page->id); $cm2 = get_coursemodule_from_instance('page', $page2->id); // Add the competency to the course module. $ccm = api::add_competency_to_course_module($cm, $c1->get('id')); $summary = external::data_for_course_competencies_page($course1->id, 0); $summary2 = external::data_for_course_competencies_page($course1->id, $cm->id); $summary3 = external::data_for_course_competencies_page($course1->id, $cm2->id); $this->assertEquals(count($summary->competencies), 1); $this->assertEquals(count($summary->competencies), count($summary2->competencies)); $this->assertEquals(count($summary3->competencies), 0); } } home3/cpr76684/public_html/Aem/user/tests/externallib_test.php 0000644 00000226502 15152101222 0020165 0 ustar 00 <?php // This file is part of Moodle - http://moodle.org/ // // Moodle is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // Moodle is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Moodle. If not, see <http://www.gnu.org/licenses/>. /** * User external PHPunit tests * * @package core_user * @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_user; use core_files_external; use core_user_external; use externallib_advanced_testcase; defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->dirroot . '/webservice/tests/helpers.php'); require_once($CFG->dirroot . '/user/externallib.php'); require_once($CFG->dirroot . '/files/externallib.php'); class externallib_test extends externallib_advanced_testcase { /** * Test get_users */ public function test_get_users() { global $USER, $CFG; $this->resetAfterTest(true); $course = self::getDataGenerator()->create_course(); $user1 = array( 'username' => 'usernametest1', 'idnumber' => 'idnumbertest1', 'firstname' => 'First Name User Test 1', 'lastname' => 'Last Name User Test 1', 'email' => 'usertest1@example.com', 'address' => '2 Test Street Perth 6000 WA', 'phone1' => '01010101010', 'phone2' => '02020203', 'department' => 'Department of user 1', 'institution' => 'Institution of user 1', 'description' => 'This is a description for user 1', 'descriptionformat' => FORMAT_MOODLE, 'city' => 'Perth', 'country' => 'AU' ); $user1 = self::getDataGenerator()->create_user($user1); set_config('usetags', 1); require_once($CFG->dirroot . '/user/editlib.php'); $user1->interests = array('Cinema', 'Tennis', 'Dance', 'Guitar', 'Cooking'); useredit_update_interests($user1, $user1->interests); $user2 = self::getDataGenerator()->create_user( array('username' => 'usernametest2', 'idnumber' => 'idnumbertest2')); $generatedusers = array(); $generatedusers[$user1->id] = $user1; $generatedusers[$user2->id] = $user2; $context = \context_course::instance($course->id); $roleid = $this->assignUserCapability('moodle/user:viewdetails', $context->id); // Enrol the users in the course. $this->getDataGenerator()->enrol_user($user1->id, $course->id, $roleid); $this->getDataGenerator()->enrol_user($user2->id, $course->id, $roleid); $this->getDataGenerator()->enrol_user($USER->id, $course->id, $roleid); // call as admin and receive all possible fields. $this->setAdminUser(); $searchparams = array( array('key' => 'invalidkey', 'value' => 'invalidkey'), array('key' => 'email', 'value' => $user1->email), array('key' => 'firstname', 'value' => $user1->firstname)); // Call the external function. $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); // Check we retrieve the good total number of enrolled users + no error on capability. $expectedreturnedusers = 1; $returnedusers = $result['users']; $this->assertEquals($expectedreturnedusers, count($returnedusers)); foreach($returnedusers as $returneduser) { $generateduser = ($returneduser['id'] == $USER->id) ? $USER : $generatedusers[$returneduser['id']]; $this->assertEquals($generateduser->username, $returneduser['username']); if (!empty($generateduser->idnumber)) { $this->assertEquals($generateduser->idnumber, $returneduser['idnumber']); } $this->assertEquals($generateduser->firstname, $returneduser['firstname']); $this->assertEquals($generateduser->lastname, $returneduser['lastname']); if ($generateduser->email != $USER->email) { // Don't check the tmp modified $USER email. $this->assertEquals($generateduser->email, $returneduser['email']); } if (!empty($generateduser->address)) { $this->assertEquals($generateduser->address, $returneduser['address']); } if (!empty($generateduser->phone1)) { $this->assertEquals($generateduser->phone1, $returneduser['phone1']); } if (!empty($generateduser->phone2)) { $this->assertEquals($generateduser->phone2, $returneduser['phone2']); } if (!empty($generateduser->department)) { $this->assertEquals($generateduser->department, $returneduser['department']); } if (!empty($generateduser->institution)) { $this->assertEquals($generateduser->institution, $returneduser['institution']); } if (!empty($generateduser->description)) { $this->assertEquals($generateduser->description, $returneduser['description']); } if (!empty($generateduser->descriptionformat)) { $this->assertEquals(FORMAT_HTML, $returneduser['descriptionformat']); } if (!empty($generateduser->city)) { $this->assertEquals($generateduser->city, $returneduser['city']); } if (!empty($generateduser->country)) { $this->assertEquals($generateduser->country, $returneduser['country']); } if (!empty($CFG->usetags) and !empty($generateduser->interests)) { $this->assertEquals(implode(', ', $generateduser->interests), $returneduser['interests']); } } // Test the invalid key warning. $warnings = $result['warnings']; $this->assertEquals(count($warnings), 1); $warning = array_pop($warnings); $this->assertEquals($warning['item'], 'invalidkey'); $this->assertEquals($warning['warningcode'], 'invalidfieldparameter'); // Test sending twice the same search field. try { $searchparams = array( array('key' => 'firstname', 'value' => 'Canard'), array('key' => 'email', 'value' => $user1->email), array('key' => 'firstname', 'value' => $user1->firstname)); // Call the external function. $result = core_user_external::get_users($searchparams); $this->fail('Expecting \'keyalreadyset\' moodle_exception to be thrown.'); } catch (\moodle_exception $e) { $this->assertEquals('keyalreadyset', $e->errorcode); } catch (\Exception $e) { $this->fail('Expecting \'keyalreadyset\' moodle_exception to be thrown.'); } } /** * Test get_users_by_field */ public function test_get_users_by_field() { global $USER, $CFG; $this->resetAfterTest(true); $course = self::getDataGenerator()->create_course(); $user1 = array( 'username' => 'usernametest1', 'idnumber' => 'idnumbertest1', 'firstname' => 'First Name User Test 1', 'lastname' => 'Last Name User Test 1', 'email' => 'usertest1@example.com', 'address' => '2 Test Street Perth 6000 WA', 'phone1' => '01010101010', 'phone2' => '02020203', 'department' => 'Department of user 1', 'institution' => 'Institution of user 1', 'description' => 'This is a description for user 1', 'descriptionformat' => FORMAT_MOODLE, 'city' => 'Perth', 'country' => 'AU', ); $user1 = self::getDataGenerator()->create_user($user1); if (!empty($CFG->usetags)) { require_once($CFG->dirroot . '/user/editlib.php'); $user1->interests = array('Cinema', 'Tennis', 'Dance', 'Guitar', 'Cooking'); useredit_update_interests($user1, $user1->interests); } $user2 = self::getDataGenerator()->create_user( array('username' => 'usernametest2', 'idnumber' => 'idnumbertest2')); $generatedusers = array(); $generatedusers[$user1->id] = $user1; $generatedusers[$user2->id] = $user2; $context = \context_course::instance($course->id); $roleid = $this->assignUserCapability('moodle/user:viewdetails', $context->id); // Enrol the users in the course. $this->getDataGenerator()->enrol_user($user1->id, $course->id, $roleid, 'manual'); $this->getDataGenerator()->enrol_user($user2->id, $course->id, $roleid, 'manual'); $this->getDataGenerator()->enrol_user($USER->id, $course->id, $roleid, 'manual'); // call as admin and receive all possible fields. $this->setAdminUser(); $fieldstosearch = array('id', 'idnumber', 'username', 'email'); foreach ($fieldstosearch as $fieldtosearch) { // 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); // Expected result differ following the searched field // Admin user in the PHPunit framework doesn't have an idnumber. if ($fieldtosearch == 'idnumber') { $expectedreturnedusers = 2; } else { $expectedreturnedusers = 3; } // Check we retrieve the good total number of enrolled users + no error on capability. $this->assertEquals($expectedreturnedusers, count($returnedusers)); foreach($returnedusers as $returneduser) { $generateduser = ($returneduser['id'] == $USER->id) ? $USER : $generatedusers[$returneduser['id']]; $this->assertEquals($generateduser->username, $returneduser['username']); if (!empty($generateduser->idnumber)) { $this->assertEquals($generateduser->idnumber, $returneduser['idnumber']); } $this->assertEquals($generateduser->firstname, $returneduser['firstname']); $this->assertEquals($generateduser->lastname, $returneduser['lastname']); if ($generateduser->email != $USER->email) { //don't check the tmp modified $USER email $this->assertEquals($generateduser->email, $returneduser['email']); } if (!empty($generateduser->address)) { $this->assertEquals($generateduser->address, $returneduser['address']); } if (!empty($generateduser->phone1)) { $this->assertEquals($generateduser->phone1, $returneduser['phone1']); } if (!empty($generateduser->phone2)) { $this->assertEquals($generateduser->phone2, $returneduser['phone2']); } if (!empty($generateduser->department)) { $this->assertEquals($generateduser->department, $returneduser['department']); } if (!empty($generateduser->institution)) { $this->assertEquals($generateduser->institution, $returneduser['institution']); } if (!empty($generateduser->description)) { $this->assertEquals($generateduser->description, $returneduser['description']); } if (!empty($generateduser->descriptionformat) and isset($returneduser['descriptionformat'])) { $this->assertEquals($generateduser->descriptionformat, $returneduser['descriptionformat']); } if (!empty($generateduser->city)) { $this->assertEquals($generateduser->city, $returneduser['city']); } if (!empty($generateduser->country)) { $this->assertEquals($generateduser->country, $returneduser['country']); } if (!empty($CFG->usetags) and !empty($generateduser->interests)) { $this->assertEquals(implode(', ', $generateduser->interests), $returneduser['interests']); } // Default language and no theme were used for the user. $this->assertEquals($CFG->lang, $returneduser['lang']); $this->assertEmpty($returneduser['theme']); } } // Test that no result are returned for search by username if we are not admin $this->setGuestUser(); // 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); // Only the own $USER username should be returned $this->assertEquals(1, count($returnedusers)); // And finally test as one of the enrolled users. $this->setUser($user1); // 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); // Only the own $USER username should be returned still. $this->assertEquals(1, count($returnedusers)); } public function get_course_user_profiles_setup($capability) { global $USER, $CFG; $this->resetAfterTest(true); $return = new \stdClass(); // Create the course and fetch its context. $return->course = self::getDataGenerator()->create_course(); $return->user1 = array( 'username' => 'usernametest1', 'idnumber' => 'idnumbertest1', 'firstname' => 'First Name User Test 1', 'lastname' => 'Last Name User Test 1', 'email' => 'usertest1@example.com', 'address' => '2 Test Street Perth 6000 WA', 'phone1' => '01010101010', 'phone2' => '02020203', 'department' => 'Department of user 1', 'institution' => 'Institution of user 1', 'description' => 'This is a description for user 1', 'descriptionformat' => FORMAT_MOODLE, 'city' => 'Perth', 'country' => 'AU' ); $return->user1 = self::getDataGenerator()->create_user($return->user1); if (!empty($CFG->usetags)) { require_once($CFG->dirroot . '/user/editlib.php'); $return->user1->interests = array('Cinema', 'Tennis', 'Dance', 'Guitar', 'Cooking'); useredit_update_interests($return->user1, $return->user1->interests); } $return->user2 = self::getDataGenerator()->create_user(); $context = \context_course::instance($return->course->id); $return->roleid = $this->assignUserCapability($capability, $context->id); // Enrol the users in the course. $this->getDataGenerator()->enrol_user($return->user1->id, $return->course->id, $return->roleid, 'manual'); $this->getDataGenerator()->enrol_user($return->user2->id, $return->course->id, $return->roleid, 'manual'); $this->getDataGenerator()->enrol_user($USER->id, $return->course->id, $return->roleid, 'manual'); $group1 = $this->getDataGenerator()->create_group(['courseid' => $return->course->id, 'name' => 'G1']); $group2 = $this->getDataGenerator()->create_group(['courseid' => $return->course->id, 'name' => 'G2']); groups_add_member($group1->id, $return->user1->id); groups_add_member($group2->id, $return->user2->id); return $return; } /** * Test get_course_user_profiles */ public function test_get_course_user_profiles() { global $USER, $CFG; $this->resetAfterTest(true); $data = $this->get_course_user_profiles_setup('moodle/user:viewdetails'); // Call the external function. $enrolledusers = core_user_external::get_course_user_profiles(array( 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); // Check we retrieve the good total number of enrolled users + no error on capability. $this->assertEquals(1, count($enrolledusers)); } public function test_get_user_course_profile_as_admin() { global $USER, $CFG; global $USER, $CFG; $this->resetAfterTest(true); $data = $this->get_course_user_profiles_setup('moodle/user:viewdetails'); // Do the same call as admin to receive all possible fields. $this->setAdminUser(); $USER->email = "admin@example.com"; // Call the external function. $enrolledusers = core_user_external::get_course_user_profiles(array( 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); // Check we get the requested user and that is in a group. $this->assertCount(1, $enrolledusers); $this->assertCount(1, $enrolledusers[0]['groups']); foreach($enrolledusers as $enrolleduser) { if ($enrolleduser['username'] == $data->user1->username) { $this->assertEquals($data->user1->idnumber, $enrolleduser['idnumber']); $this->assertEquals($data->user1->firstname, $enrolleduser['firstname']); $this->assertEquals($data->user1->lastname, $enrolleduser['lastname']); $this->assertEquals($data->user1->email, $enrolleduser['email']); $this->assertEquals($data->user1->address, $enrolleduser['address']); $this->assertEquals($data->user1->phone1, $enrolleduser['phone1']); $this->assertEquals($data->user1->phone2, $enrolleduser['phone2']); $this->assertEquals($data->user1->department, $enrolleduser['department']); $this->assertEquals($data->user1->institution, $enrolleduser['institution']); $this->assertEquals($data->user1->description, $enrolleduser['description']); $this->assertEquals(FORMAT_HTML, $enrolleduser['descriptionformat']); $this->assertEquals($data->user1->city, $enrolleduser['city']); $this->assertEquals($data->user1->country, $enrolleduser['country']); if (!empty($CFG->usetags)) { $this->assertEquals(implode(', ', $data->user1->interests), $enrolleduser['interests']); } } } } /** * Test create_users */ public function test_create_users() { global $DB; $this->resetAfterTest(true); $user1 = array( 'username' => 'usernametest1', 'password' => 'Moodle2012!', 'idnumber' => 'idnumbertest1', 'firstname' => 'First Name User Test 1', 'lastname' => 'Last Name User Test 1', 'middlename' => 'Middle Name User Test 1', 'lastnamephonetic' => '最後のお名前のテスト一号', 'firstnamephonetic' => 'お名前のテスト一号', 'alternatename' => 'Alternate Name User Test 1', 'email' => 'usertest1@example.com', 'description' => 'This is a description for user 1', 'city' => 'Perth', 'country' => 'AU', 'preferences' => [[ 'type' => 'htmleditor', 'value' => 'atto' ], [ 'type' => 'invalidpreference', 'value' => 'abcd' ] ], 'department' => 'College of Science', 'institution' => 'National Institute of Physics', 'phone1' => '01 2345 6789', 'maildisplay' => 1, 'interests' => 'badminton, basketball, cooking, ' ); // User with an authentication method done externally. $user2 = array( 'username' => 'usernametest2', 'firstname' => 'First Name User Test 2', 'lastname' => 'Last Name User Test 2', 'email' => 'usertest2@example.com', 'auth' => 'oauth2' ); $context = \context_system::instance(); $roleid = $this->assignUserCapability('moodle/user:create', $context->id); $this->assignUserCapability('moodle/user:editprofile', $context->id, $roleid); // Call the external function. $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); // Check we retrieve the good total number of created users + no error on capability. $this->assertCount(2, $createdusers); foreach($createdusers as $createduser) { $dbuser = $DB->get_record('user', array('id' => $createduser['id'])); if ($createduser['username'] === $user1['username']) { $usertotest = $user1; $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); // There should be 3 user interests. $this->assertCount(3, $interests); } else if ($createduser['username'] === $user2['username']) { $usertotest = $user2; } foreach ($dbuser as $property => $value) { if ($property === 'password') { if ($usertotest === $user2) { // External auth mechanisms don't store password in the user table. $this->assertEquals(AUTH_PASSWORD_NOT_CACHED, $value); } else { // Skip hashed passwords. continue; } } // Confirm that the values match. if (isset($usertotest[$property])) { $this->assertEquals($usertotest[$property], $value); } } } // Call without required capability $this->unassignUserCapability('moodle/user:create', $context->id, $roleid); $this->expectException('required_capability_exception'); core_user_external::create_users(array($user1)); } /** * Test create_users with password and createpassword parameter not set. */ public function test_create_users_empty_password() { $this->resetAfterTest(); $this->setAdminUser(); $user = [ 'username' => 'usernametest1', 'firstname' => 'First Name User Test 1', 'lastname' => 'Last Name User Test 1', 'email' => 'usertest1@example.com', ]; // This should throw an exception because either password or createpassword param must be passed for auth_manual. $this->expectException(\invalid_parameter_exception::class); core_user_external::create_users([$user]); } /** * Data provider for \core_user_externallib_testcase::test_create_users_with_same_emails(). */ public function create_users_provider_with_same_emails() { return [ 'Same emails allowed, same case' => [ 1, false ], 'Same emails allowed, different case' => [ 1, true ], 'Same emails disallowed, same case' => [ 0, false ], 'Same emails disallowed, different case' => [ 0, true ], ]; } /** * Test for \core_user_external::create_users() when user using the same email addresses are being created. * * @dataProvider create_users_provider_with_same_emails * @param int $sameemailallowed The value to set for $CFG->allowaccountssameemail. * @param boolean $differentcase Whether to user a different case for the other user. */ public function test_create_users_with_same_emails($sameemailallowed, $differentcase) { global $DB; $this->resetAfterTest(); $this->setAdminUser(); // Allow multiple users with the same email address. set_config('allowaccountssameemail', $sameemailallowed); $users = [ [ 'username' => 's1', 'firstname' => 'Johnny', 'lastname' => 'Bravo', 'email' => 's1@example.com', 'password' => 'Passw0rd!' ], [ 'username' => 's2', 'firstname' => 'John', 'lastname' => 'Doe', 'email' => $differentcase ? 'S1@EXAMPLE.COM' : 's1@example.com', 'password' => 'Passw0rd!' ], ]; if (!$sameemailallowed) { // This should throw an exception when $CFG->allowaccountssameemail is empty. $this->expectException(\invalid_parameter_exception::class); } // Create our users. core_user_external::create_users($users); // Confirm that the users have been created. list($insql, $params) = $DB->get_in_or_equal(['s1', 's2']); $this->assertEquals(2, $DB->count_records_select('user', 'username ' . $insql, $params)); } /** * Test create_users with invalid parameters * * @dataProvider data_create_users_invalid_parameter * @param array $data User data to attempt to register. * @param string $expectmessage Expected exception message. */ public function test_create_users_invalid_parameter(array $data, $expectmessage) { global $USER, $CFG, $DB; $this->resetAfterTest(true); $this->assignUserCapability('moodle/user:create', SYSCONTEXTID); $this->expectException('invalid_parameter_exception'); $this->expectExceptionMessage($expectmessage); core_user_external::create_users(array($data)); } /** * Data provider for {@see self::test_create_users_invalid_parameter()}. * * @return array */ public function data_create_users_invalid_parameter() { return [ 'blank_username' => [ 'data' => [ 'username' => '', 'firstname' => 'Foo', 'lastname' => 'Bar', 'email' => 'foobar@example.com', 'createpassword' => 1, ], 'expectmessage' => 'The field username cannot be blank', ], 'blank_firtname' => [ 'data' => [ 'username' => 'foobar', 'firstname' => "\t \n", 'lastname' => 'Bar', 'email' => 'foobar@example.com', 'createpassword' => 1, ], 'expectmessage' => 'The field firstname cannot be blank', ], 'blank_lastname' => [ 'data' => [ 'username' => 'foobar', 'firstname' => '0', 'lastname' => ' ', 'email' => 'foobar@example.com', 'createpassword' => 1, ], 'expectmessage' => 'The field lastname cannot be blank', ], 'invalid_email' => [ 'data' => [ 'username' => 'foobar', 'firstname' => 'Foo', 'lastname' => 'Bar', 'email' => '@foobar', 'createpassword' => 1, ], 'expectmessage' => 'Email address is invalid', ], 'missing_password' => [ 'data' => [ 'username' => 'foobar', 'firstname' => 'Foo', 'lastname' => 'Bar', 'email' => 'foobar@example.com', ], 'expectmessage' => 'Invalid password: you must provide a password, or set createpassword', ], ]; } /** * Test delete_users */ public function test_delete_users() { global $USER, $CFG, $DB; $this->resetAfterTest(true); $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); // Check the users were correctly created. $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(); $roleid = $this->assignUserCapability('moodle/user:delete', $context->id); // Call the external function. core_user_external::delete_users(array($user1->id, $user2->id)); // Check we retrieve no users + no error on capability. $this->assertEquals(0, $DB->count_records_select('user', 'deleted = 0 AND (id = :userid1 OR id = :userid2)', array('userid1' => $user1->id, 'userid2' => $user2->id))); // Call without required capability. $this->unassignUserCapability('moodle/user:delete', $context->id, $roleid); $this->expectException('required_capability_exception'); core_user_external::delete_users(array($user1->id, $user2->id)); } /** * Test update_users */ public function test_update_users() { global $USER, $CFG, $DB; $this->resetAfterTest(true); $this->preventResetByRollback(); $wsuser = self::getDataGenerator()->create_user(); self::setUser($wsuser); $context = \context_user::instance($USER->id); $contextid = $context->id; $filename = "reddot.png"; $filecontent = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38" . "GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="; // 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); $draftid = $draftfile['itemid']; $user1 = self::getDataGenerator()->create_user(); $user1 = array( 'id' => $user1->id, 'username' => 'usernametest1', 'password' => 'Moodle2012!', 'idnumber' => 'idnumbertest1', 'firstname' => 'First Name User Test 1', 'lastname' => 'Last Name User Test 1', 'middlename' => 'Middle Name User Test 1', 'lastnamephonetic' => '最後のお名前のテスト一号', 'firstnamephonetic' => 'お名前のテスト一号', 'alternatename' => 'Alternate Name User Test 1', 'email' => 'usertest1@example.com', 'description' => 'This is a description for user 1', 'city' => 'Perth', 'userpicture' => $draftid, 'country' => 'AU', 'preferences' => [[ 'type' => 'htmleditor', 'value' => 'atto' ], [ 'type' => 'invialidpreference', 'value' => 'abcd' ] ], 'department' => 'College of Science', 'institution' => 'National Institute of Physics', 'phone1' => '01 2345 6789', 'maildisplay' => 1, 'interests' => 'badminton, basketball, cooking, ' ); $context = \context_system::instance(); $roleid = $this->assignUserCapability('moodle/user:update', $context->id); $this->assignUserCapability('moodle/user:editprofile', $context->id, $roleid); // Check we can't update deleted users, guest users, site admin. $user2 = $user3 = $user4 = $user1; $user2['id'] = $CFG->siteguest; $siteadmins = explode(',', $CFG->siteadmins); $user3['id'] = array_shift($siteadmins); $userdeleted = self::getDataGenerator()->create_user(); $user4['id'] = $userdeleted->id; user_delete_user($userdeleted); $user5 = self::getDataGenerator()->create_user(); $user5 = array('id' => $user5->id, 'email' => $user5->email); // 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); // Check warnings. $this->assertEquals($user2['id'], $returnvalue['warnings'][0]['itemid']); // Guest user. $this->assertEquals('usernotupdatedguest', $returnvalue['warnings'][0]['warningcode']); $this->assertEquals($user3['id'], $returnvalue['warnings'][1]['itemid']); // Admin user. $this->assertEquals('usernotupdatedadmin', $returnvalue['warnings'][1]['warningcode']); $this->assertEquals($user4['id'], $returnvalue['warnings'][2]['itemid']); // Deleted user. $this->assertEquals('usernotupdateddeleted', $returnvalue['warnings'][2]['warningcode']); $dbuser2 = $DB->get_record('user', array('id' => $user2['id'])); $this->assertNotEquals($dbuser2->username, $user2['username']); $dbuser3 = $DB->get_record('user', array('id' => $user3['id'])); $this->assertNotEquals($dbuser3->username, $user3['username']); $dbuser4 = $DB->get_record('user', array('id' => $user4['id'])); $this->assertNotEquals($dbuser4->username, $user4['username']); $dbuser = $DB->get_record('user', array('id' => $user1['id'])); $this->assertEquals($dbuser->username, $user1['username']); $this->assertEquals($dbuser->idnumber, $user1['idnumber']); $this->assertEquals($dbuser->firstname, $user1['firstname']); $this->assertEquals($dbuser->lastname, $user1['lastname']); $this->assertEquals($dbuser->email, $user1['email']); $this->assertEquals($dbuser->description, $user1['description']); $this->assertEquals($dbuser->city, $user1['city']); $this->assertEquals($dbuser->country, $user1['country']); $this->assertNotEquals(0, $dbuser->picture, 'Picture must be set to the new icon itemid for this user'); $this->assertEquals($dbuser->department, $user1['department']); $this->assertEquals($dbuser->institution, $user1['institution']); $this->assertEquals($dbuser->phone1, $user1['phone1']); $this->assertEquals($dbuser->maildisplay, $user1['maildisplay']); $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', $user1['id'], \core_tag_tag::BOTH_STANDARD_AND_NOT, 0, false); // There should be 3 user interests. $this->assertCount(3, $interests); // Confirm no picture change when parameter is not supplied. unset($user1['userpicture']); core_user_external::update_users(array($user1)); $dbusernopic = $DB->get_record('user', array('id' => $user1['id'])); $this->assertEquals($dbuser->picture, $dbusernopic->picture, 'Picture not change without the parameter.'); // Confirm delete of picture deletes the picture from the user record. $user1['userpicture'] = 0; core_user_external::update_users(array($user1)); $dbuserdelpic = $DB->get_record('user', array('id' => $user1['id'])); $this->assertEquals(0, $dbuserdelpic->picture, 'Picture must be deleted when sent as 0.'); // 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); $this->assertEquals('useremailinvalid', $returnvalue['warnings'][0]['warningcode']); $this->assertStringContainsString('Invalid email address', $returnvalue['warnings'][0]['message']); // 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); $this->assertEquals('useremailduplicate', $returnvalue['warnings'][0]['warningcode']); $this->assertStringContainsString('Duplicate email address', $returnvalue['warnings'][0]['message']); // 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); $this->assertEquals('invaliduserid', $returnvalue['warnings'][0]['warningcode']); $this->assertStringContainsString('Invalid user ID', $returnvalue['warnings'][0]['message']); // Updating a remote user. $user1['mnethostid'] = 5; 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); $this->assertEquals('usernotupdatedremote', $returnvalue['warnings'][0]['warningcode']); $this->assertStringContainsString('User is a remote user', $returnvalue['warnings'][0]['message']); // Call without required capability. $this->unassignUserCapability('moodle/user:update', $context->id, $roleid); $this->expectException('required_capability_exception'); core_user_external::update_users(array($user1)); } /** * Data provider for testing \core_user_external::update_users() for users with same emails * * @return array */ public function users_with_same_emails() { return [ 'Same emails not allowed: Update name using exactly the same email' => [ 0, 'John', 's1@example.com', 'Johnny', 's1@example.com', false, true ], 'Same emails not allowed: Update using someone else\'s email' => [ 0, 'John', 's1@example.com', 'Johnny', 's2@example.com', true, false ], 'Same emails allowed: Update using someone else\'s email' => [ 1, 'John', 's1@example.com', 'Johnny', 's2@example.com', true, true ], 'Same emails not allowed: Update using same email but with different case' => [ 0, 'John', 's1@example.com', 'Johnny', 'S1@EXAMPLE.COM', false, true ], 'Same emails not allowed: Update using another user\'s email similar to user but with different case' => [ 0, 'John', 's1@example.com', 'Johnny', 'S1@EXAMPLE.COM', true, false ], 'Same emails allowed: Update using another user\'s email similar to user but with different case' => [ 1, 'John', 's1@example.com', 'Johnny', 'S1@EXAMPLE.COM', true, true ], ]; } /** * Test update_users using similar emails with varying cases. * * @dataProvider users_with_same_emails * @param boolean $allowsameemail The value to set for $CFG->allowaccountssameemail. * @param string $currentname The user's current name. * @param string $currentemail The user's current email. * @param string $newname The user's new name. * @param string $newemail The user's new email. * @param boolean $withanotheruser Whether to create another user that has the same email as the target user's new email. * @param boolean $successexpected Whether we expect that the target user's email/name will be updated. */ public function test_update_users_emails_with_different_cases($allowsameemail, $currentname, $currentemail, $newname, $newemail, $withanotheruser, $successexpected) { global $DB; $this->resetAfterTest(); $this->setAdminUser(); // Set the value for $CFG->allowaccountssameemail. set_config('allowaccountssameemail', $allowsameemail); $generator = self::getDataGenerator(); // Create the user that we wish to update. $usertoupdate = $generator->create_user(['email' => $currentemail, 'firstname' => $currentname]); if ($withanotheruser) { // Create another user that has the same email as the new email that we'd like to update for our target user. $generator->create_user(['email' => $newemail]); } // Build the user update parameters. $updateparams = [ 'id' => $usertoupdate->id, 'email' => $newemail, 'firstname' => $newname ]; // Let's try to update the user's information. core_user_external::update_users([$updateparams]); // Fetch the updated user record. $userrecord = $DB->get_record('user', ['id' => $usertoupdate->id], 'id, email, firstname'); // If we expect the update to succeed, then the email/name would have been changed. if ($successexpected) { $expectedemail = $newemail; $expectedname = $newname; } else { $expectedemail = $currentemail; $expectedname = $currentname; } // Confirm that our expectations are met. $this->assertEquals($expectedemail, $userrecord->email); $this->assertEquals($expectedname, $userrecord->firstname); } /** * Test add_user_private_files */ public function test_add_user_private_files() { global $USER, $CFG, $DB; $this->resetAfterTest(true); $context = \context_system::instance(); $roleid = $this->assignUserCapability('moodle/user:manageownfiles', $context->id); $context = \context_user::instance($USER->id); $contextid = $context->id; $component = "user"; $filearea = "draft"; $itemid = 0; $filepath = "/"; $filename = "Simple.txt"; $filecontent = base64_encode("Let us create a nice simple file"); $contextlevel = null; $instanceid = null; $browser = get_file_browser(); // 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); $draftid = $draftfile['itemid']; // Make sure the file was created. $file = $browser->get_file_info($context, $component, $filearea, $draftid, $filepath, $filename); $this->assertNotEmpty($file); // Make sure the file does not exist in the user private files. $file = $browser->get_file_info($context, $component, 'private', 0, $filepath, $filename); $this->assertEmpty($file); // Call the external function. core_user_external::add_user_private_files($draftid); // Make sure the file was added to the user private files. $file = $browser->get_file_info($context, $component, 'private', 0, $filepath, $filename); $this->assertNotEmpty($file); } /** * Test add_user_private_files quota */ public function test_add_user_private_files_quota() { global $USER, $CFG, $DB; $this->resetAfterTest(true); $context = \context_system::instance(); $roleid = $this->assignUserCapability('moodle/user:manageownfiles', $context->id); $context = \context_user::instance($USER->id); $contextid = $context->id; $component = "user"; $filearea = "draft"; $itemid = 0; $filepath = "/"; $filename = "Simple.txt"; $filecontent = base64_encode("Let us create a nice simple file"); $contextlevel = null; $instanceid = null; $browser = get_file_browser(); // 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); $draftid = $draftfile['itemid']; // Call the external function to add the file to private files. core_user_external::add_user_private_files($draftid); // Force the quota so we are sure it won't be space to add the new file. $CFG->userquota = file_get_user_used_space() + 1; // Generate a new draftitemid for the same testfile. $draftfile = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent, $contextlevel, $instanceid); $draftid = $draftfile['itemid']; $this->expectException('moodle_exception'); $this->expectExceptionMessage(get_string('maxareabytes', 'error')); // Call the external function to include the new file. core_user_external::add_user_private_files($draftid); } /** * Test add user device */ public function test_add_user_device() { global $USER, $CFG, $DB; $this->resetAfterTest(true); $device = array( 'appid' => 'com.moodle.moodlemobile', 'name' => 'occam', 'model' => 'Nexus 4', 'platform' => 'Android', 'version' => '4.2.2', 'pushid' => 'apushdkasdfj4835', 'uuid' => 'asdnfl348qlksfaasef859' ); // Call the external function. core_user_external::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'], $device['version'], $device['pushid'], $device['uuid']); $created = $DB->get_record('user_devices', array('pushid' => $device['pushid'])); $created = (array) $created; $this->assertEquals($device, array_intersect_key((array)$created, $device)); // Test reuse the same pushid value. $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); $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); $this->assertEquals(1, $DB->count_records('user_devices')); $updated = $DB->get_record('user_devices', array('pushid' => $device['pushid'])); $this->assertEquals($device, array_intersect_key((array)$updated, $device)); // Test creating a new device just changing the uuid. $device['uuid'] = 'newuidforthesameuser'; $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); $this->assertEquals(2, $DB->count_records('user_devices')); } /** * Test remove user device */ public function test_remove_user_device() { global $USER, $CFG, $DB; $this->resetAfterTest(true); $device = array( 'appid' => 'com.moodle.moodlemobile', 'name' => 'occam', 'model' => 'Nexus 4', 'platform' => 'Android', 'version' => '4.2.2', 'pushid' => 'apushdkasdfj4835', 'uuid' => 'ABCDE3723ksdfhasfaasef859' ); // A device with the same properties except the appid and pushid. $device2 = $device; $device2['pushid'] = "0987654321"; $device2['appid'] = "other.app.com"; $this->setAdminUser(); // Create a user device using the external API function. core_user_external::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'], $device['version'], $device['pushid'], $device['uuid']); // Create the same device but for a different app. core_user_external::add_user_device($device2['appid'], $device2['name'], $device2['model'], $device2['platform'], $device2['version'], $device2['pushid'], $device2['uuid']); // 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); $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); $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); $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); $this->assertTrue($result['removed']); } /** * Test get_user_preferences */ public function test_get_user_preferences() { $this->resetAfterTest(true); $user = self::getDataGenerator()->create_user(); set_user_preference('calendar_maxevents', 1, $user); set_user_preference('some_random_text', 'text', $user); $this->setUser($user); $result = core_user_external::get_user_preferences(); $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']); foreach ($result['preferences'] as $pref) { if ($pref['name'] === '_lastloaded') { continue; } // Check we receive the expected preferences. $this->assertEquals(get_user_preferences($pref['name']), $pref['value']); } // 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); $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); $this->assertCount(0, $result['warnings']); $this->assertCount(1, $result['preferences']); $this->assertEquals(null, $result['preferences'][0]['value']); // 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); $this->assertCount(0, $result['warnings']); $this->assertCount(3, $result['preferences']); foreach ($result['preferences'] as $pref) { if ($pref['name'] === '_lastloaded') { continue; } // Check we receive the expected preferences. $this->assertEquals(get_user_preferences($pref['name'], null, $user), $pref['value']); } // Check that as a non admin user we cannot retrieve other users preferences. $anotheruser = self::getDataGenerator()->create_user(); $this->setUser($anotheruser); $this->expectException('required_capability_exception'); $result = core_user_external::get_user_preferences('', $user->id); } /** * Test update_picture */ public function test_update_picture() { global $DB, $USER; $this->resetAfterTest(true); $user = self::getDataGenerator()->create_user(); self::setUser($user); $context = \context_user::instance($USER->id); $contextid = $context->id; $filename = "reddot.png"; $filecontent = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38" . "GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="; // Call the files api to create a file. $draftfile = core_files_external::upload($contextid, 'user', 'draft', 0, '/', $filename, $filecontent, null, null); $draftid = $draftfile['itemid']; // Change user profile image. $result = core_user_external::update_picture($draftid); $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']); // Check expected URL for serving the image. $this->assertStringContainsString("/$contextid/user/icon", $result['profileimageurl']); // Delete image. $result = core_user_external::update_picture(0, true); $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); // Add again the user profile image (as admin). $this->setAdminUser(); $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); // 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']); $this->assertStringContainsString("/$contextid/user/icon", $result['profileimageurl']); } /** * Test update_picture disabled */ public function test_update_picture_disabled() { global $CFG; $this->resetAfterTest(true); $CFG->disableuserimages = true; $this->setAdminUser(); $this->expectException('moodle_exception'); core_user_external::update_picture(0); } /** * Test set_user_preferences */ public function test_set_user_preferences_save() { global $DB; $this->resetAfterTest(true); $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); // Save users preferences. $this->setAdminUser(); $preferences = array( array( 'name' => 'htmleditor', 'value' => 'atto', 'userid' => $user1->id, ), array( 'name' => 'htmleditor', 'value' => 'tinymce', 'userid' => $user2->id, ) ); $result = core_user_external::set_user_preferences($preferences); $result = \external_api::clean_returnvalue(core_user_external::set_user_preferences_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertCount(2, $result['saved']); // Get preference from DB to avoid cache. $this->assertEquals('atto', $DB->get_field('user_preferences', 'value', array('userid' => $user1->id, 'name' => 'htmleditor'))); $this->assertEquals('tinymce', $DB->get_field('user_preferences', 'value', array('userid' => $user2->id, 'name' => 'htmleditor'))); } /** * Test set_user_preferences */ public function test_set_user_preferences_save_invalid_pref() { global $DB; $this->resetAfterTest(true); $user1 = self::getDataGenerator()->create_user(); // Save users preferences. $this->setAdminUser(); $preferences = array( array( 'name' => 'some_random_pref', 'value' => 'abc', 'userid' => $user1->id, ), ); $result = core_user_external::set_user_preferences($preferences); $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']); // Nothing was written to DB. $this->assertEmpty($DB->count_records('user_preferences', array('name' => 'some_random_pref'))); } /** * Test set_user_preferences for an invalid user */ public function test_set_user_preferences_invalid_user() { $this->resetAfterTest(true); $this->setAdminUser(); $preferences = array( array( 'name' => 'calendar_maxevents', 'value' => 4, 'userid' => -2 ) ); $result = core_user_external::set_user_preferences($preferences); $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']); $this->assertEquals(-2, $result['warnings'][0]['itemid']); } /** * Test set_user_preferences using an invalid preference */ public function test_set_user_preferences_invalid_preference() { global $USER, $DB; $this->resetAfterTest(true); // Create a very long value. $this->setAdminUser(); $preferences = array( array( 'name' => 'calendar_maxevents', 'value' => str_repeat('a', 1334), 'userid' => $USER->id ) ); $result = core_user_external::set_user_preferences($preferences); $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. $this->assertEquals(1, $DB->get_field('user_preferences', 'value', array('userid' => $USER->id, 'name' => 'calendar_maxevents'))); } /** * Test set_user_preferences for other user not being admin */ public function test_set_user_preferences_capability() { $this->resetAfterTest(true); $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); $this->setUser($user1); $preferences = array( array( 'name' => 'calendar_maxevents', 'value' => 4, 'userid' => $user2->id ) ); $result = core_user_external::set_user_preferences($preferences); $this->assertCount(1, $result['warnings']); $this->assertCount(0, $result['saved']); $this->assertEquals('nopermission', $result['warnings'][0]['warningcode']); $this->assertEquals($user2->id, $result['warnings'][0]['itemid']); } /** * Test update_user_preferences unsetting an existing preference. */ public function test_update_user_preferences_unset() { global $DB; $this->resetAfterTest(true); $user = self::getDataGenerator()->create_user(); // Save users preferences. $this->setAdminUser(); $preferences = array( array( 'name' => 'htmleditor', 'value' => 'atto', 'userid' => $user->id, ) ); $result = core_user_external::set_user_preferences($preferences); $result = \external_api::clean_returnvalue(core_user_external::set_user_preferences_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertCount(1, $result['saved']); // Get preference from DB to avoid cache. $this->assertEquals('atto', $DB->get_field('user_preferences', 'value', array('userid' => $user->id, 'name' => 'htmleditor'))); // Now, unset. $result = core_user_external::update_user_preferences($user->id, null, array(array('type' => 'htmleditor'))); $this->assertEquals(0, $DB->count_records('user_preferences', array('userid' => $user->id, 'name' => 'htmleditor'))); } /** * Test agree_site_policy */ public function test_agree_site_policy() { global $CFG, $DB, $USER; $this->resetAfterTest(true); $user = self::getDataGenerator()->create_user(); $this->setUser($user); // Site policy not set. $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('nositepolicy', $result['warnings'][0]['warningcode']); // Set a policy issue. $CFG->sitepolicy = 'https://moodle.org'; $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); $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); $this->assertFalse($result['status']); $this->assertCount(1, $result['warnings']); $this->assertEquals('alreadyagreed', $result['warnings'][0]['warningcode']); // Set something to make require_login throws an exception. $otheruser = self::getDataGenerator()->create_user(); $this->setUser($otheruser); $DB->set_field('user', 'lastname', '', array('id' => $USER->id)); $USER->lastname = ''; try { $result = core_user_external::agree_site_policy(); $this->fail('Expecting \'usernotfullysetup\' moodle_exception to be thrown'); } catch (\moodle_exception $e) { $this->assertEquals('usernotfullysetup', $e->errorcode); } catch (\Exception $e) { $this->fail('Expecting \'usernotfullysetup\' moodle_exception to be thrown.'); } } /** * Test get_private_files_info */ public function test_get_private_files_info() { $this->resetAfterTest(true); $user = self::getDataGenerator()->create_user(); $this->setUser($user); $usercontext = \context_user::instance($user->id); $filerecord = array( 'contextid' => $usercontext->id, 'component' => 'user', 'filearea' => 'private', 'itemid' => 0, 'filepath' => '/', 'filename' => 'thefile', ); $fs = get_file_storage(); $file = $fs->create_file_from_string($filerecord, 'abc'); // 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); $this->assertEquals(1, $result['filecount']); $this->assertEquals($file->get_filesize(), $result['filesize']); $this->assertEquals(0, $result['foldercount']); $this->assertEquals($file->get_filesize(), $result['filesizewithoutreferences']); // 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); $this->assertEquals(1, $result['filecount']); $this->assertEquals($file->get_filesize(), $result['filesize']); $this->assertEquals(0, $result['foldercount']); $this->assertEquals($file->get_filesize(), $result['filesizewithoutreferences']); } /** * Test get_private_files_info missing permissions. */ public function test_get_private_files_info_missing_permissions() { $this->resetAfterTest(true); $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); $this->setUser($user1); $this->expectException('required_capability_exception'); // Try to retrieve other user private files info. core_user_external::get_private_files_info($user2->id); } /** * Test the functionality of the {@see \core_user\external\search_identity} class. */ public function test_external_search_identity() { global $CFG; $this->resetAfterTest(true); $this->setAdminUser(); $user1 = self::getDataGenerator()->create_user([ 'firstname' => 'Firstone', 'lastname' => 'Lastone', 'username' => 'usernameone', 'idnumber' => 'idnumberone', 'email' => 'userone@example.com', 'phone1' => 'tel1', 'phone2' => 'tel2', 'department' => 'Department Foo', 'institution' => 'Institution Foo', 'city' => 'City One', 'country' => 'AU', ]); $user2 = self::getDataGenerator()->create_user([ 'firstname' => 'Firsttwo', 'lastname' => 'Lasttwo', 'username' => 'usernametwo', 'idnumber' => 'idnumbertwo', 'email' => 'usertwo@example.com', 'phone1' => 'tel1', 'phone2' => 'tel2', 'department' => 'Department Foo', 'institution' => 'Institution Foo', 'city' => 'City One', 'country' => 'AU', ]); $user3 = self::getDataGenerator()->create_user([ 'firstname' => 'Firstthree', 'lastname' => 'Lastthree', 'username' => 'usernamethree', 'idnumber' => 'idnumberthree', 'email' => 'userthree@example.com', 'phone1' => 'tel1', 'phone2' => 'tel2', 'department' => 'Department Foo', 'institution' => 'Institution Foo', 'city' => 'City One', 'country' => 'AU', ]); $CFG->showuseridentity = 'email,idnumber,city'; $CFG->maxusersperpage = 3; $result = \core_user\external\search_identity::execute('Lastt'); $result = \external_api::clean_returnvalue(\core_user\external\search_identity::execute_returns(), $result); $this->assertEquals(2, count($result['list'])); $this->assertEquals(3, $result['maxusersperpage']); $this->assertEquals(false, $result['overflow']); foreach ($result['list'] as $user) { $this->assertEquals(3, count($user['extrafields'])); $this->assertEquals('email', $user['extrafields'][0]['name']); $this->assertEquals('idnumber', $user['extrafields'][1]['name']); $this->assertEquals('city', $user['extrafields'][2]['name']); } $CFG->showuseridentity = 'username'; $CFG->maxusersperpage = 2; $result = \core_user\external\search_identity::execute('Firstt'); $result = \external_api::clean_returnvalue(\core_user\external\search_identity::execute_returns(), $result); $this->assertEquals(2, count($result['list'])); $this->assertEquals(2, $result['maxusersperpage']); $this->assertEquals(false, $result['overflow']); foreach ($result['list'] as $user) { $this->assertEquals(1, count($user['extrafields'])); $this->assertEquals('username', $user['extrafields'][0]['name']); } $CFG->showuseridentity = 'email'; $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); $this->assertEquals(0, count($result['list'])); $this->assertEquals(2, $result['maxusersperpage']); $this->assertEquals(false, $result['overflow']); $CFG->showuseridentity = 'city'; $CFG->maxusersperpage = 2; foreach ($result['list'] as $user) { $this->assertEquals(1, count($user['extrafields'])); $this->assertEquals('username', $user['extrafields'][0]['name']); } $result = \core_user\external\search_identity::execute('City One'); $result = \external_api::clean_returnvalue(\core_user\external\search_identity::execute_returns(), $result); $this->assertEquals(2, count($result['list'])); $this->assertEquals(2, $result['maxusersperpage']); $this->assertEquals(true, $result['overflow']); } /** * Test functionality of the {@see \core_user\external\search_identity} class with alternativefullnameformat defined. */ public function test_external_search_identity_with_alternativefullnameformat() { global $CFG; $this->resetAfterTest(true); $this->setAdminUser(); $user1 = self::getDataGenerator()->create_user([ 'lastname' => '小柳', 'lastnamephonetic' => 'Koyanagi', 'firstname' => '秋', 'firstnamephonetic' => 'Aki', 'email' => 'koyanagiaki@example.com', 'country' => 'JP', ]); $CFG->showuseridentity = 'email'; $CFG->maxusersperpage = 3; $CFG->alternativefullnameformat = '<ruby>lastname firstname <rp>(</rp><rt>lastnamephonetic firstnamephonetic</rt><rp>)</rp></ruby>'; $result = \core_user\external\search_identity::execute('Ak'); $result = \external_api::clean_returnvalue(\core_user\external\search_identity::execute_returns(), $result); $this->assertEquals(1, count($result['list'])); $this->assertEquals(3, $result['maxusersperpage']); $this->assertEquals(false, $result['overflow']); foreach ($result['list'] as $user) { $this->assertEquals(1, count($user['extrafields'])); $this->assertEquals('email', $user['extrafields'][0]['name']); } } /** * Test verifying that update_user_preferences prevents changes to the default homepage for other users. */ public function test_update_user_preferences_homepage_permission_callback() { global $DB; $this->resetAfterTest(); $user = self::getDataGenerator()->create_user(); $this->setUser($user); $adminuser = get_admin(); // Allow user selection of the default homepage via preferences. set_config('defaulthomepage', HOMEPAGE_USER); // Try to save another user's home page preference which uses the permissioncallback. $preferences = [ [ 'name' => 'user_home_page_preference', 'value' => '3', 'userid' => $adminuser->id, ] ]; $result = core_user_external::set_user_preferences($preferences); $result = \external_api::clean_returnvalue(core_user_external::set_user_preferences_returns(), $result); $this->assertCount(1, $result['warnings']); $this->assertCount(0, $result['saved']); // Verify no change to the preference, checking from DB to avoid cache. $this->assertEquals(null, $DB->get_field('user_preferences', 'value', ['userid' => $adminuser->id, 'name' => 'user_home_page_preference'])); } } home3/cpr76684/public_html/Aem/mod/forum/tests/externallib_test.php 0000644 00000501562 15152164514 0021135 0 ustar 00 <?php // This file is part of Moodle - http://moodle.org/ // // Moodle is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // Moodle is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Moodle. If not, see <http://www.gnu.org/licenses/>. namespace mod_forum; use externallib_advanced_testcase; use mod_forum_external; defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->dirroot . '/webservice/tests/helpers.php'); require_once($CFG->dirroot . '/mod/forum/lib.php'); /** * The module forums external functions unit tests * * @package mod_forum * @category external * @copyright 2012 Mark Nelson <markn@moodle.com> * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class externallib_test extends externallib_advanced_testcase { /** * Tests set up */ protected function setUp(): void { global $CFG; // We must clear the subscription caches. This has to be done both before each test, and after in case of other // tests using these functions. \mod_forum\subscriptions::reset_forum_cache(); require_once($CFG->dirroot . '/mod/forum/externallib.php'); } public function tearDown(): void { // We must clear the subscription caches. This has to be done both before each test, and after in case of other // tests using these functions. \mod_forum\subscriptions::reset_forum_cache(); } /** * Get the expected attachment. * * @param stored_file $file * @param array $values * @param moodle_url|null $url * @return array */ protected function get_expected_attachment(\stored_file $file, array $values = [], ?\moodle_url $url = null): array { if (!$url) { $url = \moodle_url::make_pluginfile_url( $file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename() ); $url->param('forcedownload', 1); } return array_merge( [ 'contextid' => $file->get_contextid(), 'component' => $file->get_component(), 'filearea' => $file->get_filearea(), 'itemid' => $file->get_itemid(), 'filepath' => $file->get_filepath(), 'filename' => $file->get_filename(), 'isdir' => $file->is_directory(), 'isimage' => $file->is_valid_image(), 'timemodified' => $file->get_timemodified(), 'timecreated' => $file->get_timecreated(), 'filesize' => $file->get_filesize(), 'author' => $file->get_author(), 'license' => $file->get_license(), 'filenameshort' => $file->get_filename(), 'filesizeformatted' => display_size((int) $file->get_filesize()), 'icon' => $file->is_directory() ? file_folder_icon(128) : file_file_icon($file, 128), 'timecreatedformatted' => userdate($file->get_timecreated()), 'timemodifiedformatted' => userdate($file->get_timemodified()), 'url' => $url->out(), ], $values); } /** * Test get forums */ public function test_mod_forum_get_forums_by_courses() { global $USER, $CFG, $DB; $this->resetAfterTest(true); // Create a user. $user = self::getDataGenerator()->create_user(array('trackforums' => 1)); // Set to the user. self::setUser($user); // Create courses to add the modules. $course1 = self::getDataGenerator()->create_course(); $course2 = self::getDataGenerator()->create_course(); // First forum. $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->introformat = FORMAT_HTML; $record->course = $course2->id; $record->trackingtype = FORUM_TRACKING_OFF; $forum2 = self::getDataGenerator()->create_module('forum', $record); $forum2->introfiles = []; // Add discussions to the forums. $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); // Expect one discussion. $forum1->numdiscussions = 1; $forum1->cancreatediscussions = true; $forum1->istracked = true; $forum1->unreadpostscount = 0; $forum1->introfiles = []; $forum1->lang = ''; $record = new \stdClass(); $record->course = $course2->id; $record->userid = $user->id; $record->forum = $forum2->id; $discussion2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); $discussion3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); // Expect two discussions. $forum2->numdiscussions = 2; // Default limited role, no create discussion capability enabled. $forum2->cancreatediscussions = false; $forum2->istracked = false; $forum2->lang = ''; // Check the forum was correctly created. $this->assertEquals(2, $DB->count_records_select('forum', 'id = :forum1 OR id = :forum2', array('forum1' => $forum1->id, 'forum2' => $forum2->id))); // Enrol the user in two courses. // DataGenerator->enrol_user automatically sets a role for the user with the permission mod/form:viewdiscussion. $this->getDataGenerator()->enrol_user($user->id, $course1->id, null, 'manual'); // Execute real Moodle enrolment as we'll call unenrol() method on the instance later. $enrol = enrol_get_plugin('manual'); $enrolinstances = enrol_get_instances($course2->id, true); foreach ($enrolinstances as $courseenrolinstance) { if ($courseenrolinstance->enrol == "manual") { $instance2 = $courseenrolinstance; break; } } $enrol->enrol_user($instance2, $user->id); // 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); $newrole = create_role('Role 2', 'role2', 'Role 2 description'); $roleid2 = $this->assignUserCapability('mod/forum:viewdiscussion', $context2->id, $newrole); // Create what we expect to be returned when querying the two courses. unset($forum1->displaywordcount); unset($forum2->displaywordcount); $expectedforums = array(); $expectedforums[$forum1->id] = (array) $forum1; $expectedforums[$forum2->id] = (array) $forum2; // 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); $this->assertCount(2, $forums); foreach ($forums as $forum) { $this->assertEquals($expectedforums[$forum['id']], $forum); } // 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); $this->assertCount(2, $forums); foreach ($forums as $forum) { $this->assertEquals($expectedforums[$forum['id']], $forum); } // Unenrol user from second course and alter expected forums. $enrol->unenrol_user($instance2, $user->id); unset($expectedforums[$forum2->id]); // 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); $this->assertCount(1, $forums); $this->assertEquals($expectedforums[$forum1->id], $forums[0]); $this->assertTrue($forums[0]['cancreatediscussions']); // 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); $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); $this->assertCount(0, $forums); } /** * Test the toggle favourite state */ public function test_mod_forum_toggle_favourite_state() { global $USER, $CFG, $DB; $this->resetAfterTest(true); // Create a user. $user = self::getDataGenerator()->create_user(array('trackforums' => 1)); // Set to the user. self::setUser($user); // Create courses to add the modules. $course1 = self::getDataGenerator()->create_course(); $this->getDataGenerator()->enrol_user($user->id, $course1->id); $record = new \stdClass(); $record->introformat = FORMAT_HTML; $record->course = $course1->id; $record->trackingtype = FORUM_TRACKING_OFF; $forum1 = self::getDataGenerator()->create_module('forum', $record); $forum1->introfiles = []; // Add discussions to the forums. $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); $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); $this->assertFalse($response['userstate']['favourited']); $this->setUser(0); try { $response = mod_forum_external::toggle_favourite_state($discussion1->id, 0); } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } } /** * Test the toggle pin state */ public function test_mod_forum_set_pin_state() { $this->resetAfterTest(true); // Create a user. $user = self::getDataGenerator()->create_user(array('trackforums' => 1)); // Set to the user. self::setUser($user); // Create courses to add the modules. $course1 = self::getDataGenerator()->create_course(); $this->getDataGenerator()->enrol_user($user->id, $course1->id); $record = new \stdClass(); $record->introformat = FORMAT_HTML; $record->course = $course1->id; $record->trackingtype = FORUM_TRACKING_OFF; $forum1 = self::getDataGenerator()->create_module('forum', $record); $forum1->introfiles = []; // Add discussions to the forums. $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); try { $response = mod_forum_external::set_pin_state($discussion1->id, 1); } 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); $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); $this->assertFalse($response['pinned']); } /** * Test get forum posts * * Tests is similar to the get_forum_discussion_posts only utilizing the new return structure and entities */ public function test_mod_forum_get_discussion_posts() { global $CFG; $this->resetAfterTest(true); // 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(); // Create course to add the module. $course1 = self::getDataGenerator()->create_course(); // Create a user who can track forums. $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); $exporteduser2 = [ 'id' => (int) $user2->id, 'fullname' => fullname($user2), 'isdeleted' => false, 'groups' => [], 'urls' => [ 'profile' => $urlfactory->get_author_profile_url($user2entity, $course1->id)->out(false), 'profileimage' => $urlfactory->get_author_profile_image_url($user2entity), ] ]; $user2->fullname = $exporteduser2['fullname']; $user3 = self::getDataGenerator()->create_user(['fullname' => "Mr Pants 1"]); $user3entity = $entityfactory->get_author_from_stdClass($user3); $exporteduser3 = [ 'id' => (int) $user3->id, 'fullname' => fullname($user3), 'groups' => [], 'isdeleted' => false, 'urls' => [ 'profile' => $urlfactory->get_author_profile_url($user3entity, $course1->id)->out(false), 'profileimage' => $urlfactory->get_author_profile_image_url($user3entity), ] ]; $user3->fullname = $exporteduser3['fullname']; $forumgenerator = self::getDataGenerator()->get_plugin_generator('mod_forum'); // Set the first created user to the test user. self::setUser($user1); // Forum with tracking off. $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); // Forum with tracking enabled. $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); // Add discussions to the forums. $record = new \stdClass(); $record->course = $course1->id; $record->userid = $user1->id; $record->forum = $forum1->id; $discussion1 = $forumgenerator->create_discussion($record); $record = new \stdClass(); $record->course = $course1->id; $record->userid = $user2->id; $record->forum = $forum1->id; $discussion2 = $forumgenerator->create_discussion($record); $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->discussion = $discussion1->id; $record->parent = $discussion1->firstpost; $record->userid = $user2->id; $discussion1reply1 = $forumgenerator->create_post($record); $filename = 'shouldbeanimage.jpg'; // Add a fake inline image to the post. $filerecordinline = array( 'contextid' => $forum1context->id, 'component' => 'mod_forum', 'filearea' => 'post', 'itemid' => $discussion1reply1->id, 'filepath' => '/', 'filename' => $filename, ); $fs = get_file_storage(); $timepost = time(); $file = $fs->create_file_from_string($filerecordinline, 'image contents (not really)'); $record->parent = $discussion1reply1->id; $record->userid = $user3->id; $discussion1reply2 = $forumgenerator->create_post($record); // Enrol the user in the course. $enrol = enrol_get_plugin('manual'); // Following line enrol and assign default role id to the user. // So the user automatically gets mod/forum:viewdiscussion on all forums of the course. $this->getDataGenerator()->enrol_user($user1->id, $course1->id); $this->getDataGenerator()->enrol_user($user2->id, $course1->id); // Delete one user, to test that we still receive posts by this user. delete_user($user3); $exporteduser3 = [ 'id' => (int) $user3->id, 'fullname' => get_string('deleteduser', 'mod_forum'), 'groups' => [], 'isdeleted' => true, 'urls' => [ 'profile' => $urlfactory->get_author_profile_url($user3entity, $course1->id)->out(false), 'profileimage' => $urlfactory->get_author_profile_image_url($user3entity), ] ]; // Create what we expect to be returned when querying the discussion. $expectedposts = array( 'posts' => array(), 'courseid' => $course1->id, 'forumid' => $forum1->id, 'ratinginfo' => array( 'contextid' => $forum1context->id, 'component' => 'mod_forum', 'ratingarea' => 'post', 'canviewall' => null, 'canviewany' => null, 'scales' => array(), 'ratings' => array(), ), 'warnings' => array(), ); // User pictures are initially empty, we should get the links once the external function is called. $isolatedurl = $urlfactory->get_discussion_view_url_from_discussion_id($discussion1reply2->discussion); $isolatedurl->params(['parent' => $discussion1reply2->id]); $message = file_rewrite_pluginfile_urls($discussion1reply2->message, 'pluginfile.php', $forum1context->id, 'mod_forum', 'post', $discussion1reply2->id); $expectedposts['posts'][] = array( 'id' => $discussion1reply2->id, 'discussionid' => $discussion1reply2->discussion, 'parentid' => $discussion1reply2->parent, 'hasparent' => true, 'timecreated' => $discussion1reply2->created, 'timemodified' => $discussion1reply2->modified, 'subject' => $discussion1reply2->subject, 'replysubject' => get_string('re', 'mod_forum') . " {$discussion1reply2->subject}", 'message' => $message, 'messageformat' => 1, // This value is usually changed by external_format_text() function. 'unread' => null, 'isdeleted' => false, 'isprivatereply' => false, 'haswordcount' => true, 'wordcount' => count_words($message), 'charcount' => count_letters($message), 'author'=> $exporteduser3, 'attachments' => [], 'messageinlinefiles' => [], 'tags' => [], 'html' => [ 'rating' => null, 'taglist' => null, 'authorsubheading' => $forumgenerator->get_author_subheading_html((object)$exporteduser3, $discussion1reply2->created) ], 'capabilities' => [ 'view' => 1, 'edit' => 0, 'delete' => 0, 'split' => 0, 'reply' => 1, 'export' => 0, 'controlreadstatus' => 0, 'canreplyprivately' => 0, 'selfenrol' => 0 ], 'urls' => [ 'view' => $urlfactory->get_view_post_url_from_post_id($discussion1reply2->discussion, $discussion1reply2->id), 'viewisolated' => $isolatedurl->out(false), 'viewparent' => $urlfactory->get_view_post_url_from_post_id($discussion1reply2->discussion, $discussion1reply2->parent), 'edit' => null, 'delete' =>null, 'split' => null, 'reply' => (new \moodle_url('/mod/forum/post.php#mformforum', [ 'reply' => $discussion1reply2->id ]))->out(false), 'export' => null, 'markasread' => null, 'markasunread' => null, 'discuss' => $urlfactory->get_discussion_view_url_from_discussion_id($discussion1reply2->discussion), ], ); $isolatedurl = $urlfactory->get_discussion_view_url_from_discussion_id($discussion1reply1->discussion); $isolatedurl->params(['parent' => $discussion1reply1->id]); $message = file_rewrite_pluginfile_urls($discussion1reply1->message, 'pluginfile.php', $forum1context->id, 'mod_forum', 'post', $discussion1reply1->id); $expectedposts['posts'][] = array( 'id' => $discussion1reply1->id, 'discussionid' => $discussion1reply1->discussion, 'parentid' => $discussion1reply1->parent, 'hasparent' => true, 'timecreated' => $discussion1reply1->created, 'timemodified' => $discussion1reply1->modified, 'subject' => $discussion1reply1->subject, 'replysubject' => get_string('re', 'mod_forum') . " {$discussion1reply1->subject}", 'message' => $message, 'messageformat' => 1, // This value is usually changed by external_format_text() function. 'unread' => null, 'isdeleted' => false, 'isprivatereply' => false, 'haswordcount' => true, 'wordcount' => count_words($message), 'charcount' => count_letters($message), 'author'=> $exporteduser2, 'attachments' => [], 'messageinlinefiles' => [ 0 => $this->get_expected_attachment($file) ], 'tags' => [], 'html' => [ 'rating' => null, 'taglist' => null, 'authorsubheading' => $forumgenerator->get_author_subheading_html((object)$exporteduser2, $discussion1reply1->created) ], 'capabilities' => [ 'view' => 1, 'edit' => 0, 'delete' => 0, 'split' => 0, 'reply' => 1, 'export' => 0, 'controlreadstatus' => 0, 'canreplyprivately' => 0, 'selfenrol' => 0 ], 'urls' => [ 'view' => $urlfactory->get_view_post_url_from_post_id($discussion1reply1->discussion, $discussion1reply1->id), 'viewisolated' => $isolatedurl->out(false), 'viewparent' => $urlfactory->get_view_post_url_from_post_id($discussion1reply1->discussion, $discussion1reply1->parent), 'edit' => null, 'delete' =>null, 'split' => null, 'reply' => (new \moodle_url('/mod/forum/post.php#mformforum', [ 'reply' => $discussion1reply1->id ]))->out(false), 'export' => null, 'markasread' => null, 'markasunread' => null, 'discuss' => $urlfactory->get_discussion_view_url_from_discussion_id($discussion1reply1->discussion), ], ); // 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); $this->assertEquals(3, count($posts['posts'])); // Unset the initial discussion post. array_pop($posts['posts']); $this->assertEquals($expectedposts, $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); foreach ($result as $f) { if ($f['id'] == $forum2->id) { $this->assertEquals(1, $f['unreadpostscount']); } } // 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); $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); $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); 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); $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); foreach ($posts['posts'] as $post) { $this->assertFalse($post['unread']); } // 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); foreach ($result as $f) { if ($f['id'] == $forum2->id) { $this->assertEquals(0, $f['unreadpostscount']); } } } /** * Test get forum posts */ public function test_mod_forum_get_discussion_posts_deleted() { global $CFG, $PAGE; $this->resetAfterTest(true); $generator = self::getDataGenerator()->get_plugin_generator('mod_forum'); // Create a course and enrol some users in it. $course1 = self::getDataGenerator()->create_course(); // Create users. $user1 = self::getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($user1->id, $course1->id); $user2 = self::getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($user2->id, $course1->id); // Set the first created user to the test user. self::setUser($user1); // Create test data. $forum1 = self::getDataGenerator()->create_module('forum', (object) [ 'course' => $course1->id, ]); $forum1context = \context_module::instance($forum1->cmid); // Add discussions to the forum. $discussion = $generator->create_discussion((object) [ 'course' => $course1->id, 'userid' => $user1->id, 'forum' => $forum1->id, ]); $discussion2 = $generator->create_discussion((object) [ 'course' => $course1->id, 'userid' => $user2->id, 'forum' => $forum1->id, ]); // Add replies to the discussion. $discussionreply1 = $generator->create_post((object) [ 'discussion' => $discussion->id, 'parent' => $discussion->firstpost, 'userid' => $user2->id, ]); $discussionreply2 = $generator->create_post((object) [ 'discussion' => $discussion->id, 'parent' => $discussionreply1->id, 'userid' => $user2->id, 'subject' => '', 'message' => '', 'messageformat' => FORMAT_PLAIN, 'deleted' => 1, ]); $discussionreply3 = $generator->create_post((object) [ 'discussion' => $discussion->id, 'parent' => $discussion->firstpost, 'userid' => $user2->id, ]); // 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); $deletedsubject = get_string('forumsubjectdeleted', 'mod_forum'); $deletedmessage = get_string('forumbodydeleted', 'mod_forum'); foreach ($posts['posts'] as $post) { if ($post['id'] == $discussionreply2->id) { $this->assertTrue($post['isdeleted']); $this->assertEquals($deletedsubject, $post['subject']); $this->assertEquals($deletedmessage, $post['message']); } else { $this->assertFalse($post['isdeleted']); $this->assertNotEquals($deletedsubject, $post['subject']); $this->assertNotEquals($deletedmessage, $post['message']); } } } /** * Test get forum posts returns inline attachments. */ public function test_mod_forum_get_discussion_posts_inline_attachments() { global $CFG; $this->resetAfterTest(true); // Create a course and enrol some users in it. $course = self::getDataGenerator()->create_course(); // Create users. $user = self::getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($user->id, $course->id); // Set the first created user to the test user. self::setUser($user); // Create test data. $forum = self::getDataGenerator()->create_module('forum', (object) [ 'course' => $course->id, ]); // Create a file in a draft area for inline attachments. $draftidinlineattach = file_get_unused_draft_itemid(); $draftidattach = file_get_unused_draft_itemid(); self::setUser($user); $usercontext = \context_user::instance($user->id); $filepath = '/'; $filearea = 'draft'; $component = 'user'; $filenameimg = 'fakeimage.png'; $filerecordinline = [ 'contextid' => $usercontext->id, 'component' => $component, 'filearea' => $filearea, 'itemid' => $draftidinlineattach, 'filepath' => $filepath, 'filename' => $filenameimg, ]; $fs = get_file_storage(); $fs->create_file_from_string($filerecordinline, 'image contents (not really)'); // Create discussion. $dummytext = 'Here is an inline image: <img src="' . $CFG->wwwroot . "/draftfile.php/{$usercontext->id}/user/draft/{$draftidinlineattach}/{$filenameimg}" . '" alt="inlineimage">.'; $options = [ [ 'name' => 'inlineattachmentsid', 'value' => $draftidinlineattach ], [ 'name' => 'attachmentsid', 'value' => $draftidattach ] ]; $discussion = mod_forum_external::add_discussion($forum->id, 'the inline attachment subject', $dummytext, -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); $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); $post = $posts['posts'][0]; $this->assertCount(1, $post['messageinlinefiles']); $this->assertEquals('fakeimage.png', $post['messageinlinefiles'][0]['filename']); } /** * Test get forum posts (qanda forum) */ public function test_mod_forum_get_discussion_posts_qanda() { global $CFG, $DB; $this->resetAfterTest(true); $record = new \stdClass(); $user1 = self::getDataGenerator()->create_user($record); $user2 = self::getDataGenerator()->create_user(); // Set the first created user to the test user. self::setUser($user1); // Create course to add the module. $course1 = self::getDataGenerator()->create_course(); $this->getDataGenerator()->enrol_user($user1->id, $course1->id); $this->getDataGenerator()->enrol_user($user2->id, $course1->id); // Forum with tracking off. $record = new \stdClass(); $record->course = $course1->id; $record->type = 'qanda'; $forum1 = self::getDataGenerator()->create_module('forum', $record); $forum1context = \context_module::instance($forum1->cmid); // Add discussions to the forums. $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->discussion = $discussion1->id; $record->parent = $discussion1->firstpost; $record->userid = $user2->id; $discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); // 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); $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->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); $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. $discussion1reply2->created -= $CFG->maxeditingtime * 2; $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); $this->assertEquals(3, count($posts['posts'])); } /** * Test get forum discussions paginated */ public function test_mod_forum_get_forum_discussions_paginated() { global $USER, $CFG, $DB, $PAGE; $this->resetAfterTest(true); // Set the CFG variable to allow track forums. $CFG->forum_trackreadposts = true; // Create a user who can track forums. $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(); $user3 = self::getDataGenerator()->create_user(); $user4 = self::getDataGenerator()->create_user(); // Set the first created user to the test user. self::setUser($user1); // Create courses to add the modules. $course1 = self::getDataGenerator()->create_course(); // First forum with tracking off. $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->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->discussion = $discussion1->id; $record->parent = $discussion1->firstpost; $record->userid = $user2->id; $discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); $record->parent = $discussion1reply1->id; $record->userid = $user3->id; $discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); $record->userid = $user4->id; $discussion1reply3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); // Enrol the user in the first course. $enrol = enrol_get_plugin('manual'); // We don't use the dataGenerator as we need to get the $instance2 to unenrol later. $enrolinstances = enrol_get_instances($course1->id, true); foreach ($enrolinstances as $courseenrolinstance) { if ($courseenrolinstance->enrol == "manual") { $instance1 = $courseenrolinstance; break; } } $enrol->enrol_user($instance1, $user1->id); // Delete one user. delete_user($user4); // 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); $newrole = create_role('Role 2', 'role2', 'Role 2 description'); $this->assignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole); // Create what we expect to be returned when querying the forums. $post1 = $DB->get_record('forum_posts', array('id' => $discussion1->firstpost), '*', MUST_EXIST); // User pictures are initially empty, we should get the links once the external function is called. $expecteddiscussions = array( 'id' => $discussion1->firstpost, 'name' => $discussion1->name, 'groupid' => (int) $discussion1->groupid, 'timemodified' => $discussion1reply3->created, 'usermodified' => (int) $discussion1reply3->userid, 'timestart' => (int) $discussion1->timestart, 'timeend' => (int) $discussion1->timeend, 'discussion' => $discussion1->id, 'parent' => 0, 'userid' => (int) $discussion1->userid, 'created' => (int) $post1->created, 'modified' => (int) $post1->modified, 'mailed' => (int) $post1->mailed, 'subject' => $post1->subject, 'message' => $post1->message, 'messageformat' => (int) $post1->messageformat, 'messagetrust' => (int) $post1->messagetrust, 'attachment' => $post1->attachment, 'totalscore' => (int) $post1->totalscore, 'mailnow' => (int) $post1->mailnow, 'userfullname' => fullname($user1), 'usermodifiedfullname' => fullname($user4), 'userpictureurl' => '', 'usermodifiedpictureurl' => '', 'numreplies' => 3, 'numunread' => 0, 'pinned' => (bool) FORUM_DISCUSSION_UNPINNED, 'locked' => false, 'canreply' => false, 'canlock' => false ); // 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); $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->size = 1; // Size f1. $expectedreturn['discussions'][0]['userpictureurl'] = $userpicture->get_url($PAGE)->out(false); $userpicture = new \user_picture($user4); $userpicture->size = 1; // Size f1. $expectedreturn['discussions'][0]['usermodifiedpictureurl'] = $userpicture->get_url($PAGE)->out(false); $this->assertEquals($expectedreturn, $discussions); // Call without required view discussion capability. $this->unassignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole); try { mod_forum_external::get_forum_discussions_paginated($forum1->id); $this->fail('Exception expected due to missing capability.'); } catch (\moodle_exception $e) { $this->assertEquals('noviewdiscussionspermission', $e->errorcode); } // Unenrol user from second course. $enrol->unenrol_user($instance1, $user1->id); // Call for the second course we unenrolled the user from, make sure exception thrown. 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) { $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); $this->assertTrue($discussions['discussions'][0]['canlock']); } /** * Test get forum discussions paginated (qanda forums) */ public function test_mod_forum_get_forum_discussions_paginated_qanda() { $this->resetAfterTest(true); // Create courses to add the modules. $course = self::getDataGenerator()->create_course(); $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); // First forum with tracking off. $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->course = $course->id; $discussionrecord->userid = $user2->id; $discussionrecord->forum = $forum->id; $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($discussionrecord); 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); $this->assertCount(1, $discussions['discussions']); $this->assertCount(0, $discussions['warnings']); self::setUser($user1); $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); $this->assertCount(1, $discussions['discussions']); $this->assertCount(0, $discussions['warnings']); } /** * Test get forum discussions */ public function test_mod_forum_get_forum_discussions() { global $CFG, $DB, $PAGE; $this->resetAfterTest(true); // Set the CFG variable to allow track forums. $CFG->forum_trackreadposts = true; // Create a user who can track forums. $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(); $user3 = self::getDataGenerator()->create_user(); $user4 = self::getDataGenerator()->create_user(); // Set the first created user to the test user. self::setUser($user1); // Create courses to add the modules. $course1 = self::getDataGenerator()->create_course(); // First forum with tracking off. $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->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->discussion = $discussion1->id; $record->parent = $discussion1->firstpost; $record->userid = $user2->id; $discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); $record->parent = $discussion1reply1->id; $record->userid = $user3->id; $discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); $record->userid = $user4->id; $discussion1reply3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); // Enrol the user in the first course. $enrol = enrol_get_plugin('manual'); // We don't use the dataGenerator as we need to get the $instance2 to unenrol later. $enrolinstances = enrol_get_instances($course1->id, true); foreach ($enrolinstances as $courseenrolinstance) { if ($courseenrolinstance->enrol == "manual") { $instance1 = $courseenrolinstance; break; } } $enrol->enrol_user($instance1, $user1->id); // Delete one user. delete_user($user4); // 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); $newrole = create_role('Role 2', 'role2', 'Role 2 description'); $this->assignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole); // Create what we expect to be returned when querying the forums. $post1 = $DB->get_record('forum_posts', array('id' => $discussion1->firstpost), '*', MUST_EXIST); // User pictures are initially empty, we should get the links once the external function is called. $expecteddiscussions = array( 'id' => $discussion1->firstpost, 'name' => $discussion1->name, 'groupid' => (int) $discussion1->groupid, 'timemodified' => (int) $discussion1reply3->created, 'usermodified' => (int) $discussion1reply3->userid, 'timestart' => (int) $discussion1->timestart, 'timeend' => (int) $discussion1->timeend, 'discussion' => (int) $discussion1->id, 'parent' => 0, 'userid' => (int) $discussion1->userid, 'created' => (int) $post1->created, 'modified' => (int) $post1->modified, 'mailed' => (int) $post1->mailed, 'subject' => $post1->subject, 'message' => $post1->message, 'messageformat' => (int) $post1->messageformat, 'messagetrust' => (int) $post1->messagetrust, 'attachment' => $post1->attachment, 'totalscore' => (int) $post1->totalscore, 'mailnow' => (int) $post1->mailnow, 'userfullname' => fullname($user1), 'usermodifiedfullname' => fullname($user4), 'userpictureurl' => '', 'usermodifiedpictureurl' => '', 'numreplies' => 3, 'numunread' => 0, 'pinned' => (bool) FORUM_DISCUSSION_UNPINNED, 'locked' => false, 'canreply' => false, 'canlock' => false, 'starred' => false, 'canfavourite' => true ); // 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); $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->size = 2; // Size f2. $expectedreturn['discussions'][0]['userpictureurl'] = $userpicture->get_url($PAGE)->out(false); $userpicture = new \user_picture($user4); $userpicture->size = 2; // Size f2. $expectedreturn['discussions'][0]['usermodifiedpictureurl'] = $userpicture->get_url($PAGE)->out(false); $this->assertEquals($expectedreturn, $discussions); // Test the starring functionality return. $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); $this->assertEquals($expectedreturn, $discussions); // Call without required view discussion capability. $this->unassignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole); try { mod_forum_external::get_forum_discussions($forum1->id); $this->fail('Exception expected due to missing capability.'); } catch (\moodle_exception $e) { $this->assertEquals('noviewdiscussionspermission', $e->errorcode); } // Unenrol user from second course. $enrol->unenrol_user($instance1, $user1->id); // Call for the second course we unenrolled the user from, make sure exception thrown. try { mod_forum_external::get_forum_discussions($forum1->id); $this->fail('Exception expected due to being unenrolled from the course.'); } 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); $this->assertTrue($discussions['discussions'][0]['canlock']); } /** * Test the sorting in get forum discussions */ public function test_mod_forum_get_forum_discussions_sorting() { global $CFG, $DB, $PAGE; $this->resetAfterTest(true); // Set the CFG variable to allow track forums. $CFG->forum_trackreadposts = true; // Create a user who can track forums. $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(); $user3 = self::getDataGenerator()->create_user(); $user4 = self::getDataGenerator()->create_user(); // Set the first created user to the test user. self::setUser($user1); // Create courses to add the modules. $course1 = self::getDataGenerator()->create_course(); // Enrol the user in the first course. $enrol = enrol_get_plugin('manual'); // We don't use the dataGenerator as we need to get the $instance2 to unenrol later. $enrolinstances = enrol_get_instances($course1->id, true); foreach ($enrolinstances as $courseenrolinstance) { if ($courseenrolinstance->enrol == "manual") { $instance1 = $courseenrolinstance; break; } } $enrol->enrol_user($instance1, $user1->id); // First forum with tracking off. $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); $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->course = $course1->id; $record->userid = $user1->id; $record->forum = $forum1->id; $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); sleep(1); // Add three replies to the discussion 1 from different users. $record = new \stdClass(); $record->discussion = $discussion1->id; $record->parent = $discussion1->firstpost; $record->userid = $user2->id; $discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); sleep(1); $record->parent = $discussion1reply1->id; $record->userid = $user3->id; $discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); sleep(1); $record->userid = $user4->id; $discussion1reply3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); sleep(1); // Create discussion2. $record2 = new \stdClass(); $record2->course = $course1->id; $record2->userid = $user1->id; $record2->forum = $forum1->id; $discussion2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record2); sleep(1); // Add one reply to the discussion 2. $record2 = new \stdClass(); $record2->discussion = $discussion2->id; $record2->parent = $discussion2->firstpost; $record2->userid = $user2->id; $discussion2reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record2); sleep(1); // Create discussion 3. $record3 = new \stdClass(); $record3->course = $course1->id; $record3->userid = $user1->id; $record3->forum = $forum1->id; $discussion3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record3); sleep(1); // Add two replies to the discussion 3. $record3 = new \stdClass(); $record3->discussion = $discussion3->id; $record3->parent = $discussion3->firstpost; $record3->userid = $user2->id; $discussion3reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record3); sleep(1); $record3->parent = $discussion3reply1->id; $record3->userid = $user3->id; $discussion3reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record3); // 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 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); $this->assertEquals($discussions['discussions'][2]['discussion'], $discussion1->id); $vaultfactory = \mod_forum\local\container::get_vault_factory(); $discussionlistvault = $vaultfactory->get_discussions_in_forum_vault(); // 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 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); $this->assertEquals($discussions['discussions'][2]['discussion'], $discussion3->id); // 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 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); $this->assertEquals($discussions['discussions'][2]['discussion'], $discussion1->id); // 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 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); $this->assertEquals($discussions['discussions'][2]['discussion'], $discussion3->id); // 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 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); $this->assertEquals($discussions['discussions'][2]['discussion'], $discussion2->id); // 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 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); $this->assertEquals($discussions['discussions'][2]['discussion'], $discussion1->id); // Pin discussion2. $DB->update_record('forum_discussions', (object) array('id' => $discussion2->id, 'pinned' => FORUM_DISCUSSION_PINNED)); // 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 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); $this->assertEquals($discussions['discussions'][1]['discussion'], $discussion3->id); $this->assertEquals($discussions['discussions'][2]['discussion'], $discussion1->id); // 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 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); $this->assertEquals($discussions['discussions'][1]['discussion'], $discussion1->id); $this->assertEquals($discussions['discussions'][2]['discussion'], $discussion3->id); } /** * Test add_discussion_post */ public function test_add_discussion_post() { global $CFG; $this->resetAfterTest(true); $user = self::getDataGenerator()->create_user(); $otheruser = self::getDataGenerator()->create_user(); self::setAdminUser(); // Create course to add the module. $course = self::getDataGenerator()->create_course(array('groupmode' => VISIBLEGROUPS, 'groupmodeforce' => 0)); // Forum with tracking off. $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); // Add discussions to the forums. $record = new \stdClass(); $record->course = $course->id; $record->userid = $user->id; $record->forum = $forum->id; $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); // Try to post (user not enrolled). self::setUser($user); 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) { $this->assertEquals('requireloginerror', $e->errorcode); } $this->getDataGenerator()->enrol_user($user->id, $course->id); $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); $posts = mod_forum_external::get_discussion_posts($discussion->id, 'modified', 'ASC'); $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'])); $tested = false; foreach ($posts['posts'] as $thispost) { if ($createdpost['postid'] == $thispost['id']) { $this->assertEquals('some subject', $thispost['subject']); $this->assertEquals('some text here...', $thispost['message']); $this->assertEquals(FORMAT_HTML, $thispost['messageformat']); // This is the default if format was not specified. $tested = true; } } $this->assertTrue($tested); // Let's simulate a call with any other format, it should be stored that way. global $DB; // Yes, we are going to use DB facilities too, because cannot rely on other functions for checking // the format. They eat it completely (going back to FORMAT_HTML. So we only can trust DB for further // processing. $formats = [FORMAT_PLAIN, FORMAT_MOODLE, FORMAT_MARKDOWN, FORMAT_HTML]; $options = []; 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); $dbformat = $DB->get_field('forum_posts', 'messageformat', ['id' => $createdpost['postid']]); $this->assertEquals($format, $dbformat); } // Now let's try the 'topreferredformat' option. That should end with the content // transformed and the format being FORMAT_HTML (when, like in this case, user preferred // format is HTML, inferred from editor in preferences). $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); $dbpost = $DB->get_record('forum_posts', ['id' => $createdpost['postid']]); // Format HTML and content converted, we should get. $this->assertEquals(FORMAT_HTML, $dbpost->messageformat); $this->assertEquals('<div class="text_to_html">with some https://example.com link</div>', $dbpost->message); // Test inline and regular attachment in post // Create a file in a draft area for inline attachments. $draftidinlineattach = file_get_unused_draft_itemid(); $draftidattach = file_get_unused_draft_itemid(); self::setUser($user); $usercontext = \context_user::instance($user->id); $filepath = '/'; $filearea = 'draft'; $component = 'user'; $filenameimg = 'shouldbeanimage.txt'; $filerecordinline = array( 'contextid' => $usercontext->id, 'component' => $component, 'filearea' => $filearea, 'itemid' => $draftidinlineattach, 'filepath' => $filepath, 'filename' => $filenameimg, ); $fs = get_file_storage(); // Create a file in a draft area for regular attachments. $filerecordattach = $filerecordinline; $attachfilename = 'attachment.txt'; $filerecordattach['filename'] = $attachfilename; $filerecordattach['itemid'] = $draftidattach; $fs->create_file_from_string($filerecordinline, 'image contents (not really)'); $fs->create_file_from_string($filerecordattach, 'simple text attachment'); $options = array(array('name' => 'inlineattachmentsid', 'value' => $draftidinlineattach), array('name' => 'attachmentsid', 'value' => $draftidattach)); $dummytext = 'Here is an inline image: <img src="' . $CFG->wwwroot . "/draftfile.php/{$usercontext->id}/user/draft/{$draftidinlineattach}/{$filenameimg}" . '" 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); $posts = mod_forum_external::get_discussion_posts($discussion->id, 'modified', 'ASC'); $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; foreach ($posts['posts'] as $thispost) { if ($createdpost['postid'] == $thispost['id']) { $this->assertEquals($createdpost['postid'], $thispost['id']); $this->assertCount(1, $thispost['attachments']); $this->assertEquals('attachment.txt', $thispost['attachments'][0]['filename']); $this->assertEquals($thispost['attachments'][0]['filename'], $attachfilename, "There should be 1 attachment"); $this->assertStringContainsString('pluginfile.php', $thispost['message']); $postfound = true; break; } } $this->assertTrue($postfound); // Check not posting in groups the user is not member of. $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id)); groups_add_member($group->id, $otheruser->id); $forum = self::getDataGenerator()->create_module('forum', $record, array('groupmode' => SEPARATEGROUPS)); $record->forum = $forum->id; $record->userid = $otheruser->id; $record->groupid = $group->id; $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); 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) { $this->assertEquals('nopostforum', $e->errorcode); } } /** * Test add_discussion_post and auto subscription to a discussion. */ public function test_add_discussion_post_subscribe_discussion() { global $USER; $this->resetAfterTest(true); self::setAdminUser(); $user = self::getDataGenerator()->create_user(); $admin = get_admin(); // Create course to add the module. $course = self::getDataGenerator()->create_course(array('groupmode' => VISIBLEGROUPS, 'groupmodeforce' => 0)); $this->getDataGenerator()->enrol_user($user->id, $course->id); // Forum with tracking off. $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->course = $course->id; $record->userid = $admin->id; $record->forum = $forum->id; $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); $discussion2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); // Try to post as user. self::setUser($user); // Enable auto subscribe discussion. $USER->autosubscribe = true; // Add a discussion post in a forum discussion where the user is not subscribed (auto-subscribe preference enabled). 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); // 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. $this->assertTrue(\mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussion1->id, $cm)); // Disable auto subscribe discussion. $USER->autosubscribe = false; $this->assertTrue(\mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussion1->id, $cm)); // Add a discussion post in a forum discussion where the user is subscribed (auto-subscribe preference disabled). 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); // 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. $this->assertTrue(\mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussion1->id, $cm)); $this->assertFalse(\mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussion2->id, $cm)); // Add a discussion post in a forum discussion where the user is not subscribed (auto-subscribe preference disabled). 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); // 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. $this->assertFalse(\mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussion2->id, $cm)); // Passing a value for the discussionsubscribe option parameter. $this->assertFalse(\mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussion2->id, $cm)); // Add a discussion post in a forum discussion where the user is not subscribed (auto-subscribe preference disabled), // and the option parameter 'discussionsubscribe' => true in the webservice. $option = array('name' => 'discussionsubscribe', 'value' => true); $options[] = $option; mod_forum_external::add_discussion_post($discussion2->firstpost, 'some subject 2', 'some text here 2...', $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); // 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. $this->assertTrue(\mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussion2->id, $cm)); } /* * Test add_discussion. A basic test since all the API functions are already covered by unit tests. */ public function test_add_discussion() { global $CFG, $USER; $this->resetAfterTest(true); // Create courses to add the modules. $course = self::getDataGenerator()->create_course(); $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); // First forum with tracking off. $record = new \stdClass(); $record->course = $course->id; $record->type = 'news'; $forum = self::getDataGenerator()->create_module('forum', $record); self::setUser($user1); $this->getDataGenerator()->enrol_user($user1->id, $course->id); 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) { $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); $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id); $discussions = \external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions); $this->assertCount(1, $discussions['discussions']); $this->assertCount(0, $discussions['warnings']); $this->assertEquals($createddiscussion['discussionid'], $discussions['discussions'][0]['discussion']); $this->assertEquals(-1, $discussions['discussions'][0]['groupid']); $this->assertEquals('the subject', $discussions['discussions'][0]['subject']); $this->assertEquals('some text here...', $discussions['discussions'][0]['message']); $discussion2pinned = mod_forum_external::add_discussion($forum->id, 'the pinned subject', 'some 2 text here...', -1, array('options' => array('name' => 'discussionpinned', '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); $this->assertCount(3, $discussions['discussions']); $this->assertEquals($discussion2pinned['discussionid'], $discussions['discussions'][0]['discussion']); // Test inline and regular attachment in new discussion // Create a file in a draft area for inline attachments. $fs = get_file_storage(); $draftidinlineattach = file_get_unused_draft_itemid(); $draftidattach = file_get_unused_draft_itemid(); $usercontext = \context_user::instance($USER->id); $filepath = '/'; $filearea = 'draft'; $component = 'user'; $filenameimg = 'shouldbeanimage.txt'; $filerecord = array( 'contextid' => $usercontext->id, 'component' => $component, 'filearea' => $filearea, 'itemid' => $draftidinlineattach, 'filepath' => $filepath, 'filename' => $filenameimg, ); // Create a file in a draft area for regular attachments. $filerecordattach = $filerecord; $attachfilename = 'attachment.txt'; $filerecordattach['filename'] = $attachfilename; $filerecordattach['itemid'] = $draftidattach; $fs->create_file_from_string($filerecord, 'image contents (not really)'); $fs->create_file_from_string($filerecordattach, 'simple text attachment'); $dummytext = 'Here is an inline image: <img src="' . $CFG->wwwroot . "/draftfile.php/{$usercontext->id}/user/draft/{$draftidinlineattach}/{$filenameimg}" . '" alt="inlineimage">.'; $options = array(array('name' => 'inlineattachmentsid', 'value' => $draftidinlineattach), 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); $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id); $discussions = \external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions); $this->assertCount(4, $discussions['discussions']); $this->assertCount(0, $createddiscussion['warnings']); // Can't guarantee order of posts during tests. $postfound = false; foreach ($discussions['discussions'] as $thisdiscussion) { if ($createddiscussion['discussionid'] == $thisdiscussion['discussion']) { $this->assertEquals($thisdiscussion['attachment'], 1, "There should be a non-inline attachment"); $this->assertCount(1, $thisdiscussion['attachments'], "There should be 1 attachment"); $this->assertEquals($thisdiscussion['attachments'][0]['filename'], $attachfilename, "There should be 1 attachment"); $this->assertStringNotContainsString('draftfile.php', $thisdiscussion['message']); $this->assertStringContainsString('pluginfile.php', $thisdiscussion['message']); $postfound = true; break; } } $this->assertTrue($postfound); } /** * Test adding discussions in a course with gorups */ public function test_add_discussion_in_course_with_groups() { global $CFG; $this->resetAfterTest(true); // Create course to add the module. $course = self::getDataGenerator()->create_course(array('groupmode' => VISIBLEGROUPS, 'groupmodeforce' => 0)); $user = self::getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($user->id, $course->id); // Forum forcing separate gropus. $record = new \stdClass(); $record->course = $course->id; $forum = self::getDataGenerator()->create_module('forum', $record, array('groupmode' => SEPARATEGROUPS)); // Try to post (user not enrolled). self::setUser($user); // The user is not enroled in any group, try to post in a forum with separate 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) { $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) { $this->assertEquals('cannotcreatediscussion', $e->errorcode); } // Create a group. $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id)); // Try to post in a group the user is not enrolled. 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) { $this->assertEquals('cannotcreatediscussion', $e->errorcode); } // Add the user to a group. groups_add_member($group->id, $user->id); // Try to post in a group the user is not enrolled. 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) { $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); $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id); $discussions = \external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions); $this->assertCount(1, $discussions['discussions']); $this->assertCount(0, $discussions['warnings']); $this->assertEquals($discussion['discussionid'], $discussions['discussions'][0]['discussion']); $this->assertEquals($group->id, $discussions['discussions'][0]['groupid']); // 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); $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id); $discussions = \external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions); $this->assertCount(2, $discussions['discussions']); $this->assertCount(0, $discussions['warnings']); $this->assertEquals($group->id, $discussions['discussions'][0]['groupid']); $this->assertEquals($group->id, $discussions['discussions'][1]['groupid']); // Enrol the same user in other group. $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id)); groups_add_member($group2->id, $user->id); // 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); $discussions = mod_forum_external::get_forum_discussions_paginated($forum->id); $discussions = \external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions); $this->assertCount(3, $discussions['discussions']); $this->assertCount(0, $discussions['warnings']); $this->assertEquals($group->id, $discussions['discussions'][0]['groupid']); $this->assertEquals($group->id, $discussions['discussions'][1]['groupid']); $this->assertEquals($group->id, $discussions['discussions'][2]['groupid']); } /* * Test set_lock_state. */ public function test_set_lock_state() { global $DB; $this->resetAfterTest(true); // Create courses to add the modules. $course = self::getDataGenerator()->create_course(); $user = self::getDataGenerator()->create_user(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); // First forum with tracking off. $record = new \stdClass(); $record->course = $course->id; $record->type = 'news'; $forum = self::getDataGenerator()->create_module('forum', $record); $record = new \stdClass(); $record->course = $course->id; $record->userid = $user->id; $record->forum = $forum->id; $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); // User who is a student. self::setUser($user); $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id, 'manual'); // Only a teacher should be able to lock a discussion. 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) { $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); $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); $this->assertFalse($result['locked']); $this->assertEquals('0', $result['times']['locked']); } /* * Test can_add_discussion. A basic test since all the API functions are already covered by unit tests. */ public function test_can_add_discussion() { global $DB; $this->resetAfterTest(true); // Create courses to add the modules. $course = self::getDataGenerator()->create_course(); $user = self::getDataGenerator()->create_user(); // First forum with tracking off. $record = new \stdClass(); $record->course = $course->id; $record->type = 'news'; $forum = self::getDataGenerator()->create_module('forum', $record); // User with no permissions to add in a news forum. self::setUser($user); $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); $this->assertFalse($result['status']); $this->assertFalse($result['canpindiscussions']); $this->assertTrue($result['cancreateattachment']); // 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); $this->assertFalse($result['status']); $this->assertFalse($result['canpindiscussions']); $this->assertFalse($result['cancreateattachment']); $DB->set_field('forum', 'maxattachments', 1, array('id' => $forum->id)); // Enable attachments again. self::setAdminUser(); $result = mod_forum_external::can_add_discussion($forum->id); $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']); } /* * A basic test to make sure users cannot post to forum after the cutoff date. */ public function test_can_add_discussion_after_cutoff() { $this->resetAfterTest(true); // Create courses to add the modules. $course = self::getDataGenerator()->create_course(); $user = self::getDataGenerator()->create_user(); // Create a forum with cutoff date set to a past date. $forum = self::getDataGenerator()->create_module('forum', ['course' => $course->id, 'cutoffdate' => time() - 1]); // User with no mod/forum:canoverridecutoff capability. self::setUser($user); $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); $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); $this->assertTrue($result['status']); } /** * Test get posts discussions including rating information. */ public function test_mod_forum_get_discussion_rating_information() { global $DB, $CFG, $PAGE; require_once($CFG->dirroot . '/rating/lib.php'); $PAGE->set_url('/my/index.php'); // Need this because some internal API calls require the $PAGE url to be set. $this->resetAfterTest(true); $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); $user3 = self::getDataGenerator()->create_user(); $teacher = self::getDataGenerator()->create_user(); // Create course to add the module. $course = self::getDataGenerator()->create_course(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher')); $this->getDataGenerator()->enrol_user($user1->id, $course->id, $studentrole->id, 'manual'); $this->getDataGenerator()->enrol_user($user2->id, $course->id, $studentrole->id, 'manual'); $this->getDataGenerator()->enrol_user($user3->id, $course->id, $studentrole->id, 'manual'); $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id, 'manual'); // Create the forum. $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); // Add discussion to the forum. $record = new \stdClass(); $record->course = $course->id; $record->userid = $user1->id; $record->forum = $forum->id; $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); // Retrieve the first post. $post = $DB->get_record('forum_posts', array('discussion' => $discussion->id)); // Rate the discussion as user2. $rating1 = new \stdClass(); $rating1->contextid = $context->id; $rating1->component = 'mod_forum'; $rating1->ratingarea = 'post'; $rating1->itemid = $post->id; $rating1->rating = 50; $rating1->scaleid = 100; $rating1->userid = $user2->id; $rating1->timecreated = time(); $rating1->timemodified = time(); $rating1->id = $DB->insert_record('rating', $rating1); // Rate the discussion as user3. $rating2 = new \stdClass(); $rating2->contextid = $context->id; $rating2->component = 'mod_forum'; $rating2->ratingarea = 'post'; $rating2->itemid = $post->id; $rating2->rating = 100; $rating2->scaleid = 100; $rating2->userid = $user3->id; $rating2->timecreated = time() + 1; $rating2->timemodified = time() + 1; $rating2->id = $DB->insert_record('rating', $rating2); // 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); $this->assertCount(1, $posts['ratinginfo']['ratings']); $this->assertTrue($posts['ratinginfo']['ratings'][0]['canviewaggregate']); $this->assertFalse($posts['ratinginfo']['canviewall']); $this->assertFalse($posts['ratinginfo']['ratings'][0]['canrate']); $this->assertEquals(2, $posts['ratinginfo']['ratings'][0]['count']); $this->assertEquals(($rating1->rating + $rating2->rating) / 2, $posts['ratinginfo']['ratings'][0]['aggregate']); // 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); $this->assertCount(1, $posts['ratinginfo']['ratings']); $this->assertTrue($posts['ratinginfo']['ratings'][0]['canviewaggregate']); $this->assertTrue($posts['ratinginfo']['canviewall']); $this->assertTrue($posts['ratinginfo']['ratings'][0]['canrate']); $this->assertEquals(2, $posts['ratinginfo']['ratings'][0]['count']); $this->assertEquals(($rating1->rating + $rating2->rating) / 2, $posts['ratinginfo']['ratings'][0]['aggregate']); } /** * Test mod_forum_get_forum_access_information. */ public function test_mod_forum_get_forum_access_information() { global $DB; $this->resetAfterTest(true); $student = self::getDataGenerator()->create_user(); $course = self::getDataGenerator()->create_course(); // Create the forum. $record = new \stdClass(); $record->course = $course->id; $forum = self::getDataGenerator()->create_module('forum', $record); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual'); 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); // Check default values for capabilities. $enabledcaps = array('canviewdiscussion', 'canstartdiscussion', 'canreplypost', 'canviewrating', 'cancreateattachment', 'canexportownpost', 'cancantogglefavourite', 'candeleteownpost', 'canallowforcesubscribe'); unset($result['warnings']); foreach ($result as $capname => $capvalue) { if (in_array($capname, $enabledcaps)) { $this->assertTrue($capvalue); } else { $this->assertFalse($capvalue); } } // Now, unassign some capabilities. unassign_capability('mod/forum:deleteownpost', $studentrole->id); unassign_capability('mod/forum:allowforcesubscribe', $studentrole->id); array_pop($enabledcaps); array_pop($enabledcaps); 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); unset($result['warnings']); foreach ($result as $capname => $capvalue) { if (in_array($capname, $enabledcaps)) { $this->assertTrue($capvalue); } else { $this->assertFalse($capvalue); } } } /** * Test add_discussion_post */ public function test_add_discussion_post_private() { global $DB; $this->resetAfterTest(true); self::setAdminUser(); // Create course to add the module. $course = self::getDataGenerator()->create_course(); // Standard forum. $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); $generator = self::getDataGenerator()->get_plugin_generator('mod_forum'); // Create an enrol users. $student1 = self::getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($student1->id, $course->id, 'student'); $student2 = self::getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($student2->id, $course->id, 'student'); $teacher1 = self::getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($teacher1->id, $course->id, 'editingteacher'); $teacher2 = self::getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($teacher2->id, $course->id, 'editingteacher'); // Add a new discussion to the forum. self::setUser($student1); $record = new \stdClass(); $record->course = $course->id; $record->userid = $student1->id; $record->forum = $forum->id; $discussion = $generator->create_discussion($record); // Have the teacher reply privately. self::setUser($teacher1); $post = mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...', [ [ 'name' => 'private', 'value' => true, ], ]); $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. $privatereply->created++; $privatereply->modified = $privatereply->created; $DB->update_record('forum_posts', $privatereply); // 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); $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); $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); $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); $this->assertEquals(1, count($posts['posts'])); $this->assertFalse($posts['posts'][0]['isprivatereply']); // A user cannot reply to a private reply. self::setUser($teacher2); $this->expectException('coding_exception'); $post = mod_forum_external::add_discussion_post($privatereply->id, 'some subject', 'some text here...', [ 'options' => [ 'name' => 'private', 'value' => false, ], ]); } /** * Test trusted text enabled. */ public function test_trusted_text_enabled() { global $USER, $CFG; $this->resetAfterTest(true); $CFG->enabletrusttext = 1; $dangeroustext = '<button>Untrusted text</button>'; $cleantext = 'Untrusted text'; // Create courses to add the modules. $course = self::getDataGenerator()->create_course(); $user1 = self::getDataGenerator()->create_user(); // First forum with tracking off. $record = new \stdClass(); $record->course = $course->id; $record->type = 'qanda'; $forum = self::getDataGenerator()->create_module('forum', $record); $context = \context_module::instance($forum->cmid); // Add discussions to the forums. $discussionrecord = new \stdClass(); $discussionrecord->course = $course->id; $discussionrecord->userid = $user1->id; $discussionrecord->forum = $forum->id; $discussionrecord->message = $dangeroustext; $discussionrecord->messagetrust = trusttext_trusted($context); $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($discussionrecord); self::setAdminUser(); $discussionrecord->userid = $USER->id; $discussionrecord->messagetrust = trusttext_trusted($context); $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); $this->assertCount(2, $discussions['discussions']); $this->assertCount(0, $discussions['warnings']); // Admin message is fully trusted. $this->assertEquals(1, $discussions['discussions'][0]['messagetrust']); $this->assertEquals($dangeroustext, $discussions['discussions'][0]['message']); // Student message is not trusted. $this->assertEquals(0, $discussions['discussions'][1]['messagetrust']); $this->assertEquals($cleantext, $discussions['discussions'][1]['message']); // 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); // 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); // Student message is not trusted. $this->assertEquals($cleantext, $posts['posts'][0]['message']); } /** * Test trusted text disabled. */ public function test_trusted_text_disabled() { global $USER, $CFG; $this->resetAfterTest(true); $CFG->enabletrusttext = 0; $dangeroustext = '<button>Untrusted text</button>'; $cleantext = 'Untrusted text'; // Create courses to add the modules. $course = self::getDataGenerator()->create_course(); $user1 = self::getDataGenerator()->create_user(); // First forum with tracking off. $record = new \stdClass(); $record->course = $course->id; $record->type = 'qanda'; $forum = self::getDataGenerator()->create_module('forum', $record); $context = \context_module::instance($forum->cmid); // Add discussions to the forums. $discussionrecord = new \stdClass(); $discussionrecord->course = $course->id; $discussionrecord->userid = $user1->id; $discussionrecord->forum = $forum->id; $discussionrecord->message = $dangeroustext; $discussionrecord->messagetrust = trusttext_trusted($context); $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($discussionrecord); self::setAdminUser(); $discussionrecord->userid = $USER->id; $discussionrecord->messagetrust = trusttext_trusted($context); $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); $this->assertCount(2, $discussions['discussions']); $this->assertCount(0, $discussions['warnings']); // Admin message is not trusted because enabletrusttext is disabled. $this->assertEquals(0, $discussions['discussions'][0]['messagetrust']); $this->assertEquals($cleantext, $discussions['discussions'][0]['message']); // Student message is not trusted. $this->assertEquals(0, $discussions['discussions'][1]['messagetrust']); $this->assertEquals($cleantext, $discussions['discussions'][1]['message']); // 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); // 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); // Student message is not trusted. $this->assertEquals($cleantext, $posts['posts'][0]['message']); } /** * Test delete a discussion. */ public function test_delete_post_discussion() { global $DB; $this->resetAfterTest(true); // Setup test data. $course = $this->getDataGenerator()->create_course(); $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id)); $user = $this->getDataGenerator()->create_user(); $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->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::delete_post($discussion->firstpost); $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))); } /** * Test delete a post. */ public function test_delete_post_post() { global $DB; $this->resetAfterTest(true); // Setup test data. $course = $this->getDataGenerator()->create_course(); $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id)); $user = $this->getDataGenerator()->create_user(); $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->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->course = $course->id; $record->userid = $user->id; $record->forum = $forum->id; $record->discussion = $discussion->id; $record->parent = $parentpost->id; $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); $this->setUser($user); $result = mod_forum_external::delete_post($post->id); $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))); } /** * Test delete a different user post. */ public function test_delete_post_other_user_post() { global $DB; $this->resetAfterTest(true); // Setup test data. $course = $this->getDataGenerator()->create_course(); $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id)); $user = $this->getDataGenerator()->create_user(); $otheruser = $this->getDataGenerator()->create_user(); $role = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST); self::getDataGenerator()->enrol_user($user->id, $course->id, $role->id); self::getDataGenerator()->enrol_user($otheruser->id, $course->id, $role->id); // Add a discussion. $record = array(); $record['course'] = $course->id; $record['forum'] = $forum->id; $record['userid'] = $user->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->course = $course->id; $record->userid = $user->id; $record->forum = $forum->id; $record->discussion = $discussion->id; $record->parent = $parentpost->id; $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); $this->setUser($otheruser); $this->expectExceptionMessage(get_string('cannotdeletepost', 'forum')); mod_forum_external::delete_post($post->id); } /* * Test get forum posts by user id. */ 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(); $postvault = $vaultfactory->get_post_vault(); $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); $exporteduser1 = [ 'id' => (int) $user1->id, 'fullname' => fullname($user1), 'groups' => [], 'urls' => [ 'profile' => $urlfactory->get_author_profile_url($user1entity, $course1->id)->out(false), 'profileimage' => $urlfactory->get_author_profile_image_url($user1entity), ], 'isdeleted' => false, ]; // Create a bunch of other users to post. $user2 = self::getDataGenerator()->create_user(); $user2entity = $entityfactory->get_author_from_stdClass($user2); $exporteduser2 = [ 'id' => (int) $user2->id, 'fullname' => fullname($user2), 'groups' => [], 'urls' => [ 'profile' => $urlfactory->get_author_profile_url($user2entity, $course1->id)->out(false), 'profileimage' => $urlfactory->get_author_profile_image_url($user2entity), ], 'isdeleted' => false, ]; $user2->fullname = $exporteduser2['fullname']; $forumgenerator = self::getDataGenerator()->get_plugin_generator('mod_forum'); // Set the first created user to the test user. self::setUser($user1); // Forum with tracking off. $record = new \stdClass(); $record->course = $course1->id; $forum1 = self::getDataGenerator()->create_module('forum', $record); $forum1context = \context_module::instance($forum1->cmid); // Add discussions to the forums. $time = time(); $record = new \stdClass(); $record->course = $course1->id; $record->userid = $user1->id; $record->forum = $forum1->id; $record->timemodified = $time + 100; $discussion1 = $forumgenerator->create_discussion($record); $discussion1firstpost = $postvault->get_first_post_for_discussion_ids([$discussion1->id]); $discussion1firstpost = $discussion1firstpost[$discussion1->firstpost]; $discussion1firstpostobject = $legacypostmapper->to_legacy_object($discussion1firstpost); $record = new \stdClass(); $record->course = $course1->id; $record->userid = $user1->id; $record->forum = $forum1->id; $record->timemodified = $time + 200; $discussion2 = $forumgenerator->create_discussion($record); $discussion2firstpost = $postvault->get_first_post_for_discussion_ids([$discussion2->id]); $discussion2firstpost = $discussion2firstpost[$discussion2->firstpost]; $discussion2firstpostobject = $legacypostmapper->to_legacy_object($discussion2firstpost); // Add 1 reply to the discussion 1 from a different user. $record = new \stdClass(); $record->discussion = $discussion1->id; $record->parent = $discussion1->firstpost; $record->userid = $user2->id; $discussion1reply1 = $forumgenerator->create_post($record); $filename = 'shouldbeanimage.jpg'; // Add a fake inline image to the post. $filerecordinline = array( 'contextid' => $forum1context->id, 'component' => 'mod_forum', 'filearea' => 'post', 'itemid' => $discussion1reply1->id, 'filepath' => '/', 'filename' => $filename, ); $fs = get_file_storage(); $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->discussion = $discussion2->id; $record->parent = $discussion2->firstpost; $record->userid = $user2->id; $discussion2reply1 = $forumgenerator->create_post($record); $filename = 'shouldbeanimage.jpg'; // Add a fake inline image to the post. $filerecordinline = array( 'contextid' => $forum1context->id, 'component' => 'mod_forum', 'filearea' => 'post', 'itemid' => $discussion2reply1->id, 'filepath' => '/', 'filename' => $filename, ); $fs = get_file_storage(); $file2 = $fs->create_file_from_string($filerecordinline, 'image contents (not really)'); // Following line enrol and assign default role id to the user. // So the user automatically gets mod/forum:viewdiscussion on all forums of the course. $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 'teacher'); $this->getDataGenerator()->enrol_user($user2->id, $course1->id); // Changed display period for the discussions in past. $discussion = new \stdClass(); $discussion->id = $discussion1->id; $discussion->timestart = $time - 200; $discussion->timeend = $time - 100; $DB->update_record('forum_discussions', $discussion); $discussion = new \stdClass(); $discussion->id = $discussion2->id; $discussion->timestart = $time - 200; $discussion->timeend = $time - 100; $DB->update_record('forum_discussions', $discussion); // Create what we expect to be returned when querying the discussion. $expectedposts = array( 'discussions' => array(), 'warnings' => array(), ); $isolatedurluser = $urlfactory->get_discussion_view_url_from_discussion_id($discussion1reply1->discussion); $isolatedurluser->params(['parent' => $discussion1reply1->id]); $isolatedurlparent = $urlfactory->get_discussion_view_url_from_discussion_id($discussion1firstpostobject->discussion); $isolatedurlparent->params(['parent' => $discussion1firstpostobject->id]); $expectedposts['discussions'][0] = [ 'name' => $discussion1->name, 'id' => $discussion1->id, 'timecreated' => $discussion1firstpost->get_time_created(), 'authorfullname' => $user1entity->get_full_name(), 'posts' => [ 'userposts' => [ [ 'id' => $discussion1reply1->id, 'discussionid' => $discussion1reply1->discussion, 'parentid' => $discussion1reply1->parent, 'hasparent' => true, 'timecreated' => $discussion1reply1->created, 'timemodified' => $discussion1reply1->modified, 'subject' => $discussion1reply1->subject, 'replysubject' => get_string('re', 'mod_forum') . " {$discussion1reply1->subject}", 'message' => file_rewrite_pluginfile_urls($discussion1reply1->message, 'pluginfile.php', $forum1context->id, 'mod_forum', 'post', $discussion1reply1->id), 'messageformat' => 1, // This value is usually changed by external_format_text() function. 'unread' => null, 'isdeleted' => false, 'isprivatereply' => false, 'haswordcount' => false, 'wordcount' => null, 'author' => $exporteduser2, 'attachments' => [], 'messageinlinefiles' => [], 'tags' => [], 'html' => [ 'rating' => null, 'taglist' => null, 'authorsubheading' => $forumgenerator->get_author_subheading_html( (object)$exporteduser2, $discussion1reply1->created) ], 'charcount' => null, 'capabilities' => [ 'view' => true, 'edit' => true, 'delete' => true, 'split' => true, 'reply' => true, 'export' => false, 'controlreadstatus' => false, 'canreplyprivately' => true, 'selfenrol' => false ], 'urls' => [ 'view' => $urlfactory->get_view_post_url_from_post_id( $discussion1reply1->discussion, $discussion1reply1->id)->out(false), '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' => $discussion1reply1->id ]))->out(false), 'delete' => (new \moodle_url('/mod/forum/post.php', [ 'delete' => $discussion1reply1->id ]))->out(false), 'split' => (new \moodle_url('/mod/forum/post.php', [ 'prune' => $discussion1reply1->id ]))->out(false), 'reply' => (new \moodle_url('/mod/forum/post.php#mformforum', [ 'reply' => $discussion1reply1->id ]))->out(false), 'export' => null, 'markasread' => null, 'markasunread' => null, 'discuss' => $urlfactory->get_discussion_view_url_from_discussion_id( $discussion1reply1->discussion)->out(false), ], ] ], 'parentposts' => [ [ 'id' => $discussion1firstpostobject->id, 'discussionid' => $discussion1firstpostobject->discussion, 'parentid' => null, 'hasparent' => false, 'timecreated' => $discussion1firstpostobject->created, 'timemodified' => $discussion1firstpostobject->modified, 'subject' => $discussion1firstpostobject->subject, 'replysubject' => get_string('re', 'mod_forum') . " {$discussion1firstpostobject->subject}", 'message' => file_rewrite_pluginfile_urls($discussion1firstpostobject->message, 'pluginfile.php', $forum1context->id, 'mod_forum', 'post', $discussion1firstpostobject->id), 'messageformat' => 1, // This value is usually changed by external_format_text() function. 'unread' => null, 'isdeleted' => false, 'isprivatereply' => false, 'haswordcount' => false, 'wordcount' => null, 'author' => $exporteduser1, 'attachments' => [], 'messageinlinefiles' => [], 'tags' => [], 'html' => [ 'rating' => null, 'taglist' => null, 'authorsubheading' => $forumgenerator->get_author_subheading_html( (object)$exporteduser1, $discussion1firstpostobject->created) ], 'charcount' => null, 'capabilities' => [ 'view' => true, 'edit' => true, 'delete' => true, 'split' => false, 'reply' => true, 'export' => false, 'controlreadstatus' => false, 'canreplyprivately' => true, 'selfenrol' => false ], 'urls' => [ 'view' => $urlfactory->get_view_post_url_from_post_id( $discussion1firstpostobject->discussion, $discussion1firstpostobject->id)->out(false), 'viewisolated' => $isolatedurlparent->out(false), 'viewparent' => null, 'edit' => (new \moodle_url('/mod/forum/post.php', [ 'edit' => $discussion1firstpostobject->id ]))->out(false), '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' => $discussion1firstpostobject->id ]))->out(false), 'export' => null, 'markasread' => null, 'markasunread' => null, 'discuss' => $urlfactory->get_discussion_view_url_from_discussion_id( $discussion1firstpostobject->discussion)->out(false), ], ] ], ], ]; $isolatedurluser = $urlfactory->get_discussion_view_url_from_discussion_id($discussion2reply1->discussion); $isolatedurluser->params(['parent' => $discussion2reply1->id]); $isolatedurlparent = $urlfactory->get_discussion_view_url_from_discussion_id($discussion2firstpostobject->discussion); $isolatedurlparent->params(['parent' => $discussion2firstpostobject->id]); $expectedposts['discussions'][1] = [ 'name' => $discussion2->name, 'id' => $discussion2->id, 'timecreated' => $discussion2firstpost->get_time_created(), 'authorfullname' => $user1entity->get_full_name(), 'posts' => [ 'userposts' => [ [ 'id' => $discussion2reply1->id, 'discussionid' => $discussion2reply1->discussion, 'parentid' => $discussion2reply1->parent, 'hasparent' => true, 'timecreated' => $discussion2reply1->created, 'timemodified' => $discussion2reply1->modified, 'subject' => $discussion2reply1->subject, 'replysubject' => get_string('re', 'mod_forum') . " {$discussion2reply1->subject}", 'message' => file_rewrite_pluginfile_urls($discussion2reply1->message, 'pluginfile.php', $forum1context->id, 'mod_forum', 'post', $discussion2reply1->id), 'messageformat' => 1, // This value is usually changed by external_format_text() function. 'unread' => null, 'isdeleted' => false, 'isprivatereply' => false, 'haswordcount' => false, 'wordcount' => null, 'author' => $exporteduser2, 'attachments' => [], 'messageinlinefiles' => [], 'tags' => [], 'html' => [ 'rating' => null, 'taglist' => null, 'authorsubheading' => $forumgenerator->get_author_subheading_html( (object)$exporteduser2, $discussion2reply1->created) ], 'charcount' => null, 'capabilities' => [ 'view' => true, 'edit' => true, 'delete' => true, 'split' => true, 'reply' => true, 'export' => false, 'controlreadstatus' => false, 'canreplyprivately' => true, 'selfenrol' => false ], 'urls' => [ 'view' => $urlfactory->get_view_post_url_from_post_id( $discussion2reply1->discussion, $discussion2reply1->id)->out(false), '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' => $discussion2reply1->id ]))->out(false), 'delete' => (new \moodle_url('/mod/forum/post.php', [ 'delete' => $discussion2reply1->id ]))->out(false), 'split' => (new \moodle_url('/mod/forum/post.php', [ 'prune' => $discussion2reply1->id ]))->out(false), 'reply' => (new \moodle_url('/mod/forum/post.php#mformforum', [ 'reply' => $discussion2reply1->id ]))->out(false), 'export' => null, 'markasread' => null, 'markasunread' => null, 'discuss' => $urlfactory->get_discussion_view_url_from_discussion_id( $discussion2reply1->discussion)->out(false), ], ] ], 'parentposts' => [ [ 'id' => $discussion2firstpostobject->id, 'discussionid' => $discussion2firstpostobject->discussion, 'parentid' => null, 'hasparent' => false, 'timecreated' => $discussion2firstpostobject->created, 'timemodified' => $discussion2firstpostobject->modified, 'subject' => $discussion2firstpostobject->subject, 'replysubject' => get_string('re', 'mod_forum') . " {$discussion2firstpostobject->subject}", 'message' => file_rewrite_pluginfile_urls($discussion2firstpostobject->message, 'pluginfile.php', $forum1context->id, 'mod_forum', 'post', $discussion2firstpostobject->id), 'messageformat' => 1, // This value is usually changed by external_format_text() function. 'unread' => null, 'isdeleted' => false, 'isprivatereply' => false, 'haswordcount' => false, 'wordcount' => null, 'author' => $exporteduser1, 'attachments' => [], 'messageinlinefiles' => [], 'tags' => [], 'html' => [ 'rating' => null, 'taglist' => null, 'authorsubheading' => $forumgenerator->get_author_subheading_html( (object)$exporteduser1, $discussion2firstpostobject->created) ], 'charcount' => null, 'capabilities' => [ 'view' => true, 'edit' => true, 'delete' => true, 'split' => false, 'reply' => true, 'export' => false, 'controlreadstatus' => false, 'canreplyprivately' => true, 'selfenrol' => false ], 'urls' => [ 'view' => $urlfactory->get_view_post_url_from_post_id( $discussion2firstpostobject->discussion, $discussion2firstpostobject->id)->out(false), 'viewisolated' => $isolatedurlparent->out(false), 'viewparent' => null, 'edit' => (new \moodle_url('/mod/forum/post.php', [ 'edit' => $discussion2firstpostobject->id ]))->out(false), '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' => $discussion2firstpostobject->id ]))->out(false), 'export' => null, 'markasread' => null, 'markasunread' => null, 'discuss' => $urlfactory->get_discussion_view_url_from_discussion_id( $discussion2firstpostobject->discussion)->out(false), ] ], ] ], ]; // 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); $this->assertEquals(2, count($discussions['discussions'])); $this->assertEquals($expectedposts, $discussions); // When groupmode is SEPARATEGROUPS, even there is no groupid specified, the post not for the user shouldn't be seen. $group1 = self::getDataGenerator()->create_group(['courseid' => $course1->id]); $group2 = self::getDataGenerator()->create_group(['courseid' => $course1->id]); // Update discussion with group. $discussion = new \stdClass(); $discussion->id = $discussion1->id; $discussion->groupid = $group1->id; $DB->update_record('forum_discussions', $discussion); $discussion = new \stdClass(); $discussion->id = $discussion2->id; $discussion->groupid = $group2->id; $DB->update_record('forum_discussions', $discussion); $cm = get_coursemodule_from_id('forum', $forum1->cmid); $cm->groupmode = SEPARATEGROUPS; $DB->update_record('course_modules', $cm); $teacher = self::getDataGenerator()->create_user(); $role = $DB->get_record('role', array('shortname' => 'teacher'), '*', MUST_EXIST); self::getDataGenerator()->enrol_user($teacher->id, $course1->id, $role->id); 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); // Discussion is only 1 record (group 2). $this->assertEquals(1, count($discussions['discussions'])); $this->assertEquals($expectedposts['discussions'][1], $discussions['discussions'][0]); } /** * Test get_discussion_post a discussion. */ public function test_get_discussion_post_discussion() { global $DB; $this->resetAfterTest(true); // Setup test data. $course = $this->getDataGenerator()->create_course(); $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id)); $user = $this->getDataGenerator()->create_user(); $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->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); $this->assertEquals($discussion->firstpost, $result['post']['id']); $this->assertFalse($result['post']['hasparent']); $this->assertEquals($discussion->message, $result['post']['message']); } /** * Test get_discussion_post a post. */ public function test_get_discussion_post_post() { global $DB; $this->resetAfterTest(true); // Setup test data. $course = $this->getDataGenerator()->create_course(); $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id)); $user = $this->getDataGenerator()->create_user(); $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->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->course = $course->id; $record->userid = $user->id; $record->forum = $forum->id; $record->discussion = $discussion->id; $record->parent = $parentpost->id; $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); $this->assertEquals($post->id, $result['post']['id']); $this->assertTrue($result['post']['hasparent']); $this->assertEquals($post->message, $result['post']['message']); } /** * Test get_discussion_post a different user post. */ public function test_get_discussion_post_other_user_post() { global $DB; $this->resetAfterTest(true); // Setup test data. $course = $this->getDataGenerator()->create_course(); $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id)); $user = $this->getDataGenerator()->create_user(); $otheruser = $this->getDataGenerator()->create_user(); $role = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST); self::getDataGenerator()->enrol_user($user->id, $course->id, $role->id); self::getDataGenerator()->enrol_user($otheruser->id, $course->id, $role->id); // Add a discussion. $record = array(); $record['course'] = $course->id; $record['forum'] = $forum->id; $record['userid'] = $user->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->course = $course->id; $record->userid = $user->id; $record->forum = $forum->id; $record->discussion = $discussion->id; $record->parent = $parentpost->id; $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); // 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); $this->assertEquals($post->id, $result['post']['id']); $this->assertTrue($result['post']['hasparent']); $this->assertEquals($post->message, $result['post']['message']); } /** * Test prepare_draft_area_for_post a different user post. */ public function test_prepare_draft_area_for_post() { global $DB; $this->resetAfterTest(true); // Setup test data. $course = $this->getDataGenerator()->create_course(); $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id)); $user = $this->getDataGenerator()->create_user(); $role = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST); self::getDataGenerator()->enrol_user($user->id, $course->id, $role->id); // Add a discussion. $record = array(); $record['course'] = $course->id; $record['forum'] = $forum->id; $record['userid'] = $user->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->course = $course->id; $record->userid = $user->id; $record->forum = $forum->id; $record->discussion = $discussion->id; $record->parent = $parentpost->id; $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); // Add some files only in the attachment area. $filename = 'faketxt.txt'; $filerecordinline = array( 'contextid' => \context_module::instance($forum->cmid)->id, 'component' => 'mod_forum', 'filearea' => 'attachment', 'itemid' => $post->id, 'filepath' => '/', 'filename' => $filename, ); $fs = get_file_storage(); $fs->create_file_from_string($filerecordinline, 'fake txt contents 1.'); $filerecordinline['filename'] = 'otherfaketxt.txt'; $fs->create_file_from_string($filerecordinline, 'fake txt contents 2.'); $this->setUser($user); // 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); $this->assertCount(2, $result['files']); $this->assertEquals($filename, $result['files'][0]['filename']); $this->assertCount(5, $result['areaoptions']); $this->assertEmpty($result['messagetext']); // 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); $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); $this->assertCount(1, $result['files']); $this->assertEquals($filename, $result['files'][0]['filename']); // Check editor (post) area. $filerecordinline['filearea'] = '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); $this->assertCount(1, $result['files']); $this->assertEquals($filerecordinline['filename'], $result['files'][0]['filename']); $this->assertCount(5, $result['areaoptions']); $this->assertEquals($post->message, $result['messagetext']); } /** * Test update_discussion_post with a discussion. */ public function test_update_discussion_post_discussion() { global $DB, $USER; $this->resetAfterTest(true); // Setup test data. $course = $this->getDataGenerator()->create_course(); $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id)); $this->setAdminUser(); // Add a discussion. $record = new \stdClass(); $record->course = $course->id; $record->userid = $USER->id; $record->forum = $forum->id; $record->pinned = FORUM_DISCUSSION_UNPINNED; $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); $subject = 'Hey subject updated'; $message = 'Hey message updated'; $messageformat = FORMAT_HTML; $options = [ ['name' => 'pinned', 'value' => true], ]; $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); $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); $this->assertEquals($subject, $result['post']['subject']); $this->assertEquals($message, $result['post']['message']); $this->assertEquals($messageformat, $result['post']['messageformat']); // Get discussion object from DB. $discussion = $DB->get_record('forum_discussions', ['id' => $discussion->id]); $this->assertEquals($subject, $discussion->name); // Check discussion subject. $this->assertEquals(FORUM_DISCUSSION_PINNED, $discussion->pinned); // Check discussion pinned. } /** * Test update_discussion_post with a post. */ public function test_update_discussion_post_post() { global $DB, $USER; $this->resetAfterTest(true); // Setup test data. $course = $this->getDataGenerator()->create_course(); $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id)); $cm = get_coursemodule_from_id('forum', $forum->cmid, 0, false, MUST_EXIST); $user = $this->getDataGenerator()->create_user(); $role = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST); self::getDataGenerator()->enrol_user($user->id, $course->id, $role->id); $this->setUser($user); // Enable auto subscribe discussion. $USER->autosubscribe = true; // Add a discussion. $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 via WS (so discussion subscription works). $result = mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...'); $newpost = $result['post']; $this->assertTrue(\mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussion->id, $cm)); // Test inline and regular attachment in post // Create a file in a draft area for inline attachments. $draftidinlineattach = file_get_unused_draft_itemid(); $draftidattach = file_get_unused_draft_itemid(); self::setUser($user); $usercontext = \context_user::instance($user->id); $filepath = '/'; $filearea = 'draft'; $component = 'user'; $filenameimg = 'fakeimage.png'; $filerecordinline = array( 'contextid' => $usercontext->id, 'component' => $component, 'filearea' => $filearea, 'itemid' => $draftidinlineattach, 'filepath' => $filepath, 'filename' => $filenameimg, ); $fs = get_file_storage(); // Create a file in a draft area for regular attachments. $filerecordattach = $filerecordinline; $attachfilename = 'faketxt.txt'; $filerecordattach['filename'] = $attachfilename; $filerecordattach['itemid'] = $draftidattach; $fs->create_file_from_string($filerecordinline, 'image contents (not really)'); $fs->create_file_from_string($filerecordattach, 'simple text attachment'); // Do not update subject. $message = 'Hey message updated'; $messageformat = FORMAT_HTML; $options = [ ['name' => 'discussionsubscribe', 'value' => false], ['name' => 'inlineattachmentsid', 'value' => $draftidinlineattach], ['name' => 'attachmentsid', 'value' => $draftidattach], ]; $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); $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); $found = false; foreach ($result['posts'] as $post) { if ($post['id'] == $newpost->id) { $this->assertEquals($newpost->subject, $post['subject']); $this->assertEquals($message, $post['message']); $this->assertEquals($messageformat, $post['messageformat']); $this->assertCount(1, $post['messageinlinefiles']); $this->assertEquals('fakeimage.png', $post['messageinlinefiles'][0]['filename']); $this->assertCount(1, $post['attachments']); $this->assertEquals('faketxt.txt', $post['attachments'][0]['filename']); $found = true; } } $this->assertTrue($found); } /** * Test update_discussion_post with other user post (no permissions). */ public function test_update_discussion_post_other_user_post() { global $DB, $USER; $this->resetAfterTest(true); // Setup test data. $course = $this->getDataGenerator()->create_course(); $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id)); $user = $this->getDataGenerator()->create_user(); $role = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST); self::getDataGenerator()->enrol_user($user->id, $course->id, $role->id); $this->setAdminUser(); // Add a discussion. $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->course = $course->id; $record->userid = $USER->id; $record->forum = $forum->id; $record->discussion = $discussion->id; $record->parent = $discussion->firstpost; $newpost = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); $this->setUser($user); $subject = 'Hey subject updated'; $message = 'Hey message updated'; $messageformat = FORMAT_HTML; $this->expectExceptionMessage(get_string('cannotupdatepost', 'forum')); mod_forum_external::update_discussion_post($newpost->id, $subject, $message, $messageformat); } /** * Test that we can update the subject of a post to the string '0' */ public function test_update_discussion_post_set_subject_to_zero(): void { global $DB, $USER; $this->resetAfterTest(true); $this->setAdminUser(); // Setup test data. $course = $this->getDataGenerator()->create_course(); $forum = $this->getDataGenerator()->create_module('forum', ['course' => $course->id]); $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion((object) [ 'userid' => $USER->id, 'course' => $course->id, 'forum' => $forum->id, 'name' => 'Test discussion subject', ]); // Update discussion post subject. $result = \external_api::clean_returnvalue( mod_forum_external::update_discussion_post_returns(), mod_forum_external::update_discussion_post($discussion->firstpost, '0') ); $this->assertTrue($result['status']); // Get updated discussion post subject from DB. $postsubject = $DB->get_field('forum_posts', 'subject', ['id' => $discussion->firstpost]); $this->assertEquals('0', $postsubject); } /** * Test that we can update the message of a post to the string '0' */ public function test_update_discussion_post_set_message_to_zero(): void { global $DB, $USER; $this->resetAfterTest(true); $this->setAdminUser(); // Setup test data. $course = $this->getDataGenerator()->create_course(); $forum = $this->getDataGenerator()->create_module('forum', ['course' => $course->id]); $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion((object) [ 'userid' => $USER->id, 'course' => $course->id, 'forum' => $forum->id, 'message' => 'Test discussion message', 'messageformat' => FORMAT_HTML, ]); // Update discussion post message. $result = \external_api::clean_returnvalue( mod_forum_external::update_discussion_post_returns(), mod_forum_external::update_discussion_post($discussion->firstpost, '', '0', FORMAT_HTML) ); $this->assertTrue($result['status']); // Get updated discussion post subject from DB. $postmessage = $DB->get_field('forum_posts', 'message', ['id' => $discussion->firstpost]); $this->assertEquals('0', $postmessage); } /** * Test that we can update the message format of a post to {@see FORMAT_MOODLE} */ public function test_update_discussion_post_set_message_format_moodle(): void { global $DB, $USER; $this->resetAfterTest(true); $this->setAdminUser(); // Setup test data. $course = $this->getDataGenerator()->create_course(); $forum = $this->getDataGenerator()->create_module('forum', ['course' => $course->id]); $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion((object) [ 'userid' => $USER->id, 'course' => $course->id, 'forum' => $forum->id, 'message' => 'Test discussion message', 'messageformat' => FORMAT_HTML, ]); // Update discussion post message & messageformat. $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) ); $this->assertTrue($result['status']); // Get updated discussion post from DB. $updatedpost = $DB->get_record('forum_posts', ['id' => $discussion->firstpost], 'message,messageformat'); $this->assertEquals((object) [ 'message' => 'Update discussion message', 'messageformat' => FORMAT_MOODLE, ], $updatedpost); } } home3/cpr76684/public_html/Aem/mod/imscp/tests/externallib_test.php 0000644 00000016244 15152164524 0021117 0 ustar 00 <?php // This file is part of Moodle - http://moodle.org/ // // Moodle is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // Moodle is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Moodle. If not, see <http://www.gnu.org/licenses/>. namespace mod_imscp; use externallib_advanced_testcase; use mod_imscp_external; defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->dirroot . '/webservice/tests/helpers.php'); /** * External mod_imscp functions unit tests * * @package mod_imscp * @category external * @copyright 2015 Juan Leyva <juan@moodle.com> * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 3.0 */ class externallib_test extends externallib_advanced_testcase { /** * Test view_imscp */ public function test_view_imscp() { global $DB; $this->resetAfterTest(true); $this->setAdminUser(); // Setup test data. $course = $this->getDataGenerator()->create_course(); $imscp = $this->getDataGenerator()->create_module('imscp', array('course' => $course->id)); $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) { $this->assertEquals('invalidrecord', $e->errorcode); } // Test not-enrolled user. $user = self::getDataGenerator()->create_user(); $this->setUser($user); try { mod_imscp_external::view_imscp($imscp->id); $this->fail('Exception expected due to not enrolled user.'); } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } // Test user with full capabilities. $studentrole = $DB->get_record('role', array('shortname' => 'student')); $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id); // Trigger and capture the event. $sink = $this->redirectEvents(); $result = mod_imscp_external::view_imscp($imscp->id); $result = \external_api::clean_returnvalue(mod_imscp_external::view_imscp_returns(), $result); $events = $sink->get_events(); $this->assertCount(1, $events); $event = array_shift($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\mod_imscp\event\course_module_viewed', $event); $this->assertEquals($context, $event->get_context()); $moodleurl = new \moodle_url('/mod/imscp/view.php', array('id' => $cm->id)); $this->assertEquals($moodleurl, $event->get_url()); $this->assertEventContextNotUsed($event); $this->assertNotEmpty($event->get_name()); // Test user with no capabilities. // We need a explicit prohibit since this capability is only defined in authenticated user and guest roles. 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(); try { mod_imscp_external::view_imscp($imscp->id); $this->fail('Exception expected due to missing capability.'); } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } } /** * Test get_imscps_by_courses */ public function test_get_imscps_by_courses() { global $DB, $USER; $this->resetAfterTest(true); // As admin. $this->setAdminUser(); $course1 = self::getDataGenerator()->create_course(); $imscpoptions1 = array( 'course' => $course1->id, 'name' => 'First IMSCP' ); $imscp1 = self::getDataGenerator()->create_module('imscp', $imscpoptions1); $course2 = self::getDataGenerator()->create_course(); $imscpoptions2 = array( 'course' => $course2->id, 'name' => 'Second IMSCP' ); $imscp2 = self::getDataGenerator()->create_module('imscp', $imscpoptions2); $student1 = $this->getDataGenerator()->create_user(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); // Enroll Student1 in Course1. self::getDataGenerator()->enrol_user($student1->id, $course1->id, $studentrole->id); $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); $this->assertCount(1, $imscps['imscps']); $this->assertEquals('First IMSCP', $imscps['imscps'][0]['name']); // As Student you cannot see some IMSCP properties like 'section'. $this->assertFalse(isset($imscps['imscps'][0]['section'])); // 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); $this->assertCount(0, $imscps['imscps']); $this->assertEquals(1, $imscps['warnings'][0]['warningcode']); // Now as admin. $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); $this->assertCount(1, $imscps['imscps']); $this->assertEquals('Second IMSCP', $imscps['imscps'][0]['name']); // As an Admin you can see some IMSCP properties like 'section'. $this->assertEquals(0, $imscps['imscps'][0]['section']); // Now, prohibit capabilities. $this->setUser($student1); $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(); $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); $this->assertCount(0, $imscps['imscps']); } } home3/cpr76684/public_html/Aem/mod/label/tests/externallib_test.php 0000644 00000014001 15152205126 0021042 0 ustar 00 <?php // This file is part of Moodle - http://moodle.org/ // // Moodle is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // Moodle is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Moodle. If not, see <http://www.gnu.org/licenses/>. namespace mod_label; use externallib_advanced_testcase; use mod_label_external; defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->dirroot . '/webservice/tests/helpers.php'); /** * External mod_label functions unit tests * * @package mod_label * @category external * @copyright 2017 Juan Leyva <juan@moodle.com> * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 3.3 */ class externallib_test extends externallib_advanced_testcase { /** * Test test_mod_label_get_labels_by_courses */ public function test_mod_label_get_labels_by_courses() { global $DB; $this->resetAfterTest(true); $course1 = self::getDataGenerator()->create_course(); $course2 = self::getDataGenerator()->create_course(); $student = self::getDataGenerator()->create_user(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id); // First label. $record = new \stdClass(); $record->course = $course1->id; $label1 = self::getDataGenerator()->create_module('label', $record); // Second label. $record = new \stdClass(); $record->course = $course2->id; $label2 = self::getDataGenerator()->create_module('label', $record); // Execute real Moodle enrolment as we'll call unenrol() method on the instance later. $enrol = enrol_get_plugin('manual'); $enrolinstances = enrol_get_instances($course2->id, true); foreach ($enrolinstances as $courseenrolinstance) { if ($courseenrolinstance->enrol == "manual") { $instance2 = $courseenrolinstance; break; } } $enrol->enrol_user($instance2, $student->id, $studentrole->id); self::setUser($student); $returndescription = mod_label_external::get_labels_by_courses_returns(); // Create what we expect to be returned when querying the two courses. $expectedfields = array('id', 'coursemodule', 'course', 'name', 'intro', 'introformat', 'introfiles', 'timemodified', 'section', 'visible', 'groupmode', 'groupingid', 'lang'); // Add expected coursemodule and data. $label1->coursemodule = $label1->cmid; $label1->introformat = 1; $label1->section = 0; $label1->visible = true; $label1->groupmode = 0; $label1->groupingid = 0; $label1->introfiles = []; $label1->lang = ''; $label2->coursemodule = $label2->cmid; $label2->introformat = 1; $label2->section = 0; $label2->visible = true; $label2->groupmode = 0; $label2->groupingid = 0; $label2->introfiles = []; $label2->lang = ''; foreach ($expectedfields as $field) { $expected1[$field] = $label1->{$field}; $expected2[$field] = $label2->{$field}; } $expectedlabels = array($expected2, $expected1); // 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); $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); $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, 'component' => 'mod_label', 'filearea' => 'intro', 'itemid' => 0, 'filepath' => '/', 'filename' => $filename, ); $fs = get_file_storage(); $timepost = time(); $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); $this->assertCount(1, $result['labels'][0]['introfiles']); $this->assertEquals($filename, $result['labels'][0]['introfiles'][0]['filename']); // Unenrol user from second course. $enrol->unenrol_user($instance2, $student->id); array_shift($expectedlabels); // Call the external function without passing course id. $result = mod_label_external::get_labels_by_courses(); $result = \external_api::clean_returnvalue($returndescription, $result); $this->assertEquals($expectedlabels, $result['labels']); // Call for the second course we unenrolled the user from, expected warning. $result = mod_label_external::get_labels_by_courses(array($course2->id)); $this->assertCount(1, $result['warnings']); $this->assertEquals('1', $result['warnings'][0]['warningcode']); $this->assertEquals($course2->id, $result['warnings'][0]['itemid']); } } home3/cpr76684/public_html/Aem/course/tests/externallib_test.php 0000644 00000554776 15152205777 0020554 0 ustar 00 <?php // This file is part of Moodle - http://moodle.org/ // // Moodle is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // Moodle is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Moodle. If not, see <http://www.gnu.org/licenses/>. /** * External course functions unit tests * * @package core_course * @category external * @copyright 2012 Jerome Mouneyrac * @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'); /** * External course functions unit tests * * @package core_course * @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 { //core_course_externallib_testcase /** * Tests set up */ protected function setUp(): void { global $CFG; require_once($CFG->dirroot . '/course/externallib.php'); } /** * Test create_categories */ public function test_create_categories() { global $DB; $this->resetAfterTest(true); // Set the required capabilities by the external function $contextid = context_system::instance()->id; $roleid = $this->assignUserCapability('moodle/category:manage', $contextid); // Create base categories. $category1 = new stdClass(); $category1->name = 'Root Test Category 1'; $category2 = new stdClass(); $category2->name = 'Root Test Category 2'; $category2->idnumber = 'rootcattest2'; $category2->desc = 'Description for root test category 1'; $category2->theme = 'classic'; $categories = array( array('name' => $category1->name, 'parent' => 0), array('name' => $category2->name, 'parent' => 0, 'idnumber' => $category2->idnumber, 'description' => $category2->desc, 'theme' => $category2->theme) ); $createdcats = core_course_external::create_categories($categories); // We need to execute the return values cleaning process to simulate the web service server. $createdcats = external_api::clean_returnvalue(core_course_external::create_categories_returns(), $createdcats); // Initially confirm that base data was inserted correctly. $this->assertEquals($category1->name, $createdcats[0]['name']); $this->assertEquals($category2->name, $createdcats[1]['name']); // Save the ids. $category1->id = $createdcats[0]['id']; $category2->id = $createdcats[1]['id']; // Create on sub category. $category3 = new stdClass(); $category3->name = 'Sub Root Test Category 3'; $subcategories = array( array('name' => $category3->name, 'parent' => $category1->id) ); $createdsubcats = core_course_external::create_categories($subcategories); // We need to execute the return values cleaning process to simulate the web service server. $createdsubcats = external_api::clean_returnvalue(core_course_external::create_categories_returns(), $createdsubcats); // Confirm that sub categories were inserted correctly. $this->assertEquals($category3->name, $createdsubcats[0]['name']); // Save the ids. $category3->id = $createdsubcats[0]['id']; // Calling the ws function should provide a new sortorder to give category1, // category2, category3. New course categories are ordered by id not name. $category1 = $DB->get_record('course_categories', array('id' => $category1->id)); $category2 = $DB->get_record('course_categories', array('id' => $category2->id)); $category3 = $DB->get_record('course_categories', array('id' => $category3->id)); // sortorder sequence (and sortorder) must be: // category 1 // category 3 // category 2 $this->assertGreaterThan($category1->sortorder, $category3->sortorder); $this->assertGreaterThan($category3->sortorder, $category2->sortorder); // Call without required capability $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid); $this->expectException('required_capability_exception'); $createdsubcats = core_course_external::create_categories($subcategories); } /** * Test delete categories */ public function test_delete_categories() { global $DB; $this->resetAfterTest(true); // Set the required capabilities by the external function $contextid = context_system::instance()->id; $roleid = $this->assignUserCapability('moodle/category:manage', $contextid); $category1 = self::getDataGenerator()->create_category(); $category2 = self::getDataGenerator()->create_category( array('parent' => $category1->id)); $category3 = self::getDataGenerator()->create_category(); $category4 = self::getDataGenerator()->create_category( array('parent' => $category3->id)); $category5 = self::getDataGenerator()->create_category( array('parent' => $category4->id)); //delete category 1 and 2 + delete category 4, category 5 moved under category 3 core_course_external::delete_categories(array( array('id' => $category1->id, 'recursive' => 1), array('id' => $category4->id) )); //check $category 1 and 2 are deleted $notdeletedcount = $DB->count_records_select('course_categories', 'id IN ( ' . $category1->id . ',' . $category2->id . ',' . $category4->id . ')'); $this->assertEquals(0, $notdeletedcount); //check that $category5 as $category3 for parent $dbcategory5 = $DB->get_record('course_categories', array('id' => $category5->id)); $this->assertEquals($dbcategory5->path, $category3->path . '/' . $category5->id); // Call without required capability $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid); $this->expectException('required_capability_exception'); $createdsubcats = core_course_external::delete_categories( array(array('id' => $category3->id))); } /** * Test get categories */ public function test_get_categories() { global $DB; $this->resetAfterTest(true); $generatedcats = array(); $category1data['idnumber'] = 'idnumbercat1'; $category1data['name'] = 'Category 1 for PHPunit test'; $category1data['description'] = 'Category 1 description'; $category1data['descriptionformat'] = FORMAT_MOODLE; $category1 = self::getDataGenerator()->create_category($category1data); $generatedcats[$category1->id] = $category1; $category2 = self::getDataGenerator()->create_category( array('parent' => $category1->id)); $generatedcats[$category2->id] = $category2; $category6 = self::getDataGenerator()->create_category( array('parent' => $category1->id, 'visible' => 0)); $generatedcats[$category6->id] = $category6; $category3 = self::getDataGenerator()->create_category(); $generatedcats[$category3->id] = $category3; $category4 = self::getDataGenerator()->create_category( array('parent' => $category3->id)); $generatedcats[$category4->id] = $category4; $category5 = self::getDataGenerator()->create_category( array('parent' => $category4->id)); $generatedcats[$category5->id] = $category5; // Set the required capabilities by the external function. $context = context_system::instance(); $roleid = $this->assignUserCapability('moodle/category:manage', $context->id); $this->assignUserCapability('moodle/category:viewhiddencategories', $context->id, $roleid); // Retrieve category1 + sub-categories except not visible ones $categories = core_course_external::get_categories(array( array('key' => 'id', 'value' => $category1->id), array('key' => 'visible', 'value' => 1)), 1); // We need to execute the return values cleaning process to simulate the web service server. $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories); // Check we retrieve the good total number of categories. $this->assertEquals(2, count($categories)); // Check the return values foreach ($categories as $category) { $generatedcat = $generatedcats[$category['id']]; $this->assertEquals($category['idnumber'], $generatedcat->idnumber); $this->assertEquals($category['name'], $generatedcat->name); // Description was converted to the HTML format. $this->assertEquals($category['description'], format_text($generatedcat->description, FORMAT_MOODLE, array('para' => false))); $this->assertEquals($category['descriptionformat'], FORMAT_HTML); } // Check categories by ids. $ids = implode(',', array_keys($generatedcats)); $categories = core_course_external::get_categories(array( array('key' => 'ids', 'value' => $ids)), 0); // We need to execute the return values cleaning process to simulate the web service server. $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories); // Check we retrieve the good total number of categories. $this->assertEquals(6, count($categories)); // Check ids. $returnedids = []; foreach ($categories as $category) { $returnedids[] = $category['id']; } // Sort the arrays upon comparision. $this->assertEqualsCanonicalizing(array_keys($generatedcats), $returnedids); // Check different params. $categories = core_course_external::get_categories(array( array('key' => 'id', 'value' => $category1->id), array('key' => 'ids', 'value' => $category1->id), array('key' => 'idnumber', 'value' => $category1->idnumber), array('key' => 'visible', 'value' => 1)), 0); // We need to execute the return values cleaning process to simulate the web service server. $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories); $this->assertEquals(1, count($categories)); // Same query, but forcing a parameters clean. $categories = core_course_external::get_categories(array( array('key' => 'id', 'value' => "$category1->id"), array('key' => 'idnumber', 'value' => $category1->idnumber), array('key' => 'name', 'value' => $category1->name . "<br/>"), array('key' => 'visible', 'value' => '1')), 0); $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories); $this->assertEquals(1, count($categories)); // Retrieve categories from parent. $categories = core_course_external::get_categories(array( array('key' => 'parent', 'value' => $category3->id)), 1); $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories); $this->assertEquals(2, count($categories)); // Retrieve all categories. $categories = core_course_external::get_categories(); // We need to execute the return values cleaning process to simulate the web service server. $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories); $this->assertEquals($DB->count_records('course_categories'), count($categories)); $this->unassignUserCapability('moodle/category:viewhiddencategories', $context->id, $roleid); // Ensure maxdepthcategory is 2 and retrieve all categories without category:viewhiddencategories capability. // It should retrieve all visible categories as well. set_config('maxcategorydepth', 2); $categories = core_course_external::get_categories(); // We need to execute the return values cleaning process to simulate the web service server. $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories); $this->assertEquals($DB->count_records('course_categories', array('visible' => 1)), count($categories)); // Call without required capability (it will fail cause of the search on idnumber). $this->expectException('moodle_exception'); $categories = core_course_external::get_categories(array( array('key' => 'id', 'value' => $category1->id), array('key' => 'idnumber', 'value' => $category1->idnumber), array('key' => 'visible', 'value' => 1)), 0); } /** * Test update_categories */ public function test_update_categories() { global $DB; $this->resetAfterTest(true); // Set the required capabilities by the external function $contextid = context_system::instance()->id; $roleid = $this->assignUserCapability('moodle/category:manage', $contextid); // Create base categories. $category1data['idnumber'] = 'idnumbercat1'; $category1data['name'] = 'Category 1 for PHPunit test'; $category1data['description'] = 'Category 1 description'; $category1data['descriptionformat'] = FORMAT_MOODLE; $category1 = self::getDataGenerator()->create_category($category1data); $category2 = self::getDataGenerator()->create_category( array('parent' => $category1->id)); $category3 = self::getDataGenerator()->create_category(); $category4 = self::getDataGenerator()->create_category( array('parent' => $category3->id)); $category5 = self::getDataGenerator()->create_category( array('parent' => $category4->id)); // We update all category1 attribut. // Then we move cat4 and cat5 parent: cat3 => cat1 $categories = array( array('id' => $category1->id, 'name' => $category1->name . '_updated', 'idnumber' => $category1->idnumber . '_updated', 'description' => $category1->description . '_updated', 'descriptionformat' => FORMAT_HTML, 'theme' => $category1->theme), array('id' => $category4->id, 'parent' => $category1->id)); core_course_external::update_categories($categories); // Check the values were updated. $dbcategories = $DB->get_records_select('course_categories', 'id IN (' . $category1->id . ',' . $category2->id . ',' . $category2->id . ',' . $category3->id . ',' . $category4->id . ',' . $category5->id .')'); $this->assertEquals($category1->name . '_updated', $dbcategories[$category1->id]->name); $this->assertEquals($category1->idnumber . '_updated', $dbcategories[$category1->id]->idnumber); $this->assertEquals($category1->description . '_updated', $dbcategories[$category1->id]->description); $this->assertEquals(FORMAT_HTML, $dbcategories[$category1->id]->descriptionformat); // Check that category4 and category5 have been properly moved. $this->assertEquals('/' . $category1->id . '/' . $category4->id, $dbcategories[$category4->id]->path); $this->assertEquals('/' . $category1->id . '/' . $category4->id . '/' . $category5->id, $dbcategories[$category5->id]->path); // Call without required capability. $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid); $this->expectException('required_capability_exception'); core_course_external::update_categories($categories); } /** * Test create_courses numsections */ public function test_create_course_numsections() { global $DB; $this->resetAfterTest(true); // Set the required capabilities by the external function. $contextid = context_system::instance()->id; $roleid = $this->assignUserCapability('moodle/course:create', $contextid); $this->assignUserCapability('moodle/course:visibility', $contextid, $roleid); $numsections = 10; $category = self::getDataGenerator()->create_category(); // Create base categories. $course1['fullname'] = 'Test course 1'; $course1['shortname'] = 'Testcourse1'; $course1['categoryid'] = $category->id; $course1['courseformatoptions'][] = array('name' => 'numsections', 'value' => $numsections); $courses = array($course1); $createdcourses = core_course_external::create_courses($courses); foreach ($createdcourses as $createdcourse) { $existingsections = $DB->get_records('course_sections', array('course' => $createdcourse['id'])); $modinfo = get_fast_modinfo($createdcourse['id']); $sections = $modinfo->get_section_info_all(); $this->assertEquals(count($sections), $numsections + 1); // Includes generic section. $this->assertEquals(count($existingsections), $numsections + 1); // Includes generic section. } } /** * Test create_courses */ public function test_create_courses() { global $DB; $this->resetAfterTest(true); // Enable course completion. set_config('enablecompletion', 1); // Enable course themes. set_config('allowcoursethemes', 1); // Custom fields. $fieldcategory = self::getDataGenerator()->create_custom_field_category(['name' => 'Other fields']); $customfield = ['shortname' => 'test', 'name' => 'Custom field', 'type' => 'text', 'categoryid' => $fieldcategory->get('id'), 'configdata' => ['visibility' => \core_course\customfield\course_handler::VISIBLETOALL]]; $field = self::getDataGenerator()->create_custom_field($customfield); // Set the required capabilities by the external function $contextid = context_system::instance()->id; $roleid = $this->assignUserCapability('moodle/course:create', $contextid); $this->assignUserCapability('moodle/course:visibility', $contextid, $roleid); $this->assignUserCapability('moodle/course:setforcedlanguage', $contextid, $roleid); $category = self::getDataGenerator()->create_category(); // Create base categories. $course1['fullname'] = 'Test course 1'; $course1['shortname'] = 'Testcourse1'; $course1['categoryid'] = $category->id; $course2['fullname'] = 'Test course 2'; $course2['shortname'] = 'Testcourse2'; $course2['categoryid'] = $category->id; $course2['idnumber'] = 'testcourse2idnumber'; $course2['summary'] = 'Description for course 2'; $course2['summaryformat'] = FORMAT_MOODLE; $course2['format'] = 'weeks'; $course2['showgrades'] = 1; $course2['newsitems'] = 3; $course2['startdate'] = 1420092000; // 01/01/2015. $course2['enddate'] = 1422669600; // 01/31/2015. $course2['numsections'] = 4; $course2['maxbytes'] = 100000; $course2['showreports'] = 1; $course2['visible'] = 0; $course2['hiddensections'] = 0; $course2['groupmode'] = 0; $course2['groupmodeforce'] = 0; $course2['defaultgroupingid'] = 0; $course2['enablecompletion'] = 1; $course2['completionnotify'] = 1; $course2['lang'] = 'en'; $course2['forcetheme'] = 'classic'; $course2['courseformatoptions'][] = array('name' => 'automaticenddate', 'value' => 0); $course3['fullname'] = 'Test course 3'; $course3['shortname'] = 'Testcourse3'; $course3['categoryid'] = $category->id; $course3['format'] = 'topics'; $course3options = array('numsections' => 8, 'hiddensections' => 1, 'coursedisplay' => 1); $course3['courseformatoptions'] = array(); foreach ($course3options as $key => $value) { $course3['courseformatoptions'][] = array('name' => $key, 'value' => $value); } $course4['fullname'] = 'Test course with custom fields'; $course4['shortname'] = 'Testcoursecustomfields'; $course4['categoryid'] = $category->id; $course4['customfields'] = [['shortname' => $customfield['shortname'], 'value' => 'Test value']]; $courses = array($course4, $course1, $course2, $course3); $createdcourses = core_course_external::create_courses($courses); // We need to execute the return values cleaning process to simulate the web service server. $createdcourses = external_api::clean_returnvalue(core_course_external::create_courses_returns(), $createdcourses); // Check that right number of courses were created. $this->assertEquals(4, count($createdcourses)); // Check that the courses were correctly created. foreach ($createdcourses as $createdcourse) { $courseinfo = course_get_format($createdcourse['id'])->get_course(); if ($createdcourse['shortname'] == $course2['shortname']) { $this->assertEquals($courseinfo->fullname, $course2['fullname']); $this->assertEquals($courseinfo->shortname, $course2['shortname']); $this->assertEquals($courseinfo->category, $course2['categoryid']); $this->assertEquals($courseinfo->idnumber, $course2['idnumber']); $this->assertEquals($courseinfo->summary, $course2['summary']); $this->assertEquals($courseinfo->summaryformat, $course2['summaryformat']); $this->assertEquals($courseinfo->format, $course2['format']); $this->assertEquals($courseinfo->showgrades, $course2['showgrades']); $this->assertEquals($courseinfo->newsitems, $course2['newsitems']); $this->assertEquals($courseinfo->startdate, $course2['startdate']); $this->assertEquals($courseinfo->enddate, $course2['enddate']); $this->assertEquals(course_get_format($createdcourse['id'])->get_last_section_number(), $course2['numsections']); $this->assertEquals($courseinfo->maxbytes, $course2['maxbytes']); $this->assertEquals($courseinfo->showreports, $course2['showreports']); $this->assertEquals($courseinfo->visible, $course2['visible']); $this->assertEquals($courseinfo->hiddensections, $course2['hiddensections']); $this->assertEquals($courseinfo->groupmode, $course2['groupmode']); $this->assertEquals($courseinfo->groupmodeforce, $course2['groupmodeforce']); $this->assertEquals($courseinfo->defaultgroupingid, $course2['defaultgroupingid']); $this->assertEquals($courseinfo->completionnotify, $course2['completionnotify']); $this->assertEquals($courseinfo->lang, $course2['lang']); $this->assertEquals($courseinfo->theme, $course2['forcetheme']); // We enabled completion at the beginning of the test. $this->assertEquals($courseinfo->enablecompletion, $course2['enablecompletion']); } else if ($createdcourse['shortname'] == $course1['shortname']) { $courseconfig = get_config('moodlecourse'); $this->assertEquals($courseinfo->fullname, $course1['fullname']); $this->assertEquals($courseinfo->shortname, $course1['shortname']); $this->assertEquals($courseinfo->category, $course1['categoryid']); $this->assertEquals($courseinfo->summaryformat, FORMAT_HTML); $this->assertEquals($courseinfo->format, $courseconfig->format); $this->assertEquals($courseinfo->showgrades, $courseconfig->showgrades); $this->assertEquals($courseinfo->newsitems, $courseconfig->newsitems); $this->assertEquals($courseinfo->maxbytes, $courseconfig->maxbytes); $this->assertEquals($courseinfo->showreports, $courseconfig->showreports); $this->assertEquals($courseinfo->groupmode, $courseconfig->groupmode); $this->assertEquals($courseinfo->groupmodeforce, $courseconfig->groupmodeforce); $this->assertEquals($courseinfo->defaultgroupingid, 0); } else if ($createdcourse['shortname'] == $course3['shortname']) { $this->assertEquals($courseinfo->fullname, $course3['fullname']); $this->assertEquals($courseinfo->shortname, $course3['shortname']); $this->assertEquals($courseinfo->category, $course3['categoryid']); $this->assertEquals($courseinfo->format, $course3['format']); $this->assertEquals($courseinfo->hiddensections, $course3options['hiddensections']); $this->assertEquals(course_get_format($createdcourse['id'])->get_last_section_number(), $course3options['numsections']); $this->assertEquals($courseinfo->coursedisplay, $course3options['coursedisplay']); } else if ($createdcourse['shortname'] == $course4['shortname']) { $this->assertEquals($courseinfo->fullname, $course4['fullname']); $this->assertEquals($courseinfo->shortname, $course4['shortname']); $this->assertEquals($courseinfo->category, $course4['categoryid']); $handler = core_course\customfield\course_handler::create(); $customfields = $handler->export_instance_data_object($createdcourse['id']); $this->assertEquals((object)['test' => 'Test value'], $customfields); } else { throw new moodle_exception('Unexpected shortname'); } } // Call without required capability $this->unassignUserCapability('moodle/course:create', $contextid, $roleid); $this->expectException('required_capability_exception'); $createdsubcats = core_course_external::create_courses($courses); } /** * Data provider for testing empty fields produce expected exceptions * * @see test_create_courses_empty_field * @see test_update_courses_empty_field * * @return array */ public function course_empty_field_provider(): array { return [ [[ 'fullname' => '', 'shortname' => 'ws101', ], 'fullname'], [[ 'fullname' => ' ', 'shortname' => 'ws101', ], 'fullname'], [[ 'fullname' => 'Web Services', 'shortname' => '', ], 'shortname'], [[ 'fullname' => 'Web Services', 'shortname' => ' ', ], 'shortname'], ]; } /** * Test creating courses with empty fields throws an exception * * @param array $course * @param string $expectedemptyfield * * @dataProvider course_empty_field_provider */ public function test_create_courses_empty_field(array $course, string $expectedemptyfield): void { $this->resetAfterTest(); $this->setAdminUser(); // Create a category for the new course. $course['categoryid'] = $this->getDataGenerator()->create_category()->id; $this->expectException(moodle_exception::class); $this->expectExceptionMessageMatches("/{$expectedemptyfield}/"); core_course_external::create_courses([$course]); } /** * Test updating courses with empty fields returns warnings * * @param array $course * @param string $expectedemptyfield * * @dataProvider course_empty_field_provider */ public function test_update_courses_empty_field(array $course, string $expectedemptyfield): void { $this->resetAfterTest(); $this->setAdminUser(); // Create a course to update. $course['id'] = $this->getDataGenerator()->create_course()->id; $result = core_course_external::update_courses([$course]); $result = core_course_external::clean_returnvalue(core_course_external::update_courses_returns(), $result); $this->assertCount(1, $result['warnings']); $warning = reset($result['warnings']); $this->assertEquals('errorinvalidparam', $warning['warningcode']); $this->assertStringContainsString($expectedemptyfield, $warning['message']); } /** * Test delete_courses */ public function test_delete_courses() { global $DB, $USER; $this->resetAfterTest(true); // Admin can delete a course. $this->setAdminUser(); // Validate_context() will fail as the email is not set by $this->setAdminUser(). $USER->email = 'emailtopass@example.com'; $course1 = self::getDataGenerator()->create_course(); $course2 = self::getDataGenerator()->create_course(); $course3 = self::getDataGenerator()->create_course(); // Delete courses. $result = core_course_external::delete_courses(array($course1->id, $course2->id)); $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result); // Check for 0 warnings. $this->assertEquals(0, count($result['warnings'])); // Check $course 1 and 2 are deleted. $notdeletedcount = $DB->count_records_select('course', 'id IN ( ' . $course1->id . ',' . $course2->id . ')'); $this->assertEquals(0, $notdeletedcount); // Try to delete non-existent course. $result = core_course_external::delete_courses(array($course1->id)); $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result); // Check for 1 warnings. $this->assertEquals(1, count($result['warnings'])); // Try to delete Frontpage course. $result = core_course_external::delete_courses(array(0)); $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result); // Check for 1 warnings. $this->assertEquals(1, count($result['warnings'])); // Fail when the user has access to course (enrolled) but does not have permission or is not admin. $student1 = self::getDataGenerator()->create_user(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $this->getDataGenerator()->enrol_user($student1->id, $course3->id, $studentrole->id); $this->setUser($student1); $result = core_course_external::delete_courses(array($course3->id)); $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result); // Check for 1 warnings. $this->assertEquals(1, count($result['warnings'])); // Fail when the user is not allow to access the course (enrolled) or is not admin. $this->setGuestUser(); $this->expectException('require_login_exception'); $result = core_course_external::delete_courses(array($course3->id)); $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result); } /** * Test get_courses */ public function test_get_courses () { global $DB; $this->resetAfterTest(true); $generatedcourses = array(); $coursedata['idnumber'] = 'idnumbercourse1'; // Adding tags here to check that format_string is applied. $coursedata['fullname'] = '<b>Course 1 for PHPunit test</b>'; $coursedata['shortname'] = '<b>Course 1 for PHPunit test</b>'; $coursedata['summary'] = 'Course 1 description'; $coursedata['summaryformat'] = FORMAT_MOODLE; $course1 = self::getDataGenerator()->create_course($coursedata); $fieldcategory = self::getDataGenerator()->create_custom_field_category( ['name' => 'Other fields']); $customfield = ['shortname' => 'test', 'name' => 'Custom field', 'type' => 'text', 'categoryid' => $fieldcategory->get('id')]; $field = self::getDataGenerator()->create_custom_field($customfield); $customfieldvalue = ['shortname' => 'test', 'value' => 'Test value']; $generatedcourses[$course1->id] = $course1; $course2 = self::getDataGenerator()->create_course(); $generatedcourses[$course2->id] = $course2; $course3 = self::getDataGenerator()->create_course(array('format' => 'topics')); $generatedcourses[$course3->id] = $course3; $course4 = self::getDataGenerator()->create_course(['customfields' => [$customfieldvalue]]); $generatedcourses[$course4->id] = $course4; // Set the required capabilities by the external function. $context = context_system::instance(); $roleid = $this->assignUserCapability('moodle/course:view', $context->id); $this->assignUserCapability('moodle/course:update', context_course::instance($course1->id)->id, $roleid); $this->assignUserCapability('moodle/course:update', context_course::instance($course2->id)->id, $roleid); $this->assignUserCapability('moodle/course:update', context_course::instance($course3->id)->id, $roleid); $this->assignUserCapability('moodle/course:update', context_course::instance($course4->id)->id, $roleid); $courses = core_course_external::get_courses(array('ids' => array($course1->id, $course2->id, $course4->id))); // We need to execute the return values cleaning process to simulate the web service server. $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses); // Check we retrieve the good total number of courses. $this->assertEquals(3, count($courses)); foreach ($courses as $course) { $coursecontext = context_course::instance($course['id']); $dbcourse = $generatedcourses[$course['id']]; $this->assertEquals($course['idnumber'], $dbcourse->idnumber); $this->assertEquals($course['fullname'], external_format_string($dbcourse->fullname, $coursecontext->id)); $this->assertEquals($course['displayname'], external_format_string(get_course_display_name_for_list($dbcourse), $coursecontext->id)); // Summary was converted to the HTML format. $this->assertEquals($course['summary'], format_text($dbcourse->summary, FORMAT_MOODLE, array('para' => false))); $this->assertEquals($course['summaryformat'], FORMAT_HTML); $this->assertEquals($course['shortname'], external_format_string($dbcourse->shortname, $coursecontext->id)); $this->assertEquals($course['categoryid'], $dbcourse->category); $this->assertEquals($course['format'], $dbcourse->format); $this->assertEquals($course['showgrades'], $dbcourse->showgrades); $this->assertEquals($course['newsitems'], $dbcourse->newsitems); $this->assertEquals($course['startdate'], $dbcourse->startdate); $this->assertEquals($course['enddate'], $dbcourse->enddate); $this->assertEquals($course['numsections'], course_get_format($dbcourse)->get_last_section_number()); $this->assertEquals($course['maxbytes'], $dbcourse->maxbytes); $this->assertEquals($course['showreports'], $dbcourse->showreports); $this->assertEquals($course['visible'], $dbcourse->visible); $this->assertEquals($course['hiddensections'], $dbcourse->hiddensections); $this->assertEquals($course['groupmode'], $dbcourse->groupmode); $this->assertEquals($course['groupmodeforce'], $dbcourse->groupmodeforce); $this->assertEquals($course['defaultgroupingid'], $dbcourse->defaultgroupingid); $this->assertEquals($course['completionnotify'], $dbcourse->completionnotify); $this->assertEquals($course['lang'], $dbcourse->lang); $this->assertEquals($course['forcetheme'], $dbcourse->theme); $this->assertEquals($course['enablecompletion'], $dbcourse->enablecompletion); if ($dbcourse->format === 'topics') { $this->assertEquals($course['courseformatoptions'], array( array('name' => 'hiddensections', 'value' => $dbcourse->hiddensections), array('name' => 'coursedisplay', 'value' => $dbcourse->coursedisplay), )); } // Assert custom field that we previously added to test course 4. if ($dbcourse->id == $course4->id) { $this->assertEquals([ 'shortname' => $customfield['shortname'], 'name' => $customfield['name'], 'type' => $customfield['type'], 'value' => $customfieldvalue['value'], 'valueraw' => $customfieldvalue['value'], ], $course['customfields'][0]); } } // Get all courses in the DB $courses = core_course_external::get_courses(array()); // We need to execute the return values cleaning process to simulate the web service server. $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses); $this->assertEquals($DB->count_records('course'), count($courses)); } /** * Test retrieving courses returns custom field data */ public function test_get_courses_customfields(): void { $this->resetAfterTest(); $this->setAdminUser(); $fieldcategory = $this->getDataGenerator()->create_custom_field_category([]); $datefield = $this->getDataGenerator()->create_custom_field([ 'categoryid' => $fieldcategory->get('id'), 'shortname' => 'mydate', 'name' => 'My date', 'type' => 'date', ]); $newcourse = $this->getDataGenerator()->create_course(['customfields' => [ [ 'shortname' => $datefield->get('shortname'), 'value' => 1580389200, // 30/01/2020 13:00 GMT. ], ]]); $courses = external_api::clean_returnvalue( core_course_external::get_courses_returns(), core_course_external::get_courses(['ids' => [$newcourse->id]]) ); $this->assertCount(1, $courses); $course = reset($courses); $this->assertArrayHasKey('customfields', $course); $this->assertCount(1, $course['customfields']); // Assert the received custom field, "value" containing a human-readable version and "valueraw" the unmodified version. $this->assertEquals([ 'name' => $datefield->get('name'), 'shortname' => $datefield->get('shortname'), 'type' => $datefield->get('type'), 'value' => userdate(1580389200), 'valueraw' => 1580389200, ], reset($course['customfields'])); } /** * Test get_courses without capability */ public function test_get_courses_without_capability() { $this->resetAfterTest(true); $course1 = $this->getDataGenerator()->create_course(); $this->setUser($this->getDataGenerator()->create_user()); // No permissions are required to get the site course. $courses = core_course_external::get_courses(array('ids' => [SITEID])); $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses); $this->assertEquals(1, count($courses)); $this->assertEquals('PHPUnit test site', $courses[0]['fullname']); $this->assertEquals('site', $courses[0]['format']); // Requesting course without being enrolled or capability to view it will throw an exception. try { core_course_external::get_courses(array('ids' => [$course1->id])); $this->fail('Exception expected'); } catch (moodle_exception $e) { $this->assertEquals(1, preg_match('/Course or activity not accessible. \(Not enrolled\)/', $e->getMessage())); } } /** * Test search_courses */ public function test_search_courses () { global $DB; $this->resetAfterTest(true); $this->setAdminUser(); $generatedcourses = array(); $coursedata1['fullname'] = 'FIRST COURSE'; $course1 = self::getDataGenerator()->create_course($coursedata1); $page = new moodle_page(); $page->set_course($course1); $page->blocks->add_blocks([BLOCK_POS_LEFT => ['news_items'], BLOCK_POS_RIGHT => []], 'course-view-*'); $coursedata2['fullname'] = 'SECOND COURSE'; $course2 = self::getDataGenerator()->create_course($coursedata2); $page = new moodle_page(); $page->set_course($course2); $page->blocks->add_blocks([BLOCK_POS_LEFT => ['news_items'], BLOCK_POS_RIGHT => []], 'course-view-*'); // Search by name. $results = core_course_external::search_courses('search', 'FIRST'); $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results); $this->assertEquals($coursedata1['fullname'], $results['courses'][0]['fullname']); $this->assertCount(1, $results['courses']); // Create the forum. $record = new stdClass(); $record->introformat = FORMAT_HTML; $record->course = $course2->id; // Set Aggregate type = Average of ratings. $forum = self::getDataGenerator()->create_module('forum', $record); // Search by module. $results = core_course_external::search_courses('modulelist', 'forum'); $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results); $this->assertEquals(1, $results['total']); // Enable coursetag option. set_config('block_tags_showcoursetags', true); // Add tag 'TAG-LABEL ON SECOND COURSE' to Course2. core_tag_tag::set_item_tags('core', 'course', $course2->id, context_course::instance($course2->id), array('TAG-LABEL ON SECOND COURSE')); $taginstance = $DB->get_record('tag_instance', array('itemtype' => 'course', 'itemid' => $course2->id), '*', MUST_EXIST); // Search by tagid. $results = core_course_external::search_courses('tagid', $taginstance->tagid); $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results); $this->assertEquals($coursedata2['fullname'], $results['courses'][0]['fullname']); // Search by block (use news_items default block). $blockid = $DB->get_field('block', 'id', array('name' => 'news_items')); $results = core_course_external::search_courses('blocklist', $blockid); $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results); $this->assertEquals(2, $results['total']); // Now as a normal user. $user = self::getDataGenerator()->create_user(); // Add a 3rd, hidden, course we shouldn't see, even when enrolled as student. $coursedata3['fullname'] = 'HIDDEN COURSE'; $coursedata3['visible'] = 0; $course3 = self::getDataGenerator()->create_course($coursedata3); $this->getDataGenerator()->enrol_user($user->id, $course3->id, 'student'); $this->getDataGenerator()->enrol_user($user->id, $course2->id, 'student'); $this->setUser($user); $results = core_course_external::search_courses('search', 'FIRST'); $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results); $this->assertCount(1, $results['courses']); $this->assertEquals(1, $results['total']); $this->assertEquals($coursedata1['fullname'], $results['courses'][0]['fullname']); // Check that we can see all courses without the limit to enrolled setting. $results = core_course_external::search_courses('search', 'COURSE', 0, 0, array(), 0); $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results); $this->assertCount(2, $results['courses']); $this->assertEquals(2, $results['total']); // Check that we only see our enrolled course when limiting. $results = core_course_external::search_courses('search', 'COURSE', 0, 0, array(), 1); $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results); $this->assertCount(1, $results['courses']); $this->assertEquals(1, $results['total']); $this->assertEquals($coursedata2['fullname'], $results['courses'][0]['fullname']); // Search by block (use news_items default block). Should fail (only admins allowed). $this->expectException('required_capability_exception'); $results = core_course_external::search_courses('blocklist', $blockid); } /** * Test searching for courses returns custom field data */ public function test_search_courses_customfields(): void { $this->resetAfterTest(); $this->setAdminUser(); $fieldcategory = $this->getDataGenerator()->create_custom_field_category([]); $datefield = $this->getDataGenerator()->create_custom_field([ 'categoryid' => $fieldcategory->get('id'), 'shortname' => 'mydate', 'name' => 'My date', 'type' => 'date', ]); $newcourse = $this->getDataGenerator()->create_course(['customfields' => [ [ 'shortname' => $datefield->get('shortname'), 'value' => 1580389200, // 30/01/2020 13:00 GMT. ], ]]); $result = external_api::clean_returnvalue( core_course_external::search_courses_returns(), core_course_external::search_courses('search', $newcourse->shortname) ); $this->assertCount(1, $result['courses']); $course = reset($result['courses']); $this->assertArrayHasKey('customfields', $course); $this->assertCount(1, $course['customfields']); // Assert the received custom field, "value" containing a human-readable version and "valueraw" the unmodified version. $this->assertEquals([ 'name' => $datefield->get('name'), 'shortname' => $datefield->get('shortname'), 'type' => $datefield->get('type'), 'value' => userdate(1580389200), 'valueraw' => 1580389200, ], reset($course['customfields'])); } /** * Create a course with contents * @return array A list with the course object and course modules objects */ private function prepare_get_course_contents_test() { global $DB, $CFG; $CFG->allowstealth = 1; // Allow stealth activities. $CFG->enablecompletion = true; // Course with 4 sections (apart from the main section), with completion and not displaying hidden sections. $course = self::getDataGenerator()->create_course(['numsections' => 4, 'enablecompletion' => 1, 'hiddensections' => 1]); $forumdescription = 'This is the forum description'; $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'intro' => $forumdescription, 'trackingtype' => 2), array('showdescription' => true, 'completion' => COMPLETION_TRACKING_MANUAL)); $forumcm = get_coursemodule_from_id('forum', $forum->cmid); // Add discussions to the tracking forced forum. $record = new stdClass(); $record->course = $course->id; $record->userid = 0; $record->forum = $forum->id; $discussionforce = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); $data = $this->getDataGenerator()->create_module('data', array('assessed' => 1, 'scale' => 100, 'course' => $course->id, 'completion' => 2, 'completionentries' => 3)); $datacm = get_coursemodule_from_instance('data', $data->id); $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id)); $pagecm = get_coursemodule_from_instance('page', $page->id); // This is an stealth page (set by visibleoncoursepage). $pagestealth = $this->getDataGenerator()->create_module('page', array('course' => $course->id, 'visibleoncoursepage' => 0)); $labeldescription = 'This is a very long label to test if more than 50 characters are returned. So bla bla bla bla <b>bold bold bold</b> bla bla bla bla.'; $label = $this->getDataGenerator()->create_module('label', array('course' => $course->id, 'intro' => $labeldescription, 'completion' => COMPLETION_TRACKING_MANUAL)); $labelcm = get_coursemodule_from_instance('label', $label->id); $tomorrow = time() + DAYSECS; // Module with availability restrictions not met. $availability = '{"op":"&","c":[{"type":"date","d":">=","t":' . $tomorrow . '},' .'{"type":"completion","cm":' . $label->cmid .',"e":1}],"showc":[true,true]}'; $url = $this->getDataGenerator()->create_module('url', array('course' => $course->id, 'name' => 'URL: % & $ ../', 'section' => 2, 'display' => RESOURCELIB_DISPLAY_POPUP, 'popupwidth' => 100, 'popupheight' => 100), array('availability' => $availability)); $urlcm = get_coursemodule_from_instance('url', $url->id); // Module for the last section. $this->getDataGenerator()->create_module('url', array('course' => $course->id, 'name' => 'URL for last section', 'section' => 3)); // Module for section 1 with availability restrictions met. $yesterday = time() - DAYSECS; $this->getDataGenerator()->create_module('url', array('course' => $course->id, 'name' => 'URL restrictions met', 'section' => 1), array('availability' => '{"op":"&","c":[{"type":"date","d":">=","t":'. $yesterday .'}],"showc":[true]}')); // Set the required capabilities by the external function. $context = context_course::instance($course->id); $roleid = $this->assignUserCapability('moodle/course:view', $context->id); $this->assignUserCapability('moodle/course:update', $context->id, $roleid); $this->assignUserCapability('mod/data:view', $context->id, $roleid); $conditions = array('course' => $course->id, 'section' => 2); $DB->set_field('course_sections', 'summary', 'Text with iframe <iframe src="https://moodle.org"></iframe>', $conditions); // Add date availability condition not met for section 3. $availability = '{"op":"&","c":[{"type":"date","d":">=","t":' . $tomorrow . '}],"showc":[true]}'; $DB->set_field('course_sections', 'availability', $availability, array('course' => $course->id, 'section' => 3)); // Create resource for last section. $pageinhiddensection = $this->getDataGenerator()->create_module('page', array('course' => $course->id, 'name' => 'Page in hidden section', 'section' => 4)); // Set not visible last section. $DB->set_field('course_sections', 'visible', 0, array('course' => $course->id, 'section' => 4)); $forumcompleteauto = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'intro' => 'forum completion tracking auto', 'trackingtype' => 2), array('showdescription' => true, 'completionview' => 1, 'completion' => COMPLETION_TRACKING_AUTOMATIC)); $forumcompleteautocm = get_coursemodule_from_id('forum', $forumcompleteauto->cmid); $sectionrecord = $DB->get_record('course_sections', $conditions); // Invalidate the section cache by given section number. course_modinfo::purge_course_section_cache_by_number($sectionrecord->course, $sectionrecord->section); rebuild_course_cache($course->id, true, true); return array($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm, $forumcompleteautocm); } /** * Test get_course_contents */ public function test_get_course_contents() { global $CFG; $this->resetAfterTest(true); $CFG->forum_allowforcedreadtracking = 1; list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test(); // We first run the test as admin. $this->setAdminUser(); $sections = core_course_external::get_course_contents($course->id, array()); // We need to execute the return values cleaning process to simulate the web service server. $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections); $modinfo = get_fast_modinfo($course); $testexecuted = 0; foreach ($sections[0]['modules'] as $module) { if ($module['id'] == $forumcm->id and $module['modname'] == 'forum') { $cm = $modinfo->cms[$forumcm->id]; $formattedtext = format_text($cm->content, FORMAT_HTML, array('noclean' => true, 'para' => false, 'filter' => false)); $this->assertEquals($formattedtext, $module['description']); $this->assertEquals($forumcm->instance, $module['instance']); $this->assertEquals(context_module::instance($forumcm->id)->id, $module['contextid']); $this->assertStringContainsString('1 unread post', $module['afterlink']); $this->assertFalse($module['noviewlink']); $this->assertNotEmpty($module['description']); // Module showdescription is on. $testexecuted = $testexecuted + 2; } else if ($module['id'] == $labelcm->id and $module['modname'] == 'label') { $cm = $modinfo->cms[$labelcm->id]; $formattedtext = format_text($cm->content, FORMAT_HTML, array('noclean' => true, 'para' => false, 'filter' => false)); $this->assertEquals($formattedtext, $module['description']); $this->assertEquals($labelcm->instance, $module['instance']); $this->assertEquals(context_module::instance($labelcm->id)->id, $module['contextid']); $this->assertTrue($module['noviewlink']); $this->assertNotEmpty($module['description']); // Label always prints the description. $testexecuted = $testexecuted + 1; } else if ($module['id'] == $datacm->id and $module['modname'] == 'data') { $this->assertStringContainsString('customcompletionrules', $module['customdata']); $this->assertFalse($module['noviewlink']); $this->assertArrayNotHasKey('description', $module); $testexecuted = $testexecuted + 1; } } foreach ($sections[2]['modules'] as $module) { if ($module['id'] == $urlcm->id and $module['modname'] == 'url') { $this->assertStringContainsString('width=100,height=100', $module['onclick']); $testexecuted = $testexecuted + 1; } } $CFG->forum_allowforcedreadtracking = 0; // Recover original value. forum_tp_count_forum_unread_posts($forumcm, $course, true); // Reset static cache for further tests. $this->assertEquals(5, $testexecuted); $this->assertEquals(0, $sections[0]['section']); $this->assertCount(6, $sections[0]['modules']); $this->assertCount(1, $sections[1]['modules']); $this->assertCount(1, $sections[2]['modules']); $this->assertCount(1, $sections[3]['modules']); // One module for the section with availability restrictions. $this->assertCount(1, $sections[4]['modules']); // One module for the hidden section with a visible activity. $this->assertNotEmpty($sections[3]['availabilityinfo']); $this->assertEquals(1, $sections[1]['section']); $this->assertEquals(2, $sections[2]['section']); $this->assertEquals(3, $sections[3]['section']); $this->assertEquals(4, $sections[4]['section']); $this->assertStringContainsString('<iframe', $sections[2]['summary']); $this->assertStringContainsString('</iframe>', $sections[2]['summary']); $this->assertNotEmpty($sections[2]['modules'][0]['availabilityinfo']); try { $sections = core_course_external::get_course_contents($course->id, array(array("name" => "invalid", "value" => 1))); $this->fail('Exception expected due to invalid option.'); } catch (moodle_exception $e) { $this->assertEquals('errorinvalidparam', $e->errorcode); } } /** * Test get_course_contents as student */ public function test_get_course_contents_student() { global $DB; $this->resetAfterTest(true); list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test(); $studentroleid = $DB->get_field('role', 'id', array('shortname' => 'student')); $user = self::getDataGenerator()->create_user(); self::getDataGenerator()->enrol_user($user->id, $course->id, $studentroleid); $this->setUser($user); $sections = core_course_external::get_course_contents($course->id, array()); // We need to execute the return values cleaning process to simulate the web service server. $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections); $this->assertCount(4, $sections); // Nothing for the not visible section. $this->assertCount(6, $sections[0]['modules']); $this->assertCount(1, $sections[1]['modules']); $this->assertCount(1, $sections[2]['modules']); $this->assertCount(0, $sections[3]['modules']); // No modules for the section with availability restrictions. $this->assertNotEmpty($sections[3]['availabilityinfo']); $this->assertEquals(1, $sections[1]['section']); $this->assertEquals(2, $sections[2]['section']); $this->assertEquals(3, $sections[3]['section']); // The module with the availability restriction met is returning contents. $this->assertNotEmpty($sections[1]['modules'][0]['contents']); // The module with the availability restriction not met is not returning contents. $this->assertArrayNotHasKey('contents', $sections[2]['modules'][0]); // Now include flag for returning stealth information (fake section). $sections = core_course_external::get_course_contents($course->id, array(array("name" => "includestealthmodules", "value" => 1))); // We need to execute the return values cleaning process to simulate the web service server. $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections); $this->assertCount(5, $sections); // Include fake section with stealth activities. $this->assertCount(6, $sections[0]['modules']); $this->assertCount(1, $sections[1]['modules']); $this->assertCount(1, $sections[2]['modules']); $this->assertCount(0, $sections[3]['modules']); // No modules for the section with availability restrictions. $this->assertCount(1, $sections[4]['modules']); // One stealth module. $this->assertEquals(-1, $sections[4]['id']); } /** * Test get_course_contents excluding modules */ public function test_get_course_contents_excluding_modules() { $this->resetAfterTest(true); list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test(); // Test exclude modules. $sections = core_course_external::get_course_contents($course->id, array(array("name" => "excludemodules", "value" => 1))); // We need to execute the return values cleaning process to simulate the web service server. $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections); $this->assertEmpty($sections[0]['modules']); $this->assertEmpty($sections[1]['modules']); } /** * Test get_course_contents excluding contents */ public function test_get_course_contents_excluding_contents() { $this->resetAfterTest(true); list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test(); // Test exclude modules. $sections = core_course_external::get_course_contents($course->id, array(array("name" => "excludecontents", "value" => 1))); // We need to execute the return values cleaning process to simulate the web service server. $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections); foreach ($sections as $section) { foreach ($section['modules'] as $module) { // Only resources return contents. if (isset($module['contents'])) { $this->assertEmpty($module['contents']); } } } } /** * Test get_course_contents filtering by section number */ public function test_get_course_contents_section_number() { $this->resetAfterTest(true); list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test(); // Test exclude modules. $sections = core_course_external::get_course_contents($course->id, array(array("name" => "sectionnumber", "value" => 0))); // We need to execute the return values cleaning process to simulate the web service server. $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections); $this->assertCount(1, $sections); $this->assertCount(6, $sections[0]['modules']); } /** * Test get_course_contents filtering by cmid */ public function test_get_course_contents_cmid() { $this->resetAfterTest(true); list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test(); // Test exclude modules. $sections = core_course_external::get_course_contents($course->id, array(array("name" => "cmid", "value" => $forumcm->id))); // We need to execute the return values cleaning process to simulate the web service server. $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections); $this->assertCount(4, $sections); $this->assertCount(1, $sections[0]['modules']); $this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]); } /** * Test get_course_contents filtering by cmid and section */ public function test_get_course_contents_section_cmid() { $this->resetAfterTest(true); list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test(); // Test exclude modules. $sections = core_course_external::get_course_contents($course->id, array( array("name" => "cmid", "value" => $forumcm->id), array("name" => "sectionnumber", "value" => 0) )); // We need to execute the return values cleaning process to simulate the web service server. $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections); $this->assertCount(1, $sections); $this->assertCount(1, $sections[0]['modules']); $this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]); } /** * Test get_course_contents filtering by modname */ public function test_get_course_contents_modname() { $this->resetAfterTest(true); list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test(); // Test exclude modules. $sections = core_course_external::get_course_contents($course->id, array(array("name" => "modname", "value" => "forum"))); // We need to execute the return values cleaning process to simulate the web service server. $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections); $this->assertCount(4, $sections); $this->assertCount(2, $sections[0]['modules']); $this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]); } /** * Test get_course_contents filtering by modname */ public function test_get_course_contents_modid() { $this->resetAfterTest(true); list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test(); // Test exclude modules. $sections = core_course_external::get_course_contents($course->id, array( array("name" => "modname", "value" => "page"), array("name" => "modid", "value" => $pagecm->instance), )); // We need to execute the return values cleaning process to simulate the web service server. $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections); $this->assertCount(4, $sections); $this->assertCount(1, $sections[0]['modules']); $this->assertEquals("page", $sections[0]['modules'][0]["modname"]); $this->assertEquals($pagecm->instance, $sections[0]['modules'][0]["instance"]); } /** * Test get_course_contents returns downloadcontent value. */ public function test_get_course_contents_downloadcontent() { $this->resetAfterTest(); list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test(); // Test exclude modules. $sections = core_course_external::get_course_contents($course->id, [ ['name' => 'modname', 'value' => 'page'], ['name' => 'modid', 'value' => $pagecm->instance] ]); // We need to execute the return values cleaning process to simulate the web service server. $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections); $this->assertCount(1, $sections[0]['modules']); $this->assertEquals('page', $sections[0]['modules'][0]['modname']); $this->assertEquals($pagecm->downloadcontent, $sections[0]['modules'][0]['downloadcontent']); $this->assertEquals(DOWNLOAD_COURSE_CONTENT_ENABLED, $sections[0]['modules'][0]['downloadcontent']); } /** * Test get course contents completion manual */ public function test_get_course_contents_completion_manual() { global $CFG; $this->resetAfterTest(true); list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm, $forumcompleteautocm) = $this->prepare_get_course_contents_test(); availability_completion\condition::wipe_static_cache(); // Test activity not completed yet. $result = core_course_external::get_course_contents($course->id, array( array("name" => "modname", "value" => "forum"), array("name" => "modid", "value" => $forumcm->instance))); // We need to execute the return values cleaning process to simulate the web service server. $result = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $result); $completiondata = $result[0]['modules'][0]["completiondata"]; $this->assertCount(1, $result[0]['modules']); $this->assertEquals("forum", $result[0]['modules'][0]["modname"]); $this->assertEquals(COMPLETION_TRACKING_MANUAL, $result[0]['modules'][0]["completion"]); $this->assertEquals(0, $completiondata['state']); $this->assertEquals(0, $completiondata['timecompleted']); $this->assertEmpty($completiondata['overrideby']); $this->assertFalse($completiondata['valueused']); $this->assertTrue($completiondata['hascompletion']); $this->assertFalse($completiondata['isautomatic']); $this->assertFalse($completiondata['istrackeduser']); $this->assertTrue($completiondata['uservisible']); // Set activity completed. core_completion_external::update_activity_completion_status_manually($forumcm->id, true); $result = core_course_external::get_course_contents($course->id, array( array("name" => "modname", "value" => "forum"), array("name" => "modid", "value" => $forumcm->instance))); // We need to execute the return values cleaning process to simulate the web service server. $result = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $result); $this->assertEquals(COMPLETION_COMPLETE, $result[0]['modules'][0]["completiondata"]['state']); $this->assertNotEmpty($result[0]['modules'][0]["completiondata"]['timecompleted']); $this->assertEmpty($result[0]['modules'][0]["completiondata"]['overrideby']); // Test activity with completion value that is used in an availability condition. $result = core_course_external::get_course_contents($course->id, array( array("name" => "modname", "value" => "label"), array("name" => "modid", "value" => $labelcm->instance))); // We need to execute the return values cleaning process to simulate the web service server. $result = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $result); $completiondata = $result[0]['modules'][0]["completiondata"]; $this->assertCount(1, $result[0]['modules']); $this->assertEquals("label", $result[0]['modules'][0]["modname"]); $this->assertEquals(COMPLETION_TRACKING_MANUAL, $result[0]['modules'][0]["completion"]); $this->assertEquals(0, $completiondata['state']); $this->assertEquals(0, $completiondata['timecompleted']); $this->assertEmpty($completiondata['overrideby']); $this->assertTrue($completiondata['valueused']); $this->assertTrue($completiondata['hascompletion']); $this->assertFalse($completiondata['isautomatic']); $this->assertFalse($completiondata['istrackeduser']); $this->assertTrue($completiondata['uservisible']); // Disable completion. $CFG->enablecompletion = 0; $result = core_course_external::get_course_contents($course->id, array( array("name" => "modname", "value" => "forum"), array("name" => "modid", "value" => $forumcm->instance))); // We need to execute the return values cleaning process to simulate the web service server. $result = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $result); $this->assertArrayNotHasKey('completiondata', $result[0]['modules'][0]); } /** * Test get course contents completion auto */ public function test_get_course_contents_completion_auto() { global $CFG; $this->resetAfterTest(true); list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm, $forumcompleteautocm) = $this->prepare_get_course_contents_test(); availability_completion\condition::wipe_static_cache(); // Test activity not completed yet. $result = core_course_external::get_course_contents($course->id, [ [ "name" => "modname", "value" => "forum" ], [ "name" => "modid", "value" => $forumcompleteautocm->instance ] ]); // We need to execute the return values cleaning process to simulate the web service server. $result = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $result); $forummod = $result[0]['modules'][0]; $completiondata = $forummod["completiondata"]; $this->assertCount(1, $result[0]['modules']); $this->assertEquals("forum", $forummod["modname"]); $this->assertEquals(COMPLETION_TRACKING_AUTOMATIC, $forummod["completion"]); $this->assertEquals(0, $completiondata['state']); $this->assertEquals(0, $completiondata['timecompleted']); $this->assertEmpty($completiondata['overrideby']); $this->assertFalse($completiondata['valueused']); $this->assertTrue($completiondata['hascompletion']); $this->assertTrue($completiondata['isautomatic']); $this->assertFalse($completiondata['istrackeduser']); $this->assertTrue($completiondata['uservisible']); $this->assertCount(1, $completiondata['details']); } /** * Test mimetype is returned for resources with showtype set. */ public function test_get_course_contents_including_mimetype() { $this->resetAfterTest(true); $this->setAdminUser(); $course = self::getDataGenerator()->create_course(); $record = new stdClass(); $record->course = $course->id; $record->showtype = 1; $resource = self::getDataGenerator()->create_module('resource', $record); $result = core_course_external::get_course_contents($course->id); $result = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $result); $this->assertCount(1, $result[0]['modules']); // One module, first section. $customdata = json_decode($result[0]['modules'][0]['customdata']); $displayoptions = unserialize($customdata->displayoptions); $this->assertEquals('text/plain', $displayoptions['filedetails']['mimetype']); } /** * Test contents info is returned. */ public function test_get_course_contents_contentsinfo() { global $USER; $this->resetAfterTest(true); $this->setAdminUser(); $timenow = time(); $course = self::getDataGenerator()->create_course(); $record = new stdClass(); $record->course = $course->id; // One resource with one file. $resource1 = self::getDataGenerator()->create_module('resource', $record); // More type of files. $record->files = file_get_unused_draft_itemid(); $usercontext = context_user::instance($USER->id); $extensions = array('txt', 'png', 'pdf'); $fs = get_file_storage(); foreach ($extensions as $key => $extension) { // Add actual file there. $filerecord = array('component' => 'user', 'filearea' => 'draft', 'contextid' => $usercontext->id, 'itemid' => $record->files, 'filename' => 'resource' . $key . '.' . $extension, 'filepath' => '/'); $fs->create_file_from_string($filerecord, 'Test resource ' . $key . ' file'); } // Create file reference. $repos = repository::get_instances(array('type' => 'user')); $userrepository = reset($repos); // Create a user private file. $userfilerecord = new stdClass; $userfilerecord->contextid = $usercontext->id; $userfilerecord->component = 'user'; $userfilerecord->filearea = 'private'; $userfilerecord->itemid = 0; $userfilerecord->filepath = '/'; $userfilerecord->filename = 'userfile.txt'; $userfilerecord->source = 'test'; $userfile = $fs->create_file_from_string($userfilerecord, 'User file content'); $userfileref = $fs->pack_reference($userfilerecord); // Clone latest "normal" file. $filerefrecord = clone (object) $filerecord; $filerefrecord->filename = 'testref.txt'; $fileref = $fs->create_file_from_reference($filerefrecord, $userrepository->id, $userfileref); // Set main file pointing to the file reference. file_set_sortorder($usercontext->id, 'user', 'draft', $record->files, $filerefrecord->filepath, $filerefrecord->filename, 1); // Once the reference has been created, create the file resource. $resource2 = self::getDataGenerator()->create_module('resource', $record); $result = core_course_external::get_course_contents($course->id); $result = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $result); $this->assertCount(2, $result[0]['modules']); foreach ($result[0]['modules'] as $module) { if ($module['instance'] == $resource1->id) { $this->assertEquals(1, $module['contentsinfo']['filescount']); $this->assertGreaterThanOrEqual($timenow, $module['contentsinfo']['lastmodified']); $this->assertEquals($module['contents'][0]['filesize'], $module['contentsinfo']['filessize']); $this->assertEquals(array('text/plain'), $module['contentsinfo']['mimetypes']); } else { $this->assertEquals(count($extensions) + 1, $module['contentsinfo']['filescount']); $filessize = $module['contents'][0]['filesize'] + $module['contents'][1]['filesize'] + $module['contents'][2]['filesize'] + $module['contents'][3]['filesize']; $this->assertEquals($filessize, $module['contentsinfo']['filessize']); $this->assertEquals('user', $module['contentsinfo']['repositorytype']); $this->assertGreaterThanOrEqual($timenow, $module['contentsinfo']['lastmodified']); $this->assertEquals(array('text/plain', 'image/png', 'application/pdf'), $module['contentsinfo']['mimetypes']); } } } /** * Test get_course_contents when hidden sections are displayed. */ public function test_get_course_contents_hiddensections() { global $DB; $this->resetAfterTest(true); list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test(); // Force returning hidden sections. $course->hiddensections = 0; update_course($course); $studentroleid = $DB->get_field('role', 'id', array('shortname' => 'student')); $user = self::getDataGenerator()->create_user(); self::getDataGenerator()->enrol_user($user->id, $course->id, $studentroleid); $this->setUser($user); $sections = core_course_external::get_course_contents($course->id, array()); // We need to execute the return values cleaning process to simulate the web service server. $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections); $this->assertCount(5, $sections); // All the sections, including the "not visible" one. $this->assertCount(6, $sections[0]['modules']); $this->assertCount(1, $sections[1]['modules']); $this->assertCount(1, $sections[2]['modules']); $this->assertCount(0, $sections[3]['modules']); // No modules for the section with availability restrictions. $this->assertCount(0, $sections[4]['modules']); // No modules for the section hidden. $this->assertNotEmpty($sections[3]['availabilityinfo']); $this->assertEquals(1, $sections[1]['section']); $this->assertEquals(2, $sections[2]['section']); $this->assertEquals(3, $sections[3]['section']); // The module with the availability restriction met is returning contents. $this->assertNotEmpty($sections[1]['modules'][0]['contents']); // The module with the availability restriction not met is not returning contents. $this->assertArrayNotHasKey('contents', $sections[2]['modules'][0]); // Now include flag for returning stealth information (fake section). $sections = core_course_external::get_course_contents($course->id, array(array("name" => "includestealthmodules", "value" => 1))); // We need to execute the return values cleaning process to simulate the web service server. $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections); $this->assertCount(6, $sections); // Include fake section with stealth activities. $this->assertCount(6, $sections[0]['modules']); $this->assertCount(1, $sections[1]['modules']); $this->assertCount(1, $sections[2]['modules']); $this->assertCount(0, $sections[3]['modules']); // No modules for the section with availability restrictions. $this->assertCount(0, $sections[4]['modules']); // No modules for the section hidden. $this->assertCount(1, $sections[5]['modules']); // One stealth module. $this->assertEquals(-1, $sections[5]['id']); } /** * Test get course contents dates. */ public function test_get_course_contents_dates() { $this->resetAfterTest(true); $this->setAdminUser(); set_config('enablecourserelativedates', 1); // Course with just main section. $timenow = time(); $course = self::getDataGenerator()->create_course( ['numsections' => 0, 'relativedatesmode' => true, 'startdate' => $timenow - DAYSECS]); $teacher = self::getDataGenerator()->create_user(); self::getDataGenerator()->enrol_user($teacher->id, $course->id, 'editingteacher'); $this->setUser($teacher); // Create resource (empty dates). $resource = self::getDataGenerator()->create_module('resource', ['course' => $course->id]); // Create activities with dates. $resource = self::getDataGenerator()->create_module('forum', ['course' => $course->id, 'duedate' => $timenow]); $resource = self::getDataGenerator()->create_module('choice', ['course' => $course->id, 'timeopen' => $timenow, 'timeclose' => $timenow + DAYSECS]); $resource = self::getDataGenerator()->create_module('assign', ['course' => $course->id, 'allowsubmissionsfromdate' => $timenow]); $result = core_course_external::get_course_contents($course->id); $result = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $result); foreach ($result[0]['modules'] as $module) { if ($module['modname'] == 'resource') { $this->assertEmpty($module['dates']); } else if ($module['modname'] == 'forum') { $this->assertCount(1, $module['dates']); $this->assertEquals('duedate', $module['dates'][0]['dataid']); $this->assertEquals($timenow, $module['dates'][0]['timestamp']); } else if ($module['modname'] == 'choice') { $this->assertCount(2, $module['dates']); $this->assertEquals('timeopen', $module['dates'][0]['dataid']); $this->assertEquals($timenow, $module['dates'][0]['timestamp']); $this->assertEquals('timeclose', $module['dates'][1]['dataid']); $this->assertEquals($timenow + DAYSECS, $module['dates'][1]['timestamp']); } else if ($module['modname'] == 'assign') { $this->assertCount(1, $module['dates']); $this->assertEquals('allowsubmissionsfromdate', $module['dates'][0]['dataid']); $this->assertEquals($timenow, $module['dates'][0]['timestamp']); $this->assertEquals($course->startdate, $module['dates'][0]['relativeto']); } } } /** * Test duplicate_course */ public function test_duplicate_course() { $this->resetAfterTest(true); // Create one course with three modules. $course = self::getDataGenerator()->create_course(); $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id)); $forumcm = get_coursemodule_from_id('forum', $forum->cmid); $forumcontext = context_module::instance($forum->cmid); $data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id)); $datacontext = context_module::instance($data->cmid); $datacm = get_coursemodule_from_instance('page', $data->id); $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id)); $pagecontext = context_module::instance($page->cmid); $pagecm = get_coursemodule_from_instance('page', $page->id); // Set the required capabilities by the external function. $coursecontext = context_course::instance($course->id); $categorycontext = context_coursecat::instance($course->category); $roleid = $this->assignUserCapability('moodle/course:create', $categorycontext->id); $this->assignUserCapability('moodle/course:view', $categorycontext->id, $roleid); $this->assignUserCapability('moodle/restore:restorecourse', $categorycontext->id, $roleid); $this->assignUserCapability('moodle/backup:backupcourse', $coursecontext->id, $roleid); $this->assignUserCapability('moodle/backup:configure', $coursecontext->id, $roleid); // Optional capabilities to copy user data. $this->assignUserCapability('moodle/backup:userinfo', $coursecontext->id, $roleid); $this->assignUserCapability('moodle/restore:userinfo', $categorycontext->id, $roleid); $newcourse['fullname'] = 'Course duplicate'; $newcourse['shortname'] = 'courseduplicate'; $newcourse['categoryid'] = $course->category; $newcourse['visible'] = true; $newcourse['options'][] = array('name' => 'users', 'value' => true); $duplicate = core_course_external::duplicate_course($course->id, $newcourse['fullname'], $newcourse['shortname'], $newcourse['categoryid'], $newcourse['visible'], $newcourse['options']); // We need to execute the return values cleaning process to simulate the web service server. $duplicate = external_api::clean_returnvalue(core_course_external::duplicate_course_returns(), $duplicate); // Check that the course has been duplicated. $this->assertEquals($newcourse['shortname'], $duplicate['shortname']); } /** * Test update_courses */ public function test_update_courses() { global $DB, $CFG, $USER, $COURSE; // Get current $COURSE to be able to restore it later (defaults to $SITE). We need this // trick because we are both updating and getting (for testing) course information // in the same request and core_course_external::update_courses() // is overwriting $COURSE all over the time with OLD values, so later // use of get_course() fetches those OLD values instead of the updated ones. // See MDL-39723 for more info. $origcourse = clone($COURSE); $this->resetAfterTest(true); // Set the required capabilities by the external function. $contextid = context_system::instance()->id; $roleid = $this->assignUserCapability('moodle/course:update', $contextid); $this->assignUserCapability('moodle/course:changecategory', $contextid, $roleid); $this->assignUserCapability('moodle/course:changelockedcustomfields', $contextid, $roleid); $this->assignUserCapability('moodle/course:changefullname', $contextid, $roleid); $this->assignUserCapability('moodle/course:changeshortname', $contextid, $roleid); $this->assignUserCapability('moodle/course:changeidnumber', $contextid, $roleid); $this->assignUserCapability('moodle/course:changesummary', $contextid, $roleid); $this->assignUserCapability('moodle/course:visibility', $contextid, $roleid); $this->assignUserCapability('moodle/course:viewhiddencourses', $contextid, $roleid); $this->assignUserCapability('moodle/course:setforcedlanguage', $contextid, $roleid); // Create category and courses. $category1 = self::getDataGenerator()->create_category(); $category2 = self::getDataGenerator()->create_category(); $originalcourse1 = self::getDataGenerator()->create_course(); self::getDataGenerator()->enrol_user($USER->id, $originalcourse1->id, $roleid); $originalcourse2 = self::getDataGenerator()->create_course(); self::getDataGenerator()->enrol_user($USER->id, $originalcourse2->id, $roleid); // Course with custom fields. $fieldcategory = self::getDataGenerator()->create_custom_field_category(['name' => 'Other fields']); $customfield = ['shortname' => 'test', 'name' => 'Custom field', 'type' => 'text', 'categoryid' => $fieldcategory->get('id'), 'configdata' => ['visibility' => \core_course\customfield\course_handler::VISIBLETOALL, 'locked' => 1]]; $field = self::getDataGenerator()->create_custom_field($customfield); $originalcourse3 = self::getDataGenerator()->create_course(['customfield_test' => 'Test value']); self::getDataGenerator()->enrol_user($USER->id, $originalcourse3->id, $roleid); // Course values to be updated. $course1['id'] = $originalcourse1->id; $course1['fullname'] = 'Updated test course 1'; $course1['shortname'] = 'Udestedtestcourse1'; $course1['categoryid'] = $category1->id; $course2['id'] = $originalcourse2->id; $course2['fullname'] = 'Updated test course 2'; $course2['shortname'] = 'Updestedtestcourse2'; $course2['categoryid'] = $category2->id; $course2['idnumber'] = 'Updatedidnumber2'; $course2['summary'] = 'Updaated description for course 2'; $course2['summaryformat'] = FORMAT_HTML; $course2['format'] = 'topics'; $course2['showgrades'] = 1; $course2['newsitems'] = 3; $course2['startdate'] = 1420092000; // 01/01/2015. $course2['enddate'] = 1422669600; // 01/31/2015. $course2['maxbytes'] = 100000; $course2['showreports'] = 1; $course2['visible'] = 0; $course2['hiddensections'] = 0; $course2['groupmode'] = 0; $course2['groupmodeforce'] = 0; $course2['defaultgroupingid'] = 0; $course2['enablecompletion'] = 1; $course2['lang'] = 'en'; $course2['forcetheme'] = 'classic'; $course3['id'] = $originalcourse3->id; $updatedcustomfieldvalue = ['shortname' => 'test', 'value' => 'Updated test value']; $course3['customfields'] = [$updatedcustomfieldvalue]; $courses = array($course1, $course2, $course3); $updatedcoursewarnings = core_course_external::update_courses($courses); $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(), $updatedcoursewarnings); $COURSE = $origcourse; // Restore $COURSE. Instead of using the OLD one set by the previous line. // Check that right number of courses were created. $this->assertEquals(0, count($updatedcoursewarnings['warnings'])); // Check that the courses were correctly created. foreach ($courses as $course) { $courseinfo = course_get_format($course['id'])->get_course(); $customfields = \core_course\customfield\course_handler::create()->export_instance_data_object($course['id']); if ($course['id'] == $course2['id']) { $this->assertEquals($course2['fullname'], $courseinfo->fullname); $this->assertEquals($course2['shortname'], $courseinfo->shortname); $this->assertEquals($course2['categoryid'], $courseinfo->category); $this->assertEquals($course2['idnumber'], $courseinfo->idnumber); $this->assertEquals($course2['summary'], $courseinfo->summary); $this->assertEquals($course2['summaryformat'], $courseinfo->summaryformat); $this->assertEquals($course2['format'], $courseinfo->format); $this->assertEquals($course2['showgrades'], $courseinfo->showgrades); $this->assertEquals($course2['newsitems'], $courseinfo->newsitems); $this->assertEquals($course2['startdate'], $courseinfo->startdate); $this->assertEquals($course2['enddate'], $courseinfo->enddate); $this->assertEquals($course2['maxbytes'], $courseinfo->maxbytes); $this->assertEquals($course2['showreports'], $courseinfo->showreports); $this->assertEquals($course2['visible'], $courseinfo->visible); $this->assertEquals($course2['hiddensections'], $courseinfo->hiddensections); $this->assertEquals($course2['groupmode'], $courseinfo->groupmode); $this->assertEquals($course2['groupmodeforce'], $courseinfo->groupmodeforce); $this->assertEquals($course2['defaultgroupingid'], $courseinfo->defaultgroupingid); $this->assertEquals($course2['lang'], $courseinfo->lang); if (!empty($CFG->allowcoursethemes)) { $this->assertEquals($course2['forcetheme'], $courseinfo->theme); } $this->assertEquals($course2['enablecompletion'], $courseinfo->enablecompletion); $this->assertEquals(['test' => null], (array)$customfields); } else if ($course['id'] == $course1['id']) { $this->assertEquals($course1['fullname'], $courseinfo->fullname); $this->assertEquals($course1['shortname'], $courseinfo->shortname); $this->assertEquals($course1['categoryid'], $courseinfo->category); $this->assertEquals(FORMAT_MOODLE, $courseinfo->summaryformat); $this->assertEquals('topics', $courseinfo->format); $this->assertEquals(5, course_get_format($course['id'])->get_last_section_number()); $this->assertEquals(0, $courseinfo->newsitems); $this->assertEquals(FORMAT_MOODLE, $courseinfo->summaryformat); $this->assertEquals(['test' => null], (array)$customfields); } else if ($course['id'] == $course3['id']) { $this->assertEquals(['test' => $updatedcustomfieldvalue['value']], (array)$customfields); } else { throw new moodle_exception('Unexpected shortname'); } } $courses = array($course1); // Try update course without update capability. $user = self::getDataGenerator()->create_user(); $this->setUser($user); $this->unassignUserCapability('moodle/course:update', $contextid, $roleid); self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid); $updatedcoursewarnings = core_course_external::update_courses($courses); $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(), $updatedcoursewarnings); $this->assertEquals(1, count($updatedcoursewarnings['warnings'])); // Try update course category without capability. $this->assignUserCapability('moodle/course:update', $contextid, $roleid); $this->unassignUserCapability('moodle/course:changecategory', $contextid, $roleid); $user = self::getDataGenerator()->create_user(); $this->setUser($user); self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid); $course1['categoryid'] = $category2->id; $courses = array($course1); $updatedcoursewarnings = core_course_external::update_courses($courses); $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(), $updatedcoursewarnings); $this->assertEquals(1, count($updatedcoursewarnings['warnings'])); // Try update course fullname without capability. $this->assignUserCapability('moodle/course:changecategory', $contextid, $roleid); $this->unassignUserCapability('moodle/course:changefullname', $contextid, $roleid); $user = self::getDataGenerator()->create_user(); $this->setUser($user); self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid); $updatedcoursewarnings = core_course_external::update_courses($courses); $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(), $updatedcoursewarnings); $this->assertEquals(0, count($updatedcoursewarnings['warnings'])); $course1['fullname'] = 'Testing fullname without permission'; $courses = array($course1); $updatedcoursewarnings = core_course_external::update_courses($courses); $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(), $updatedcoursewarnings); $this->assertEquals(1, count($updatedcoursewarnings['warnings'])); // Try update course shortname without capability. $this->assignUserCapability('moodle/course:changefullname', $contextid, $roleid); $this->unassignUserCapability('moodle/course:changeshortname', $contextid, $roleid); $user = self::getDataGenerator()->create_user(); $this->setUser($user); self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid); $updatedcoursewarnings = core_course_external::update_courses($courses); $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(), $updatedcoursewarnings); $this->assertEquals(0, count($updatedcoursewarnings['warnings'])); $course1['shortname'] = 'Testing shortname without permission'; $courses = array($course1); $updatedcoursewarnings = core_course_external::update_courses($courses); $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(), $updatedcoursewarnings); $this->assertEquals(1, count($updatedcoursewarnings['warnings'])); // Try update course idnumber without capability. $this->assignUserCapability('moodle/course:changeshortname', $contextid, $roleid); $this->unassignUserCapability('moodle/course:changeidnumber', $contextid, $roleid); $user = self::getDataGenerator()->create_user(); $this->setUser($user); self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid); $updatedcoursewarnings = core_course_external::update_courses($courses); $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(), $updatedcoursewarnings); $this->assertEquals(0, count($updatedcoursewarnings['warnings'])); $course1['idnumber'] = 'NEWIDNUMBER'; $courses = array($course1); $updatedcoursewarnings = core_course_external::update_courses($courses); $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(), $updatedcoursewarnings); $this->assertEquals(1, count($updatedcoursewarnings['warnings'])); // Try update course summary without capability. $this->assignUserCapability('moodle/course:changeidnumber', $contextid, $roleid); $this->unassignUserCapability('moodle/course:changesummary', $contextid, $roleid); $user = self::getDataGenerator()->create_user(); $this->setUser($user); self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid); $updatedcoursewarnings = core_course_external::update_courses($courses); $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(), $updatedcoursewarnings); $this->assertEquals(0, count($updatedcoursewarnings['warnings'])); $course1['summary'] = 'New summary'; $courses = array($course1); $updatedcoursewarnings = core_course_external::update_courses($courses); $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(), $updatedcoursewarnings); $this->assertEquals(1, count($updatedcoursewarnings['warnings'])); // Try update course with invalid summary format. $this->assignUserCapability('moodle/course:changesummary', $contextid, $roleid); $user = self::getDataGenerator()->create_user(); $this->setUser($user); self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid); $updatedcoursewarnings = core_course_external::update_courses($courses); $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(), $updatedcoursewarnings); $this->assertEquals(0, count($updatedcoursewarnings['warnings'])); $course1['summaryformat'] = 10; $courses = array($course1); $updatedcoursewarnings = core_course_external::update_courses($courses); $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(), $updatedcoursewarnings); $this->assertEquals(1, count($updatedcoursewarnings['warnings'])); // Try update course visibility without capability. $this->unassignUserCapability('moodle/course:visibility', $contextid, $roleid); $user = self::getDataGenerator()->create_user(); $this->setUser($user); self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid); $course1['summaryformat'] = FORMAT_MOODLE; $courses = array($course1); $updatedcoursewarnings = core_course_external::update_courses($courses); $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(), $updatedcoursewarnings); $this->assertEquals(0, count($updatedcoursewarnings['warnings'])); $course1['visible'] = 0; $courses = array($course1); $updatedcoursewarnings = core_course_external::update_courses($courses); $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(), $updatedcoursewarnings); $this->assertEquals(1, count($updatedcoursewarnings['warnings'])); // Try update course custom fields without capability. $this->unassignUserCapability('moodle/course:changelockedcustomfields', $contextid, $roleid); $user = self::getDataGenerator()->create_user(); $this->setUser($user); self::getDataGenerator()->enrol_user($user->id, $course3['id'], $roleid); $newupdatedcustomfieldvalue = ['shortname' => 'test', 'value' => 'New updated value']; $course3['customfields'] = [$newupdatedcustomfieldvalue]; core_course_external::update_courses([$course3]); // Custom field was not updated. $customfields = \core_course\customfield\course_handler::create()->export_instance_data_object($course3['id']); $this->assertEquals(['test' => $updatedcustomfieldvalue['value']], (array)$customfields); } /** * Test delete course_module. */ public function test_delete_modules() { global $DB; // Ensure we reset the data after this test. $this->resetAfterTest(true); // Create a user. $user = self::getDataGenerator()->create_user(); // Set the tests to run as the user. self::setUser($user); // Create a course to add the modules. $course = self::getDataGenerator()->create_course(); // Create two test modules. $record = new stdClass(); $record->course = $course->id; $module1 = self::getDataGenerator()->create_module('forum', $record); $module2 = self::getDataGenerator()->create_module('assign', $record); // Check the forum was correctly created. $this->assertEquals(1, $DB->count_records('forum', array('id' => $module1->id))); // Check the assignment was correctly created. $this->assertEquals(1, $DB->count_records('assign', array('id' => $module2->id))); // Check data exists in the course modules table. $this->assertEquals(2, $DB->count_records_select('course_modules', 'id = :module1 OR id = :module2', array('module1' => $module1->cmid, 'module2' => $module2->cmid))); // Enrol the user in the course. $enrol = enrol_get_plugin('manual'); $enrolinstances = enrol_get_instances($course->id, true); foreach ($enrolinstances as $courseenrolinstance) { if ($courseenrolinstance->enrol == "manual") { $instance = $courseenrolinstance; break; } } $enrol->enrol_user($instance, $user->id); // Assign capabilities to delete module 1. $modcontext = context_module::instance($module1->cmid); $this->assignUserCapability('moodle/course:manageactivities', $modcontext->id); // Assign capabilities to delete module 2. $modcontext = context_module::instance($module2->cmid); $newrole = create_role('Role 2', 'role2', 'Role 2 description'); $this->assignUserCapability('moodle/course:manageactivities', $modcontext->id, $newrole); // Deleting these module instances. core_course_external::delete_modules(array($module1->cmid, $module2->cmid)); // Check the forum was deleted. $this->assertEquals(0, $DB->count_records('forum', array('id' => $module1->id))); // Check the assignment was deleted. $this->assertEquals(0, $DB->count_records('assign', array('id' => $module2->id))); // Check we retrieve no data in the course modules table. $this->assertEquals(0, $DB->count_records_select('course_modules', 'id = :module1 OR id = :module2', array('module1' => $module1->cmid, 'module2' => $module2->cmid))); // Call with non-existent course module id and ensure exception thrown. try { core_course_external::delete_modules(array('1337')); $this->fail('Exception expected due to missing course module.'); } catch (dml_missing_record_exception $e) { $this->assertEquals('invalidcoursemodule', $e->errorcode); } // Create two modules. $module1 = self::getDataGenerator()->create_module('forum', $record); $module2 = self::getDataGenerator()->create_module('assign', $record); // Since these modules were recreated the user will not have capabilities // to delete them, ensure exception is thrown if they try. try { core_course_external::delete_modules(array($module1->cmid, $module2->cmid)); $this->fail('Exception expected due to missing capability.'); } catch (moodle_exception $e) { $this->assertEquals('nopermissions', $e->errorcode); } // Unenrol user from the course. $enrol->unenrol_user($instance, $user->id); // Try and delete modules from the course the user was unenrolled in, make sure exception thrown. try { core_course_external::delete_modules(array($module1->cmid, $module2->cmid)); $this->fail('Exception expected due to being unenrolled from the course.'); } catch (moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } } /** * Test import_course into an empty course */ public function test_import_course_empty() { global $USER; $this->resetAfterTest(true); $course1 = self::getDataGenerator()->create_course(); $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course1->id, 'name' => 'Forum test')); $page = $this->getDataGenerator()->create_module('page', array('course' => $course1->id, 'name' => 'Page test')); $course2 = self::getDataGenerator()->create_course(); $course1cms = get_fast_modinfo($course1->id)->get_cms(); $course2cms = get_fast_modinfo($course2->id)->get_cms(); // Verify the state of the courses before we do the import. $this->assertCount(2, $course1cms); $this->assertEmpty($course2cms); // Setup the user to run the operation (ugly hack because validate_context() will // fail as the email is not set by $this->setAdminUser()). $this->setAdminUser(); $USER->email = 'emailtopass@example.com'; // Import from course1 to course2. core_course_external::import_course($course1->id, $course2->id, 0); // Verify that now we have two modules in both courses. $course1cms = get_fast_modinfo($course1->id)->get_cms(); $course2cms = get_fast_modinfo($course2->id)->get_cms(); $this->assertCount(2, $course1cms); $this->assertCount(2, $course2cms); // Verify that the names transfered across correctly. foreach ($course2cms as $cm) { if ($cm->modname === 'page') { $this->assertEquals($cm->name, $page->name); } else if ($cm->modname === 'forum') { $this->assertEquals($cm->name, $forum->name); } else { $this->fail('Unknown CM found.'); } } } /** * Test import_course into an filled course */ public function test_import_course_filled() { global $USER; $this->resetAfterTest(true); // Add forum and page to course1. $course1 = self::getDataGenerator()->create_course(); $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test')); $page = $this->getDataGenerator()->create_module('page', array('course'=>$course1->id, 'name' => 'Page test')); // Add quiz to course 2. $course2 = self::getDataGenerator()->create_course(); $quiz = $this->getDataGenerator()->create_module('quiz', array('course'=>$course2->id, 'name' => 'Page test')); $course1cms = get_fast_modinfo($course1->id)->get_cms(); $course2cms = get_fast_modinfo($course2->id)->get_cms(); // Verify the state of the courses before we do the import. $this->assertCount(2, $course1cms); $this->assertCount(1, $course2cms); // Setup the user to run the operation (ugly hack because validate_context() will // fail as the email is not set by $this->setAdminUser()). $this->setAdminUser(); $USER->email = 'emailtopass@example.com'; // Import from course1 to course2 without deleting content. core_course_external::import_course($course1->id, $course2->id, 0); $course2cms = get_fast_modinfo($course2->id)->get_cms(); // Verify that now we have three modules in course2. $this->assertCount(3, $course2cms); // Verify that the names transfered across correctly. foreach ($course2cms as $cm) { if ($cm->modname === 'page') { $this->assertEquals($cm->name, $page->name); } else if ($cm->modname === 'forum') { $this->assertEquals($cm->name, $forum->name); } else if ($cm->modname === 'quiz') { $this->assertEquals($cm->name, $quiz->name); } else { $this->fail('Unknown CM found.'); } } } /** * Test import_course with only blocks set to backup */ public function test_import_course_blocksonly() { global $USER, $DB; $this->resetAfterTest(true); // Add forum and page to course1. $course1 = self::getDataGenerator()->create_course(); $course1ctx = context_course::instance($course1->id); $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test')); $block = $this->getDataGenerator()->create_block('online_users', array('parentcontextid' => $course1ctx->id)); $course2 = self::getDataGenerator()->create_course(); $course2ctx = context_course::instance($course2->id); $initialblockcount = $DB->count_records('block_instances', array('parentcontextid' => $course2ctx->id)); $initialcmcount = count(get_fast_modinfo($course2->id)->get_cms()); // Setup the user to run the operation (ugly hack because validate_context() will // fail as the email is not set by $this->setAdminUser()). $this->setAdminUser(); $USER->email = 'emailtopass@example.com'; // Import from course1 to course2 without deleting content, but excluding // activities. $options = array( array('name' => 'activities', 'value' => 0), array('name' => 'blocks', 'value' => 1), array('name' => 'filters', 'value' => 0), ); core_course_external::import_course($course1->id, $course2->id, 0, $options); $newcmcount = count(get_fast_modinfo($course2->id)->get_cms()); $newblockcount = $DB->count_records('block_instances', array('parentcontextid' => $course2ctx->id)); // Check that course modules haven't changed, but that blocks have. $this->assertEquals($initialcmcount, $newcmcount); $this->assertEquals(($initialblockcount + 1), $newblockcount); } /** * Test import_course into an filled course, deleting content. */ public function test_import_course_deletecontent() { global $USER; $this->resetAfterTest(true); // Add forum and page to course1. $course1 = self::getDataGenerator()->create_course(); $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test')); $page = $this->getDataGenerator()->create_module('page', array('course'=>$course1->id, 'name' => 'Page test')); // Add quiz to course 2. $course2 = self::getDataGenerator()->create_course(); $quiz = $this->getDataGenerator()->create_module('quiz', array('course'=>$course2->id, 'name' => 'Page test')); $course1cms = get_fast_modinfo($course1->id)->get_cms(); $course2cms = get_fast_modinfo($course2->id)->get_cms(); // Verify the state of the courses before we do the import. $this->assertCount(2, $course1cms); $this->assertCount(1, $course2cms); // Setup the user to run the operation (ugly hack because validate_context() will // fail as the email is not set by $this->setAdminUser()). $this->setAdminUser(); $USER->email = 'emailtopass@example.com'; // Import from course1 to course2, deleting content. core_course_external::import_course($course1->id, $course2->id, 1); $course2cms = get_fast_modinfo($course2->id)->get_cms(); // Verify that now we have two modules in course2. $this->assertCount(2, $course2cms); // Verify that the course only contains the imported modules. foreach ($course2cms as $cm) { if ($cm->modname === 'page') { $this->assertEquals($cm->name, $page->name); } else if ($cm->modname === 'forum') { $this->assertEquals($cm->name, $forum->name); } else { $this->fail('Unknown CM found: '.$cm->name); } } } /** * Ensure import_course handles incorrect deletecontent option correctly. */ public function test_import_course_invalid_deletecontent_option() { $this->resetAfterTest(true); $course1 = self::getDataGenerator()->create_course(); $course2 = self::getDataGenerator()->create_course(); $this->expectException('moodle_exception'); $this->expectExceptionMessage(get_string('invalidextparam', 'webservice', -1)); // Import from course1 to course2, with invalid option core_course_external::import_course($course1->id, $course2->id, -1);; } /** * Test view_course function */ public function test_view_course() { $this->resetAfterTest(); // Course without sections. $course = $this->getDataGenerator()->create_course(array('numsections' => 5), array('createsections' => true)); $this->setAdminUser(); // Redirect events to the sink, so we can recover them later. $sink = $this->redirectEvents(); $result = core_course_external::view_course($course->id, 1); $result = external_api::clean_returnvalue(core_course_external::view_course_returns(), $result); $events = $sink->get_events(); $event = reset($events); // Check the event details are correct. $this->assertInstanceOf('\core\event\course_viewed', $event); $this->assertEquals(context_course::instance($course->id), $event->get_context()); $this->assertEquals(1, $event->other['coursesectionnumber']); $result = core_course_external::view_course($course->id); $result = external_api::clean_returnvalue(core_course_external::view_course_returns(), $result); $events = $sink->get_events(); $event = array_pop($events); $sink->close(); // Check the event details are correct. $this->assertInstanceOf('\core\event\course_viewed', $event); $this->assertEquals(context_course::instance($course->id), $event->get_context()); $this->assertEmpty($event->other); } /** * Test get_course_module */ public function test_get_course_module() { global $DB; $this->resetAfterTest(true); $this->setAdminUser(); $course = self::getDataGenerator()->create_course(['enablecompletion' => 1]); $record = array( 'course' => $course->id, 'name' => 'First Assignment' ); $options = array( 'idnumber' => 'ABC', 'visible' => 0, 'completion' => COMPLETION_TRACKING_AUTOMATIC, 'completiongradeitemnumber' => 0, 'completionpassgrade' => 1, ); // Hidden activity. $assign = self::getDataGenerator()->create_module('assign', $record, $options); $outcomescale = 'Distinction, Very Good, Good, Pass, Fail'; // Insert a custom grade scale to be used by an outcome. $gradescale = new grade_scale(); $gradescale->name = 'gettcoursemodulescale'; $gradescale->courseid = $course->id; $gradescale->userid = 0; $gradescale->scale = $outcomescale; $gradescale->description = 'This scale is used to mark standard assignments.'; $gradescale->insert(); // Insert an outcome. $data = new stdClass(); $data->courseid = $course->id; $data->fullname = 'Team work'; $data->shortname = 'Team work'; $data->scaleid = $gradescale->id; $outcome = new grade_outcome($data, false); $outcome->insert(); $outcomegradeitem = new grade_item(); $outcomegradeitem->itemname = $outcome->shortname; $outcomegradeitem->itemtype = 'mod'; $outcomegradeitem->itemmodule = 'assign'; $outcomegradeitem->iteminstance = $assign->id; $outcomegradeitem->outcomeid = $outcome->id; $outcomegradeitem->cmid = 0; $outcomegradeitem->courseid = $course->id; $outcomegradeitem->aggregationcoef = 0; $outcomegradeitem->itemnumber = 1000; // Outcomes start at 1000. $outcomegradeitem->gradetype = GRADE_TYPE_SCALE; $outcomegradeitem->scaleid = $outcome->scaleid; $outcomegradeitem->insert(); $assignmentgradeitem = grade_item::fetch( array( 'itemtype' => 'mod', 'itemmodule' => 'assign', 'iteminstance' => $assign->id, 'itemnumber' => 0, 'courseid' => $course->id ) ); $outcomegradeitem->set_parent($assignmentgradeitem->categoryid); $outcomegradeitem->move_after_sortorder($assignmentgradeitem->sortorder); // Test admin user can see the complete hidden activity. $result = core_course_external::get_course_module($assign->cmid); $result = external_api::clean_returnvalue(core_course_external::get_course_module_returns(), $result); $this->assertCount(0, $result['warnings']); // Test we retrieve all the fields. $this->assertCount(30, $result['cm']); $this->assertEquals($record['name'], $result['cm']['name']); $this->assertEquals($options['idnumber'], $result['cm']['idnumber']); $this->assertEquals(100, $result['cm']['grade']); $this->assertEquals(0.0, $result['cm']['gradepass']); $this->assertEquals('submissions', $result['cm']['advancedgrading'][0]['area']); $this->assertEmpty($result['cm']['advancedgrading'][0]['method']); $this->assertEquals($outcomescale, $result['cm']['outcomes'][0]['scale']); $this->assertEquals(DOWNLOAD_COURSE_CONTENT_ENABLED, $result['cm']['downloadcontent']); $student = $this->getDataGenerator()->create_user(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); self::getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id); $this->setUser($student); // The user shouldn't be able to see the activity. try { core_course_external::get_course_module($assign->cmid); $this->fail('Exception expected due to invalid permissions.'); } catch (moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } // Make module visible. set_coursemodule_visible($assign->cmid, 1); // Test student user. $result = core_course_external::get_course_module($assign->cmid); $result = external_api::clean_returnvalue(core_course_external::get_course_module_returns(), $result); $this->assertCount(0, $result['warnings']); // Test we retrieve only the few files we can see. $this->assertCount(12, $result['cm']); $this->assertEquals($assign->cmid, $result['cm']['id']); $this->assertEquals($course->id, $result['cm']['course']); $this->assertEquals('assign', $result['cm']['modname']); $this->assertEquals($assign->id, $result['cm']['instance']); } /** * Test get_course_module_by_instance */ public function test_get_course_module_by_instance() { global $DB; $this->resetAfterTest(true); $this->setAdminUser(); $course = self::getDataGenerator()->create_course(); $record = array( 'course' => $course->id, 'name' => 'First quiz', 'grade' => 90.00 ); $options = array( 'idnumber' => 'ABC', 'visible' => 0 ); // Hidden activity. $quiz = self::getDataGenerator()->create_module('quiz', $record, $options); // Test admin user can see the complete hidden activity. $result = core_course_external::get_course_module_by_instance('quiz', $quiz->id); $result = external_api::clean_returnvalue(core_course_external::get_course_module_by_instance_returns(), $result); $this->assertCount(0, $result['warnings']); // Test we retrieve all the fields. $this->assertCount(28, $result['cm']); $this->assertEquals($record['name'], $result['cm']['name']); $this->assertEquals($record['grade'], $result['cm']['grade']); $this->assertEquals($options['idnumber'], $result['cm']['idnumber']); $this->assertEquals(DOWNLOAD_COURSE_CONTENT_ENABLED, $result['cm']['downloadcontent']); $student = $this->getDataGenerator()->create_user(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); self::getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id); $this->setUser($student); // The user shouldn't be able to see the activity. try { core_course_external::get_course_module_by_instance('quiz', $quiz->id); $this->fail('Exception expected due to invalid permissions.'); } catch (moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } // Make module visible. set_coursemodule_visible($quiz->cmid, 1); // Test student user. $result = core_course_external::get_course_module_by_instance('quiz', $quiz->id); $result = external_api::clean_returnvalue(core_course_external::get_course_module_by_instance_returns(), $result); $this->assertCount(0, $result['warnings']); // Test we retrieve only the few files we can see. $this->assertCount(12, $result['cm']); $this->assertEquals($quiz->cmid, $result['cm']['id']); $this->assertEquals($course->id, $result['cm']['course']); $this->assertEquals('quiz', $result['cm']['modname']); $this->assertEquals($quiz->id, $result['cm']['instance']); // Try with an invalid module name. try { core_course_external::get_course_module_by_instance('abc', $quiz->id); $this->fail('Exception expected due to invalid module name.'); } catch (dml_read_exception $e) { $this->assertEquals('dmlreadexception', $e->errorcode); } } /** * Test get_user_navigation_options */ public function test_get_user_navigation_options() { global $USER; $this->resetAfterTest(); $course1 = self::getDataGenerator()->create_course(); $course2 = self::getDataGenerator()->create_course(); // Create a viewer user. $viewer = self::getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($viewer->id, $course1->id); $this->getDataGenerator()->enrol_user($viewer->id, $course2->id); $this->setUser($viewer->id); $courses = array($course1->id , $course2->id, SITEID); $result = core_course_external::get_user_navigation_options($courses); $result = external_api::clean_returnvalue(core_course_external::get_user_navigation_options_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertCount(3, $result['courses']); foreach ($result['courses'] as $course) { $navoptions = new stdClass; foreach ($course['options'] as $option) { $navoptions->{$option['name']} = $option['available']; } $this->assertCount(8, $course['options']); if ($course['id'] == SITEID) { $this->assertTrue($navoptions->blogs); $this->assertFalse($navoptions->notes); $this->assertFalse($navoptions->participants); $this->assertTrue($navoptions->badges); $this->assertTrue($navoptions->tags); $this->assertFalse($navoptions->grades); $this->assertFalse($navoptions->search); $this->assertTrue($navoptions->competencies); } else { $this->assertTrue($navoptions->blogs); $this->assertFalse($navoptions->notes); $this->assertTrue($navoptions->participants); $this->assertFalse($navoptions->badges); $this->assertFalse($navoptions->tags); $this->assertTrue($navoptions->grades); $this->assertFalse($navoptions->search); $this->assertTrue($navoptions->competencies); } } } /** * Test get_user_administration_options */ public function test_get_user_administration_options() { global $USER; $this->resetAfterTest(); $course1 = self::getDataGenerator()->create_course(); $course2 = self::getDataGenerator()->create_course(); // Create a viewer user. $viewer = self::getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($viewer->id, $course1->id); $this->getDataGenerator()->enrol_user($viewer->id, $course2->id); $this->setUser($viewer->id); $courses = array($course1->id , $course2->id, SITEID); $result = core_course_external::get_user_administration_options($courses); $result = external_api::clean_returnvalue(core_course_external::get_user_administration_options_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertCount(3, $result['courses']); foreach ($result['courses'] as $course) { $adminoptions = new stdClass; foreach ($course['options'] as $option) { $adminoptions->{$option['name']} = $option['available']; } if ($course['id'] == SITEID) { $this->assertCount(17, $course['options']); $this->assertFalse($adminoptions->update); $this->assertFalse($adminoptions->filters); $this->assertFalse($adminoptions->reports); $this->assertFalse($adminoptions->backup); $this->assertFalse($adminoptions->restore); $this->assertFalse($adminoptions->files); $this->assertFalse(!isset($adminoptions->tags)); $this->assertFalse($adminoptions->gradebook); $this->assertFalse($adminoptions->outcomes); $this->assertFalse($adminoptions->badges); $this->assertFalse($adminoptions->import); $this->assertFalse($adminoptions->reset); $this->assertFalse($adminoptions->roles); $this->assertFalse($adminoptions->editcompletion); $this->assertFalse($adminoptions->copy); } else { $this->assertCount(15, $course['options']); $this->assertFalse($adminoptions->update); $this->assertFalse($adminoptions->filters); $this->assertFalse($adminoptions->reports); $this->assertFalse($adminoptions->backup); $this->assertFalse($adminoptions->restore); $this->assertFalse($adminoptions->files); $this->assertFalse($adminoptions->tags); $this->assertFalse($adminoptions->gradebook); $this->assertFalse($adminoptions->outcomes); $this->assertTrue($adminoptions->badges); $this->assertFalse($adminoptions->import); $this->assertFalse($adminoptions->reset); $this->assertFalse($adminoptions->roles); $this->assertFalse($adminoptions->editcompletion); $this->assertFalse($adminoptions->copy); } } } /** * Test get_courses_by_fields */ public function test_get_courses_by_field() { global $DB; $this->resetAfterTest(true); $category1 = self::getDataGenerator()->create_category(array('name' => 'Cat 1')); $category2 = self::getDataGenerator()->create_category(array('parent' => $category1->id)); $course1 = self::getDataGenerator()->create_course( array('category' => $category1->id, 'shortname' => 'c1', 'format' => 'topics')); $fieldcategory = self::getDataGenerator()->create_custom_field_category(['name' => 'Other fields']); $customfield = ['shortname' => 'test', 'name' => 'Custom field', 'type' => 'text', 'categoryid' => $fieldcategory->get('id')]; $field = self::getDataGenerator()->create_custom_field($customfield); $customfieldvalue = ['shortname' => 'test', 'value' => 'Test value']; $course2 = self::getDataGenerator()->create_course(array('visible' => 0, 'category' => $category2->id, 'idnumber' => 'i2', 'customfields' => [$customfieldvalue])); $student1 = self::getDataGenerator()->create_user(); $user1 = self::getDataGenerator()->create_user(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); self::getDataGenerator()->enrol_user($student1->id, $course1->id, $studentrole->id); self::getDataGenerator()->enrol_user($student1->id, $course2->id, $studentrole->id); self::setAdminUser(); // As admins, we should be able to retrieve everything. $result = core_course_external::get_courses_by_field(); $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result); $this->assertCount(3, $result['courses']); // Expect to receive all the fields. $this->assertCount(40, $result['courses'][0]); $this->assertCount(41, $result['courses'][1]); // One more field because is not the site course. $this->assertCount(41, $result['courses'][2]); // One more field because is not the site course. $result = core_course_external::get_courses_by_field('id', $course1->id); $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result); $this->assertCount(1, $result['courses']); $this->assertEquals($course1->id, $result['courses'][0]['id']); // Expect to receive all the fields. $this->assertCount(41, $result['courses'][0]); // Check default values for course format topics. $this->assertCount(3, $result['courses'][0]['courseformatoptions']); foreach ($result['courses'][0]['courseformatoptions'] as $option) { switch ($option['name']) { case 'hiddensections': $this->assertEquals(1, $option['value']); break; case 'coursedisplay': $this->assertEquals(0, $option['value']); break; case 'indentation': $this->assertEquals(1, $option['value']); break; default: } } $result = core_course_external::get_courses_by_field('id', $course2->id); $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result); $this->assertCount(1, $result['courses']); $this->assertEquals($course2->id, $result['courses'][0]['id']); // Check custom fields properly returned. $this->assertEquals([ 'shortname' => $customfield['shortname'], 'name' => $customfield['name'], 'type' => $customfield['type'], 'value' => $customfieldvalue['value'], 'valueraw' => $customfieldvalue['value'], ], $result['courses'][0]['customfields'][0]); $result = core_course_external::get_courses_by_field('ids', "$course1->id,$course2->id"); $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result); $this->assertCount(2, $result['courses']); // Check default filters. $this->assertCount(6, $result['courses'][0]['filters']); $this->assertCount(6, $result['courses'][1]['filters']); $result = core_course_external::get_courses_by_field('category', $category1->id); $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result); $this->assertCount(1, $result['courses']); $this->assertEquals($course1->id, $result['courses'][0]['id']); $this->assertEquals('Cat 1', $result['courses'][0]['categoryname']); $result = core_course_external::get_courses_by_field('shortname', 'c1'); $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result); $this->assertCount(1, $result['courses']); $this->assertEquals($course1->id, $result['courses'][0]['id']); $result = core_course_external::get_courses_by_field('idnumber', 'i2'); $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result); $this->assertCount(1, $result['courses']); $this->assertEquals($course2->id, $result['courses'][0]['id']); $result = core_course_external::get_courses_by_field('idnumber', 'x'); $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result); $this->assertCount(0, $result['courses']); // Change filter value. filter_set_local_state('mediaplugin', context_course::instance($course1->id)->id, TEXTFILTER_OFF); self::setUser($student1); // All visible courses (including front page) for normal student. $result = core_course_external::get_courses_by_field(); $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result); $this->assertCount(2, $result['courses']); $this->assertCount(33, $result['courses'][0]); $this->assertCount(34, $result['courses'][1]); // One field more (course format options), not present in site course. $result = core_course_external::get_courses_by_field('id', $course1->id); $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result); $this->assertCount(1, $result['courses']); $this->assertEquals($course1->id, $result['courses'][0]['id']); // Expect to receive all the files that a student can see. $this->assertCount(34, $result['courses'][0]); // Check default filters. $filters = $result['courses'][0]['filters']; $this->assertCount(6, $filters); $found = false; foreach ($filters as $filter) { if ($filter['filter'] == 'mediaplugin' and $filter['localstate'] == TEXTFILTER_OFF) { $found = true; } } $this->assertTrue($found); // Course 2 is not visible. $result = core_course_external::get_courses_by_field('id', $course2->id); $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result); $this->assertCount(0, $result['courses']); $result = core_course_external::get_courses_by_field('ids', "$course1->id,$course2->id"); $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result); $this->assertCount(1, $result['courses']); $result = core_course_external::get_courses_by_field('category', $category1->id); $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result); $this->assertCount(1, $result['courses']); $this->assertEquals($course1->id, $result['courses'][0]['id']); $result = core_course_external::get_courses_by_field('shortname', 'c1'); $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result); $this->assertCount(1, $result['courses']); $this->assertEquals($course1->id, $result['courses'][0]['id']); $result = core_course_external::get_courses_by_field('idnumber', 'i2'); $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result); $this->assertCount(0, $result['courses']); $result = core_course_external::get_courses_by_field('idnumber', 'x'); $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result); $this->assertCount(0, $result['courses']); self::setUser($user1); // All visible courses (including front page) for authenticated user. $result = core_course_external::get_courses_by_field(); $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result); $this->assertCount(2, $result['courses']); $this->assertCount(33, $result['courses'][0]); // Site course. $this->assertCount(16, $result['courses'][1]); // Only public information, not enrolled. $result = core_course_external::get_courses_by_field('id', $course1->id); $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result); $this->assertCount(1, $result['courses']); $this->assertEquals($course1->id, $result['courses'][0]['id']); // Expect to receive all the files that a authenticated can see. $this->assertCount(16, $result['courses'][0]); // Course 2 is not visible. $result = core_course_external::get_courses_by_field('id', $course2->id); $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result); $this->assertCount(0, $result['courses']); $result = core_course_external::get_courses_by_field('ids', "$course1->id,$course2->id"); $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result); $this->assertCount(1, $result['courses']); $result = core_course_external::get_courses_by_field('category', $category1->id); $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result); $this->assertCount(1, $result['courses']); $this->assertEquals($course1->id, $result['courses'][0]['id']); $result = core_course_external::get_courses_by_field('shortname', 'c1'); $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result); $this->assertCount(1, $result['courses']); $this->assertEquals($course1->id, $result['courses'][0]['id']); $result = core_course_external::get_courses_by_field('idnumber', 'i2'); $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result); $this->assertCount(0, $result['courses']); $result = core_course_external::get_courses_by_field('idnumber', 'x'); $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result); $this->assertCount(0, $result['courses']); } /** * Test retrieving courses by field returns custom field data */ public function test_get_courses_by_field_customfields(): void { $this->resetAfterTest(); $this->setAdminUser(); $fieldcategory = $this->getDataGenerator()->create_custom_field_category([]); $datefield = $this->getDataGenerator()->create_custom_field([ 'categoryid' => $fieldcategory->get('id'), 'shortname' => 'mydate', 'name' => 'My date', 'type' => 'date', ]); $newcourse = $this->getDataGenerator()->create_course(['customfields' => [ [ 'shortname' => $datefield->get('shortname'), 'value' => 1580389200, // 30/01/2020 13:00 GMT. ], ]]); $result = external_api::clean_returnvalue( core_course_external::get_courses_by_field_returns(), core_course_external::get_courses_by_field('id', $newcourse->id) ); $this->assertCount(1, $result['courses']); $course = reset($result['courses']); $this->assertArrayHasKey('customfields', $course); $this->assertCount(1, $course['customfields']); // Assert the received custom field, "value" containing a human-readable version and "valueraw" the unmodified version. $this->assertEquals([ 'name' => $datefield->get('name'), 'shortname' => $datefield->get('shortname'), 'type' => $datefield->get('type'), 'value' => userdate(1580389200), 'valueraw' => 1580389200, ], reset($course['customfields'])); } public function test_get_courses_by_field_invalid_field() { $this->expectException('invalid_parameter_exception'); $result = core_course_external::get_courses_by_field('zyx', 'x'); } public function test_get_courses_by_field_invalid_courses() { $result = core_course_external::get_courses_by_field('id', '-1'); $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result); $this->assertCount(0, $result['courses']); } /** * Test get_courses_by_field_invalid_theme_and_lang */ public function test_get_courses_by_field_invalid_theme_and_lang() { $this->resetAfterTest(true); $this->setAdminUser(); $course = self::getDataGenerator()->create_course(array('theme' => 'kkt', 'lang' => 'kkl')); $result = core_course_external::get_courses_by_field('id', $course->id); $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result); $this->assertEmpty($result['courses']['0']['theme']); $this->assertEmpty($result['courses']['0']['lang']); } public function test_check_updates() { global $DB; $this->resetAfterTest(true); $this->setAdminUser(); // Create different types of activities. $course = self::getDataGenerator()->create_course(); $tocreate = array('assign', 'book', 'choice', 'folder', 'forum', 'glossary', 'imscp', 'label', 'lti', 'page', 'quiz', 'resource', 'scorm', 'survey', 'url', 'wiki'); $modules = array(); foreach ($tocreate as $modname) { $modules[$modname]['instance'] = $this->getDataGenerator()->create_module($modname, array('course' => $course->id)); $modules[$modname]['cm'] = get_coursemodule_from_id(false, $modules[$modname]['instance']->cmid); $modules[$modname]['context'] = context_module::instance($modules[$modname]['instance']->cmid); } $student = self::getDataGenerator()->create_user(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); self::getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id); $this->setUser($student); $since = time(); $this->waitForSecond(); $params = array(); foreach ($modules as $modname => $data) { $params[$data['cm']->id] = array( 'contextlevel' => 'module', 'id' => $data['cm']->id, 'since' => $since ); } // Check there is nothing updated because modules are fresh new. $result = core_course_external::check_updates($course->id, $params); $result = external_api::clean_returnvalue(core_course_external::check_updates_returns(), $result); $this->assertCount(0, $result['instances']); $this->assertCount(0, $result['warnings']); // Test with get_updates_since the same data. $result = core_course_external::get_updates_since($course->id, $since); $result = external_api::clean_returnvalue(core_course_external::get_updates_since_returns(), $result); $this->assertCount(0, $result['instances']); $this->assertCount(0, $result['warnings']); // Update a module after a second. $this->waitForSecond(); set_coursemodule_name($modules['forum']['cm']->id, 'New forum name'); $found = false; $result = core_course_external::check_updates($course->id, $params); $result = external_api::clean_returnvalue(core_course_external::check_updates_returns(), $result); $this->assertCount(1, $result['instances']); $this->assertCount(0, $result['warnings']); foreach ($result['instances'] as $module) { foreach ($module['updates'] as $update) { if ($module['id'] == $modules['forum']['cm']->id and $update['name'] == 'configuration') { $found = true; } } } $this->assertTrue($found); // Test with get_updates_since the same data. $result = core_course_external::get_updates_since($course->id, $since); $result = external_api::clean_returnvalue(core_course_external::get_updates_since_returns(), $result); $this->assertCount(1, $result['instances']); $this->assertCount(0, $result['warnings']); $found = false; $this->assertCount(1, $result['instances']); $this->assertCount(0, $result['warnings']); foreach ($result['instances'] as $module) { foreach ($module['updates'] as $update) { if ($module['id'] == $modules['forum']['cm']->id and $update['name'] == 'configuration') { $found = true; } } } $this->assertTrue($found); // Do not retrieve the configuration field. $filter = array('files'); $found = false; $result = core_course_external::check_updates($course->id, $params, $filter); $result = external_api::clean_returnvalue(core_course_external::check_updates_returns(), $result); $this->assertCount(0, $result['instances']); $this->assertCount(0, $result['warnings']); $this->assertFalse($found); // Add invalid cmid. $params[] = array( 'contextlevel' => 'module', 'id' => -2, 'since' => $since ); $result = core_course_external::check_updates($course->id, $params); $result = external_api::clean_returnvalue(core_course_external::check_updates_returns(), $result); $this->assertCount(1, $result['warnings']); $this->assertEquals(-2, $result['warnings'][0]['itemid']); } /** * Test cases for the get_enrolled_courses_by_timeline_classification test. */ public function get_get_enrolled_courses_by_timeline_classification_test_cases():array { $now = time(); $day = 86400; $coursedata = [ [ 'shortname' => 'apast', 'startdate' => $now - ($day * 2), 'enddate' => $now - $day ], [ 'shortname' => 'bpast', 'startdate' => $now - ($day * 2), 'enddate' => $now - $day ], [ 'shortname' => 'cpast', 'startdate' => $now - ($day * 2), 'enddate' => $now - $day ], [ 'shortname' => 'dpast', 'startdate' => $now - ($day * 2), 'enddate' => $now - $day ], [ 'shortname' => 'epast', 'startdate' => $now - ($day * 2), 'enddate' => $now - $day ], [ 'shortname' => 'ainprogress', 'startdate' => $now - $day, 'enddate' => $now + $day ], [ 'shortname' => 'binprogress', 'startdate' => $now - $day, 'enddate' => $now + $day ], [ 'shortname' => 'cinprogress', 'startdate' => $now - $day, 'enddate' => $now + $day ], [ 'shortname' => 'dinprogress', 'startdate' => $now - $day, 'enddate' => $now + $day ], [ 'shortname' => 'einprogress', 'startdate' => $now - $day, 'enddate' => $now + $day ], [ 'shortname' => 'afuture', 'startdate' => $now + $day ], [ 'shortname' => 'bfuture', 'startdate' => $now + $day ], [ 'shortname' => 'cfuture', 'startdate' => $now + $day ], [ 'shortname' => 'dfuture', 'startdate' => $now + $day ], [ 'shortname' => 'efuture', 'startdate' => $now + $day ] ]; // Raw enrolled courses result set should be returned in this order: // afuture, ainprogress, apast, bfuture, binprogress, bpast, cfuture, cinprogress, cpast, // dfuture, dinprogress, dpast, efuture, einprogress, epast // // By classification the offset values for each record should be: // COURSE_TIMELINE_FUTURE // 0 (afuture), 3 (bfuture), 6 (cfuture), 9 (dfuture), 12 (efuture) // COURSE_TIMELINE_INPROGRESS // 1 (ainprogress), 4 (binprogress), 7 (cinprogress), 10 (dinprogress), 13 (einprogress) // COURSE_TIMELINE_PAST // 2 (apast), 5 (bpast), 8 (cpast), 11 (dpast), 14 (epast). // // NOTE: The offset applies to the unfiltered full set of courses before the classification // filtering is done. // E.g. In our example if an offset of 2 is given then it would mean the first // two courses (afuture, ainprogress) are ignored. return [ 'empty set' => [ 'coursedata' => [], 'classification' => 'future', 'limit' => 2, 'offset' => 0, 'sort' => 'shortname ASC', 'expectedcourses' => [], 'expectednextoffset' => 0, ], // COURSE_TIMELINE_FUTURE. 'future not limit no offset' => [ 'coursedata' => $coursedata, 'classification' => 'future', 'limit' => 0, 'offset' => 0, 'sort' => 'shortname ASC', 'expectedcourses' => ['afuture', 'bfuture', 'cfuture', 'dfuture', 'efuture'], 'expectednextoffset' => 15, ], 'future no offset' => [ 'coursedata' => $coursedata, 'classification' => 'future', 'limit' => 2, 'offset' => 0, 'sort' => 'shortname ASC', 'expectedcourses' => ['afuture', 'bfuture'], 'expectednextoffset' => 4, ], 'future offset' => [ 'coursedata' => $coursedata, 'classification' => 'future', 'limit' => 2, 'offset' => 2, 'sort' => 'shortname ASC', 'expectedcourses' => ['bfuture', 'cfuture'], 'expectednextoffset' => 7, ], 'future exact limit' => [ 'coursedata' => $coursedata, 'classification' => 'future', 'limit' => 5, 'offset' => 0, 'sort' => 'shortname ASC', 'expectedcourses' => ['afuture', 'bfuture', 'cfuture', 'dfuture', 'efuture'], 'expectednextoffset' => 13, ], 'future limit less results' => [ 'coursedata' => $coursedata, 'classification' => 'future', 'limit' => 10, 'offset' => 0, 'sort' => 'shortname ASC', 'expectedcourses' => ['afuture', 'bfuture', 'cfuture', 'dfuture', 'efuture'], 'expectednextoffset' => 15, ], 'future limit less results with offset' => [ 'coursedata' => $coursedata, 'classification' => 'future', 'limit' => 10, 'offset' => 5, 'sort' => 'shortname ASC', 'expectedcourses' => ['cfuture', 'dfuture', 'efuture'], 'expectednextoffset' => 15, ], 'all no limit or offset' => [ 'coursedata' => $coursedata, 'classification' => 'all', 'limit' => 0, 'offset' => 0, 'sort' => 'shortname ASC', 'expectedcourses' => [ 'afuture', 'ainprogress', 'apast', 'bfuture', 'binprogress', 'bpast', 'cfuture', 'cinprogress', 'cpast', 'dfuture', 'dinprogress', 'dpast', 'efuture', 'einprogress', 'epast' ], 'expectednextoffset' => 15, ], 'all limit no offset' => [ 'coursedata' => $coursedata, 'classification' => 'all', 'limit' => 5, 'offset' => 0, 'sort' => 'shortname ASC', 'expectedcourses' => [ 'afuture', 'ainprogress', 'apast', 'bfuture', 'binprogress' ], 'expectednextoffset' => 5, ], 'all limit and offset' => [ 'coursedata' => $coursedata, 'classification' => 'all', 'limit' => 5, 'offset' => 5, 'sort' => 'shortname ASC', 'expectedcourses' => [ 'bpast', 'cfuture', 'cinprogress', 'cpast', 'dfuture' ], 'expectednextoffset' => 10, ], 'all offset past result set' => [ 'coursedata' => $coursedata, 'classification' => 'all', 'limit' => 5, 'offset' => 50, 'sort' => 'shortname ASC', 'expectedcourses' => [], 'expectednextoffset' => 50, ], 'all limit and offset with sort ul.timeaccess desc' => [ 'coursedata' => $coursedata, 'classification' => 'inprogress', 'limit' => 0, 'offset' => 0, 'sort' => 'ul.timeaccess desc', 'expectedcourses' => [ 'ainprogress', 'binprogress', 'cinprogress', 'dinprogress', 'einprogress' ], 'expectednextoffset' => 15, ], 'all limit and offset with sort sql injection for sort or 1==1' => [ 'coursedata' => $coursedata, 'classification' => 'all', 'limit' => 5, 'offset' => 5, 'sort' => 'ul.timeaccess desc or 1==1', 'expectedcourses' => [], 'expectednextoffset' => 0, 'expectedexception' => 'Invalid $sort parameter in enrol_get_my_courses()', ], 'all limit and offset with sql injection of sort a custom one' => [ 'coursedata' => $coursedata, 'classification' => 'all', 'limit' => 5, 'offset' => 5, 'sort' => "ul.timeaccess LIMIT 1--", 'expectedcourses' => [], 'expectednextoffset' => 0, 'expectedexception' => 'Invalid $sort parameter in enrol_get_my_courses()', ], 'all limit and offset with wrong sort direction' => [ 'coursedata' => $coursedata, 'classification' => 'all', 'limit' => 5, 'offset' => 5, 'sort' => "ul.timeaccess abcdasc", 'expectedcourses' => [], 'expectednextoffset' => 0, 'expectedexception' => 'Invalid sort direction in $sort parameter in enrol_get_my_courses()', ], 'all limit and offset with wrong sort direction' => [ 'coursedata' => $coursedata, 'classification' => 'all', 'limit' => 5, 'offset' => 5, 'sort' => "ul.timeaccess.foo ascd", 'expectedcourses' => [], 'expectednextoffset' => 0, 'expectedexception' => 'Invalid sort direction in $sort parameter in enrol_get_my_courses()', ], 'all limit and offset with wrong sort param' => [ 'coursedata' => $coursedata, 'classification' => 'all', 'limit' => 5, 'offset' => 5, 'sort' => "foobar", 'expectedcourses' => [], 'expectednextoffset' => 0, 'expectedexception' => 'Invalid $sort parameter in enrol_get_my_courses()', ], 'all limit and offset with wrong field name' => [ 'coursedata' => $coursedata, 'classification' => 'all', 'limit' => 5, 'offset' => 5, 'sort' => "ul.foobar", 'expectedcourses' => [], 'expectednextoffset' => 0, 'expectedexception' => 'Invalid $sort parameter in enrol_get_my_courses()', ], 'all limit and offset with wrong field separator' => [ 'coursedata' => $coursedata, 'classification' => 'all', 'limit' => 5, 'offset' => 5, 'sort' => "ul.timeaccess.foo", 'expectedcourses' => [], 'expectednextoffset' => 0, 'expectedexception' => 'Invalid $sort parameter in enrol_get_my_courses()', ], 'all limit and offset with wrong field separator #' => [ 'coursedata' => $coursedata, 'classification' => 'all', 'limit' => 5, 'offset' => 5, 'sort' => "ul#timeaccess", 'expectedcourses' => [], 'expectednextoffset' => 0, 'expectedexception' => 'Invalid $sort parameter in enrol_get_my_courses()', ], 'all limit and offset with wrong field separator $' => [ 'coursedata' => $coursedata, 'classification' => 'all', 'limit' => 5, 'offset' => 5, 'sort' => 'ul$timeaccess', 'expectedcourses' => [], 'expectednextoffset' => 0, 'expectedexception' => 'Invalid $sort parameter in enrol_get_my_courses()', ], 'all limit and offset with wrong field name' => [ 'coursedata' => $coursedata, 'classification' => 'all', 'limit' => 5, 'offset' => 5, 'sort' => 'timeaccess123', 'expectedcourses' => [], 'expectednextoffset' => 0, 'expectedexception' => 'Invalid $sort parameter in enrol_get_my_courses()', ], 'all limit and offset with no sort direction for ul' => [ 'coursedata' => $coursedata, 'classification' => 'inprogress', 'limit' => 0, 'offset' => 0, 'sort' => "ul.timeaccess", 'expectedcourses' => ['ainprogress', 'binprogress', 'cinprogress', 'dinprogress', 'einprogress'], 'expectednextoffset' => 15, ], 'all limit and offset with valid field name and no prefix, test for ul' => [ 'coursedata' => $coursedata, 'classification' => 'inprogress', 'limit' => 0, 'offset' => 0, 'sort' => "timeaccess", 'expectedcourses' => ['ainprogress', 'binprogress', 'cinprogress', 'dinprogress', 'einprogress'], 'expectednextoffset' => 15, ], 'all limit and offset with valid field name and no prefix' => [ 'coursedata' => $coursedata, 'classification' => 'all', 'limit' => 5, 'offset' => 5, 'sort' => "fullname", 'expectedcourses' => ['bpast', 'cpast', 'dfuture', 'dpast', 'efuture'], 'expectednextoffset' => 10, ], 'all limit and offset with valid field name and no prefix and with sort direction' => [ 'coursedata' => $coursedata, 'classification' => 'all', 'limit' => 5, 'offset' => 5, 'sort' => "fullname desc", 'expectedcourses' => ['bpast', 'cpast', 'dfuture', 'dpast', 'efuture'], 'expectednextoffset' => 10, ], 'Search courses for courses containing bfut' => [ 'coursedata' => $coursedata, 'classification' => 'search', 'limit' => 0, 'offset' => 0, 'sort' => null, 'expectedcourses' => ['bfuture'], 'expectednextoffset' => 1, 'expectedexception' => null, 'searchvalue' => 'bfut', ], 'Search courses for courses containing inp' => [ 'coursedata' => $coursedata, 'classification' => 'search', 'limit' => 0, 'offset' => 0, 'sort' => null, 'expectedcourses' => ['ainprogress', 'binprogress', 'cinprogress', 'dinprogress', 'einprogress'], 'expectednextoffset' => 5, 'expectedexception' => null, 'searchvalue' => 'inp', ], 'Search courses for courses containing fail' => [ 'coursedata' => $coursedata, 'classification' => 'search', 'limit' => 0, 'offset' => 0, 'sort' => null, 'expectedcourses' => [], 'expectednextoffset' => 0, 'expectedexception' => null, 'searchvalue' => 'fail', ], 'Search courses for courses containing !`~[]C' => [ 'coursedata' => $coursedata, 'classification' => 'search', 'limit' => 0, 'offset' => 0, 'sort' => null, 'expectedcourses' => [], 'expectednextoffset' => 0, 'expectedexception' => null, 'searchvalue' => '!`~[]C', ], ]; } /** * Test the get_enrolled_courses_by_timeline_classification function. * * @dataProvider get_get_enrolled_courses_by_timeline_classification_test_cases() * @param array $coursedata Courses to create * @param string $classification Timeline classification * @param int $limit Maximum number of results * @param int $offset Offset the unfiltered courses result set by this amount * @param string $sort sort the courses * @param array $expectedcourses Expected courses in result * @param int $expectednextoffset Expected next offset value in result * @param string|null $expectedexception Expected exception string * @param string|null $searchvalue If we are searching, what do we need to look for? */ public function test_get_enrolled_courses_by_timeline_classification( $coursedata, $classification, $limit, $offset, $sort, $expectedcourses, $expectednextoffset, $expectedexception = null, $searchvalue = null ) { $this->resetAfterTest(); $generator = $this->getDataGenerator(); $courses = array_map(function($coursedata) use ($generator) { return $generator->create_course($coursedata); }, $coursedata); $student = $generator->create_user(); foreach ($courses as $course) { $generator->enrol_user($student->id, $course->id, 'student'); } $this->setUser($student); if (isset($expectedexception)) { $this->expectException('coding_exception'); $this->expectExceptionMessage($expectedexception); } // NOTE: The offset applies to the unfiltered full set of courses before the classification // filtering is done. // E.g. In our example if an offset of 2 is given then it would mean the first // two courses (afuture, ainprogress) are ignored. $result = core_course_external::get_enrolled_courses_by_timeline_classification( $classification, $limit, $offset, $sort, null, null, $searchvalue ); $result = external_api::clean_returnvalue( core_course_external::get_enrolled_courses_by_timeline_classification_returns(), $result ); $actual = array_map(function($course) { return $course['shortname']; }, $result['courses']); $this->assertEqualsCanonicalizing($expectedcourses, $actual); $this->assertEquals($expectednextoffset, $result['nextoffset']); } /** * Test the get_recent_courses function. */ public function test_get_recent_courses() { global $USER, $DB; $this->resetAfterTest(); $generator = $this->getDataGenerator(); set_config('hiddenuserfields', 'lastaccess'); $courses = array(); for ($i = 1; $i < 12; $i++) { $courses[] = $generator->create_course(); }; $student = $generator->create_user(); $teacher = $generator->create_user(); foreach ($courses as $course) { $generator->enrol_user($student->id, $course->id, 'student'); } $generator->enrol_user($teacher->id, $courses[0]->id, 'teacher'); $this->setUser($student); $result = core_course_external::get_recent_courses($USER->id); // No course accessed. $this->assertCount(0, $result); foreach ($courses as $course) { core_course_external::view_course($course->id); } // Every course accessed. $result = core_course_external::get_recent_courses($USER->id); $this->assertCount( 11, $result); // Every course accessed, result limited to 10 courses. $result = core_course_external::get_recent_courses($USER->id, 10); $this->assertCount(10, $result); $guestcourse = $generator->create_course( (object)array('shortname' => 'guestcourse', 'enrol_guest_status_0' => ENROL_INSTANCE_ENABLED, 'enrol_guest_password_0' => '')); core_course_external::view_course($guestcourse->id); // Every course accessed, even the not enrolled one. $result = core_course_external::get_recent_courses($USER->id); $this->assertCount(12, $result); // Offset 5, return 7 out of 12. $result = core_course_external::get_recent_courses($USER->id, 0, 5); $this->assertCount(7, $result); // Offset 5 and limit 3, return 3 out of 12. $result = core_course_external::get_recent_courses($USER->id, 3, 5); $this->assertCount(3, $result); // Sorted by course id ASC. $result = core_course_external::get_recent_courses($USER->id, 0, 0, 'id ASC'); $this->assertEquals($courses[0]->id, array_shift($result)->id); // Sorted by course id DESC. $result = core_course_external::get_recent_courses($USER->id, 0, 0, 'id DESC'); $this->assertEquals($guestcourse->id, array_shift($result)->id); // If last access is hidden, only get the courses where has viewhiddenuserfields capability. $this->setUser($teacher); $teacherroleid = $DB->get_field('role', 'id', array('shortname' => 'editingteacher')); $usercontext = context_user::instance($student->id); $this->assignUserCapability('moodle/user:viewdetails', $usercontext, $teacherroleid); // Sorted by course id DESC. $result = core_course_external::get_recent_courses($student->id); $this->assertCount(1, $result); $this->assertEquals($courses[0]->id, array_shift($result)->id); } /** * Test get enrolled users by cmid function. */ public function test_get_enrolled_users_by_cmid() { global $PAGE; $this->resetAfterTest(true); $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); $user3 = self::getDataGenerator()->create_user(); $user1picture = new user_picture($user1); $user1picture->size = 1; $user1->profileimage = $user1picture->get_url($PAGE)->out(false); $user2picture = new user_picture($user2); $user2picture->size = 1; $user2->profileimage = $user2picture->get_url($PAGE)->out(false); $user3picture = new user_picture($user3); $user3picture->size = 1; $user3->profileimage = $user3picture->get_url($PAGE)->out(false); // Set the first created user to the test user. self::setUser($user1); // Create course to add the module. $course1 = self::getDataGenerator()->create_course(); // Forum with tracking off. $record = new stdClass(); $record->course = $course1->id; $forum1 = self::getDataGenerator()->create_module('forum', $record); // Following lines enrol and assign default role id to the users. $this->getDataGenerator()->enrol_user($user1->id, $course1->id); $this->getDataGenerator()->enrol_user($user2->id, $course1->id); // Enrol a suspended user in the course. $this->getDataGenerator()->enrol_user($user3->id, $course1->id, null, 'manual', 0, 0, ENROL_USER_SUSPENDED); // Create what we expect to be returned when querying the course module. $expectedusers = array( 'users' => array(), 'warnings' => array(), ); $expectedusers['users'][0] = [ 'id' => $user1->id, 'fullname' => fullname($user1), 'firstname' => $user1->firstname, 'lastname' => $user1->lastname, 'profileimage' => $user1->profileimage, ]; $expectedusers['users'][1] = [ 'id' => $user2->id, 'fullname' => fullname($user2), 'firstname' => $user2->firstname, 'lastname' => $user2->lastname, 'profileimage' => $user2->profileimage, ]; $expectedusers['users'][2] = [ 'id' => $user3->id, 'fullname' => fullname($user3), 'firstname' => $user3->firstname, 'lastname' => $user3->lastname, 'profileimage' => $user3->profileimage, ]; // Test getting the users in a given context. $users = core_course_external::get_enrolled_users_by_cmid($forum1->cmid); $users = external_api::clean_returnvalue(core_course_external::get_enrolled_users_by_cmid_returns(), $users); $this->assertEquals(3, count($users['users'])); $this->assertEquals($expectedusers, $users); // Test getting only the active users in a given context. $users = core_course_external::get_enrolled_users_by_cmid($forum1->cmid, 0, true); $users = external_api::clean_returnvalue(core_course_external::get_enrolled_users_by_cmid_returns(), $users); $expectedusers['users'] = [ [ 'id' => $user1->id, 'fullname' => fullname($user1), 'firstname' => $user1->firstname, 'lastname' => $user1->lastname, 'profileimage' => $user1->profileimage, ], [ 'id' => $user2->id, 'fullname' => fullname($user2), 'firstname' => $user2->firstname, 'lastname' => $user2->lastname, 'profileimage' => $user2->profileimage, ] ]; $this->assertEquals(2, count($users['users'])); $this->assertEquals($expectedusers, $users); } /** * Verify that content items can be added to user favourites. */ public function test_add_content_item_to_user_favourites() { $this->resetAfterTest(); $course = $this->getDataGenerator()->create_course(); $user = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher'); $this->setUser($user); // Using the internal API, confirm that no items are set as favourites for the user. $contentitemservice = new \core_course\local\service\content_item_service( new \core_course\local\repository\content_item_readonly_repository() ); $contentitems = $contentitemservice->get_all_content_items($user); $favourited = array_filter($contentitems, function($contentitem) { return $contentitem->favourite == true; }); $this->assertCount(0, $favourited); // Using the external API, favourite a content item for the user. $assign = $contentitems[array_search('assign', array_column($contentitems, 'name'))]; $contentitem = core_course_external::add_content_item_to_user_favourites('mod_assign', $assign->id, $user->id); $contentitem = external_api::clean_returnvalue(core_course_external::add_content_item_to_user_favourites_returns(), $contentitem); // Verify the returned item is a favourite. $this->assertTrue($contentitem['favourite']); // Using the internal API, confirm we see a single favourite item. $contentitems = $contentitemservice->get_all_content_items($user); $favourited = array_values(array_filter($contentitems, function($contentitem) { return $contentitem->favourite == true; })); $this->assertCount(1, $favourited); $this->assertEquals('assign', $favourited[0]->name); } /** * Verify that content items can be removed from user favourites. */ public function test_remove_content_item_from_user_favourites() { $this->resetAfterTest(); $course = $this->getDataGenerator()->create_course(); $user = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher'); $this->setUser($user); // Using the internal API, set a favourite for the user. $contentitemservice = new \core_course\local\service\content_item_service( new \core_course\local\repository\content_item_readonly_repository() ); $contentitems = $contentitemservice->get_all_content_items($user); $assign = $contentitems[array_search('assign', array_column($contentitems, 'name'))]; $contentitemservice->add_to_user_favourites($user, $assign->componentname, $assign->id); $contentitems = $contentitemservice->get_all_content_items($user); $favourited = array_filter($contentitems, function($contentitem) { return $contentitem->favourite == true; }); $this->assertCount(1, $favourited); // Now, verify the external API can remove the favourite. $contentitem = core_course_external::remove_content_item_from_user_favourites('mod_assign', $assign->id); $contentitem = external_api::clean_returnvalue(core_course_external::remove_content_item_from_user_favourites_returns(), $contentitem); // Verify the returned item is a favourite. $this->assertFalse($contentitem['favourite']); // Using the internal API, confirm we see no favourite items. $contentitems = $contentitemservice->get_all_content_items($user); $favourited = array_filter($contentitems, function($contentitem) { return $contentitem->favourite == true; }); $this->assertCount(0, $favourited); } /** * Test the web service returning course content items for inclusion in activity choosers, etc. */ public function test_get_course_content_items() { $this->resetAfterTest(); $course = self::getDataGenerator()->create_course(); $user = self::getDataGenerator()->create_and_enrol($course, 'editingteacher'); // Fetch available content items as the editing teacher. $this->setUser($user); $result = core_course_external::get_course_content_items($course->id); $result = external_api::clean_returnvalue(core_course_external::get_course_content_items_returns(), $result); $contentitemservice = new \core_course\local\service\content_item_service( new \core_course\local\repository\content_item_readonly_repository() ); // Check if the webservice returns exactly what the service defines, albeit in array form. $serviceitemsasarray = array_map(function($item) { return (array) $item; }, $contentitemservice->get_content_items_for_user_in_course($user, $course)); $this->assertEquals($serviceitemsasarray, $result['content_items']); } /** * Test the web service returning course content items, specifically in case where the user can't manage activities. */ public function test_get_course_content_items_no_permission_to_manage() { $this->resetAfterTest(); $course = self::getDataGenerator()->create_course(); $user = self::getDataGenerator()->create_and_enrol($course, 'student'); // Fetch available content items as a student, who won't have the permission to manage activities. $this->setUser($user); $result = core_course_external::get_course_content_items($course->id); $result = external_api::clean_returnvalue(core_course_external::get_course_content_items_returns(), $result); $this->assertEmpty($result['content_items']); } /** * Test toggling the recommendation of an activity. */ public function test_toggle_activity_recommendation() { global $CFG; $this->resetAfterTest(); $context = context_system::instance(); $usercontext = context_user::instance($CFG->siteguest); $component = 'core_course'; $favouritefactory = \core_favourites\service_factory::get_service_for_user_context($usercontext); $areaname = 'test_core'; $areaid = 3; // Test we have the favourite. $this->setAdminUser(); $result = core_course_external::toggle_activity_recommendation($areaname, $areaid); $this->assertTrue($favouritefactory->favourite_exists($component, \core_course\local\service\content_item_service::RECOMMENDATION_PREFIX . $areaname, $areaid, $context)); $this->assertTrue($result['status']); // Test that it is now gone. $result = core_course_external::toggle_activity_recommendation($areaname, $areaid); $this->assertFalse($favouritefactory->favourite_exists($component, $areaname, $areaid, $context)); $this->assertFalse($result['status']); } } home3/cpr76684/public_html/Aem/group/tests/externallib_test.php 0000644 00000116345 15152360066 0020363 0 ustar 00 <?php // This file is part of Moodle - http://moodle.org/ // // Moodle is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // Moodle is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Moodle. If not, see <http://www.gnu.org/licenses/>. namespace core_group; use core_group_external; use externallib_advanced_testcase; defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->dirroot . '/webservice/tests/helpers.php'); require_once($CFG->dirroot . '/group/externallib.php'); require_once($CFG->dirroot . '/group/lib.php'); /** * 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 */ public function test_create_groups() { global $DB; $this->resetAfterTest(true); $course = self::getDataGenerator()->create_course(); $group1 = array(); $group1['courseid'] = $course->id; $group1['name'] = 'Group Test 1'; $group1['description'] = 'Group Test 1 description'; $group1['descriptionformat'] = FORMAT_MOODLE; $group1['enrolmentkey'] = 'Test group enrol secret phrase'; $group1['idnumber'] = 'TEST1'; $group2 = array(); $group2['courseid'] = $course->id; $group2['name'] = 'Group Test 2'; $group2['description'] = 'Group Test 2 description'; $group3 = array(); $group3['courseid'] = $course->id; $group3['name'] = 'Group Test 3'; $group3['description'] = 'Group Test 3 description'; $group3['idnumber'] = 'TEST1'; $group4 = array(); $group4['courseid'] = $course->id; $group4['name'] = 'Group Test 4'; $group4['description'] = 'Group Test 4 description'; // Set the required capabilities by the external function $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. $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); // Checks against DB values $this->assertEquals(2, count($groups)); foreach ($groups as $group) { $dbgroup = $DB->get_record('groups', array('id' => $group['id']), '*', MUST_EXIST); switch ($dbgroup->name) { case $group1['name']: $groupdescription = $group1['description']; $groupcourseid = $group1['courseid']; $this->assertEquals($dbgroup->descriptionformat, $group1['descriptionformat']); $this->assertEquals($dbgroup->enrolmentkey, $group1['enrolmentkey']); $this->assertEquals($dbgroup->idnumber, $group1['idnumber']); break; case $group2['name']: $groupdescription = $group2['description']; $groupcourseid = $group2['courseid']; break; default: throw new \moodle_exception('unknowgroupname'); break; } $this->assertEquals($dbgroup->description, $groupdescription); $this->assertEquals($dbgroup->courseid, $groupcourseid); } try { $froups = core_group_external::create_groups(array($group3)); $this->fail('Exception expected due to already existing idnumber.'); } catch (\moodle_exception $e) { $this->assertInstanceOf('moodle_exception', $e); $this->assertEquals(get_string('idnumbertaken', 'error'), $e->getMessage()); } // Call without required capability $this->unassignUserCapability('moodle/course:managegroups', $context->id, $roleid); $this->expectException(\required_capability_exception::class); $froups = core_group_external::create_groups(array($group4)); } /** * Test update_groups */ public function test_update_groups() { global $DB; $this->resetAfterTest(true); $course = self::getDataGenerator()->create_course(); $group1data = array(); $group1data['courseid'] = $course->id; $group1data['name'] = 'Group Test 1'; $group1data['description'] = 'Group Test 1 description'; $group1data['descriptionformat'] = FORMAT_MOODLE; $group1data['enrolmentkey'] = 'Test group enrol secret phrase'; $group1data['idnumber'] = 'TEST1'; $group2data = array(); $group2data['courseid'] = $course->id; $group2data['name'] = 'Group Test 2'; $group2data['description'] = 'Group Test 2 description'; $group2data['idnumber'] = 'TEST2'; // Set the required capabilities by the external function. $context = \context_course::instance($course->id); $roleid = $this->assignUserCapability('moodle/course:managegroups', $context->id); $this->assignUserCapability('moodle/course:view', $context->id, $roleid); // Create the test groups. $group1 = self::getDataGenerator()->create_group($group1data); $group2 = self::getDataGenerator()->create_group($group2data); $group1data['id'] = $group1->id; unset($group1data['courseid']); $group2data['id'] = $group2->id; unset($group2data['courseid']); // No exceptions should be triggered. $group1data['idnumber'] = 'CHANGED'; core_group_external::update_groups(array($group1data)); $group2data['description'] = 'Group Test 2 description CHANGED'; core_group_external::update_groups(array($group2data)); foreach ([$group1, $group2] as $group) { $dbgroup = $DB->get_record('groups', array('id' => $group->id), '*', MUST_EXIST); switch ($dbgroup->name) { case $group1data['name']: $this->assertEquals($dbgroup->idnumber, $group1data['idnumber']); $groupdescription = $group1data['description']; break; case $group2data['name']: $this->assertEquals($dbgroup->idnumber, $group2data['idnumber']); $groupdescription = $group2data['description']; break; default: throw new \moodle_exception('unknowngroupname'); break; } $this->assertEquals($dbgroup->description, $groupdescription); } // Taken idnumber exception. $group1data['idnumber'] = 'TEST2'; try { $groups = core_group_external::update_groups(array($group1data)); $this->fail('Exception expected due to already existing idnumber.'); } catch (\moodle_exception $e) { $this->assertInstanceOf('moodle_exception', $e); $this->assertEquals(get_string('idnumbertaken', 'error'), $e->getMessage()); } // Call without required capability. $group1data['idnumber'] = 'TEST1'; $this->unassignUserCapability('moodle/course:managegroups', $context->id, $roleid); $this->expectException(\required_capability_exception::class); $groups = core_group_external::update_groups(array($group1data)); } /** * Test get_groups */ public function test_get_groups() { global $DB; $this->resetAfterTest(true); $course = self::getDataGenerator()->create_course(); $group1data = array(); $group1data['courseid'] = $course->id; $group1data['name'] = 'Group Test 1'; $group1data['description'] = 'Group Test 1 description'; $group1data['descriptionformat'] = FORMAT_MOODLE; $group1data['enrolmentkey'] = 'Test group enrol secret phrase'; $group1data['idnumber'] = 'TEST1'; $group2data = array(); $group2data['courseid'] = $course->id; $group2data['name'] = 'Group Test 2'; $group2data['description'] = 'Group Test 2 description'; $group1 = self::getDataGenerator()->create_group($group1data); $group2 = self::getDataGenerator()->create_group($group2data); // Set the required capabilities by the external function $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. $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); // Checks against DB values $this->assertEquals(2, count($groups)); foreach ($groups as $group) { $dbgroup = $DB->get_record('groups', array('id' => $group['id']), '*', MUST_EXIST); switch ($dbgroup->name) { case $group1->name: $groupdescription = $group1->description; $groupcourseid = $group1->courseid; $this->assertEquals($dbgroup->descriptionformat, $group1->descriptionformat); $this->assertEquals($dbgroup->enrolmentkey, $group1->enrolmentkey); $this->assertEquals($dbgroup->idnumber, $group1->idnumber); break; case $group2->name: $groupdescription = $group2->description; $groupcourseid = $group2->courseid; break; default: throw new \moodle_exception('unknowgroupname'); break; } $this->assertEquals($dbgroup->description, $groupdescription); $this->assertEquals($dbgroup->courseid, $groupcourseid); } // Call without required capability $this->unassignUserCapability('moodle/course:managegroups', $context->id, $roleid); $this->expectException(\required_capability_exception::class); $groups = core_group_external::get_groups(array($group1->id, $group2->id)); } /** * Test delete_groups */ public function test_delete_groups() { global $DB; $this->resetAfterTest(true); $course = self::getDataGenerator()->create_course(); $group1data = array(); $group1data['courseid'] = $course->id; $group1data['name'] = 'Group Test 1'; $group1data['description'] = 'Group Test 1 description'; $group1data['descriptionformat'] = FORMAT_MOODLE; $group1data['enrolmentkey'] = 'Test group enrol secret phrase'; $group2data = array(); $group2data['courseid'] = $course->id; $group2data['name'] = 'Group Test 2'; $group2data['description'] = 'Group Test 2 description'; $group3data['courseid'] = $course->id; $group3data['name'] = 'Group Test 3'; $group3data['description'] = 'Group Test 3 description'; $group1 = self::getDataGenerator()->create_group($group1data); $group2 = self::getDataGenerator()->create_group($group2data); $group3 = self::getDataGenerator()->create_group($group3data); // Set the required capabilities by the external function $context = \context_course::instance($course->id); $roleid = $this->assignUserCapability('moodle/course:managegroups', $context->id); $this->assignUserCapability('moodle/course:view', $context->id, $roleid); // Checks against DB values $groupstotal = $DB->count_records('groups', array()); $this->assertEquals(3, $groupstotal); // Call the external function. core_group_external::delete_groups(array($group1->id, $group2->id)); // Checks against DB values $groupstotal = $DB->count_records('groups', array()); $this->assertEquals(1, $groupstotal); // Call without required capability $this->unassignUserCapability('moodle/course:managegroups', $context->id, $roleid); $this->expectException(\required_capability_exception::class); $froups = core_group_external::delete_groups(array($group3->id)); } /** * Test create and update groupings. * @return void */ public function test_create_update_groupings() { global $DB; $this->resetAfterTest(true); $this->setAdminUser(); $course = self::getDataGenerator()->create_course(); $grouping1data = array(); $grouping1data['courseid'] = $course->id; $grouping1data['name'] = 'Grouping 1 Test'; $grouping1data['description'] = 'Grouping 1 Test description'; $grouping1data['descriptionformat'] = FORMAT_MOODLE; $grouping1data['idnumber'] = 'TEST'; $grouping1 = self::getDataGenerator()->create_grouping($grouping1data); $grouping1data['name'] = 'Another group'; try { $groupings = core_group_external::create_groupings(array($grouping1data)); $this->fail('Exception expected due to already existing idnumber.'); } catch (\moodle_exception $e) { $this->assertInstanceOf('moodle_exception', $e); $this->assertEquals(get_string('idnumbertaken', 'error'), $e->getMessage()); } // No exception should be triggered. $grouping1data['id'] = $grouping1->id; $grouping1data['idnumber'] = 'CHANGED'; unset($grouping1data['courseid']); core_group_external::update_groupings(array($grouping1data)); $grouping2data = array(); $grouping2data['courseid'] = $course->id; $grouping2data['name'] = 'Grouping 2 Test'; $grouping2data['description'] = 'Grouping 2 Test description'; $grouping2data['descriptionformat'] = FORMAT_MOODLE; $grouping2data['idnumber'] = 'TEST'; $grouping2 = self::getDataGenerator()->create_grouping($grouping2data); $grouping2data['id'] = $grouping2->id; $grouping2data['idnumber'] = 'CHANGED'; unset($grouping2data['courseid']); try { $groupings = core_group_external::update_groupings(array($grouping2data)); $this->fail('Exception expected due to already existing idnumber.'); } catch (\moodle_exception $e) { $this->assertInstanceOf('moodle_exception', $e); $this->assertEquals(get_string('idnumbertaken', 'error'), $e->getMessage()); } } /** * Test get_groupings */ public function test_get_groupings() { global $DB; $this->resetAfterTest(true); $course = self::getDataGenerator()->create_course(); $groupingdata = array(); $groupingdata['courseid'] = $course->id; $groupingdata['name'] = 'Grouping Test'; $groupingdata['description'] = 'Grouping Test description'; $groupingdata['descriptionformat'] = FORMAT_MOODLE; $grouping = self::getDataGenerator()->create_grouping($groupingdata); // Set the required capabilities by the external function. $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); $this->assertEquals(1, count($groupings)); $group1data = array(); $group1data['courseid'] = $course->id; $group1data['name'] = 'Group Test 1'; $group1data['description'] = 'Group Test 1 description'; $group1data['descriptionformat'] = FORMAT_MOODLE; $group2data = array(); $group2data['courseid'] = $course->id; $group2data['name'] = 'Group Test 2'; $group2data['description'] = 'Group Test 2 description'; $group2data['descriptionformat'] = FORMAT_MOODLE; $group1 = self::getDataGenerator()->create_group($group1data); $group2 = self::getDataGenerator()->create_group($group2data); groups_assign_grouping($grouping->id, $group1->id); groups_assign_grouping($grouping->id, $group2->id); // 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); $this->assertEquals(1, count($groupings)); $this->assertEquals(2, count($groupings[0]['groups'])); foreach ($groupings[0]['groups'] as $group) { $dbgroup = $DB->get_record('groups', array('id' => $group['id']), '*', MUST_EXIST); $dbgroupinggroups = $DB->get_record('groupings_groups', array('groupingid' => $groupings[0]['id'], 'groupid' => $group['id']), '*', MUST_EXIST); switch ($dbgroup->name) { case $group1->name: $groupdescription = $group1->description; $groupcourseid = $group1->courseid; break; case $group2->name: $groupdescription = $group2->description; $groupcourseid = $group2->courseid; break; default: throw new \moodle_exception('unknowgroupname'); break; } $this->assertEquals($dbgroup->description, $groupdescription); $this->assertEquals($dbgroup->courseid, $groupcourseid); } } /** * Test delete_groupings. */ public function test_delete_groupings() { global $DB; $this->resetAfterTest(true); $course = self::getDataGenerator()->create_course(); $groupingdata1 = array(); $groupingdata1['courseid'] = $course->id; $groupingdata1['name'] = 'Grouping Test'; $groupingdata1['description'] = 'Grouping Test description'; $groupingdata1['descriptionformat'] = FORMAT_MOODLE; $groupingdata2 = array(); $groupingdata2['courseid'] = $course->id; $groupingdata2['name'] = 'Grouping Test'; $groupingdata2['description'] = 'Grouping Test description'; $groupingdata2['descriptionformat'] = FORMAT_MOODLE; $groupingdata3 = array(); $groupingdata3['courseid'] = $course->id; $groupingdata3['name'] = 'Grouping Test'; $groupingdata3['description'] = 'Grouping Test description'; $groupingdata3['descriptionformat'] = FORMAT_MOODLE; $grouping1 = self::getDataGenerator()->create_grouping($groupingdata1); $grouping2 = self::getDataGenerator()->create_grouping($groupingdata2); $grouping3 = self::getDataGenerator()->create_grouping($groupingdata3); // Set the required capabilities by the external function. $context = \context_course::instance($course->id); $roleid = $this->assignUserCapability('moodle/course:managegroups', $context->id); $this->assignUserCapability('moodle/course:view', $context->id, $roleid); // Checks against DB values. $groupingstotal = $DB->count_records('groupings', array()); $this->assertEquals(3, $groupingstotal); // Call the external function. core_group_external::delete_groupings(array($grouping1->id, $grouping2->id)); // Checks against DB values. $groupingstotal = $DB->count_records('groupings', array()); $this->assertEquals(1, $groupingstotal); // Call without required capability. $this->unassignUserCapability('moodle/course:managegroups', $context->id, $roleid); $this->expectException(\required_capability_exception::class); core_group_external::delete_groupings(array($grouping3->id)); } /** * Test get_groups */ public function test_get_course_user_groups() { global $DB; $this->resetAfterTest(true); $student1 = self::getDataGenerator()->create_user(); $student2 = self::getDataGenerator()->create_user(); $teacher = self::getDataGenerator()->create_user(); $course = self::getDataGenerator()->create_course(); $anothercourse = self::getDataGenerator()->create_course(); $emptycourse = self::getDataGenerator()->create_course(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $this->getDataGenerator()->enrol_user($student1->id, $course->id, $studentrole->id); $this->getDataGenerator()->enrol_user($student1->id, $anothercourse->id, $studentrole->id); $this->getDataGenerator()->enrol_user($student2->id, $course->id, $studentrole->id); $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher')); $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id); $this->getDataGenerator()->enrol_user($teacher->id, $emptycourse->id, $teacherrole->id); $group1data = array(); $group1data['courseid'] = $course->id; $group1data['name'] = 'Group Test 1'; $group1data['description'] = 'Group Test 1 description'; $group1data['idnumber'] = 'TEST1'; $group2data = array(); $group2data['courseid'] = $course->id; $group2data['name'] = 'Group Test 2'; $group2data['description'] = 'Group Test 2 description'; $group3data = array(); $group3data['courseid'] = $anothercourse->id; $group3data['name'] = 'Group Test 3'; $group3data['description'] = 'Group Test 3 description'; $group3data['idnumber'] = 'TEST3'; $group1 = self::getDataGenerator()->create_group($group1data); $group2 = self::getDataGenerator()->create_group($group2data); $group3 = self::getDataGenerator()->create_group($group3data); groups_add_member($group1->id, $student1->id); groups_add_member($group1->id, $student2->id); groups_add_member($group2->id, $student1->id); groups_add_member($group3->id, $student1->id); // Create a grouping. $groupingdata = array(); $groupingdata['courseid'] = $course->id; $groupingdata['name'] = 'Grouping Test'; $groupingdata['description'] = 'Grouping Test description'; $groupingdata['descriptionformat'] = FORMAT_MOODLE; $grouping = self::getDataGenerator()->create_grouping($groupingdata); // Grouping only containing group1. groups_assign_grouping($grouping->id, $group1->id); $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); // Check that I see my groups. $this->assertCount(2, $groups['groups']); $this->assertEquals($course->id, $groups['groups'][0]['courseid']); $this->assertEquals($course->id, $groups['groups'][1]['courseid']); // 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); // Check that I see my groups in the grouping. $this->assertCount(1, $groups['groups']); $this->assertEquals($group1->id, $groups['groups'][0]['id']); // 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); // 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); // Check that I see my groups. $this->assertCount(1, $groups['groups']); $this->assertEquals($group1data['name'], $groups['groups'][0]['name']); $this->assertEquals($group1data['description'], $groups['groups'][0]['description']); $this->assertEquals($group1data['idnumber'], $groups['groups'][0]['idnumber']); $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); // 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); // 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); // 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. $this->assertEquals('1', $groups['warnings'][0]['warningcode']); // 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); // 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']); // Check permissions. $this->setUser($student1); // 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); $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); $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); $this->assertCount(1, $groups['warnings']); $this->assertEquals('notenrolled', $groups['warnings'][0]['warningcode']); } /** * Test get_activity_allowed_groups */ public function test_get_activity_allowed_groups() { global $DB; $this->resetAfterTest(true); $generator = self::getDataGenerator(); $student = $generator->create_user(); $otherstudent = $generator->create_user(); $teacher = $generator->create_user(); $course = $generator->create_course(); $othercourse = $generator->create_course(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher')); $generator->enrol_user($student->id, $course->id, $studentrole->id); $generator->enrol_user($otherstudent->id, $othercourse->id, $studentrole->id); $generator->enrol_user($teacher->id, $course->id, $teacherrole->id); $forum1 = $generator->create_module("forum", array('course' => $course->id), array('groupmode' => VISIBLEGROUPS)); $forum2 = $generator->create_module("forum", array('course' => $othercourse->id)); $forum3 = $generator->create_module("forum", array('course' => $course->id), array('visible' => 0)); // Request data for tests. $cm1 = get_coursemodule_from_instance("forum", $forum1->id); $cm2 = get_coursemodule_from_instance("forum", $forum2->id); $cm3 = get_coursemodule_from_instance("forum", $forum3->id); $group1data = array(); $group1data['courseid'] = $course->id; $group1data['name'] = 'Group Test 1'; $group1data['description'] = 'Group Test 1 description'; $group1data['idnumber'] = 'TEST1'; $group2data = array(); $group2data['courseid'] = $course->id; $group2data['name'] = 'Group Test 2'; $group2data['description'] = 'Group Test 2 description'; $group2data['idnumber'] = 'TEST2'; $group1 = $generator->create_group($group1data); $group2 = $generator->create_group($group2data); groups_add_member($group1->id, $student->id); groups_add_member($group2->id, $student->id); $this->setUser($student); // First try possible errors. try { $data = core_group_external::get_activity_allowed_groups($cm2->id); } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } try { $data = core_group_external::get_activity_allowed_groups($cm3->id); } 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); $this->assertCount(2, $groups['groups']); $this->assertFalse($groups['canaccessallgroups']); foreach ($groups['groups'] as $group) { if ($group['name'] == $group1data['name']) { $this->assertEquals($group1data['description'], $group['description']); $this->assertEquals($group1data['idnumber'], $group['idnumber']); } else { $this->assertEquals($group2data['description'], $group['description']); $this->assertEquals($group2data['idnumber'], $group['idnumber']); } } $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); $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); $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); $this->assertCount(2, $groups['groups']); // Teachers by default can access all groups. $this->assertTrue($groups['canaccessallgroups']); } /** * Test get_activity_groupmode */ public function test_get_activity_groupmode() { global $DB; $this->resetAfterTest(true); $generator = self::getDataGenerator(); $student = $generator->create_user(); $course = $generator->create_course(); $othercourse = $generator->create_course(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $generator->enrol_user($student->id, $course->id, $studentrole->id); $forum1 = $generator->create_module("forum", array('course' => $course->id), array('groupmode' => VISIBLEGROUPS)); $forum2 = $generator->create_module("forum", array('course' => $othercourse->id)); $forum3 = $generator->create_module("forum", array('course' => $course->id), array('visible' => 0)); // Request data for tests. $cm1 = get_coursemodule_from_instance("forum", $forum1->id); $cm2 = get_coursemodule_from_instance("forum", $forum2->id); $cm3 = get_coursemodule_from_instance("forum", $forum3->id); $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); $this->assertEquals(VISIBLEGROUPS, $data['groupmode']); try { $data = core_group_external::get_activity_groupmode($cm2->id); } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } try { $data = core_group_external::get_activity_groupmode($cm3->id); } catch (\moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } } /** * Test add_group_members. */ public function test_add_group_members() { global $DB; $this->resetAfterTest(true); $student1 = self::getDataGenerator()->create_user(); $student2 = self::getDataGenerator()->create_user(); $student3 = self::getDataGenerator()->create_user(); $course = self::getDataGenerator()->create_course(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $this->getDataGenerator()->enrol_user($student1->id, $course->id, $studentrole->id); $this->getDataGenerator()->enrol_user($student2->id, $course->id, $studentrole->id); $this->getDataGenerator()->enrol_user($student3->id, $course->id, $studentrole->id); $group1data = array(); $group1data['courseid'] = $course->id; $group1data['name'] = 'Group Test 1'; $group1data['description'] = 'Group Test 1 description'; $group1data['idnumber'] = 'TEST1'; $group1 = self::getDataGenerator()->create_group($group1data); // Checks against DB values. $memberstotal = $DB->count_records('groups_members', ['groupid' => $group1->id]); $this->assertEquals(0, $memberstotal); // Set the required capabilities by the external function. $context = \context_course::instance($course->id); $roleid = $this->assignUserCapability('moodle/course:managegroups', $context->id); $this->assignUserCapability('moodle/course:view', $context->id, $roleid); core_group_external::add_group_members([ 'members' => [ 'groupid' => $group1->id, 'userid' => $student1->id, ] ]); core_group_external::add_group_members([ 'members' => [ 'groupid' => $group1->id, 'userid' => $student2->id, ] ]); core_group_external::add_group_members([ 'members' => [ 'groupid' => $group1->id, 'userid' => $student3->id, ] ]); // Checks against DB values. $memberstotal = $DB->count_records('groups_members', ['groupid' => $group1->id]); $this->assertEquals(3, $memberstotal); } /** * Test delete_group_members. */ public function test_delete_group_members() { global $DB; $this->resetAfterTest(true); $student1 = self::getDataGenerator()->create_user(); $student2 = self::getDataGenerator()->create_user(); $student3 = self::getDataGenerator()->create_user(); $course = self::getDataGenerator()->create_course(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $this->getDataGenerator()->enrol_user($student1->id, $course->id, $studentrole->id); $this->getDataGenerator()->enrol_user($student2->id, $course->id, $studentrole->id); $this->getDataGenerator()->enrol_user($student3->id, $course->id, $studentrole->id); $group1data = array(); $group1data['courseid'] = $course->id; $group1data['name'] = 'Group Test 1'; $group1data['description'] = 'Group Test 1 description'; $group1data['idnumber'] = 'TEST1'; $group1 = self::getDataGenerator()->create_group($group1data); groups_add_member($group1->id, $student1->id); groups_add_member($group1->id, $student2->id); groups_add_member($group1->id, $student3->id); // Checks against DB values. $memberstotal = $DB->count_records('groups_members', ['groupid' => $group1->id]); $this->assertEquals(3, $memberstotal); // Set the required capabilities by the external function. $context = \context_course::instance($course->id); $roleid = $this->assignUserCapability('moodle/course:managegroups', $context->id); $this->assignUserCapability('moodle/course:view', $context->id, $roleid); core_group_external::delete_group_members([ 'members' => [ 'groupid' => $group1->id, 'userid' => $student2->id, ] ]); // Checks against DB values. $memberstotal = $DB->count_records('groups_members', ['groupid' => $group1->id]); $this->assertEquals(2, $memberstotal); } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | ���֧ߧ֧�ѧ�ڧ� ����ѧߧڧ��: 0 |
proxy
|
phpinfo
|
���ѧ����ۧܧ�