Merge "Deprecate ORMTable::getFieldPrefix"
[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
25 require_once 'ApiQueryTestBase.php';
26
27 abstract class ApiQueryContinueTestBase extends ApiQueryTestBase {
28
29 /**
30 * Enable to print in-depth debugging info during the test run
31 */
32 protected $mVerbose = false;
33
34 /**
35 * Run query() and compare against expected values
36 * @param array $expected
37 * @param array $params Api parameters
38 * @param int $expectedCount Max number of iterations
39 * @param string $id Unit test id
40 * @param bool $continue True to use smart continue
41 * @return array Merged results data array
42 */
43 protected function checkC( $expected, $params, $expectedCount, $id, $continue = true ) {
44 $result = $this->query( $params, $expectedCount, $id, $continue );
45 $this->assertResult( $expected, $result, $id );
46 }
47
48 /**
49 * Run query in a loop until no more values are available
50 * @param array $params Api parameters
51 * @param int $expectedCount Max number of iterations
52 * @param string $id Unit test id
53 * @param bool $useContinue True to use smart continue
54 * @return array Merged results data array
55 * @throws Exception
56 */
57 protected function query( $params, $expectedCount, $id, $useContinue = true ) {
58 if ( isset( $params['action'] ) ) {
59 $this->assertEquals( 'query', $params['action'], 'Invalid query action' );
60 } else {
61 $params['action'] = 'query';
62 }
63 if ( $useContinue && !isset( $params['continue'] ) ) {
64 $params['continue'] = '';
65 }
66 $count = 0;
67 $result = array();
68 $continue = array();
69 do {
70 $request = array_merge( $params, $continue );
71 uksort( $request, function ( $a, $b ) {
72 // put 'continue' params at the end - lazy method
73 $a = strpos( $a, 'continue' ) !== false ? 'zzz ' . $a : $a;
74 $b = strpos( $b, 'continue' ) !== false ? 'zzz ' . $b : $b;
75
76 return strcmp( $a, $b );
77 } );
78 $reqStr = http_build_query( $request );
79 //$reqStr = str_replace( '&', ' & ', $reqStr );
80 $this->assertLessThan( $expectedCount, $count, "$id more data: $reqStr" );
81 if ( $this->mVerbose ) {
82 print "$id (#$count): $reqStr\n";
83 }
84 try {
85 $data = $this->doApiRequest( $request );
86 } catch ( Exception $e ) {
87 throw new Exception( "$id on $count", 0, $e );
88 }
89 $data = $data[0];
90 if ( isset( $data['warnings'] ) ) {
91 $warnings = json_encode( $data['warnings'] );
92 $this->fail( "$id Warnings on #$count in $reqStr\n$warnings" );
93 }
94 $this->assertArrayHasKey( 'query', $data, "$id no 'query' on #$count in $reqStr" );
95 if ( isset( $data['continue'] ) ) {
96 $continue = $data['continue'];
97 unset( $data['continue'] );
98 } else {
99 $continue = array();
100 }
101 if ( $this->mVerbose ) {
102 $this->printResult( $data );
103 }
104 $this->mergeResult( $result, $data );
105 $count++;
106 if ( empty( $continue ) ) {
107 $this->assertEquals( $expectedCount, $count, "$id finished early" );
108
109 return $result;
110 } elseif ( !$useContinue ) {
111 $this->assertFalse( 'Non-smart query must be requested all at once' );
112 }
113 } while ( true );
114 }
115
116 /**
117 * @param array $data
118 */
119 private function printResult( $data ) {
120 $q = $data['query'];
121 $print = array();
122 if ( isset( $q['pages'] ) ) {
123 foreach ( $q['pages'] as $p ) {
124 $m = $p['title'];
125 if ( isset( $p['links'] ) ) {
126 $m .= '/[' . implode( ',', array_map(
127 function ( $v ) {
128 return $v['title'];
129 },
130 $p['links'] ) ) . ']';
131 }
132 if ( isset( $p['categories'] ) ) {
133 $m .= '/(' . implode( ',', array_map(
134 function ( $v ) {
135 return str_replace( 'Category:', '', $v['title'] );
136 },
137 $p['categories'] ) ) . ')';
138 }
139 $print[] = $m;
140 }
141 }
142 if ( isset( $q['allcategories'] ) ) {
143 $print[] = '*Cats/(' . implode( ',', array_map(
144 function ( $v ) {
145 return $v['*'];
146 },
147 $q['allcategories'] ) ) . ')';
148 }
149 self::GetItems( $q, 'allpages', 'Pages', $print );
150 self::GetItems( $q, 'alllinks', 'Links', $print );
151 self::GetItems( $q, 'alltransclusions', 'Trnscl', $print );
152 print ' ' . implode( ' ', $print ) . "\n";
153 }
154
155 private static function GetItems( $q, $moduleName, $name, &$print ) {
156 if ( isset( $q[$moduleName] ) ) {
157 $print[] = "*$name/[" . implode( ',',
158 array_map(
159 function ( $v ) {
160 return $v['title'];
161 },
162 $q[$moduleName] ) ) . ']';
163 }
164 }
165
166 /**
167 * Recursively merge the new result returned from the query to the previous results.
168 * @param mixed $results
169 * @param mixed $newResult
170 * @param bool $numericIds If true, treat keys as ids to be merged instead of appending
171 */
172 protected function mergeResult( &$results, $newResult, $numericIds = false ) {
173 $this->assertEquals(
174 is_array( $results ),
175 is_array( $newResult ),
176 'Type of result and data do not match'
177 );
178 if ( !is_array( $results ) ) {
179 $this->assertEquals( $results, $newResult, 'Repeated result must be the same as before' );
180 } else {
181 $sort = null;
182 foreach ( $newResult as $key => $value ) {
183 if ( !$numericIds && $sort === null ) {
184 if ( !is_array( $value ) ) {
185 $sort = false;
186 } elseif ( array_key_exists( 'title', $value ) ) {
187 $sort = function ( $a, $b ) {
188 return strcmp( $a['title'], $b['title'] );
189 };
190 } else {
191 $sort = false;
192 }
193 }
194 $keyExists = array_key_exists( $key, $results );
195 if ( is_numeric( $key ) ) {
196 if ( $numericIds ) {
197 if ( !$keyExists ) {
198 $results[$key] = $value;
199 } else {
200 $this->mergeResult( $results[$key], $value );
201 }
202 } else {
203 $results[] = $value;
204 }
205 } elseif ( !$keyExists ) {
206 $results[$key] = $value;
207 } else {
208 $this->mergeResult( $results[$key], $value, $key === 'pages' );
209 }
210 }
211 if ( $numericIds ) {
212 ksort( $results, SORT_NUMERIC );
213 } elseif ( $sort !== null && $sort !== false ) {
214 usort( $results, $sort );
215 }
216 }
217 }
218 }