3 namespace MediaWiki\Tests\Api
;
5 use MediaWiki\Api\ApiContinuationManager
;
6 use MediaWiki\Api\ApiMain
;
7 use MediaWiki\Api\ApiResult
;
8 use MediaWiki\Api\ApiUsageException
;
9 use MediaWiki\Context\DerivativeContext
;
10 use MediaWiki\Context\RequestContext
;
11 use MediaWiki\Request\FauxRequest
;
12 use UnexpectedValueException
;
15 * @covers \MediaWiki\Api\ApiContinuationManager
18 class ApiContinuationManagerTest
extends ApiTestCase
{
20 private static function getManager( $continue, $allModules, $generatedModules ) {
21 $context = new DerivativeContext( RequestContext
::getMain() );
22 $context->setRequest( new FauxRequest( [ 'continue' => $continue ] ) );
23 $main = new ApiMain( $context );
24 return new ApiContinuationManager( $main, $allModules, $generatedModules );
27 public function testContinuation() {
29 new MockApiQueryBase( 'mock1' ),
30 new MockApiQueryBase( 'mock2' ),
31 new MockApiQueryBase( 'mocklist' ),
33 $generator = new MockApiQueryBase( 'generator' );
35 $manager = self
::getManager( '', $allModules, [ 'mock1', 'mock2' ] );
36 $this->assertSame( ApiMain
::class, $manager->getSource() );
37 $this->assertSame( false, $manager->isGeneratorDone() );
38 $this->assertSame( $allModules, $manager->getRunModules() );
39 $manager->addContinueParam( $allModules[0], 'm1continue', [ 1, 2 ] );
40 $manager->addContinueParam( $allModules[2], 'mlcontinue', 2 );
41 $manager->addGeneratorContinueParam( $generator, 'gcontinue', 3 );
42 $this->assertSame( [ [
44 'm1continue' => '1|2',
45 'continue' => '||mock2',
46 ], false ], $manager->getContinuation() );
48 'mock1' => [ 'm1continue' => '1|2' ],
49 'mocklist' => [ 'mlcontinue' => 2 ],
50 'generator' => [ 'gcontinue' => 3 ],
51 ], $manager->getRawContinuation() );
53 $result = new ApiResult( 0 );
54 $manager->setContinuationIntoResult( $result );
57 'm1continue' => '1|2',
58 'continue' => '||mock2',
59 ], $result->getResultData( 'continue' ) );
60 $this->assertSame( null, $result->getResultData( 'batchcomplete' ) );
62 $manager = self
::getManager( '', $allModules, [ 'mock1', 'mock2' ] );
63 $this->assertSame( false, $manager->isGeneratorDone() );
64 $this->assertSame( $allModules, $manager->getRunModules() );
65 $manager->addContinueParam( $allModules[0], 'm1continue', [ 1, 2 ] );
66 $manager->addGeneratorContinueParam( $generator, 'gcontinue', [ 3, 4 ] );
67 $this->assertSame( [ [
68 'm1continue' => '1|2',
69 'continue' => '||mock2|mocklist',
70 ], false ], $manager->getContinuation() );
72 'mock1' => [ 'm1continue' => '1|2' ],
73 'generator' => [ 'gcontinue' => '3|4' ],
74 ], $manager->getRawContinuation() );
76 $manager = self
::getManager( '', $allModules, [ 'mock1', 'mock2' ] );
77 $this->assertSame( false, $manager->isGeneratorDone() );
78 $this->assertSame( $allModules, $manager->getRunModules() );
79 $manager->addContinueParam( $allModules[2], 'mlcontinue', 2 );
80 $manager->addGeneratorContinueParam( $generator, 'gcontinue', 3 );
81 $this->assertSame( [ [
84 'continue' => 'gcontinue||',
85 ], true ], $manager->getContinuation() );
87 'mocklist' => [ 'mlcontinue' => 2 ],
88 'generator' => [ 'gcontinue' => 3 ],
89 ], $manager->getRawContinuation() );
91 $result = new ApiResult( 0 );
92 $manager->setContinuationIntoResult( $result );
96 'continue' => 'gcontinue||',
97 ], $result->getResultData( 'continue' ) );
98 $this->assertSame( true, $result->getResultData( 'batchcomplete' ) );
100 $manager = self
::getManager( '', $allModules, [ 'mock1', 'mock2' ] );
101 $this->assertSame( false, $manager->isGeneratorDone() );
102 $this->assertSame( $allModules, $manager->getRunModules() );
103 $manager->addGeneratorContinueParam( $generator, 'gcontinue', 3 );
104 $this->assertSame( [ [
106 'continue' => 'gcontinue||mocklist',
107 ], true ], $manager->getContinuation() );
109 'generator' => [ 'gcontinue' => 3 ],
110 ], $manager->getRawContinuation() );
112 $manager = self
::getManager( '', $allModules, [ 'mock1', 'mock2' ] );
113 $this->assertSame( false, $manager->isGeneratorDone() );
114 $this->assertSame( $allModules, $manager->getRunModules() );
115 $manager->addContinueParam( $allModules[0], 'm1continue', [ 1, 2 ] );
116 $manager->addContinueParam( $allModules[2], 'mlcontinue', 2 );
117 $this->assertSame( [ [
119 'm1continue' => '1|2',
120 'continue' => '||mock2',
121 ], false ], $manager->getContinuation() );
123 'mock1' => [ 'm1continue' => '1|2' ],
124 'mocklist' => [ 'mlcontinue' => 2 ],
125 ], $manager->getRawContinuation() );
127 $manager = self
::getManager( '', $allModules, [ 'mock1', 'mock2' ] );
128 $this->assertSame( false, $manager->isGeneratorDone() );
129 $this->assertSame( $allModules, $manager->getRunModules() );
130 $manager->addContinueParam( $allModules[0], 'm1continue', [ 1, 2 ] );
131 $this->assertSame( [ [
132 'm1continue' => '1|2',
133 'continue' => '||mock2|mocklist',
134 ], false ], $manager->getContinuation() );
136 'mock1' => [ 'm1continue' => '1|2' ],
137 ], $manager->getRawContinuation() );
139 $manager = self
::getManager( '', $allModules, [ 'mock1', 'mock2' ] );
140 $this->assertSame( false, $manager->isGeneratorDone() );
141 $this->assertSame( $allModules, $manager->getRunModules() );
142 $manager->addContinueParam( $allModules[2], 'mlcontinue', 2 );
143 $this->assertSame( [ [
145 'continue' => '-||mock1|mock2',
146 ], true ], $manager->getContinuation() );
148 'mocklist' => [ 'mlcontinue' => 2 ],
149 ], $manager->getRawContinuation() );
151 $manager = self
::getManager( '', $allModules, [ 'mock1', 'mock2' ] );
152 $this->assertSame( false, $manager->isGeneratorDone() );
153 $this->assertSame( $allModules, $manager->getRunModules() );
154 $this->assertSame( [ [], true ], $manager->getContinuation() );
155 $this->assertSame( [], $manager->getRawContinuation() );
157 $manager = self
::getManager( '||mock2', $allModules, [ 'mock1', 'mock2' ] );
158 $this->assertSame( false, $manager->isGeneratorDone() );
160 array_values( array_diff_key( $allModules, [ 1 => 1 ] ) ),
161 $manager->getRunModules()
164 $manager = self
::getManager( '-||', $allModules, [ 'mock1', 'mock2' ] );
165 $this->assertSame( true, $manager->isGeneratorDone() );
167 array_values( array_diff_key( $allModules, [ 0 => 0, 1 => 1 ] ) ),
168 $manager->getRunModules()
172 self
::getManager( 'foo', $allModules, [ 'mock1', 'mock2' ] );
173 $this->fail( 'Expected exception not thrown' );
174 } catch ( ApiUsageException
$ex ) {
175 $this->assertApiErrorCode( 'badcontinue', $ex,
180 $manager = self
::getManager(
182 array_slice( $allModules, 0, 2 ),
186 $manager->addContinueParam( $allModules[1], 'm2continue', 1 );
187 $this->fail( 'Expected exception not thrown' );
188 } catch ( UnexpectedValueException
$ex ) {
190 'Module \'mock2\' was not supposed to have been executed, ' .
191 'but it was executed anyway',
197 $manager->addContinueParam( $allModules[2], 'mlcontinue', 1 );
198 $this->fail( 'Expected exception not thrown' );
199 } catch ( UnexpectedValueException
$ex ) {
201 'Module \'mocklist\' called ' . ApiContinuationManager
::class . '::addContinueParam ' .
202 'but was not passed to ' . ApiContinuationManager
::class . '::__construct',