Daniel Kinzler has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/358915 )
Change subject: makeEmptyContent must return empty content. ...................................................................... makeEmptyContent must return empty content. This adds a sanity test that asserts that the Content object returned by ContentHandler::makeEmptyContent will return true from the isEmpty and isValid methods. Change-Id: Id3ed9fa1d684d737fe12e73ef20b6e336c7411a5 --- M includes/content/JsonContent.php M tests/phpunit/includes/content/JsonContentTest.php M tests/phpunit/structure/ContentHandlerSanityTest.php 3 files changed, 24 insertions(+), 10 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/15/358915/1 diff --git a/includes/content/JsonContent.php b/includes/content/JsonContent.php index 14c8182..4033369 100644 --- a/includes/content/JsonContent.php +++ b/includes/content/JsonContent.php @@ -61,6 +61,17 @@ } /** + * @return bool Whether content is an empty JSON object or array + */ + public function isEmpty() { + // NOTE: casting to array turns an empty stdClass instance into an empty array, + // but will turn an empty string, 0, or false into a non-empty array. + // This exactly matches the intended semantics of isEmpty for JSON. + $data = (array)$this->getData()->getValue(); + return empty( $data ); + } + + /** * Pretty-print JSON. * * If called before validation, it may return JSON "null". diff --git a/tests/phpunit/includes/content/JsonContentTest.php b/tests/phpunit/includes/content/JsonContentTest.php index de8e371..1349a00 100644 --- a/tests/phpunit/includes/content/JsonContentTest.php +++ b/tests/phpunit/includes/content/JsonContentTest.php @@ -8,23 +8,24 @@ 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' ] ], + [ 'foo', false, true, null ], + [ '[]', true, true, [] ], + [ '{}', true, true, (object)[] ], + [ '""', true, false, '' ], + [ '"0"', true, false, '0' ], + [ '"bar"', true, false, 'bar' ], + [ '0', true, false, '0' ], + [ '{ "0": "bar" }', true, false, (object)[ 'bar' ] ], ]; } /** * @dataProvider provideValidConstruction */ - public function testIsValid( $text, $isValid, $expected ) { + public function testIsValid( $text, $isValid, $isEmpty, $expected ) { $obj = new JsonContent( $text, CONTENT_MODEL_JSON ); - $this->assertEquals( $isValid, $obj->isValid() ); + $this->assertEquals( $isValid, $obj->isValid(), 'isValid()' ); + $this->assertEquals( $isEmpty, $obj->isEmpty(), 'isEmpty()' ); $this->assertEquals( $expected, $obj->getData()->getValue() ); } diff --git a/tests/phpunit/structure/ContentHandlerSanityTest.php b/tests/phpunit/structure/ContentHandlerSanityTest.php index 88257f9..d3dcf92 100644 --- a/tests/phpunit/structure/ContentHandlerSanityTest.php +++ b/tests/phpunit/structure/ContentHandlerSanityTest.php @@ -54,6 +54,8 @@ ); } else { $this->assertInstanceOf( Content::class, $content ); + $this->assertTrue( $content->isValid(), 'isValid()' ); + $this->assertTrue( $content->isEmpty(), 'isEmpty()' ); } } -- To view, visit https://gerrit.wikimedia.org/r/358915 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Id3ed9fa1d684d737fe12e73ef20b6e336c7411a5 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/core Gerrit-Branch: master Gerrit-Owner: Daniel Kinzler <daniel.kinz...@wikimedia.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits