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