summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/api/query/ApiQueryContinueTestBase.php
blob: fbb1e640daa62ccb838367b426c05ad39264128f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
 *
 *
 * Created on Jan 1, 2013
 *
 * Copyright © 2013 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 */

require_once 'ApiQueryTestBase.php';

abstract class ApiQueryContinueTestBase extends ApiQueryTestBase {

	/**
	 * Enable to print in-depth debugging info during the test run
	 */
	protected $mVerbose = false;

	/**
	 * Run query() and compare against expected values
	 */
	protected function checkC( $expected, $params, $expectedCount, $id, $continue = true ) {
		$result = $this->query( $params, $expectedCount, $id, $continue );
		$this->assertResult( $expected, $result, $id );
	}

	/**
	 * Run query in a loop until no more values are available
	 * @param array $params api parameters
	 * @param int $expectedCount max number of iterations
	 * @param string $id unit test id
	 * @param boolean $useContinue true to use smart continue
	 * @return mixed: merged results data array
	 * @throws Exception
	 */
	protected function query( $params, $expectedCount, $id, $useContinue = true ) {
		if ( isset( $params['action'] ) ) {
			$this->assertEquals( 'query', $params['action'], 'Invalid query action' );
		} else {
			$params['action'] = 'query';
		}
		if ( $useContinue && !isset( $params['continue'] ) ) {
			$params['continue'] = '';
		}
		$count = 0;
		$result = array();
		$continue = array();
		do {
			$request = array_merge( $params, $continue );
			uksort( $request, function ( $a, $b ) {
				// put 'continue' params at the end - lazy method
				$a = strpos( $a, 'continue' ) !== false ? 'zzz ' . $a : $a;
				$b = strpos( $b, 'continue' ) !== false ? 'zzz ' . $b : $b;

				return strcmp( $a, $b );
			} );
			$reqStr = http_build_query( $request );
			//$reqStr = str_replace( '&', ' & ', $reqStr );
			$this->assertLessThan( $expectedCount, $count, "$id more data: $reqStr" );
			if ( $this->mVerbose ) {
				print "$id (#$count): $reqStr\n";
			}
			try {
				$data = $this->doApiRequest( $request );
			} catch ( Exception $e ) {
				throw new Exception( "$id on $count", 0, $e );
			}
			$data = $data[0];
			if ( isset( $data['warnings'] ) ) {
				$warnings = json_encode( $data['warnings'] );
				$this->fail( "$id Warnings on #$count in $reqStr\n$warnings" );
			}
			$this->assertArrayHasKey( 'query', $data, "$id no 'query' on #$count in $reqStr" );
			if ( isset( $data['continue'] ) ) {
				$continue = $data['continue'];
				unset( $data['continue'] );
			} else {
				$continue = array();
			}
			if ( $this->mVerbose ) {
				$this->printResult( $data );
			}
			$this->mergeResult( $result, $data );
			$count++;
			if ( empty( $continue ) ) {
				// $this->assertEquals( $expectedCount, $count, "$id finished early" );
				if ( $expectedCount > $count ) {
					print "***** $id Finished early in $count turns. $expectedCount was expected\n";
				}

				return $result;
			} elseif ( !$useContinue ) {
				$this->assertFalse( 'Non-smart query must be requested all at once' );
			}
		} while ( true );
	}

	private function printResult( $data ) {
		$q = $data['query'];
		$print = array();
		if ( isset( $q['pages'] ) ) {
			foreach ( $q['pages'] as $p ) {
				$m = $p['title'];
				if ( isset( $p['links'] ) ) {
					$m .= '/[' . implode( ',', array_map(
						function ( $v ) {
							return $v['title'];
						},
						$p['links'] ) ) . ']';
				}
				if ( isset( $p['categories'] ) ) {
					$m .= '/(' . implode( ',', array_map(
						function ( $v ) {
							return str_replace( 'Category:', '', $v['title'] );
						},
						$p['categories'] ) ) . ')';
				}
				$print[] = $m;
			}
		}
		if ( isset( $q['allcategories'] ) ) {
			$print[] = '*Cats/(' . implode( ',', array_map(
				function ( $v ) {
					return $v['*'];
				},
				$q['allcategories'] ) ) . ')';
		}
		self::GetItems( $q, 'allpages', 'Pages', $print );
		self::GetItems( $q, 'alllinks', 'Links', $print );
		self::GetItems( $q, 'alltransclusions', 'Trnscl', $print );
		print ' ' . implode( '  ', $print ) . "\n";
	}

	private static function GetItems( $q, $moduleName, $name, &$print ) {
		if ( isset( $q[$moduleName] ) ) {
			$print[] = "*$name/[" . implode( ',',
				array_map( function ( $v ) {
						return $v['title'];
					},
					$q[$moduleName] ) ) . ']';
		}
	}

	/**
	 * Recursively merge the new result returned from the query to the previous results.
	 * @param mixed $results
	 * @param mixed $newResult
	 * @param bool $numericIds If true, treat keys as ids to be merged instead of appending
	 */
	protected function mergeResult( &$results, $newResult, $numericIds = false ) {
		$this->assertEquals( is_array( $results ), is_array( $newResult ), 'Type of result and data do not match' );
		if ( !is_array( $results ) ) {
			$this->assertEquals( $results, $newResult, 'Repeated result must be the same as before' );
		} else {
			$sort = null;
			foreach ( $newResult as $key => $value ) {
				if ( !$numericIds && $sort === null ) {
					if ( !is_array( $value ) ) {
						$sort = false;
					} elseif ( array_key_exists( 'title', $value ) ) {
						$sort = function ( $a, $b ) {
							return strcmp( $a['title'], $b['title'] );
						};
					} else {
						$sort = false;
					}
				}
				$keyExists = array_key_exists( $key, $results );
				if ( is_numeric( $key ) ) {
					if ( $numericIds ) {
						if ( !$keyExists ) {
							$results[$key] = $value;
						} else {
							$this->mergeResult( $results[$key], $value );
						}
					} else {
						$results[] = $value;
					}
				} elseif ( !$keyExists ) {
					$results[$key] = $value;
				} else {
					$this->mergeResult( $results[$key], $value, $key === 'pages' );
				}
			}
			if ( $numericIds ) {
				ksort( $results, SORT_NUMERIC );
			} elseif ( $sort !== null && $sort !== false ) {
				uasort( $results, $sort );
			}
		}
	}
}