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