MediaWiki  1.23.2
SearchEngineTest.php
Go to the documentation of this file.
1 <?php
2 
11 
15  protected $search;
16 
17  protected $pageList;
18 
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 
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 }
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:189
SearchEngineTest\tearDown
tearDown()
Definition: SearchEngineTest.php:46
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
SearchEngineTest
@group Search @group Database
Definition: SearchEngineTest.php:10
SearchEngineTest\testTextSearch
testTextSearch()
Definition: SearchEngineTest.php:142
MediaWikiTestCase\isWikitextNS
isWikitextNS( $ns)
Returns true if the given namespace defaults to Wikitext according to $wgNamespaceContentModels.
Definition: MediaWikiTestCase.php:886
SearchEngineTest\addPages
addPages()
Definition: SearchEngineTest.php:52
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:388
SearchEngineTest\testFullWidth
testFullWidth()
Definition: SearchEngineTest.php:123
NS_MAIN
const NS_MAIN
Definition: Defines.php:79
SearchEngineTest\$search
SearchEngine $search
Definition: SearchEngineTest.php:14
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:103
SearchEngineTest\fetchIds
fetchIds( $results)
Definition: SearchEngineTest.php:77
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Definition: MediaWikiTestCase.php:302
SearchEngineTest\testTextTitlePowerSearch
testTextTitlePowerSearch()
Definition: SearchEngineTest.php:170
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
$comment
$comment
Definition: importImages.php:107
SearchEngineTest\testTitleSearch
testTitleSearch()
Definition: SearchEngineTest.php:160
ContentHandler\makeContent
static makeContent( $text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
Definition: ContentHandler.php:144
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
$matches
if(!defined( 'MEDIAWIKI')) if(!isset( $wgVersion)) $matches
Definition: NoLocalSettings.php:33
SearchEngineTest\insertPage
insertPage( $pageName, $text, $ns)
Insert a new page.
Definition: SearchEngineTest.php:106
MediaWikiLangTestCase
Base class that store and restore the Language objects.
Definition: MediaWikiLangTestCase.php:6
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:237
SearchEngine
Contain a class for special pages.
Definition: SearchEngine.php:32
SearchEngineTest\$pageList
$pageList
Definition: SearchEngineTest.php:16
SearchEngineTest\testTextPowerSearch
testTextPowerSearch()
Definition: SearchEngineTest.php:149
LinkCache\singleton
static & singleton()
Get an instance of this class.
Definition: LinkCache.php:49
SearchEngineTest\setUp
setUp()
Checks for database type & version.
Definition: SearchEngineTest.php:22