summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/content/JsonContentTest.php
blob: 77b542f4b32823f871228598cb2659372a696025 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php

/**
 * @author Adam Shorland
 * @covers JsonContent
 */
class JsonContentTest extends MediaWikiLangTestCase {

	/**
	 * @dataProvider provideValidConstruction
	 */
	public function testValidConstruct( $text, $modelId, $isValid, $expected ) {
		$obj = new JsonContent( $text, $modelId );
		$this->assertEquals( $isValid, $obj->isValid() );
		$this->assertEquals( $expected, $obj->getJsonData() );
	}

	public static function provideValidConstruction() {
		return array(
			array( 'foo', CONTENT_MODEL_JSON, false, null ),
			array( FormatJson::encode( array() ), CONTENT_MODEL_JSON, true, array() ),
			array( FormatJson::encode( array( 'foo' ) ), CONTENT_MODEL_JSON, true, array( 'foo' ) ),
		);
	}

	/**
	 * @dataProvider provideDataToEncode
	 */
	public function testBeautifyUsesFormatJson( $data ) {
		$obj = new JsonContent( FormatJson::encode( $data ) );
		$this->assertEquals( FormatJson::encode( $data, true ), $obj->beautifyJSON() );
	}

	public static function provideDataToEncode() {
		return array(
			array( array() ),
			array( array( 'foo' ) ),
			array( array( 'foo', 'bar' ) ),
			array( array( 'baz' => 'foo', 'bar' ) ),
			array( array( 'baz' => 1000, 'bar' ) ),
		);
	}

	/**
	 * @dataProvider provideDataToEncode
	 */
	public function testPreSaveTransform( $data ) {
		$obj = new JsonContent( FormatJson::encode( $data ) );
		$newObj = $obj->preSaveTransform( $this->getMockTitle(), $this->getMockUser(), $this->getMockParserOptions() );
		$this->assertTrue( $newObj->equals( new JsonContent( FormatJson::encode( $data, true ) ) ) );
	}

	private function getMockTitle() {
		return $this->getMockBuilder( 'Title' )
			->disableOriginalConstructor()
			->getMock();
	}

	private function getMockUser() {
		return $this->getMockBuilder( 'User' )
			->disableOriginalConstructor()
			->getMock();
	}
	private function getMockParserOptions() {
		return $this->getMockBuilder( 'ParserOptions' )
			->disableOriginalConstructor()
			->getMock();
	}

	/**
	 * @dataProvider provideDataAndParserText
	 */
	public function testFillParserOutput( $data, $expected ) {
		$obj = new JsonContent( FormatJson::encode( $data ) );
		$parserOutput = $obj->getParserOutput( $this->getMockTitle(), null, null, true );
		$this->assertInstanceOf( 'ParserOutput', $parserOutput );
		$this->assertEquals( $expected, $parserOutput->getText() );
	}

	public static function provideDataAndParserText() {
		return array(
			array(
				array(),
				'<table class="mw-json"><tbody></tbody></table>'
			),
			array(
				array( 'foo' ),
				'<table class="mw-json"><tbody><tr><th>0</th><td class="value">&quot;foo&quot;</td></tr></tbody></table>'
			),
			array(
				array( 'foo', 'bar' ),
				'<table class="mw-json"><tbody><tr><th>0</th><td class="value">&quot;foo&quot;</td></tr>' .
				"\n" .
				'<tr><th>1</th><td class="value">&quot;bar&quot;</td></tr></tbody></table>'
			),
			array(
				array( 'baz' => 'foo', 'bar' ),
				'<table class="mw-json"><tbody><tr><th>baz</th><td class="value">&quot;foo&quot;</td></tr>' .
				"\n" .
				'<tr><th>0</th><td class="value">&quot;bar&quot;</td></tr></tbody></table>'
			),
			array(
				array( 'baz' => 1000, 'bar' ),
				'<table class="mw-json"><tbody><tr><th>baz</th><td class="value">1000</td></tr>' .
				"\n" .
				'<tr><th>0</th><td class="value">&quot;bar&quot;</td></tr></tbody></table>'
			),
			array(
				array( '<script>alert("evil!")</script>'),
				'<table class="mw-json"><tbody><tr><th>0</th><td class="value">&quot;&lt;script&gt;alert(&quot;evil!&quot;)&lt;/script&gt;&quot;</td></tr></tbody></table>',
			),
		);
	}
}