API: Overhaul ApiResult, make format=xml not throw, and add json formatversion
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiContinuationManagerTest.php
1 <?php
2
3 /**
4 * @covers ApiContinuationManager
5 * @group API
6 */
7 class ApiContinuationManagerTest extends MediaWikiTestCase {
8
9 private static function getManager( $continue, $allModules, $generatedModules ) {
10 $context = new DerivativeContext( RequestContext::getMain() );
11 $context->setRequest( new FauxRequest( array( 'continue' => $continue ) ) );
12 $main = new ApiMain( $context );
13 return new ApiContinuationManager( $main, $allModules, $generatedModules );
14 }
15
16 public function testContinuation() {
17 $allModules = array(
18 new MockApiQueryBase( 'mock1' ),
19 new MockApiQueryBase( 'mock2' ),
20 new MockApiQueryBase( 'mocklist' ),
21 );
22 $generator = new MockApiQueryBase( 'generator' );
23
24 $manager = self::getManager( '', $allModules, array( 'mock1', 'mock2' ) );
25 $this->assertSame( 'ApiMain', $manager->getSource() );
26 $this->assertSame( false, $manager->isGeneratorDone() );
27 $this->assertSame( $allModules, $manager->getRunModules() );
28 $manager->addContinueParam( $allModules[0], 'm1continue', array( 1, 2 ) );
29 $manager->addContinueParam( $allModules[2], 'mlcontinue', 2 );
30 $manager->addGeneratorContinueParam( $generator, 'gcontinue', 3 );
31 $this->assertSame( array( array(
32 'mlcontinue' => 2,
33 'm1continue' => '1|2',
34 'continue' => '||mock2',
35 ), false ), $manager->getContinuation() );
36 $this->assertSame( array(
37 'mock1' => array( 'm1continue' => '1|2' ),
38 'mocklist' => array( 'mlcontinue' => 2 ),
39 'generator' => array( 'gcontinue' => 3 ),
40 ), $manager->getRawContinuation() );
41
42 $result = new ApiResult( 0 );
43 $manager->setContinuationIntoResult( $result );
44 $this->assertSame( array(
45 'mlcontinue' => 2,
46 'm1continue' => '1|2',
47 'continue' => '||mock2',
48 ), $result->getResultData( 'continue' ) );
49 $this->assertSame( null, $result->getResultData( 'batchcomplete' ) );
50
51 $manager = self::getManager( '', $allModules, array( 'mock1', 'mock2' ) );
52 $this->assertSame( false, $manager->isGeneratorDone() );
53 $this->assertSame( $allModules, $manager->getRunModules() );
54 $manager->addContinueParam( $allModules[0], 'm1continue', array( 1, 2 ) );
55 $manager->addGeneratorContinueParam( $generator, 'gcontinue', array( 3, 4 ) );
56 $this->assertSame( array( array(
57 'm1continue' => '1|2',
58 'continue' => '||mock2|mocklist',
59 ), false ), $manager->getContinuation() );
60 $this->assertSame( array(
61 'mock1' => array( 'm1continue' => '1|2' ),
62 'generator' => array( 'gcontinue' => '3|4' ),
63 ), $manager->getRawContinuation() );
64
65 $manager = self::getManager( '', $allModules, array( 'mock1', 'mock2' ) );
66 $this->assertSame( false, $manager->isGeneratorDone() );
67 $this->assertSame( $allModules, $manager->getRunModules() );
68 $manager->addContinueParam( $allModules[2], 'mlcontinue', 2 );
69 $manager->addGeneratorContinueParam( $generator, 'gcontinue', 3 );
70 $this->assertSame( array( array(
71 'mlcontinue' => 2,
72 'gcontinue' => 3,
73 'continue' => 'gcontinue||',
74 ), true ), $manager->getContinuation() );
75 $this->assertSame( array(
76 'mocklist' => array( 'mlcontinue' => 2 ),
77 'generator' => array( 'gcontinue' => 3 ),
78 ), $manager->getRawContinuation() );
79
80 $result = new ApiResult( 0 );
81 $manager->setContinuationIntoResult( $result );
82 $this->assertSame( array(
83 'mlcontinue' => 2,
84 'gcontinue' => 3,
85 'continue' => 'gcontinue||',
86 ), $result->getResultData( 'continue' ) );
87 $this->assertSame( '', $result->getResultData( 'batchcomplete' ) );
88
89 $manager = self::getManager( '', $allModules, array( 'mock1', 'mock2' ) );
90 $this->assertSame( false, $manager->isGeneratorDone() );
91 $this->assertSame( $allModules, $manager->getRunModules() );
92 $manager->addGeneratorContinueParam( $generator, 'gcontinue', 3 );
93 $this->assertSame( array( array(
94 'gcontinue' => 3,
95 'continue' => 'gcontinue||mocklist',
96 ), true ), $manager->getContinuation() );
97 $this->assertSame( array(
98 'generator' => array( 'gcontinue' => 3 ),
99 ), $manager->getRawContinuation() );
100
101 $manager = self::getManager( '', $allModules, array( 'mock1', 'mock2' ) );
102 $this->assertSame( false, $manager->isGeneratorDone() );
103 $this->assertSame( $allModules, $manager->getRunModules() );
104 $manager->addContinueParam( $allModules[0], 'm1continue', array( 1, 2 ) );
105 $manager->addContinueParam( $allModules[2], 'mlcontinue', 2 );
106 $this->assertSame( array( array(
107 'mlcontinue' => 2,
108 'm1continue' => '1|2',
109 'continue' => '||mock2',
110 ), false ), $manager->getContinuation() );
111 $this->assertSame( array(
112 'mock1' => array( 'm1continue' => '1|2' ),
113 'mocklist' => array( 'mlcontinue' => 2 ),
114 ), $manager->getRawContinuation() );
115
116 $manager = self::getManager( '', $allModules, array( 'mock1', 'mock2' ) );
117 $this->assertSame( false, $manager->isGeneratorDone() );
118 $this->assertSame( $allModules, $manager->getRunModules() );
119 $manager->addContinueParam( $allModules[0], 'm1continue', array( 1, 2 ) );
120 $this->assertSame( array( array(
121 'm1continue' => '1|2',
122 'continue' => '||mock2|mocklist',
123 ), false ), $manager->getContinuation() );
124 $this->assertSame( array(
125 'mock1' => array( 'm1continue' => '1|2' ),
126 ), $manager->getRawContinuation() );
127
128 $manager = self::getManager( '', $allModules, array( 'mock1', 'mock2' ) );
129 $this->assertSame( false, $manager->isGeneratorDone() );
130 $this->assertSame( $allModules, $manager->getRunModules() );
131 $manager->addContinueParam( $allModules[2], 'mlcontinue', 2 );
132 $this->assertSame( array( array(
133 'mlcontinue' => 2,
134 'continue' => '-||mock1|mock2',
135 ), true ), $manager->getContinuation() );
136 $this->assertSame( array(
137 'mocklist' => array( 'mlcontinue' => 2 ),
138 ), $manager->getRawContinuation() );
139
140 $manager = self::getManager( '', $allModules, array( 'mock1', 'mock2' ) );
141 $this->assertSame( false, $manager->isGeneratorDone() );
142 $this->assertSame( $allModules, $manager->getRunModules() );
143 $this->assertSame( array( array(), true ), $manager->getContinuation() );
144 $this->assertSame( array(), $manager->getRawContinuation() );
145
146 $manager = self::getManager( '||mock2', $allModules, array( 'mock1', 'mock2' ) );
147 $this->assertSame( false, $manager->isGeneratorDone() );
148 $this->assertSame(
149 array_values( array_diff_key( $allModules, array( 1 => 1 ) ) ),
150 $manager->getRunModules()
151 );
152
153 $manager = self::getManager( '-||', $allModules, array( 'mock1', 'mock2' ) );
154 $this->assertSame( true, $manager->isGeneratorDone() );
155 $this->assertSame(
156 array_values( array_diff_key( $allModules, array( 0 => 0, 1 => 1 ) ) ),
157 $manager->getRunModules()
158 );
159
160 try {
161 self::getManager( 'foo', $allModules, array( 'mock1', 'mock2' ) );
162 $this->fail( 'Expected exception not thrown' );
163 } catch ( UsageException $ex ) {
164 $this->assertSame(
165 'Invalid continue param. You should pass the original value returned by the previous query',
166 $ex->getMessage(),
167 'Expected exception'
168 );
169 }
170
171 $manager = self::getManager( '||mock2', array_slice( $allModules, 0, 2 ), array( 'mock1', 'mock2' ) );
172 try {
173 $manager->addContinueParam( $allModules[1], 'm2continue', 1 );
174 $this->fail( 'Expected exception not thrown' );
175 } catch ( UnexpectedValueException $ex ) {
176 $this->assertSame(
177 'Module \'mock2\' was not supposed to have been executed, but it was executed anyway',
178 $ex->getMessage(),
179 'Expected exception'
180 );
181 }
182 try {
183 $manager->addContinueParam( $allModules[2], 'mlcontinue', 1 );
184 $this->fail( 'Expected exception not thrown' );
185 } catch ( UnexpectedValueException $ex ) {
186 $this->assertSame(
187 'Module \'mocklist\' called ApiContinuationManager::addContinueParam but was not passed to ApiContinuationManager::__construct',
188 $ex->getMessage(),
189 'Expected exception'
190 );
191 }
192
193 }
194
195 }