MDL-58454 badges: Fix some minor errors for Open Badges v2
[moodle.git] / lib / tests / xmlize_test.php
blob554f165275ffdb0ef52eb4bb222bc3f904c5c197
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * Test xmlize xml import.
20 * @package core
21 * @category test
22 * @copyright 2017 Kilian Singer {@link http://quantumtechnology.info}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->libdir . '/xmlize.php');
31 /**
32 * This test compares library against the original xmlize XML importer.
34 * @copyright 2017 Kilian Singer {@link http://quantumtechnology.info}
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class core_xmlize_testcase extends basic_testcase {
38 /**
39 * Test an XML import using a valid XML file.
41 * The test expected file was generated using the original xmlize
42 * implentation found at https://github.com/rmccue/XMLize/blob/master/xmlize-php5.inc.
44 public function test_xmlimport_of_proper_file() {
45 $xml = file_get_contents(__DIR__ . '/sample_questions.xml');
46 $serialised = file_get_contents(__DIR__ . '/sample_questions.ser');
47 $this->assertEquals(unserialize($serialised), xmlize($xml));
50 /**
51 * Test an XML import using invalid XML.
53 public function test_xmlimport_of_wrong_file() {
54 $xml = file_get_contents(__DIR__ . '/sample_questions_wrong.xml');
55 $this->expectException('xml_format_exception');
56 $this->expectExceptionMessage('Error parsing XML: Mismatched tag at line 18, char 23');
57 $xmlnew = xmlize($xml, 1, "UTF-8", true);
60 /**
61 * Test an XML import using legacy question data with old image tag.
63 public function test_xmlimport_of_sample_question_with_old_image_tag() {
64 $xml = file_get_contents(__DIR__ . '/sample_questions_with_old_image_tag.xml');
65 $serialised = file_get_contents(__DIR__ . '/sample_questions_with_old_image_tag.ser');
67 // Compare the legacy representation in its serialized state and after unserialization.
68 $this->assertEquals($serialised, serialize(xmlize($xml)));
69 $this->assertEquals(unserialize($serialised), xmlize($xml));