Merge "Support tighter rate limiting for "non-standard" thumbnails"
[lhc/web/wiklou.git] / tests / phpunit / includes / search / SearchEngineTest.php
1 <?php
2
3 /**
4 * @group Search
5 * @group Database
6 *
7 * @covers SearchEngine<extended>
8 * @note Coverage will only ever show one of on of the Search* classes
9 */
10 class SearchEngineTest extends MediaWikiLangTestCase {
11
12 /**
13 * @var SearchEngine
14 */
15 protected $search;
16
17 protected $pageList;
18
19 /**
20 * Checks for database type & version.
21 * Will skip current test if DB does not support search.
22 */
23 protected function setUp() {
24 parent::setUp();
25
26 // Search tests require MySQL or SQLite with FTS
27 $dbType = $this->db->getType();
28 $dbSupported = ( $dbType === 'mysql' )
29 || ( $dbType === 'sqlite' && $this->db->getFulltextSearchModule() == 'FTS3' );
30
31 if ( !$dbSupported ) {
32 $this->markTestSkipped( "MySQL or SQLite with FTS3 only" );
33 }
34
35 $searchType = $this->db->getSearchEngine();
36 $this->setMwGlobals( array(
37 'wgSearchType' => $searchType
38 ) );
39
40 if ( !isset( self::$pageList ) ) {
41 $this->addPages();
42 }
43
44 $this->search = new $searchType( $this->db );
45 }
46
47 protected function tearDown() {
48 unset( $this->search );
49
50 parent::tearDown();
51 }
52
53 protected function addPages() {
54 if ( !$this->isWikitextNS( NS_MAIN ) ) {
55 // @todo cover the case of non-wikitext content in the main namespace
56 return;
57 }
58
59 $this->insertPage( "Not_Main_Page", "This is not a main page", 0 );
60 $this->insertPage( 'Talk:Not_Main_Page', 'This is not a talk page to the main page, see [[smithee]]', 1 );
61 $this->insertPage( 'Smithee', 'A smithee is one who smiths. See also [[Alan Smithee]]', 0 );
62 $this->insertPage( 'Talk:Smithee', 'This article sucks.', 1 );
63 $this->insertPage( 'Unrelated_page', 'Nothing in this page is about the S word.', 0 );
64 $this->insertPage( 'Another_page', 'This page also is unrelated.', 0 );
65 $this->insertPage( 'Help:Help', 'Help me!', 4 );
66 $this->insertPage( 'Thppt', 'Blah blah', 0 );
67 $this->insertPage( 'Alan_Smithee', 'yum', 0 );
68 $this->insertPage( 'Pages', 'are\'food', 0 );
69 $this->insertPage( 'HalfOneUp', 'AZ', 0 );
70 $this->insertPage( 'FullOneUp', 'AZ', 0 );
71 $this->insertPage( 'HalfTwoLow', 'az', 0 );
72 $this->insertPage( 'FullTwoLow', 'az', 0 );
73 $this->insertPage( 'HalfNumbers', '1234567890', 0 );
74 $this->insertPage( 'FullNumbers', '1234567890', 0 );
75 $this->insertPage( 'DomainName', 'example.com', 0 );
76 }
77
78 protected function fetchIds( $results ) {
79 if ( !$this->isWikitextNS( NS_MAIN ) ) {
80 $this->markTestIncomplete( __CLASS__ . " does no yet support non-wikitext content "
81 . "in the main namespace" );
82 }
83 $this->assertTrue( is_object( $results ) );
84
85 $matches = array();
86 $row = $results->next();
87 while ( $row ) {
88 $matches[] = $row->getTitle()->getPrefixedText();
89 $row = $results->next();
90 }
91 $results->free();
92 # Search is not guaranteed to return results in a certain order;
93 # sort them numerically so we will compare simply that we received
94 # the expected matches.
95 sort( $matches );
96
97 return $matches;
98 }
99
100 /**
101 * Insert a new page
102 *
103 * @param string $pageName Page name
104 * @param string $text Page's content
105 * @param int $ns Unused
106 */
107 protected function insertPage( $pageName, $text, $ns ) {
108 $title = Title::newFromText( $pageName, $ns );
109
110 $user = User::newFromName( 'WikiSysop' );
111 $comment = 'Search Test';
112
113 // avoid memory leak...?
114 LinkCache::singleton()->clear();
115
116 $page = WikiPage::factory( $title );
117 $page->doEditContent( ContentHandler::makeContent( $text, $title ), $comment, 0, false, $user );
118
119 $this->pageList[] = array( $title, $page->getId() );
120
121 return true;
122 }
123
124 public function testFullWidth() {
125 $this->assertEquals(
126 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
127 $this->fetchIds( $this->search->searchText( 'AZ' ) ),
128 "Search for normalized from Half-width Upper" );
129 $this->assertEquals(
130 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
131 $this->fetchIds( $this->search->searchText( 'az' ) ),
132 "Search for normalized from Half-width Lower" );
133 $this->assertEquals(
134 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
135 $this->fetchIds( $this->search->searchText( 'AZ' ) ),
136 "Search for normalized from Full-width Upper" );
137 $this->assertEquals(
138 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
139 $this->fetchIds( $this->search->searchText( 'az' ) ),
140 "Search for normalized from Full-width Lower" );
141 }
142
143 public function testTextSearch() {
144 $this->assertEquals(
145 array( 'Smithee' ),
146 $this->fetchIds( $this->search->searchText( 'smithee' ) ),
147 "Plain search failed" );
148 }
149
150 public function testTextPowerSearch() {
151 $this->search->setNamespaces( array( 0, 1, 4 ) );
152 $this->assertEquals(
153 array(
154 'Smithee',
155 'Talk:Not Main Page',
156 ),
157 $this->fetchIds( $this->search->searchText( 'smithee' ) ),
158 "Power search failed" );
159 }
160
161 public function testTitleSearch() {
162 $this->assertEquals(
163 array(
164 'Alan Smithee',
165 'Smithee',
166 ),
167 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
168 "Title search failed" );
169 }
170
171 public function testTextTitlePowerSearch() {
172 $this->search->setNamespaces( array( 0, 1, 4 ) );
173 $this->assertEquals(
174 array(
175 'Alan Smithee',
176 'Smithee',
177 'Talk:Smithee',
178 ),
179 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
180 "Title power search failed" );
181 }
182
183 }