Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiQuerySearchTest.php
index 0700cf7..93c5345 100644 (file)
@@ -10,22 +10,22 @@ class ApiQuerySearchTest extends ApiTestCase {
                        'empty search result' => [ [], [] ],
                        'has search results' => [
                                [ 'Zomg' ],
-                               [ $this->mockResult( 'Zomg' ) ],
+                               [ $this->mockResultClosure( 'Zomg' ) ],
                        ],
                        'filters broken search results' => [
                                [ 'A', 'B' ],
                                [
-                                       $this->mockResult( 'a' ),
-                                       $this->mockResult( 'Zomg' )->setBrokenTitle( true ),
-                                       $this->mockResult( 'b' ),
+                                       $this->mockResultClosure( 'a' ),
+                                       $this->mockResultClosure( 'Zomg', [ 'setBrokenTitle' => true ] ),
+                                       $this->mockResultClosure( 'b' ),
                                ],
                        ],
                        'filters results with missing revision' => [
                                [ 'B', 'A' ],
                                [
-                                       $this->mockResult( 'Zomg' )->setMissingRevision( true ),
-                                       $this->mockResult( 'b' ),
-                                       $this->mockResult( 'a' ),
+                                       $this->mockResultClosure( 'Zomg', [ 'setMissingRevision' => true ] ),
+                                       $this->mockResultClosure( 'b' ),
+                                       $this->mockResultClosure( 'a' ),
                                ],
                        ],
                ];
@@ -54,9 +54,12 @@ class ApiQuerySearchTest extends ApiTestCase {
                        'one wiki response' => [
                                [ 'utwiki' => [ 'Qwerty' ] ],
                                [
-                                       SearchResultSet::SECONDARY_RESULTS => [
+                                       ISearchResultSet::SECONDARY_RESULTS => [
                                                'utwiki' => new MockSearchResultSet( [
-                                                       $this->mockResult( 'Qwerty' )->setInterwikiPrefix( 'utwiki' ),
+                                                       $this->mockResultClosure(
+                                                               'Qwerty',
+                                                               [ 'setInterwikiPrefix' => 'utwiki' ]
+                                                       ),
                                                ] ),
                                        ],
                                ]
@@ -102,8 +105,28 @@ class ApiQuerySearchTest extends ApiTestCase {
                ] );
        }
 
-       private function mockResult( $title ) {
-               return MockSearchResult::newFromtitle( Title::newFromText( $title ) );
+       /**
+        * Returns a closure that evaluates to a MockSearchResult, to be resolved by
+        * MockSearchEngine::addMockResults() or MockresultSet::extractResults().
+        *
+        * This is needed because MockSearchResults cannot be instantiated in a data provider,
+        * since they load revisions. This would hit the "real" database instead of the mock
+        * database, which in turn may cause cache pollution and other inconsistencies, see T202641.
+        *
+        * @param string $title
+        * @param array $setters
+        * @return callable function(): MockSearchResult
+        */
+       private function mockResultClosure( $title, $setters = [] ) {
+               return function () use ( $title, $setters ){
+                       $result = MockSearchResult::newFromTitle( Title::newFromText( $title ) );
+
+                       foreach ( $setters as $method => $param ) {
+                               $result->$method( $param );
+                       }
+
+                       return $result;
+               };
        }
 
 }