wiki.techinc.nl/tests/phpunit/includes/content/JsonContentTest.php

153 lines
3.9 KiB
PHP
Raw Normal View History

<?php
/**
* @author Addshore
* @covers JsonContent
*/
class JsonContentTest extends MediaWikiLangTestCase {
public static function provideValidConstruction() {
return [
[ 'foo', false, null ],
[ '[]', true, [] ],
[ '{}', true, (object)[] ],
[ '""', true, '' ],
[ '"0"', true, '0' ],
[ '"bar"', true, 'bar' ],
[ '0', true, '0' ],
[ '{ "0": "bar" }', true, (object)[ 'bar' ] ],
];
}
/**
* @dataProvider provideValidConstruction
*/
content: Refactor and fix various bugs in JsonContent Follows-up d2a82fcb60. These issues weren't previously exposed as nothing uses JsonContent by default in core, and the extensions using it (e.g. EventLogging) lock it down very early. As are most of these methods were never really put to use (they were called after the extension does its superset of checking, or too early and WikiPage ignores it). Bug fixes * Empty JSON object was converted to an array by PST conversion. The beautifyJSON method is intended for prettify purposes but actually modified the content stored in the database and made it no longer roundtrip ({} != []). We can't change getJsonData to return an object since it's a public method and people use it as an array. So we can't cast it to a PHP object as that would break back-compat. Turns out the class doesn't even support non-objects anyway (a primitive in JSON can trivially cause a fatal as it wasn't consistently considered invalid, though it didn't actually fatal due to some lucky spaghetti code in WikiPage). * Fix beautifyJSON by checking for empty objects to prevent implicit {} to [] conversion. * Add isValid() check to fillParserOutput() as it's called early on. Otherwise it throws a warning that 'foreach' (in objectTable) iterates over null. In practice it doesn't matter since the entire parser output is rejected when WikiPage eventually checks isValid (through Content::prepareSave). * Consider all non- (PHP) array values invalid instead of just non-null values. Enhancements * Display message "Empty object" instead of a completely blank page for an empty object. * Display message "Empty object" or "Empty array" instead of an empty table cell. * Render arrays as a list of values (without indices). * Remove italics from table cells for values. The monospace font should be enough. It also offsets it from the "Empty" placeholders (which are italicised). Refactoring and clean up * Use FormatJson::parse so that we can use Status to distinguish between null parse result and thus reliably cache it. Ideally we wouldn't need to cache it, but right now this code is pulled apart and called in so many strange ways that we end up calling this several times. * Improve fairly meaningless test (testBeautifyJson) that was calling FormatJson in its data provider, exactly what the method being tested did. It also provided the test with data that could never end up in normal usage (a PHP-style associated array with implied numerical indices). * Document that this class rejects non-array values. * Document the problem with WikiPage assumming PST can run on any content. WikiPage fundamentally still assumes wikitext, in that there's no concept of invalid content. * Fix incorrect documentation for getJsonData's return value (It may return null.) * Fix incorrect documentation for beautifyJSON's return value. (It never returned boolean.) Bug: T76553 Change-Id: Ifed379ba4674a8289b554a95953951886bf2cbfd
2014-12-03 02:10:50 +00:00
public function testIsValid( $text, $isValid, $expected ) {
$obj = new JsonContent( $text, CONTENT_MODEL_JSON );
$this->assertEquals( $isValid, $obj->isValid() );
content: Refactor and fix various bugs in JsonContent Follows-up d2a82fcb60. These issues weren't previously exposed as nothing uses JsonContent by default in core, and the extensions using it (e.g. EventLogging) lock it down very early. As are most of these methods were never really put to use (they were called after the extension does its superset of checking, or too early and WikiPage ignores it). Bug fixes * Empty JSON object was converted to an array by PST conversion. The beautifyJSON method is intended for prettify purposes but actually modified the content stored in the database and made it no longer roundtrip ({} != []). We can't change getJsonData to return an object since it's a public method and people use it as an array. So we can't cast it to a PHP object as that would break back-compat. Turns out the class doesn't even support non-objects anyway (a primitive in JSON can trivially cause a fatal as it wasn't consistently considered invalid, though it didn't actually fatal due to some lucky spaghetti code in WikiPage). * Fix beautifyJSON by checking for empty objects to prevent implicit {} to [] conversion. * Add isValid() check to fillParserOutput() as it's called early on. Otherwise it throws a warning that 'foreach' (in objectTable) iterates over null. In practice it doesn't matter since the entire parser output is rejected when WikiPage eventually checks isValid (through Content::prepareSave). * Consider all non- (PHP) array values invalid instead of just non-null values. Enhancements * Display message "Empty object" instead of a completely blank page for an empty object. * Display message "Empty object" or "Empty array" instead of an empty table cell. * Render arrays as a list of values (without indices). * Remove italics from table cells for values. The monospace font should be enough. It also offsets it from the "Empty" placeholders (which are italicised). Refactoring and clean up * Use FormatJson::parse so that we can use Status to distinguish between null parse result and thus reliably cache it. Ideally we wouldn't need to cache it, but right now this code is pulled apart and called in so many strange ways that we end up calling this several times. * Improve fairly meaningless test (testBeautifyJson) that was calling FormatJson in its data provider, exactly what the method being tested did. It also provided the test with data that could never end up in normal usage (a PHP-style associated array with implied numerical indices). * Document that this class rejects non-array values. * Document the problem with WikiPage assumming PST can run on any content. WikiPage fundamentally still assumes wikitext, in that there's no concept of invalid content. * Fix incorrect documentation for getJsonData's return value (It may return null.) * Fix incorrect documentation for beautifyJSON's return value. (It never returned boolean.) Bug: T76553 Change-Id: Ifed379ba4674a8289b554a95953951886bf2cbfd
2014-12-03 02:10:50 +00:00
$this->assertEquals( $expected, $obj->getData()->getValue() );
}
public static function provideDataToEncode() {
return [
[
content: Refactor and fix various bugs in JsonContent Follows-up d2a82fcb60. These issues weren't previously exposed as nothing uses JsonContent by default in core, and the extensions using it (e.g. EventLogging) lock it down very early. As are most of these methods were never really put to use (they were called after the extension does its superset of checking, or too early and WikiPage ignores it). Bug fixes * Empty JSON object was converted to an array by PST conversion. The beautifyJSON method is intended for prettify purposes but actually modified the content stored in the database and made it no longer roundtrip ({} != []). We can't change getJsonData to return an object since it's a public method and people use it as an array. So we can't cast it to a PHP object as that would break back-compat. Turns out the class doesn't even support non-objects anyway (a primitive in JSON can trivially cause a fatal as it wasn't consistently considered invalid, though it didn't actually fatal due to some lucky spaghetti code in WikiPage). * Fix beautifyJSON by checking for empty objects to prevent implicit {} to [] conversion. * Add isValid() check to fillParserOutput() as it's called early on. Otherwise it throws a warning that 'foreach' (in objectTable) iterates over null. In practice it doesn't matter since the entire parser output is rejected when WikiPage eventually checks isValid (through Content::prepareSave). * Consider all non- (PHP) array values invalid instead of just non-null values. Enhancements * Display message "Empty object" instead of a completely blank page for an empty object. * Display message "Empty object" or "Empty array" instead of an empty table cell. * Render arrays as a list of values (without indices). * Remove italics from table cells for values. The monospace font should be enough. It also offsets it from the "Empty" placeholders (which are italicised). Refactoring and clean up * Use FormatJson::parse so that we can use Status to distinguish between null parse result and thus reliably cache it. Ideally we wouldn't need to cache it, but right now this code is pulled apart and called in so many strange ways that we end up calling this several times. * Improve fairly meaningless test (testBeautifyJson) that was calling FormatJson in its data provider, exactly what the method being tested did. It also provided the test with data that could never end up in normal usage (a PHP-style associated array with implied numerical indices). * Document that this class rejects non-array values. * Document the problem with WikiPage assumming PST can run on any content. WikiPage fundamentally still assumes wikitext, in that there's no concept of invalid content. * Fix incorrect documentation for getJsonData's return value (It may return null.) * Fix incorrect documentation for beautifyJSON's return value. (It never returned boolean.) Bug: T76553 Change-Id: Ifed379ba4674a8289b554a95953951886bf2cbfd
2014-12-03 02:10:50 +00:00
// Round-trip empty array
'[]',
'[]',
],
[
content: Refactor and fix various bugs in JsonContent Follows-up d2a82fcb60. These issues weren't previously exposed as nothing uses JsonContent by default in core, and the extensions using it (e.g. EventLogging) lock it down very early. As are most of these methods were never really put to use (they were called after the extension does its superset of checking, or too early and WikiPage ignores it). Bug fixes * Empty JSON object was converted to an array by PST conversion. The beautifyJSON method is intended for prettify purposes but actually modified the content stored in the database and made it no longer roundtrip ({} != []). We can't change getJsonData to return an object since it's a public method and people use it as an array. So we can't cast it to a PHP object as that would break back-compat. Turns out the class doesn't even support non-objects anyway (a primitive in JSON can trivially cause a fatal as it wasn't consistently considered invalid, though it didn't actually fatal due to some lucky spaghetti code in WikiPage). * Fix beautifyJSON by checking for empty objects to prevent implicit {} to [] conversion. * Add isValid() check to fillParserOutput() as it's called early on. Otherwise it throws a warning that 'foreach' (in objectTable) iterates over null. In practice it doesn't matter since the entire parser output is rejected when WikiPage eventually checks isValid (through Content::prepareSave). * Consider all non- (PHP) array values invalid instead of just non-null values. Enhancements * Display message "Empty object" instead of a completely blank page for an empty object. * Display message "Empty object" or "Empty array" instead of an empty table cell. * Render arrays as a list of values (without indices). * Remove italics from table cells for values. The monospace font should be enough. It also offsets it from the "Empty" placeholders (which are italicised). Refactoring and clean up * Use FormatJson::parse so that we can use Status to distinguish between null parse result and thus reliably cache it. Ideally we wouldn't need to cache it, but right now this code is pulled apart and called in so many strange ways that we end up calling this several times. * Improve fairly meaningless test (testBeautifyJson) that was calling FormatJson in its data provider, exactly what the method being tested did. It also provided the test with data that could never end up in normal usage (a PHP-style associated array with implied numerical indices). * Document that this class rejects non-array values. * Document the problem with WikiPage assumming PST can run on any content. WikiPage fundamentally still assumes wikitext, in that there's no concept of invalid content. * Fix incorrect documentation for getJsonData's return value (It may return null.) * Fix incorrect documentation for beautifyJSON's return value. (It never returned boolean.) Bug: T76553 Change-Id: Ifed379ba4674a8289b554a95953951886bf2cbfd
2014-12-03 02:10:50 +00:00
// Round-trip empty object
'{}',
'{}',
],
[
content: Refactor and fix various bugs in JsonContent Follows-up d2a82fcb60. These issues weren't previously exposed as nothing uses JsonContent by default in core, and the extensions using it (e.g. EventLogging) lock it down very early. As are most of these methods were never really put to use (they were called after the extension does its superset of checking, or too early and WikiPage ignores it). Bug fixes * Empty JSON object was converted to an array by PST conversion. The beautifyJSON method is intended for prettify purposes but actually modified the content stored in the database and made it no longer roundtrip ({} != []). We can't change getJsonData to return an object since it's a public method and people use it as an array. So we can't cast it to a PHP object as that would break back-compat. Turns out the class doesn't even support non-objects anyway (a primitive in JSON can trivially cause a fatal as it wasn't consistently considered invalid, though it didn't actually fatal due to some lucky spaghetti code in WikiPage). * Fix beautifyJSON by checking for empty objects to prevent implicit {} to [] conversion. * Add isValid() check to fillParserOutput() as it's called early on. Otherwise it throws a warning that 'foreach' (in objectTable) iterates over null. In practice it doesn't matter since the entire parser output is rejected when WikiPage eventually checks isValid (through Content::prepareSave). * Consider all non- (PHP) array values invalid instead of just non-null values. Enhancements * Display message "Empty object" instead of a completely blank page for an empty object. * Display message "Empty object" or "Empty array" instead of an empty table cell. * Render arrays as a list of values (without indices). * Remove italics from table cells for values. The monospace font should be enough. It also offsets it from the "Empty" placeholders (which are italicised). Refactoring and clean up * Use FormatJson::parse so that we can use Status to distinguish between null parse result and thus reliably cache it. Ideally we wouldn't need to cache it, but right now this code is pulled apart and called in so many strange ways that we end up calling this several times. * Improve fairly meaningless test (testBeautifyJson) that was calling FormatJson in its data provider, exactly what the method being tested did. It also provided the test with data that could never end up in normal usage (a PHP-style associated array with implied numerical indices). * Document that this class rejects non-array values. * Document the problem with WikiPage assumming PST can run on any content. WikiPage fundamentally still assumes wikitext, in that there's no concept of invalid content. * Fix incorrect documentation for getJsonData's return value (It may return null.) * Fix incorrect documentation for beautifyJSON's return value. (It never returned boolean.) Bug: T76553 Change-Id: Ifed379ba4674a8289b554a95953951886bf2cbfd
2014-12-03 02:10:50 +00:00
// Round-trip empty array/object (nested)
'{ "foo": {}, "bar": [] }',
"{\n \"foo\": {},\n \"bar\": []\n}",
],
[
content: Refactor and fix various bugs in JsonContent Follows-up d2a82fcb60. These issues weren't previously exposed as nothing uses JsonContent by default in core, and the extensions using it (e.g. EventLogging) lock it down very early. As are most of these methods were never really put to use (they were called after the extension does its superset of checking, or too early and WikiPage ignores it). Bug fixes * Empty JSON object was converted to an array by PST conversion. The beautifyJSON method is intended for prettify purposes but actually modified the content stored in the database and made it no longer roundtrip ({} != []). We can't change getJsonData to return an object since it's a public method and people use it as an array. So we can't cast it to a PHP object as that would break back-compat. Turns out the class doesn't even support non-objects anyway (a primitive in JSON can trivially cause a fatal as it wasn't consistently considered invalid, though it didn't actually fatal due to some lucky spaghetti code in WikiPage). * Fix beautifyJSON by checking for empty objects to prevent implicit {} to [] conversion. * Add isValid() check to fillParserOutput() as it's called early on. Otherwise it throws a warning that 'foreach' (in objectTable) iterates over null. In practice it doesn't matter since the entire parser output is rejected when WikiPage eventually checks isValid (through Content::prepareSave). * Consider all non- (PHP) array values invalid instead of just non-null values. Enhancements * Display message "Empty object" instead of a completely blank page for an empty object. * Display message "Empty object" or "Empty array" instead of an empty table cell. * Render arrays as a list of values (without indices). * Remove italics from table cells for values. The monospace font should be enough. It also offsets it from the "Empty" placeholders (which are italicised). Refactoring and clean up * Use FormatJson::parse so that we can use Status to distinguish between null parse result and thus reliably cache it. Ideally we wouldn't need to cache it, but right now this code is pulled apart and called in so many strange ways that we end up calling this several times. * Improve fairly meaningless test (testBeautifyJson) that was calling FormatJson in its data provider, exactly what the method being tested did. It also provided the test with data that could never end up in normal usage (a PHP-style associated array with implied numerical indices). * Document that this class rejects non-array values. * Document the problem with WikiPage assumming PST can run on any content. WikiPage fundamentally still assumes wikitext, in that there's no concept of invalid content. * Fix incorrect documentation for getJsonData's return value (It may return null.) * Fix incorrect documentation for beautifyJSON's return value. (It never returned boolean.) Bug: T76553 Change-Id: Ifed379ba4674a8289b554a95953951886bf2cbfd
2014-12-03 02:10:50 +00:00
'{ "foo": "bar" }',
"{\n \"foo\": \"bar\"\n}",
],
[
content: Refactor and fix various bugs in JsonContent Follows-up d2a82fcb60. These issues weren't previously exposed as nothing uses JsonContent by default in core, and the extensions using it (e.g. EventLogging) lock it down very early. As are most of these methods were never really put to use (they were called after the extension does its superset of checking, or too early and WikiPage ignores it). Bug fixes * Empty JSON object was converted to an array by PST conversion. The beautifyJSON method is intended for prettify purposes but actually modified the content stored in the database and made it no longer roundtrip ({} != []). We can't change getJsonData to return an object since it's a public method and people use it as an array. So we can't cast it to a PHP object as that would break back-compat. Turns out the class doesn't even support non-objects anyway (a primitive in JSON can trivially cause a fatal as it wasn't consistently considered invalid, though it didn't actually fatal due to some lucky spaghetti code in WikiPage). * Fix beautifyJSON by checking for empty objects to prevent implicit {} to [] conversion. * Add isValid() check to fillParserOutput() as it's called early on. Otherwise it throws a warning that 'foreach' (in objectTable) iterates over null. In practice it doesn't matter since the entire parser output is rejected when WikiPage eventually checks isValid (through Content::prepareSave). * Consider all non- (PHP) array values invalid instead of just non-null values. Enhancements * Display message "Empty object" instead of a completely blank page for an empty object. * Display message "Empty object" or "Empty array" instead of an empty table cell. * Render arrays as a list of values (without indices). * Remove italics from table cells for values. The monospace font should be enough. It also offsets it from the "Empty" placeholders (which are italicised). Refactoring and clean up * Use FormatJson::parse so that we can use Status to distinguish between null parse result and thus reliably cache it. Ideally we wouldn't need to cache it, but right now this code is pulled apart and called in so many strange ways that we end up calling this several times. * Improve fairly meaningless test (testBeautifyJson) that was calling FormatJson in its data provider, exactly what the method being tested did. It also provided the test with data that could never end up in normal usage (a PHP-style associated array with implied numerical indices). * Document that this class rejects non-array values. * Document the problem with WikiPage assumming PST can run on any content. WikiPage fundamentally still assumes wikitext, in that there's no concept of invalid content. * Fix incorrect documentation for getJsonData's return value (It may return null.) * Fix incorrect documentation for beautifyJSON's return value. (It never returned boolean.) Bug: T76553 Change-Id: Ifed379ba4674a8289b554a95953951886bf2cbfd
2014-12-03 02:10:50 +00:00
'{ "foo": 1000 }',
"{\n \"foo\": 1000\n}",
],
[
content: Refactor and fix various bugs in JsonContent Follows-up d2a82fcb60. These issues weren't previously exposed as nothing uses JsonContent by default in core, and the extensions using it (e.g. EventLogging) lock it down very early. As are most of these methods were never really put to use (they were called after the extension does its superset of checking, or too early and WikiPage ignores it). Bug fixes * Empty JSON object was converted to an array by PST conversion. The beautifyJSON method is intended for prettify purposes but actually modified the content stored in the database and made it no longer roundtrip ({} != []). We can't change getJsonData to return an object since it's a public method and people use it as an array. So we can't cast it to a PHP object as that would break back-compat. Turns out the class doesn't even support non-objects anyway (a primitive in JSON can trivially cause a fatal as it wasn't consistently considered invalid, though it didn't actually fatal due to some lucky spaghetti code in WikiPage). * Fix beautifyJSON by checking for empty objects to prevent implicit {} to [] conversion. * Add isValid() check to fillParserOutput() as it's called early on. Otherwise it throws a warning that 'foreach' (in objectTable) iterates over null. In practice it doesn't matter since the entire parser output is rejected when WikiPage eventually checks isValid (through Content::prepareSave). * Consider all non- (PHP) array values invalid instead of just non-null values. Enhancements * Display message "Empty object" instead of a completely blank page for an empty object. * Display message "Empty object" or "Empty array" instead of an empty table cell. * Render arrays as a list of values (without indices). * Remove italics from table cells for values. The monospace font should be enough. It also offsets it from the "Empty" placeholders (which are italicised). Refactoring and clean up * Use FormatJson::parse so that we can use Status to distinguish between null parse result and thus reliably cache it. Ideally we wouldn't need to cache it, but right now this code is pulled apart and called in so many strange ways that we end up calling this several times. * Improve fairly meaningless test (testBeautifyJson) that was calling FormatJson in its data provider, exactly what the method being tested did. It also provided the test with data that could never end up in normal usage (a PHP-style associated array with implied numerical indices). * Document that this class rejects non-array values. * Document the problem with WikiPage assumming PST can run on any content. WikiPage fundamentally still assumes wikitext, in that there's no concept of invalid content. * Fix incorrect documentation for getJsonData's return value (It may return null.) * Fix incorrect documentation for beautifyJSON's return value. (It never returned boolean.) Bug: T76553 Change-Id: Ifed379ba4674a8289b554a95953951886bf2cbfd
2014-12-03 02:10:50 +00:00
'{ "foo": 1000, "0": "bar" }',
"{\n \"foo\": 1000,\n \"0\": \"bar\"\n}",
],
];
}
/**
* @dataProvider provideDataToEncode
*/
content: Refactor and fix various bugs in JsonContent Follows-up d2a82fcb60. These issues weren't previously exposed as nothing uses JsonContent by default in core, and the extensions using it (e.g. EventLogging) lock it down very early. As are most of these methods were never really put to use (they were called after the extension does its superset of checking, or too early and WikiPage ignores it). Bug fixes * Empty JSON object was converted to an array by PST conversion. The beautifyJSON method is intended for prettify purposes but actually modified the content stored in the database and made it no longer roundtrip ({} != []). We can't change getJsonData to return an object since it's a public method and people use it as an array. So we can't cast it to a PHP object as that would break back-compat. Turns out the class doesn't even support non-objects anyway (a primitive in JSON can trivially cause a fatal as it wasn't consistently considered invalid, though it didn't actually fatal due to some lucky spaghetti code in WikiPage). * Fix beautifyJSON by checking for empty objects to prevent implicit {} to [] conversion. * Add isValid() check to fillParserOutput() as it's called early on. Otherwise it throws a warning that 'foreach' (in objectTable) iterates over null. In practice it doesn't matter since the entire parser output is rejected when WikiPage eventually checks isValid (through Content::prepareSave). * Consider all non- (PHP) array values invalid instead of just non-null values. Enhancements * Display message "Empty object" instead of a completely blank page for an empty object. * Display message "Empty object" or "Empty array" instead of an empty table cell. * Render arrays as a list of values (without indices). * Remove italics from table cells for values. The monospace font should be enough. It also offsets it from the "Empty" placeholders (which are italicised). Refactoring and clean up * Use FormatJson::parse so that we can use Status to distinguish between null parse result and thus reliably cache it. Ideally we wouldn't need to cache it, but right now this code is pulled apart and called in so many strange ways that we end up calling this several times. * Improve fairly meaningless test (testBeautifyJson) that was calling FormatJson in its data provider, exactly what the method being tested did. It also provided the test with data that could never end up in normal usage (a PHP-style associated array with implied numerical indices). * Document that this class rejects non-array values. * Document the problem with WikiPage assumming PST can run on any content. WikiPage fundamentally still assumes wikitext, in that there's no concept of invalid content. * Fix incorrect documentation for getJsonData's return value (It may return null.) * Fix incorrect documentation for beautifyJSON's return value. (It never returned boolean.) Bug: T76553 Change-Id: Ifed379ba4674a8289b554a95953951886bf2cbfd
2014-12-03 02:10:50 +00:00
public function testBeautifyJson( $input, $beautified ) {
$obj = new JsonContent( $input );
$this->assertEquals( $beautified, $obj->beautifyJSON() );
}
/**
* @dataProvider provideDataToEncode
*/
content: Refactor and fix various bugs in JsonContent Follows-up d2a82fcb60. These issues weren't previously exposed as nothing uses JsonContent by default in core, and the extensions using it (e.g. EventLogging) lock it down very early. As are most of these methods were never really put to use (they were called after the extension does its superset of checking, or too early and WikiPage ignores it). Bug fixes * Empty JSON object was converted to an array by PST conversion. The beautifyJSON method is intended for prettify purposes but actually modified the content stored in the database and made it no longer roundtrip ({} != []). We can't change getJsonData to return an object since it's a public method and people use it as an array. So we can't cast it to a PHP object as that would break back-compat. Turns out the class doesn't even support non-objects anyway (a primitive in JSON can trivially cause a fatal as it wasn't consistently considered invalid, though it didn't actually fatal due to some lucky spaghetti code in WikiPage). * Fix beautifyJSON by checking for empty objects to prevent implicit {} to [] conversion. * Add isValid() check to fillParserOutput() as it's called early on. Otherwise it throws a warning that 'foreach' (in objectTable) iterates over null. In practice it doesn't matter since the entire parser output is rejected when WikiPage eventually checks isValid (through Content::prepareSave). * Consider all non- (PHP) array values invalid instead of just non-null values. Enhancements * Display message "Empty object" instead of a completely blank page for an empty object. * Display message "Empty object" or "Empty array" instead of an empty table cell. * Render arrays as a list of values (without indices). * Remove italics from table cells for values. The monospace font should be enough. It also offsets it from the "Empty" placeholders (which are italicised). Refactoring and clean up * Use FormatJson::parse so that we can use Status to distinguish between null parse result and thus reliably cache it. Ideally we wouldn't need to cache it, but right now this code is pulled apart and called in so many strange ways that we end up calling this several times. * Improve fairly meaningless test (testBeautifyJson) that was calling FormatJson in its data provider, exactly what the method being tested did. It also provided the test with data that could never end up in normal usage (a PHP-style associated array with implied numerical indices). * Document that this class rejects non-array values. * Document the problem with WikiPage assumming PST can run on any content. WikiPage fundamentally still assumes wikitext, in that there's no concept of invalid content. * Fix incorrect documentation for getJsonData's return value (It may return null.) * Fix incorrect documentation for beautifyJSON's return value. (It never returned boolean.) Bug: T76553 Change-Id: Ifed379ba4674a8289b554a95953951886bf2cbfd
2014-12-03 02:10:50 +00:00
public function testPreSaveTransform( $input, $transformed ) {
$obj = new JsonContent( $input );
$newObj = $obj->preSaveTransform(
$this->getMockTitle(),
$this->getMockUser(),
$this->getMockParserOptions()
);
content: Refactor and fix various bugs in JsonContent Follows-up d2a82fcb60. These issues weren't previously exposed as nothing uses JsonContent by default in core, and the extensions using it (e.g. EventLogging) lock it down very early. As are most of these methods were never really put to use (they were called after the extension does its superset of checking, or too early and WikiPage ignores it). Bug fixes * Empty JSON object was converted to an array by PST conversion. The beautifyJSON method is intended for prettify purposes but actually modified the content stored in the database and made it no longer roundtrip ({} != []). We can't change getJsonData to return an object since it's a public method and people use it as an array. So we can't cast it to a PHP object as that would break back-compat. Turns out the class doesn't even support non-objects anyway (a primitive in JSON can trivially cause a fatal as it wasn't consistently considered invalid, though it didn't actually fatal due to some lucky spaghetti code in WikiPage). * Fix beautifyJSON by checking for empty objects to prevent implicit {} to [] conversion. * Add isValid() check to fillParserOutput() as it's called early on. Otherwise it throws a warning that 'foreach' (in objectTable) iterates over null. In practice it doesn't matter since the entire parser output is rejected when WikiPage eventually checks isValid (through Content::prepareSave). * Consider all non- (PHP) array values invalid instead of just non-null values. Enhancements * Display message "Empty object" instead of a completely blank page for an empty object. * Display message "Empty object" or "Empty array" instead of an empty table cell. * Render arrays as a list of values (without indices). * Remove italics from table cells for values. The monospace font should be enough. It also offsets it from the "Empty" placeholders (which are italicised). Refactoring and clean up * Use FormatJson::parse so that we can use Status to distinguish between null parse result and thus reliably cache it. Ideally we wouldn't need to cache it, but right now this code is pulled apart and called in so many strange ways that we end up calling this several times. * Improve fairly meaningless test (testBeautifyJson) that was calling FormatJson in its data provider, exactly what the method being tested did. It also provided the test with data that could never end up in normal usage (a PHP-style associated array with implied numerical indices). * Document that this class rejects non-array values. * Document the problem with WikiPage assumming PST can run on any content. WikiPage fundamentally still assumes wikitext, in that there's no concept of invalid content. * Fix incorrect documentation for getJsonData's return value (It may return null.) * Fix incorrect documentation for beautifyJSON's return value. (It never returned boolean.) Bug: T76553 Change-Id: Ifed379ba4674a8289b554a95953951886bf2cbfd
2014-12-03 02:10:50 +00:00
$this->assertTrue( $newObj->equals( new JsonContent( $transformed ) ) );
}
private function getMockTitle() {
return $this->getMockBuilder( Title::class )
->disableOriginalConstructor()
->getMock();
}
private function getMockUser() {
return $this->getMockBuilder( User::class )
->disableOriginalConstructor()
->getMock();
}
private function getMockParserOptions() {
return $this->getMockBuilder( ParserOptions::class )
->disableOriginalConstructor()
->getMock();
}
public static function provideDataAndParserText() {
return [
[
[],
'<table class="mw-json"><tbody><tr><td>' .
'<table class="mw-json"><tbody><tr><td class="mw-json-empty">Empty array</td></tr>'
. '</tbody></table></td></tr></tbody></table>'
],
[
(object)[],
'<table class="mw-json"><tbody><tr><td class="mw-json-empty">Empty object</td></tr>' .
'</tbody></table>'
],
[
(object)[ 'foo' ],
'<table class="mw-json"><tbody><tr><th>0</th><td class="value">"foo"</td></tr>' .
'</tbody></table>'
],
[
(object)[ 'foo', 'bar' ],
'<table class="mw-json"><tbody><tr><th>0</th><td class="value">"foo"</td></tr>' .
'<tr><th>1</th><td class="value">"bar"</td></tr></tbody></table>'
],
[
(object)[ 'baz' => 'foo', 'bar' ],
'<table class="mw-json"><tbody><tr><th>baz</th><td class="value">"foo"</td></tr>' .
'<tr><th>0</th><td class="value">"bar"</td></tr></tbody></table>'
],
[
(object)[ 'baz' => 1000, 'bar' ],
'<table class="mw-json"><tbody><tr><th>baz</th><td class="value">1000</td></tr>' .
'<tr><th>0</th><td class="value">"bar"</td></tr></tbody></table>'
],
[
(object)[ '<script>alert("evil!")</script>' ],
'<table class="mw-json"><tbody><tr><th>0</th><td class="value">"' .
'&lt;script>alert("evil!")&lt;/script>"' .
'</td></tr></tbody></table>',
],
];
}
/**
* @dataProvider provideDataAndParserText
*/
public function testFillParserOutput( $data, $expected ) {
$obj = new JsonContent( FormatJson::encode( $data ) );
$parserOutput = $obj->getParserOutput( $this->getMockTitle(), null, null, true );
$this->assertInstanceOf( ParserOutput::class, $parserOutput );
$this->assertEquals( $expected, $parserOutput->getText() );
}
}