Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/backup/util/structure/simpletest/testbackupstructures.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00027 // Prevent direct access to this file
00028 if (!defined('MOODLE_INTERNAL')) {
00029     die('Direct access to this script is forbidden.');
00030 }
00031 
00032 // Include all the needed stuff
00033 require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
00034 require_once($CFG->dirroot . '/backup/util/xml/output/memory_xml_output.class.php');
00035 
00039 class backup_structure_test extends UnitTestCaseUsingDatabase {
00040 
00041     public static $includecoverage = array(
00042         'backup/util/structure'
00043     );
00044     public static $excludecoverage = array(
00045         'backup/util/structure/simpletest'
00046     );
00047 
00048     protected $testtables = array(
00049         'lib'       => array(
00050             'files', 'rating'),
00051         'mod/forum' => array(
00052             'forum', 'forum_discussions', 'forum_posts',
00053             'forum_read'));
00054 
00055     protected $forumid;   // To store the inserted forum->id
00056     protected $contextid; // Official contextid for these tests
00057 
00058 
00059     public function setUp() {
00060         parent::setUp();
00061 
00062         $this->switch_to_test_db(); // Switch to test DB for all the execution
00063 
00064         foreach ($this->testtables as $dir => $tables) {
00065             $this->create_test_tables($tables, $dir); // Create tables
00066         }
00067 
00068         $this->contextid = 666; // Let's assume this is the context for the forum
00069         $this->fill_records(); // Add common stuff needed by various test methods
00070     }
00071 
00072     public function tearDown() {
00073         parent::tearDown(); // In charge of droppng all the test tables
00074     }
00075 
00076     private function fill_records() {
00077         global $DB;
00078 
00079         // Create one forum
00080         $forum_data = (object)array('course' => 1, 'name' => 'Test forum', 'intro' => 'Intro forum');
00081         $this->forumid = $DB->insert_record('forum', $forum_data);
00082         // With two related file
00083         $f1_forum_data = (object)array(
00084                                      'contenthash' => 'testf1', 'contextid' => $this->contextid,
00085                                      'component'=>'mod_forum', 'filearea' => 'intro', 'filename' => 'tf1', 'itemid' => 0,
00086                                      'filesize' => 123, 'timecreated' => 0, 'timemodified' => 0,
00087                                      'pathnamehash' => 'testf1'
00088                                  );
00089         $DB->insert_record('files', $f1_forum_data);
00090         $f2_forum_data = (object)array(
00091                                      'contenthash' => 'tesft2', 'contextid' => $this->contextid,
00092                                      'component'=>'mod_forum', 'filearea' => 'intro', 'filename' => 'tf2', 'itemid' => 0,
00093                                      'filesize' => 123, 'timecreated' => 0, 'timemodified' => 0,
00094                                      'pathnamehash' => 'testf2'
00095                                  );
00096         $DB->insert_record('files', $f2_forum_data);
00097 
00098         // Create two discussions
00099         $discussion1 = (object)array('course' => 1, 'forum' => $this->forumid, 'name' => 'd1', 'userid' => 100, 'groupid' => 200);
00100         $d1id = $DB->insert_record('forum_discussions', $discussion1);
00101         $discussion2 = (object)array('course' => 1, 'forum' => $this->forumid, 'name' => 'd2', 'userid' => 101, 'groupid' => 201);
00102         $d2id = $DB->insert_record('forum_discussions', $discussion2);
00103 
00104         // Create four posts
00105         $post1 = (object)array('discussion' => $d1id, 'userid' => 100, 'subject' => 'p1', 'message' => 'm1');
00106         $p1id = $DB->insert_record('forum_posts', $post1);
00107         $post2 = (object)array('discussion' => $d1id, 'parent' => $p1id, 'userid' => 102, 'subject' => 'p2', 'message' => 'm2');
00108         $p2id = $DB->insert_record('forum_posts', $post2);
00109         $post3 = (object)array('discussion' => $d1id, 'parent' => $p2id, 'userid' => 103, 'subject' => 'p3', 'message' => 'm3');
00110         $p3id = $DB->insert_record('forum_posts', $post3);
00111         $post4 = (object)array('discussion' => $d2id, 'userid' => 101, 'subject' => 'p4', 'message' => 'm4');
00112         $p4id = $DB->insert_record('forum_posts', $post4);
00113         // With two related file
00114         $f1_post1 = (object)array(
00115                                 'contenthash' => 'testp1', 'contextid' => $this->contextid, 'component'=>'mod_forum',
00116                                 'filearea' => 'post', 'filename' => 'tp1', 'itemid' => $p1id,
00117                                 'filesize' => 123, 'timecreated' => 0, 'timemodified' => 0,
00118                                 'pathnamehash' => 'testp1'
00119                             );
00120         $DB->insert_record('files', $f1_post1);
00121         $f1_post2 = (object)array(
00122                                 'contenthash' => 'testp2', 'contextid' => $this->contextid, 'component'=>'mod_forum',
00123                                 'filearea' => 'attachment', 'filename' => 'tp2', 'itemid' => $p2id,
00124                                 'filesize' => 123, 'timecreated' => 0, 'timemodified' => 0,
00125                                 'pathnamehash' => 'testp2'
00126                             );
00127         $DB->insert_record('files', $f1_post2);
00128 
00129         // Create two ratings
00130         $rating1 = (object)array(
00131             'contextid' => $this->contextid, 'userid' => 104, 'itemid' => $p1id, 'rating' => 2,
00132             'scaleid' => -1, 'timecreated' => time(), 'timemodified' => time());
00133         $r1id = $DB->insert_record('rating', $rating1);
00134         $rating2 = (object)array(
00135             'contextid' => $this->contextid, 'userid' => 105, 'itemid' => $p1id, 'rating' => 3,
00136             'scaleid' => -1, 'timecreated' => time(), 'timemodified' => time());
00137         $r2id = $DB->insert_record('rating', $rating2);
00138 
00139         // Create 1 reads
00140         $read1 = (object)array('userid' => 102, 'forumid' => $this->forumid, 'discussionid' => $d2id, 'postid' => $p4id);
00141         $DB->insert_record('forum_read', $read1);
00142     }
00143 
00147     function test_backup_structure_construct() {
00148         global $DB;
00149 
00150         $backupid = 'Testing Backup ID'; // Official backupid for these tests
00151 
00152         // Create all the elements that will conform the tree
00153         $forum = new backup_nested_element('forum',
00154                                            array('id'),
00155                                            array(
00156                                                'type', 'name', 'intro', 'introformat',
00157                                                'assessed', 'assesstimestart', 'assesstimefinish', 'scale',
00158                                                'maxbytes', 'maxattachments', 'forcesubscribe', 'trackingtype',
00159                                                'rsstype', 'rssarticles', 'timemodified', 'warnafter',
00160                                                'blockafter',
00161                                                new backup_final_element('blockperiod'),
00162                                                new mock_skip_final_element('completiondiscussions'),
00163                                                new mock_modify_final_element('completionreplies'),
00164                                                new mock_final_element_interceptor('completionposts'))
00165                                           );
00166         $discussions = new backup_nested_element('discussions');
00167         $discussion = new backup_nested_element('discussion',
00168                                                 array('id'),
00169                                                 array(
00170                                                     'forum', 'name', 'firstpost', 'userid',
00171                                                     'groupid', 'assessed', 'timemodified', 'usermodified',
00172                                                     'timestart', 'timeend')
00173                                                );
00174         $posts = new backup_nested_element('posts');
00175         $post = new backup_nested_element('post',
00176                                           array('id'),
00177                                           array(
00178                                               'discussion', 'parent', 'userid', 'created',
00179                                               'modified', 'mailed', 'subject', 'message',
00180                                               'messageformat', 'messagetrust', 'attachment', 'totalscore',
00181                                               'mailnow')
00182                                          );
00183         $ratings = new backup_nested_element('ratings');
00184         $rating = new backup_nested_element('rating',
00185                                             array('id'),
00186                                             array('userid', 'itemid', 'time', 'post_rating')
00187                                            );
00188         $reads = new backup_nested_element('readposts');
00189         $read = new backup_nested_element('read',
00190                                           array('id'),
00191                                           array(
00192                                               'userid', 'discussionid', 'postid',
00193                                               'firstread', 'lastread')
00194                                          );
00195         $inventeds = new backup_nested_element('invented_elements',
00196                                                array('reason', 'version')
00197                                                    );
00198         $invented = new backup_nested_element('invented',
00199                                               null,
00200                                               array('one', 'two', 'three')
00201                                              );
00202         $one = $invented->get_final_element('one');
00203         $one->add_attributes(array('attr1', 'attr2'));
00204 
00205         // Build the tree
00206         $forum->add_child($discussions);
00207         $discussions->add_child($discussion);
00208 
00209         $discussion->add_child($posts);
00210         $posts->add_child($post);
00211 
00212         $post->add_child($ratings);
00213         $ratings->add_child($rating);
00214 
00215         $forum->add_child($reads);
00216         $reads->add_child($read);
00217 
00218         $forum->add_child($inventeds);
00219         $inventeds->add_child($invented);
00220 
00221         // Let's add 1 optigroup with 4 elements
00222         $alternative1 = new backup_optigroup_element('alternative1',
00223                                                      array('name', 'value'), '../../id', 1);
00224         $alternative2 = new backup_optigroup_element('alternative2',
00225                                                      array('name', 'value'), backup::VAR_PARENTID, 2);
00226         $alternative3 = new backup_optigroup_element('alternative3',
00227                                                      array('name', 'value'), '/forum/discussions/discussion/posts/post/id', 3);
00228         $alternative4 = new backup_optigroup_element('alternative4',
00229                                                      array('forumtype', 'forumname')); // Alternative without conditions
00230         // Create the optigroup, adding one element
00231         $optigroup = new backup_optigroup('alternatives', $alternative1, false);
00232         // Add second opti element
00233         $optigroup->add_child($alternative2);
00234 
00235         // Add optigroup to post element
00236         $post->add_optigroup($optigroup);
00237         // Add third opti element, on purpose after the add_optigroup() line above to check param evaluation works ok
00238         $optigroup->add_child($alternative3);
00239         // Add 4th opti element (the one without conditions, so will be present always)
00240         $optigroup->add_child($alternative4);
00241 
00244         $dupetest1 = new backup_nested_element('dupetest1', null, array('field1', 'field2'));
00245         $dupetest2 = new backup_nested_element('dupetest2', null, array('field1', 'field2'));
00246         $dupetest3 = new backup_nested_element('dupetest3', null, array('field1', 'field2'));
00247         $dupetest4 = new backup_nested_element('dupetest1', null, array('field1', 'field2'));
00248         $dupetest1->add_child($dupetest3);
00249         $dupetest2->add_child($dupetest4);
00250         $alternative1->add_child($dupetest1);
00251         $alternative2->add_child($dupetest2);
00252 
00253         // Define sources
00254         $forum->set_source_table('forum', array('id' => backup::VAR_ACTIVITYID));
00255         $discussion->set_source_sql('SELECT *
00256                                        FROM {forum_discussions}
00257                                       WHERE forum = ?',
00258                                      array('/forum/id')
00259                                     );
00260         $post->set_source_table('forum_posts', array('discussion' => '/forum/discussions/discussion/id'));
00261         $rating->set_source_sql('SELECT *
00262                                    FROM {rating}
00263                                   WHERE itemid = ?',
00264                                  array(backup::VAR_PARENTID)
00265                                 );
00266 
00267         $read->set_source_table('forum_read', array('id' => '../../id'));
00268 
00269         $inventeds->set_source_array(array((object)array('reason' => 'I love Moodle', 'version' => '1.0'),
00270                                            (object)array('reason' => 'I love Moodle', 'version' => '2.0'))); // 2 object array
00271         $invented->set_source_array(array((object)array('one' => 1, 'two' => 2, 'three' => 3),
00272                                           (object)array('one' => 11, 'two' => 22, 'three' => 33))); // 2 object array
00273 
00274         // Set optigroup_element sources
00275         $alternative1->set_source_array(array((object)array('name' => 'alternative1', 'value' => 1))); // 1 object array
00276         // Skip alternative2 source definition on purpose (will be tested)
00277         // $alternative2->set_source_array(array((object)array('name' => 'alternative2', 'value' => 2))); // 1 object array
00278         $alternative3->set_source_array(array((object)array('name' => 'alternative3', 'value' => 3))); // 1 object array
00279         // Alternative 4 source is the forum type and name, so we'll get that in ALL posts (no conditions) that
00280         // have not another alternative (post4 in our testing data in the only not matching any other alternative)
00281         $alternative4->set_source_sql('SELECT type AS forumtype, name AS forumname
00282                                          FROM {forum}
00283                                         WHERE id = ?',
00284                                      array('/forum/id')
00285                                     );
00286         // Set children of optigroup_element source
00287         $dupetest1->set_source_array(array((object)array('field1' => '1', 'field2' => 1))); // 1 object array
00288         $dupetest2->set_source_array(array((object)array('field1' => '2', 'field2' => 2))); // 1 object array
00289         $dupetest3->set_source_array(array((object)array('field1' => '3', 'field2' => 3))); // 1 object array
00290         $dupetest4->set_source_array(array((object)array('field1' => '4', 'field2' => 4))); // 1 object array
00291 
00292         // Define some aliases
00293         $rating->set_source_alias('rating', 'post_rating'); // Map the 'rating' value from DB to 'post_rating' final element
00294 
00295         // Mark to detect files of type 'forum_intro' in forum (and not item id)
00296         $forum->annotate_files('mod_forum', 'intro', null);
00297 
00298         // Mark to detect file of type 'forum_post' and 'forum_attachment' in post (with itemid being post->id)
00299         $post->annotate_files('mod_forum', 'post', 'id');
00300         $post->annotate_files('mod_forum', 'attachment', 'id');
00301 
00302         // Mark various elements to be annotated
00303         $discussion->annotate_ids('user1', 'userid');
00304         $post->annotate_ids('forum_post', 'id');
00305         $rating->annotate_ids('user2', 'userid');
00306         $rating->annotate_ids('forum_post', 'itemid');
00307 
00308         // Create the backup_ids_temp table
00309         backup_controller_dbops::create_backup_ids_temp_table($backupid);
00310 
00311         // Instantiate in memory xml output
00312         $xo = new memory_xml_output();
00313 
00314         // Instantiate xml_writer and start it
00315         $xw = new xml_writer($xo);
00316         $xw->start();
00317 
00318         // Instantiate the backup processor
00319         $processor = new backup_structure_processor($xw);
00320 
00321         // Set some variables
00322         $processor->set_var(backup::VAR_ACTIVITYID, $this->forumid);
00323         $processor->set_var(backup::VAR_BACKUPID, $backupid);
00324         $processor->set_var(backup::VAR_CONTEXTID,$this->contextid);
00325 
00326         // Process the backup structure with the backup processor
00327         $forum->process($processor);
00328 
00329         // Stop the xml_writer
00330         $xw->stop();
00331 
00332         // Check various counters
00333         $this->assertEqual($forum->get_counter(), $DB->count_records('forum'));
00334         $this->assertEqual($discussion->get_counter(), $DB->count_records('forum_discussions'));
00335         $this->assertEqual($rating->get_counter(), $DB->count_records('rating'));
00336         $this->assertEqual($read->get_counter(), $DB->count_records('forum_read'));
00337         $this->assertEqual($inventeds->get_counter(), 2); // Array
00338 
00339         // Perform some validations with the generated XML
00340         $dom = DOMDocument::loadXML($xo->get_allcontents());
00341         $xpath = new DOMXPath($dom);
00342         // Some more counters
00343         $query = '/forum/discussions/discussion/posts/post';
00344         $posts = $xpath->query($query);
00345         $this->assertEqual($posts->length, $DB->count_records('forum_posts'));
00346         $query = '/forum/invented_elements/invented';
00347         $inventeds = $xpath->query($query);
00348         $this->assertEqual($inventeds->length, 2*2);
00349 
00350         // Check ratings information against DB
00351         $ratings = $dom->getElementsByTagName('rating');
00352         $this->assertEqual($ratings->length, $DB->count_records('rating'));
00353         foreach ($ratings as $rating) {
00354             $ratarr = array();
00355             $ratarr['id'] = $rating->getAttribute('id');
00356             foreach ($rating->childNodes as $node) {
00357                 if ($node->nodeType != XML_TEXT_NODE) {
00358                     $ratarr[$node->nodeName] = $node->nodeValue;
00359                 }
00360             }
00361             $this->assertEqual($ratarr['userid'], $DB->get_field('rating', 'userid', array('id' => $ratarr['id'])));
00362             $this->assertEqual($ratarr['itemid'], $DB->get_field('rating', 'itemid', array('id' => $ratarr['id'])));
00363             $this->assertEqual($ratarr['post_rating'], $DB->get_field('rating', 'rating', array('id' => $ratarr['id'])));
00364         }
00365 
00366         // Check forum has "blockeperiod" with value 0 (was declared by object instead of name)
00367         $query = '/forum[blockperiod="0"]';
00368         $result = $xpath->query($query);
00369         $this->assertEqual($result->length, 1);
00370 
00371         // Check forum is missing "completiondiscussions" (as we are using mock_skip_final_element)
00372         $query = '/forum/completiondiscussions';
00373         $result = $xpath->query($query);
00374         $this->assertEqual($result->length, 0);
00375 
00376         // Check forum has "completionreplies" with value "original was 0, now changed" (because of mock_modify_final_element)
00377         $query = '/forum[completionreplies="original was 0, now changed"]';
00378         $result = $xpath->query($query);
00379         $this->assertEqual($result->length, 1);
00380 
00381         // Check forum has "completionposts" with value "intercepted!" (because of mock_final_element_interceptor)
00382         $query = '/forum[completionposts="intercepted!"]';
00383         $result = $xpath->query($query);
00384         $this->assertEqual($result->length, 1);
00385 
00386         // Check there isn't any alternative2 tag, as far as it hasn't source defined
00387         $query = '//alternative2';
00388         $result = $xpath->query($query);
00389         $this->assertEqual($result->length, 0);
00390 
00391         // Check there are 4 "field1" elements
00392         $query = '/forum/discussions/discussion/posts/post//field1';
00393         $result = $xpath->query($query);
00394         $this->assertEqual($result->length, 4);
00395 
00396         // Check first post has one name element with value "alternative1"
00397         $query = '/forum/discussions/discussion/posts/post[@id="1"][name="alternative1"]';
00398         $result = $xpath->query($query);
00399         $this->assertEqual($result->length, 1);
00400 
00401         // Check there are two "dupetest1" elements
00402         $query = '/forum/discussions/discussion/posts/post//dupetest1';
00403         $result = $xpath->query($query);
00404         $this->assertEqual($result->length, 2);
00405 
00406         // Check second post has one name element with value "dupetest2"
00407         $query = '/forum/discussions/discussion/posts/post[@id="2"]/dupetest2';
00408         $result = $xpath->query($query);
00409         $this->assertEqual($result->length, 1);
00410 
00411         // Check element "dupetest2" of second post has one field1 element with value "2"
00412         $query = '/forum/discussions/discussion/posts/post[@id="2"]/dupetest2[field1="2"]';
00413         $result = $xpath->query($query);
00414         $this->assertEqual($result->length, 1);
00415 
00416         // Check forth post has no name element
00417         $query = '/forum/discussions/discussion/posts/post[@id="4"]/name';
00418         $result = $xpath->query($query);
00419         $this->assertEqual($result->length, 0);
00420 
00421         // Check 1st, 2nd and 3rd posts have no forumtype element
00422         $query = '/forum/discussions/discussion/posts/post[@id="1"]/forumtype';
00423         $result = $xpath->query($query);
00424         $this->assertEqual($result->length, 0);
00425         $query = '/forum/discussions/discussion/posts/post[@id="2"]/forumtype';
00426         $result = $xpath->query($query);
00427         $this->assertEqual($result->length, 0);
00428         $query = '/forum/discussions/discussion/posts/post[@id="3"]/forumtype';
00429         $result = $xpath->query($query);
00430         $this->assertEqual($result->length, 0);
00431 
00432         // Check 4th post has one forumtype element with value "general"
00433         // (because it doesn't matches alternatives 1, 2, 3, then alternative 4,
00434         // the one without conditions is being applied)
00435         $query = '/forum/discussions/discussion/posts/post[@id="4"][forumtype="general"]';
00436         $result = $xpath->query($query);
00437         $this->assertEqual($result->length, 1);
00438 
00439         // Check annotations information against DB
00440         // Count records in original tables
00441         $c_postsid    = $DB->count_records_sql('SELECT COUNT(DISTINCT id) FROM {forum_posts}');
00442         $c_dissuserid = $DB->count_records_sql('SELECT COUNT(DISTINCT userid) FROM {forum_discussions}');
00443         $c_ratuserid  = $DB->count_records_sql('SELECT COUNT(DISTINCT userid) FROM {rating}');
00444         // Count records in backup_ids_table
00445         $f_forumpost = $DB->count_records('backup_ids_temp', array('backupid' => $backupid, 'itemname' => 'forum_post'));
00446         $f_user1     = $DB->count_records('backup_ids_temp', array('backupid' => $backupid, 'itemname' => 'user1'));
00447         $f_user2     = $DB->count_records('backup_ids_temp', array('backupid' => $backupid, 'itemname' => 'user2'));
00448         $c_notbackupid = $DB->count_records_select('backup_ids_temp', 'backupid != ?', array($backupid));
00449         // Peform tests by comparing counts
00450         $this->assertEqual($c_notbackupid, 0); // there isn't any record with incorrect backupid
00451         $this->assertEqual($c_postsid, $f_forumpost); // All posts have been registered
00452         $this->assertEqual($c_dissuserid, $f_user1); // All users coming from discussions have been registered
00453         $this->assertEqual($c_ratuserid, $f_user2); // All users coming from ratings have been registered
00454 
00455         // Check file annotations against DB
00456         $fannotations = $DB->get_records('backup_ids_temp', array('backupid' => $backupid, 'itemname' => 'file'));
00457         $ffiles       = $DB->get_records('files', array('contextid' => $this->contextid));
00458         $this->assertEqual(count($fannotations), count($ffiles)); // Same number of recs in both (all files have been annotated)
00459         foreach ($fannotations as $annotation) { // Check ids annotated
00460             $this->assertTrue($DB->record_exists('files', array('id' => $annotation->itemid)));
00461         }
00462 
00463         // Drop the backup_ids_temp table
00464         backup_controller_dbops::drop_backup_ids_temp_table('testingid');
00465     }
00466 
00470     function test_backup_structure_wrong() {
00471 
00472         // Instantiate the backup processor
00473         $processor = new backup_structure_processor(new xml_writer(new memory_xml_output()));
00474         $this->assertTrue($processor instanceof base_processor);
00475 
00476         // Set one var twice
00477         $processor->set_var('onenewvariable', 999);
00478         try {
00479             $processor->set_var('onenewvariable', 999);
00480             $this->assertTrue(false, 'backup_processor_exception expected');
00481         } catch (exception $e) {
00482             $this->assertTrue($e instanceof backup_processor_exception);
00483             $this->assertEqual($e->errorcode, 'processorvariablealreadyset');
00484             $this->assertEqual($e->a, 'onenewvariable');
00485         }
00486 
00487         // Get non-existing var
00488         try {
00489             $var = $processor->get_var('nonexistingvar');
00490             $this->assertTrue(false, 'backup_processor_exception expected');
00491         } catch (exception $e) {
00492             $this->assertTrue($e instanceof backup_processor_exception);
00493             $this->assertEqual($e->errorcode, 'processorvariablenotfound');
00494             $this->assertEqual($e->a, 'nonexistingvar');
00495         }
00496 
00497         // Create nested element and try ro get its parent id (doesn't exisit => exception)
00498         $ne = new backup_nested_element('test', 'one', 'two', 'three');
00499         try {
00500             $ne->set_source_table('forum', array('id' => backup::VAR_PARENTID));
00501             $ne->process($processor);
00502             $this->assertTrue(false, 'base_element_struct_exception expected');
00503         } catch (exception $e) {
00504             $this->assertTrue($e instanceof base_element_struct_exception);
00505             $this->assertEqual($e->errorcode, 'cannotfindparentidforelement');
00506         }
00507 
00508         // Try to process one nested/final/attribute elements without processor
00509         $ne = new backup_nested_element('test', 'one', 'two', 'three');
00510         try {
00511             $ne->process(new stdclass());
00512             $this->assertTrue(false, 'base_element_struct_exception expected');
00513         } catch (exception $e) {
00514             $this->assertTrue($e instanceof base_element_struct_exception);
00515             $this->assertEqual($e->errorcode, 'incorrect_processor');
00516         }
00517         $fe = new backup_final_element('test');
00518         try {
00519             $fe->process(new stdclass());
00520             $this->assertTrue(false, 'base_element_struct_exception expected');
00521         } catch (exception $e) {
00522             $this->assertTrue($e instanceof base_element_struct_exception);
00523             $this->assertEqual($e->errorcode, 'incorrect_processor');
00524         }
00525         $at = new backup_attribute('test');
00526         try {
00527             $at->process(new stdclass());
00528             $this->assertTrue(false, 'base_element_struct_exception expected');
00529         } catch (exception $e) {
00530             $this->assertTrue($e instanceof base_element_struct_exception);
00531             $this->assertEqual($e->errorcode, 'incorrect_processor');
00532         }
00533 
00534         // Try to put an incorrect alias
00535         $ne = new backup_nested_element('test', 'one', 'two', 'three');
00536         try {
00537             $ne->set_source_alias('last', 'nonexisting');
00538             $this->assertTrue(false, 'base_element_struct_exception expected');
00539         } catch (exception $e) {
00540             $this->assertTrue($e instanceof base_element_struct_exception);
00541             $this->assertEqual($e->errorcode, 'incorrectaliasfinalnamenotfound');
00542             $this->assertEqual($e->a, 'nonexisting');
00543         }
00544 
00545         // Try various incorrect paths specifying source
00546         $ne = new backup_nested_element('test', 'one', 'two', 'three');
00547         try {
00548             $ne->set_source_table('forum', array('/test/subtest'));
00549             $this->assertTrue(false, 'base_element_struct_exception expected');
00550         } catch (exception $e) {
00551             $this->assertTrue($e instanceof base_element_struct_exception);
00552             $this->assertEqual($e->errorcode, 'baseelementincorrectfinalorattribute');
00553             $this->assertEqual($e->a, 'subtest');
00554         }
00555         try {
00556             $ne->set_source_table('forum', array('/wrongtest'));
00557             $this->assertTrue(false, 'base_element_struct_exception expected');
00558         } catch (exception $e) {
00559             $this->assertTrue($e instanceof base_element_struct_exception);
00560             $this->assertEqual($e->errorcode, 'baseelementincorrectgrandparent');
00561             $this->assertEqual($e->a, 'wrongtest');
00562         }
00563         try {
00564             $ne->set_source_table('forum', array('../nonexisting'));
00565             $this->assertTrue(false, 'base_element_struct_exception expected');
00566         } catch (exception $e) {
00567             $this->assertTrue($e instanceof base_element_struct_exception);
00568             $this->assertEqual($e->errorcode, 'baseelementincorrectparent');
00569             $this->assertEqual($e->a, '..');
00570         }
00571 
00572         // Try various incorrect file annotations
00573 
00574         $ne = new backup_nested_element('test', 'one', 'two', 'three');
00575         $ne->annotate_files('test', 'filearea', null);
00576         try {
00577             $ne->annotate_files('test', 'filearea', null); // Try to add annotations twice
00578             $this->assertTrue(false, 'base_element_struct_exception expected');
00579         } catch (exception $e) {
00580             $this->assertTrue($e instanceof base_element_struct_exception);
00581             $this->assertEqual($e->errorcode, 'annotate_files_duplicate_annotation');
00582             $this->assertEqual($e->a, 'test/filearea/');
00583         }
00584 
00585         $ne = new backup_nested_element('test', 'one', 'two', 'three');
00586         try {
00587             $ne->annotate_files('test', 'filearea', 'four'); // Incorrect element
00588             $this->assertTrue(false, 'base_element_struct_exception expected');
00589         } catch (exception $e) {
00590             $this->assertTrue($e instanceof base_element_struct_exception);
00591             $this->assertEqual($e->errorcode, 'baseelementincorrectfinalorattribute');
00592             $this->assertEqual($e->a, 'four');
00593         }
00594 
00595         // Try to add incorrect element to backup_optigroup
00596         $bog = new backup_optigroup('test');
00597         try {
00598             $bog->add_child(new backup_nested_element('test2'));
00599             $this->assertTrue(false, 'base_optigroup_exception expected');
00600         } catch (exception $e) {
00601             $this->assertTrue($e instanceof base_optigroup_exception);
00602             $this->assertEqual($e->errorcode, 'optigroup_element_incorrect');
00603             $this->assertEqual($e->a, 'backup_nested_element');
00604         }
00605 
00606         $bog = new backup_optigroup('test');
00607         try {
00608             $bog->add_child('test2');
00609             $this->assertTrue(false, 'base_optigroup_exception expected');
00610         } catch (exception $e) {
00611             $this->assertTrue($e instanceof base_optigroup_exception);
00612             $this->assertEqual($e->errorcode, 'optigroup_element_incorrect');
00613             $this->assertEqual($e->a, 'non object');
00614         }
00615 
00616         try {
00617             $bog = new backup_optigroup('test', new stdclass());
00618             $this->assertTrue(false, 'base_optigroup_exception expected');
00619         } catch (exception $e) {
00620             $this->assertTrue($e instanceof base_optigroup_exception);
00621             $this->assertEqual($e->errorcode, 'optigroup_elements_incorrect');
00622         }
00623 
00624         // Try a wrong processor with backup_optigroup
00625         $bog = new backup_optigroup('test');
00626         try {
00627             $bog->process(new stdclass());
00628             $this->assertTrue(false, 'base_element_struct_exception expected');
00629         } catch (exception $e) {
00630             $this->assertTrue($e instanceof base_element_struct_exception);
00631             $this->assertEqual($e->errorcode, 'incorrect_processor');
00632         }
00633 
00634         // Try duplicating used elements with backup_optigroup
00635         // Adding top->down
00636         $bog = new backup_optigroup('test', null, true);
00637         $boge1 = new backup_optigroup_element('boge1');
00638         $boge2 = new backup_optigroup_element('boge2');
00639         $ne1 = new backup_nested_element('ne1');
00640         $ne2 = new backup_nested_element('ne1');
00641         $bog->add_child($boge1);
00642         $bog->add_child($boge2);
00643         $boge1->add_child($ne1);
00644         try {
00645             $boge2->add_child($ne2);
00646             $this->assertTrue(false, 'base_optigroup_exception expected');
00647         } catch (exception $e) {
00648             $this->assertTrue($e instanceof base_optigroup_exception);
00649             $this->assertEqual($e->errorcode, 'multiple_optigroup_duplicate_element');
00650             $this->assertEqual($e->a, 'ne1');
00651         }
00652         // Adding down->top
00653         $bog = new backup_optigroup('test', null, true);
00654         $boge1 = new backup_optigroup_element('boge1');
00655         $boge2 = new backup_optigroup_element('boge2');
00656         $ne1 = new backup_nested_element('ne1');
00657         $ne2 = new backup_nested_element('ne1');
00658         $boge1->add_child($ne1);
00659         $boge2->add_child($ne2);
00660         $bog->add_child($boge1);
00661         try {
00662             $bog->add_child($boge2);
00663             $this->assertTrue(false, 'base_element_struct_exception expected');
00664         } catch (exception $e) {
00665             $this->assertTrue($e instanceof base_element_struct_exception);
00666             $this->assertEqual($e->errorcode, 'baseelementexisting');
00667             $this->assertEqual($e->a, 'ne1');
00668         }
00669 
00670     }
00671 }
00672 
 All Data Structures Namespaces Files Functions Variables Enumerations