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