* Removed require/require_once from maintenance scripts where possible, replaced...
[lhc/web/wiklou.git] / maintenance / tests / SearchUpdateTest.php
1 <?php
2
3 class DatabaseMock extends DatabaseBase {
4 function __construct( $server = false, $user = false, $password = false, $dbName = false,
5 $failFunction = false, $flags = 0, $tablePrefix = 'get from global' )
6 {
7 $this->mConn = true;
8 $this->mOpened = true;
9 }
10
11 function open( $server, $user, $password, $dbName ) { return true; }
12 function doQuery( $sql ) { }
13 function fetchObject( $res ) { }
14 function fetchRow( $res ) { }
15 function numRows( $res ) { }
16 function numFields( $res ) { }
17 function fieldName( $res, $n ) { }
18 function insertId() { }
19 function dataSeek( $res, $row ) { }
20 function lastErrno() { return 0; }
21 function lastError() { return ''; }
22 function affectedRows() { }
23 function fieldInfo( $table, $field ) { }
24 function strencode( $s ) { }
25 function getSoftwareLink() { }
26 function getServerVersion() { }
27 function getType() { }
28 }
29
30 class MockSearch extends SearchEngine {
31 public static $id;
32 public static $title;
33 public static $text;
34
35 public function __construct( $db ) {
36 }
37
38 public function update( $id, $title, $text ) {
39 self::$id = $id;
40 self::$title = $title;
41 self::$text = $text;
42 }
43 }
44
45 class SearchUpdateTest extends PHPUnit_Framework_TestCase {
46 static $searchType;
47 static $dbtype;
48 static $factoryconf;
49 static $dbservers;
50
51 function update( $text, $title = 'Test', $id = 1 ) {
52 $u = new SearchUpdate( $id, $title, $text );
53 $u->doUpdate();
54 return array( MockSearch::$title, MockSearch::$text );
55 }
56
57 function updateText( $text ) {
58 list( $title, $resultText ) = $this->update( $text );
59 $resultText = trim( $resultText ); // abstract from some implementation details
60 return $resultText;
61 }
62
63 function setUp() {
64 global $wgSearchType, $wgDBtype, $wgLBFactoryConf, $wgDBservers;
65
66 self::$searchType = $wgSearchType;
67 self::$dbtype = $wgDBtype;
68 self::$factoryconf = $wgLBFactoryConf;
69 self::$dbservers = $wgDBservers;
70
71 $wgSearchType = 'MockSearch';
72 $wgDBtype = 'mock';
73 $wgLBFactoryConf['class'] = 'LBFactory_Simple';
74 $wgDBservers = null;
75 LBFactory::destroyInstance();
76 }
77
78 function tearDown() {
79 global $wgSearchType, $wgDBtype, $wgLBFactoryConf, $wgDBservers;
80
81 LBFactory::destroyInstance();
82
83 $wgSearchType = self::$searchType;
84 $wgDBtype = self::$dbtype;
85 $wgLBFactoryConf = self::$factoryconf;
86 $wgDBservers = self::$dbservers;
87 }
88
89 function testUpdateText() {
90 $this->assertEquals(
91 'test',
92 $this->updateText( '<div>TeSt</div>' ),
93 'HTML stripped, text lowercased'
94 );
95
96 $this->assertEquals(
97 'foo bar boz quux',
98 $this->updateText( <<<EOT
99 <table style="color:red; font-size:100px">
100 <tr class="scary"><td><div>foo</div></td><tr>bar</td></tr>
101 <tr><td>boz</td><tr>quux</td></tr>
102 </table>
103 EOT
104 ), 'Stripping HTML tables' );
105
106 $this->assertEquals(
107 'a b',
108 $this->updateText( 'a > b' ),
109 'Handle unclosed tags'
110 );
111
112 $text = str_pad( "foo <barbarbar \n", 10000, 'x' );
113
114 $this->assertNotEquals(
115 '',
116 $this->updateText( $text ),
117 'Bug 18609'
118 );
119 }
120 }