Fixed incorrect size for api result
authorYuri Astrakhan <yurik@wikimedia.org>
Fri, 11 Dec 2015 22:45:14 +0000 (01:45 +0300)
committerReedy <reedy@wikimedia.org>
Sat, 12 Dec 2015 15:59:32 +0000 (15:59 +0000)
Added a unit test to highlight the problem

Change-Id: I5d4bcb755bd3686a92e7b111946a49892699729f

includes/api/ApiResult.php
tests/phpunit/includes/api/ApiResultTest.php

index e28cb82..bd5fe08 100644 (file)
@@ -1090,7 +1090,7 @@ class ApiResult implements ApiSerializable {
                $s = 0;
                if ( is_array( $value ) ) {
                        foreach ( $value as $k => $v ) {
-                               if ( !self::isMetadataKey( $s ) ) {
+                               if ( !self::isMetadataKey( $k ) ) {
                                        $s += self::valueSize( $v );
                                }
                        }
index d43db71..9dbde3d 100644 (file)
@@ -458,6 +458,13 @@ class ApiResultTest extends MediaWikiTestCase {
                        );
                }
 
+               // Add two values and some metadata, but ensure metadata is not counted
+               $result = new ApiResult( 100 );
+               $obj = array( 'attr' => '12345' );
+               ApiResult::setContentValue( $obj, 'content', '1234567890' );
+               $this->assertTrue( $result->addValue( null, 'foo', $obj ) );
+               $this->assertSame( 15, $result->getSize() );
+
                $result = new ApiResult( 10 );
                $formatter = new ApiErrorFormatter( $result, Language::factory( 'en' ), 'none', false );
                $result->setErrorFormatter( $formatter );