7 class ApiResultTest
extends MediaWikiTestCase
{
12 public function testStaticDataMethods() {
15 ApiResult
::setValue( $arr, 'setValue', '1' );
17 ApiResult
::setValue( $arr, null, 'unnamed 1' );
18 ApiResult
::setValue( $arr, null, 'unnamed 2' );
20 ApiResult
::setValue( $arr, 'deleteValue', '2' );
21 ApiResult
::unsetValue( $arr, 'deleteValue' );
23 ApiResult
::setContentValue( $arr, 'setContentValue', '3' );
25 $this->assertSame( array(
29 ApiResult
::META_CONTENT
=> 'setContentValue',
30 'setContentValue' => '3',
34 ApiResult
::setValue( $arr, 'setValue', '99' );
35 $this->fail( 'Expected exception not thrown' );
36 } catch ( RuntimeException
$ex ) {
38 'Attempting to add element setValue=99, existing value is 1',
45 ApiResult
::setContentValue( $arr, 'setContentValue2', '99' );
46 $this->fail( 'Expected exception not thrown' );
47 } catch ( RuntimeException
$ex ) {
49 'Attempting to set content element as setContentValue2 when setContentValue ' .
50 'is already set as the content element',
56 ApiResult
::setValue( $arr, 'setValue', '99', ApiResult
::OVERRIDE
);
57 $this->assertSame( '99', $arr['setValue'] );
59 ApiResult
::setContentValue( $arr, 'setContentValue2', '99', ApiResult
::OVERRIDE
);
60 $this->assertSame( 'setContentValue2', $arr[ApiResult
::META_CONTENT
] );
62 $arr = array( 'foo' => 1, 'bar' => 1 );
63 ApiResult
::setValue( $arr, 'top', '2', ApiResult
::ADD_ON_TOP
);
64 ApiResult
::setValue( $arr, null, '2', ApiResult
::ADD_ON_TOP
);
65 ApiResult
::setValue( $arr, 'bottom', '2' );
66 ApiResult
::setValue( $arr, 'foo', '2', ApiResult
::OVERRIDE
);
67 ApiResult
::setValue( $arr, 'bar', '2', ApiResult
::OVERRIDE | ApiResult
::ADD_ON_TOP
);
68 $this->assertSame( array( 0, 'top', 'foo', 'bar', 'bottom' ), array_keys( $arr ) );
71 ApiResult
::setValue( $arr, 'sub', array( 'foo' => 1 ) );
72 ApiResult
::setValue( $arr, 'sub', array( 'bar' => 1 ) );
73 $this->assertSame( array( 'sub' => array( 'foo' => 1, 'bar' => 1 ) ), $arr );
76 ApiResult
::setValue( $arr, 'sub', array( 'foo' => 2, 'baz' => 2 ) );
77 $this->fail( 'Expected exception not thrown' );
78 } catch ( RuntimeException
$ex ) {
80 'Conflicting keys (foo) when attempting to merge element sub',
87 $title = Title
::newFromText( "MediaWiki:Foobar" );
91 ApiResult
::setValue( $arr, 'title', $title );
92 ApiResult
::setValue( $arr, 'obj', $obj );
93 $this->assertSame( array(
94 'title' => (string)$title,
95 'obj' => array( 'foo' => 1, 'bar' => 2, ApiResult
::META_TYPE
=> 'assoc' ),
100 ApiResult
::setValue( $arr, 'file', $fh );
101 $this->fail( 'Expected exception not thrown' );
102 } catch ( InvalidArgumentException
$ex ) {
104 'Cannot add resource(stream) to ApiResult',
110 ApiResult
::setValue( $arr, null, $fh );
111 $this->fail( 'Expected exception not thrown' );
112 } catch ( InvalidArgumentException
$ex ) {
114 'Cannot add resource(stream) to ApiResult',
121 ApiResult
::setValue( $arr, 'sub', $obj );
122 $this->fail( 'Expected exception not thrown' );
123 } catch ( InvalidArgumentException
$ex ) {
125 'Cannot add resource(stream) to ApiResult',
132 ApiResult
::setValue( $arr, null, $obj );
133 $this->fail( 'Expected exception not thrown' );
134 } catch ( InvalidArgumentException
$ex ) {
136 'Cannot add resource(stream) to ApiResult',
144 ApiResult
::setValue( $arr, 'inf', INF
);
145 $this->fail( 'Expected exception not thrown' );
146 } catch ( InvalidArgumentException
$ex ) {
148 'Cannot add non-finite floats to ApiResult',
154 ApiResult
::setValue( $arr, null, INF
);
155 $this->fail( 'Expected exception not thrown' );
156 } catch ( InvalidArgumentException
$ex ) {
158 'Cannot add non-finite floats to ApiResult',
164 ApiResult
::setValue( $arr, 'nan', NAN
);
165 $this->fail( 'Expected exception not thrown' );
166 } catch ( InvalidArgumentException
$ex ) {
168 'Cannot add non-finite floats to ApiResult',
174 ApiResult
::setValue( $arr, null, NAN
);
175 $this->fail( 'Expected exception not thrown' );
176 } catch ( InvalidArgumentException
$ex ) {
178 'Cannot add non-finite floats to ApiResult',
184 ApiResult
::setValue( $arr, null, NAN
, ApiResult
::NO_VALIDATE
);
187 ApiResult
::setValue( $arr, null, NAN
, ApiResult
::NO_SIZE_CHECK
);
188 $this->fail( 'Expected exception not thrown' );
189 } catch ( InvalidArgumentException
$ex ) {
191 'Cannot add non-finite floats to ApiResult',
198 $result2 = new ApiResult( 8388608 );
199 $result2->addValue( null, 'foo', 'bar' );
200 ApiResult
::setValue( $arr, 'baz', $result2 );
201 $this->assertSame( array(
203 ApiResult
::META_TYPE
=> 'assoc',
209 ApiResult
::setValue( $arr, 'foo', "foo\x80bar" );
210 ApiResult
::setValue( $arr, 'bar', "a\xcc\x81" );
211 ApiResult
::setValue( $arr, 'baz', 74 );
212 ApiResult
::setValue( $arr, null, "foo\x80bar" );
213 ApiResult
::setValue( $arr, null, "a\xcc\x81" );
214 $this->assertSame( array(
215 'foo' => "foo\xef\xbf\xbdbar",
218 0 => "foo\xef\xbf\xbdbar",
226 public function testInstanceDataMethods() {
227 $result = new ApiResult( 8388608 );
229 $result->addValue( null, 'setValue', '1' );
231 $result->addValue( null, null, 'unnamed 1' );
232 $result->addValue( null, null, 'unnamed 2' );
234 $result->addValue( null, 'deleteValue', '2' );
235 $result->removeValue( null, 'deleteValue' );
237 $result->addValue( array( 'a', 'b' ), 'deleteValue', '3' );
238 $result->removeValue( array( 'a', 'b', 'deleteValue' ), null, '3' );
240 $result->addContentValue( null, 'setContentValue', '3' );
242 $this->assertSame( array(
246 'a' => array( 'b' => array() ),
247 'setContentValue' => '3',
248 ApiResult
::META_TYPE
=> 'assoc',
249 ApiResult
::META_CONTENT
=> 'setContentValue',
250 ), $result->getResultData() );
251 $this->assertSame( 20, $result->getSize() );
254 $result->addValue( null, 'setValue', '99' );
255 $this->fail( 'Expected exception not thrown' );
256 } catch ( RuntimeException
$ex ) {
258 'Attempting to add element setValue=99, existing value is 1',
265 $result->addContentValue( null, 'setContentValue2', '99' );
266 $this->fail( 'Expected exception not thrown' );
267 } catch ( RuntimeException
$ex ) {
269 'Attempting to set content element as setContentValue2 when setContentValue ' .
270 'is already set as the content element',
276 $result->addValue( null, 'setValue', '99', ApiResult
::OVERRIDE
);
277 $this->assertSame( '99', $result->getResultData( array( 'setValue' ) ) );
279 $result->addContentValue( null, 'setContentValue2', '99', ApiResult
::OVERRIDE
);
280 $this->assertSame( 'setContentValue2',
281 $result->getResultData( array( ApiResult
::META_CONTENT
) ) );
284 $this->assertSame( array(
285 ApiResult
::META_TYPE
=> 'assoc',
286 ), $result->getResultData() );
287 $this->assertSame( 0, $result->getSize() );
289 $result->addValue( null, 'foo', 1 );
290 $result->addValue( null, 'bar', 1 );
291 $result->addValue( null, 'top', '2', ApiResult
::ADD_ON_TOP
);
292 $result->addValue( null, null, '2', ApiResult
::ADD_ON_TOP
);
293 $result->addValue( null, 'bottom', '2' );
294 $result->addValue( null, 'foo', '2', ApiResult
::OVERRIDE
);
295 $result->addValue( null, 'bar', '2', ApiResult
::OVERRIDE | ApiResult
::ADD_ON_TOP
);
296 $this->assertSame( array( 0, 'top', 'foo', 'bar', 'bottom', ApiResult
::META_TYPE
),
297 array_keys( $result->getResultData() ) );
300 $result->addValue( null, 'foo', array( 'bar' => 1 ) );
301 $result->addValue( array( 'foo', 'top' ), 'x', 2, ApiResult
::ADD_ON_TOP
);
302 $result->addValue( array( 'foo', 'bottom' ), 'x', 2 );
303 $this->assertSame( array( 'top', 'bar', 'bottom' ),
304 array_keys( $result->getResultData( array( 'foo' ) ) ) );
307 $result->addValue( null, 'sub', array( 'foo' => 1 ) );
308 $result->addValue( null, 'sub', array( 'bar' => 1 ) );
309 $this->assertSame( array(
310 'sub' => array( 'foo' => 1, 'bar' => 1 ),
311 ApiResult
::META_TYPE
=> 'assoc',
312 ), $result->getResultData() );
315 $result->addValue( null, 'sub', array( 'foo' => 2, 'baz' => 2 ) );
316 $this->fail( 'Expected exception not thrown' );
317 } catch ( RuntimeException
$ex ) {
319 'Conflicting keys (foo) when attempting to merge element sub',
326 $title = Title
::newFromText( "MediaWiki:Foobar" );
330 $result->addValue( null, 'title', $title );
331 $result->addValue( null, 'obj', $obj );
332 $this->assertSame( array(
333 'title' => (string)$title,
334 'obj' => array( 'foo' => 1, 'bar' => 2, ApiResult
::META_TYPE
=> 'assoc' ),
335 ApiResult
::META_TYPE
=> 'assoc',
336 ), $result->getResultData() );
340 $result->addValue( null, 'file', $fh );
341 $this->fail( 'Expected exception not thrown' );
342 } catch ( InvalidArgumentException
$ex ) {
344 'Cannot add resource(stream) to ApiResult',
350 $result->addValue( null, null, $fh );
351 $this->fail( 'Expected exception not thrown' );
352 } catch ( InvalidArgumentException
$ex ) {
354 'Cannot add resource(stream) to ApiResult',
361 $result->addValue( null, 'sub', $obj );
362 $this->fail( 'Expected exception not thrown' );
363 } catch ( InvalidArgumentException
$ex ) {
365 'Cannot add resource(stream) to ApiResult',
372 $result->addValue( null, null, $obj );
373 $this->fail( 'Expected exception not thrown' );
374 } catch ( InvalidArgumentException
$ex ) {
376 'Cannot add resource(stream) to ApiResult',
384 $result->addValue( null, 'inf', INF
);
385 $this->fail( 'Expected exception not thrown' );
386 } catch ( InvalidArgumentException
$ex ) {
388 'Cannot add non-finite floats to ApiResult',
394 $result->addValue( null, null, INF
);
395 $this->fail( 'Expected exception not thrown' );
396 } catch ( InvalidArgumentException
$ex ) {
398 'Cannot add non-finite floats to ApiResult',
404 $result->addValue( null, 'nan', NAN
);
405 $this->fail( 'Expected exception not thrown' );
406 } catch ( InvalidArgumentException
$ex ) {
408 'Cannot add non-finite floats to ApiResult',
414 $result->addValue( null, null, NAN
);
415 $this->fail( 'Expected exception not thrown' );
416 } catch ( InvalidArgumentException
$ex ) {
418 'Cannot add non-finite floats to ApiResult',
424 $result->addValue( null, null, NAN
, ApiResult
::NO_VALIDATE
);
427 $result->addValue( null, null, NAN
, ApiResult
::NO_SIZE_CHECK
);
428 $this->fail( 'Expected exception not thrown' );
429 } catch ( InvalidArgumentException
$ex ) {
431 'Cannot add non-finite floats to ApiResult',
438 $result->addParsedLimit( 'foo', 12 );
439 $this->assertSame( array(
440 'limits' => array( 'foo' => 12 ),
441 ApiResult
::META_TYPE
=> 'assoc',
442 ), $result->getResultData() );
443 $result->addParsedLimit( 'foo', 13 );
444 $this->assertSame( array(
445 'limits' => array( 'foo' => 13 ),
446 ApiResult
::META_TYPE
=> 'assoc',
447 ), $result->getResultData() );
448 $this->assertSame( null, $result->getResultData( array( 'foo', 'bar', 'baz' ) ) );
449 $this->assertSame( 13, $result->getResultData( array( 'limits', 'foo' ) ) );
451 $result->getResultData( array( 'limits', 'foo', 'bar' ) );
452 $this->fail( 'Expected exception not thrown' );
453 } catch ( InvalidArgumentException
$ex ) {
455 'Path limits.foo is not an array',
461 $result = new ApiResult( 10 );
462 $formatter = new ApiErrorFormatter( $result, Language
::factory( 'en' ), 'none', false );
463 $result->setErrorFormatter( $formatter );
464 $this->assertFalse( $result->addValue( null, 'foo', '12345678901' ) );
465 $this->assertTrue( $result->addValue( null, 'foo', '12345678901', ApiResult
::NO_SIZE_CHECK
) );
466 $this->assertSame( 0, $result->getSize() );
468 $this->assertTrue( $result->addValue( null, 'foo', '1234567890' ) );
469 $this->assertFalse( $result->addValue( null, 'foo', '1' ) );
470 $result->removeValue( null, 'foo' );
471 $this->assertTrue( $result->addValue( null, 'foo', '1' ) );
473 $result = new ApiResult( 10 );
474 $obj = new ApiResultTestSerializableObject( 'ok' );
475 $obj->foobar
= 'foobaz';
476 $this->assertTrue( $result->addValue( null, 'foo', $obj ) );
477 $this->assertSame( 2, $result->getSize() );
479 $result = new ApiResult( 8388608 );
480 $result2 = new ApiResult( 8388608 );
481 $result2->addValue( null, 'foo', 'bar' );
482 $result->addValue( null, 'baz', $result2 );
483 $this->assertSame( array(
486 ApiResult
::META_TYPE
=> 'assoc',
488 ApiResult
::META_TYPE
=> 'assoc',
489 ), $result->getResultData() );
491 $result = new ApiResult( 8388608 );
492 $result->addValue( null, 'foo', "foo\x80bar" );
493 $result->addValue( null, 'bar', "a\xcc\x81" );
494 $result->addValue( null, 'baz', 74 );
495 $result->addValue( null, null, "foo\x80bar" );
496 $result->addValue( null, null, "a\xcc\x81" );
497 $this->assertSame( array(
498 'foo' => "foo\xef\xbf\xbdbar",
501 0 => "foo\xef\xbf\xbdbar",
503 ApiResult
::META_TYPE
=> 'assoc',
504 ), $result->getResultData() );
510 public function testMetadata() {
511 $arr = array( 'foo' => array( 'bar' => array() ) );
512 $result = new ApiResult( 8388608 );
513 $result->addValue( null, 'foo', array( 'bar' => array() ) );
518 ApiResult
::META_INDEXED_TAG_NAME
=> 'ritn',
519 ApiResult
::META_TYPE
=> 'default',
521 ApiResult
::META_INDEXED_TAG_NAME
=> 'ritn',
522 ApiResult
::META_TYPE
=> 'default',
524 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
525 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
526 ApiResult
::META_PRESERVE_KEYS
=> array( 'foo', 'bar' ),
527 ApiResult
::META_TYPE
=> 'array',
530 ApiResult
::setSubelementsList( $arr, 'foo' );
531 ApiResult
::setSubelementsList( $arr, array( 'bar', 'baz' ) );
532 ApiResult
::unsetSubelementsList( $arr, 'baz' );
533 ApiResult
::setIndexedTagNameRecursive( $arr, 'ritn' );
534 ApiResult
::setIndexedTagName( $arr, 'itn' );
535 ApiResult
::setPreserveKeysList( $arr, 'foo' );
536 ApiResult
::setPreserveKeysList( $arr, array( 'bar', 'baz' ) );
537 ApiResult
::unsetPreserveKeysList( $arr, 'baz' );
538 ApiResult
::setArrayTypeRecursive( $arr, 'default' );
539 ApiResult
::setArrayType( $arr, 'array' );
540 $this->assertSame( $expect, $arr );
542 $result->addSubelementsList( null, 'foo' );
543 $result->addSubelementsList( null, array( 'bar', 'baz' ) );
544 $result->removeSubelementsList( null, 'baz' );
545 $result->addIndexedTagNameRecursive( null, 'ritn' );
546 $result->addIndexedTagName( null, 'itn' );
547 $result->addPreserveKeysList( null, 'foo' );
548 $result->addPreserveKeysList( null, array( 'bar', 'baz' ) );
549 $result->removePreserveKeysList( null, 'baz' );
550 $result->addArrayTypeRecursive( null, 'default' );
551 $result->addArrayType( null, 'array' );
552 $this->assertEquals( $expect, $result->getResultData() );
554 $arr = array( 'foo' => array( 'bar' => array() ) );
558 ApiResult
::META_TYPE
=> 'kvp',
559 ApiResult
::META_KVP_KEY_NAME
=> 'key',
561 ApiResult
::META_TYPE
=> 'kvp',
562 ApiResult
::META_KVP_KEY_NAME
=> 'key',
564 ApiResult
::META_TYPE
=> 'BCkvp',
565 ApiResult
::META_KVP_KEY_NAME
=> 'bc',
567 ApiResult
::setArrayTypeRecursive( $arr, 'kvp', 'key' );
568 ApiResult
::setArrayType( $arr, 'BCkvp', 'bc' );
569 $this->assertSame( $expect, $arr );
575 public function testUtilityFunctions() {
578 'bar' => array( '_dummy' => 'foobaz' ),
579 'bar2' => (object)array( '_dummy' => 'foobaz' ),
581 '_dummy' => 'foobaz',
583 'foo2' => (object)array(
584 'bar' => array( '_dummy' => 'foobaz' ),
585 'bar2' => (object)array( '_dummy' => 'foobaz' ),
587 '_dummy' => 'foobaz',
589 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
590 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
591 ApiResult
::META_PRESERVE_KEYS
=> array( 'foo', 'bar', '_dummy2', 0 ),
592 ApiResult
::META_TYPE
=> 'array',
593 '_dummy' => 'foobaz',
594 '_dummy2' => 'foobaz!',
596 $this->assertEquals( array(
599 'bar2' => (object)array(),
602 'foo2' => (object)array(
604 'bar2' => (object)array(),
607 '_dummy2' => 'foobaz!',
608 ), ApiResult
::stripMetadata( $arr ), 'ApiResult::stripMetadata' );
611 $data = ApiResult
::stripMetadataNonRecursive( $arr, $metadata );
612 $this->assertEquals( array(
614 'bar' => array( '_dummy' => 'foobaz' ),
615 'bar2' => (object)array( '_dummy' => 'foobaz' ),
617 '_dummy' => 'foobaz',
619 'foo2' => (object)array(
620 'bar' => array( '_dummy' => 'foobaz' ),
621 'bar2' => (object)array( '_dummy' => 'foobaz' ),
623 '_dummy' => 'foobaz',
625 '_dummy2' => 'foobaz!',
626 ), $data, 'ApiResult::stripMetadataNonRecursive ($data)' );
627 $this->assertEquals( array(
628 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
629 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
630 ApiResult
::META_PRESERVE_KEYS
=> array( 'foo', 'bar', '_dummy2', 0 ),
631 ApiResult
::META_TYPE
=> 'array',
632 '_dummy' => 'foobaz',
633 ), $metadata, 'ApiResult::stripMetadataNonRecursive ($metadata)' );
636 $data = ApiResult
::stripMetadataNonRecursive( (object)$arr, $metadata );
637 $this->assertEquals( (object)array(
639 'bar' => array( '_dummy' => 'foobaz' ),
640 'bar2' => (object)array( '_dummy' => 'foobaz' ),
642 '_dummy' => 'foobaz',
644 'foo2' => (object)array(
645 'bar' => array( '_dummy' => 'foobaz' ),
646 'bar2' => (object)array( '_dummy' => 'foobaz' ),
648 '_dummy' => 'foobaz',
650 '_dummy2' => 'foobaz!',
651 ), $data, 'ApiResult::stripMetadataNonRecursive on object ($data)' );
652 $this->assertEquals( array(
653 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
654 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
655 ApiResult
::META_PRESERVE_KEYS
=> array( 'foo', 'bar', '_dummy2', 0 ),
656 ApiResult
::META_TYPE
=> 'array',
657 '_dummy' => 'foobaz',
658 ), $metadata, 'ApiResult::stripMetadataNonRecursive on object ($metadata)' );
663 * @dataProvider provideTransformations
664 * @param string $label
665 * @param array $input
666 * @param array $transforms
667 * @param array|Exception $expect
669 public function testTransformations( $label, $input, $transforms, $expect ) {
670 $result = new ApiResult( false );
671 $result->addValue( null, 'test', $input );
673 if ( $expect instanceof Exception
) {
675 $output = $result->getResultData( 'test', $transforms );
676 $this->fail( 'Expected exception not thrown', $label );
677 } catch ( Exception
$ex ) {
678 $this->assertEquals( $ex, $expect, $label );
681 $output = $result->getResultData( 'test', $transforms );
682 $this->assertEquals( $expect, $output, $label );
686 public function provideTransformations() {
687 $kvp = function ( $keyKey, $key, $valKey, $value ) {
691 ApiResult
::META_PRESERVE_KEYS
=> array( $keyKey ),
692 ApiResult
::META_CONTENT
=> $valKey,
693 ApiResult
::META_TYPE
=> 'assoc',
697 'defaultArray' => array( 2 => 'a', 0 => 'b', 1 => 'c' ),
698 'defaultAssoc' => array( 'x' => 'a', 1 => 'b', 0 => 'c' ),
699 'defaultAssoc2' => array( 2 => 'a', 3 => 'b', 0 => 'c' ),
700 'array' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'array' ),
701 'BCarray' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'BCarray' ),
702 'BCassoc' => array( 'a', 'b', 'c', ApiResult
::META_TYPE
=> 'BCassoc' ),
703 'assoc' => array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
704 'kvp' => array( 'x' => 'a', 'y' => 'b', 'z' => array( 'c' ), ApiResult
::META_TYPE
=> 'kvp' ),
705 'BCkvp' => array( 'x' => 'a', 'y' => 'b',
706 ApiResult
::META_TYPE
=> 'BCkvp',
707 ApiResult
::META_KVP_KEY_NAME
=> 'key',
709 'kvpmerge' => array( 'x' => 'a', 'y' => array( 'b' ), 'z' => array( 'c' => 'd' ),
710 ApiResult
::META_TYPE
=> 'kvp',
711 ApiResult
::META_KVP_MERGE
=> true,
713 'emptyDefault' => array( '_dummy' => 1 ),
714 'emptyAssoc' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'assoc' ),
716 ApiResult
::META_PRESERVE_KEYS
=> array( '_dummy' ),
720 'bar' => array( '_dummy' => 'foobaz' ),
722 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
723 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
724 ApiResult
::META_PRESERVE_KEYS
=> array( 'foo', 'bar', '_dummy2', 0 ),
725 ApiResult
::META_TYPE
=> 'array',
728 '_dummy' => 'foobaz',
730 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
731 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
732 ApiResult
::META_PRESERVE_KEYS
=> array( 'foo', 'bar', '_dummy2', 0 ),
733 ApiResult
::META_TYPE
=> 'array',
734 '_dummy' => 'foobaz',
735 '_dummy2' => 'foobaz!',
746 ApiResult
::META_BC_BOOLS
=> array( 0, 'true', 'false' ),
748 array( 'BC' => array() ),
753 ApiResult
::META_BC_BOOLS
=> array( 0, 'true', 'false' ),
757 'BC: META_BC_SUBELEMENTS',
761 ApiResult
::META_BC_SUBELEMENTS
=> array( 'bc' ),
763 array( 'BC' => array() ),
767 ApiResult
::META_CONTENT
=> '*',
768 ApiResult
::META_TYPE
=> 'assoc',
771 ApiResult
::META_BC_SUBELEMENTS
=> array( 'bc' ),
778 ApiResult
::META_CONTENT
=> 'content',
780 array( 'BC' => array() ),
783 ApiResult
::META_CONTENT
=> '*',
789 'foo' => 'foo value',
790 'bar' => 'bar value',
791 '_baz' => 'baz value',
792 ApiResult
::META_TYPE
=> 'BCkvp',
793 ApiResult
::META_KVP_KEY_NAME
=> 'key',
794 ApiResult
::META_PRESERVE_KEYS
=> array( '_baz' ),
796 array( 'BC' => array() ),
798 $kvp( 'key', 'foo', '*', 'foo value' ),
799 $kvp( 'key', 'bar', '*', 'bar value' ),
800 $kvp( 'key', '_baz', '*', 'baz value' ),
801 ApiResult
::META_TYPE
=> 'array',
802 ApiResult
::META_KVP_KEY_NAME
=> 'key',
803 ApiResult
::META_PRESERVE_KEYS
=> array( '_baz' ),
809 ApiResult
::META_TYPE
=> 'BCarray',
811 array( 'BC' => array() ),
813 ApiResult
::META_TYPE
=> 'default',
819 ApiResult
::META_TYPE
=> 'BCassoc',
821 array( 'BC' => array() ),
823 ApiResult
::META_TYPE
=> 'default',
827 'BC: BCkvp exception',
829 ApiResult
::META_TYPE
=> 'BCkvp',
831 array( 'BC' => array() ),
832 new UnexpectedValueException(
833 'Type "BCkvp" used without setting ApiResult::META_KVP_KEY_NAME metadata item'
837 'BC: nobool, no*, nosub',
841 'content' => 'content',
842 ApiResult
::META_CONTENT
=> 'content',
844 ApiResult
::META_BC_SUBELEMENTS
=> array( 'bc' ),
845 'BCarray' => array( ApiResult
::META_TYPE
=> 'BCarray' ),
846 'BCassoc' => array( ApiResult
::META_TYPE
=> 'BCassoc' ),
848 'foo' => 'foo value',
849 'bar' => 'bar value',
850 '_baz' => 'baz value',
851 ApiResult
::META_TYPE
=> 'BCkvp',
852 ApiResult
::META_KVP_KEY_NAME
=> 'key',
853 ApiResult
::META_PRESERVE_KEYS
=> array( '_baz' ),
856 array( 'BC' => array( 'nobool', 'no*', 'nosub' ) ),
860 'content' => 'content',
862 'BCarray' => array( ApiResult
::META_TYPE
=> 'default' ),
863 'BCassoc' => array( ApiResult
::META_TYPE
=> 'default' ),
865 $kvp( 'key', 'foo', '*', 'foo value' ),
866 $kvp( 'key', 'bar', '*', 'bar value' ),
867 $kvp( 'key', '_baz', '*', 'baz value' ),
868 ApiResult
::META_TYPE
=> 'array',
869 ApiResult
::META_KVP_KEY_NAME
=> 'key',
870 ApiResult
::META_PRESERVE_KEYS
=> array( '_baz' ),
872 ApiResult
::META_CONTENT
=> 'content',
873 ApiResult
::META_BC_SUBELEMENTS
=> array( 'bc' ),
878 'Types: Normal transform',
880 array( 'Types' => array() ),
882 'defaultArray' => array( 'b', 'c', 'a', ApiResult
::META_TYPE
=> 'array' ),
883 'defaultAssoc' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
884 'defaultAssoc2' => array( 2 => 'a', 3 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
885 'array' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
886 'BCarray' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
887 'BCassoc' => array( 'a', 'b', 'c', ApiResult
::META_TYPE
=> 'assoc' ),
888 'assoc' => array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
889 'kvp' => array( 'x' => 'a', 'y' => 'b',
890 'z' => array( 'c', ApiResult
::META_TYPE
=> 'array' ),
891 ApiResult
::META_TYPE
=> 'assoc'
893 'BCkvp' => array( 'x' => 'a', 'y' => 'b',
894 ApiResult
::META_TYPE
=> 'assoc',
895 ApiResult
::META_KVP_KEY_NAME
=> 'key',
899 'y' => array( 'b', ApiResult
::META_TYPE
=> 'array' ),
900 'z' => array( 'c' => 'd', ApiResult
::META_TYPE
=> 'assoc' ),
901 ApiResult
::META_TYPE
=> 'assoc',
902 ApiResult
::META_KVP_MERGE
=> true,
904 'emptyDefault' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'array' ),
905 'emptyAssoc' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'assoc' ),
907 ApiResult
::META_PRESERVE_KEYS
=> array( '_dummy' ),
908 ApiResult
::META_TYPE
=> 'assoc',
912 'Types: AssocAsObject',
914 array( 'Types' => array( 'AssocAsObject' => true ) ),
916 'defaultArray' => array( 'b', 'c', 'a', ApiResult
::META_TYPE
=> 'array' ),
917 'defaultAssoc' => (object)array( 'x' => 'a',
918 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc'
920 'defaultAssoc2' => (object)array( 2 => 'a', 3 => 'b',
921 0 => 'c', ApiResult
::META_TYPE
=> 'assoc'
923 'array' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
924 'BCarray' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
925 'BCassoc' => (object)array( 'a', 'b', 'c', ApiResult
::META_TYPE
=> 'assoc' ),
926 'assoc' => (object)array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
927 'kvp' => (object)array( 'x' => 'a', 'y' => 'b',
928 'z' => array( 'c', ApiResult
::META_TYPE
=> 'array' ),
929 ApiResult
::META_TYPE
=> 'assoc'
931 'BCkvp' => (object)array( 'x' => 'a', 'y' => 'b',
932 ApiResult
::META_TYPE
=> 'assoc',
933 ApiResult
::META_KVP_KEY_NAME
=> 'key',
935 'kvpmerge' => (object)array(
937 'y' => array( 'b', ApiResult
::META_TYPE
=> 'array' ),
938 'z' => (object)array( 'c' => 'd', ApiResult
::META_TYPE
=> 'assoc' ),
939 ApiResult
::META_TYPE
=> 'assoc',
940 ApiResult
::META_KVP_MERGE
=> true,
942 'emptyDefault' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'array' ),
943 'emptyAssoc' => (object)array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'assoc' ),
945 ApiResult
::META_PRESERVE_KEYS
=> array( '_dummy' ),
946 ApiResult
::META_TYPE
=> 'assoc',
952 array( 'Types' => array( 'ArmorKVP' => 'name' ) ),
954 'defaultArray' => array( 'b', 'c', 'a', ApiResult
::META_TYPE
=> 'array' ),
955 'defaultAssoc' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
956 'defaultAssoc2' => array( 2 => 'a', 3 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
957 'array' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
958 'BCarray' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
959 'BCassoc' => array( 'a', 'b', 'c', ApiResult
::META_TYPE
=> 'assoc' ),
960 'assoc' => array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
962 $kvp( 'name', 'x', 'value', 'a' ),
963 $kvp( 'name', 'y', 'value', 'b' ),
964 $kvp( 'name', 'z', 'value', array( 'c', ApiResult
::META_TYPE
=> 'array' ) ),
965 ApiResult
::META_TYPE
=> 'array'
968 $kvp( 'key', 'x', 'value', 'a' ),
969 $kvp( 'key', 'y', 'value', 'b' ),
970 ApiResult
::META_TYPE
=> 'array',
971 ApiResult
::META_KVP_KEY_NAME
=> 'key',
974 $kvp( 'name', 'x', 'value', 'a' ),
975 $kvp( 'name', 'y', 'value', array( 'b', ApiResult
::META_TYPE
=> 'array' ) ),
979 ApiResult
::META_TYPE
=> 'assoc',
980 ApiResult
::META_PRESERVE_KEYS
=> array( 'name' )
982 ApiResult
::META_TYPE
=> 'array',
983 ApiResult
::META_KVP_MERGE
=> true,
985 'emptyDefault' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'array' ),
986 'emptyAssoc' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'assoc' ),
988 ApiResult
::META_PRESERVE_KEYS
=> array( '_dummy' ),
989 ApiResult
::META_TYPE
=> 'assoc',
993 'Types: ArmorKVP + BC',
995 array( 'BC' => array(), 'Types' => array( 'ArmorKVP' => 'name' ) ),
997 'defaultArray' => array( 'b', 'c', 'a', ApiResult
::META_TYPE
=> 'array' ),
998 'defaultAssoc' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
999 'defaultAssoc2' => array( 2 => 'a', 3 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
1000 'array' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
1001 'BCarray' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
1002 'BCassoc' => array( 'a', 'b', 'c', ApiResult
::META_TYPE
=> 'array' ),
1003 'assoc' => array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
1005 $kvp( 'name', 'x', '*', 'a' ),
1006 $kvp( 'name', 'y', '*', 'b' ),
1007 $kvp( 'name', 'z', '*', array( 'c', ApiResult
::META_TYPE
=> 'array' ) ),
1008 ApiResult
::META_TYPE
=> 'array'
1011 $kvp( 'key', 'x', '*', 'a' ),
1012 $kvp( 'key', 'y', '*', 'b' ),
1013 ApiResult
::META_TYPE
=> 'array',
1014 ApiResult
::META_KVP_KEY_NAME
=> 'key',
1016 'kvpmerge' => array(
1017 $kvp( 'name', 'x', '*', 'a' ),
1018 $kvp( 'name', 'y', '*', array( 'b', ApiResult
::META_TYPE
=> 'array' ) ),
1022 ApiResult
::META_TYPE
=> 'assoc',
1023 ApiResult
::META_PRESERVE_KEYS
=> array( 'name' ) ),
1024 ApiResult
::META_TYPE
=> 'array',
1025 ApiResult
::META_KVP_MERGE
=> true,
1027 'emptyDefault' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'array' ),
1028 'emptyAssoc' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'assoc' ),
1030 ApiResult
::META_PRESERVE_KEYS
=> array( '_dummy' ),
1031 ApiResult
::META_TYPE
=> 'assoc',
1035 'Types: ArmorKVP + AssocAsObject',
1037 array( 'Types' => array( 'ArmorKVP' => 'name', 'AssocAsObject' => true ) ),
1039 'defaultArray' => array( 'b', 'c', 'a', ApiResult
::META_TYPE
=> 'array' ),
1040 'defaultAssoc' => (object)array( 'x' => 'a', 1 => 'b',
1041 0 => 'c', ApiResult
::META_TYPE
=> 'assoc'
1043 'defaultAssoc2' => (object)array( 2 => 'a', 3 => 'b',
1044 0 => 'c', ApiResult
::META_TYPE
=> 'assoc'
1046 'array' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
1047 'BCarray' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
1048 'BCassoc' => (object)array( 'a', 'b', 'c', ApiResult
::META_TYPE
=> 'assoc' ),
1049 'assoc' => (object)array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
1051 (object)$kvp( 'name', 'x', 'value', 'a' ),
1052 (object)$kvp( 'name', 'y', 'value', 'b' ),
1053 (object)$kvp( 'name', 'z', 'value', array( 'c', ApiResult
::META_TYPE
=> 'array' ) ),
1054 ApiResult
::META_TYPE
=> 'array'
1057 (object)$kvp( 'key', 'x', 'value', 'a' ),
1058 (object)$kvp( 'key', 'y', 'value', 'b' ),
1059 ApiResult
::META_TYPE
=> 'array',
1060 ApiResult
::META_KVP_KEY_NAME
=> 'key',
1062 'kvpmerge' => array(
1063 (object)$kvp( 'name', 'x', 'value', 'a' ),
1064 (object)$kvp( 'name', 'y', 'value', array( 'b', ApiResult
::META_TYPE
=> 'array' ) ),
1068 ApiResult
::META_TYPE
=> 'assoc',
1069 ApiResult
::META_PRESERVE_KEYS
=> array( 'name' )
1071 ApiResult
::META_TYPE
=> 'array',
1072 ApiResult
::META_KVP_MERGE
=> true,
1074 'emptyDefault' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'array' ),
1075 'emptyAssoc' => (object)array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'assoc' ),
1077 ApiResult
::META_PRESERVE_KEYS
=> array( '_dummy' ),
1078 ApiResult
::META_TYPE
=> 'assoc',
1082 'Types: BCkvp exception',
1084 ApiResult
::META_TYPE
=> 'BCkvp',
1086 array( 'Types' => array() ),
1087 new UnexpectedValueException(
1088 'Type "BCkvp" used without setting ApiResult::META_KVP_KEY_NAME metadata item'
1093 'Strip: With ArmorKVP + AssocAsObject transforms',
1095 array( 'Types' => array( 'ArmorKVP' => 'name', 'AssocAsObject' => true ), 'Strip' => 'all' ),
1097 'defaultArray' => array( 'b', 'c', 'a' ),
1098 'defaultAssoc' => (object)array( 'x' => 'a', 1 => 'b', 0 => 'c' ),
1099 'defaultAssoc2' => (object)array( 2 => 'a', 3 => 'b', 0 => 'c' ),
1100 'array' => array( 'a', 'c', 'b' ),
1101 'BCarray' => array( 'a', 'c', 'b' ),
1102 'BCassoc' => (object)array( 'a', 'b', 'c' ),
1103 'assoc' => (object)array( 2 => 'a', 0 => 'b', 1 => 'c' ),
1105 (object)array( 'name' => 'x', 'value' => 'a' ),
1106 (object)array( 'name' => 'y', 'value' => 'b' ),
1107 (object)array( 'name' => 'z', 'value' => array( 'c' ) ),
1110 (object)array( 'key' => 'x', 'value' => 'a' ),
1111 (object)array( 'key' => 'y', 'value' => 'b' ),
1113 'kvpmerge' => array(
1114 (object)array( 'name' => 'x', 'value' => 'a' ),
1115 (object)array( 'name' => 'y', 'value' => array( 'b' ) ),
1116 (object)array( 'name' => 'z', 'c' => 'd' ),
1118 'emptyDefault' => array(),
1119 'emptyAssoc' => (object)array(),
1127 array( 'Strip' => 'all' ),
1134 '_dummy2' => 'foobaz!',
1140 array( 'Strip' => 'base' ),
1143 'bar' => array( '_dummy' => 'foobaz' ),
1145 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
1146 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
1147 ApiResult
::META_PRESERVE_KEYS
=> array( 'foo', 'bar', '_dummy2', 0 ),
1148 ApiResult
::META_TYPE
=> 'array',
1151 '_dummy' => 'foobaz',
1153 '_dummy2' => 'foobaz!',
1159 array( 'Strip' => 'bc' ),
1164 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
1165 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
1169 '_dummy2' => 'foobaz!',
1170 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
1171 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
1183 ApiResult
::META_CONTENT
=> 'foo',
1184 ApiResult
::META_PRESERVE_KEYS
=> array( '_dummy2', '_dummy3' ),
1187 'Custom' => array( $this, 'customTransform' ),
1195 'baz' => array( 'a', 'b' ),
1196 '_dummy2' => '_DUMMY2',
1197 '_dummy3' => '_DUMMY3',
1198 ApiResult
::META_CONTENT
=> 'bar',
1206 * Custom transformer for testTransformations
1207 * @param array &$data
1208 * @param array &$metadata
1210 public function customTransform( &$data, &$metadata ) {
1211 // Prevent recursion
1212 if ( isset( $metadata['_added'] ) ) {
1213 $metadata[ApiResult
::META_TYPE
] = 'array';
1217 foreach ( $data as $k => $v ) {
1218 $data[$k] = strtoupper( $k );
1220 $data['baz'] = array( '_added' => 1, 'z' => 'b', 'y' => 'a' );
1221 $metadata[ApiResult
::META_PRESERVE_KEYS
][0] = '_dummy';
1222 $data[ApiResult
::META_CONTENT
] = 'bar';
1228 public function testAddMetadataToResultVars() {
1233 'sequential_numeric_keys' => array( 'a', 'b', 'c' ),
1234 'non_sequential_numeric_keys' => array( 'a', 'b', 4 => 'c' ),
1235 'string_keys' => array(
1239 'object_sequential_keys' => (object)array( 'a', 'b', 'c' ),
1240 '_type' => "should be overwritten in result",
1242 $this->assertSame( array(
1243 ApiResult
::META_TYPE
=> 'kvp',
1244 ApiResult
::META_KVP_KEY_NAME
=> 'key',
1245 ApiResult
::META_PRESERVE_KEYS
=> array(
1247 'sequential_numeric_keys', 'non_sequential_numeric_keys',
1248 'string_keys', 'object_sequential_keys'
1250 ApiResult
::META_BC_BOOLS
=> array( 'b' ),
1251 ApiResult
::META_INDEXED_TAG_NAME
=> 'var',
1255 'sequential_numeric_keys' => array(
1256 ApiResult
::META_TYPE
=> 'array',
1257 ApiResult
::META_BC_BOOLS
=> array(),
1258 ApiResult
::META_INDEXED_TAG_NAME
=> 'value',
1263 'non_sequential_numeric_keys' => array(
1264 ApiResult
::META_TYPE
=> 'kvp',
1265 ApiResult
::META_KVP_KEY_NAME
=> 'key',
1266 ApiResult
::META_PRESERVE_KEYS
=> array( 0, 1, 4 ),
1267 ApiResult
::META_BC_BOOLS
=> array(),
1268 ApiResult
::META_INDEXED_TAG_NAME
=> 'var',
1273 'string_keys' => array(
1274 ApiResult
::META_TYPE
=> 'kvp',
1275 ApiResult
::META_KVP_KEY_NAME
=> 'key',
1276 ApiResult
::META_PRESERVE_KEYS
=> array( 'one', 'two' ),
1277 ApiResult
::META_BC_BOOLS
=> array(),
1278 ApiResult
::META_INDEXED_TAG_NAME
=> 'var',
1282 'object_sequential_keys' => array(
1283 ApiResult
::META_TYPE
=> 'kvp',
1284 ApiResult
::META_KVP_KEY_NAME
=> 'key',
1285 ApiResult
::META_PRESERVE_KEYS
=> array( 0, 1, 2 ),
1286 ApiResult
::META_BC_BOOLS
=> array(),
1287 ApiResult
::META_INDEXED_TAG_NAME
=> 'var',
1292 ), ApiResult
::addMetadataToResultVars( $arr ) );
1298 public function testDeprecatedFunctions() {
1299 // Ignore ApiResult deprecation warnings during this test
1300 set_error_handler( function ( $errno, $errstr ) use ( &$warnings ) {
1301 if ( preg_match( '/Use of ApiResult::\S+ was deprecated in MediaWiki \d+.\d+\./', $errstr ) ) {
1304 if ( preg_match( '/Use of ApiMain to ApiResult::__construct ' .
1305 'was deprecated in MediaWiki \d+.\d+\./', $errstr ) ) {
1310 $reset = new ScopedCallback( 'restore_error_handler' );
1312 $context = new DerivativeContext( RequestContext
::getMain() );
1313 $context->setConfig( new HashConfig( array(
1314 'APIModules' => array(),
1315 'APIFormatModules' => array(),
1316 'APIMaxResultSize' => 42,
1318 $main = new ApiMain( $context );
1319 $result = TestingAccessWrapper
::newFromObject( new ApiResult( $main ) );
1320 $this->assertSame( 42, $result->maxSize
);
1321 $this->assertSame( $main->getErrorFormatter(), $result->errorFormatter
);
1322 $this->assertSame( $main, $result->mainForContinuation
);
1324 $result = new ApiResult( 8388608 );
1326 $result->addContentValue( null, 'test', 'content' );
1327 $result->addContentValue( array( 'foo', 'bar' ), 'test', 'content' );
1328 $result->addIndexedTagName( null, 'itn' );
1329 $result->addSubelementsList( null, array( 'sub' ) );
1330 $this->assertSame( array(
1337 ), $result->getData() );
1340 ApiResult
::setContent( $arr, 'value' );
1341 ApiResult
::setContent( $arr, 'value2', 'foobar' );
1342 $this->assertSame( array(
1343 ApiResult
::META_CONTENT
=> 'content',
1344 'content' => 'value',
1346 ApiResult
::META_CONTENT
=> 'content',
1347 'content' => 'value2',
1351 $result = new ApiResult( 3 );
1352 $formatter = new ApiErrorFormatter_BackCompat( $result );
1353 $result->setErrorFormatter( $formatter );
1354 $result->disableSizeCheck();
1355 $this->assertTrue( $result->addValue( null, 'foo', '1234567890' ) );
1356 $result->enableSizeCheck();
1357 $this->assertSame( 0, $result->getSize() );
1358 $this->assertFalse( $result->addValue( null, 'foo', '1234567890' ) );
1360 $arr = array( 'foo' => array( 'bar' => 1 ) );
1361 $result->setIndexedTagName_recursive( $arr, 'itn' );
1362 $this->assertSame( array(
1365 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn'
1369 $status = Status
::newGood();
1370 $status->fatal( 'parentheses', '1' );
1371 $status->fatal( 'parentheses', '2' );
1372 $status->warning( 'parentheses', '3' );
1373 $status->warning( 'parentheses', '4' );
1374 $this->assertSame( array(
1377 'message' => 'parentheses',
1380 ApiResult
::META_INDEXED_TAG_NAME
=> 'param',
1385 'message' => 'parentheses',
1388 ApiResult
::META_INDEXED_TAG_NAME
=> 'param',
1391 ApiResult
::META_INDEXED_TAG_NAME
=> 'error',
1392 ), $result->convertStatusToArray( $status, 'error' ) );
1393 $this->assertSame( array(
1395 'type' => 'warning',
1396 'message' => 'parentheses',
1399 ApiResult
::META_INDEXED_TAG_NAME
=> 'param',
1403 'type' => 'warning',
1404 'message' => 'parentheses',
1407 ApiResult
::META_INDEXED_TAG_NAME
=> 'param',
1410 ApiResult
::META_INDEXED_TAG_NAME
=> 'warning',
1411 ), $result->convertStatusToArray( $status, 'warning' ) );
1417 public function testDeprecatedContinuation() {
1418 // Ignore ApiResult deprecation warnings during this test
1419 set_error_handler( function ( $errno, $errstr ) use ( &$warnings ) {
1420 if ( preg_match( '/Use of ApiResult::\S+ was deprecated in MediaWiki \d+.\d+\./', $errstr ) ) {
1426 $reset = new ScopedCallback( 'restore_error_handler' );
1427 $allModules = array(
1428 new MockApiQueryBase( 'mock1' ),
1429 new MockApiQueryBase( 'mock2' ),
1430 new MockApiQueryBase( 'mocklist' ),
1432 $generator = new MockApiQueryBase( 'generator' );
1434 $main = new ApiMain( RequestContext
::getMain() );
1435 $result = new ApiResult( 8388608 );
1436 $result->setMainForContinuation( $main );
1437 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1438 $this->assertSame( array( false, $allModules ), $ret );
1439 $result->setContinueParam( $allModules[0], 'm1continue', array( 1, 2 ) );
1440 $result->setContinueParam( $allModules[2], 'mlcontinue', 2 );
1441 $result->setGeneratorContinueParam( $generator, 'gcontinue', 3 );
1442 $result->endContinuation( 'raw' );
1443 $result->endContinuation( 'standard' );
1444 $this->assertSame( array(
1446 'm1continue' => '1|2',
1447 'continue' => '||mock2',
1448 ), $result->getResultData( 'continue' ) );
1449 $this->assertSame( null, $result->getResultData( 'batchcomplete' ) );
1450 $this->assertSame( array(
1451 'mock1' => array( 'm1continue' => '1|2' ),
1452 'mocklist' => array( 'mlcontinue' => 2 ),
1453 'generator' => array( 'gcontinue' => 3 ),
1454 ), $result->getResultData( 'query-continue' ) );
1455 $main->setContinuationManager( null );
1457 $result = new ApiResult( 8388608 );
1458 $result->setMainForContinuation( $main );
1459 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1460 $this->assertSame( array( false, $allModules ), $ret );
1461 $result->setContinueParam( $allModules[0], 'm1continue', array( 1, 2 ) );
1462 $result->setGeneratorContinueParam( $generator, 'gcontinue', array( 3, 4 ) );
1463 $result->endContinuation( 'raw' );
1464 $result->endContinuation( 'standard' );
1465 $this->assertSame( array(
1466 'm1continue' => '1|2',
1467 'continue' => '||mock2|mocklist',
1468 ), $result->getResultData( 'continue' ) );
1469 $this->assertSame( null, $result->getResultData( 'batchcomplete' ) );
1470 $this->assertSame( array(
1471 'mock1' => array( 'm1continue' => '1|2' ),
1472 'generator' => array( 'gcontinue' => '3|4' ),
1473 ), $result->getResultData( 'query-continue' ) );
1474 $main->setContinuationManager( null );
1476 $result = new ApiResult( 8388608 );
1477 $result->setMainForContinuation( $main );
1478 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1479 $this->assertSame( array( false, $allModules ), $ret );
1480 $result->setContinueParam( $allModules[2], 'mlcontinue', 2 );
1481 $result->setGeneratorContinueParam( $generator, 'gcontinue', 3 );
1482 $result->endContinuation( 'raw' );
1483 $result->endContinuation( 'standard' );
1484 $this->assertSame( array(
1487 'continue' => 'gcontinue||',
1488 ), $result->getResultData( 'continue' ) );
1489 $this->assertSame( true, $result->getResultData( 'batchcomplete' ) );
1490 $this->assertSame( array(
1491 'mocklist' => array( 'mlcontinue' => 2 ),
1492 'generator' => array( 'gcontinue' => 3 ),
1493 ), $result->getResultData( 'query-continue' ) );
1494 $main->setContinuationManager( null );
1496 $result = new ApiResult( 8388608 );
1497 $result->setMainForContinuation( $main );
1498 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1499 $this->assertSame( array( false, $allModules ), $ret );
1500 $result->setGeneratorContinueParam( $generator, 'gcontinue', 3 );
1501 $result->endContinuation( 'raw' );
1502 $result->endContinuation( 'standard' );
1503 $this->assertSame( array(
1505 'continue' => 'gcontinue||mocklist',
1506 ), $result->getResultData( 'continue' ) );
1507 $this->assertSame( true, $result->getResultData( 'batchcomplete' ) );
1508 $this->assertSame( array(
1509 'generator' => array( 'gcontinue' => 3 ),
1510 ), $result->getResultData( 'query-continue' ) );
1511 $main->setContinuationManager( null );
1513 $result = new ApiResult( 8388608 );
1514 $result->setMainForContinuation( $main );
1515 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1516 $this->assertSame( array( false, $allModules ), $ret );
1517 $result->setContinueParam( $allModules[0], 'm1continue', array( 1, 2 ) );
1518 $result->setContinueParam( $allModules[2], 'mlcontinue', 2 );
1519 $result->endContinuation( 'raw' );
1520 $result->endContinuation( 'standard' );
1521 $this->assertSame( array(
1523 'm1continue' => '1|2',
1524 'continue' => '||mock2',
1525 ), $result->getResultData( 'continue' ) );
1526 $this->assertSame( null, $result->getResultData( 'batchcomplete' ) );
1527 $this->assertSame( array(
1528 'mock1' => array( 'm1continue' => '1|2' ),
1529 'mocklist' => array( 'mlcontinue' => 2 ),
1530 ), $result->getResultData( 'query-continue' ) );
1531 $main->setContinuationManager( null );
1533 $result = new ApiResult( 8388608 );
1534 $result->setMainForContinuation( $main );
1535 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1536 $this->assertSame( array( false, $allModules ), $ret );
1537 $result->setContinueParam( $allModules[0], 'm1continue', array( 1, 2 ) );
1538 $result->endContinuation( 'raw' );
1539 $result->endContinuation( 'standard' );
1540 $this->assertSame( array(
1541 'm1continue' => '1|2',
1542 'continue' => '||mock2|mocklist',
1543 ), $result->getResultData( 'continue' ) );
1544 $this->assertSame( null, $result->getResultData( 'batchcomplete' ) );
1545 $this->assertSame( array(
1546 'mock1' => array( 'm1continue' => '1|2' ),
1547 ), $result->getResultData( 'query-continue' ) );
1548 $main->setContinuationManager( null );
1550 $result = new ApiResult( 8388608 );
1551 $result->setMainForContinuation( $main );
1552 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1553 $this->assertSame( array( false, $allModules ), $ret );
1554 $result->setContinueParam( $allModules[2], 'mlcontinue', 2 );
1555 $result->endContinuation( 'raw' );
1556 $result->endContinuation( 'standard' );
1557 $this->assertSame( array(
1559 'continue' => '-||mock1|mock2',
1560 ), $result->getResultData( 'continue' ) );
1561 $this->assertSame( true, $result->getResultData( 'batchcomplete' ) );
1562 $this->assertSame( array(
1563 'mocklist' => array( 'mlcontinue' => 2 ),
1564 ), $result->getResultData( 'query-continue' ) );
1565 $main->setContinuationManager( null );
1567 $result = new ApiResult( 8388608 );
1568 $result->setMainForContinuation( $main );
1569 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1570 $this->assertSame( array( false, $allModules ), $ret );
1571 $result->endContinuation( 'raw' );
1572 $result->endContinuation( 'standard' );
1573 $this->assertSame( null, $result->getResultData( 'continue' ) );
1574 $this->assertSame( true, $result->getResultData( 'batchcomplete' ) );
1575 $this->assertSame( null, $result->getResultData( 'query-continue' ) );
1576 $main->setContinuationManager( null );
1578 $result = new ApiResult( 8388608 );
1579 $result->setMainForContinuation( $main );
1580 $ret = $result->beginContinuation( '||mock2', $allModules, array( 'mock1', 'mock2' ) );
1582 array( false, array_values( array_diff_key( $allModules, array( 1 => 1 ) ) ) ),
1585 $main->setContinuationManager( null );
1587 $result = new ApiResult( 8388608 );
1588 $result->setMainForContinuation( $main );
1589 $ret = $result->beginContinuation( '-||', $allModules, array( 'mock1', 'mock2' ) );
1591 array( true, array_values( array_diff_key( $allModules, array( 0 => 0, 1 => 1 ) ) ) ),
1594 $main->setContinuationManager( null );
1596 $result = new ApiResult( 8388608 );
1597 $result->setMainForContinuation( $main );
1599 $result->beginContinuation( 'foo', $allModules, array( 'mock1', 'mock2' ) );
1600 $this->fail( 'Expected exception not thrown' );
1601 } catch ( UsageException
$ex ) {
1603 'Invalid continue param. You should pass the original value returned by the previous query',
1605 'Expected exception'
1608 $main->setContinuationManager( null );
1610 $result = new ApiResult( 8388608 );
1611 $result->setMainForContinuation( $main );
1612 $result->beginContinuation( '||mock2', array_slice( $allModules, 0, 2 ),
1613 array( 'mock1', 'mock2' ) );
1615 $result->setContinueParam( $allModules[1], 'm2continue', 1 );
1616 $this->fail( 'Expected exception not thrown' );
1617 } catch ( UnexpectedValueException
$ex ) {
1619 'Module \'mock2\' was not supposed to have been executed, but it was executed anyway',
1621 'Expected exception'
1625 $result->setContinueParam( $allModules[2], 'mlcontinue', 1 );
1626 $this->fail( 'Expected exception not thrown' );
1627 } catch ( UnexpectedValueException
$ex ) {
1629 'Module \'mocklist\' called ApiContinuationManager::addContinueParam ' .
1630 'but was not passed to ApiContinuationManager::__construct',
1632 'Expected exception'
1635 $main->setContinuationManager( null );
1639 public function testObjectSerialization() {
1641 ApiResult
::setValue( $arr, 'foo', (object)array( 'a' => 1, 'b' => 2 ) );
1642 $this->assertSame( array(
1645 ApiResult
::META_TYPE
=> 'assoc',
1649 ApiResult
::setValue( $arr, 'foo', new ApiResultTestStringifiableObject() );
1650 $this->assertSame( 'Ok', $arr['foo'] );
1653 ApiResult
::setValue( $arr, 'foo', new ApiResultTestSerializableObject( 'Ok' ) );
1654 $this->assertSame( 'Ok', $arr['foo'] );
1658 ApiResult
::setValue( $arr, 'foo', new ApiResultTestSerializableObject(
1659 new ApiResultTestStringifiableObject()
1661 $this->fail( 'Expected exception not thrown' );
1662 } catch ( UnexpectedValueException
$ex ) {
1664 'ApiResultTestSerializableObject::serializeForApiResult() ' .
1665 'returned an object of class ApiResultTestStringifiableObject',
1667 'Expected exception'
1673 ApiResult
::setValue( $arr, 'foo', new ApiResultTestSerializableObject( NAN
) );
1674 $this->fail( 'Expected exception not thrown' );
1675 } catch ( UnexpectedValueException
$ex ) {
1677 'ApiResultTestSerializableObject::serializeForApiResult() ' .
1678 'returned an invalid value: Cannot add non-finite floats to ApiResult',
1680 'Expected exception'
1685 ApiResult
::setValue( $arr, 'foo', new ApiResultTestSerializableObject(
1687 'one' => new ApiResultTestStringifiableObject( '1' ),
1688 'two' => new ApiResultTestSerializableObject( 2 ),
1691 $this->assertSame( array(
1699 class ApiResultTestStringifiableObject
{
1702 public function __construct( $ret = 'Ok' ) {
1706 public function __toString() {
1711 class ApiResultTestSerializableObject
{
1714 public function __construct( $ret ) {
1718 public function __toString() {
1722 public function serializeForApiResult() {