/** * Standard procedure to add strings into AMOS repository */ public function test_simple_string_lifecycle() { global $DB, $CFG, $USER; // basic operations $component = new mlang_component('test', 'xx', mlang_version::by_branch('MOODLE_20_STABLE')); $this->assertFalse($component->has_string()); $this->assertFalse($component->has_string('nonexisting')); $this->assertFalse($component->has_string('welcome')); $component->add_string(new mlang_string('welcome', 'Welcome')); $this->assertTrue($component->has_string()); $this->assertFalse($component->has_string('nonexisting')); $this->assertTrue($component->has_string('welcome')); $this->assertNull($component->get_string('nonexisting')); $s = $component->get_string('welcome'); $this->assertTrue($s instanceof mlang_string); $this->assertEqual('welcome', $s->id); $this->assertEqual('Welcome', $s->text); $component->unlink_string('nonexisting'); $this->assertTrue($component->has_string()); $component->unlink_string('welcome'); $this->assertFalse($component->has_string()); $component->add_string(new mlang_string('welcome', 'Welcome')); $component->clear(); unset($component); // commit a single string $now = time(); $component = new mlang_component('test', 'xx', mlang_version::by_branch('MOODLE_20_STABLE')); $component->add_string(new mlang_string('welcome', 'Welcome', $now)); $stage = new mlang_stage(); $stage->add($component); $stage->commit('First string in AMOS', array('source' => 'unittest'), true); $component->clear(); unset($component); unset($stage); $this->assertEqual(1, $DB->count_records('amos_repository')); $this->assertTrue($DB->record_exists('amos_repository', array('component' => 'test', 'lang' => 'xx', 'stringid' => 'welcome', 'timemodified' => $now))); // add two other strings $now = time(); $stage = new mlang_stage(); $component = new mlang_component('test', 'xx', mlang_version::by_branch('MOODLE_20_STABLE')); $component->add_string(new mlang_string('hello', 'Hello', $now)); $component->add_string(new mlang_string('world', 'World', $now)); $stage->add($component); $stage->commit('Two other string into AMOS', array('source' => 'unittest'), true); $component->clear(); unset($component); unset($stage); $this->assertEqual(3, $DB->count_records('amos_repository')); $this->assertTrue($DB->record_exists('amos_repository', array('component' => 'test', 'lang' => 'xx', 'stringid' => 'hello', 'timemodified' => $now))); $this->assertTrue($DB->record_exists('amos_repository', array('component' => 'test', 'lang' => 'xx', 'stringid' => 'world', 'timemodified' => $now))); // delete a string $now = time(); $stage = new mlang_stage(); $component = new mlang_component('test', 'xx', mlang_version::by_branch('MOODLE_20_STABLE')); $component->add_string(new mlang_string('welcome', '', $now, true)); $stage->add($component); $stage->commit('Marking string as deleted', array('source' => 'unittest'), true); $component->clear(); unset($component); unset($stage); $this->assertEqual(4, $DB->count_records('amos_repository')); $this->assertTrue($DB->record_exists('amos_repository', array('component' => 'test', 'lang' => 'xx', 'stringid' => 'welcome', 'timemodified' => $now, 'deleted' => 1))); }
/** * Get a snapshot of all strings in the given component * * @param string $name * @param string $lang * @param mlang_version $version * @param int $timestamp time of the snapshot, empty for the most recent * @param bool $deleted shall deleted strings be included? * @param bool $fullinfo shall full information about the string (commit messages, source etc) be returned? * @return mlang_component component with the strings from the snapshot */ public static function from_snapshot($name, $lang, mlang_version $version, $timestamp = null, $deleted = false, $fullinfo = false, array $stringids = null) { global $DB; $params = array('inner_branch' => $version->code, 'inner_lang' => $lang, 'inner_component' => $name, 'outer_branch' => $version->code, 'outer_lang' => $lang, 'outer_component' => $name); if (!empty($stringids)) { list($inner_strsql, $inner_strparams) = $DB->get_in_or_equal($stringids, SQL_PARAMS_NAMED, 'innerstringid000000'); list($outer_strsql, $outer_strparams) = $DB->get_in_or_equal($stringids, SQL_PARAMS_NAMED, 'outerstringid000000'); $params = array_merge($params, $inner_strparams, $outer_strparams); } if ($fullinfo) { $sql = "SELECT r.id, r.commitid, r.branch, r.lang, r.component, r.stringid, r.text, r.timemodified, r.deleted,\n c.source, c.timecommitted, c.commitmsg, c.commithash, c.userid, c.userinfo"; } else { $sql = "SELECT r.stringid, r.text, r.timemodified, r.deleted"; } $sql .= " FROM {amos_repository} r\n JOIN (SELECT branch, lang, component, stringid, MAX(timemodified) AS timemodified\n FROM {amos_repository}\n WHERE branch=:inner_branch\n AND lang=:inner_lang\n AND component=:inner_component"; if (!empty($stringids)) { $sql .= " AND stringid {$inner_strsql}"; } if (!empty($timestamp)) { $sql .= " AND timemodified <= :timemodified"; $params = array_merge($params, array('timemodified' => $timestamp)); } $sql .= " GROUP BY branch,lang,component,stringid) j\n ON (r.branch = j.branch\n AND r.lang = j.lang\n AND r.component = j.component\n AND r.stringid = j.stringid\n AND r.timemodified = j.timemodified)"; if ($fullinfo) { $sql .= "\n LEFT JOIN {amos_commits} c\n ON (r.commitid = c.id)"; } $sql .= " WHERE r.branch=:outer_branch\n AND r.lang=:outer_lang\n AND r.component=:outer_component"; if (!empty($stringids)) { $sql .= " AND r.stringid {$outer_strsql}"; } $sql .= " ORDER BY r.stringid, r.id"; $rs = $DB->get_recordset_sql($sql, $params); $component = new mlang_component($name, $lang, $version); foreach ($rs as $r) { if (empty($deleted) and $r->deleted) { // we do not want to include deleted strings - note that this must be checked here and not // in SQL above so that the same string can be deleted and re-added again $component->unlink_string($r->stringid); // this is needed because there can be two strings with // the same timemodified, one deleted, one not continue; } if ($fullinfo) { $extra = new stdclass(); foreach ($r as $property => $value) { if (!in_array($property, array('stringid', 'text', 'timemodified', 'deleted'))) { $extra->{$property} = $value; } } } else { $extra = null; } // we force here so in case of two string with the same timemodified, the higher id wins $component->add_string(new mlang_string($r->stringid, $r->text, $r->timemodified, $r->deleted, $extra), true); } $rs->close(); return $component; }