Merge "(bug 18195) Allow changing preferences via API"
[lhc/web/wiklou.git] / tests / phpunit / includes / RevisionStorageTest.php
1 <?php
2
3 /**
4 * Test class for Revision storage.
5 *
6 * @group Database
7 * ^--- important, causes temporary tables to be used instead of the real database
8 */
9 class RevisionStorageTest extends PHPUnit_Framework_TestCase {
10
11 var $the_page;
12
13 public function setUp() {
14 if ( !$this->the_page ) {
15 $this->the_page = $this->createPage( 'RevisionStorageTest_the_page', "just a dummy page" );
16 }
17 }
18
19 protected function makeRevision( $props = null ) {
20 if ( $props === null ) $props = array();
21
22 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) $props['text'] = 'Lorem Ipsum';
23 if ( !isset( $props['comment'] ) ) $props['comment'] = 'just a test';
24 if ( !isset( $props['page'] ) ) $props['page'] = $this->the_page->getId();
25
26 $rev = new Revision( $props );
27
28 $dbw = wfgetDB( DB_MASTER );
29 $rev->insertOn( $dbw );
30
31 return $rev;
32 }
33
34 protected function createPage( $page, $text, $model = null ) {
35 if ( is_string( $page ) ) $page = Title::newFromText( $page );
36 if ( $page instanceof Title ) $page = new WikiPage( $page );
37
38 if ( $page->exists() ) {
39 $page->doDeleteArticle( "done" );
40 }
41
42 $page->doEdit( $text, "testing", EDIT_NEW );
43
44 return $page;
45 }
46
47 protected function assertRevEquals( Revision $orig, Revision $rev = null ) {
48 $this->assertNotNull( $rev, 'missing revision' );
49
50 $this->assertEquals( $orig->getId(), $rev->getId() );
51 $this->assertEquals( $orig->getPage(), $rev->getPage() );
52 $this->assertEquals( $orig->getTimestamp(), $rev->getTimestamp() );
53 $this->assertEquals( $orig->getUser(), $rev->getUser() );
54 $this->assertEquals( $orig->getSha1(), $rev->getSha1() );
55 }
56
57 /**
58 * @covers Revision::__construct
59 */
60 public function testConstructFromRow()
61 {
62 $orig = $this->makeRevision();
63
64 $dbr = wfgetDB( DB_SLAVE );
65 $res = $dbr->select( 'revision', '*', array( 'rev_id' => $orig->getId() ) );
66 $this->assertTrue( is_object( $res ), 'query failed' );
67
68 $row = $res->fetchObject();
69 $res->free();
70
71 $rev = new Revision( $row );
72
73 $this->assertRevEquals( $orig, $rev );
74 }
75
76 /**
77 * @covers Revision::newFromRow
78 */
79 public function testNewFromRow()
80 {
81 $orig = $this->makeRevision();
82
83 $dbr = wfgetDB( DB_SLAVE );
84 $res = $dbr->select( 'revision', '*', array( 'rev_id' => $orig->getId() ) );
85 $this->assertTrue( is_object( $res ), 'query failed' );
86
87 $row = $res->fetchObject();
88 $res->free();
89
90 $rev = Revision::newFromRow( $row );
91
92 $this->assertRevEquals( $orig, $rev );
93 }
94
95
96 /**
97 * @covers Revision::newFromArchiveRow
98 */
99 public function testNewFromArchiveRow()
100 {
101 $page = $this->createPage( 'RevisionStorageTest_testNewFromArchiveRow', 'Lorem Ipsum' );
102 $orig = $page->getRevision();
103 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
104
105 $dbr = wfgetDB( DB_SLAVE );
106 $res = $dbr->select( 'archive', '*', array( 'ar_rev_id' => $orig->getId() ) );
107 $this->assertTrue( is_object( $res ), 'query failed' );
108
109 $row = $res->fetchObject();
110 $res->free();
111
112 $rev = Revision::newFromArchiveRow( $row );
113
114 $this->assertRevEquals( $orig, $rev );
115 }
116
117 /**
118 * @covers Revision::newFromId
119 */
120 public function testNewFromId()
121 {
122 $orig = $this->makeRevision();
123
124 $rev = Revision::newFromId( $orig->getId() );
125
126 $this->assertRevEquals( $orig, $rev );
127 }
128
129 /**
130 * @covers Revision::fetchRevision
131 */
132 public function testFetchRevision()
133 {
134 $page = $this->createPage( 'RevisionStorageTest_testFetchRevision', 'one' );
135 $id1 = $page->getRevision()->getId();
136
137 $page->doEdit( 'two', 'second rev' );
138 $id2 = $page->getRevision()->getId();
139
140 $res = Revision::fetchRevision( $page->getTitle() );
141
142 #note: order is unspecified
143 $rows = array();
144 while ( ( $row = $res->fetchObject() ) ) {
145 $rows[ $row->rev_id ]= $row;
146 }
147
148 $row = $res->fetchObject();
149 $this->assertEquals( 1, count($rows), 'expected exactly one revision' );
150 $this->assertArrayHasKey( $id2, $rows, 'missing revision with id ' . $id2 );
151 }
152
153 /**
154 * @covers Revision::selectFields
155 */
156 public function testSelectFields()
157 {
158 $fields = Revision::selectFields();
159
160 $this->assertTrue( in_array( 'rev_id', $fields ), 'missing rev_id in list of fields');
161 $this->assertTrue( in_array( 'rev_page', $fields ), 'missing rev_page in list of fields');
162 $this->assertTrue( in_array( 'rev_timestamp', $fields ), 'missing rev_timestamp in list of fields');
163 $this->assertTrue( in_array( 'rev_user', $fields ), 'missing rev_user in list of fields');
164 }
165
166 /**
167 * @covers Revision::getPage
168 */
169 public function testGetPage()
170 {
171 $page = $this->the_page;
172
173 $orig = $this->makeRevision( array( 'page' => $page->getId() ) );
174 $rev = Revision::newFromId( $orig->getId() );
175
176 $this->assertEquals( $page->getId(), $rev->getPage() );
177 }
178
179 /**
180 * @covers Revision::getText
181 */
182 public function testGetText()
183 {
184 $orig = $this->makeRevision( array( 'text' => 'hello hello.' ) );
185 $rev = Revision::newFromId( $orig->getId() );
186
187 $this->assertEquals( 'hello hello.', $rev->getText() );
188 }
189
190 /**
191 * @covers Revision::revText
192 */
193 public function testRevText()
194 {
195 $orig = $this->makeRevision( array( 'text' => 'hello hello rev.' ) );
196 $rev = Revision::newFromId( $orig->getId() );
197
198 $this->assertEquals( 'hello hello rev.', $rev->revText() );
199 }
200
201 /**
202 * @covers Revision::getRawText
203 */
204 public function testGetRawText()
205 {
206 $orig = $this->makeRevision( array( 'text' => 'hello hello raw.' ) );
207 $rev = Revision::newFromId( $orig->getId() );
208
209 $this->assertEquals( 'hello hello raw.', $rev->getRawText() );
210 }
211 /**
212 * @covers Revision::isCurrent
213 */
214 public function testIsCurrent()
215 {
216 $page = $this->createPage( 'RevisionStorageTest_testIsCurrent', 'Lorem Ipsum' );
217 $rev1 = $page->getRevision();
218
219 # @todo: find out if this should be true
220 # $this->assertTrue( $rev1->isCurrent() );
221
222 $rev1x = Revision::newFromId( $rev1->getId() );
223 $this->assertTrue( $rev1x->isCurrent() );
224
225 $page->doEdit( 'Bla bla', 'second rev' );
226 $rev2 = $page->getRevision();
227
228 # @todo: find out if this should be true
229 # $this->assertTrue( $rev2->isCurrent() );
230
231 $rev1x = Revision::newFromId( $rev1->getId() );
232 $this->assertFalse( $rev1x->isCurrent() );
233
234 $rev2x = Revision::newFromId( $rev2->getId() );
235 $this->assertTrue( $rev2x->isCurrent() );
236 }
237
238 /**
239 * @covers Revision::getPrevious
240 */
241 public function testGetPrevious()
242 {
243 $page = $this->createPage( 'RevisionStorageTest_testGetPrevious', 'Lorem Ipsum testGetPrevious' );
244 $rev1 = $page->getRevision();
245
246 $this->assertNull( $rev1->getPrevious() );
247
248 $page->doEdit( 'Bla bla', 'second rev testGetPrevious' );
249 $rev2 = $page->getRevision();
250
251 $this->assertNotNull( $rev2->getPrevious() );
252 $this->assertEquals( $rev1->getId(), $rev2->getPrevious()->getId() );
253 }
254
255 /**
256 * @covers Revision::getNext
257 */
258 public function testGetNext()
259 {
260 $page = $this->createPage( 'RevisionStorageTest_testGetNext', 'Lorem Ipsum testGetNext' );
261 $rev1 = $page->getRevision();
262
263 $this->assertNull( $rev1->getNext() );
264
265 $page->doEdit( 'Bla bla', 'second rev testGetNext' );
266 $rev2 = $page->getRevision();
267
268 $this->assertNotNull( $rev1->getNext() );
269 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
270 }
271
272 /**
273 * @covers Revision::newNullRevision
274 */
275 public function testNewNullRevision()
276 {
277 $page = $this->createPage( 'RevisionStorageTest_testNewNullRevision', 'some testing text' );
278 $orig = $page->getRevision();
279
280 $dbw = wfGetDB( DB_MASTER );
281 $rev = Revision::newNullRevision( $dbw, $page->getId(), 'a null revision', false );
282
283 $this->assertNotEquals( $orig->getId(), $rev->getId(), 'new null revision shold have a different id from the original revision' );
284 $this->assertEquals( $orig->getTextId(), $rev->getTextId(), 'new null revision shold have the same text id as the original revision' );
285 $this->assertEquals( 'some testing text', $rev->getText() );
286 }
287 }
288 ?>