本文整理汇总了PHP中accesslib_clear_all_caches_for_unit_testing函数的典型用法代码示例。如果您正苦于以下问题:PHP accesslib_clear_all_caches_for_unit_testing函数的具体用法?PHP accesslib_clear_all_caches_for_unit_testing怎么用?PHP accesslib_clear_all_caches_for_unit_testing使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了accesslib_clear_all_caches_for_unit_testing函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: load_data
/**
* Loads some data to be used by the different tests
* @param int $s1grade Student 1 grade
* @param int $s2grade Student 2 grade
* @return array Course and users instances
*/
private function load_data($s1grade, $s2grade)
{
global $DB;
$course = $this->getDataGenerator()->create_course(array('groupmode' => SEPARATEGROUPS, 'groupmodeforce' => 1));
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$student1 = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($student1->id, $course->id, $studentrole->id);
$student2 = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($student2->id, $course->id, $studentrole->id);
$teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
$teacher = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
$context = context_course::instance($course->id);
assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $teacherrole->id, $context);
accesslib_clear_all_caches_for_unit_testing();
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
$group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
groups_add_member($group1->id, $student1->id);
groups_add_member($group1->id, $teacher->id);
groups_add_member($group2->id, $student2->id);
$assignment = $this->getDataGenerator()->create_module('assign', array('name' => "Test assign", 'course' => $course->id));
$modcontext = get_coursemodule_from_instance('assign', $assignment->id, $course->id);
$assignment->cmidnumber = $modcontext->id;
$student1grade = array('userid' => $student1->id, 'rawgrade' => $s1grade);
$student2grade = array('userid' => $student2->id, 'rawgrade' => $s2grade);
$studentgrades = array($student1->id => $student1grade, $student2->id => $student2grade);
assign_grade_item_update($assignment, $studentgrades);
return array($course, $teacher, $student1, $student2, $assignment);
}
示例2: test_user_get_user_details_courses
/**
* Test user_get_user_details_courses
*/
public function test_user_get_user_details_courses()
{
global $DB;
$this->resetAfterTest();
// Create user and modify user profile.
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$course1 = $this->getDataGenerator()->create_course();
$coursecontext = context_course::instance($course1->id);
$teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
$this->getDataGenerator()->enrol_user($user1->id, $course1->id);
$this->getDataGenerator()->enrol_user($user2->id, $course1->id);
role_assign($teacherrole->id, $user1->id, $coursecontext->id);
role_assign($teacherrole->id, $user2->id, $coursecontext->id);
accesslib_clear_all_caches_for_unit_testing();
// Get user2 details as a user with super system capabilities.
$result = user_get_user_details_courses($user2);
$this->assertEquals($user2->id, $result['id']);
$this->assertEquals(fullname($user2), $result['fullname']);
$this->assertEquals($course1->id, $result['enrolledcourses'][0]['id']);
$this->setUser($user1);
// Get user2 details as a user who can only see this user in a course.
$result = user_get_user_details_courses($user2);
$this->assertEquals($user2->id, $result['id']);
$this->assertEquals(fullname($user2), $result['fullname']);
$this->assertEquals($course1->id, $result['enrolledcourses'][0]['id']);
}
示例3: unassignUserCapability
/**
* Unassign a capability to $USER
*
* @param string $capability capability name
* @param int $contextid
* @param int $roleid
*/
public static function unassignUserCapability($capability, $contextid, $roleid) {
global $USER;
unassign_capability($capability, $roleid, $contextid);
accesslib_clear_all_caches_for_unit_testing();
}
示例4: assign_capability
protected function assign_capability($capability, $permission = CAP_ALLOW, $contextid = null) {
if ($contextid === null) {
$contextid = context_system::instance();
}
if (is_object($contextid)) {
$contextid = $contextid->id;
}
assign_capability($capability, $permission, $this->get_roleid($contextid), $contextid, true);
accesslib_clear_all_caches_for_unit_testing();
}
示例5: create_role_with_caps
/**
* Create a role with capabilities and permissions.
*
* @param string|array $caps Capability names.
* @param int $perm Constant CAP_* to apply to the capabilities.
* @return int The new role ID.
*/
protected function create_role_with_caps($caps, $perm)
{
$caps = (array) $caps;
$dg = $this->getDataGenerator();
$roleid = $dg->create_role();
foreach ($caps as $cap) {
assign_capability($cap, $perm, $roleid, context_system::instance()->id, true);
}
accesslib_clear_all_caches_for_unit_testing();
return $roleid;
}
示例6: test_load_users
/**
* Test load_users method.
*/
public function test_load_users()
{
global $DB;
$this->setAdminUser();
$this->resetAfterTest(true);
$roleteacher = $DB->get_record('role', array('shortname' => 'teacher'), '*', MUST_EXIST);
// Create a course, users and groups.
$course = $this->getDataGenerator()->create_course();
$coursecontext = context_course::instance($course->id);
$group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
$teacher = $this->getDataGenerator()->create_user();
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($teacher->id, $course->id, $roleteacher->id);
$this->getDataGenerator()->enrol_user($user1->id, $course->id);
$this->getDataGenerator()->enrol_user($user2->id, $course->id);
$this->getDataGenerator()->create_group_member(array('groupid' => $group->id, 'userid' => $teacher->id));
$this->getDataGenerator()->create_group_member(array('groupid' => $group->id, 'userid' => $user1->id));
$this->getDataGenerator()->create_group_member(array('groupid' => $group->id, 'userid' => $user2->id));
// Perform a regrade before creating the report.
grade_regrade_final_grades($course->id);
$screentest = new gradereport_singleview_screen_testable($course->id, 0, $group->id);
$groupusers = $screentest->test_load_users();
$this->assertCount(2, $groupusers);
// Now, let's suspend the enrolment of a user. Should return only one user.
$this->getDataGenerator()->enrol_user($user2->id, $course->id, $roleteacher->id, 'manual', 0, 0, ENROL_USER_SUSPENDED);
$users = $screentest->test_load_users();
$this->assertCount(1, $users);
// Change the viewsuspendedusers capabilities and set the user preference to display suspended users.
assign_capability('moodle/course:viewsuspendedusers', CAP_ALLOW, $roleteacher->id, $coursecontext, true);
set_user_preference('grade_report_showonlyactiveenrol', false, $teacher);
accesslib_clear_all_caches_for_unit_testing();
$this->setUser($teacher);
$screentest = new gradereport_singleview_screen_testable($course->id, 0, $group->id);
$users = $screentest->test_load_users();
$this->assertCount(2, $users);
// Change the capability again, now the user can't see the suspended enrolments.
assign_capability('moodle/course:viewsuspendedusers', CAP_PROHIBIT, $roleteacher->id, $coursecontext, true);
set_user_preference('grade_report_showonlyactiveenrol', false, $teacher);
accesslib_clear_all_caches_for_unit_testing();
$users = $screentest->test_load_users();
$this->assertCount(1, $users);
// Now, activate the user enrolment again. We shall get 2 users now.
$this->getDataGenerator()->enrol_user($user2->id, $course->id, $roleteacher->id, 'manual', 0, 0, ENROL_USER_ACTIVE);
$users = $screentest->test_load_users();
$this->assertCount(2, $users);
}
示例7: setUp
public function setUp() {
global $CFG;
parent::setUp();
foreach ($this->testtables as $dir => $tables) {
$this->create_test_tables($tables, $dir); // Create tables
}
$this->switch_to_test_db(); // Switch to test DB for all the execution
// Nasty hack, recreate the system context record (the accesslib API does not allow to create it easily)
$this->create_system_context_record();
// Reset all caches
accesslib_clear_all_caches_for_unit_testing();
$this->syscontext = get_context_instance(CONTEXT_SYSTEM);
$this->fill_records();
// Ignore any frontpageroleid, that would require to crete more contexts
$this->originaldefaultfrontpageroleid = $CFG->defaultfrontpageroleid;
$CFG->defaultfrontpageroleid = null;
}
示例8: test_get_calendar_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']);
}
示例9: test_can_edit_submission
/**
* Testing can_edit_submission
*/
public function test_can_edit_submission()
{
global $PAGE, $DB;
$this->create_extra_users();
$this->setAdminUser();
// Create assignment (onlinetext).
$assign = $this->create_instance(array('assignsubmission_onlinetext_enabled' => 1, 'submissiondrafts' => 1));
$PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id)));
// Check student can edit their own submission.
$this->assertTrue($assign->can_edit_submission($this->students[0]->id, $this->students[0]->id));
// Check student cannot edit others submission.
$this->assertFalse($assign->can_edit_submission($this->students[0]->id, $this->students[1]->id));
// Check teacher cannot (by default) edit a students submission.
$this->assertFalse($assign->can_edit_submission($this->students[0]->id, $this->teachers[0]->id));
// Add the required capability to edit a student submission.
$roleid = create_role('Dummy role', 'dummyrole', 'dummy role description');
assign_capability('mod/assign:editothersubmission', CAP_ALLOW, $roleid, $assign->get_context()->id);
role_assign($roleid, $this->teachers[0]->id, $assign->get_context()->id);
accesslib_clear_all_caches_for_unit_testing();
// Retest - should now have access.
$this->assertTrue($assign->can_edit_submission($this->students[0]->id, $this->teachers[0]->id));
// Force create an assignment with SEPARATEGROUPS.
$data = new stdClass();
$data->courseid = $this->course->id;
$data->name = 'Grouping';
$groupingid = groups_create_grouping($data);
groups_assign_grouping($groupingid, $this->groups[0]->id);
groups_assign_grouping($groupingid, $this->groups[1]->id);
$assign = $this->create_instance(array('groupingid' => $groupingid, 'groupmode' => SEPARATEGROUPS));
// Add the capability to the new assignment for extra students 0 and 1.
assign_capability('mod/assign:editothersubmission', CAP_ALLOW, $roleid, $assign->get_context()->id);
role_assign($roleid, $this->extrastudents[0]->id, $assign->get_context()->id);
role_assign($roleid, $this->extrastudents[1]->id, $assign->get_context()->id);
accesslib_clear_all_caches_for_unit_testing();
// Verify the extra student does not have the capability to edit a submission not in their group.
$this->assertFalse($assign->can_edit_submission($this->students[0]->id, $this->extrastudents[1]->id));
// Verify the extra student does have the capability to edit a submission in their group.
$this->assertTrue($assign->can_edit_submission($this->students[0]->id, $this->extrastudents[0]->id));
}
示例10: test_view_book
/**
* Test view_book
*/
public function test_view_book() {
global $DB;
$this->resetAfterTest(true);
$this->setAdminUser();
// Setup test data.
$course = $this->getDataGenerator()->create_course();
$book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
$bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
$chapter = $bookgenerator->create_chapter(array('bookid' => $book->id));
$chapterhidden = $bookgenerator->create_chapter(array('bookid' => $book->id, 'hidden' => 1));
$context = context_module::instance($book->cmid);
$cm = get_coursemodule_from_instance('book', $book->id);
// Test invalid instance id.
try {
mod_book_external::view_book(0);
$this->fail('Exception expected due to invalid mod_book instance id.');
} catch (moodle_exception $e) {
$this->assertEquals('invalidrecord', $e->errorcode);
}
// Test not-enrolled user.
$user = self::getDataGenerator()->create_user();
$this->setUser($user);
try {
mod_book_external::view_book($book->id, 0);
$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_book_external::view_book($book->id, 0);
$result = external_api::clean_returnvalue(mod_book_external::view_book_returns(), $result);
$events = $sink->get_events();
$this->assertCount(2, $events);
$event = array_shift($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_book\event\course_module_viewed', $event);
$this->assertEquals($context, $event->get_context());
$moodleurl = new \moodle_url('/mod/book/view.php', array('id' => $cm->id));
$this->assertEquals($moodleurl, $event->get_url());
$this->assertEventContextNotUsed($event);
$this->assertNotEmpty($event->get_name());
$event = array_shift($events);
$this->assertInstanceOf('\mod_book\event\chapter_viewed', $event);
$this->assertEquals($chapter->id, $event->objectid);
$result = mod_book_external::view_book($book->id, $chapter->id);
$result = external_api::clean_returnvalue(mod_book_external::view_book_returns(), $result);
$events = $sink->get_events();
// We expect a total of 3 events.
$this->assertCount(3, $events);
// Try to view a hidden chapter.
try {
mod_book_external::view_book($book->id, $chapterhidden->id);
$this->fail('Exception expected due to missing capability.');
} catch (moodle_exception $e) {
$this->assertEquals('errorchapter', $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/book:read', CAP_PROHIBIT, $studentrole->id, $context->id);
accesslib_clear_all_caches_for_unit_testing();
try {
mod_book_external::view_book($book->id, 0);
$this->fail('Exception expected due to missing capability.');
} catch (moodle_exception $e) {
$this->assertEquals('nopermissions', $e->errorcode);
}
}
示例11: get_enrolled_users_with_capability_setup
public function get_enrolled_users_with_capability_setup($capability)
{
global $USER, $DB;
$this->resetAfterTest(true);
$return = new stdClass();
// Create the course and fetch its context.
$return->course = self::getDataGenerator()->create_course();
$context = context_course::instance($return->course->id);
// Create one teacher, and two students.
$return->teacher = self::getDataGenerator()->create_user();
$return->student1 = self::getDataGenerator()->create_user();
$return->student2 = self::getDataGenerator()->create_user();
// Create a new student role based on the student archetype but with the capability prohibitted.
$fakestudentroleid = create_role('Fake student role', 'fakestudent', 'Fake student role', 'student');
assign_capability($capability, CAP_PROHIBIT, $fakestudentroleid, $context->id);
// Enrol all of the users in the course.
// * 'teacher' is an editing teacher.
// * 'student1' is a standard student.
// * 'student2' is a student with the capability prohibitted.
$editingteacherroleid = $DB->get_field('role', 'id', array('shortname' => 'editingteacher'));
$studentroleid = $DB->get_field('role', 'id', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($return->teacher->id, $return->course->id, $editingteacherroleid);
$this->getDataGenerator()->enrol_user($return->student1->id, $return->course->id, $studentroleid);
$this->getDataGenerator()->enrol_user($return->student2->id, $return->course->id, $fakestudentroleid);
// Log in as the teacher.
$this->setUser($return->teacher);
// Clear caches.
accesslib_clear_all_caches_for_unit_testing();
return $return;
}
示例12: test_submit_answers
/**
* Test submit_answers
*/
public function test_submit_answers()
{
global $DB;
// Test user with full capabilities.
$this->setUser($this->student);
// Build our questions and responses array.
$realquestions = array();
$questions = survey_get_questions($this->survey);
$i = 5;
foreach ($questions as $q) {
if ($q->type >= 0) {
if ($q->multi) {
$subquestions = survey_get_subquestions($q);
foreach ($subquestions as $sq) {
$realquestions[] = array('key' => 'q' . $sq->id, 'value' => $i % 5 + 1);
$i++;
}
} else {
$realquestions[] = array('key' => 'q' . $q->id, 'value' => $i % 5 + 1);
$i++;
}
}
}
$result = mod_survey_external::submit_answers($this->survey->id, $realquestions);
$result = external_api::clean_returnvalue(mod_survey_external::submit_answers_returns(), $result);
$this->assertTrue($result['status']);
$this->assertCount(0, $result['warnings']);
$dbanswers = $DB->get_records_menu('survey_answers', array('survey' => $this->survey->id), '', 'question, answer1');
foreach ($realquestions as $q) {
$id = str_replace('q', '', $q['key']);
$this->assertEquals($q['value'], $dbanswers[$id]);
}
// Submit again, we expect an error here.
try {
mod_survey_external::submit_answers($this->survey->id, $realquestions);
$this->fail('Exception expected due to answers already submitted.');
} catch (moodle_exception $e) {
$this->assertEquals('alreadysubmitted', $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/survey:participate', CAP_PROHIBIT, $this->studentrole->id, $this->context->id);
accesslib_clear_all_caches_for_unit_testing();
try {
mod_survey_external::submit_answers($this->survey->id, $realquestions);
$this->fail('Exception expected due to missing capability.');
} catch (moodle_exception $e) {
$this->assertEquals('nopermissions', $e->errorcode);
}
// Test not-enrolled user.
$usernotenrolled = self::getDataGenerator()->create_user();
$this->setUser($usernotenrolled);
try {
mod_survey_external::submit_answers($this->survey->id, $realquestions);
$this->fail('Exception expected due to not enrolled user.');
} catch (moodle_exception $e) {
$this->assertEquals('requireloginerror', $e->errorcode);
}
}
示例13: test_validate_attempt
/**
* 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 = 'abc';
$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);
}
}
示例14: test_get_switchable_roles
function test_get_switchable_roles()
{
global $USER;
$tablenames = array('role', 'role_capabilities', 'role_assignments', 'role_allow_switch', 'capabilities', 'context', 'role_names');
$this->create_test_tables($tablenames, 'lib');
$this->switch_to_test_db();
$this->switch_to_test_cfg();
// Ensure SYSCONTEXTID is set.
get_context_instance(CONTEXT_SYSTEM);
$contexts = $this->load_test_data('context', array('contextlevel', 'instanceid', 'path', 'depth'), array('sys' => array(CONTEXT_SYSTEM, 0, '/' . SYSCONTEXTID, 1), 'cat' => array(CONTEXT_COURSECAT, 66, '/' . SYSCONTEXTID . '/' . (SYSCONTEXTID + 1), 2), 'cou' => array(CONTEXT_COURSE, 666, '/' . SYSCONTEXTID . '/' . (SYSCONTEXTID + 1) . '/' . (SYSCONTEXTID + 2), 3), 'fp' => array(CONTEXT_COURSE, SITEID, '/' . SYSCONTEXTID . '/' . SITEID, 2)));
$this->testdb->set_field('context', 'id', SYSCONTEXTID, array('id' => $contexts['sys']->id));
$this->testdb->set_field('context', 'id', SYSCONTEXTID + 1, array('id' => $contexts['cat']->id));
$this->testdb->set_field('context', 'id', SYSCONTEXTID + 2, array('id' => $contexts['cou']->id));
$syscontext = $contexts['sys'];
$syscontext->id = SYSCONTEXTID;
$context = $contexts['cou'];
$context->id = SYSCONTEXTID + 2;
$roles = $this->load_test_data('role', array('name', 'shortname', 'description', 'sortorder'), array('r1' => array('r1', 'r1', 'not null', 2), 'r2' => array('r2', 'r2', 'not null', 3), 'funny' => array('funny', 'funny', 'not null', 4)));
$r1id = $roles['r1']->id;
$r2id = $roles['r2']->id;
$funnyid = $roles['funny']->id;
// strange role
// Note that get_switchable_roles requires at least one capability for
// each role. I am not really clear why it was implemented that way
// but this makes the test work.
$roles = $this->load_test_data('role_capabilities', array('roleid', 'capability'), array(array($r1id, 'moodle/say:hello'), array($r2id, 'moodle/say:hello'), array($funnyid, 'moodle/say:hello')));
$this->load_test_data('role_assignments', array('userid', 'contextid', 'roleid'), array(array(2, SYSCONTEXTID + 1, $r1id), array(3, SYSCONTEXTID + 2, $r2id)));
$this->load_test_data('role_allow_switch', array('roleid', 'allowswitch'), array(array($r1id, $r2id), array($r2id, $r1id), array($r2id, $r2id), array($r2id, $funnyid)));
// r1 should be able to switch to r2, but this user only has r1 in $context, not $syscontext.
$this->switch_global_user_id(2);
accesslib_clear_all_caches_for_unit_testing();
$this->assert(new ArraysHaveSameValuesExpectation(array()), array_keys(get_switchable_roles($syscontext)));
$this->assert(new ArraysHaveSameValuesExpectation(array($r2id)), array_keys(get_switchable_roles($context)));
$this->revert_global_user_id();
// The table says r2 should be able to switch to all of r1, r2 and funny;
// this used to be restricted further beyond the switch table (for example
// to prevent you switching to roles with doanything) but is not any more
// (for example because doanything does not exist).
$this->switch_global_user_id(3);
accesslib_clear_all_caches_for_unit_testing();
$this->assert(new ArraysHaveSameValuesExpectation(array()), array_keys(get_switchable_roles($syscontext)));
$this->assert(new ArraysHaveSameValuesExpectation(array($r2id, $r1id, $funnyid)), array_keys(get_switchable_roles($context)));
}
示例15: setUp
public function setUp()
{
parent::setUp();
// Make sure accesslib has cached a sensible system context object
// before we switch to the test DB.
$this->syscontext = get_context_instance(CONTEXT_SYSTEM);
// Create the table we need and switch to test DB.
$this->create_test_tables(array('filter_active', 'filter_config', 'context', 'course', 'course_categories', 'course_modules', 'modules', 'course_sections', 'course_modules_availability', 'grade_items', 'cache_text'), 'lib');
$this->create_test_tables(array('label'), 'mod/label');
$this->switch_to_test_db();
// Nasty hack, recreate the system context record (the accesslib API does not allow to create it easily)
$this->create_system_context_record();
// Reset all caches
accesslib_clear_all_caches_for_unit_testing();
$this->syscontext = get_context_instance(CONTEXT_SYSTEM);
// Make the category
$cat = new stdClass();
$cat->name = 'testcategory';
$cat->parent = 0;
$cat->depth = 1;
$cat->sortorder = 100;
$cat->timemodified = time();
$catid = $this->testdb->insert_record('course_categories', $cat);
$this->testdb->set_field('course_categories', 'path', '/' . $catid, array('id' => $catid));
$this->catcontext = context_coursecat::instance($catid);
// Make the course
$course = (object) array('shortname' => 'TEST101');
$courseid = $this->testdb->insert_record('course', $course);
$this->coursecontext = context_course::instance($courseid);
// Set up section
$section = (object) array('course' => $courseid);
$section->id = $this->testdb->insert_record('course_sections', $section);
// Make course-modules
$mod = (object) array('name' => 'label', 'visible' => 1);
$mod->id = $this->testdb->insert_record('modules', $mod);
$label1 = (object) array('course' => $courseid, 'intro' => 'Intro 1', 'name' => 'Label 1');
$label1->id = $this->testdb->insert_record('label', $label1);
$cm1 = (object) array('course' => $courseid, 'section' => $section->id, 'module' => $mod->id, 'instance' => $label1->id);
$cm1->id = $this->testdb->insert_record('course_modules', $cm1);
$this->activity1context = context_module::instance($cm1->id);
$label2 = (object) array('course' => $courseid, 'intro' => 'Intro 2', 'name' => 'Label 2');
$label2->id = $this->testdb->insert_record('label', $label2);
$cm2 = (object) array('course' => $courseid, 'section' => $section->id, 'module' => $mod->id, 'instance' => $label2->id);
$cm2->id = $this->testdb->insert_record('course_modules', $cm2);
$this->testdb->set_field('course_sections', 'sequence', "{$cm1->id},{$cm2->id}", array('id' => $section->id));
$this->activity2context = context_module::instance($cm2->id);
}