JsonContentTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * @author Addshore
  4. * @covers JsonContent
  5. */
  6. class JsonContentTest extends MediaWikiLangTestCase {
  7. public static function provideValidConstruction() {
  8. return [
  9. [ 'foo', false, null ],
  10. [ '[]', true, [] ],
  11. [ '{}', true, (object)[] ],
  12. [ '""', true, '' ],
  13. [ '"0"', true, '0' ],
  14. [ '"bar"', true, 'bar' ],
  15. [ '0', true, '0' ],
  16. [ '{ "0": "bar" }', true, (object)[ 'bar' ] ],
  17. ];
  18. }
  19. /**
  20. * @dataProvider provideValidConstruction
  21. */
  22. public function testIsValid( $text, $isValid, $expected ) {
  23. $obj = new JsonContent( $text, CONTENT_MODEL_JSON );
  24. $this->assertEquals( $isValid, $obj->isValid() );
  25. $this->assertEquals( $expected, $obj->getData()->getValue() );
  26. }
  27. public static function provideDataToEncode() {
  28. return [
  29. [
  30. // Round-trip empty array
  31. '[]',
  32. '[]',
  33. ],
  34. [
  35. // Round-trip empty object
  36. '{}',
  37. '{}',
  38. ],
  39. [
  40. // Round-trip empty array/object (nested)
  41. '{ "foo": {}, "bar": [] }',
  42. "{\n \"foo\": {},\n \"bar\": []\n}",
  43. ],
  44. [
  45. '{ "foo": "bar" }',
  46. "{\n \"foo\": \"bar\"\n}",
  47. ],
  48. [
  49. '{ "foo": 1000 }',
  50. "{\n \"foo\": 1000\n}",
  51. ],
  52. [
  53. '{ "foo": 1000, "0": "bar" }',
  54. "{\n \"foo\": 1000,\n \"0\": \"bar\"\n}",
  55. ],
  56. ];
  57. }
  58. /**
  59. * @dataProvider provideDataToEncode
  60. */
  61. public function testBeautifyJson( $input, $beautified ) {
  62. $obj = new JsonContent( $input );
  63. $this->assertEquals( $beautified, $obj->beautifyJSON() );
  64. }
  65. /**
  66. * @dataProvider provideDataToEncode
  67. */
  68. public function testPreSaveTransform( $input, $transformed ) {
  69. $obj = new JsonContent( $input );
  70. $newObj = $obj->preSaveTransform(
  71. $this->getMockTitle(),
  72. $this->getMockUser(),
  73. $this->getMockParserOptions()
  74. );
  75. $this->assertTrue( $newObj->equals( new JsonContent( $transformed ) ) );
  76. }
  77. private function getMockTitle() {
  78. return $this->getMockBuilder( Title::class )
  79. ->disableOriginalConstructor()
  80. ->getMock();
  81. }
  82. private function getMockUser() {
  83. return $this->getMockBuilder( User::class )
  84. ->disableOriginalConstructor()
  85. ->getMock();
  86. }
  87. private function getMockParserOptions() {
  88. return $this->getMockBuilder( ParserOptions::class )
  89. ->disableOriginalConstructor()
  90. ->getMock();
  91. }
  92. public static function provideDataAndParserText() {
  93. return [
  94. [
  95. [],
  96. '<table class="mw-json"><tbody><tr><td>' .
  97. '<table class="mw-json"><tbody><tr><td class="mw-json-empty">Empty array</td></tr>'
  98. . '</tbody></table></td></tr></tbody></table>'
  99. ],
  100. [
  101. (object)[],
  102. '<table class="mw-json"><tbody><tr><td class="mw-json-empty">Empty object</td></tr>' .
  103. '</tbody></table>'
  104. ],
  105. [
  106. (object)[ 'foo' ],
  107. '<table class="mw-json"><tbody><tr><th>0</th><td class="value">"foo"</td></tr>' .
  108. '</tbody></table>'
  109. ],
  110. [
  111. (object)[ 'foo', 'bar' ],
  112. '<table class="mw-json"><tbody><tr><th>0</th><td class="value">"foo"</td></tr>' .
  113. '<tr><th>1</th><td class="value">"bar"</td></tr></tbody></table>'
  114. ],
  115. [
  116. (object)[ 'baz' => 'foo', 'bar' ],
  117. '<table class="mw-json"><tbody><tr><th>baz</th><td class="value">"foo"</td></tr>' .
  118. '<tr><th>0</th><td class="value">"bar"</td></tr></tbody></table>'
  119. ],
  120. [
  121. (object)[ 'baz' => 1000, 'bar' ],
  122. '<table class="mw-json"><tbody><tr><th>baz</th><td class="value">1000</td></tr>' .
  123. '<tr><th>0</th><td class="value">"bar"</td></tr></tbody></table>'
  124. ],
  125. [
  126. (object)[ '<script>alert("evil!")</script>' ],
  127. '<table class="mw-json"><tbody><tr><th>0</th><td class="value">"' .
  128. '&lt;script>alert("evil!")&lt;/script>"' .
  129. '</td></tr></tbody></table>',
  130. ],
  131. ];
  132. }
  133. /**
  134. * @dataProvider provideDataAndParserText
  135. */
  136. public function testFillParserOutput( $data, $expected ) {
  137. $obj = new JsonContent( FormatJson::encode( $data ) );
  138. $parserOutput = $obj->getParserOutput( $this->getMockTitle(), null, null, true );
  139. $this->assertInstanceOf( ParserOutput::class, $parserOutput );
  140. $this->assertEquals( $expected, $parserOutput->getText() );
  141. }
  142. }