Ejemplo n.º 1
0
 protected function define_execution() {
     $progress = $this->task->get_progress();
     $progress->start_progress('Deleting backup directories');
     backup_helper::check_and_create_backup_dir($this->get_backupid());// Create backup temp dir
     backup_helper::clear_backup_dir($this->get_backupid(), $progress);           // Empty temp dir, just in case
     backup_controller_dbops::drop_backup_ids_temp_table($this->get_backupid()); // Drop ids temp table
     backup_controller_dbops::create_backup_ids_temp_table($this->get_backupid()); // Create ids temp table
     $progress->end_progress();
 }
Ejemplo n.º 2
0
 protected function define_execution()
 {
     backup_helper::check_and_create_backup_dir($this->get_backupid());
     // Create backup temp dir
     backup_helper::clear_backup_dir($this->get_backupid());
     // Empty temp dir, just in case
     backup_helper::delete_old_backup_dirs(time() - 4 * 60 * 60);
     // Delete > 4 hours temp dirs
     backup_controller_dbops::create_backup_ids_temp_table($this->get_backupid());
     // Create ids temp table
 }
Ejemplo n.º 3
0
 /**
  * Creates the temporary storage for stashed data
  *
  * This implementation uses backup_ids_temp table.
  */
 public function create_stash_storage() {
     backup_controller_dbops::create_backup_ids_temp_table($this->get_id());
 }
Ejemplo n.º 4
0
 function test_backup_controller_dbops()
 {
     global $DB;
     $dbman = $DB->get_manager();
     // Going to use some database_manager services for testing
     // Instantiate non interactive backup_controller
     $bc = new mock_backup_controller4dbops(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
     $this->assertTrue($bc instanceof backup_controller);
     // Calculate checksum
     $checksum = $bc->calculate_checksum();
     $this->assertEqual(strlen($checksum), 32);
     // is one md5
     // save controller
     $recid = backup_controller_dbops::save_controller($bc, $checksum);
     $this->assertTrue($recid);
     $this->todelete[] = array('backup_controllers', $recid);
     // mark this record for deletion
     // save it again (should cause update to happen)
     $recid2 = backup_controller_dbops::save_controller($bc, $checksum);
     $this->assertTrue($recid2);
     $this->todelete[] = array('backup_controllers', $recid2);
     // mark this record for deletion
     $this->assertEqual($recid, $recid2);
     // Same record in both save operations
     // Try incorrect checksum
     $bc = new mock_backup_controller4dbops(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
     $checksum = $bc->calculate_checksum();
     try {
         $recid = backup_controller_dbops::save_controller($bc, 'lalala');
         $this->assertTrue(false, 'backup_dbops_exception expected');
     } catch (exception $e) {
         $this->assertTrue($e instanceof backup_dbops_exception);
         $this->assertEqual($e->errorcode, 'backup_controller_dbops_saving_checksum_mismatch');
     }
     // Try to save non backup_controller object
     $bc = new stdclass();
     try {
         $recid = backup_controller_dbops::save_controller($bc, 'lalala');
         $this->assertTrue(false, 'backup_controller_exception expected');
     } catch (exception $e) {
         $this->assertTrue($e instanceof backup_controller_exception);
         $this->assertEqual($e->errorcode, 'backup_controller_expected');
     }
     // save and load controller (by backupid). Then compare
     $bc = new mock_backup_controller4dbops(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
     $checksum = $bc->calculate_checksum();
     // Calculate checksum
     $backupid = $bc->get_backupid();
     $this->assertEqual(strlen($backupid), 32);
     // is one md5
     $recid = backup_controller_dbops::save_controller($bc, $checksum);
     // save controller
     $this->todelete[] = array('backup_controllers', $recid);
     // mark this record for deletion
     $newbc = backup_controller_dbops::load_controller($backupid);
     // load controller
     $this->assertTrue($newbc instanceof backup_controller);
     $newchecksum = $newbc->calculate_checksum();
     $this->assertEqual($newchecksum, $checksum);
     // try to load non-existing controller
     try {
         $bc = backup_controller_dbops::load_controller('1234567890');
         $this->assertTrue(false, 'backup_dbops_exception expected');
     } catch (exception $e) {
         $this->assertTrue($e instanceof backup_dbops_exception);
         $this->assertEqual($e->errorcode, 'backup_controller_dbops_nonexisting');
     }
     // backup_ids_temp table tests
     // If, for any reason table exists, drop it
     if ($dbman->table_exists('backup_ids_temp')) {
         $dbman->drop_temp_table(new xmldb_table('backup_ids_temp'));
     }
     // Check backup_ids_temp table doesn't exist
     $this->assertFalse($dbman->table_exists('backup_ids_temp'));
     // Create and check it exists
     backup_controller_dbops::create_backup_ids_temp_table('testingid');
     $this->assertTrue($dbman->table_exists('backup_ids_temp'));
     // Drop and check it doesn't exists anymore
     backup_controller_dbops::drop_backup_ids_temp_table('testingid');
     $this->assertFalse($dbman->table_exists('backup_ids_temp'));
 }
 public static function create_restore_temp_tables($restoreid)
 {
     global $CFG, $DB;
     $dbman = $DB->get_manager();
     // We are going to use database_manager services
     if ($dbman->table_exists('backup_ids_temp')) {
         // Table exists, from restore prechecks
         // TODO: Improve this by inserting/selecting some record to see there is restoreid match
         // TODO: If not match, exception, table corresponds to another backup/restore operation
         return true;
     }
     backup_controller_dbops::create_backup_ids_temp_table($restoreid);
     backup_controller_dbops::create_backup_files_temp_table($restoreid);
     return false;
 }
Ejemplo n.º 6
0
 /**
  * Backup structures tests (construction, definition and execution)
  */
 function test_backup_structure_construct()
 {
     global $DB;
     $backupid = 'Testing Backup ID';
     // Official backupid for these tests
     // Create all the elements that will conform the tree
     $forum = new backup_nested_element('forum', array('id'), array('type', 'name', 'intro', 'introformat', 'assessed', 'assesstimestart', 'assesstimefinish', 'scale', 'maxbytes', 'maxattachments', 'forcesubscribe', 'trackingtype', 'rsstype', 'rssarticles', 'timemodified', 'warnafter', 'blockafter', new backup_final_element('blockperiod'), new mock_skip_final_element('completiondiscussions'), new mock_modify_final_element('completionreplies'), new mock_final_element_interceptor('completionposts')));
     $discussions = new backup_nested_element('discussions');
     $discussion = new backup_nested_element('discussion', array('id'), array('forum', 'name', 'firstpost', 'userid', 'groupid', 'assessed', 'timemodified', 'usermodified', 'timestart', 'timeend'));
     $posts = new backup_nested_element('posts');
     $post = new backup_nested_element('post', array('id'), array('discussion', 'parent', 'userid', 'created', 'modified', 'mailed', 'subject', 'message', 'messageformat', 'messagetrust', 'attachment', 'totalscore', 'mailnow'));
     $ratings = new backup_nested_element('ratings');
     $rating = new backup_nested_element('rating', array('id'), array('userid', 'itemid', 'time', 'post_rating'));
     $reads = new backup_nested_element('readposts');
     $read = new backup_nested_element('read', array('id'), array('userid', 'discussionid', 'postid', 'firstread', 'lastread'));
     $inventeds = new backup_nested_element('invented_elements', array('reason', 'version'));
     $invented = new backup_nested_element('invented', null, array('one', 'two', 'three'));
     $one = $invented->get_final_element('one');
     $one->add_attributes(array('attr1', 'attr2'));
     // Build the tree
     $forum->add_child($discussions);
     $discussions->add_child($discussion);
     $discussion->add_child($posts);
     $posts->add_child($post);
     $post->add_child($ratings);
     $ratings->add_child($rating);
     $forum->add_child($reads);
     $reads->add_child($read);
     $forum->add_child($inventeds);
     $inventeds->add_child($invented);
     // Let's add 1 optigroup with 4 elements
     $alternative1 = new backup_optigroup_element('alternative1', array('name', 'value'), '../../id', 1);
     $alternative2 = new backup_optigroup_element('alternative2', array('name', 'value'), backup::VAR_PARENTID, 2);
     $alternative3 = new backup_optigroup_element('alternative3', array('name', 'value'), '/forum/discussions/discussion/posts/post/id', 3);
     $alternative4 = new backup_optigroup_element('alternative4', array('forumtype', 'forumname'));
     // Alternative without conditions
     // Create the optigroup, adding one element
     $optigroup = new backup_optigroup('alternatives', $alternative1, false);
     // Add second opti element
     $optigroup->add_child($alternative2);
     // Add optigroup to post element
     $post->add_optigroup($optigroup);
     // Add third opti element, on purpose after the add_optigroup() line above to check param evaluation works ok
     $optigroup->add_child($alternative3);
     // Add 4th opti element (the one without conditions, so will be present always)
     $optigroup->add_child($alternative4);
     /// Create some new nested elements, both named 'dupetest1', and add them to alternative1 and alternative2
     /// (not problem as far as the optigroup in not unique)
     $dupetest1 = new backup_nested_element('dupetest1', null, array('field1', 'field2'));
     $dupetest2 = new backup_nested_element('dupetest2', null, array('field1', 'field2'));
     $dupetest3 = new backup_nested_element('dupetest3', null, array('field1', 'field2'));
     $dupetest4 = new backup_nested_element('dupetest1', null, array('field1', 'field2'));
     $dupetest1->add_child($dupetest3);
     $dupetest2->add_child($dupetest4);
     $alternative1->add_child($dupetest1);
     $alternative2->add_child($dupetest2);
     // Define sources
     $forum->set_source_table('forum', array('id' => backup::VAR_ACTIVITYID));
     $discussion->set_source_sql('SELECT *
                                    FROM {forum_discussions}
                                   WHERE forum = ?', array('/forum/id'));
     $post->set_source_table('forum_posts', array('discussion' => '/forum/discussions/discussion/id'));
     $rating->set_source_sql('SELECT *
                                FROM {rating}
                               WHERE itemid = ?', array(backup::VAR_PARENTID));
     $read->set_source_table('forum_read', array('id' => '../../id'));
     $inventeds->set_source_array(array((object) array('reason' => 'I love Moodle', 'version' => '1.0'), (object) array('reason' => 'I love Moodle', 'version' => '2.0')));
     // 2 object array
     $invented->set_source_array(array((object) array('one' => 1, 'two' => 2, 'three' => 3), (object) array('one' => 11, 'two' => 22, 'three' => 33)));
     // 2 object array
     // Set optigroup_element sources
     $alternative1->set_source_array(array((object) array('name' => 'alternative1', 'value' => 1)));
     // 1 object array
     // Skip alternative2 source definition on purpose (will be tested)
     // $alternative2->set_source_array(array((object)array('name' => 'alternative2', 'value' => 2))); // 1 object array
     $alternative3->set_source_array(array((object) array('name' => 'alternative3', 'value' => 3)));
     // 1 object array
     // Alternative 4 source is the forum type and name, so we'll get that in ALL posts (no conditions) that
     // have not another alternative (post4 in our testing data in the only not matching any other alternative)
     $alternative4->set_source_sql('SELECT type AS forumtype, name AS forumname
                                      FROM {forum}
                                     WHERE id = ?', array('/forum/id'));
     // Set children of optigroup_element source
     $dupetest1->set_source_array(array((object) array('field1' => '1', 'field2' => 1)));
     // 1 object array
     $dupetest2->set_source_array(array((object) array('field1' => '2', 'field2' => 2)));
     // 1 object array
     $dupetest3->set_source_array(array((object) array('field1' => '3', 'field2' => 3)));
     // 1 object array
     $dupetest4->set_source_array(array((object) array('field1' => '4', 'field2' => 4)));
     // 1 object array
     // Define some aliases
     $rating->set_source_alias('rating', 'post_rating');
     // Map the 'rating' value from DB to 'post_rating' final element
     // Mark to detect files of type 'forum_intro' in forum (and not item id)
     $forum->annotate_files('mod_forum', 'intro', null);
     // Mark to detect file of type 'forum_post' and 'forum_attachment' in post (with itemid being post->id)
     $post->annotate_files('mod_forum', 'post', 'id');
     $post->annotate_files('mod_forum', 'attachment', 'id');
     // Mark various elements to be annotated
     $discussion->annotate_ids('user1', 'userid');
     $post->annotate_ids('forum_post', 'id');
     $rating->annotate_ids('user2', 'userid');
     $rating->annotate_ids('forum_post', 'itemid');
     // Create the backup_ids_temp table
     backup_controller_dbops::create_backup_ids_temp_table($backupid);
     // Instantiate in memory xml output
     $xo = new memory_xml_output();
     // Instantiate xml_writer and start it
     $xw = new xml_writer($xo);
     $xw->start();
     // Instantiate the backup processor
     $processor = new backup_structure_processor($xw);
     // Set some variables
     $processor->set_var(backup::VAR_ACTIVITYID, $this->forumid);
     $processor->set_var(backup::VAR_BACKUPID, $backupid);
     $processor->set_var(backup::VAR_CONTEXTID, $this->contextid);
     // Process the backup structure with the backup processor
     $forum->process($processor);
     // Stop the xml_writer
     $xw->stop();
     // Check various counters
     $this->assertEquals($forum->get_counter(), $DB->count_records('forum'));
     $this->assertEquals($discussion->get_counter(), $DB->count_records('forum_discussions'));
     $this->assertEquals($rating->get_counter(), $DB->count_records('rating'));
     $this->assertEquals($read->get_counter(), $DB->count_records('forum_read'));
     $this->assertEquals($inventeds->get_counter(), 2);
     // Array
     // Perform some validations with the generated XML
     $dom = new DomDocument();
     $dom->loadXML($xo->get_allcontents());
     $xpath = new DOMXPath($dom);
     // Some more counters
     $query = '/forum/discussions/discussion/posts/post';
     $posts = $xpath->query($query);
     $this->assertEquals($posts->length, $DB->count_records('forum_posts'));
     $query = '/forum/invented_elements/invented';
     $inventeds = $xpath->query($query);
     $this->assertEquals($inventeds->length, 2 * 2);
     // Check ratings information against DB
     $ratings = $dom->getElementsByTagName('rating');
     $this->assertEquals($ratings->length, $DB->count_records('rating'));
     foreach ($ratings as $rating) {
         $ratarr = array();
         $ratarr['id'] = $rating->getAttribute('id');
         foreach ($rating->childNodes as $node) {
             if ($node->nodeType != XML_TEXT_NODE) {
                 $ratarr[$node->nodeName] = $node->nodeValue;
             }
         }
         $this->assertEquals($ratarr['userid'], $DB->get_field('rating', 'userid', array('id' => $ratarr['id'])));
         $this->assertEquals($ratarr['itemid'], $DB->get_field('rating', 'itemid', array('id' => $ratarr['id'])));
         $this->assertEquals($ratarr['post_rating'], $DB->get_field('rating', 'rating', array('id' => $ratarr['id'])));
     }
     // Check forum has "blockeperiod" with value 0 (was declared by object instead of name)
     $query = '/forum[blockperiod="0"]';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 1);
     // Check forum is missing "completiondiscussions" (as we are using mock_skip_final_element)
     $query = '/forum/completiondiscussions';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 0);
     // Check forum has "completionreplies" with value "original was 0, now changed" (because of mock_modify_final_element)
     $query = '/forum[completionreplies="original was 0, now changed"]';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 1);
     // Check forum has "completionposts" with value "intercepted!" (because of mock_final_element_interceptor)
     $query = '/forum[completionposts="intercepted!"]';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 1);
     // Check there isn't any alternative2 tag, as far as it hasn't source defined
     $query = '//alternative2';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 0);
     // Check there are 4 "field1" elements
     $query = '/forum/discussions/discussion/posts/post//field1';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 4);
     // Check first post has one name element with value "alternative1"
     $query = '/forum/discussions/discussion/posts/post[@id="1"][name="alternative1"]';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 1);
     // Check there are two "dupetest1" elements
     $query = '/forum/discussions/discussion/posts/post//dupetest1';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 2);
     // Check second post has one name element with value "dupetest2"
     $query = '/forum/discussions/discussion/posts/post[@id="2"]/dupetest2';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 1);
     // Check element "dupetest2" of second post has one field1 element with value "2"
     $query = '/forum/discussions/discussion/posts/post[@id="2"]/dupetest2[field1="2"]';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 1);
     // Check forth post has no name element
     $query = '/forum/discussions/discussion/posts/post[@id="4"]/name';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 0);
     // Check 1st, 2nd and 3rd posts have no forumtype element
     $query = '/forum/discussions/discussion/posts/post[@id="1"]/forumtype';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 0);
     $query = '/forum/discussions/discussion/posts/post[@id="2"]/forumtype';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 0);
     $query = '/forum/discussions/discussion/posts/post[@id="3"]/forumtype';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 0);
     // Check 4th post has one forumtype element with value "general"
     // (because it doesn't matches alternatives 1, 2, 3, then alternative 4,
     // the one without conditions is being applied)
     $query = '/forum/discussions/discussion/posts/post[@id="4"][forumtype="general"]';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 1);
     // Check annotations information against DB
     // Count records in original tables
     $c_postsid = $DB->count_records_sql('SELECT COUNT(DISTINCT id) FROM {forum_posts}');
     $c_dissuserid = $DB->count_records_sql('SELECT COUNT(DISTINCT userid) FROM {forum_discussions}');
     $c_ratuserid = $DB->count_records_sql('SELECT COUNT(DISTINCT userid) FROM {rating}');
     // Count records in backup_ids_table
     $f_forumpost = $DB->count_records('backup_ids_temp', array('backupid' => $backupid, 'itemname' => 'forum_post'));
     $f_user1 = $DB->count_records('backup_ids_temp', array('backupid' => $backupid, 'itemname' => 'user1'));
     $f_user2 = $DB->count_records('backup_ids_temp', array('backupid' => $backupid, 'itemname' => 'user2'));
     $c_notbackupid = $DB->count_records_select('backup_ids_temp', 'backupid != ?', array($backupid));
     // Peform tests by comparing counts
     $this->assertEquals($c_notbackupid, 0);
     // there isn't any record with incorrect backupid
     $this->assertEquals($c_postsid, $f_forumpost);
     // All posts have been registered
     $this->assertEquals($c_dissuserid, $f_user1);
     // All users coming from discussions have been registered
     $this->assertEquals($c_ratuserid, $f_user2);
     // All users coming from ratings have been registered
     // Check file annotations against DB
     $fannotations = $DB->get_records('backup_ids_temp', array('backupid' => $backupid, 'itemname' => 'file'));
     $ffiles = $DB->get_records('files', array('contextid' => $this->contextid));
     $this->assertEquals(count($fannotations), count($ffiles));
     // Same number of recs in both (all files have been annotated)
     foreach ($fannotations as $annotation) {
         // Check ids annotated
         $this->assertTrue($DB->record_exists('files', array('id' => $annotation->itemid)));
     }
     // Drop the backup_ids_temp table
     backup_controller_dbops::drop_backup_ids_temp_table('testingid');
 }