2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 defined('MOODLE_INTERNAL') ||
die();
22 require_once($CFG->libdir
. '/xmlize.php');
25 * This test compares library against the original xmlize XML importer.
29 * @copyright 2017 Kilian Singer {@link http://quantumtechnology.info}
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 class xmlize_test
extends \basic_testcase
{
34 * Test an XML import using a valid XML file.
36 * The test expected file was generated using the original xmlize
37 * implentation found at https://github.com/rmccue/XMLize/blob/master/xmlize-php5.inc.
39 public function test_xmlimport_of_proper_file() {
40 $xml = file_get_contents(__DIR__
. '/sample_questions.xml');
41 $serialised = file_get_contents(__DIR__
. '/sample_questions.ser');
42 $this->assertEquals(unserialize($serialised), xmlize($xml));
46 * Test an XML import using invalid XML.
48 public function test_xmlimport_of_wrong_file() {
49 $xml = file_get_contents(__DIR__
. '/sample_questions_wrong.xml');
50 $this->expectException('xml_format_exception');
51 $this->expectExceptionMessage('Error parsing XML: Mismatched tag at line 18, char 23');
52 $xmlnew = xmlize($xml, 1, "UTF-8", true);
56 * Test an XML import using legacy question data with old image tag.
58 public function test_xmlimport_of_sample_question_with_old_image_tag() {
59 $xml = file_get_contents(__DIR__
. '/sample_questions_with_old_image_tag.xml');
60 $serialised = file_get_contents(__DIR__
. '/sample_questions_with_old_image_tag.ser');
62 // Compare the legacy representation in its serialized state and after unserialization.
63 $this->assertEquals($serialised, serialize(xmlize($xml)));
64 $this->assertEquals(unserialize($serialised), xmlize($xml));