Merge "Title: Refactor JS/CSS page handling to be more sane"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / XmlTypeCheckTest.php
1 <?php
2 /**
3 * PHPUnit tests for XMLTypeCheck.
4 * @author physikerwelt
5 * @group Xml
6 * @covers XMLTypeCheck
7 */
8 class XmlTypeCheckTest extends PHPUnit\Framework\TestCase {
9
10 use MediaWikiCoversValidator;
11
12 const WELL_FORMED_XML = "<root><child /></root>";
13 const MAL_FORMED_XML = "<root><child /></error>";
14 // phpcs:ignore Generic.Files.LineLength
15 const XML_WITH_PIH = '<?xml version="1.0"?><?xml-stylesheet type="text/xsl" href="/w/index.php"?><svg><child /></svg>';
16
17 /**
18 * @covers XMLTypeCheck::newFromString
19 * @covers XMLTypeCheck::getRootElement
20 */
21 public function testWellFormedXML() {
22 $testXML = XmlTypeCheck::newFromString( self::WELL_FORMED_XML );
23 $this->assertTrue( $testXML->wellFormed );
24 $this->assertEquals( 'root', $testXML->getRootElement() );
25 }
26
27 /**
28 * @covers XMLTypeCheck::newFromString
29 */
30 public function testMalFormedXML() {
31 $testXML = XmlTypeCheck::newFromString( self::MAL_FORMED_XML );
32 $this->assertFalse( $testXML->wellFormed );
33 }
34
35 /**
36 * Verify we check for recursive entity DOS
37 *
38 * (If the DOS isn't properly handled, the test runner will probably go OOM...)
39 */
40 public function testRecursiveEntity() {
41 $xml = <<<'XML'
42 <?xml version="1.0" encoding="utf-8"?>
43 <!DOCTYPE foo [
44 <!ENTITY test "&a;&a;&a;&a;&a;&a;&a;&a;&a;&a;&a;&a;&a;&a;&a;&a;&a;&a;&a;&a;&a;&a;&a;">
45 <!ENTITY a "&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;">
46 <!ENTITY b "&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;">
47 <!ENTITY c "&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;">
48 <!ENTITY d "&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;">
49 <!ENTITY e "&f;&f;&f;&f;&f;&f;&f;&f;&f;&f;&f;&f;&f;&f;&f;&f;&f;&f;&f;&f;&f;&f;&f;&f;">
50 <!ENTITY f "&g;&g;&g;&g;&g;&g;&g;&g;&g;&g;&g;&g;&g;&g;&g;&g;&g;&g;&g;&g;&g;&g;&g;&g;">
51 <!ENTITY g "-00000000000000000000000000000000000000000000000000000000000000000000000-">
52 ]>
53 <foo>
54 <bar>&test;</bar>
55 </foo>
56 XML;
57 $check = XmlTypeCheck::newFromString( $xml );
58 $this->assertFalse( $check->wellFormed );
59 }
60
61 /**
62 * @covers XMLTypeCheck::processingInstructionHandler
63 */
64 public function testProcessingInstructionHandler() {
65 $called = false;
66 $testXML = new XmlTypeCheck(
67 self::XML_WITH_PIH,
68 null,
69 false,
70 [
71 'processing_instruction_handler' => function () use ( &$called ) {
72 $called = true;
73 }
74 ]
75 );
76 $this->assertTrue( $called );
77 }
78
79 }