. /** * @package moodlecore * @subpackage backup-tests * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ // Prevent direct access to this file if (!defined('MOODLE_INTERNAL')) { die('Direct access to this script is forbidden.'); } // Include all the needed stuff require_once($CFG->dirroot . '/backup/util/xml/parser/progressive_parser.class.php'); require_once($CFG->dirroot . '/backup/util/xml/parser/processors/progressive_parser_processor.class.php'); require_once($CFG->dirroot . '/backup/util/xml/parser/processors/simplified_parser_processor.class.php'); /* * progressive_parser and progressive_parser_processor tests */ class progressive_parser_test extends UnitTestCase { public static $includecoverage = array('backup/util/xml/parser'); public static $excludecoverage = array('backup/util/xml/parser/simpletest'); /* * test progressive_parser public methods */ function test_parser_public_api() { global $CFG; // Instantiate progressive_parser $pp = new progressive_parser(); $this->assertTrue($pp instanceof progressive_parser); $pr = new mock_parser_processor(); $this->assertTrue($pr instanceof progressive_parser_processor); // Try to process without processor try { $pp->process(); $this->assertTrue(false); } catch (exception $e) { $this->assertTrue($e instanceof progressive_parser_exception); $this->assertEqual($e->errorcode, 'undefined_parser_processor'); } // Assign processor to parser $pp->set_processor($pr); // Try to process without file and contents try { $pp->process(); $this->assertTrue(false); } catch (exception $e) { $this->assertTrue($e instanceof progressive_parser_exception); $this->assertEqual($e->errorcode, 'undefined_xml_to_parse'); } // Assign *invalid* processor to parser try { $pp->set_processor(new stdClass()); $this->assertTrue(false); } catch (exception $e) { $this->assertTrue($e instanceof progressive_parser_exception); $this->assertEqual($e->errorcode, 'invalid_parser_processor'); } // Set file from fixtures (test1.xml) and process it $pp = new progressive_parser(); $pr = new mock_parser_processor(); $pp->set_processor($pr); $pp->set_file($CFG->dirroot . '/backup/util/xml/parser/simpletest/fixtures/test1.xml'); $pp->process(); $serfromfile = serialize($pr->get_chunks()); // Get serialized results (to compare later) // Set *unexisting* file from fixtures try { $pp->set_file($CFG->dirroot . '/backup/util/xml/parser/simpletest/fixtures/test0.xml'); $this->assertTrue(false); } catch (exception $e) { $this->assertTrue($e instanceof progressive_parser_exception); $this->assertEqual($e->errorcode, 'invalid_file_to_parse'); } // Set contents from fixtures (test1.xml) and process it $pp = new progressive_parser(); $pr = new mock_parser_processor(); $pp->set_processor($pr); $pp->set_contents(file_get_contents($CFG->dirroot . '/backup/util/xml/parser/simpletest/fixtures/test1.xml')); $pp->process(); $serfrommemory = serialize($pr->get_chunks()); // Get serialized results (to compare later) // Set *empty* contents try { $pp->set_contents(''); $this->assertTrue(false); } catch (exception $e) { $this->assertTrue($e instanceof progressive_parser_exception); $this->assertEqual($e->errorcode, 'invalid_contents_to_parse'); } // Check that both results from file processing and content processing are equal $this->assertEqual($serfromfile, $serfrommemory); // Check case_folding is working ok $pp = new progressive_parser(true); $pr = new mock_parser_processor(); $pp->set_processor($pr); $pp->set_file($CFG->dirroot . '/backup/util/xml/parser/simpletest/fixtures/test1.xml'); $pp->process(); $chunks = $pr->get_chunks(); $this->assertTrue($chunks[0]['path'] === '/FIRSTTAG'); $this->assertTrue($chunks[0]['tags']['SECONDTAG']['name'] === 'SECONDTAG'); $this->assertTrue($chunks[0]['tags']['SECONDTAG']['attrs']['NAME'] === 'secondtag'); // Check invalid XML exception is working ok $pp = new progressive_parser(true); $pr = new mock_parser_processor(); $pp->set_processor($pr); $pp->set_file($CFG->dirroot . '/backup/util/xml/parser/simpletest/fixtures/test2.xml'); try { $pp->process(); } catch (exception $e) { $this->assertTrue($e instanceof progressive_parser_exception); $this->assertEqual($e->errorcode, 'xml_parsing_error'); } // Check double process throws exception $pp = new progressive_parser(true); $pr = new mock_parser_processor(); $pp->set_processor($pr); $pp->set_file($CFG->dirroot . '/backup/util/xml/parser/simpletest/fixtures/test1.xml'); $pp->process(); try { // Second process, will throw exception $pp->process(); $this->assertTrue(false); } catch (exception $e) { $this->assertTrue($e instanceof progressive_parser_exception); $this->assertEqual($e->errorcode, 'progressive_parser_already_used'); } } /* * test progressive_parser parsing results using testing_parser_processor and test1.xml * auto-described file from fixtures */ function test_parser_results() { global $CFG; // Instantiate progressive_parser $pp = new progressive_parser(); // Instantiate processor, passing the unit test as param $pr = new mock_auto_parser_processor($this); $this->assertTrue($pr instanceof progressive_parser_processor); // Assign processor to parser $pp->set_processor($pr); // Set file from fixtures $pp->set_file($CFG->dirroot . '/backup/util/xml/parser/simpletest/fixtures/test3.xml'); // Process the file, the autotest processor will perform a bunch of automatic tests $pp->process(); // Get processor debug info $debug = $pr->debug_info(); $this->assertTrue(is_array($debug)); $this->assertTrue(array_key_exists('chunks', $debug)); // Check the number of chunks is correct for the file $this->assertEqual($debug['chunks'], 10); } /* * test progressive_parser parsing results using simplified_parser_processor and test4.xml * (one simple glossary backup file example) */ function test_simplified_parser_results() { global $CFG; // Instantiate progressive_parser $pp = new progressive_parser(); // Instantiate simplified_parser_processor declaring the interesting paths $pr = new mock_simplified_parser_processor(array( '/activity', '/activity/glossary', '/activity/glossary/entries/entry', '/activity/glossary/entries/entry/aliases/alias', '/activity/glossary/entries/entry/ratings/rating', '/activity/glossary/categories/category')); $this->assertTrue($pr instanceof progressive_parser_processor); // Assign processor to parser $pp->set_processor($pr); // Set file from fixtures $pp->set_file($CFG->dirroot . '/backup/util/xml/parser/simpletest/fixtures/test4.xml'); // Process the file $pp->process(); // Get processor debug info $debug = $pr->debug_info(); $this->assertTrue(is_array($debug)); $this->assertTrue(array_key_exists('chunks', $debug)); // Check the number of chunks is correct for the file $this->assertEqual($debug['chunks'], 8); // Get all the simplified chunks and perform various validations $chunks = $pr->get_chunks(); // chunk[0] (/activity) tests $this->assertEqual(count($chunks[0]), 3); $this->assertEqual($chunks[0]['path'], '/activity'); $this->assertEqual($chunks[0]['level'],'2'); $tags = $chunks[0]['tags']; $this->assertEqual(count($tags), 4); $this->assertEqual($tags['id'], 1); $this->assertEqual($tags['moduleid'], 5); $this->assertEqual($tags['modulename'], 'glossary'); $this->assertEqual($tags['contextid'], 26); $this->assertEqual($chunks[0]['level'],'2'); // chunk[1] (/activity/glossary) tests $this->assertEqual(count($chunks[1]), 3); $this->assertEqual($chunks[1]['path'], '/activity/glossary'); $this->assertEqual($chunks[1]['level'],'3'); $tags = $chunks[1]['tags']; $this->assertEqual(count($tags), 24); $this->assertEqual($tags['id'], 1); $this->assertEqual($tags['intro'], '
One simple glossary to test backup & restore. Here it\'s the standard image:
'. "\n". '![]()