MediaWiki  1.27.2
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 testTextPowerSearch() {
124  $this->search->setNamespaces( [ 0, 1, 4 ] );
125  $this->assertEquals(
126  [
127  'Smithee',
128  'Talk:Not Main Page',
129  ],
130  $this->fetchIds( $this->search->searchText( 'smithee' ) ),
131  "Power search failed" );
132  }
133 
134  public function testTitleSearch() {
135  $this->assertEquals(
136  [
137  'Alan Smithee',
138  'Smithee',
139  ],
140  $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
141  "Title search failed" );
142  }
143 
144  public function testTextTitlePowerSearch() {
145  $this->search->setNamespaces( [ 0, 1, 4 ] );
146  $this->assertEquals(
147  [
148  'Alan Smithee',
149  'Smithee',
150  'Talk:Smithee',
151  ],
152  $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
153  "Title power search failed" );
154  }
155 
156 }
const NS_MAIN
Definition: Defines.php:69
insertPage($pageName, $text= 'Sample page for unit test.')
Insert a new page.
setUp()
Checks for database type & version.
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