jenkins-bot has submitted this change and it was merged.

Change subject: API: ApiResult must validate even when using numeric 
auto-indexes
......................................................................


API: ApiResult must validate even when using numeric auto-indexes

Bug: T97490
Change-Id: I5301a615a992b090000a59f86e13b9f78dcd5aec
---
M includes/api/ApiResult.php
M tests/phpunit/includes/api/ApiResultTest.php
2 files changed, 94 insertions(+), 4 deletions(-)

Approvals:
  Anomie: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/api/ApiResult.php b/includes/api/ApiResult.php
index 345cf43..044b6e0 100644
--- a/includes/api/ApiResult.php
+++ b/includes/api/ApiResult.php
@@ -276,6 +276,10 @@
         * @param int $flags Zero or more OR-ed flags like OVERRIDE | 
ADD_ON_TOP.
         */
        public static function setValue( array &$arr, $name, $value, $flags = 0 
) {
+               if ( !( $flags & ApiResult::NO_VALIDATE ) ) {
+                       $value = self::validateValue( $value );
+               }
+
                if ( $name === null ) {
                        if ( $flags & ApiResult::ADD_ON_TOP ) {
                                array_unshift( $arr, $value );
@@ -283,10 +287,6 @@
                                array_push( $arr, $value );
                        }
                        return;
-               }
-
-               if ( !( $flags & ApiResult::NO_VALIDATE ) ) {
-                       $value = self::validateValue( $value );
                }
 
                $exists = isset( $arr[$name] );
diff --git a/tests/phpunit/includes/api/ApiResultTest.php 
b/tests/phpunit/includes/api/ApiResultTest.php
index f2fcb4a..f0d8455 100644
--- a/tests/phpunit/includes/api/ApiResultTest.php
+++ b/tests/phpunit/includes/api/ApiResultTest.php
@@ -107,8 +107,29 @@
                        );
                }
                try {
+                       ApiResult::setValue( $arr, null, $fh );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( InvalidArgumentException $ex ) {
+                       $this->assertSame(
+                               'Cannot add resource(stream) to ApiResult',
+                               $ex->getMessage(),
+                               'Expected exception'
+                       );
+               }
+               try {
                        $obj->file = $fh;
                        ApiResult::setValue( $arr, 'sub', $obj );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( InvalidArgumentException $ex ) {
+                       $this->assertSame(
+                               'Cannot add resource(stream) to ApiResult',
+                               $ex->getMessage(),
+                               'Expected exception'
+                       );
+               }
+               try {
+                       $obj->file = $fh;
+                       ApiResult::setValue( $arr, null, $obj );
                        $this->fail( 'Expected exception not thrown' );
                } catch ( InvalidArgumentException $ex ) {
                        $this->assertSame(
@@ -130,7 +151,27 @@
                        );
                }
                try {
+                       ApiResult::setValue( $arr, null, INF );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( InvalidArgumentException $ex ) {
+                       $this->assertSame(
+                               'Cannot add non-finite floats to ApiResult',
+                               $ex->getMessage(),
+                               'Expected exception'
+                       );
+               }
+               try {
                        ApiResult::setValue( $arr, 'nan', NAN );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( InvalidArgumentException $ex ) {
+                       $this->assertSame(
+                               'Cannot add non-finite floats to ApiResult',
+                               $ex->getMessage(),
+                               'Expected exception'
+                       );
+               }
+               try {
+                       ApiResult::setValue( $arr, null, NAN );
                        $this->fail( 'Expected exception not thrown' );
                } catch ( InvalidArgumentException $ex ) {
                        $this->assertSame(
@@ -155,10 +196,14 @@
                ApiResult::setValue( $arr, 'foo', "foo\x80bar" );
                ApiResult::setValue( $arr, 'bar', "a\xcc\x81" );
                ApiResult::setValue( $arr, 'baz', 74 );
+               ApiResult::setValue( $arr, null, "foo\x80bar" );
+               ApiResult::setValue( $arr, null, "a\xcc\x81" );
                $this->assertSame( array(
                        'foo' => "foo\xef\xbf\xbdbar",
                        'bar' => "\xc3\xa1",
                        'baz' => 74,
+                       0 => "foo\xef\xbf\xbdbar",
+                       1 => "\xc3\xa1",
                ), $arr );
        }
 
@@ -289,8 +334,29 @@
                        );
                }
                try {
+                       $result->addValue( null, null, $fh );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( InvalidArgumentException $ex ) {
+                       $this->assertSame(
+                               'Cannot add resource(stream) to ApiResult',
+                               $ex->getMessage(),
+                               'Expected exception'
+                       );
+               }
+               try {
                        $obj->file = $fh;
                        $result->addValue( null, 'sub', $obj );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( InvalidArgumentException $ex ) {
+                       $this->assertSame(
+                               'Cannot add resource(stream) to ApiResult',
+                               $ex->getMessage(),
+                               'Expected exception'
+                       );
+               }
+               try {
+                       $obj->file = $fh;
+                       $result->addValue( null, null, $obj );
                        $this->fail( 'Expected exception not thrown' );
                } catch ( InvalidArgumentException $ex ) {
                        $this->assertSame(
@@ -312,7 +378,27 @@
                        );
                }
                try {
+                       $result->addValue( null, null, INF );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( InvalidArgumentException $ex ) {
+                       $this->assertSame(
+                               'Cannot add non-finite floats to ApiResult',
+                               $ex->getMessage(),
+                               'Expected exception'
+                       );
+               }
+               try {
                        $result->addValue( null, 'nan', NAN );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( InvalidArgumentException $ex ) {
+                       $this->assertSame(
+                               'Cannot add non-finite floats to ApiResult',
+                               $ex->getMessage(),
+                               'Expected exception'
+                       );
+               }
+               try {
+                       $result->addValue( null, null, NAN );
                        $this->fail( 'Expected exception not thrown' );
                } catch ( InvalidArgumentException $ex ) {
                        $this->assertSame(
@@ -374,10 +460,14 @@
                $result->addValue( null, 'foo', "foo\x80bar" );
                $result->addValue( null, 'bar', "a\xcc\x81" );
                $result->addValue( null, 'baz', 74 );
+               $result->addValue( null, null, "foo\x80bar" );
+               $result->addValue( null, null, "a\xcc\x81" );
                $this->assertSame( array(
                        'foo' => "foo\xef\xbf\xbdbar",
                        'bar' => "\xc3\xa1",
                        'baz' => 74,
+                       0 => "foo\xef\xbf\xbdbar",
+                       1 => "\xc3\xa1",
                        ApiResult::META_TYPE => 'assoc',
                ), $result->getResultData() );
        }

-- 
To view, visit https://gerrit.wikimedia.org/r/207456
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I5301a615a992b090000a59f86e13b9f78dcd5aec
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/core
Gerrit-Branch: wmf/1.26wmf3
Gerrit-Owner: Anomie <bjor...@wikimedia.org>
Gerrit-Reviewer: Anomie <bjor...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to