Merge "API: Remove warning about continuation change"
[lhc/web/wiklou.git] / tests / phpunit / includes / api / query / ApiQueryContinueTestBase.php
1 <?php
2 /**
3 * Created on Jan 1, 2013
4 *
5 * Copyright © 2013 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 */
24 abstract class ApiQueryContinueTestBase extends ApiQueryTestBase {
25
26 /**
27 * Enable to print in-depth debugging info during the test run
28 */
29 protected $mVerbose = false;
30
31 /**
32 * Run query() and compare against expected values
33 * @param array $expected
34 * @param array $params Api parameters
35 * @param int $expectedCount Max number of iterations
36 * @param string $id Unit test id
37 * @param bool $continue True to use smart continue
38 * @return array Merged results data array
39 */
40 protected function checkC( $expected, $params, $expectedCount, $id, $continue = true ) {
41 $result = $this->query( $params, $expectedCount, $id, $continue );
42 $this->assertResult( $expected, $result, $id );
43 }
44
45 /**
46 * Run query in a loop until no more values are available
47 * @param array $params Api parameters
48 * @param int $expectedCount Max number of iterations
49 * @param string $id Unit test id
50 * @param bool $useContinue True to use smart continue
51 * @return array Merged results data array
52 * @throws Exception
53 */
54 protected function query( $params, $expectedCount, $id, $useContinue = true ) {
55 if ( isset( $params['action'] ) ) {
56 $this->assertEquals( 'query', $params['action'], 'Invalid query action' );
57 } else {
58 $params['action'] = 'query';
59 }
60 $count = 0;
61 $result = array();
62 $continue = array();
63 do {
64 $request = array_merge( $params, $continue );
65 uksort( $request, function ( $a, $b ) {
66 // put 'continue' params at the end - lazy method
67 $a = strpos( $a, 'continue' ) !== false ? 'zzz ' . $a : $a;
68 $b = strpos( $b, 'continue' ) !== false ? 'zzz ' . $b : $b;
69
70 return strcmp( $a, $b );
71 } );
72 $reqStr = http_build_query( $request );
73 // $reqStr = str_replace( '&', ' & ', $reqStr );
74 $this->assertLessThan( $expectedCount, $count, "$id more data: $reqStr" );
75 if ( $this->mVerbose ) {
76 print "$id (#$count): $reqStr\n";
77 }
78 try {
79 $data = $this->doApiRequest( $request );
80 } catch ( Exception $e ) {
81 throw new Exception( "$id on $count", 0, $e );
82 }
83 $data = $data[0];
84 if ( isset( $data['warnings'] ) ) {
85 $warnings = json_encode( $data['warnings'] );
86 $this->fail( "$id Warnings on #$count in $reqStr\n$warnings" );
87 }
88 $this->assertArrayHasKey( 'query', $data, "$id no 'query' on #$count in $reqStr" );
89 if ( isset( $data['continue'] ) ) {
90 $continue = $data['continue'];
91 unset( $data['continue'] );
92 } else {
93 $continue = array();
94 }
95 if ( $this->mVerbose ) {
96 $this->printResult( $data );
97 }
98 $this->mergeResult( $result, $data );
99 $count++;
100 if ( empty( $continue ) ) {
101 $this->assertEquals( $expectedCount, $count, "$id finished early" );
102
103 return $result;
104 } elseif ( !$useContinue ) {
105 $this->assertFalse( 'Non-smart query must be requested all at once' );
106 }
107 } while ( true );
108 }
109
110 /**
111 * @param array $data
112 */
113 private function printResult( $data ) {
114 $q = $data['query'];
115 $print = array();
116 if ( isset( $q['pages'] ) ) {
117 foreach ( $q['pages'] as $p ) {
118 $m = $p['title'];
119 if ( isset( $p['links'] ) ) {
120 $m .= '/[' . implode( ',', array_map(
121 function ( $v ) {
122 return $v['title'];
123 },
124 $p['links'] ) ) . ']';
125 }
126 if ( isset( $p['categories'] ) ) {
127 $m .= '/(' . implode( ',', array_map(
128 function ( $v ) {
129 return str_replace( 'Category:', '', $v['title'] );
130 },
131 $p['categories'] ) ) . ')';
132 }
133 $print[] = $m;
134 }
135 }
136 if ( isset( $q['allcategories'] ) ) {
137 $print[] = '*Cats/(' . implode( ',', array_map(
138 function ( $v ) {
139 return $v['*'];
140 },
141 $q['allcategories'] ) ) . ')';
142 }
143 self::GetItems( $q, 'allpages', 'Pages', $print );
144 self::GetItems( $q, 'alllinks', 'Links', $print );
145 self::GetItems( $q, 'alltransclusions', 'Trnscl', $print );
146 print ' ' . implode( ' ', $print ) . "\n";
147 }
148
149 private static function GetItems( $q, $moduleName, $name, &$print ) {
150 if ( isset( $q[$moduleName] ) ) {
151 $print[] = "*$name/[" . implode( ',',
152 array_map(
153 function ( $v ) {
154 return $v['title'];
155 },
156 $q[$moduleName] ) ) . ']';
157 }
158 }
159
160 /**
161 * Recursively merge the new result returned from the query to the previous results.
162 * @param mixed $results
163 * @param mixed $newResult
164 * @param bool $numericIds If true, treat keys as ids to be merged instead of appending
165 */
166 protected function mergeResult( &$results, $newResult, $numericIds = false ) {
167 $this->assertEquals(
168 is_array( $results ),
169 is_array( $newResult ),
170 'Type of result and data do not match'
171 );
172 if ( !is_array( $results ) ) {
173 $this->assertEquals( $results, $newResult, 'Repeated result must be the same as before' );
174 } else {
175 $sort = null;
176 foreach ( $newResult as $key => $value ) {
177 if ( !$numericIds && $sort === null ) {
178 if ( !is_array( $value ) ) {
179 $sort = false;
180 } elseif ( array_key_exists( 'title', $value ) ) {
181 $sort = function ( $a, $b ) {
182 return strcmp( $a['title'], $b['title'] );
183 };
184 } else {
185 $sort = false;
186 }
187 }
188 $keyExists = array_key_exists( $key, $results );
189 if ( is_numeric( $key ) ) {
190 if ( $numericIds ) {
191 if ( !$keyExists ) {
192 $results[$key] = $value;
193 } else {
194 $this->mergeResult( $results[$key], $value );
195 }
196 } else {
197 $results[] = $value;
198 }
199 } elseif ( !$keyExists ) {
200 $results[$key] = $value;
201 } else {
202 $this->mergeResult( $results[$key], $value, $key === 'pages' );
203 }
204 }
205 if ( $numericIds ) {
206 ksort( $results, SORT_NUMERIC );
207 } elseif ( $sort !== null && $sort !== false ) {
208 usort( $results, $sort );
209 }
210 }
211 }
212 }