Change from global to parameter.
[lhc/web/wiklou.git] / maintenance / tests / SearchEngineTest.php
1 <?php
2
3 /**
4 * @group Stub
5 */
6 class SearchEngineTest extends MediaWikiTestSetup {
7 var $db, $search, $pageList;
8
9 function pageExists( $title ) {
10 return false;
11 }
12
13 function insertSearchData() {
14 if ( $this->pageExists( 'Not_Main_Page' ) ) {
15 return;
16 }
17 $this->insertPage( "Not_Main_Page", "This is not a main page", 0 );
18 $this->insertPage( 'Talk:Not_Main_Page', 'This is not a talk page to the main page, see [[smithee]]', 1 );
19 $this->insertPage( 'Smithee', 'A smithee is one who smiths. See also [[Alan Smithee]]', 0 );
20 $this->insertPage( 'Talk:Smithee', 'This article sucks.', 1 );
21 $this->insertPage( 'Unrelated_page', 'Nothing in this page is about the S word.', 0 );
22 $this->insertPage( 'Another_page', 'This page also is unrelated.', 0 );
23 $this->insertPage( 'Help:Help', 'Help me!', 4 );
24 $this->insertPage( 'Thppt', 'Blah blah', 0 );
25 $this->insertPage( 'Alan_Smithee', 'yum', 0 );
26 $this->insertPage( 'Pages', 'are\'food', 0 );
27 $this->insertPage( 'HalfOneUp', 'AZ', 0 );
28 $this->insertPage( 'FullOneUp', 'AZ', 0 );
29 $this->insertPage( 'HalfTwoLow', 'az', 0 );
30 $this->insertPage( 'FullTwoLow', 'az', 0 );
31 $this->insertPage( 'HalfNumbers', '1234567890', 0 );
32 $this->insertPage( 'FullNumbers', '1234567890', 0 );
33 $this->insertPage( 'DomainName', 'example.com', 0 );
34 }
35
36 function removeSearchData() {
37 return;
38 while ( count( $this->pageList ) ) {
39 list( $title, $id ) = array_pop( $this->pageList );
40 $article = new Article( $title, $id );
41 $article->doDeleteArticle( "Search Test" );
42 }
43 }
44
45 function fetchIds( $results ) {
46 $this->assertTrue( is_object( $results ) );
47
48 if ( $this->db->getType() !== 'mysql' && $this->db->getType() !== 'sqlite' ) {
49 $this->markTestSkipped( "MySQL or SQLite only" );
50 }
51 $matches = array();
52 while ( $row = $results->next() ) {
53 $matches[] = $row->getTitle()->getPrefixedText();
54 }
55 $results->free();
56 # Search is not guaranteed to return results in a certain order;
57 # sort them numerically so we will compare simply that we received
58 # the expected matches.
59 sort( $matches );
60 return $matches;
61 }
62
63 // Modified version of WikiRevision::importOldRevision()
64 function insertPage( $pageName, $text, $ns ) {
65 $dbw = $this->db;
66 $title = Title::newFromText( $pageName );
67
68 $userId = 0;
69 $userText = 'WikiSysop';
70 $comment = 'Search Test';
71
72 // avoid memory leak...?
73 $linkCache = LinkCache::singleton();
74 $linkCache->clear();
75
76 $article = new Article( $title );
77 $pageId = $article->getId();
78 $created = false;
79 if ( $pageId == 0 ) {
80 # must create the page...
81 $pageId = $article->insertOn( $dbw );
82 $created = true;
83 }
84
85 # FIXME: Use original rev_id optionally (better for backups)
86 # Insert the row
87 $revision = new Revision( array(
88 'page' => $pageId,
89 'text' => $text,
90 'comment' => $comment,
91 'user' => $userId,
92 'user_text' => $userText,
93 'timestamp' => 0,
94 'minor_edit' => false,
95 ) );
96 $revId = $revision->insertOn( $dbw );
97 $changed = $article->updateIfNewerOn( $dbw, $revision );
98
99 $GLOBALS['wgTitle'] = $title;
100 if ( $created ) {
101 Article::onArticleCreate( $title );
102 $article->createUpdates( $revision );
103 } elseif ( $changed ) {
104 Article::onArticleEdit( $title );
105 $article->editUpdates(
106 $text, $comment, false, 0, $revId );
107 }
108
109 $su = new SearchUpdate( $article->getId(), $pageName, $text );
110 $su->doUpdate();
111
112 $this->pageList[] = array( $title, $article->getId() );
113
114 return true;
115 }
116
117 function testFullWidth() {
118 $this->assertEquals(
119 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
120 $this->fetchIds( $this->search->searchText( 'AZ' ) ),
121 "Search for normalized from Half-width Upper" );
122 $this->assertEquals(
123 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
124 $this->fetchIds( $this->search->searchText( 'az' ) ),
125 "Search for normalized from Half-width Lower" );
126 $this->assertEquals(
127 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
128 $this->fetchIds( $this->search->searchText( 'AZ' ) ),
129 "Search for normalized from Full-width Upper" );
130 $this->assertEquals(
131 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
132 $this->fetchIds( $this->search->searchText( 'az' ) ),
133 "Search for normalized from Full-width Lower" );
134 }
135
136 function testTextSearch() {
137 $this->assertEquals(
138 array( 'Smithee' ),
139 $this->fetchIds( $this->search->searchText( 'smithee' ) ),
140 "Plain search failed" );
141 }
142
143 function testTextPowerSearch() {
144 $this->search->setNamespaces( array( 0, 1, 4 ) );
145 $this->assertEquals(
146 array(
147 'Smithee',
148 'Talk:Not Main Page',
149 ),
150 $this->fetchIds( $this->search->searchText( 'smithee' ) ),
151 "Power search failed" );
152 }
153
154 function testTitleSearch() {
155 $this->assertEquals(
156 array(
157 'Alan Smithee',
158 'Smithee',
159 ),
160 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
161 "Title search failed" );
162 }
163
164 function testTextTitlePowerSearch() {
165 $this->search->setNamespaces( array( 0, 1, 4 ) );
166 $this->assertEquals(
167 array(
168 'Alan Smithee',
169 'Smithee',
170 'Talk:Smithee',
171 ),
172 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
173 "Title power search failed" );
174 }
175
176 }