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