Merge "Drop cache interwiki"
[mediawiki.git] / tests / phpunit / includes / api / query / ApiQueryTestBase.php
blob17f24579d81eed0885f6705aa4f5d6340df1200f
1 <?php
3 /**
4 * Copyright © 2013 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
21 * @file
24 namespace MediaWiki\Tests\Api\Query;
26 use MediaWiki\Tests\Api\ApiTestCase;
27 use MediaWiki\User\User;
28 use PHPUnit\Framework\ExpectationFailedException;
29 use SebastianBergmann\Comparator\ComparisonFailure;
31 /**
32 * This class has some common functionality for testing query module
34 abstract class ApiQueryTestBase extends ApiTestCase {
36 private const PARAM_ASSERT = <<<STR
37 Each parameter must be an array of two elements,
38 first - an array of params to the API call,
39 and the second array - expected results as returned by the API
40 STR;
42 /**
43 * Merges all requests parameter + expected values into one
44 * @param array ...$arrays List of arrays, each of which contains exactly two elements
45 * @return array
47 protected function merge( ...$arrays ) {
48 $request = [];
49 $expected = [];
50 foreach ( $arrays as $array ) {
51 [ $req, $exp ] = $this->validateRequestExpectedPair( $array );
52 $request = array_merge_recursive( $request, $req );
53 $this->mergeExpected( $expected, $exp );
56 return [ $request, $expected ];
59 /**
60 * Check that the parameter is a valid two element array,
61 * with the first element being API request and the second - expected result
62 * @param array $v
63 * @return array
65 private function validateRequestExpectedPair( $v ) {
66 $this->assertIsArray( $v, self::PARAM_ASSERT );
67 $this->assertCount( 2, $v, self::PARAM_ASSERT );
68 $this->assertArrayHasKey( 0, $v, self::PARAM_ASSERT );
69 $this->assertArrayHasKey( 1, $v, self::PARAM_ASSERT );
70 $this->assertIsArray( $v[0], self::PARAM_ASSERT );
71 $this->assertIsArray( $v[1], self::PARAM_ASSERT );
73 return $v;
76 /**
77 * Recursively merges the expected values in the $item into the $all
78 * @param array &$all
79 * @param array $item
81 private function mergeExpected( &$all, $item ) {
82 foreach ( $item as $k => $v ) {
83 if ( array_key_exists( $k, $all ) ) {
84 if ( is_array( $all[$k] ) ) {
85 $this->mergeExpected( $all[$k], $v );
86 } else {
87 $this->assertEquals( $all[$k], $v );
89 } else {
90 $all[$k] = $v;
95 /**
96 * Checks that the request's result matches the expected results.
97 * Assumes no rawcontinue and a complete batch.
98 * @param array $values Array containing two elements: [ request, expected_results ]
99 * @param array|null $session
100 * @param bool $appendModule
101 * @param User|null $user
103 protected function check( $values, ?array $session = null,
104 $appendModule = false, ?User $user = null
106 [ $req, $exp ] = $this->validateRequestExpectedPair( $values );
107 $req['action'] ??= 'query';
108 foreach ( $req as &$val ) {
109 if ( is_array( $val ) ) {
110 $val = implode( '|', array_unique( $val ) );
113 $result = $this->doApiRequest( $req, $session, $appendModule, $user );
114 $this->assertResult( [ 'batchcomplete' => true, 'query' => $exp ], $result[0], $req );
117 protected function assertResult( $exp, $result, $message = '' ) {
118 try {
119 $exp = self::sanitizeResultArray( $exp );
120 $result = self::sanitizeResultArray( $result );
121 $this->assertEquals( $exp, $result );
122 } catch ( ExpectationFailedException $e ) {
123 if ( is_array( $message ) ) {
124 $message = http_build_query( $message );
127 throw new ExpectationFailedException(
128 $e->getMessage() . "\nRequest: $message",
129 new ComparisonFailure(
130 $exp,
131 $result,
132 print_r( $exp, true ),
133 print_r( $result, true ),
134 false,
135 $e->getComparisonFailure()->getMessage() . "\nRequest: $message"
142 * Recursively ksorts a result array and removes any 'pageid' keys.
143 * @param array $result
144 * @return array
146 private static function sanitizeResultArray( $result ) {
147 unset( $result['pageid'] );
148 foreach ( $result as $key => $value ) {
149 if ( is_array( $value ) ) {
150 $result[$key] = self::sanitizeResultArray( $value );
154 // Sort the result by keys, then take advantage of how array_merge will
155 // renumber numeric keys while leaving others alone.
156 ksort( $result );
157 return array_merge( $result );
161 /** @deprecated class alias since 1.42 */
162 class_alias( ApiQueryTestBase::class, 'ApiQueryTestBase' );