Add, update, tweak documentation
[lhc/web/wiklou.git] / tests / phpunit / includes / search / SearchEngineTest.php
1 <?php
2
3 /**
4 * This class is not directly tested. Instead it is extended by SearchDbTest.
5 * @group Search
6 * @group Database
7 */
8 class SearchEngineTest extends MediaWikiTestCase {
9 protected $search, $pageList;
10
11 function tearDown() {
12 unset( $this->search );
13 }
14
15 /**
16 * Checks for database type & version.
17 * Will skip current test if DB does not support search.
18 */
19 function setUp() {
20 parent::setUp();
21 // Search tests require MySQL or SQLite with FTS
22 # Get database type and version
23 $dbType = $this->db->getType();
24 $dbSupported =
25 ($dbType === 'mysql')
26 || ( $dbType === 'sqlite' && $this->db->getFulltextSearchModule() == 'FTS3' );
27
28 if( !$dbSupported ) {
29 $this->markTestSkipped( "MySQL or SQLite with FTS3 only" );
30 }
31
32 $searchType = $this->db->getSearchEngine();
33 $this->search = new $searchType( $this->db );
34 }
35
36 function pageExists( $title ) {
37 return false;
38 }
39
40 function addDBData() {
41 if ( $this->pageExists( 'Not_Main_Page' ) ) {
42 return;
43 }
44 $this->insertPage( "Not_Main_Page", "This is not a main page", 0 );
45 $this->insertPage( 'Talk:Not_Main_Page', 'This is not a talk page to the main page, see [[smithee]]', 1 );
46 $this->insertPage( 'Smithee', 'A smithee is one who smiths. See also [[Alan Smithee]]', 0 );
47 $this->insertPage( 'Talk:Smithee', 'This article sucks.', 1 );
48 $this->insertPage( 'Unrelated_page', 'Nothing in this page is about the S word.', 0 );
49 $this->insertPage( 'Another_page', 'This page also is unrelated.', 0 );
50 $this->insertPage( 'Help:Help', 'Help me!', 4 );
51 $this->insertPage( 'Thppt', 'Blah blah', 0 );
52 $this->insertPage( 'Alan_Smithee', 'yum', 0 );
53 $this->insertPage( 'Pages', 'are\'food', 0 );
54 $this->insertPage( 'HalfOneUp', 'AZ', 0 );
55 $this->insertPage( 'FullOneUp', 'AZ', 0 );
56 $this->insertPage( 'HalfTwoLow', 'az', 0 );
57 $this->insertPage( 'FullTwoLow', 'az', 0 );
58 $this->insertPage( 'HalfNumbers', '1234567890', 0 );
59 $this->insertPage( 'FullNumbers', '1234567890', 0 );
60 $this->insertPage( 'DomainName', 'example.com', 0 );
61 }
62
63 function fetchIds( $results ) {
64 $this->assertTrue( is_object( $results ) );
65
66 $matches = array();
67 $row = $results->next();
68 while ( $row ) {
69 $matches[] = $row->getTitle()->getPrefixedText();
70 $row = $results->next();
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 /**
81 * Insert a new page
82 *
83 * @param $pageName String: page name
84 * @param $text String: page's content
85 * @param $n Integer: unused
86 */
87 function insertPage( $pageName, $text, $ns ) {
88 $dbw = $this->db;
89 $title = Title::newFromText( $pageName );
90
91 $user = User::newFromName( 'WikiSysop' );
92 $comment = 'Search Test';
93
94 // avoid memory leak...?
95 $linkCache = LinkCache::singleton();
96 $linkCache->clear();
97
98 $article = new Article( $title );
99 $article->doEdit( $text, $comment, 0, false, $user );
100
101 $this->pageList[] = array( $title, $article->getId() );
102
103 return true;
104 }
105
106 function testFullWidth() {
107 $this->assertEquals(
108 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
109 $this->fetchIds( $this->search->searchText( 'AZ' ) ),
110 "Search for normalized from Half-width Upper" );
111 $this->assertEquals(
112 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
113 $this->fetchIds( $this->search->searchText( 'az' ) ),
114 "Search for normalized from Half-width Lower" );
115 $this->assertEquals(
116 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
117 $this->fetchIds( $this->search->searchText( 'AZ' ) ),
118 "Search for normalized from Full-width Upper" );
119 $this->assertEquals(
120 array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
121 $this->fetchIds( $this->search->searchText( 'az' ) ),
122 "Search for normalized from Full-width Lower" );
123 }
124
125 function testTextSearch() {
126 $this->assertEquals(
127 array( 'Smithee' ),
128 $this->fetchIds( $this->search->searchText( 'smithee' ) ),
129 "Plain search failed" );
130 }
131
132 function testTextPowerSearch() {
133 $this->search->setNamespaces( array( 0, 1, 4 ) );
134 $this->assertEquals(
135 array(
136 'Smithee',
137 'Talk:Not Main Page',
138 ),
139 $this->fetchIds( $this->search->searchText( 'smithee' ) ),
140 "Power search failed" );
141 }
142
143 function testTitleSearch() {
144 $this->assertEquals(
145 array(
146 'Alan Smithee',
147 'Smithee',
148 ),
149 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
150 "Title search failed" );
151 }
152
153 function testTextTitlePowerSearch() {
154 $this->search->setNamespaces( array( 0, 1, 4 ) );
155 $this->assertEquals(
156 array(
157 'Alan Smithee',
158 'Smithee',
159 'Talk:Smithee',
160 ),
161 $this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
162 "Title power search failed" );
163 }
164
165 }