function test_memory_xml_output() { // Instantiate xml_output $xo = new memory_xml_output(); $this->assertTrue($xo instanceof xml_output); // Try to write some contents before starting it $xo = new memory_xml_output(); try { $xo->write('test'); $this->assertTrue(false, 'xml_output_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof xml_output_exception); $this->assertEqual($e->errorcode, 'xml_output_not_started'); } // Try to set buffer size if unsupported $xo = new memory_xml_output(); try { $xo->set_buffersize(8192); $this->assertTrue(false, 'xml_output_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof xml_output_exception); $this->assertEqual($e->errorcode, 'xml_output_buffer_nosupport'); } // Try to set buffer after start $xo = new memory_xml_output(); $xo->start(); try { $xo->set_buffersize(8192); $this->assertTrue(false, 'xml_output_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof xml_output_exception); $this->assertEqual($e->errorcode, 'xml_output_already_started'); } // Try to stop output before starting it $xo = new memory_xml_output(); try { $xo->stop(); $this->assertTrue(false, 'xml_output_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof xml_output_exception); $this->assertEqual($e->errorcode, 'xml_output_not_started'); } // Try to debug_info() before starting $xo = new memory_xml_output(); try { $xo->debug_info(); $this->assertTrue(false, 'xml_output_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof xml_output_exception); $this->assertEqual($e->errorcode, 'xml_output_not_stopped'); } // Start output twice $xo = new memory_xml_output(); $xo->start(); try { $xo->start(); $this->assertTrue(false, 'xml_output_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof xml_output_exception); $this->assertEqual($e->errorcode, 'xml_output_already_started'); } // Try to debug_info() before stoping $xo = new memory_xml_output(); $xo->start(); try { $xo->debug_info(); $this->assertTrue(false, 'xml_output_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof xml_output_exception); $this->assertEqual($e->errorcode, 'xml_output_not_stopped'); } // Stop output twice $xo = new memory_xml_output(); $xo->start(); $xo->stop(); try { $xo->stop(); $this->assertTrue(false, 'xml_output_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof xml_output_exception); $this->assertEqual($e->errorcode, 'xml_output_not_started'); } // Try to re-start after stop $xo = new memory_xml_output(); $xo->start(); $xo->stop(); try { $xo->start(); $this->assertTrue(false, 'xml_output_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof xml_output_exception); $this->assertEqual($e->errorcode, 'xml_output_already_stopped'); } // Try to get contents before stopping $xo = new memory_xml_output(); $xo->start(); try { $xo->get_allcontents(); $this->assertTrue(false, 'xml_output_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof xml_output_exception); $this->assertEqual($e->errorcode, 'xml_output_not_stopped'); } // Write some contents and check them $xo = new memory_xml_output(); $xo->start(); $xo->write('first test'); $xo->stop(); $this->assertEqual('first test', $xo->get_allcontents()); // Write 3 times and check them $xo = new memory_xml_output(); $xo->start(); $xo->write('first test'); $xo->write(', sencond test'); $xo->write(', third test'); $xo->stop(); $this->assertEqual('first test, sencond test, third test', $xo->get_allcontents()); // Write some line feeds, tabs and friends $string = "\n\r\tcrazy test\n\r\t"; $xo = new memory_xml_output(); $xo->start(); $xo->write($string); $xo->stop(); $this->assertEqual($string, $xo->get_allcontents()); // Write some UTF-8 chars $string = 'áéíóú'; $xo = new memory_xml_output(); $xo->start(); $xo->write($string); $xo->stop(); $this->assertEqual($string, $xo->get_allcontents()); // Write some empty content $xo = new memory_xml_output(); $xo->start(); $xo->write('Hello '); $xo->write(null); $xo->write(false); $xo->write(''); $xo->write('World'); $xo->write(null); $xo->stop(); $this->assertEqual('Hello World', $xo->get_allcontents()); // Get debug info $xo = new memory_xml_output(); $xo->start(); $xo->write('01234'); $xo->write('56789'); $xo->stop(); $this->assertEqual('0123456789', $xo->get_allcontents()); $debug = $xo->debug_info(); $this->assertTrue(is_array($debug)); $this->assertTrue(array_key_exists('sent', $debug)); $this->assertEqual($debug['sent'], 10); }
/** * test xml_writer public methods */ function test_xml_writer_public_api() { global $CFG; // Instantiate xml_output $xo = new memory_xml_output(); $this->assertTrue($xo instanceof xml_output); // Instantiate xml_writer with null xml_output try { $xw = new mock_xml_writer(null); $this->assertTrue(false, 'xml_writer_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof xml_writer_exception); $this->assertEquals($e->errorcode, 'invalid_xml_output'); } // Instantiate xml_writer with wrong xml_output object try { $xw = new mock_xml_writer(new stdclass()); $this->assertTrue(false, 'xml_writer_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof xml_writer_exception); $this->assertEquals($e->errorcode, 'invalid_xml_output'); } // Instantiate xml_writer with wrong xml_contenttransformer object try { $xw = new mock_xml_writer($xo, new stdclass()); $this->assertTrue(false, 'xml_writer_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof xml_writer_exception); $this->assertEquals($e->errorcode, 'invalid_xml_contenttransformer'); } // Instantiate xml_writer and start it twice $xw = new mock_xml_writer($xo); $xw->start(); try { $xw->start(); $this->assertTrue(false, 'xml_writer_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof xml_writer_exception); $this->assertEquals($e->errorcode, 'xml_writer_already_started'); } // Instantiate xml_writer and stop it twice $xo = new memory_xml_output(); $xw = new mock_xml_writer($xo); $xw->start(); $xw->stop(); try { $xw->stop(); $this->assertTrue(false, 'xml_writer_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof xml_writer_exception); $this->assertEquals($e->errorcode, 'xml_writer_already_stopped'); } // Stop writer without starting it $xo = new memory_xml_output(); $xw = new mock_xml_writer($xo); try { $xw->stop(); $this->assertTrue(false, 'xml_writer_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof xml_writer_exception); $this->assertEquals($e->errorcode, 'xml_writer_not_started'); } // Start writer after stopping it $xo = new memory_xml_output(); $xw = new mock_xml_writer($xo); $xw->start(); $xw->stop(); try { $xw->start(); $this->assertTrue(false, 'xml_writer_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof xml_writer_exception); $this->assertEquals($e->errorcode, 'xml_writer_already_stopped'); } // Try to set prologue/schema after start $xo = new memory_xml_output(); $xw = new mock_xml_writer($xo); $xw->start(); try { $xw->set_nonamespace_schema('http://moodle.org'); $this->assertTrue(false, 'xml_writer_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof xml_writer_exception); $this->assertEquals($e->errorcode, 'xml_writer_already_started'); } try { $xw->set_prologue('sweet prologue'); $this->assertTrue(false, 'xml_writer_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof xml_writer_exception); $this->assertEquals($e->errorcode, 'xml_writer_already_started'); } // Instantiate properly with memory_xml_output, start and stop. // Must get default UTF-8 prologue $xo = new memory_xml_output(); $xw = new mock_xml_writer($xo); $xw->start(); $xw->stop(); $this->assertEquals($xo->get_allcontents(), $xw->get_default_prologue()); // Instantiate, set prologue and schema, put 1 full tag and get results $xo = new memory_xml_output(); $xw = new mock_xml_writer($xo); $xw->set_prologue('CLEARLY WRONG PROLOGUE'); $xw->set_nonamespace_schema('http://moodle.org/littleschema'); $xw->start(); $xw->full_tag('TEST', 'Hello World!', array('id' => 1)); $xw->stop(); $result = $xo->get_allcontents(); // Perform various checks $this->assertEquals(strpos($result, 'WRONG'), 8); $this->assertEquals(strpos($result, '<TEST id="1"'), 22); $this->assertEquals(strpos($result, 'xmlns:xsi='), 39); $this->assertEquals(strpos($result, 'http://moodle.org/littleschema'), 128); $this->assertEquals(strpos($result, 'Hello World'), 160); $this->assertFalse(strpos($result, $xw->get_default_prologue())); // Try to close one tag in wrong order $xo = new memory_xml_output(); $xw = new mock_xml_writer($xo); $xw->start(); $xw->begin_tag('first'); $xw->begin_tag('second'); try { $xw->end_tag('first'); $this->assertTrue(false, 'xml_writer_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof xml_writer_exception); $this->assertEquals($e->errorcode, 'xml_writer_end_tag_no_match'); } // Try to close one tag before starting any tag $xo = new memory_xml_output(); $xw = new mock_xml_writer($xo); $xw->start(); try { $xw->end_tag('first'); $this->assertTrue(false, 'xml_writer_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof xml_writer_exception); $this->assertEquals($e->errorcode, 'xml_writer_end_tag_no_match'); } // Full tag without contents (null and empty string) $xo = new memory_xml_output(); $xw = new mock_xml_writer($xo); $xw->set_prologue(''); // empty prologue for easier matching $xw->start(); $xw->full_tag('tagname', null, array('attrname' => 'attrvalue')); $xw->full_tag('tagname2', '', array('attrname' => 'attrvalue')); $xw->stop(); $result = $xo->get_allcontents(); $this->assertEquals($result, '<tagname attrname="attrvalue" /><tagname2 attrname="attrvalue"></tagname2>'); // Test case-folding is working $xo = new memory_xml_output(); $xw = new mock_xml_writer($xo, null, true); $xw->set_prologue(''); // empty prologue for easier matching $xw->start(); $xw->full_tag('tagname', 'textcontent', array('attrname' => 'attrvalue')); $xw->stop(); $result = $xo->get_allcontents(); $this->assertEquals($result, '<TAGNAME ATTRNAME="attrvalue">textcontent</TAGNAME>'); // Test UTF-8 chars in tag and attribute names, attr values and contents $xo = new memory_xml_output(); $xw = new mock_xml_writer($xo); $xw->set_prologue(''); // empty prologue for easier matching $xw->start(); $xw->full_tag('áéíóú', 'ÁÉÍÓÚ', array('àèìòù' => 'ÀÈÌÒÙ')); $xw->stop(); $result = $xo->get_allcontents(); $this->assertEquals($result, '<áéíóú àèìòù="ÀÈÌÒÙ">ÁÉÍÓÚ</áéíóú>'); // Try non-safe content in attributes $xo = new memory_xml_output(); $xw = new mock_xml_writer($xo); $xw->set_prologue(''); // empty prologue for easier matching $xw->start(); $xw->full_tag('tagname', 'textcontent', array('attrname' => 'attr' . chr(27) . '\'"value')); $xw->stop(); $result = $xo->get_allcontents(); $this->assertEquals($result, '<tagname attrname="attr\'"value">textcontent</tagname>'); // Try non-safe content in text $xo = new memory_xml_output(); $xw = new mock_xml_writer($xo); $xw->set_prologue(''); // empty prologue for easier matching $xw->start(); $xw->full_tag('tagname', "text\r\ncontent\rwith" . chr(27), array('attrname' => 'attrvalue')); $xw->stop(); $result = $xo->get_allcontents(); $this->assertEquals($result, '<tagname attrname="attrvalue">text' . "\ncontent\n" . 'with</tagname>'); // Try to stop the writer without clossing all the open tags $xo = new memory_xml_output(); $xw = new mock_xml_writer($xo); $xw->start(); $xw->begin_tag('first'); try { $xw->stop(); $this->assertTrue(false, 'xml_writer_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof xml_writer_exception); $this->assertEquals($e->errorcode, 'xml_writer_open_tags_remaining'); } // Test simple transformer $xo = new memory_xml_output(); $xt = new mock_xml_contenttransformer(); $xw = new mock_xml_writer($xo, $xt); $xw->set_prologue(''); // empty prologue for easier matching $xw->start(); $xw->full_tag('tagname', null, array('attrname' => 'attrvalue')); $xw->full_tag('tagname2', 'somecontent', array('attrname' => 'attrvalue')); $xw->stop(); $result = $xo->get_allcontents(); $this->assertEquals($result, '<tagname attrname="attrvalue" /><tagname2 attrname="attrvalue">testsomecontent</tagname2>'); // Build a complex XML file and test results against stored file in fixtures $xo = new memory_xml_output(); $xw = new mock_xml_writer($xo); $xw->start(); $xw->begin_tag('toptag', array('name' => 'toptag', 'level' => 1, 'path' => '/toptag')); $xw->full_tag('secondtag', 'secondvalue', array('name' => 'secondtag', 'level' => 2, 'path' => '/toptag/secondtag', 'value' => 'secondvalue')); $xw->begin_tag('thirdtag', array('name' => 'thirdtag', 'level' => 2, 'path' => '/toptag/thirdtag')); $xw->full_tag('onevalue', 'onevalue', array('name' => 'onevalue', 'level' => 3, 'path' => '/toptag/thirdtag/onevalue')); $xw->full_tag('onevalue', 'anothervalue', array('name' => 'onevalue', 'level' => 3, 'value' => 'anothervalue')); $xw->full_tag('onevalue', 'yetanothervalue', array('name' => 'onevalue', 'level' => 3, 'value' => 'yetanothervalue')); $xw->full_tag('twovalue', 'twovalue', array('name' => 'twovalue', 'level' => 3, 'path' => '/toptag/thirdtag/twovalue')); $xw->begin_tag('forthtag', array('name' => 'forthtag', 'level' => 3, 'path' => '/toptag/thirdtag/forthtag')); $xw->full_tag('innervalue', 'innervalue'); $xw->begin_tag('innertag'); $xw->begin_tag('superinnertag', array('name' => 'superinnertag', 'level' => 5)); $xw->full_tag('superinnervalue', 'superinnervalue', array('name' => 'superinnervalue', 'level' => 6)); $xw->end_tag('superinnertag'); $xw->end_tag('innertag'); $xw->end_tag('forthtag'); $xw->begin_tag('fifthtag', array('level' => 3)); $xw->begin_tag('sixthtag', array('level' => 4)); $xw->full_tag('seventh', 'seventh', array('level' => 5)); $xw->end_tag('sixthtag'); $xw->end_tag('fifthtag'); $xw->full_tag('finalvalue', 'finalvalue', array('name' => 'finalvalue', 'level' => 3, 'path' => '/toptag/thirdtag/finalvalue')); $xw->full_tag('finalvalue'); $xw->end_tag('thirdtag'); $xw->end_tag('toptag'); $xw->stop(); $result = $xo->get_allcontents(); $fcontents = file_get_contents($CFG->dirroot . '/backup/util/xml/simpletest/fixtures/test1.xml'); // Normalise carriage return characters. $fcontents = str_replace("\r\n", "\n", $fcontents); $this->assertEquals(trim($result), trim($fcontents)); }
require_once $CFG->dirroot . '/backup/util/xml/output/memory_xml_output.class.php'; require_once $CFG->dirroot . '/backup/util/xml/xml_writer.class.php'; // Site context $sitecontext = get_context_instance(CONTEXT_SYSTEM); // Init file storage object $fs = get_file_storage(); // Init empty array of roles to export $role_ids = array(); // File information $fileinfo = array('userid' => $USER->id, 'contextid' => $sitecontext->id, 'component' => 'report_rolesmigration', 'filearea' => 'backup', 'itemid' => time(), 'filepath' => '/admin/report/rolesmigration/temp/', 'filename' => 'rolesexport.xml'); // Delete file if it already exists if ($file = $fs->get_file($fileinfo['contextid'], $fileinfo['component'], $fileinfo['filearea'], $fileinfo['itemid'], $fileinfo['filepath'], $fileinfo['filename'])) { $file->delete(); } // Init XML oupt and writer objects $xml_output = new memory_xml_output(); $xml = new xml_writer($xml_output); // Start the XML construction process $xml->start(); // Open the top level XML element $xml->begin_tag('MOODLE_ROLES_MIGRATION'); // General site and migration data $xml->begin_tag('INFO'); $xml->full_tag('NAME', 'rolesmigration'); $xml->full_tag('MOODLE_VERSION', $CFG->version); $xml->full_tag('MOODLE_RELEASE', $CFG->release); $xml->full_tag('BACKUP_VERSION', $CFG->backup_version); $xml->full_tag('BACKUP_RELEASE', $CFG->backup_release); $xml->full_tag('DATE', time()); $xml->full_tag('ORIGINAL_WWWROOT', $CFG->wwwroot); $xml->full_tag('ORIGINAL_SITE_IDENTIFIER_HASH', md5(get_site_identifier()));
/** * Packages the entire flavour and returns it * @todo Add a checksum */ public function packaging_execute() { global $USER, $CFG; $errorredirect = $this->url . '?sesskey=' . sesskey(); // Getting selected data $selectedingredients = $this->get_ingredients_from_form(); if (!$selectedingredients) { redirect($errorredirect, get_string('nothingselected', 'local_flavours'), 2); } // Flavour data $form = new flavours_packaging_form($this->url); if (!($data = $form->get_data())) { print_error('errorpackaging', 'local_flavours'); } // Starting <xml> $xmloutput = new memory_xml_output(); $xmltransformer = new flavours_xml_transformer(); $xmlwriter = new flavours_xml_writer($xmloutput, $xmltransformer); $xmlwriter->start(); $xmlwriter->begin_tag('flavour'); $xmlwriter->full_tag('name', $data->name); $xmlwriter->full_tag('description', $data->description); $xmlwriter->full_tag('author', $data->author); $xmlwriter->full_tag('timecreated', time()); $xmlwriter->full_tag('sourceurl', $CFG->wwwroot); $xmlwriter->full_tag('sourcemoodlerelease', $CFG->release); $xmlwriter->full_tag('sourcemoodleversion', $CFG->version); // Random code to store the flavour data $hash = sha1('flavour_' . $USER->id . '_' . time()); $flavourpath = $this->flavourstmpfolder . '/' . $hash; if (file_exists($flavourpath) || !mkdir($flavourpath, $CFG->directorypermissions)) { print_error('errorpackaging', 'local_flavours'); } // Adding the selected ingredients data $xmlwriter->begin_tag('ingredient'); foreach ($selectedingredients as $ingredienttype => $ingredientsdata) { // instance_ingredient_type gets a new flavours_ingredient_* object $type = $this->instance_ingredient_type($ingredienttype); $xmlwriter->begin_tag($type->id); // It executes the ingredient type specific actions to package $type->package_ingredients($xmlwriter, $flavourpath, $ingredientsdata); $xmlwriter->end_tag($type->id); } $xmlwriter->end_tag('ingredient'); // Finishing flavour index $xmlwriter->end_tag('flavour'); $xmlwriter->stop(); $flavourxml = $xmloutput->get_allcontents(); // Creating the .xml with the flavour info $xmlfilepath = $flavourpath . '/flavour.xml'; if (!($xmlfh = fopen($xmlfilepath, 'w'))) { print_error('errorpackaging', 'local_flavours'); } fwrite($xmlfh, $flavourxml); fclose($xmlfh); // Flavour contents compression $packer = new zip_packer(); $zipfilepath = $this->flavourstmpfolder . '/' . $hash . '/flavour_' . date('Y-m-d') . '.zip'; if (!$packer->archive_to_pathname(array('flavour' => $flavourpath), $zipfilepath)) { print_error('errorpackaging', 'local_flavours'); } session_get_instance()->write_close(); send_file($zipfilepath, basename($zipfilepath)); // To avoid the html headers and all the print* stuff die; }
/** * 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'); }