db61bc80742002e8de92c6d6b151336f60d7b8f4
[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 // Silence warning
61 if ( !isset( $params['continue'] ) ) {
62 $params['continue'] = '';
63 }
64 $count = 0;
65 $result = array();
66 $continue = array();
67 do {
68 $request = array_merge( $params, $continue );
69 uksort( $request, function ( $a, $b ) {
70 // put 'continue' params at the end - lazy method
71 $a = strpos( $a, 'continue' ) !== false ? 'zzz ' . $a : $a;
72 $b = strpos( $b, 'continue' ) !== false ? 'zzz ' . $b : $b;
73
74 return strcmp( $a, $b );
75 } );
76 $reqStr = http_build_query( $request );
77 //$reqStr = str_replace( '&', ' & ', $reqStr );
78 $this->assertLessThan( $expectedCount, $count, "$id more data: $reqStr" );
79 if ( $this->mVerbose ) {
80 print "$id (#$count): $reqStr\n";
81 }
82 try {
83 $data = $this->doApiRequest( $request );
84 } catch ( Exception $e ) {
85 throw new Exception( "$id on $count", 0, $e );
86 }
87 $data = $data[0];
88 if ( isset( $data['warnings'] ) ) {
89 $warnings = json_encode( $data['warnings'] );
90 $this->fail( "$id Warnings on #$count in $reqStr\n$warnings" );
91 }
92 $this->assertArrayHasKey( 'query', $data, "$id no 'query' on #$count in $reqStr" );
93 if ( isset( $data['continue'] ) ) {
94 $continue = $data['continue'];
95 unset( $data['continue'] );
96 } else {
97 $continue = array();
98 }
99 if ( $this->mVerbose ) {
100 $this->printResult( $data );
101 }
102 $this->mergeResult( $result, $data );
103 $count++;
104 if ( empty( $continue ) ) {
105 $this->assertEquals( $expectedCount, $count, "$id finished early" );
106
107 return $result;
108 } elseif ( !$useContinue ) {
109 $this->assertFalse( 'Non-smart query must be requested all at once' );
110 }
111 } while ( true );
112 }
113
114 /**
115 * @param array $data
116 */
117 private function printResult( $data ) {
118 $q = $data['query'];
119 $print = array();
120 if ( isset( $q['pages'] ) ) {
121 foreach ( $q['pages'] as $p ) {
122 $m = $p['title'];
123 if ( isset( $p['links'] ) ) {
124 $m .= '/[' . implode( ',', array_map(
125 function ( $v ) {
126 return $v['title'];
127 },
128 $p['links'] ) ) . ']';
129 }
130 if ( isset( $p['categories'] ) ) {
131 $m .= '/(' . implode( ',', array_map(
132 function ( $v ) {
133 return str_replace( 'Category:', '', $v['title'] );
134 },
135 $p['categories'] ) ) . ')';
136 }
137 $print[] = $m;
138 }
139 }
140 if ( isset( $q['allcategories'] ) ) {
141 $print[] = '*Cats/(' . implode( ',', array_map(
142 function ( $v ) {
143 return $v['*'];
144 },
145 $q['allcategories'] ) ) . ')';
146 }
147 self::GetItems( $q, 'allpages', 'Pages', $print );
148 self::GetItems( $q, 'alllinks', 'Links', $print );
149 self::GetItems( $q, 'alltransclusions', 'Trnscl', $print );
150 print ' ' . implode( ' ', $print ) . "\n";
151 }
152
153 private static function GetItems( $q, $moduleName, $name, &$print ) {
154 if ( isset( $q[$moduleName] ) ) {
155 $print[] = "*$name/[" . implode( ',',
156 array_map(
157 function ( $v ) {
158 return $v['title'];
159 },
160 $q[$moduleName] ) ) . ']';
161 }
162 }
163
164 /**
165 * Recursively merge the new result returned from the query to the previous results.
166 * @param mixed $results
167 * @param mixed $newResult
168 * @param bool $numericIds If true, treat keys as ids to be merged instead of appending
169 */
170 protected function mergeResult( &$results, $newResult, $numericIds = false ) {
171 $this->assertEquals(
172 is_array( $results ),
173 is_array( $newResult ),
174 'Type of result and data do not match'
175 );
176 if ( !is_array( $results ) ) {
177 $this->assertEquals( $results, $newResult, 'Repeated result must be the same as before' );
178 } else {
179 $sort = null;
180 foreach ( $newResult as $key => $value ) {
181 if ( !$numericIds && $sort === null ) {
182 if ( !is_array( $value ) ) {
183 $sort = false;
184 } elseif ( array_key_exists( 'title', $value ) ) {
185 $sort = function ( $a, $b ) {
186 return strcmp( $a['title'], $b['title'] );
187 };
188 } else {
189 $sort = false;
190 }
191 }
192 $keyExists = array_key_exists( $key, $results );
193 if ( is_numeric( $key ) ) {
194 if ( $numericIds ) {
195 if ( !$keyExists ) {
196 $results[$key] = $value;
197 } else {
198 $this->mergeResult( $results[$key], $value );
199 }
200 } else {
201 $results[] = $value;
202 }
203 } elseif ( !$keyExists ) {
204 $results[$key] = $value;
205 } else {
206 $this->mergeResult( $results[$key], $value, $key === 'pages' );
207 }
208 }
209 if ( $numericIds ) {
210 ksort( $results, SORT_NUMERIC );
211 } elseif ( $sort !== null && $sort !== false ) {
212 usort( $results, $sort );
213 }
214 }
215 }
216 }