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',
185 $result2 = new ApiResult( 8388608 );
186 $result2->addValue( null, 'foo', 'bar' );
187 ApiResult
::setValue( $arr, 'baz', $result2 );
188 $this->assertSame( array(
190 ApiResult
::META_TYPE
=> 'assoc',
196 ApiResult
::setValue( $arr, 'foo', "foo\x80bar" );
197 ApiResult
::setValue( $arr, 'bar', "a\xcc\x81" );
198 ApiResult
::setValue( $arr, 'baz', 74 );
199 ApiResult
::setValue( $arr, null, "foo\x80bar" );
200 ApiResult
::setValue( $arr, null, "a\xcc\x81" );
201 $this->assertSame( array(
202 'foo' => "foo\xef\xbf\xbdbar",
205 0 => "foo\xef\xbf\xbdbar",
213 public function testInstanceDataMethods() {
214 $result = new ApiResult( 8388608 );
216 $result->addValue( null, 'setValue', '1' );
218 $result->addValue( null, null, 'unnamed 1' );
219 $result->addValue( null, null, 'unnamed 2' );
221 $result->addValue( null, 'deleteValue', '2' );
222 $result->removeValue( null, 'deleteValue' );
224 $result->addValue( array( 'a', 'b' ), 'deleteValue', '3' );
225 $result->removeValue( array( 'a', 'b', 'deleteValue' ), null, '3' );
227 $result->addContentValue( null, 'setContentValue', '3' );
229 $this->assertSame( array(
233 'a' => array( 'b' => array() ),
234 'setContentValue' => '3',
235 ApiResult
::META_TYPE
=> 'assoc',
236 ApiResult
::META_CONTENT
=> 'setContentValue',
237 ), $result->getResultData() );
238 $this->assertSame( 20, $result->getSize() );
241 $result->addValue( null, 'setValue', '99' );
242 $this->fail( 'Expected exception not thrown' );
243 } catch ( RuntimeException
$ex ) {
245 'Attempting to add element setValue=99, existing value is 1',
252 $result->addContentValue( null, 'setContentValue2', '99' );
253 $this->fail( 'Expected exception not thrown' );
254 } catch ( RuntimeException
$ex ) {
256 'Attempting to set content element as setContentValue2 when setContentValue ' .
257 'is already set as the content element',
263 $result->addValue( null, 'setValue', '99', ApiResult
::OVERRIDE
);
264 $this->assertSame( '99', $result->getResultData( array( 'setValue' ) ) );
266 $result->addContentValue( null, 'setContentValue2', '99', ApiResult
::OVERRIDE
);
267 $this->assertSame( 'setContentValue2',
268 $result->getResultData( array( ApiResult
::META_CONTENT
) ) );
271 $this->assertSame( array(
272 ApiResult
::META_TYPE
=> 'assoc',
273 ), $result->getResultData() );
274 $this->assertSame( 0, $result->getSize() );
276 $result->addValue( null, 'foo', 1 );
277 $result->addValue( null, 'bar', 1 );
278 $result->addValue( null, 'top', '2', ApiResult
::ADD_ON_TOP
);
279 $result->addValue( null, null, '2', ApiResult
::ADD_ON_TOP
);
280 $result->addValue( null, 'bottom', '2' );
281 $result->addValue( null, 'foo', '2', ApiResult
::OVERRIDE
);
282 $result->addValue( null, 'bar', '2', ApiResult
::OVERRIDE | ApiResult
::ADD_ON_TOP
);
283 $this->assertSame( array( 0, 'top', 'foo', 'bar', 'bottom', ApiResult
::META_TYPE
),
284 array_keys( $result->getResultData() ) );
287 $result->addValue( null, 'foo', array( 'bar' => 1 ) );
288 $result->addValue( array( 'foo', 'top' ), 'x', 2, ApiResult
::ADD_ON_TOP
);
289 $result->addValue( array( 'foo', 'bottom' ), 'x', 2 );
290 $this->assertSame( array( 'top', 'bar', 'bottom' ),
291 array_keys( $result->getResultData( array( 'foo' ) ) ) );
294 $result->addValue( null, 'sub', array( 'foo' => 1 ) );
295 $result->addValue( null, 'sub', array( 'bar' => 1 ) );
296 $this->assertSame( array(
297 'sub' => array( 'foo' => 1, 'bar' => 1 ),
298 ApiResult
::META_TYPE
=> 'assoc',
299 ), $result->getResultData() );
302 $result->addValue( null, 'sub', array( 'foo' => 2, 'baz' => 2 ) );
303 $this->fail( 'Expected exception not thrown' );
304 } catch ( RuntimeException
$ex ) {
306 'Conflicting keys (foo) when attempting to merge element sub',
313 $title = Title
::newFromText( "MediaWiki:Foobar" );
317 $result->addValue( null, 'title', $title );
318 $result->addValue( null, 'obj', $obj );
319 $this->assertSame( array(
320 'title' => (string)$title,
321 'obj' => array( 'foo' => 1, 'bar' => 2, ApiResult
::META_TYPE
=> 'assoc' ),
322 ApiResult
::META_TYPE
=> 'assoc',
323 ), $result->getResultData() );
327 $result->addValue( null, 'file', $fh );
328 $this->fail( 'Expected exception not thrown' );
329 } catch ( InvalidArgumentException
$ex ) {
331 'Cannot add resource(stream) to ApiResult',
337 $result->addValue( null, null, $fh );
338 $this->fail( 'Expected exception not thrown' );
339 } catch ( InvalidArgumentException
$ex ) {
341 'Cannot add resource(stream) to ApiResult',
348 $result->addValue( null, 'sub', $obj );
349 $this->fail( 'Expected exception not thrown' );
350 } catch ( InvalidArgumentException
$ex ) {
352 'Cannot add resource(stream) to ApiResult',
359 $result->addValue( null, null, $obj );
360 $this->fail( 'Expected exception not thrown' );
361 } catch ( InvalidArgumentException
$ex ) {
363 'Cannot add resource(stream) to ApiResult',
371 $result->addValue( null, 'inf', INF
);
372 $this->fail( 'Expected exception not thrown' );
373 } catch ( InvalidArgumentException
$ex ) {
375 'Cannot add non-finite floats to ApiResult',
381 $result->addValue( null, null, INF
);
382 $this->fail( 'Expected exception not thrown' );
383 } catch ( InvalidArgumentException
$ex ) {
385 'Cannot add non-finite floats to ApiResult',
391 $result->addValue( null, 'nan', NAN
);
392 $this->fail( 'Expected exception not thrown' );
393 } catch ( InvalidArgumentException
$ex ) {
395 'Cannot add non-finite floats to ApiResult',
401 $result->addValue( null, null, NAN
);
402 $this->fail( 'Expected exception not thrown' );
403 } catch ( InvalidArgumentException
$ex ) {
405 'Cannot add non-finite floats to ApiResult',
412 $result->addParsedLimit( 'foo', 12 );
413 $this->assertSame( array(
414 'limits' => array( 'foo' => 12 ),
415 ApiResult
::META_TYPE
=> 'assoc',
416 ), $result->getResultData() );
417 $result->addParsedLimit( 'foo', 13 );
418 $this->assertSame( array(
419 'limits' => array( 'foo' => 13 ),
420 ApiResult
::META_TYPE
=> 'assoc',
421 ), $result->getResultData() );
422 $this->assertSame( null, $result->getResultData( array( 'foo', 'bar', 'baz' ) ) );
423 $this->assertSame( 13, $result->getResultData( array( 'limits', 'foo' ) ) );
425 $result->getResultData( array( 'limits', 'foo', 'bar' ) );
426 $this->fail( 'Expected exception not thrown' );
427 } catch ( InvalidArgumentException
$ex ) {
429 'Path limits.foo is not an array',
435 $result = new ApiResult( 10 );
436 $formatter = new ApiErrorFormatter( $result, Language
::factory( 'en' ), 'none', false );
437 $result->setErrorFormatter( $formatter );
438 $this->assertFalse( $result->addValue( null, 'foo', '12345678901' ) );
439 $this->assertTrue( $result->addValue( null, 'foo', '12345678901', ApiResult
::NO_SIZE_CHECK
) );
440 $this->assertSame( 0, $result->getSize() );
442 $this->assertTrue( $result->addValue( null, 'foo', '1234567890' ) );
443 $this->assertFalse( $result->addValue( null, 'foo', '1' ) );
444 $result->removeValue( null, 'foo' );
445 $this->assertTrue( $result->addValue( null, 'foo', '1' ) );
447 $result = new ApiResult( 8388608 );
448 $result2 = new ApiResult( 8388608 );
449 $result2->addValue( null, 'foo', 'bar' );
450 $result->addValue( null, 'baz', $result2 );
451 $this->assertSame( array(
454 ApiResult
::META_TYPE
=> 'assoc',
456 ApiResult
::META_TYPE
=> 'assoc',
457 ), $result->getResultData() );
459 $result = new ApiResult( 8388608 );
460 $result->addValue( null, 'foo', "foo\x80bar" );
461 $result->addValue( null, 'bar', "a\xcc\x81" );
462 $result->addValue( null, 'baz', 74 );
463 $result->addValue( null, null, "foo\x80bar" );
464 $result->addValue( null, null, "a\xcc\x81" );
465 $this->assertSame( array(
466 'foo' => "foo\xef\xbf\xbdbar",
469 0 => "foo\xef\xbf\xbdbar",
471 ApiResult
::META_TYPE
=> 'assoc',
472 ), $result->getResultData() );
478 public function testMetadata() {
479 $arr = array( 'foo' => array( 'bar' => array() ) );
480 $result = new ApiResult( 8388608 );
481 $result->addValue( null, 'foo', array( 'bar' => array() ) );
486 ApiResult
::META_INDEXED_TAG_NAME
=> 'ritn',
487 ApiResult
::META_TYPE
=> 'default',
489 ApiResult
::META_INDEXED_TAG_NAME
=> 'ritn',
490 ApiResult
::META_TYPE
=> 'default',
492 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
493 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
494 ApiResult
::META_PRESERVE_KEYS
=> array( 'foo', 'bar' ),
495 ApiResult
::META_TYPE
=> 'array',
498 ApiResult
::setSubelementsList( $arr, 'foo' );
499 ApiResult
::setSubelementsList( $arr, array( 'bar', 'baz' ) );
500 ApiResult
::unsetSubelementsList( $arr, 'baz' );
501 ApiResult
::setIndexedTagNameRecursive( $arr, 'ritn' );
502 ApiResult
::setIndexedTagName( $arr, 'itn' );
503 ApiResult
::setPreserveKeysList( $arr, 'foo' );
504 ApiResult
::setPreserveKeysList( $arr, array( 'bar', 'baz' ) );
505 ApiResult
::unsetPreserveKeysList( $arr, 'baz' );
506 ApiResult
::setArrayTypeRecursive( $arr, 'default' );
507 ApiResult
::setArrayType( $arr, 'array' );
508 $this->assertSame( $expect, $arr );
510 $result->addSubelementsList( null, 'foo' );
511 $result->addSubelementsList( null, array( 'bar', 'baz' ) );
512 $result->removeSubelementsList( null, 'baz' );
513 $result->addIndexedTagNameRecursive( null, 'ritn' );
514 $result->addIndexedTagName( null, 'itn' );
515 $result->addPreserveKeysList( null, 'foo' );
516 $result->addPreserveKeysList( null, array( 'bar', 'baz' ) );
517 $result->removePreserveKeysList( null, 'baz' );
518 $result->addArrayTypeRecursive( null, 'default' );
519 $result->addArrayType( null, 'array' );
520 $this->assertEquals( $expect, $result->getResultData() );
522 $arr = array( 'foo' => array( 'bar' => array() ) );
526 ApiResult
::META_TYPE
=> 'kvp',
527 ApiResult
::META_KVP_KEY_NAME
=> 'key',
529 ApiResult
::META_TYPE
=> 'kvp',
530 ApiResult
::META_KVP_KEY_NAME
=> 'key',
532 ApiResult
::META_TYPE
=> 'BCkvp',
533 ApiResult
::META_KVP_KEY_NAME
=> 'bc',
535 ApiResult
::setArrayTypeRecursive( $arr, 'kvp', 'key' );
536 ApiResult
::setArrayType( $arr, 'BCkvp', 'bc' );
537 $this->assertSame( $expect, $arr );
543 public function testUtilityFunctions() {
546 'bar' => array( '_dummy' => 'foobaz' ),
547 'bar2' => (object)array( '_dummy' => 'foobaz' ),
549 '_dummy' => 'foobaz',
551 'foo2' => (object)array(
552 'bar' => array( '_dummy' => 'foobaz' ),
553 'bar2' => (object)array( '_dummy' => 'foobaz' ),
555 '_dummy' => 'foobaz',
557 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
558 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
559 ApiResult
::META_PRESERVE_KEYS
=> array( 'foo', 'bar', '_dummy2', 0 ),
560 ApiResult
::META_TYPE
=> 'array',
561 '_dummy' => 'foobaz',
562 '_dummy2' => 'foobaz!',
564 $this->assertEquals( array(
567 'bar2' => (object)array(),
570 'foo2' => (object)array(
572 'bar2' => (object)array(),
575 '_dummy2' => 'foobaz!',
576 ), ApiResult
::stripMetadata( $arr ), 'ApiResult::stripMetadata' );
579 $data = ApiResult
::stripMetadataNonRecursive( $arr, $metadata );
580 $this->assertEquals( array(
582 'bar' => array( '_dummy' => 'foobaz' ),
583 'bar2' => (object)array( '_dummy' => 'foobaz' ),
585 '_dummy' => 'foobaz',
587 'foo2' => (object)array(
588 'bar' => array( '_dummy' => 'foobaz' ),
589 'bar2' => (object)array( '_dummy' => 'foobaz' ),
591 '_dummy' => 'foobaz',
593 '_dummy2' => 'foobaz!',
594 ), $data, 'ApiResult::stripMetadataNonRecursive ($data)' );
595 $this->assertEquals( array(
596 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
597 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
598 ApiResult
::META_PRESERVE_KEYS
=> array( 'foo', 'bar', '_dummy2', 0 ),
599 ApiResult
::META_TYPE
=> 'array',
600 '_dummy' => 'foobaz',
601 ), $metadata, 'ApiResult::stripMetadataNonRecursive ($metadata)' );
604 $data = ApiResult
::stripMetadataNonRecursive( (object)$arr, $metadata );
605 $this->assertEquals( (object)array(
607 'bar' => array( '_dummy' => 'foobaz' ),
608 'bar2' => (object)array( '_dummy' => 'foobaz' ),
610 '_dummy' => 'foobaz',
612 'foo2' => (object)array(
613 'bar' => array( '_dummy' => 'foobaz' ),
614 'bar2' => (object)array( '_dummy' => 'foobaz' ),
616 '_dummy' => 'foobaz',
618 '_dummy2' => 'foobaz!',
619 ), $data, 'ApiResult::stripMetadataNonRecursive on object ($data)' );
620 $this->assertEquals( array(
621 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
622 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
623 ApiResult
::META_PRESERVE_KEYS
=> array( 'foo', 'bar', '_dummy2', 0 ),
624 ApiResult
::META_TYPE
=> 'array',
625 '_dummy' => 'foobaz',
626 ), $metadata, 'ApiResult::stripMetadataNonRecursive on object ($metadata)' );
631 * @dataProvider provideTransformations
632 * @param string $label
633 * @param array $input
634 * @param array $transforms
635 * @param array|Exception $expect
637 public function testTransformations( $label, $input, $transforms, $expect ) {
638 $result = new ApiResult( false );
639 $result->addValue( null, 'test', $input );
641 if ( $expect instanceof Exception
) {
643 $output = $result->getResultData( 'test', $transforms );
644 $this->fail( 'Expected exception not thrown', $label );
645 } catch ( Exception
$ex ) {
646 $this->assertEquals( $ex, $expect, $label );
649 $output = $result->getResultData( 'test', $transforms );
650 $this->assertEquals( $expect, $output, $label );
654 public function provideTransformations() {
655 $kvp = function ( $keyKey, $key, $valKey, $value ) {
659 ApiResult
::META_PRESERVE_KEYS
=> array( $keyKey ),
660 ApiResult
::META_CONTENT
=> $valKey,
661 ApiResult
::META_TYPE
=> 'assoc',
665 'defaultArray' => array( 2 => 'a', 0 => 'b', 1 => 'c' ),
666 'defaultAssoc' => array( 'x' => 'a', 1 => 'b', 0 => 'c' ),
667 'defaultAssoc2' => array( 2 => 'a', 3 => 'b', 0 => 'c' ),
668 'array' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'array' ),
669 'BCarray' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'BCarray' ),
670 'BCassoc' => array( 'a', 'b', 'c', ApiResult
::META_TYPE
=> 'BCassoc' ),
671 'assoc' => array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
672 'kvp' => array( 'x' => 'a', 'y' => 'b', 'z' => array( 'c' ), ApiResult
::META_TYPE
=> 'kvp' ),
673 'BCkvp' => array( 'x' => 'a', 'y' => 'b',
674 ApiResult
::META_TYPE
=> 'BCkvp',
675 ApiResult
::META_KVP_KEY_NAME
=> 'key',
677 'emptyDefault' => array( '_dummy' => 1 ),
678 'emptyAssoc' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'assoc' ),
680 ApiResult
::META_PRESERVE_KEYS
=> array( '_dummy' ),
684 'bar' => array( '_dummy' => 'foobaz' ),
686 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
687 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
688 ApiResult
::META_PRESERVE_KEYS
=> array( 'foo', 'bar', '_dummy2', 0 ),
689 ApiResult
::META_TYPE
=> 'array',
692 '_dummy' => 'foobaz',
694 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
695 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
696 ApiResult
::META_PRESERVE_KEYS
=> array( 'foo', 'bar', '_dummy2', 0 ),
697 ApiResult
::META_TYPE
=> 'array',
698 '_dummy' => 'foobaz',
699 '_dummy2' => 'foobaz!',
710 ApiResult
::META_BC_BOOLS
=> array( 0, 'true', 'false' ),
712 array( 'BC' => array() ),
717 ApiResult
::META_BC_BOOLS
=> array( 0, 'true', 'false' ),
721 'BC: META_BC_SUBELEMENTS',
725 ApiResult
::META_BC_SUBELEMENTS
=> array( 'bc' ),
727 array( 'BC' => array() ),
731 ApiResult
::META_CONTENT
=> '*',
732 ApiResult
::META_TYPE
=> 'assoc',
735 ApiResult
::META_BC_SUBELEMENTS
=> array( 'bc' ),
742 ApiResult
::META_CONTENT
=> 'content',
744 array( 'BC' => array() ),
747 ApiResult
::META_CONTENT
=> '*',
753 'foo' => 'foo value',
754 'bar' => 'bar value',
755 '_baz' => 'baz value',
756 ApiResult
::META_TYPE
=> 'BCkvp',
757 ApiResult
::META_KVP_KEY_NAME
=> 'key',
758 ApiResult
::META_PRESERVE_KEYS
=> array( '_baz' ),
760 array( 'BC' => array() ),
762 $kvp( 'key', 'foo', '*', 'foo value' ),
763 $kvp( 'key', 'bar', '*', 'bar value' ),
764 $kvp( 'key', '_baz', '*', 'baz value' ),
765 ApiResult
::META_TYPE
=> 'array',
766 ApiResult
::META_KVP_KEY_NAME
=> 'key',
767 ApiResult
::META_PRESERVE_KEYS
=> array( '_baz' ),
773 ApiResult
::META_TYPE
=> 'BCarray',
775 array( 'BC' => array() ),
777 ApiResult
::META_TYPE
=> 'default',
783 ApiResult
::META_TYPE
=> 'BCassoc',
785 array( 'BC' => array() ),
787 ApiResult
::META_TYPE
=> 'default',
791 'BC: BCkvp exception',
793 ApiResult
::META_TYPE
=> 'BCkvp',
795 array( 'BC' => array() ),
796 new UnexpectedValueException(
797 'Type "BCkvp" used without setting ApiResult::META_KVP_KEY_NAME metadata item'
801 'BC: nobool, no*, nosub',
805 'content' => 'content',
806 ApiResult
::META_CONTENT
=> 'content',
808 ApiResult
::META_BC_SUBELEMENTS
=> array( 'bc' ),
809 'BCarray' => array( ApiResult
::META_TYPE
=> 'BCarray' ),
810 'BCassoc' => array( ApiResult
::META_TYPE
=> 'BCassoc' ),
812 'foo' => 'foo value',
813 'bar' => 'bar value',
814 '_baz' => 'baz value',
815 ApiResult
::META_TYPE
=> 'BCkvp',
816 ApiResult
::META_KVP_KEY_NAME
=> 'key',
817 ApiResult
::META_PRESERVE_KEYS
=> array( '_baz' ),
820 array( 'BC' => array( 'nobool', 'no*', 'nosub' ) ),
824 'content' => 'content',
826 'BCarray' => array( ApiResult
::META_TYPE
=> 'default' ),
827 'BCassoc' => array( ApiResult
::META_TYPE
=> 'default' ),
829 $kvp( 'key', 'foo', '*', 'foo value' ),
830 $kvp( 'key', 'bar', '*', 'bar value' ),
831 $kvp( 'key', '_baz', '*', 'baz value' ),
832 ApiResult
::META_TYPE
=> 'array',
833 ApiResult
::META_KVP_KEY_NAME
=> 'key',
834 ApiResult
::META_PRESERVE_KEYS
=> array( '_baz' ),
836 ApiResult
::META_CONTENT
=> 'content',
837 ApiResult
::META_BC_SUBELEMENTS
=> array( 'bc' ),
842 'Types: Normal transform',
844 array( 'Types' => array() ),
846 'defaultArray' => array( 'b', 'c', 'a', ApiResult
::META_TYPE
=> 'array' ),
847 'defaultAssoc' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
848 'defaultAssoc2' => array( 2 => 'a', 3 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
849 'array' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
850 'BCarray' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
851 'BCassoc' => array( 'a', 'b', 'c', ApiResult
::META_TYPE
=> 'assoc' ),
852 'assoc' => array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
853 'kvp' => array( 'x' => 'a', 'y' => 'b',
854 'z' => array( 'c', ApiResult
::META_TYPE
=> 'array' ),
855 ApiResult
::META_TYPE
=> 'assoc'
857 'BCkvp' => array( 'x' => 'a', 'y' => 'b',
858 ApiResult
::META_TYPE
=> 'assoc',
859 ApiResult
::META_KVP_KEY_NAME
=> 'key',
861 'emptyDefault' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'array' ),
862 'emptyAssoc' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'assoc' ),
864 ApiResult
::META_PRESERVE_KEYS
=> array( '_dummy' ),
865 ApiResult
::META_TYPE
=> 'assoc',
869 'Types: AssocAsObject',
871 array( 'Types' => array( 'AssocAsObject' => true ) ),
873 'defaultArray' => array( 'b', 'c', 'a', ApiResult
::META_TYPE
=> 'array' ),
874 'defaultAssoc' => (object)array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
875 'defaultAssoc2' => (object)array( 2 => 'a', 3 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
876 'array' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
877 'BCarray' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
878 'BCassoc' => (object)array( 'a', 'b', 'c', ApiResult
::META_TYPE
=> 'assoc' ),
879 'assoc' => (object)array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
880 'kvp' => (object)array( 'x' => 'a', 'y' => 'b',
881 'z' => array( 'c', ApiResult
::META_TYPE
=> 'array' ),
882 ApiResult
::META_TYPE
=> 'assoc'
884 'BCkvp' => (object)array( 'x' => 'a', 'y' => 'b',
885 ApiResult
::META_TYPE
=> 'assoc',
886 ApiResult
::META_KVP_KEY_NAME
=> 'key',
888 'emptyDefault' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'array' ),
889 'emptyAssoc' => (object)array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'assoc' ),
891 ApiResult
::META_PRESERVE_KEYS
=> array( '_dummy' ),
892 ApiResult
::META_TYPE
=> 'assoc',
898 array( 'Types' => array( 'ArmorKVP' => 'name' ) ),
900 'defaultArray' => array( 'b', 'c', 'a', ApiResult
::META_TYPE
=> 'array' ),
901 'defaultAssoc' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
902 'defaultAssoc2' => array( 2 => 'a', 3 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
903 'array' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
904 'BCarray' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
905 'BCassoc' => array( 'a', 'b', 'c', ApiResult
::META_TYPE
=> 'assoc' ),
906 'assoc' => array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
908 $kvp( 'name', 'x', 'value', 'a' ),
909 $kvp( 'name', 'y', 'value', 'b' ),
910 $kvp( 'name', 'z', 'value', array( 'c', ApiResult
::META_TYPE
=> 'array' ) ),
911 ApiResult
::META_TYPE
=> 'array'
914 $kvp( 'key', 'x', 'value', 'a' ),
915 $kvp( 'key', 'y', 'value', 'b' ),
916 ApiResult
::META_TYPE
=> 'array',
917 ApiResult
::META_KVP_KEY_NAME
=> 'key',
919 'emptyDefault' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'array' ),
920 'emptyAssoc' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'assoc' ),
922 ApiResult
::META_PRESERVE_KEYS
=> array( '_dummy' ),
923 ApiResult
::META_TYPE
=> 'assoc',
927 'Types: ArmorKVP + BC',
929 array( 'BC' => array(), 'Types' => array( 'ArmorKVP' => 'name' ) ),
931 'defaultArray' => array( 'b', 'c', 'a', ApiResult
::META_TYPE
=> 'array' ),
932 'defaultAssoc' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
933 'defaultAssoc2' => array( 2 => 'a', 3 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
934 'array' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
935 'BCarray' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
936 'BCassoc' => array( 'a', 'b', 'c', ApiResult
::META_TYPE
=> 'array' ),
937 'assoc' => array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
939 $kvp( 'name', 'x', '*', 'a' ),
940 $kvp( 'name', 'y', '*', 'b' ),
941 $kvp( 'name', 'z', '*', array( 'c', ApiResult
::META_TYPE
=> 'array' ) ),
942 ApiResult
::META_TYPE
=> 'array'
945 $kvp( 'key', 'x', '*', 'a' ),
946 $kvp( 'key', 'y', '*', 'b' ),
947 ApiResult
::META_TYPE
=> 'array',
948 ApiResult
::META_KVP_KEY_NAME
=> 'key',
950 'emptyDefault' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'array' ),
951 'emptyAssoc' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'assoc' ),
953 ApiResult
::META_PRESERVE_KEYS
=> array( '_dummy' ),
954 ApiResult
::META_TYPE
=> 'assoc',
958 'Types: ArmorKVP + AssocAsObject',
960 array( 'Types' => array( 'ArmorKVP' => 'name', 'AssocAsObject' => true ) ),
962 'defaultArray' => array( 'b', 'c', 'a', ApiResult
::META_TYPE
=> 'array' ),
963 'defaultAssoc' => (object)array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
964 'defaultAssoc2' => (object)array( 2 => 'a', 3 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
965 'array' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
966 'BCarray' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
967 'BCassoc' => (object)array( 'a', 'b', 'c', ApiResult
::META_TYPE
=> 'assoc' ),
968 'assoc' => (object)array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
970 (object)$kvp( 'name', 'x', 'value', 'a' ),
971 (object)$kvp( 'name', 'y', 'value', 'b' ),
972 (object)$kvp( 'name', 'z', 'value', array( 'c', ApiResult
::META_TYPE
=> 'array' ) ),
973 ApiResult
::META_TYPE
=> 'array'
976 (object)$kvp( 'key', 'x', 'value', 'a' ),
977 (object)$kvp( 'key', 'y', 'value', 'b' ),
978 ApiResult
::META_TYPE
=> 'array',
979 ApiResult
::META_KVP_KEY_NAME
=> 'key',
981 'emptyDefault' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'array' ),
982 'emptyAssoc' => (object)array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'assoc' ),
984 ApiResult
::META_PRESERVE_KEYS
=> array( '_dummy' ),
985 ApiResult
::META_TYPE
=> 'assoc',
989 'Types: BCkvp exception',
991 ApiResult
::META_TYPE
=> 'BCkvp',
993 array( 'Types' => array() ),
994 new UnexpectedValueException(
995 'Type "BCkvp" used without setting ApiResult::META_KVP_KEY_NAME metadata item'
1000 'Strip: With ArmorKVP + AssocAsObject transforms',
1002 array( 'Types' => array( 'ArmorKVP' => 'name', 'AssocAsObject' => true ), 'Strip' => 'all' ),
1004 'defaultArray' => array( 'b', 'c', 'a' ),
1005 'defaultAssoc' => (object)array( 'x' => 'a', 1 => 'b', 0 => 'c' ),
1006 'defaultAssoc2' => (object)array( 2 => 'a', 3 => 'b', 0 => 'c' ),
1007 'array' => array( 'a', 'c', 'b' ),
1008 'BCarray' => array( 'a', 'c', 'b' ),
1009 'BCassoc' => (object)array( 'a', 'b', 'c' ),
1010 'assoc' => (object)array( 2 => 'a', 0 => 'b', 1 => 'c' ),
1012 (object)array( 'name' => 'x', 'value' => 'a' ),
1013 (object)array( 'name' => 'y', 'value' => 'b' ),
1014 (object)array( 'name' => 'z', 'value' => array( 'c' ) ),
1017 (object)array( 'key' => 'x', 'value' => 'a' ),
1018 (object)array( 'key' => 'y', 'value' => 'b' ),
1020 'emptyDefault' => array(),
1021 'emptyAssoc' => (object)array(),
1029 array( 'Strip' => 'all' ),
1036 '_dummy2' => 'foobaz!',
1042 array( 'Strip' => 'base' ),
1045 'bar' => array( '_dummy' => 'foobaz' ),
1047 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
1048 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
1049 ApiResult
::META_PRESERVE_KEYS
=> array( 'foo', 'bar', '_dummy2', 0 ),
1050 ApiResult
::META_TYPE
=> 'array',
1053 '_dummy' => 'foobaz',
1055 '_dummy2' => 'foobaz!',
1061 array( 'Strip' => 'bc' ),
1066 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
1067 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
1071 '_dummy2' => 'foobaz!',
1072 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
1073 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
1085 ApiResult
::META_CONTENT
=> 'foo',
1086 ApiResult
::META_PRESERVE_KEYS
=> array( '_dummy2', '_dummy3' ),
1089 'Custom' => array( $this, 'customTransform' ),
1097 'baz' => array( 'a', 'b' ),
1098 '_dummy2' => '_DUMMY2',
1099 '_dummy3' => '_DUMMY3',
1100 ApiResult
::META_CONTENT
=> 'bar',
1108 * Custom transformer for testTransformations
1109 * @param array &$data
1110 * @param array &$metadata
1112 public function customTransform( &$data, &$metadata ) {
1113 // Prevent recursion
1114 if ( isset( $metadata['_added'] ) ) {
1115 $metadata[ApiResult
::META_TYPE
] = 'array';
1119 foreach ( $data as $k => $v ) {
1120 $data[$k] = strtoupper( $k );
1122 $data['baz'] = array( '_added' => 1, 'z' => 'b', 'y' => 'a' );
1123 $metadata[ApiResult
::META_PRESERVE_KEYS
][0] = '_dummy';
1124 $data[ApiResult
::META_CONTENT
] = 'bar';
1130 public function testDeprecatedFunctions() {
1131 // Ignore ApiResult deprecation warnings during this test
1132 set_error_handler( function ( $errno, $errstr ) use ( &$warnings ) {
1133 if ( preg_match( '/Use of ApiResult::\S+ was deprecated in MediaWiki \d+.\d+\./', $errstr ) ) {
1136 if ( preg_match( '/Use of ApiMain to ApiResult::__construct was deprecated in MediaWiki \d+.\d+\./', $errstr ) ) {
1141 $reset = new ScopedCallback( 'restore_error_handler' );
1143 $context = new DerivativeContext( RequestContext
::getMain() );
1144 $context->setConfig( new HashConfig( array(
1145 'APIModules' => array(),
1146 'APIFormatModules' => array(),
1147 'APIMaxResultSize' => 42,
1149 $main = new ApiMain( $context );
1150 $result = TestingAccessWrapper
::newFromObject( new ApiResult( $main ) );
1151 $this->assertSame( 42, $result->maxSize
);
1152 $this->assertSame( $main->getErrorFormatter(), $result->errorFormatter
);
1153 $this->assertSame( $main, $result->mainForContinuation
);
1155 $result = new ApiResult( 8388608 );
1157 $result->addContentValue( null, 'test', 'content' );
1158 $result->addContentValue( array( 'foo', 'bar' ), 'test', 'content' );
1159 $result->addIndexedTagName( null, 'itn' );
1160 $result->addSubelementsList( null, array( 'sub' ) );
1161 $this->assertSame( array(
1168 ), $result->getData() );
1169 $result->setRawMode();
1170 $this->assertSame( array(
1177 '_element' => 'itn',
1178 '_subelements' => array( 'sub' ),
1179 ), $result->getData() );
1182 ApiResult
::setContent( $arr, 'value' );
1183 ApiResult
::setContent( $arr, 'value2', 'foobar' );
1184 $this->assertSame( array(
1185 ApiResult
::META_CONTENT
=> 'content',
1186 'content' => 'value',
1188 ApiResult
::META_CONTENT
=> 'content',
1189 'content' => 'value2',
1193 $result = new ApiResult( 3 );
1194 $formatter = new ApiErrorFormatter_BackCompat( $result );
1195 $result->setErrorFormatter( $formatter );
1196 $result->disableSizeCheck();
1197 $this->assertTrue( $result->addValue( null, 'foo', '1234567890' ) );
1198 $result->enableSizeCheck();
1199 $this->assertSame( 0, $result->getSize() );
1200 $this->assertFalse( $result->addValue( null, 'foo', '1234567890' ) );
1202 $arr = array( 'foo' => array( 'bar' => 1 ) );
1203 $result->setIndexedTagName_recursive( $arr, 'itn' );
1204 $this->assertSame( array(
1207 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn'
1211 $status = Status
::newGood();
1212 $status->fatal( 'parentheses', '1' );
1213 $status->fatal( 'parentheses', '2' );
1214 $status->warning( 'parentheses', '3' );
1215 $status->warning( 'parentheses', '4' );
1216 $this->assertSame( array(
1219 'message' => 'parentheses',
1222 ApiResult
::META_INDEXED_TAG_NAME
=> 'param',
1227 'message' => 'parentheses',
1230 ApiResult
::META_INDEXED_TAG_NAME
=> 'param',
1233 ApiResult
::META_INDEXED_TAG_NAME
=> 'error',
1234 ), $result->convertStatusToArray( $status, 'error' ) );
1235 $this->assertSame( array(
1237 'type' => 'warning',
1238 'message' => 'parentheses',
1241 ApiResult
::META_INDEXED_TAG_NAME
=> 'param',
1245 'type' => 'warning',
1246 'message' => 'parentheses',
1249 ApiResult
::META_INDEXED_TAG_NAME
=> 'param',
1252 ApiResult
::META_INDEXED_TAG_NAME
=> 'warning',
1253 ), $result->convertStatusToArray( $status, 'warning' ) );
1259 public function testDeprecatedContinuation() {
1260 // Ignore ApiResult deprecation warnings during this test
1261 set_error_handler( function ( $errno, $errstr ) use ( &$warnings ) {
1262 if ( preg_match( '/Use of ApiResult::\S+ was deprecated in MediaWiki \d+.\d+\./', $errstr ) ) {
1268 $reset = new ScopedCallback( 'restore_error_handler' );
1269 $allModules = array(
1270 new MockApiQueryBase( 'mock1' ),
1271 new MockApiQueryBase( 'mock2' ),
1272 new MockApiQueryBase( 'mocklist' ),
1274 $generator = new MockApiQueryBase( 'generator' );
1276 $main = new ApiMain( RequestContext
::getMain() );
1277 $result = new ApiResult( 8388608 );
1278 $result->setMainForContinuation( $main );
1279 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1280 $this->assertSame( array( false, $allModules ), $ret );
1281 $result->setContinueParam( $allModules[0], 'm1continue', array( 1, 2 ) );
1282 $result->setContinueParam( $allModules[2], 'mlcontinue', 2 );
1283 $result->setGeneratorContinueParam( $generator, 'gcontinue', 3 );
1284 $result->endContinuation( 'raw' );
1285 $result->endContinuation( 'standard' );
1286 $this->assertSame( array(
1288 'm1continue' => '1|2',
1289 'continue' => '||mock2',
1290 ), $result->getResultData( 'continue' ) );
1291 $this->assertSame( null, $result->getResultData( 'batchcomplete' ) );
1292 $this->assertSame( array(
1293 'mock1' => array( 'm1continue' => '1|2' ),
1294 'mocklist' => array( 'mlcontinue' => 2 ),
1295 'generator' => array( 'gcontinue' => 3 ),
1296 ), $result->getResultData( 'query-continue' ) );
1297 $main->setContinuationManager( null );
1299 $result = new ApiResult( 8388608 );
1300 $result->setMainForContinuation( $main );
1301 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1302 $this->assertSame( array( false, $allModules ), $ret );
1303 $result->setContinueParam( $allModules[0], 'm1continue', array( 1, 2 ) );
1304 $result->setGeneratorContinueParam( $generator, 'gcontinue', array( 3, 4 ) );
1305 $result->endContinuation( 'raw' );
1306 $result->endContinuation( 'standard' );
1307 $this->assertSame( array(
1308 'm1continue' => '1|2',
1309 'continue' => '||mock2|mocklist',
1310 ), $result->getResultData( 'continue' ) );
1311 $this->assertSame( null, $result->getResultData( 'batchcomplete' ) );
1312 $this->assertSame( array(
1313 'mock1' => array( 'm1continue' => '1|2' ),
1314 'generator' => array( 'gcontinue' => '3|4' ),
1315 ), $result->getResultData( 'query-continue' ) );
1316 $main->setContinuationManager( null );
1318 $result = new ApiResult( 8388608 );
1319 $result->setMainForContinuation( $main );
1320 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1321 $this->assertSame( array( false, $allModules ), $ret );
1322 $result->setContinueParam( $allModules[2], 'mlcontinue', 2 );
1323 $result->setGeneratorContinueParam( $generator, 'gcontinue', 3 );
1324 $result->endContinuation( 'raw' );
1325 $result->endContinuation( 'standard' );
1326 $this->assertSame( array(
1329 'continue' => 'gcontinue||',
1330 ), $result->getResultData( 'continue' ) );
1331 $this->assertSame( true, $result->getResultData( 'batchcomplete' ) );
1332 $this->assertSame( array(
1333 'mocklist' => array( 'mlcontinue' => 2 ),
1334 'generator' => array( 'gcontinue' => 3 ),
1335 ), $result->getResultData( 'query-continue' ) );
1336 $main->setContinuationManager( null );
1338 $result = new ApiResult( 8388608 );
1339 $result->setMainForContinuation( $main );
1340 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1341 $this->assertSame( array( false, $allModules ), $ret );
1342 $result->setGeneratorContinueParam( $generator, 'gcontinue', 3 );
1343 $result->endContinuation( 'raw' );
1344 $result->endContinuation( 'standard' );
1345 $this->assertSame( array(
1347 'continue' => 'gcontinue||mocklist',
1348 ), $result->getResultData( 'continue' ) );
1349 $this->assertSame( true, $result->getResultData( 'batchcomplete' ) );
1350 $this->assertSame( array(
1351 'generator' => array( 'gcontinue' => 3 ),
1352 ), $result->getResultData( 'query-continue' ) );
1353 $main->setContinuationManager( null );
1355 $result = new ApiResult( 8388608 );
1356 $result->setMainForContinuation( $main );
1357 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1358 $this->assertSame( array( false, $allModules ), $ret );
1359 $result->setContinueParam( $allModules[0], 'm1continue', array( 1, 2 ) );
1360 $result->setContinueParam( $allModules[2], 'mlcontinue', 2 );
1361 $result->endContinuation( 'raw' );
1362 $result->endContinuation( 'standard' );
1363 $this->assertSame( array(
1365 'm1continue' => '1|2',
1366 'continue' => '||mock2',
1367 ), $result->getResultData( 'continue' ) );
1368 $this->assertSame( null, $result->getResultData( 'batchcomplete' ) );
1369 $this->assertSame( array(
1370 'mock1' => array( 'm1continue' => '1|2' ),
1371 'mocklist' => array( 'mlcontinue' => 2 ),
1372 ), $result->getResultData( 'query-continue' ) );
1373 $main->setContinuationManager( null );
1375 $result = new ApiResult( 8388608 );
1376 $result->setMainForContinuation( $main );
1377 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1378 $this->assertSame( array( false, $allModules ), $ret );
1379 $result->setContinueParam( $allModules[0], 'm1continue', array( 1, 2 ) );
1380 $result->endContinuation( 'raw' );
1381 $result->endContinuation( 'standard' );
1382 $this->assertSame( array(
1383 'm1continue' => '1|2',
1384 'continue' => '||mock2|mocklist',
1385 ), $result->getResultData( 'continue' ) );
1386 $this->assertSame( null, $result->getResultData( 'batchcomplete' ) );
1387 $this->assertSame( array(
1388 'mock1' => array( 'm1continue' => '1|2' ),
1389 ), $result->getResultData( 'query-continue' ) );
1390 $main->setContinuationManager( null );
1392 $result = new ApiResult( 8388608 );
1393 $result->setMainForContinuation( $main );
1394 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1395 $this->assertSame( array( false, $allModules ), $ret );
1396 $result->setContinueParam( $allModules[2], 'mlcontinue', 2 );
1397 $result->endContinuation( 'raw' );
1398 $result->endContinuation( 'standard' );
1399 $this->assertSame( array(
1401 'continue' => '-||mock1|mock2',
1402 ), $result->getResultData( 'continue' ) );
1403 $this->assertSame( true, $result->getResultData( 'batchcomplete' ) );
1404 $this->assertSame( array(
1405 'mocklist' => array( 'mlcontinue' => 2 ),
1406 ), $result->getResultData( 'query-continue' ) );
1407 $main->setContinuationManager( null );
1409 $result = new ApiResult( 8388608 );
1410 $result->setMainForContinuation( $main );
1411 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1412 $this->assertSame( array( false, $allModules ), $ret );
1413 $result->endContinuation( 'raw' );
1414 $result->endContinuation( 'standard' );
1415 $this->assertSame( null, $result->getResultData( 'continue' ) );
1416 $this->assertSame( true, $result->getResultData( 'batchcomplete' ) );
1417 $this->assertSame( null, $result->getResultData( 'query-continue' ) );
1418 $main->setContinuationManager( null );
1420 $result = new ApiResult( 8388608 );
1421 $result->setMainForContinuation( $main );
1422 $ret = $result->beginContinuation( '||mock2', $allModules, array( 'mock1', 'mock2' ) );
1424 array( false, array_values( array_diff_key( $allModules, array( 1 => 1 ) ) ) ),
1427 $main->setContinuationManager( null );
1429 $result = new ApiResult( 8388608 );
1430 $result->setMainForContinuation( $main );
1431 $ret = $result->beginContinuation( '-||', $allModules, array( 'mock1', 'mock2' ) );
1433 array( true, array_values( array_diff_key( $allModules, array( 0 => 0, 1 => 1 ) ) ) ),
1436 $main->setContinuationManager( null );
1438 $result = new ApiResult( 8388608 );
1439 $result->setMainForContinuation( $main );
1441 $result->beginContinuation( 'foo', $allModules, array( 'mock1', 'mock2' ) );
1442 $this->fail( 'Expected exception not thrown' );
1443 } catch ( UsageException
$ex ) {
1445 'Invalid continue param. You should pass the original value returned by the previous query',
1447 'Expected exception'
1450 $main->setContinuationManager( null );
1452 $result = new ApiResult( 8388608 );
1453 $result->setMainForContinuation( $main );
1454 $result->beginContinuation( '||mock2', array_slice( $allModules, 0, 2 ), array( 'mock1', 'mock2' ) );
1456 $result->setContinueParam( $allModules[1], 'm2continue', 1 );
1457 $this->fail( 'Expected exception not thrown' );
1458 } catch ( UnexpectedValueException
$ex ) {
1460 'Module \'mock2\' was not supposed to have been executed, but it was executed anyway',
1462 'Expected exception'
1466 $result->setContinueParam( $allModules[2], 'mlcontinue', 1 );
1467 $this->fail( 'Expected exception not thrown' );
1468 } catch ( UnexpectedValueException
$ex ) {
1470 'Module \'mocklist\' called ApiContinuationManager::addContinueParam but was not passed to ApiContinuationManager::__construct',
1472 'Expected exception'
1475 $main->setContinuationManager( null );
1479 public function testObjectSerialization() {
1481 ApiResult
::setValue( $arr, 'foo', (object)array( 'a' => 1, 'b' => 2 ) );
1482 $this->assertSame( array(
1485 ApiResult
::META_TYPE
=> 'assoc',
1489 ApiResult
::setValue( $arr, 'foo', new ApiResultTestStringifiableObject() );
1490 $this->assertSame( 'Ok', $arr['foo'] );
1493 ApiResult
::setValue( $arr, 'foo', new ApiResultTestSerializableObject( 'Ok' ) );
1494 $this->assertSame( 'Ok', $arr['foo'] );
1498 ApiResult
::setValue( $arr, 'foo', new ApiResultTestSerializableObject(
1499 new ApiResultTestStringifiableObject()
1501 $this->fail( 'Expected exception not thrown' );
1502 } catch ( UnexpectedValueException
$ex ) {
1504 'ApiResultTestSerializableObject::serializeForApiResult() returned an object of class ApiResultTestStringifiableObject',
1506 'Expected exception'
1512 ApiResult
::setValue( $arr, 'foo', new ApiResultTestSerializableObject( NAN
) );
1513 $this->fail( 'Expected exception not thrown' );
1514 } catch ( UnexpectedValueException
$ex ) {
1516 'ApiResultTestSerializableObject::serializeForApiResult() returned an invalid value: Cannot add non-finite floats to ApiResult',
1518 'Expected exception'
1523 ApiResult
::setValue( $arr, 'foo', new ApiResultTestSerializableObject(
1525 'one' => new ApiResultTestStringifiableObject( '1' ),
1526 'two' => new ApiResultTestSerializableObject( 2 ),
1529 $this->assertSame( array(
1537 class ApiResultTestStringifiableObject
{
1540 public function __construct( $ret = 'Ok' ) {
1544 public function __toString() {
1549 class ApiResultTestSerializableObject
{
1552 public function __construct( $ret ) {
1556 public function __toString() {
1560 public function serializeForApiResult() {