Beispiel #1
0
 /**
  * Test a specific type of module.
  *
  * @param string $modulename - the module name to test
  */
 private function update_specific_module_test($modulename)
 {
     global $DB, $CFG;
     $this->resetAfterTest(true);
     $this->setAdminUser();
     // Warnings: you'll need to change this line if ever you come to test a module not following Moodle standard.
     require_once $CFG->dirroot . '/mod/' . $modulename . '/lib.php';
     // Enable avaibility.
     // If not enabled all conditional fields will be ignored.
     set_config('enableavailability', 1);
     // Enable course completion.
     // If not enabled all completion settings will be ignored.
     set_config('enablecompletion', COMPLETION_ENABLED);
     // Enable forum RSS feeds.
     set_config('enablerssfeeds', 1);
     set_config('forum_enablerssfeeds', 1);
     $course = $this->getDataGenerator()->create_course(array('numsections' => 1, 'enablecompletion' => COMPLETION_ENABLED), array('createsections' => true));
     $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
     // Create assign module instance for testing gradeitem.
     $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
     $params['course'] = $course->id;
     $instance = $generator->create_instance($params);
     $assigncm = get_coursemodule_from_instance('assign', $instance->id);
     // Create the test forum to update.
     $initvalues = new stdClass();
     $initvalues->introformat = FORMAT_HTML;
     $initvalues->course = $course->id;
     $forum = self::getDataGenerator()->create_module('forum', $initvalues);
     // Retrieve course module.
     $cm = get_coursemodule_from_instance('forum', $forum->id);
     // Module test values.
     $moduleinfo = new stdClass();
     // Always mandatory generic values to any module.
     $moduleinfo->coursemodule = $cm->id;
     $moduleinfo->modulename = $modulename;
     $moduleinfo->course = $course->id;
     $moduleinfo->groupingid = $grouping->id;
     $moduleinfo->visible = true;
     // Sometimes optional generic values for some modules.
     $moduleinfo->name = 'My test module';
     $moduleinfo->showdescription = 1;
     // standard boolean
     require_once $CFG->libdir . '/gradelib.php';
     $gradecats = grade_get_categories_menu($moduleinfo->course, false);
     $gradecatid = current(array_keys($gradecats));
     // Retrieve the first key of $gradecats
     $moduleinfo->gradecat = $gradecatid;
     $moduleinfo->groupmode = VISIBLEGROUPS;
     $moduleinfo->cmidnumber = 'idnumber_XXX';
     // Completion common to all module.
     $moduleinfo->completion = COMPLETION_TRACKING_AUTOMATIC;
     $moduleinfo->completionview = COMPLETION_VIEW_REQUIRED;
     $moduleinfo->completiongradeitemnumber = 1;
     $moduleinfo->completionexpected = time() + 7 * 24 * 3600;
     $moduleinfo->completionunlocked = 1;
     // Conditional activity.
     $coursegradeitem = grade_item::fetch_course_item($moduleinfo->course);
     //the activity will become available only when the user reach some grade into the course itself.
     $moduleinfo->availability = json_encode(\core_availability\tree::get_root_json(array(\availability_date\condition::get_json('>=', time()), \availability_date\condition::get_json('<', time() + 7 * 24 * 3600), \availability_grade\condition::get_json($coursegradeitem->id, 10, 80), \availability_profile\condition::get_json(false, 'email', 'contains', '@'), \availability_completion\condition::get_json($assigncm->id, COMPLETION_COMPLETE)), '&'));
     // Grading and Advanced grading.
     require_once $CFG->dirroot . '/rating/lib.php';
     $moduleinfo->assessed = RATING_AGGREGATE_AVERAGE;
     $moduleinfo->scale = 10;
     // Note: it could be minus (for specific course scale). It is a signed number.
     $moduleinfo->assesstimestart = time();
     $moduleinfo->assesstimefinish = time() + 7 * 24 * 3600;
     // RSS.
     $moduleinfo->rsstype = 2;
     $moduleinfo->rssarticles = 10;
     // Optional intro editor (depends of module).
     $draftid_editor = 0;
     file_prepare_draft_area($draftid_editor, null, null, null, null);
     $moduleinfo->introeditor = array('text' => 'This is a module', 'format' => FORMAT_HTML, 'itemid' => $draftid_editor);
     // Following is the advanced grading method area called 'submissions' for the 'assign' module.
     if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
         $moduleinfo->grade = 100;
     }
     // Plagiarism form values.
     // No plagiarism plugin installed by default. Use this space to make your own test.
     // Values specific to the module.
     $modulesetvalues = $modulename . '_update_set_values';
     $this->{$modulesetvalues}($moduleinfo);
     // Create the module.
     $result = update_module($moduleinfo);
     // Retrieve the module info.
     $dbmodinstance = $DB->get_record($moduleinfo->modulename, array('id' => $result->instance));
     $dbcm = get_coursemodule_from_instance($moduleinfo->modulename, $result->instance);
     // Retrieve the grade item.
     $gradeitem = $DB->get_record('grade_items', array('courseid' => $moduleinfo->course, 'iteminstance' => $dbmodinstance->id, 'itemmodule' => $moduleinfo->modulename));
     // Compare the values common to all module instances.
     $this->assertEquals($moduleinfo->modulename, $dbcm->modname);
     $this->assertEquals($moduleinfo->course, $dbcm->course);
     $this->assertEquals($moduleinfo->groupingid, $dbcm->groupingid);
     $this->assertEquals($moduleinfo->visible, $dbcm->visible);
     $this->assertEquals($moduleinfo->completion, $dbcm->completion);
     $this->assertEquals($moduleinfo->completionview, $dbcm->completionview);
     $this->assertEquals($moduleinfo->completiongradeitemnumber, $dbcm->completiongradeitemnumber);
     $this->assertEquals($moduleinfo->completionexpected, $dbcm->completionexpected);
     $this->assertEquals($moduleinfo->availability, $dbcm->availability);
     $this->assertEquals($moduleinfo->showdescription, $dbcm->showdescription);
     $this->assertEquals($moduleinfo->groupmode, $dbcm->groupmode);
     $this->assertEquals($moduleinfo->cmidnumber, $dbcm->idnumber);
     $this->assertEquals($moduleinfo->gradecat, $gradeitem->categoryid);
     // Optional grade testing.
     if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
         $this->assertEquals($moduleinfo->grade, $dbmodinstance->grade);
     }
     // Some optional (but quite common) to some module.
     $this->assertEquals($moduleinfo->name, $dbmodinstance->name);
     $this->assertEquals($moduleinfo->intro, $dbmodinstance->intro);
     $this->assertEquals($moduleinfo->introformat, $dbmodinstance->introformat);
     // Test specific to the module.
     $modulerunasserts = $modulename . '_update_run_asserts';
     $this->{$modulerunasserts}($moduleinfo, $dbmodinstance);
     return $moduleinfo;
 }
Beispiel #2
0
/**
 * This function will empty a course of user data.
 * It will retain the activities and the structure of the course.
 *
 * @param object $data an object containing all the settings including courseid (without magic quotes)
 * @return array status array of array component, item, error
 */
function reset_course_userdata($data)
{
    global $CFG, $DB;
    require_once $CFG->libdir . '/gradelib.php';
    require_once $CFG->libdir . '/completionlib.php';
    require_once $CFG->dirroot . '/group/lib.php';
    $data->courseid = $data->id;
    $context = context_course::instance($data->courseid);
    $eventparams = array('context' => $context, 'courseid' => $data->id, 'other' => array('reset_options' => (array) $data));
    $event = \core\event\course_reset_started::create($eventparams);
    $event->trigger();
    // Calculate the time shift of dates.
    if (!empty($data->reset_start_date)) {
        // Time part of course startdate should be zero.
        $data->timeshift = $data->reset_start_date - usergetmidnight($data->reset_start_date_old);
    } else {
        $data->timeshift = 0;
    }
    // Result array: component, item, error.
    $status = array();
    // Start the resetting.
    $componentstr = get_string('general');
    // Move the course start time.
    if (!empty($data->reset_start_date) and $data->timeshift) {
        // Change course start data.
        $DB->set_field('course', 'startdate', $data->reset_start_date, array('id' => $data->courseid));
        // Update all course and group events - do not move activity events.
        $updatesql = "UPDATE {event}\n                         SET timestart = timestart + ?\n                       WHERE courseid=? AND instance=0";
        $DB->execute($updatesql, array($data->timeshift, $data->courseid));
        // Update any date activity restrictions.
        if ($CFG->enableavailability) {
            \availability_date\condition::update_all_dates($data->courseid, $data->timeshift);
        }
        $status[] = array('component' => $componentstr, 'item' => get_string('datechanged'), 'error' => false);
    }
    if (!empty($data->reset_end_date)) {
        // If the user set a end date value respect it.
        $DB->set_field('course', 'enddate', $data->reset_end_date, array('id' => $data->courseid));
    } else {
        if ($data->timeshift > 0 && $data->reset_end_date_old) {
            // If there is a time shift apply it to the end date as well.
            $enddate = $data->reset_end_date_old + $data->timeshift;
            $DB->set_field('course', 'enddate', $enddate, array('id' => $data->courseid));
        }
    }
    if (!empty($data->reset_events)) {
        $DB->delete_records('event', array('courseid' => $data->courseid));
        $status[] = array('component' => $componentstr, 'item' => get_string('deleteevents', 'calendar'), 'error' => false);
    }
    if (!empty($data->reset_notes)) {
        require_once $CFG->dirroot . '/notes/lib.php';
        note_delete_all($data->courseid);
        $status[] = array('component' => $componentstr, 'item' => get_string('deletenotes', 'notes'), 'error' => false);
    }
    if (!empty($data->delete_blog_associations)) {
        require_once $CFG->dirroot . '/blog/lib.php';
        blog_remove_associations_for_course($data->courseid);
        $status[] = array('component' => $componentstr, 'item' => get_string('deleteblogassociations', 'blog'), 'error' => false);
    }
    if (!empty($data->reset_completion)) {
        // Delete course and activity completion information.
        $course = $DB->get_record('course', array('id' => $data->courseid));
        $cc = new completion_info($course);
        $cc->delete_all_completion_data();
        $status[] = array('component' => $componentstr, 'item' => get_string('deletecompletiondata', 'completion'), 'error' => false);
    }
    if (!empty($data->reset_competency_ratings)) {
        \core_competency\api::hook_course_reset_competency_ratings($data->courseid);
        $status[] = array('component' => $componentstr, 'item' => get_string('deletecompetencyratings', 'core_competency'), 'error' => false);
    }
    $componentstr = get_string('roles');
    if (!empty($data->reset_roles_overrides)) {
        $children = $context->get_child_contexts();
        foreach ($children as $child) {
            $DB->delete_records('role_capabilities', array('contextid' => $child->id));
        }
        $DB->delete_records('role_capabilities', array('contextid' => $context->id));
        // Force refresh for logged in users.
        $context->mark_dirty();
        $status[] = array('component' => $componentstr, 'item' => get_string('deletecourseoverrides', 'role'), 'error' => false);
    }
    if (!empty($data->reset_roles_local)) {
        $children = $context->get_child_contexts();
        foreach ($children as $child) {
            role_unassign_all(array('contextid' => $child->id));
        }
        // Force refresh for logged in users.
        $context->mark_dirty();
        $status[] = array('component' => $componentstr, 'item' => get_string('deletelocalroles', 'role'), 'error' => false);
    }
    // First unenrol users - this cleans some of related user data too, such as forum subscriptions, tracking, etc.
    $data->unenrolled = array();
    if (!empty($data->unenrol_users)) {
        $plugins = enrol_get_plugins(true);
        $instances = enrol_get_instances($data->courseid, true);
        foreach ($instances as $key => $instance) {
            if (!isset($plugins[$instance->enrol])) {
                unset($instances[$key]);
                continue;
            }
        }
        foreach ($data->unenrol_users as $withroleid) {
            if ($withroleid) {
                $sql = "SELECT ue.*\n                          FROM {user_enrolments} ue\n                          JOIN {enrol} e ON (e.id = ue.enrolid AND e.courseid = :courseid)\n                          JOIN {context} c ON (c.contextlevel = :courselevel AND c.instanceid = e.courseid)\n                          JOIN {role_assignments} ra ON (ra.contextid = c.id AND ra.roleid = :roleid AND ra.userid = ue.userid)";
                $params = array('courseid' => $data->courseid, 'roleid' => $withroleid, 'courselevel' => CONTEXT_COURSE);
            } else {
                // Without any role assigned at course context.
                $sql = "SELECT ue.*\n                          FROM {user_enrolments} ue\n                          JOIN {enrol} e ON (e.id = ue.enrolid AND e.courseid = :courseid)\n                          JOIN {context} c ON (c.contextlevel = :courselevel AND c.instanceid = e.courseid)\n                     LEFT JOIN {role_assignments} ra ON (ra.contextid = c.id AND ra.userid = ue.userid)\n                         WHERE ra.id IS null";
                $params = array('courseid' => $data->courseid, 'courselevel' => CONTEXT_COURSE);
            }
            $rs = $DB->get_recordset_sql($sql, $params);
            foreach ($rs as $ue) {
                if (!isset($instances[$ue->enrolid])) {
                    continue;
                }
                $instance = $instances[$ue->enrolid];
                $plugin = $plugins[$instance->enrol];
                if (!$plugin->allow_unenrol($instance) and !$plugin->allow_unenrol_user($instance, $ue)) {
                    continue;
                }
                $plugin->unenrol_user($instance, $ue->userid);
                $data->unenrolled[$ue->userid] = $ue->userid;
            }
            $rs->close();
        }
    }
    if (!empty($data->unenrolled)) {
        $status[] = array('component' => $componentstr, 'item' => get_string('unenrol', 'enrol') . ' (' . count($data->unenrolled) . ')', 'error' => false);
    }
    $componentstr = get_string('groups');
    // Remove all group members.
    if (!empty($data->reset_groups_members)) {
        groups_delete_group_members($data->courseid);
        $status[] = array('component' => $componentstr, 'item' => get_string('removegroupsmembers', 'group'), 'error' => false);
    }
    // Remove all groups.
    if (!empty($data->reset_groups_remove)) {
        groups_delete_groups($data->courseid, false);
        $status[] = array('component' => $componentstr, 'item' => get_string('deleteallgroups', 'group'), 'error' => false);
    }
    // Remove all grouping members.
    if (!empty($data->reset_groupings_members)) {
        groups_delete_groupings_groups($data->courseid, false);
        $status[] = array('component' => $componentstr, 'item' => get_string('removegroupingsmembers', 'group'), 'error' => false);
    }
    // Remove all groupings.
    if (!empty($data->reset_groupings_remove)) {
        groups_delete_groupings($data->courseid, false);
        $status[] = array('component' => $componentstr, 'item' => get_string('deleteallgroupings', 'group'), 'error' => false);
    }
    // Look in every instance of every module for data to delete.
    $unsupportedmods = array();
    if ($allmods = $DB->get_records('modules')) {
        foreach ($allmods as $mod) {
            $modname = $mod->name;
            $modfile = $CFG->dirroot . '/mod/' . $modname . '/lib.php';
            $moddeleteuserdata = $modname . '_reset_userdata';
            // Function to delete user data.
            if (file_exists($modfile)) {
                if (!$DB->count_records($modname, array('course' => $data->courseid))) {
                    continue;
                    // Skip mods with no instances.
                }
                include_once $modfile;
                if (function_exists($moddeleteuserdata)) {
                    $modstatus = $moddeleteuserdata($data);
                    if (is_array($modstatus)) {
                        $status = array_merge($status, $modstatus);
                    } else {
                        debugging('Module ' . $modname . ' returned incorrect staus - must be an array!');
                    }
                } else {
                    $unsupportedmods[] = $mod;
                }
            } else {
                debugging('Missing lib.php in ' . $modname . ' module!');
            }
        }
    }
    // Mention unsupported mods.
    if (!empty($unsupportedmods)) {
        foreach ($unsupportedmods as $mod) {
            $status[] = array('component' => get_string('modulenameplural', $mod->name), 'item' => '', 'error' => get_string('resetnotimplemented'));
        }
    }
    $componentstr = get_string('gradebook', 'grades');
    // Reset gradebook,.
    if (!empty($data->reset_gradebook_items)) {
        remove_course_grades($data->courseid, false);
        grade_grab_course_grades($data->courseid);
        grade_regrade_final_grades($data->courseid);
        $status[] = array('component' => $componentstr, 'item' => get_string('removeallcourseitems', 'grades'), 'error' => false);
    } else {
        if (!empty($data->reset_gradebook_grades)) {
            grade_course_reset($data->courseid);
            $status[] = array('component' => $componentstr, 'item' => get_string('removeallcoursegrades', 'grades'), 'error' => false);
        }
    }
    // Reset comments.
    if (!empty($data->reset_comments)) {
        require_once $CFG->dirroot . '/comment/lib.php';
        comment::reset_course_page_comments($context);
    }
    $event = \core\event\course_reset_ended::create($eventparams);
    $event->trigger();
    return $status;
}
Beispiel #3
0
 /**
  * Reset contents of all database tables to initial values, reset caches, etc.
  *
  * Note: this is relatively slow (cca 2 seconds for pg and 7 for mysql) - please use with care!
  *
  * @static
  * @param bool $detectchanges
  *      true  - changes in global state and database are reported as errors
  *      false - no errors reported
  *      null  - only critical problems are reported as errors
  * @return void
  */
 public static function reset_all_data($detectchanges = false)
 {
     global $DB, $CFG, $USER, $SITE, $COURSE, $PAGE, $OUTPUT, $SESSION;
     // Stop any message redirection.
     phpunit_util::stop_message_redirection();
     // Stop any message redirection.
     phpunit_util::stop_event_redirection();
     // Start a new email redirection.
     // This will clear any existing phpmailer redirection.
     // We redirect all phpmailer output to this message sink which is
     // called instead of phpmailer actually sending the message.
     phpunit_util::start_phpmailer_redirection();
     // We used to call gc_collect_cycles here to ensure desctructors were called between tests.
     // This accounted for 25% of the total time running phpunit - so we removed it.
     // Show any unhandled debugging messages, the runbare() could already reset it.
     self::display_debugging_messages();
     self::reset_debugging();
     // reset global $DB in case somebody mocked it
     $DB = self::get_global_backup('DB');
     if ($DB->is_transaction_started()) {
         // we can not reset inside transaction
         $DB->force_transaction_rollback();
     }
     $resetdb = self::reset_database();
     $warnings = array();
     if ($detectchanges === true) {
         if ($resetdb) {
             $warnings[] = 'Warning: unexpected database modification, resetting DB state';
         }
         $oldcfg = self::get_global_backup('CFG');
         $oldsite = self::get_global_backup('SITE');
         foreach ($CFG as $k => $v) {
             if (!property_exists($oldcfg, $k)) {
                 $warnings[] = 'Warning: unexpected new $CFG->' . $k . ' value';
             } else {
                 if ($oldcfg->{$k} !== $CFG->{$k}) {
                     $warnings[] = 'Warning: unexpected change of $CFG->' . $k . ' value';
                 }
             }
             unset($oldcfg->{$k});
         }
         if ($oldcfg) {
             foreach ($oldcfg as $k => $v) {
                 $warnings[] = 'Warning: unexpected removal of $CFG->' . $k;
             }
         }
         if ($USER->id != 0) {
             $warnings[] = 'Warning: unexpected change of $USER';
         }
         if ($COURSE->id != $oldsite->id) {
             $warnings[] = 'Warning: unexpected change of $COURSE';
         }
         if ($CFG->ostype === 'WINDOWS') {
             if (setlocale(LC_TIME, 0) !== 'English_Australia.1252') {
                 $warnings[] = 'Warning: unexpected change of locale';
             }
         } else {
             if (setlocale(LC_TIME, 0) !== 'en_AU.UTF-8') {
                 $warnings[] = 'Warning: unexpected change of locale';
             }
         }
     }
     if (ini_get('max_execution_time') != 0) {
         // This is special warning for all resets because we do not want any
         // libraries to mess with timeouts unintentionally.
         // Our PHPUnit integration is not supposed to change it either.
         if ($detectchanges !== false) {
             $warnings[] = 'Warning: max_execution_time was changed to ' . ini_get('max_execution_time');
         }
         set_time_limit(0);
     }
     // restore original globals
     $_SERVER = self::get_global_backup('_SERVER');
     $CFG = self::get_global_backup('CFG');
     $SITE = self::get_global_backup('SITE');
     $_GET = array();
     $_POST = array();
     $_FILES = array();
     $_REQUEST = array();
     $COURSE = $SITE;
     // reinitialise following globals
     $OUTPUT = new bootstrap_renderer();
     $PAGE = new moodle_page();
     $FULLME = null;
     $ME = null;
     $SCRIPT = null;
     // Empty sessison and set fresh new not-logged-in user.
     \core\session\manager::init_empty_session();
     // reset all static caches
     \core\event\manager::phpunit_reset();
     accesslib_clear_all_caches(true);
     get_string_manager()->reset_caches(true);
     reset_text_filters_cache(true);
     events_get_handlers('reset');
     core_text::reset_caches();
     get_message_processors(false, true);
     filter_manager::reset_caches();
     core_filetypes::reset_caches();
     // Reset static unit test options.
     if (class_exists('\\availability_date\\condition', false)) {
         \availability_date\condition::set_current_time_for_test(0);
     }
     // Reset internal users.
     core_user::reset_internal_users();
     //TODO MDL-25290: add more resets here and probably refactor them to new core function
     // Reset course and module caches.
     if (class_exists('format_base')) {
         // If file containing class is not loaded, there is no cache there anyway.
         format_base::reset_course_cache(0);
     }
     get_fast_modinfo(0, 0, true);
     // Reset other singletons.
     if (class_exists('core_plugin_manager')) {
         core_plugin_manager::reset_caches(true);
     }
     if (class_exists('\\core\\update\\checker')) {
         \core\update\checker::reset_caches(true);
     }
     if (class_exists('\\core\\update\\deployer')) {
         \core\update\deployer::reset_caches(true);
     }
     // Clear static cache within restore.
     if (class_exists('restore_section_structure_step')) {
         restore_section_structure_step::reset_caches();
     }
     // purge dataroot directory
     self::reset_dataroot();
     // restore original config once more in case resetting of caches changed CFG
     $CFG = self::get_global_backup('CFG');
     // inform data generator
     self::get_data_generator()->reset();
     // fix PHP settings
     error_reporting($CFG->debug);
     // Reset the date/time class.
     core_date::phpunit_reset();
     // Make sure the time locale is consistent - that is Australian English.
     if ($CFG->ostype === 'WINDOWS') {
         setlocale(LC_TIME, 'English_Australia.1252');
     } else {
         setlocale(LC_TIME, 'en_AU.UTF-8');
     }
     // verify db writes just in case something goes wrong in reset
     if (self::$lastdbwrites != $DB->perf_get_writes()) {
         error_log('Unexpected DB writes in phpunit_util::reset_all_data()');
         self::$lastdbwrites = $DB->perf_get_writes();
     }
     if ($warnings) {
         $warnings = implode("\n", $warnings);
         trigger_error($warnings, E_USER_WARNING);
     }
 }
Beispiel #4
0
 /**
  * Tests get_user_list_sql.
  */
 public function test_get_user_list_sql()
 {
     global $DB;
     $this->resetAfterTest();
     $generator = $this->getDataGenerator();
     // Create a test course with 2 groups and users in each combination of them.
     $course = $generator->create_course();
     $group1 = $generator->create_group(array('courseid' => $course->id));
     $group2 = $generator->create_group(array('courseid' => $course->id));
     $userin1 = $generator->create_user();
     $userin2 = $generator->create_user();
     $userinboth = $generator->create_user();
     $userinneither = $generator->create_user();
     $studentroleid = $DB->get_field('role', 'id', array('shortname' => 'student'));
     foreach (array($userin1, $userin2, $userinboth, $userinneither) as $user) {
         $generator->enrol_user($user->id, $course->id, $studentroleid);
     }
     groups_add_member($group1, $userin1);
     groups_add_member($group2, $userin2);
     groups_add_member($group1, $userinboth);
     groups_add_member($group2, $userinboth);
     $info = new \core_availability\mock_info($course);
     // Tree with single group condition.
     $tree = new tree(tree::get_root_json(array(\availability_group\condition::get_json($group1->id))));
     list($sql, $params) = $tree->get_user_list_sql(false, $info, false);
     $result = $DB->get_fieldset_sql($sql, $params);
     sort($result);
     $this->assertEquals(array($userin1->id, $userinboth->id), $result);
     // Tree with 'AND' of both group conditions.
     $tree = new tree(tree::get_root_json(array(\availability_group\condition::get_json($group1->id), \availability_group\condition::get_json($group2->id))));
     list($sql, $params) = $tree->get_user_list_sql(false, $info, false);
     $result = $DB->get_fieldset_sql($sql, $params);
     sort($result);
     $this->assertEquals(array($userinboth->id), $result);
     // Tree with 'AND' of both group conditions.
     $tree = new tree(tree::get_root_json(array(\availability_group\condition::get_json($group1->id), \availability_group\condition::get_json($group2->id)), tree::OP_OR));
     list($sql, $params) = $tree->get_user_list_sql(false, $info, false);
     $result = $DB->get_fieldset_sql($sql, $params);
     sort($result);
     $this->assertEquals(array($userin1->id, $userin2->id, $userinboth->id), $result);
     // Check with flipped logic (NOT above level of tree).
     list($sql, $params) = $tree->get_user_list_sql(true, $info, false);
     $result = $DB->get_fieldset_sql($sql, $params);
     sort($result);
     $this->assertEquals(array($userinneither->id), $result);
     // Tree with 'OR' of group conditions and a non-filtering condition.
     // The non-filtering condition should mean that ALL users are included.
     $tree = new tree(tree::get_root_json(array(\availability_group\condition::get_json($group1->id), \availability_date\condition::get_json(\availability_date\condition::DIRECTION_UNTIL, 3)), tree::OP_OR));
     list($sql, $params) = $tree->get_user_list_sql(false, $info, false);
     $this->assertEquals('', $sql);
     $this->assertEquals(array(), $params);
 }
Beispiel #5
0
 /**
  * Tests the update_all_dates function.
  */
 public function test_update_all_dates()
 {
     global $DB;
     $this->resetAfterTest();
     // Create a course with 3 pages.
     $generator = $this->getDataGenerator();
     $course = $generator->create_course();
     $rec = array('course' => $course);
     $page1 = $generator->get_plugin_generator('mod_page')->create_instance($rec);
     $page2 = $generator->get_plugin_generator('mod_page')->create_instance($rec);
     $page3 = $generator->get_plugin_generator('mod_page')->create_instance($rec);
     // Set the availability page 2 to a simple date condition. You can access
     // it from 1337 onwards.
     $simplecondition = tree::get_root_json(array(condition::get_json(condition::DIRECTION_FROM, 1337)));
     $DB->set_field('course_modules', 'availability', json_encode($simplecondition), array('id' => $page2->cmid));
     // Set page 3 to a complex set of conditions including a nested date condition.
     // You can access it until 1459, *or* after 2810 if you belong to a group.
     $complexcondition = tree::get_root_json(array(condition::get_json(condition::DIRECTION_UNTIL, 1459), tree::get_nested_json(array(condition::get_json(condition::DIRECTION_FROM, 2810), \availability_group\condition::get_json()))), tree::OP_OR);
     $DB->set_field('course_modules', 'availability', json_encode($complexcondition), array('id' => $page3->cmid));
     // Now use the update_all_dates function to move date forward 100000.
     condition::update_all_dates($course->id, 100000);
     // Get the expected conditions after adjusting time, and compare to database.
     $simplecondition->c[0]->t = 101337;
     $complexcondition->c[0]->t = 101459;
     $complexcondition->c[1]->c[0]->t = 102810;
     $this->assertEquals($simplecondition, json_decode($DB->get_field('course_modules', 'availability', array('id' => $page2->cmid))));
     $this->assertEquals($complexcondition, json_decode($DB->get_field('course_modules', 'availability', array('id' => $page3->cmid))));
     // The one without availability conditions should still be null.
     $this->assertNull($DB->get_field('course_modules', 'availability', array('id' => $page1->cmid)));
 }
 /**
  * Get date condition for module availability.
  * @param $time
  * @param string $comparator
  * @return string
  * @throws \coding_exception
  */
 protected function get_date_condition_json($time, $comparator = '>=')
 {
     return json_encode(\core_availability\tree::get_root_json([\availability_date\condition::get_json($comparator, $time)]));
 }
Beispiel #7
0
 /**
  * Tests the get_description and get_standalone_description functions.
  */
 public function test_get_description()
 {
     global $SITE, $CFG;
     $this->resetAfterTest();
     $this->setTimezone('UTC');
     $modinfo = get_fast_modinfo($SITE);
     $info = new \core_availability\mock_info();
     $time = strtotime('2014-02-18 14:55:01 GMT');
     // Test with >=.
     $date = new condition((object) array('d' => '>=', 't' => $time));
     $information = $date->get_description(true, false, $info);
     $this->assertRegExp('~after.*18 February 2014, 2:55 (PM|pm)~', $information);
     $information = $date->get_description(true, true, $info);
     $this->assertRegExp('~before.*18 February 2014, 2:55 (PM|pm)~', $information);
     $information = $date->get_standalone_description(true, false, $info);
     $this->assertRegExp('~from.*18 February 2014, 2:55 (PM|pm)~', $information);
     $information = $date->get_standalone_description(true, true, $info);
     $this->assertRegExp('~until.*18 February 2014, 2:55 (PM|pm)~', $information);
     // Test with <.
     $date = new condition((object) array('d' => '<', 't' => $time));
     $information = $date->get_description(true, false, $info);
     $this->assertRegExp('~before.*18 February 2014, 2:55 (PM|pm)~', $information);
     $information = $date->get_description(true, true, $info);
     $this->assertRegExp('~after.*18 February 2014, 2:55 (PM|pm)~', $information);
     $information = $date->get_standalone_description(true, false, $info);
     $this->assertRegExp('~until.*18 February 2014, 2:55 (PM|pm)~', $information);
     $information = $date->get_standalone_description(true, true, $info);
     $this->assertRegExp('~from.*18 February 2014, 2:55 (PM|pm)~', $information);
     // Test special case for dates that are midnight.
     $date = new condition((object) array('d' => '>=', 't' => strtotime('2014-03-05 00:00 GMT')));
     $information = $date->get_description(true, false, $info);
     $this->assertRegExp('~on or after.*5 March 2014([^0-9]*)$~', $information);
     $information = $date->get_description(true, true, $info);
     $this->assertRegExp('~before.*end of.*4 March 2014([^0-9]*)$~', $information);
     $information = $date->get_standalone_description(true, false, $info);
     $this->assertRegExp('~from.*5 March 2014([^0-9]*)$~', $information);
     $information = $date->get_standalone_description(true, true, $info);
     $this->assertRegExp('~until end of.*4 March 2014([^0-9]*)$~', $information);
     // In the 'until' case for midnight, it shows the previous day. (I.e.
     // if the date is 5 March 00:00, then we show it as available until 4
     // March, implying 'the end of'.)
     $date = new condition((object) array('d' => '<', 't' => strtotime('2014-03-05 00:00 GMT')));
     $information = $date->get_description(true, false, $info);
     $this->assertRegExp('~before end of.*4 March 2014([^0-9]*)$~', $information);
     $information = $date->get_description(true, true, $info);
     $this->assertRegExp('~on or after.*5 March 2014([^0-9]*)$~', $information);
     $information = $date->get_standalone_description(true, false, $info);
     $this->assertRegExp('~until end of.*4 March 2014([^0-9]*)$~', $information);
     $information = $date->get_standalone_description(true, true, $info);
     $this->assertRegExp('~from.*5 March 2014([^0-9]*)$~', $information);
 }