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