Merge "convertExtensionToRegistration: Put some keys (name, version, etc.) on top"
[lhc/web/wiklou.git] / tests / phpunit / includes / parser / ParserOutputTest.php
1 <?php
2
3 /**
4 * @group Database
5 * ^--- trigger DB shadowing because we are using Title magic
6 */
7 class ParserOutputTest extends MediaWikiTestCase {
8
9 public static function provideIsLinkInternal() {
10 return array(
11 // Different domains
12 array( false, 'http://example.org', 'http://mediawiki.org' ),
13 // Same domains
14 array( true, 'http://example.org', 'http://example.org' ),
15 array( true, 'https://example.org', 'https://example.org' ),
16 array( true, '//example.org', '//example.org' ),
17 // Same domain different cases
18 array( true, 'http://example.org', 'http://EXAMPLE.ORG' ),
19 // Paths, queries, and fragments are not relevant
20 array( true, 'http://example.org', 'http://example.org/wiki/Main_Page' ),
21 array( true, 'http://example.org', 'http://example.org?my=query' ),
22 array( true, 'http://example.org', 'http://example.org#its-a-fragment' ),
23 // Different protocols
24 array( false, 'http://example.org', 'https://example.org' ),
25 array( false, 'https://example.org', 'http://example.org' ),
26 // Protocol relative servers always match http and https links
27 array( true, '//example.org', 'http://example.org' ),
28 array( true, '//example.org', 'https://example.org' ),
29 // But they don't match strange things like this
30 array( false, '//example.org', 'irc://example.org' ),
31 );
32 }
33
34 /**
35 * Test to make sure ParserOutput::isLinkInternal behaves properly
36 * @dataProvider provideIsLinkInternal
37 * @covers ParserOutput::isLinkInternal
38 */
39 public function testIsLinkInternal( $shouldMatch, $server, $url ) {
40 $this->assertEquals( $shouldMatch, ParserOutput::isLinkInternal( $server, $url ) );
41 }
42
43 /**
44 * @covers ParserOutput::setExtensionData
45 * @covers ParserOutput::getExtensionData
46 */
47 public function testExtensionData() {
48 $po = new ParserOutput();
49
50 $po->setExtensionData( "one", "Foo" );
51
52 $this->assertEquals( "Foo", $po->getExtensionData( "one" ) );
53 $this->assertNull( $po->getExtensionData( "spam" ) );
54
55 $po->setExtensionData( "two", "Bar" );
56 $this->assertEquals( "Foo", $po->getExtensionData( "one" ) );
57 $this->assertEquals( "Bar", $po->getExtensionData( "two" ) );
58
59 $po->setExtensionData( "one", null );
60 $this->assertNull( $po->getExtensionData( "one" ) );
61 $this->assertEquals( "Bar", $po->getExtensionData( "two" ) );
62 }
63
64 /**
65 * @covers ParserOutput::setProperty
66 * @covers ParserOutput::getProperty
67 * @covers ParserOutput::unsetProperty
68 * @covers ParserOutput::getProperties
69 */
70 public function testProperties() {
71 $po = new ParserOutput();
72
73 $po->setProperty( 'foo', 'val' );
74
75 $properties = $po->getProperties();
76 $this->assertEquals( $po->getProperty( 'foo' ), 'val' );
77 $this->assertEquals( $properties['foo'], 'val' );
78
79 $po->setProperty( 'foo', 'second val' );
80
81 $properties = $po->getProperties();
82 $this->assertEquals( $po->getProperty( 'foo' ), 'second val' );
83 $this->assertEquals( $properties['foo'], 'second val' );
84
85 $po->unsetProperty( 'foo' );
86
87 $properties = $po->getProperties();
88 $this->assertEquals( $po->getProperty( 'foo' ), false );
89 $this->assertArrayNotHasKey( 'foo', $properties );
90 }
91
92 /**
93 * @covers ParserOutput::hasCustomDataUpdates
94 * @covers ParserOutput::addSecondaryDataUpdate
95 */
96 public function testHasCustomDataUpdates() {
97 $po = new ParserOutput();
98 $this->assertFalse( $po->hasCustomDataUpdates() );
99
100 $dataUpdate = $this->getMock( 'DataUpdate' );
101 $po->addSecondaryDataUpdate( $dataUpdate );
102 $this->assertTrue( $po->hasCustomDataUpdates() );
103 }
104
105 /**
106 * @covers ParserOutput::getSecondaryDataUpdates
107 * @covers ParserOutput::addSecondaryDataUpdate
108 */
109 public function testGetSecondaryDataUpdates() {
110 // NOTE: getSecondaryDataUpdates always returns a LinksUpdate object
111 // in addition to the DataUpdates registered via addSecondaryDataUpdate().
112
113 $title = Title::makeTitle( NS_MAIN, 'Dummy' );
114 $title->resetArticleID( 7777777 );
115
116 $po = new ParserOutput();
117 $this->assertCount( 1, $po->getSecondaryDataUpdates( $title ) );
118
119 $dataUpdate = $this->getMock( 'DataUpdate' );
120 $po->addSecondaryDataUpdate( $dataUpdate );
121 $this->assertCount( 2, $po->getSecondaryDataUpdates( $title ) );
122
123 // Test Fallback to getTitleText
124 $this->insertPage( 'Project:ParserOutputTestDummyPage' );
125 $po->setTitleText( 'Project:ParserOutputTestDummyPage' );
126 $this->assertCount( 2, $po->getSecondaryDataUpdates() );
127 }
128
129 /**
130 * @covers ParserOutput::getSecondaryDataUpdates
131 * @covers ParserOutput::__sleep
132 */
133 public function testGetSecondaryDataUpdates_serialization() {
134 $title = Title::makeTitle( NS_MAIN, 'Dummy' );
135 $title->resetArticleID( 7777777 );
136
137 $po = new ParserOutput();
138
139 // Serializing is fine with no custom DataUpdates.
140 $po = unserialize( serialize( $po ) );
141 $this->assertCount( 1, $po->getSecondaryDataUpdates( $title ) );
142
143 // If there are custom DataUpdates, getSecondaryDataUpdates
144 // should fail after serialization.
145 $dataUpdate = $this->getMock( 'DataUpdate' );
146 $po->addSecondaryDataUpdate( $dataUpdate );
147 $po = unserialize( serialize( $po ) );
148
149 $this->setExpectedException( 'MWException' );
150 $po->getSecondaryDataUpdates( $title );
151 }
152
153 }