Merge "Adding python 3 support for the Makefile.py file for the zhtable"
[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 */
37 protected function checkC( $expected, $params, $expectedCount, $id, $continue = true ) {
38 $result = $this->query( $params, $expectedCount, $id, $continue );
39 $this->assertResult( $expected, $result, $id );
40 }
41
42 /**
43 * Run query in a loop until no more values are available
44 * @param array $params api parameters
45 * @param int $expectedCount max number of iterations
46 * @param string $id unit test id
47 * @param boolean $useContinue true to use smart continue
48 * @return mixed: merged results data array
49 * @throws Exception
50 */
51 protected function query( $params, $expectedCount, $id, $useContinue = true ) {
52 if ( isset( $params['action'] ) ) {
53 $this->assertEquals( 'query', $params['action'], 'Invalid query action' );
54 } else {
55 $params['action'] = 'query';
56 }
57 if ( $useContinue && !isset( $params['continue'] ) ) {
58 $params['continue'] = '';
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 if ( $expectedCount > $count ) {
103 print "***** $id Finished early in $count turns. $expectedCount was expected\n";
104 }
105
106 return $result;
107 } elseif ( !$useContinue ) {
108 $this->assertFalse( 'Non-smart query must be requested all at once' );
109 }
110 } while ( true );
111 }
112
113 /**
114 * @param array $data
115 */
116 private function printResult( $data ) {
117 $q = $data['query'];
118 $print = array();
119 if ( isset( $q['pages'] ) ) {
120 foreach ( $q['pages'] as $p ) {
121 $m = $p['title'];
122 if ( isset( $p['links'] ) ) {
123 $m .= '/[' . implode( ',', array_map(
124 function ( $v ) {
125 return $v['title'];
126 },
127 $p['links'] ) ) . ']';
128 }
129 if ( isset( $p['categories'] ) ) {
130 $m .= '/(' . implode( ',', array_map(
131 function ( $v ) {
132 return str_replace( 'Category:', '', $v['title'] );
133 },
134 $p['categories'] ) ) . ')';
135 }
136 $print[] = $m;
137 }
138 }
139 if ( isset( $q['allcategories'] ) ) {
140 $print[] = '*Cats/(' . implode( ',', array_map(
141 function ( $v ) {
142 return $v['*'];
143 },
144 $q['allcategories'] ) ) . ')';
145 }
146 self::GetItems( $q, 'allpages', 'Pages', $print );
147 self::GetItems( $q, 'alllinks', 'Links', $print );
148 self::GetItems( $q, 'alltransclusions', 'Trnscl', $print );
149 print ' ' . implode( ' ', $print ) . "\n";
150 }
151
152 private static function GetItems( $q, $moduleName, $name, &$print ) {
153 if ( isset( $q[$moduleName] ) ) {
154 $print[] = "*$name/[" . implode( ',',
155 array_map(
156 function ( $v ) {
157 return $v['title'];
158 },
159 $q[$moduleName] ) ) . ']';
160 }
161 }
162
163 /**
164 * Recursively merge the new result returned from the query to the previous results.
165 * @param mixed $results
166 * @param mixed $newResult
167 * @param bool $numericIds If true, treat keys as ids to be merged instead of appending
168 */
169 protected function mergeResult( &$results, $newResult, $numericIds = false ) {
170 $this->assertEquals( is_array( $results ), is_array( $newResult ), 'Type of result and data do not match' );
171 if ( !is_array( $results ) ) {
172 $this->assertEquals( $results, $newResult, 'Repeated result must be the same as before' );
173 } else {
174 $sort = null;
175 foreach ( $newResult as $key => $value ) {
176 if ( !$numericIds && $sort === null ) {
177 if ( !is_array( $value ) ) {
178 $sort = false;
179 } elseif ( array_key_exists( 'title', $value ) ) {
180 $sort = function ( $a, $b ) {
181 return strcmp( $a['title'], $b['title'] );
182 };
183 } else {
184 $sort = false;
185 }
186 }
187 $keyExists = array_key_exists( $key, $results );
188 if ( is_numeric( $key ) ) {
189 if ( $numericIds ) {
190 if ( !$keyExists ) {
191 $results[$key] = $value;
192 } else {
193 $this->mergeResult( $results[$key], $value );
194 }
195 } else {
196 $results[] = $value;
197 }
198 } elseif ( !$keyExists ) {
199 $results[$key] = $value;
200 } else {
201 $this->mergeResult( $results[$key], $value, $key === 'pages' );
202 }
203 }
204 if ( $numericIds ) {
205 ksort( $results, SORT_NUMERIC );
206 } elseif ( $sort !== null && $sort !== false ) {
207 usort( $results, $sort );
208 }
209 }
210 }
211 }