/** * Creates the specified element. More info about available elements in http://docs.moodle.org/dev/Acceptance_testing#Fixtures. * * @Given /^the following lp "(?P<element_string>(?:[^"]|\\")*)" exist:$/ * * @throws Exception * @throws PendingException * @param string $elementname The name of the entity to add * @param TableNode $data */ public function the_following_lp_exist($elementname, TableNode $data) { // Now that we need them require the data generators. require_once __DIR__ . '/../../../../../lib/phpunit/classes/util.php'; if (empty(self::$elements[$elementname])) { throw new PendingException($elementname . ' data generator is not implemented'); } $datagenerator = testing_util::get_data_generator(); $this->datageneratorlp = $datagenerator->get_plugin_generator('core_competency'); $elementdatagenerator = self::$elements[$elementname]['datagenerator']; $requiredfields = self::$elements[$elementname]['required']; if (!empty(self::$elements[$elementname]['switchids'])) { $switchids = self::$elements[$elementname]['switchids']; } foreach ($data->getHash() as $elementdata) { // Check if all the required fields are there. foreach ($requiredfields as $requiredfield) { if (!isset($elementdata[$requiredfield])) { throw new Exception($elementname . ' requires the field ' . $requiredfield . ' to be specified'); } } // Switch from human-friendly references to ids. if (isset($switchids)) { foreach ($switchids as $element => $field) { $methodname = 'get_' . $element . '_id'; // Not all the switch fields are required, default vars will be assigned by data generators. if (isset($elementdata[$element])) { // Temp $id var to avoid problems when $element == $field. $id = $this->{$methodname}($elementdata[$element]); unset($elementdata[$element]); $elementdata[$field] = $id; } } } // Preprocess the entities that requires a special treatment. if (method_exists($this, 'preprocess_' . $elementdatagenerator)) { $elementdata = $this->{'preprocess_' . $elementdatagenerator}($elementdata); } // Creates element. $methodname = 'create_' . $elementdatagenerator; if (method_exists($this->datageneratorlp, $methodname)) { // Using data generators directly. $this->datageneratorlp->{$methodname}($elementdata); } else { if (method_exists($this, 'process_' . $elementdatagenerator)) { // Using an alternative to the direct data generator call. $this->{'process_' . $elementdatagenerator}($elementdata); } else { throw new PendingException($elementname . ' data generator is not implemented'); } } } }
/** * Add a label to a course that is a link to a file in the fixtures directory of this plugin. * * @Given /^course "(?P<COURSE_SHORTNAME>[^"]*)" has a label linking to Jmol fixture "(?P<FILE_NAME>[^"]*)"$/ */ public function course_contains_label_linking_fixture($shortname, $filename) { global $CFG, $DB; $courseid = $DB->get_field('course', 'id', array('shortname' => $shortname)); if (!$courseid) { throw new Exception('The specified course with shortname "' . $shortname . '" does not exist.'); } if (!is_readable($CFG->dirroot . '/filter/jmol/tests/fixtures/' . $filename)) { throw new Exception('The fixture file "' . $filename . '" does not exist.'); } $link = html_writer::link(new moodle_url('/filter/jmol/tests/fixtures/' . $filename), "Molecule"); $generator = testing_util::get_data_generator(); $generator->create_module('label', array('course' => $courseid, 'intro' => $link)); }
/** * This should be called before each db query. * @param string $sql The query string. * @param array $params An array of parameters. * @param int $type The type of query. ( SQL_QUERY_SELECT | SQL_QUERY_AUX | SQL_QUERY_INSERT | SQL_QUERY_UPDATE | SQL_QUERY_STRUCTURE ) * @param mixed $extrainfo This is here for any driver specific extra information. * @return void */ protected function query_start($sql, array $params = null, $type, $extrainfo = null) { if ($this->loggingquery) { return; } $this->last_sql = $sql; $this->last_params = $params; $this->last_type = $type; $this->last_extrainfo = $extrainfo; $this->last_time = microtime(true); switch ($type) { case SQL_QUERY_SELECT: case SQL_QUERY_AUX: $this->reads++; break; case SQL_QUERY_INSERT: case SQL_QUERY_UPDATE: case SQL_QUERY_STRUCTURE: $this->writes++; default: if (PHPUNIT_TEST || defined('BEHAT_TEST') && BEHAT_TEST || defined('BEHAT_SITE_RUNNING')) { // Set list of tables that are updated. require_once __DIR__ . '/../testing/classes/util.php'; testing_util::set_table_modified_by_sql($sql); } } $this->print_debug($sql, $params); }
/** * Reset all database tables to default values. * @static * @return bool true if reset done, false if skipped */ public static function reset_database() { global $DB; if (!is_null(self::$lastdbwrites) and self::$lastdbwrites == $DB->perf_get_writes()) { return false; } if (!parent::reset_database()) { return false; } self::$lastdbwrites = $DB->perf_get_writes(); return true; }
/** * Calculate unique version hash for all plugins and core. * @static * @return string sha1 hash */ public static function get_version_hash() { global $CFG; if (self::$versionhash) { return self::$versionhash; } $versions = array(); // main version first $version = null; include($CFG->dirroot.'/version.php'); $versions['core'] = $version; // modules $mods = get_plugin_list('mod'); ksort($mods); foreach ($mods as $mod => $fullmod) { $module = new stdClass(); $module->version = null; include($fullmod.'/version.php'); $versions[$mod] = $module->version; } // now the rest of plugins $plugintypes = get_plugin_types(); unset($plugintypes['mod']); ksort($plugintypes); foreach ($plugintypes as $type => $unused) { $plugs = get_plugin_list($type); ksort($plugs); foreach ($plugs as $plug => $fullplug) { $plugin = new stdClass(); $plugin->version = null; @include($fullplug.'/version.php'); $versions[$plug] = $plugin->version; } } self::$versionhash = sha1(serialize($versions)); return self::$versionhash; }
/** * Skip the original dataroot files to not been reset. * * @static * @param string $utilclassname the util class name.. */ protected static function skip_original_data_files($utilclassname) { $jsonfilepath = self::get_dataroot() . '/' . self::$originaldatafilesjson; if (file_exists($jsonfilepath)) { $listfiles = file_get_contents($jsonfilepath); // Mark each files as to not be reset. if (!empty($listfiles) && !self::$originaldatafilesjsonadded) { $originaldatarootfiles = json_decode($listfiles); // Keep the json file. Only drop_dataroot() should delete it. $originaldatarootfiles[] = self::$originaldatafilesjson; $utilclassname::$datarootskiponreset = array_merge($utilclassname::$datarootskiponreset, $originaldatarootfiles); self::$originaldatafilesjsonadded = true; } } }
/** * Reset all database tables to default values. * @static * @return bool true if reset done, false if skipped */ public static function reset_database() { global $DB; if (!is_null(self::$lastdbwrites) and self::$lastdbwrites == $DB->perf_get_writes()) { return false; } $tables = $DB->get_tables(false); if (!$tables or empty($tables['config'])) { // not installed yet return false; } if (!($data = self::get_tabledata())) { // not initialised yet return false; } if (!($structure = self::get_tablestructure())) { // not initialised yet return false; } $empties = self::guess_unmodified_empty_tables(); foreach ($data as $table => $records) { if (empty($records)) { if (isset($empties[$table])) { // table was not modified and is empty } else { $DB->delete_records($table, array()); } continue; } if (isset($structure[$table]['id']) and $structure[$table]['id']->auto_increment) { $currentrecords = $DB->get_records($table, array(), 'id ASC'); $changed = false; foreach ($records as $id => $record) { if (!isset($currentrecords[$id])) { $changed = true; break; } if ((array) $record != (array) $currentrecords[$id]) { $changed = true; break; } unset($currentrecords[$id]); } if (!$changed) { if ($currentrecords) { $lastrecord = end($records); $DB->delete_records_select($table, "id > ?", array($lastrecord->id)); continue; } else { continue; } } } $DB->delete_records($table, array()); foreach ($records as $record) { $DB->import_record($table, $record, false, true); } } // reset all next record ids - aka sequences self::reset_all_database_sequences($empties); // remove extra tables foreach ($tables as $table) { if (!isset($data[$table])) { $DB->get_manager()->drop_table(new xmldb_table($table)); } } self::$lastdbwrites = $DB->perf_get_writes(); return true; }