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
|
<?php
/**
* Test for filter utilities.
*
* 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
*/
class SearchSuggestionSetTest extends \MediaWikiUnitTestCase {
/**
* Test that adding a new suggestion at the end
* will keep proper score ordering
* @covers \SearchSuggestionSet::append
*/
public function testAppend() {
$set = SearchSuggestionSet::emptySuggestionSet();
$this->assertSame( 0, $set->getSize() );
$set->append( new SearchSuggestion( 3 ) );
$this->assertEquals( 3, $set->getWorstScore() );
$this->assertEquals( 3, $set->getBestScore() );
$suggestion = new SearchSuggestion( 4 );
$set->append( $suggestion );
$this->assertEquals( 2, $set->getWorstScore() );
$this->assertEquals( 3, $set->getBestScore() );
$this->assertEquals( 2, $suggestion->getScore() );
$suggestion = new SearchSuggestion( 2 );
$set->append( $suggestion );
$this->assertSame( 1, $set->getWorstScore() );
$this->assertEquals( 3, $set->getBestScore() );
$this->assertSame( 1, $suggestion->getScore() );
$scores = $set->map( static function ( $s ) {
return $s->getScore();
} );
$sorted = $scores;
asort( $sorted );
$this->assertEquals( $sorted, $scores );
}
/**
* Test that adding a new best suggestion will keep proper score
* ordering
* @covers \SearchSuggestionSet::getWorstScore
* @covers \SearchSuggestionSet::getBestScore
* @covers \SearchSuggestionSet::prepend
*/
public function testInsertBest() {
$set = SearchSuggestionSet::emptySuggestionSet();
$this->assertSame( 0, $set->getSize() );
$set->prepend( new SearchSuggestion( 3 ) );
$this->assertEquals( 3, $set->getWorstScore() );
$this->assertEquals( 3, $set->getBestScore() );
$suggestion = new SearchSuggestion( 4 );
$set->prepend( $suggestion );
$this->assertEquals( 3, $set->getWorstScore() );
$this->assertEquals( 4, $set->getBestScore() );
$this->assertEquals( 4, $suggestion->getScore() );
$suggestion = new SearchSuggestion( 0 );
$set->prepend( $suggestion );
$this->assertEquals( 3, $set->getWorstScore() );
$this->assertEquals( 5, $set->getBestScore() );
$this->assertEquals( 5, $suggestion->getScore() );
$suggestion = new SearchSuggestion( 2 );
$set->prepend( $suggestion );
$this->assertEquals( 3, $set->getWorstScore() );
$this->assertEquals( 6, $set->getBestScore() );
$this->assertEquals( 6, $suggestion->getScore() );
$scores = $set->map( static function ( $s ) {
return $s->getScore();
} );
$sorted = $scores;
asort( $sorted );
$this->assertEquals( $sorted, $scores );
}
/**
* @covers \SearchSuggestionSet::shrink
*/
public function testShrink() {
$set = SearchSuggestionSet::emptySuggestionSet();
for ( $i = 0; $i < 100; $i++ ) {
$set->append( new SearchSuggestion( 0, 'test', null, $i ) );
}
$set->shrink( 10 );
$this->assertEquals( 10, $set->getSize() );
$this->assertTrue( $set->hasMoreResults() );
$set->prepend( new SearchSuggestion( 0, 'test', null, 10 ) );
$this->assertEquals( 11, $set->getSize() );
$this->assertEquals( 10, $set->getSuggestions()[0]->getSuggestedTitleID() );
$set->shrink( 0 );
$this->assertSame( 0, $set->getSize() );
}
/**
* @covers \SearchSuggestionSet::remove
*/
public function testRemove() {
$set = SearchSuggestionSet::emptySuggestionSet();
$sug = new SearchSuggestion( 0.3, 'sugg', null, 1 );
$set->append( $sug );
// same text, id
$this->assertTrue( $set->remove( $sug ) );
$this->assertSame( 0, $set->getSize() );
$set->append( $sug );
// different text, id
$this->assertFalse( $set->remove( new SearchSuggestion( 0.3, 'something else', null, 2 ) ) );
$this->assertSame( 1, $set->getSize() );
// same text, different/missing id
$this->assertTrue( $set->remove( new SearchSuggestion( 0.3, 'sugg', null, 2 ) ) );
$this->assertSame( 0, $set->getSize() );
}
/** @return iterable */
public static function provideNoTitles(): iterable {
yield 'Empty Array' => [ [] ];
}
/**
* @covers \SearchSuggestionSet::fromTitles
* @dataProvider provideNoTitles
*/
public function testFromNoTitles( array $titles ): void {
$actual = SearchSuggestionSet::fromTitles( $titles );
$this->assertSame( 0, $actual->getSize() );
$this->assertSame( [], $actual->getSuggestions() );
$this->assertInstanceOf( SearchSuggestionSet::class, $actual );
}
}
|