d21319a4d51a65cb6177b5032b0ec5f350f1f9ef
[lhc/web/wiklou.git] / 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
47 function update( $text, $title = 'Test', $id = 1 ) {
48 $u = new SearchUpdate( $id, $title, $text );
49 $u->doUpdate();
50 return array( MockSearch::$title, MockSearch::$text );
51 }
52
53 function updateText( $text ) {
54 list( $title, $resultText ) = $this->update( $text );
55 $resultText = trim( $resultText ); // abstract from some implementation details
56 return $resultText;
57 }
58
59 function setUp() {
60 global $wgSearchType, $wgDBtype, $wgLBFactoryConf, $wgDBservers;
61 $wgSearchType = 'MockSearch';
62 $wgDBtype = 'mock';
63 $wgLBFactoryConf['class'] = 'LBFactory_Simple';
64 $wgDBservers = null;
65 LBFactory::destroyInstance();
66 }
67
68 function tearDown() {
69 LBFactory::destroyInstance();
70 }
71
72 function testUpdateText() {
73 $this->assertEquals(
74 'test',
75 $this->updateText( '<div>TeSt</div>' ),
76 'HTML stripped, text lowercased'
77 );
78
79 $this->assertEquals(
80 'foo bar boz quux',
81 $this->updateText( <<<EOT
82 <table style="color:red; font-size:100px">
83 <tr class="scary"><td><div>foo</div></td><tr>bar</td></tr>
84 <tr><td>boz</td><tr>quux</td></tr>
85 </table>
86 EOT
87 ), 'Stripping HTML tables' );
88
89 $this->assertEquals(
90 'a b',
91 $this->updateText( 'a > b' ),
92 'Handle unclosed tags'
93 );
94
95 $text = str_pad( "foo <barbarbar \n", 10000, 'x' );
96
97 $this->assertNotEquals(
98 '',
99 $this->updateText( $text ),
100 'Bug 18609'
101 );
102 }
103 }