/** * Test validate_attempt */ public function test_validate_attempt() { global $DB; // Create a new quiz with one attempt started. list($quiz, $context, $quizobj, $attempt, $attemptobj) = $this->create_quiz_with_questions(true); $this->setUser($this->student); // Invalid attempt. try { $params = array('attemptid' => -1, 'page' => 0); testable_mod_quiz_external::validate_attempt($params); $this->fail('Exception expected due to invalid attempt id.'); } catch (dml_missing_record_exception $e) { $this->assertEquals('invalidrecord', $e->errorcode); } // Test OK case. $params = array('attemptid' => $attempt->id, 'page' => 0); $result = testable_mod_quiz_external::validate_attempt($params); $this->assertEquals($attempt->id, $result[0]->get_attempt()->id); $this->assertEquals([], $result[1]); // Test with preflight data. $quiz->password = '******'; $DB->update_record('quiz', $quiz); try { $params = array('attemptid' => $attempt->id, 'page' => 0, 'preflightdata' => array(array("name" => "quizpassword", "value" => 'bad'))); testable_mod_quiz_external::validate_attempt($params); $this->fail('Exception expected due to invalid passwod.'); } catch (moodle_exception $e) { $this->assertEquals(get_string('passworderror', 'quizaccess_password'), $e->errorcode); } // Now, try everything correct. $params['preflightdata'][0]['value'] = 'abc'; $result = testable_mod_quiz_external::validate_attempt($params); $this->assertEquals($attempt->id, $result[0]->get_attempt()->id); $this->assertEquals([], $result[1]); // Page out of range. $DB->update_record('quiz', $quiz); $params['page'] = 4; try { testable_mod_quiz_external::validate_attempt($params); $this->fail('Exception expected due to page out of range.'); } catch (moodle_quiz_exception $e) { $this->assertEquals('Invalid page number', $e->errorcode); } $params['page'] = 0; // Try to open attempt in closed quiz. $quiz->timeopen = time() - WEEKSECS; $quiz->timeclose = time() - DAYSECS; $DB->update_record('quiz', $quiz); // This should work, ommit access rules. testable_mod_quiz_external::validate_attempt($params, false); // Get a generic error because prior to checking the dates the attempt is closed. try { testable_mod_quiz_external::validate_attempt($params); $this->fail('Exception expected due to passed dates.'); } catch (moodle_quiz_exception $e) { $this->assertEquals('attempterror', $e->errorcode); } // Finish the attempt. $attemptobj = quiz_attempt::create($attempt->id); $attemptobj->process_finish(time(), false); try { testable_mod_quiz_external::validate_attempt($params, false); $this->fail('Exception expected due to attempt finished.'); } catch (moodle_quiz_exception $e) { $this->assertEquals('attemptalreadyclosed', $e->errorcode); } // 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/quiz:attempt', CAP_PROHIBIT, $this->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 { testable_mod_quiz_external::validate_attempt($params); $this->fail('Exception expected due to missing permissions.'); } catch (required_capability_exception $e) { $this->assertEquals('nopermissions', $e->errorcode); } // Now try with a different user. $this->setUser($this->teacher); $params['page'] = 0; try { testable_mod_quiz_external::validate_attempt($params); $this->fail('Exception expected due to not your attempt.'); } catch (moodle_quiz_exception $e) { $this->assertEquals('notyourattempt', $e->errorcode); } }
public function test_matching_cacherev() { global $DB, $CFG; $this->resetAfterTest(); $this->setAdminUser(); $cache = cache::make('core', 'coursemodinfo'); // Generate the course and pre-requisite module. $course = $this->getDataGenerator()->create_course(array('format' => 'topics', 'numsections' => 3), array('createsections' => true)); // Make sure the cacherev is set. $cacherev = $DB->get_field('course', 'cacherev', array('id' => $course->id)); $this->assertGreaterThan(0, $cacherev); $prevcacherev = $cacherev; // Reset course cache and make sure cacherev is bumped up but cache is empty. rebuild_course_cache($course->id, true); $cacherev = $DB->get_field('course', 'cacherev', array('id' => $course->id)); $this->assertGreaterThan($prevcacherev, $cacherev); $this->assertEmpty($cache->get($course->id)); $prevcacherev = $cacherev; // Build course cache. Cacherev should not change but cache is now not empty. Make sure cacherev is the same everywhere. $modinfo = get_fast_modinfo($course->id); $cacherev = $DB->get_field('course', 'cacherev', array('id' => $course->id)); $this->assertEquals($prevcacherev, $cacherev); $cachedvalue = $cache->get($course->id); $this->assertNotEmpty($cachedvalue); $this->assertEquals($cacherev, $cachedvalue->cacherev); $this->assertEquals($cacherev, $modinfo->get_course()->cacherev); $prevcacherev = $cacherev; // Little trick to check that cache is not rebuilt druing the next step - substitute the value in MUC and later check that it is still there. $cache->set($course->id, (object) array_merge((array) $cachedvalue, array('secretfield' => 1))); // Clear static cache and call get_fast_modinfo() again (pretend we are in another request). Cache should not be rebuilt. course_modinfo::clear_instance_cache(); $modinfo = get_fast_modinfo($course->id); $cacherev = $DB->get_field('course', 'cacherev', array('id' => $course->id)); $this->assertEquals($prevcacherev, $cacherev); $cachedvalue = $cache->get($course->id); $this->assertNotEmpty($cachedvalue); $this->assertEquals($cacherev, $cachedvalue->cacherev); $this->assertNotEmpty($cachedvalue->secretfield); $this->assertEquals($cacherev, $modinfo->get_course()->cacherev); $prevcacherev = $cacherev; // Rebuild course cache. Cacherev must be incremented everywhere. rebuild_course_cache($course->id); $cacherev = $DB->get_field('course', 'cacherev', array('id' => $course->id)); $this->assertGreaterThan($prevcacherev, $cacherev); $cachedvalue = $cache->get($course->id); $this->assertNotEmpty($cachedvalue); $this->assertEquals($cacherev, $cachedvalue->cacherev); $modinfo = get_fast_modinfo($course->id); $this->assertEquals($cacherev, $modinfo->get_course()->cacherev); $prevcacherev = $cacherev; // Update cacherev in DB and make sure the cache will be rebuilt on the next call to get_fast_modinfo(). increment_revision_number('course', 'cacherev', 'id = ?', array($course->id)); // We need to clear static cache for course_modinfo instances too. course_modinfo::clear_instance_cache(); $modinfo = get_fast_modinfo($course->id); $cacherev = $DB->get_field('course', 'cacherev', array('id' => $course->id)); $this->assertGreaterThan($prevcacherev, $cacherev); $cachedvalue = $cache->get($course->id); $this->assertNotEmpty($cachedvalue); $this->assertEquals($cacherev, $cachedvalue->cacherev); $this->assertEquals($cacherev, $modinfo->get_course()->cacherev); $prevcacherev = $cacherev; // Reset cache for all courses and make sure this course cache is reset. rebuild_course_cache(0, true); $cacherev = $DB->get_field('course', 'cacherev', array('id' => $course->id)); $this->assertGreaterThan($prevcacherev, $cacherev); $this->assertEmpty($cache->get($course->id)); // Rebuild again. $modinfo = get_fast_modinfo($course->id); $cachedvalue = $cache->get($course->id); $this->assertNotEmpty($cachedvalue); $this->assertEquals($cacherev, $cachedvalue->cacherev); $this->assertEquals($cacherev, $modinfo->get_course()->cacherev); $prevcacherev = $cacherev; // Purge all caches and make sure cacherev is increased and data from MUC erased. purge_all_caches(); $cacherev = $DB->get_field('course', 'cacherev', array('id' => $course->id)); $this->assertGreaterThan($prevcacherev, $cacherev); $this->assertEmpty($cache->get($course->id)); }
/** * Test get_calendar_events */ public function test_get_calendar_events() { global $DB, $USER; $this->resetAfterTest(true); $this->setAdminUser(); // Create a few stuff to test with. $user = $this->getDataGenerator()->create_user(); $course = $this->getDataGenerator()->create_course(); $record = new stdClass(); $record->courseid = $course->id; $group = $this->getDataGenerator()->create_group($record); $beforecount = $DB->count_records('event'); // Let's create a few events. $siteevent = $this->create_calendar_event('site', $USER->id, 'site'); $record = new stdClass(); $record->courseid = $course->id; $courseevent = $this->create_calendar_event('course', $USER->id, 'course', 2, time(), $record); $userevent = $this->create_calendar_event('user', $USER->id); $record = new stdClass(); $record->courseid = $course->id; $record->groupid = $group->id; $groupevent = $this->create_calendar_event('group', $USER->id, 'group', 0, time(), $record); $paramevents = array('eventids' => array($siteevent->id), 'courseids' => array($course->id), 'groupids' => array($group->id)); $options = array('siteevents' => true, 'userevents' => true); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); // Check to see if we got all events. $this->assertEquals(5, count($events['events'])); $this->assertEquals(0, count($events['warnings'])); $options = array('siteevents' => true, 'userevents' => true, 'timeend' => time() + 7 * WEEKSECS); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(5, count($events['events'])); $this->assertEquals(0, count($events['warnings'])); // Let's play around with caps. $this->setUser($user); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(2, count($events['events'])); // site, user. $this->assertEquals(2, count($events['warnings'])); // course, group. $role = $DB->get_record('role', array('shortname' => 'student')); $this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(4, count($events['events'])); // site, user, both course events. $this->assertEquals(1, count($events['warnings'])); // group. $options = array('siteevents' => true, 'userevents' => true, 'timeend' => time() + HOURSECS); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(3, count($events['events'])); // site, user, one course event. $this->assertEquals(1, count($events['warnings'])); // group. groups_add_member($group, $user); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(4, count($events['events'])); // site, user, group, one course event. $this->assertEquals(0, count($events['warnings'])); $paramevents = array('courseids' => array($course->id), 'groupids' => array($group->id)); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(4, count($events['events'])); // site, user, group, one course event. $this->assertEquals(0, count($events['warnings'])); $paramevents = array('groupids' => array($group->id, 23)); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(3, count($events['events'])); // site, user, group. $this->assertEquals(1, count($events['warnings'])); $paramevents = array('courseids' => array(23)); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(2, count($events['events'])); // site, user. $this->assertEquals(1, count($events['warnings'])); $paramevents = array(); $options = array('siteevents' => false, 'userevents' => false, 'timeend' => time() + 7 * WEEKSECS); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(0, count($events['events'])); // nothing returned. $this->assertEquals(0, count($events['warnings'])); $paramevents = array('eventids' => array($siteevent->id, $groupevent->id)); $options = array('siteevents' => false, 'userevents' => false, 'timeend' => time() + 7 * WEEKSECS); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(2, count($events['events'])); // site, group. $this->assertEquals(0, count($events['warnings'])); $paramevents = array('eventids' => array($siteevent->id)); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(1, count($events['events'])); // site. $this->assertEquals(0, count($events['warnings'])); // Try getting a course event by its id. $paramevents = array('eventids' => array($courseevent->id)); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(1, count($events['events'])); $this->assertEquals(0, count($events['warnings'])); // Now, create an activity event. $this->setAdminUser(); $nexttime = time() + DAYSECS; $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id, 'duedate' => $nexttime)); $this->setUser($user); $paramevents = array('courseids' => array($course->id)); $options = array('siteevents' => true, 'userevents' => true, 'timeend' => time() + WEEKSECS); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertCount(5, $events['events']); // Hide the assignment. set_coursemodule_visible($assign->cmid, 0); // Empty all the caches that may be affected by this change. accesslib_clear_all_caches_for_unit_testing(); course_modinfo::clear_instance_cache(); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); // Expect one less. $this->assertCount(4, $events['events']); }
/** * Rebuilds or resets the cached list of course activities stored in MUC. * * rebuild_course_cache() must NEVER be called from lib/db/upgrade.php. * At the same time course cache may ONLY be cleared using this function in * upgrade scripts of plugins. * * During the bulk operations if it is necessary to reset cache of multiple * courses it is enough to call {@link increment_revision_number()} for the * table 'course' and field 'cacherev' specifying affected courses in select. * * Cached course information is stored in MUC core/coursemodinfo and is * validated with the DB field {course}.cacherev * * @global moodle_database $DB * @param int $courseid id of course to rebuild, empty means all * @param boolean $clearonly only clear the cache, gets rebuild automatically on the fly. * Recommended to set to true to avoid unnecessary multiple rebuilding. */ function rebuild_course_cache($courseid=0, $clearonly=false) { global $COURSE, $SITE, $DB, $CFG; // Function rebuild_course_cache() can not be called during upgrade unless it's clear only. if (!$clearonly && !upgrade_ensure_not_running(true)) { $clearonly = true; } // Destroy navigation caches navigation_cache::destroy_volatile_caches(); if (class_exists('format_base')) { // if file containing class is not loaded, there is no cache there anyway format_base::reset_course_cache($courseid); } $cachecoursemodinfo = cache::make('core', 'coursemodinfo'); if (empty($courseid)) { // Clearing caches for all courses. increment_revision_number('course', 'cacherev', ''); $cachecoursemodinfo->purge(); course_modinfo::clear_instance_cache(); // Update global values too. $sitecacherev = $DB->get_field('course', 'cacherev', array('id' => SITEID)); $SITE->cachrev = $sitecacherev; if ($COURSE->id == SITEID) { $COURSE->cacherev = $sitecacherev; } else { $COURSE->cacherev = $DB->get_field('course', 'cacherev', array('id' => $COURSE->id)); } } else { // Clearing cache for one course, make sure it is deleted from user request cache as well. increment_revision_number('course', 'cacherev', 'id = :id', array('id' => $courseid)); $cachecoursemodinfo->delete($courseid); course_modinfo::clear_instance_cache($courseid); // Update global values too. if ($courseid == $COURSE->id || $courseid == $SITE->id) { $cacherev = $DB->get_field('course', 'cacherev', array('id' => $courseid)); if ($courseid == $COURSE->id) { $COURSE->cacherev = $cacherev; } if ($courseid == $SITE->id) { $SITE->cachrev = $cacherev; } } } if ($clearonly) { return; } if ($courseid) { $select = array('id'=>$courseid); } else { $select = array(); core_php_time_limit::raise(); // this could take a while! MDL-10954 } $rs = $DB->get_recordset("course", $select,'','id,'.join(',', course_modinfo::$cachedfields)); // Rebuild cache for each course. foreach ($rs as $course) { course_modinfo::build_course_cache($course); } $rs->close(); }
/** * Test convect fetching. */ public function test_concept_fetching() { global $CFG, $DB; $this->resetAfterTest(true); $this->setAdminUser(); $CFG->glossary_linkbydefault = 1; $CFG->glossary_linkentries = 0; // Create a test courses. $course1 = $this->getDataGenerator()->create_course(); $course2 = $this->getDataGenerator()->create_course(); $site = $DB->get_record('course', array('id' => SITEID)); // Create a glossary. $glossary1a = $this->getDataGenerator()->create_module('glossary', array('course' => $course1->id, 'mainglossary' => 1, 'usedynalink' => 1)); $glossary1b = $this->getDataGenerator()->create_module('glossary', array('course' => $course1->id, 'mainglossary' => 1, 'usedynalink' => 1)); $glossary1c = $this->getDataGenerator()->create_module('glossary', array('course' => $course1->id, 'mainglossary' => 1, 'usedynalink' => 0)); $glossary2 = $this->getDataGenerator()->create_module('glossary', array('course' => $course2->id, 'mainglossary' => 1, 'usedynalink' => 1)); $glossary3 = $this->getDataGenerator()->create_module('glossary', array('course' => $site->id, 'mainglossary' => 1, 'usedynalink' => 1, 'globalglossary' => 1)); /** @var mod_glossary_generator $generator */ $generator = $this->getDataGenerator()->get_plugin_generator('mod_glossary'); $entry1a1 = $generator->create_content($glossary1a, array('concept' => 'first', 'usedynalink' => 1), array('prvni', 'erste')); $entry1a2 = $generator->create_content($glossary1a, array('concept' => 'A&B', 'usedynalink' => 1)); $entry1a3 = $generator->create_content($glossary1a, array('concept' => 'neee', 'usedynalink' => 0)); $entry1b1 = $generator->create_content($glossary1b, array('concept' => 'second', 'usedynalink' => 1)); $entry1c1 = $generator->create_content($glossary1c, array('concept' => 'third', 'usedynalink' => 1)); $entry31 = $generator->create_content($glossary3, array('concept' => 'global', 'usedynalink' => 1), array('globalni')); $cat1 = $generator->create_category($glossary1a, array('name' => 'special'), array($entry1a1, $entry1a2)); \mod_glossary\local\concept_cache::reset_caches(); $concepts1 = \mod_glossary\local\concept_cache::get_concepts($course1->id); $this->assertCount(3, $concepts1[0]); $this->arrayHasKey($concepts1[0], $glossary1a->id); $this->arrayHasKey($concepts1[0], $glossary1b->id); $this->arrayHasKey($concepts1[0], $glossary3->id); $this->assertCount(3, $concepts1[1]); $this->arrayHasKey($concepts1[1], $glossary1a->id); $this->arrayHasKey($concepts1[1], $glossary1b->id); $this->arrayHasKey($concepts1[0], $glossary3->id); $this->assertCount(5, $concepts1[1][$glossary1a->id]); foreach ($concepts1[1][$glossary1a->id] as $concept) { $this->assertSame(array('id', 'glossaryid', 'concept', 'casesensitive', 'category', 'fullmatch'), array_keys((array) $concept)); if ($concept->concept === 'first') { $this->assertEquals($entry1a1->id, $concept->id); $this->assertEquals($glossary1a->id, $concept->glossaryid); $this->assertEquals(0, $concept->category); } else { if ($concept->concept === 'prvni') { $this->assertEquals($entry1a1->id, $concept->id); $this->assertEquals($glossary1a->id, $concept->glossaryid); $this->assertEquals(0, $concept->category); } else { if ($concept->concept === 'erste') { $this->assertEquals($entry1a1->id, $concept->id); $this->assertEquals($glossary1a->id, $concept->glossaryid); $this->assertEquals(0, $concept->category); } else { if ($concept->concept === 'A&B') { $this->assertEquals($entry1a2->id, $concept->id); $this->assertEquals($glossary1a->id, $concept->glossaryid); $this->assertEquals(0, $concept->category); } else { if ($concept->concept === 'special') { $this->assertEquals($cat1->id, $concept->id); $this->assertEquals($glossary1a->id, $concept->glossaryid); $this->assertEquals(1, $concept->category); } else { $this->fail('Unexpected concept: ' . $concept->concept); } } } } } } $this->assertCount(1, $concepts1[1][$glossary1b->id]); foreach ($concepts1[1][$glossary1b->id] as $concept) { $this->assertSame(array('id', 'glossaryid', 'concept', 'casesensitive', 'category', 'fullmatch'), array_keys((array) $concept)); if ($concept->concept === 'second') { $this->assertEquals($entry1b1->id, $concept->id); $this->assertEquals($glossary1b->id, $concept->glossaryid); $this->assertEquals(0, $concept->category); } else { $this->fail('Unexpected concept: ' . $concept->concept); } } $this->assertCount(2, $concepts1[1][$glossary3->id]); foreach ($concepts1[1][$glossary3->id] as $concept) { $this->assertSame(array('id', 'glossaryid', 'concept', 'casesensitive', 'category', 'fullmatch'), array_keys((array) $concept)); if ($concept->concept === 'global') { $this->assertEquals($entry31->id, $concept->id); $this->assertEquals($glossary3->id, $concept->glossaryid); $this->assertEquals(0, $concept->category); } else { if ($concept->concept === 'globalni') { $this->assertEquals($entry31->id, $concept->id); $this->assertEquals($glossary3->id, $concept->glossaryid); $this->assertEquals(0, $concept->category); } else { $this->fail('Unexpected concept: ' . $concept->concept); } } } $concepts3 = \mod_glossary\local\concept_cache::get_concepts($site->id); $this->assertCount(1, $concepts3[0]); $this->arrayHasKey($concepts3[0], $glossary3->id); $this->assertCount(1, $concepts3[1]); $this->arrayHasKey($concepts3[0], $glossary3->id); foreach ($concepts3[1][$glossary3->id] as $concept) { $this->assertSame(array('id', 'glossaryid', 'concept', 'casesensitive', 'category', 'fullmatch'), array_keys((array) $concept)); if ($concept->concept === 'global') { $this->assertEquals($entry31->id, $concept->id); $this->assertEquals($glossary3->id, $concept->glossaryid); $this->assertEquals(0, $concept->category); } else { if ($concept->concept === 'globalni') { $this->assertEquals($entry31->id, $concept->id); $this->assertEquals($glossary3->id, $concept->glossaryid); $this->assertEquals(0, $concept->category); } else { $this->fail('Unexpected concept: ' . $concept->concept); } } } $concepts2 = \mod_glossary\local\concept_cache::get_concepts($course2->id); $this->assertEquals($concepts3, $concepts2); // Test uservisible flag. set_config('enableavailability', 1); $glossary1d = $this->getDataGenerator()->create_module('glossary', array('course' => $course1->id, 'mainglossary' => 1, 'usedynalink' => 1, 'availability' => json_encode(\core_availability\tree::get_root_json(array(\availability_group\condition::get_json()))))); $entry1d1 = $generator->create_content($glossary1d, array('concept' => 'membersonly', 'usedynalink' => 1)); $user = $this->getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($user->id, $course1->id); $this->getDataGenerator()->enrol_user($user->id, $course2->id); \mod_glossary\local\concept_cache::reset_caches(); $concepts1 = \mod_glossary\local\concept_cache::get_concepts($course1->id); $this->assertCount(4, $concepts1[0]); $this->assertCount(4, $concepts1[1]); $this->setUser($user); course_modinfo::clear_instance_cache(); \mod_glossary\local\concept_cache::reset_caches(); $concepts1 = \mod_glossary\local\concept_cache::get_concepts($course1->id); $this->assertCount(3, $concepts1[0]); $this->assertCount(3, $concepts1[1]); }
/** * Test test_view_submission_status */ public function test_view_submission_status() { global $DB; $this->resetAfterTest(true); $this->setAdminUser(); // Setup test data. $course = $this->getDataGenerator()->create_course(); $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id)); $context = context_module::instance($assign->cmid); $cm = get_coursemodule_from_instance('assign', $assign->id); // Test invalid instance id. try { mod_assign_external::view_submission_status(0); $this->fail('Exception expected due to invalid mod_assign instance id.'); } catch (moodle_exception $e) { $this->assertEquals('invalidrecord', $e->errorcode); } // Test not-enrolled user. $user = self::getDataGenerator()->create_user(); $this->setUser($user); try { mod_assign_external::view_submission_status($assign->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_assign_external::view_submission_status($assign->id); $result = external_api::clean_returnvalue(mod_assign_external::view_submission_status_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_assign\\event\\submission_status_viewed', $event); $this->assertEquals($context, $event->get_context()); $moodleurl = new \moodle_url('/mod/assign/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/assign:view', CAP_PROHIBIT, $studentrole->id, $context->id); accesslib_clear_all_caches_for_unit_testing(); course_modinfo::clear_instance_cache(); try { mod_assign_external::view_submission_status($assign->id); $this->fail('Exception expected due to missing capability.'); } catch (moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } }
$coursecontext = context_course::instance($course->id); if (!has_capability('moodle/course:manageactivities', $coursecontext)) { echo get_string('progress_dating_error_access', 'block_snap'); //check the start dates for the course } elseif (!$course->startdate) { echo html_writer::tag('p', get_string('progress_dating_error', 'block_snap'), array('class' => 'alert alert-danger')); //check to see if completion is enabled for the course } elseif (!$course->enablecompletion) { echo html_writer::tag('p', get_string('no_completion_tracking', 'block_snap'), array('class' => 'alert alert-danger')); //otherwise, display the dating tool } else { echo html_writer::tag('p', get_string('progress_dating_description', 'block_snap'), array('class' => 'alert alert-info')); //create a warning about having javascript disabled that we will remove with javascript echo '<noscript>' . html_writer::tag('p', get_string('progress_dating_nojs', 'block_snap'), array('class' => 'snap-nojs alert alert-danger')) . '</noscript>'; //pull the modules course_modinfo::clear_instance_cache($course); $modinfo = get_fast_modinfo($course); $modlist = array(); $callist = array(); //establish the start date of the course $start = new DateTime(); $start->setTimestamp($course->startdate); //show when the course starts echo '<h3 class="course-starts">' . get_string('starts_on', 'block_snap') . ' <span>' . $start->format('l, F j, Y') . '</span></h3>' . PHP_EOL; //iterate all the modules and create each item for dragging around foreach ($modinfo->cms as $mod) { //if completion is not enabled, then skip it if (!$mod->completion) { continue; } //create the html for the module icon
/** * Test get_calendar_events */ public function test_get_calendar_events() { global $DB, $USER; $this->resetAfterTest(true); $this->setAdminUser(); // Create a few stuff to test with. $user = $this->getDataGenerator()->create_user(); $course = $this->getDataGenerator()->create_course(); $record = new stdClass(); $record->courseid = $course->id; $group = $this->getDataGenerator()->create_group($record); $beforecount = $DB->count_records('event'); // Let's create a few events. $siteevent = $this->create_calendar_event('site', $USER->id, 'site'); // This event will have description with an inline fake image. $draftidfile = file_get_unused_draft_itemid(); $usercontext = context_course::instance($course->id); $filerecord = array('contextid' => $usercontext->id, 'component' => 'user', 'filearea' => 'draft', 'itemid' => $draftidfile, 'filepath' => '/', 'filename' => 'fakeimage.png'); $fs = get_file_storage(); $fs->create_file_from_string($filerecord, 'img contents'); $record = new stdClass(); $record->courseid = $course->id; $record->description = array('format' => FORMAT_HTML, 'text' => 'Text with img <img src="@@PLUGINFILE@@/fakeimage.png">', 'itemid' => $draftidfile); $courseevent = $this->create_calendar_event('course', $USER->id, 'course', 2, time(), $record); $userevent = $this->create_calendar_event('user', $USER->id); $record = new stdClass(); $record->courseid = $course->id; $record->groupid = $group->id; $groupevent = $this->create_calendar_event('group', $USER->id, 'group', 0, time(), $record); $paramevents = array('eventids' => array($siteevent->id), 'courseids' => array($course->id), 'groupids' => array($group->id)); $options = array('siteevents' => true, 'userevents' => true); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); // Check to see if we got all events. $this->assertEquals(5, count($events['events'])); $this->assertEquals(0, count($events['warnings'])); $options = array('siteevents' => true, 'userevents' => true, 'timeend' => time() + 7 * WEEKSECS); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(5, count($events['events'])); $this->assertEquals(0, count($events['warnings'])); // Expect the same URL in the description of two different events (because they are repeated). $coursecontext = context_course::instance($course->id); $expectedurl = "webservice/pluginfile.php/{$coursecontext->id}/calendar/event_description/{$courseevent->id}/fakeimage.png"; $withdescription = 0; foreach ($events['events'] as $event) { if (!empty($event['description'])) { $withdescription++; $this->assertContains($expectedurl, $event['description']); } } $this->assertEquals(2, $withdescription); // Let's play around with caps. $this->setUser($user); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(2, count($events['events'])); // site, user. $this->assertEquals(2, count($events['warnings'])); // course, group. $role = $DB->get_record('role', array('shortname' => 'student')); $this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(4, count($events['events'])); // site, user, both course events. $this->assertEquals(1, count($events['warnings'])); // group. $options = array('siteevents' => true, 'userevents' => true, 'timeend' => time() + HOURSECS); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(3, count($events['events'])); // site, user, one course event. $this->assertEquals(1, count($events['warnings'])); // group. groups_add_member($group, $user); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(4, count($events['events'])); // site, user, group, one course event. $this->assertEquals(0, count($events['warnings'])); $paramevents = array('courseids' => array($course->id), 'groupids' => array($group->id)); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(4, count($events['events'])); // site, user, group, one course event. $this->assertEquals(0, count($events['warnings'])); $paramevents = array('groupids' => array($group->id, 23)); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(3, count($events['events'])); // site, user, group. $this->assertEquals(1, count($events['warnings'])); $paramevents = array('courseids' => array(23)); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(2, count($events['events'])); // site, user. $this->assertEquals(1, count($events['warnings'])); $paramevents = array(); $options = array('siteevents' => false, 'userevents' => false, 'timeend' => time() + 7 * WEEKSECS); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(0, count($events['events'])); // nothing returned. $this->assertEquals(0, count($events['warnings'])); $paramevents = array('eventids' => array($siteevent->id, $groupevent->id)); $options = array('siteevents' => false, 'userevents' => false, 'timeend' => time() + 7 * WEEKSECS); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(2, count($events['events'])); // site, group. $this->assertEquals(0, count($events['warnings'])); $paramevents = array('eventids' => array($siteevent->id)); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(1, count($events['events'])); // site. $this->assertEquals(0, count($events['warnings'])); // Try getting a course event by its id. $paramevents = array('eventids' => array($courseevent->id)); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertEquals(1, count($events['events'])); $this->assertEquals(0, count($events['warnings'])); // Now, create an activity event. $this->setAdminUser(); $nexttime = time() + DAYSECS; $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id, 'duedate' => $nexttime)); $this->setUser($user); $paramevents = array('courseids' => array($course->id)); $options = array('siteevents' => true, 'userevents' => true, 'timeend' => time() + WEEKSECS); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); $this->assertCount(5, $events['events']); // Hide the assignment. set_coursemodule_visible($assign->cmid, 0); // Empty all the caches that may be affected by this change. accesslib_clear_all_caches_for_unit_testing(); course_modinfo::clear_instance_cache(); $events = core_calendar_external::get_calendar_events($paramevents, $options); $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events); // Expect one less. $this->assertCount(4, $events['events']); }
/** * Test view_lti */ public function test_view_lti() { global $DB; // Test invalid instance id. try { mod_lti_external::view_lti(0); $this->fail('Exception expected due to invalid mod_lti instance id.'); } catch (moodle_exception $e) { $this->assertEquals('invalidrecord', $e->errorcode); } // Test not-enrolled user. $usernotenrolled = self::getDataGenerator()->create_user(); $this->setUser($usernotenrolled); try { mod_lti_external::view_lti($this->lti->id); $this->fail('Exception expected due to not enrolled user.'); } catch (moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } // Test user with full capabilities. $this->setUser($this->student); // Trigger and capture the event. $sink = $this->redirectEvents(); $result = mod_lti_external::view_lti($this->lti->id); $result = external_api::clean_returnvalue(mod_lti_external::view_lti_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_lti\\event\\course_module_viewed', $event); $this->assertEquals($this->context, $event->get_context()); $moodlelti = new \moodle_url('/mod/lti/view.php', array('id' => $this->cm->id)); $this->assertEquals($moodlelti, $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/lti:view', CAP_PROHIBIT, $this->studentrole->id, $this->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_lti_external::view_lti($this->lti->id); $this->fail('Exception expected due to missing capability.'); } catch (moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } }
/** * Test start_attempt */ public function test_start_attempt() { global $DB; // Create a new quiz with attempts. $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz'); $data = array('course' => $this->course->id, 'sumgrades' => 1); $quiz = $quizgenerator->create_instance($data); $context = context_module::instance($quiz->cmid); try { mod_quiz_external::start_attempt($quiz->id); $this->fail('Exception expected due to missing questions.'); } catch (moodle_quiz_exception $e) { $this->assertEquals('noquestionsfound', $e->errorcode); } // Create a question. $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); $cat = $questiongenerator->create_question_category(); $question = $questiongenerator->create_question('numerical', null, array('category' => $cat->id)); quiz_add_quiz_question($question->id, $quiz); $quizobj = quiz::create($quiz->id, $this->student->id); // Set grade to pass. $item = grade_item::fetch(array('courseid' => $this->course->id, 'itemtype' => 'mod', 'itemmodule' => 'quiz', 'iteminstance' => $quiz->id, 'outcomeid' => null)); $item->gradepass = 80; $item->update(); $this->setUser($this->student); // Try to open attempt in closed quiz. $quiz->timeopen = time() - WEEKSECS; $quiz->timeclose = time() - DAYSECS; $DB->update_record('quiz', $quiz); $result = mod_quiz_external::start_attempt($quiz->id); $result = external_api::clean_returnvalue(mod_quiz_external::start_attempt_returns(), $result); $this->assertEquals([], $result['attempt']); $this->assertCount(1, $result['warnings']); // Now with a password. $quiz->timeopen = 0; $quiz->timeclose = 0; $quiz->password = '******'; $DB->update_record('quiz', $quiz); try { mod_quiz_external::start_attempt($quiz->id, array(array("name" => "quizpassword", "value" => 'bad'))); $this->fail('Exception expected due to invalid passwod.'); } catch (moodle_exception $e) { $this->assertEquals(get_string('passworderror', 'quizaccess_password'), $e->errorcode); } // Now, try everything correct. $result = mod_quiz_external::start_attempt($quiz->id, array(array("name" => "quizpassword", "value" => 'abc'))); $result = external_api::clean_returnvalue(mod_quiz_external::start_attempt_returns(), $result); $this->assertEquals(1, $result['attempt']['attempt']); $this->assertEquals($this->student->id, $result['attempt']['userid']); $this->assertEquals($quiz->id, $result['attempt']['quiz']); $this->assertCount(0, $result['warnings']); $attemptid = $result['attempt']['id']; // We are good, try to start a new attempt now. try { mod_quiz_external::start_attempt($quiz->id, array(array("name" => "quizpassword", "value" => 'abc'))); $this->fail('Exception expected due to attempt not finished.'); } catch (moodle_quiz_exception $e) { $this->assertEquals('attemptstillinprogress', $e->errorcode); } // Finish the started attempt. // Process some responses from the student. $timenow = time(); $attemptobj = quiz_attempt::create($attemptid); $tosubmit = array(1 => array('answer' => '3.14')); $attemptobj->process_submitted_actions($timenow, false, $tosubmit); // Finish the attempt. $attemptobj = quiz_attempt::create($attemptid); $this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question()); $attemptobj->process_finish($timenow, false); // We should be able to start a new attempt. $result = mod_quiz_external::start_attempt($quiz->id, array(array("name" => "quizpassword", "value" => 'abc'))); $result = external_api::clean_returnvalue(mod_quiz_external::start_attempt_returns(), $result); $this->assertEquals(2, $result['attempt']['attempt']); $this->assertEquals($this->student->id, $result['attempt']['userid']); $this->assertEquals($quiz->id, $result['attempt']['quiz']); $this->assertCount(0, $result['warnings']); // 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/quiz:attempt', CAP_PROHIBIT, $this->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_quiz_external::start_attempt($quiz->id); $this->fail('Exception expected due to missing capability.'); } catch (required_capability_exception $e) { $this->assertEquals('nopermissions', $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']); }