Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / search / SearchResultSetTest.php
blob3120246b84d63eb2aa7067cc1eb5791e70015d13
1 <?php
3 use MediaWiki\Revision\RevisionLookup;
4 use MediaWiki\Title\Title;
6 class SearchResultSetTest extends MediaWikiIntegrationTestCase {
7 protected function setUp(): void {
8 parent::setUp();
9 $this->setService( 'RevisionLookup', $this->createMock( RevisionLookup::class ) );
12 /**
13 * @covers \SearchResultSet::getIterator
14 * @covers \BaseSearchResultSet::next
15 * @covers \BaseSearchResultSet::rewind
17 public function testIterate() {
18 $title = Title::makeTitle( NS_MAIN, __METHOD__ );
19 $result = SearchResult::newFromTitle( $title );
20 $resultSet = new MockSearchResultSet( [ $result ] );
21 $this->assertSame( 1, $resultSet->numRows() );
22 $count = 0;
23 foreach ( $resultSet as $iterResult ) {
24 $this->assertEquals( $result, $iterResult );
25 $count++;
27 $this->assertSame( 1, $count );
29 $this->hideDeprecated( 'BaseSearchResultSet::rewind' );
30 $this->hideDeprecated( 'BaseSearchResultSet::next' );
31 $resultSet->rewind();
32 $count = 0;
33 foreach ( $resultSet as $iterResult ) {
34 $this->assertEquals( $result, $iterResult );
35 $count++;
37 $this->assertSame( 1, $count );
40 /**
41 * @covers \SearchResultSetTrait::augmentResult
42 * @covers \SearchResultSetTrait::setAugmentedData
44 public function testDelayedResultAugment() {
45 $title = Title::makeTitle( NS_MAIN, __METHOD__ );
46 $title->resetArticleID( 42 );
47 $result = SearchResult::newFromTitle( $title );
48 $resultSet = new MockSearchResultSet( [ $result ] );
49 $resultSet->augmentResult( $result );
50 $this->assertEquals( [], $result->getExtensionData() );
51 $resultSet->setAugmentedData( 'foo', [
52 $result->getTitle()->getArticleID() => 'bar'
53 ] );
54 $this->assertEquals( [ 'foo' => 'bar' ], $result->getExtensionData() );
57 /**
58 * @covers \SearchResultSet::shrink
59 * @covers \SearchResultSet::count
60 * @covers \SearchResultSet::hasMoreResults
62 public function testHasMoreResults() {
63 $title = Title::makeTitle( NS_MAIN, __METHOD__ );
64 $result = SearchResult::newFromTitle( $title );
65 $resultSet = new MockSearchResultSet( array_fill( 0, 3, $result ) );
66 $this->assertCount( 3, $resultSet );
67 $this->assertFalse( $resultSet->hasMoreResults() );
68 $resultSet->shrink( 3 );
69 $this->assertFalse( $resultSet->hasMoreResults() );
70 $resultSet->shrink( 2 );
71 $this->assertTrue( $resultSet->hasMoreResults() );
74 /**
75 * @covers \SearchResultSet::shrink
77 public function testShrink() {
78 $title = Title::makeTitle( NS_MAIN, __METHOD__ );
79 $results = array_fill( 0, 3, SearchResult::newFromTitle( $title ) );
80 $resultSet = new MockSearchResultSet( $results );
81 $this->assertCount( 3, $resultSet->extractResults() );
82 $this->assertCount( 3, $resultSet->extractTitles() );
83 $resultSet->shrink( 1 );
84 $this->assertCount( 1, $resultSet->extractResults() );
85 $this->assertCount( 1, $resultSet->extractTitles() );