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