/
mediawiki_test.php
163 lines (143 loc) · 4.87 KB
/
mediawiki_test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
require_once 'config.php';
/**
* Test the MediaWiki API client.
*
* It appears that SimpleTest does not allow cookies to be set from an outside
* method, so full coverage is impossible when edit credentials are required.
* Until a solution is found, this test case will fail if the test MediaWiki
* requires credentials.
*/
class TestMediawiki extends UnitTestCase
{
const TEST_TITLE = 'SGVsbG8gV29ybGQ=';
/**
* Contains no dynamic or custom Wikitext (e.g. signatures, media file
* links, references.).
*/
const TEST_WIKITEXT = "'''Bold text'''
''Italic text''
[http://www.example.com link title]
== Headline text ==
<nowiki>Insert non-formatted text here</nowiki>
----
<s>Strike-through text</s>
<br />
<sup>Superscript text</sup>
<sub>Subscript text</sub>
<small>Small Text</small>
<blockquote>
Block quote
</blockquote>
{| class=\"wikitable\"
|-
! header 1
! header 2
! header 3
|-
| row 1, cell 1
| row 1, cell 2
| row 1, cell 3
|-
| row 2, cell 1
| row 2, cell 2
| row 2, cell 3
|}";
/**
* When getting a preview and page HTML, MediaWiki returns an HTML comment
* containing a dynamic "NewPP limit report." Here, this is removed prior to
* asserting valid get responses.
*/
const TEST_EXPECTED_HTML = '<p><b>Bold text</b>
<i>Italic text</i>
<a rel="nofollow" class="external text" href="http://www.example.com">link title</a>
</p>
<h2> <span class="mw-headline" id="Headline_text"> Headline text </span></h2>
<p>Insert non-formatted text here
</p>
<hr />
<p><s>Strike-through text</s>
<br />
<sup>Superscript text</sup>
<sub>Subscript text</sub>
<small>Small Text</small>
</p>
<blockquote>
Block quote
</blockquote>
<table class="wikitable">
<tr>
<th> header 1
</th>
<th> header 2
</th>
<th> header 3
</th></tr>
<tr>
<td> row 1, cell 1
</td>
<td> row 1, cell 2
</td>
<td> row 1, cell 3
</td></tr>
<tr>
<td> row 2, cell 1
</td>
<td> row 2, cell 2
</td>
<td> row 2, cell 3
</td></tr></table>
';
const TEST_EXPECTED_PREVIEW = '';
private $_testMediawiki;
private $_testEditCredentials;
/**
* Use __construct() instead of setUp() because it's unnecessary to set up
* the test case before every test method.
*/
public function __construct()
{
parent::__construct();
// Do not pass cookies to a browser when testing.
require_once 'Scripto/Service/MediaWiki.php';
$this->_testMediawiki = new Scripto_Service_MediaWiki(TEST_MEDIAWIKI_API_URL, false);
}
public function testCredentials()
{
// Test login and logout if username and password is provided.
if (TEST_MEDIAWIKI_USERNAME && TEST_MEDIAWIKI_PASSWORD) {
// Assert login works. Throws an error if login is unsuccessful.
$this->_testMediawiki->login(TEST_MEDIAWIKI_USERNAME, TEST_MEDIAWIKI_PASSWORD);
// Assert logout works.
$this->_testMediawiki->logout();
$userInfo = $this->_testMediawiki->getUserInfo();
$this->assertTrue(isset($userInfo['query']['userinfo']['anon']), 'Logout unsuccessful');
}
}
public function testEditPage()
{
// Assert the test page's preview is valid. Remove dynamic HTML comments.
$testPagePreview = $this->_testMediawiki->getPreview(self::TEST_WIKITEXT);
$this->assertEqual(self::TEST_EXPECTED_HTML, $this->_removeHtmlComments($testPagePreview), 'The test page preview HTML is invalid');
// Clear the page before testing edit page. Resetting the database or
// deleting the page is preferable, but resetting is too involved and
// Scripto_Service_MediaWiki does not implement a delete page feature
// because deleting requires special (sysops) permissions.
$this->_testMediawiki->edit(self::TEST_TITLE, '.');
$text = $this->_testMediawiki->getLatestRevisionWikitext(self::TEST_TITLE);
$this->assertEqual('.', $text, 'Clearing the test page did not work');
// Edit the page with test text.
$this->_testMediawiki->edit(self::TEST_TITLE, self::TEST_WIKITEXT);
// Assert the test page's Wikitext is valid.
$textPageWikitext = $this->_testMediawiki->getLatestRevisionWikitext(self::TEST_TITLE);
$this->assertEqual(self::TEST_WIKITEXT, $textPageWikitext, 'Editing the test page with test wikitext did not work ');
// Assert the test page's HTML is valid. Remove dynamic HTML comments.
$testPageHtml = $this->_testMediawiki->getLatestRevisionHtml(self::TEST_TITLE);
$this->assertEqual(self::TEST_EXPECTED_HTML, $this->_removeHtmlComments($testPageHtml), 'The test page HTML is invalid');
}
private function _removeHtmlComments($text)
{
// Must include "s" modifier so "." matches new lines.
return preg_replace('/<!--.*-->/s', '', $text);
}
}