Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / api / ApiContinuationManagerTest.php
blob9c0d886cbae1b60fdafcecb066ed94b046a5d197
1 <?php
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;
14 /**
15 * @covers \MediaWiki\Api\ApiContinuationManager
16 * @group API
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() {
28 $allModules = [
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( [ [
43 'mlcontinue' => 2,
44 'm1continue' => '1|2',
45 'continue' => '||mock2',
46 ], false ], $manager->getContinuation() );
47 $this->assertSame( [
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 );
55 $this->assertSame( [
56 'mlcontinue' => 2,
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() );
71 $this->assertSame( [
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( [ [
82 'mlcontinue' => 2,
83 'gcontinue' => 3,
84 'continue' => 'gcontinue||',
85 ], true ], $manager->getContinuation() );
86 $this->assertSame( [
87 'mocklist' => [ 'mlcontinue' => 2 ],
88 'generator' => [ 'gcontinue' => 3 ],
89 ], $manager->getRawContinuation() );
91 $result = new ApiResult( 0 );
92 $manager->setContinuationIntoResult( $result );
93 $this->assertSame( [
94 'mlcontinue' => 2,
95 'gcontinue' => 3,
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( [ [
105 'gcontinue' => 3,
106 'continue' => 'gcontinue||mocklist',
107 ], true ], $manager->getContinuation() );
108 $this->assertSame( [
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( [ [
118 'mlcontinue' => 2,
119 'm1continue' => '1|2',
120 'continue' => '||mock2',
121 ], false ], $manager->getContinuation() );
122 $this->assertSame( [
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() );
135 $this->assertSame( [
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( [ [
144 'mlcontinue' => 2,
145 'continue' => '-||mock1|mock2',
146 ], true ], $manager->getContinuation() );
147 $this->assertSame( [
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() );
159 $this->assertSame(
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() );
166 $this->assertSame(
167 array_values( array_diff_key( $allModules, [ 0 => 0, 1 => 1 ] ) ),
168 $manager->getRunModules()
171 try {
172 self::getManager( 'foo', $allModules, [ 'mock1', 'mock2' ] );
173 $this->fail( 'Expected exception not thrown' );
174 } catch ( ApiUsageException $ex ) {
175 $this->assertApiErrorCode( 'badcontinue', $ex,
176 'Expected exception'
180 $manager = self::getManager(
181 '||mock2',
182 array_slice( $allModules, 0, 2 ),
183 [ 'mock1', 'mock2' ]
185 try {
186 $manager->addContinueParam( $allModules[1], 'm2continue', 1 );
187 $this->fail( 'Expected exception not thrown' );
188 } catch ( UnexpectedValueException $ex ) {
189 $this->assertSame(
190 'Module \'mock2\' was not supposed to have been executed, ' .
191 'but it was executed anyway',
192 $ex->getMessage(),
193 'Expected exception'
196 try {
197 $manager->addContinueParam( $allModules[2], 'mlcontinue', 1 );
198 $this->fail( 'Expected exception not thrown' );
199 } catch ( UnexpectedValueException $ex ) {
200 $this->assertSame(
201 'Module \'mocklist\' called ' . ApiContinuationManager::class . '::addContinueParam ' .
202 'but was not passed to ' . ApiContinuationManager::class . '::__construct',
203 $ex->getMessage(),
204 'Expected exception'