MediaWiki  1.27.4
SearchEngineTest.php
Go to the documentation of this file.
1 <?php
2 
11 
15  protected $search;
16 
21  protected function setUp() {
22  parent::setUp();
23 
24  // Search tests require MySQL or SQLite with FTS
25  $dbType = $this->db->getType();
26  $dbSupported = ( $dbType === 'mysql' )
27  || ( $dbType === 'sqlite' && $this->db->getFulltextSearchModule() == 'FTS3' );
28 
29  if ( !$dbSupported ) {
30  $this->markTestSkipped( "MySQL or SQLite with FTS3 only" );
31  }
32 
33  $searchType = $this->db->getSearchEngine();
34  $this->setMwGlobals( [
35  'wgSearchType' => $searchType
36  ] );
37 
38  $this->search = new $searchType( $this->db );
39  }
40 
41  protected function tearDown() {
42  unset( $this->search );
43 
44  parent::tearDown();
45  }
46 
47  public function addDBDataOnce() {
48  if ( !$this->isWikitextNS( NS_MAIN ) ) {
49  // @todo cover the case of non-wikitext content in the main namespace
50  return;
51  }
52 
53  $this->insertPage( 'Not_Main_Page', 'This is not a main page' );
54  $this->insertPage(
55  'Talk:Not_Main_Page',
56  'This is not a talk page to the main page, see [[smithee]]'
57  );
58  $this->insertPage( 'Smithee', 'A smithee is one who smiths. See also [[Alan Smithee]]' );
59  $this->insertPage( 'Talk:Smithee', 'This article sucks.' );
60  $this->insertPage( 'Unrelated_page', 'Nothing in this page is about the S word.' );
61  $this->insertPage( 'Another_page', 'This page also is unrelated.' );
62  $this->insertPage( 'Help:Help', 'Help me!' );
63  $this->insertPage( 'Thppt', 'Blah blah' );
64  $this->insertPage( 'Alan_Smithee', 'yum' );
65  $this->insertPage( 'Pages', 'are\'food' );
66  $this->insertPage( 'HalfOneUp', 'AZ' );
67  $this->insertPage( 'FullOneUp', 'AZ' );
68  $this->insertPage( 'HalfTwoLow', 'az' );
69  $this->insertPage( 'FullTwoLow', 'az' );
70  $this->insertPage( 'HalfNumbers', '1234567890' );
71  $this->insertPage( 'FullNumbers', '1234567890' );
72  $this->insertPage( 'DomainName', 'example.com' );
73  }
74 
75  protected function fetchIds( $results ) {
76  if ( !$this->isWikitextNS( NS_MAIN ) ) {
77  $this->markTestIncomplete( __CLASS__ . " does no yet support non-wikitext content "
78  . "in the main namespace" );
79  }
80  $this->assertTrue( is_object( $results ) );
81 
82  $matches = [];
83  $row = $results->next();
84  while ( $row ) {
85  $matches[] = $row->getTitle()->getPrefixedText();
86  $row = $results->next();
87  }
88  $results->free();
89  # Search is not guaranteed to return results in a certain order;
90  # sort them numerically so we will compare simply that we received
91  # the expected matches.
92  sort( $matches );
93 
94  return $matches;
95  }
96 
97  public function testFullWidth() {
98  $this->assertEquals(
99  [ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
100  $this->fetchIds( $this->search->searchText( 'AZ' ) ),
101  "Search for normalized from Half-width Upper" );
102  $this->assertEquals(
103  [ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
104  $this->fetchIds( $this->search->searchText( 'az' ) ),
105  "Search for normalized from Half-width Lower" );
106  $this->assertEquals(
107  [ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
108  $this->fetchIds( $this->search->searchText( 'AZ' ) ),
109  "Search for normalized from Full-width Upper" );
110  $this->assertEquals(
111  [ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
112  $this->fetchIds( $this->search->searchText( 'az' ) ),
113  "Search for normalized from Full-width Lower" );
114  }
115 
116  public function testTextSearch() {
117  $this->assertEquals(
118  [ 'Smithee' ],
119  $this->fetchIds( $this->search->searchText( 'smithee' ) ),
120  "Plain search failed" );
121  }
122 
123  public function testWildcardSearch() {
124  $res = $this->search->searchText( 'smith*' );
125  $this->assertEquals(
126  [ 'Smithee' ],
127  $this->fetchIds( $res ),
128  "Search with wildcards" );
129 
130  $res = $this->search->searchText( 'smithson*' );
131  $this->assertEquals(
132  [],
133  $this->fetchIds( $res ),
134  "Search with wildcards must not find unrelated articles" );
135 
136  $res = $this->search->searchText( 'smith* smithee' );
137  $this->assertEquals(
138  [ 'Smithee' ],
139  $this->fetchIds( $res ),
140  "Search with wildcards can be combined with simple terms" );
141 
142  $res = $this->search->searchText( 'smith* "one who smiths"' );
143  $this->assertEquals(
144  [ 'Smithee' ],
145  $this->fetchIds( $res ),
146  "Search with wildcards can be combined with phrase search" );
147  }
148 
149  public function testPhraseSearch() {
150  $res = $this->search->searchText( '"smithee is one who smiths"' );
151  $this->assertEquals(
152  [ 'Smithee' ],
153  $this->fetchIds( $res ),
154  "Search a phrase" );
155 
156  $res = $this->search->searchText( '"smithee is who smiths"' );
157  $this->assertEquals(
158  [],
159  $this->fetchIds( $res ),
160  "Phrase search is not sloppy, search terms must be adjacent" );
161 
162  $res = $this->search->searchText( '"is smithee one who smiths"' );
163  $this->assertEquals(
164  [],
165  $this->fetchIds( $res ),
166  "Phrase search is ordered" );
167  }
168 
169  public function testPhraseSearchHighlight() {
170  $phrase = "smithee is one who smiths";
171  $res = $this->search->searchText( "\"$phrase\"" );
172  $match = $res->next();
173  $snippet = "A <span class='searchmatch'>" . $phrase . "</span>";
174  $this->assertStringStartsWith( $snippet,
175  $match->getTextSnippet( $res->termMatches() ),
176  "Phrase search failed to highlight" );
177  }
178 
179  public function testTextPowerSearch() {
180  $this->search->setNamespaces( [ 0, 1, 4 ] );
181  $this->assertEquals(
182  [
183  'Smithee',
184  'Talk:Not Main Page',
185  ],
186  $this->fetchIds( $this->search->searchText( 'smithee' ) ),
187  "Power search failed" );
188  }
189 
190  public function testTitleSearch() {
191  $this->assertEquals(
192  [
193  'Alan Smithee',
194  'Smithee',
195  ],
196  $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
197  "Title search failed" );
198  }
199 
200  public function testTextTitlePowerSearch() {
201  $this->search->setNamespaces( [ 0, 1, 4 ] );
202  $this->assertEquals(
203  [
204  'Alan Smithee',
205  'Smithee',
206  'Talk:Smithee',
207  ],
208  $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
209  "Title power search failed" );
210  }
211 
212 }
const NS_MAIN
Definition: Defines.php:70
insertPage($pageName, $text= 'Sample page for unit test.')
Insert a new page.
setUp()
Checks for database type & version.
$res
Definition: database.txt:21
Base class that store and restore the Language objects.
Search Database.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
isWikitextNS($ns)
Returns true if the given namespace defaults to Wikitext according to $wgNamespaceContentModels.
setMwGlobals($pairs, $value=null)
SearchEngine $search
$matches