4 * Test class for Revision storage.
6 * @group ContentHandler
8 * ^--- important, causes temporary tables to be used instead of the real database
10 class RevisionStorageTest
extends PHPUnit_Framework_TestCase
{
14 public function setUp() {
15 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
17 $wgExtraNamespaces[ 12312 ] = 'Dummy';
18 $wgExtraNamespaces[ 12313 ] = 'Dummy_talk';
20 $wgNamespaceContentModels[ 12312 ] = 'DUMMY';
21 $wgContentHandlers[ 'DUMMY' ] = 'DummyContentHandlerForTesting';
23 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
24 $wgContLang->resetNamespaces(); # reset namespace cache
26 if ( !$this->the_page
) {
27 $this->the_page
= $this->createPage( 'RevisionStorageTest_the_page', "just a dummy page" );
31 public function tearDown() {
32 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
34 unset( $wgExtraNamespaces[ 12312 ] );
35 unset( $wgExtraNamespaces[ 12313 ] );
37 unset( $wgNamespaceContentModels[ 12312 ] );
38 unset( $wgContentHandlers[ 'DUMMY' ] );
40 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
41 $wgContLang->resetNamespaces(); # reset namespace cache
44 protected function makeRevision( $props = null ) {
45 if ( $props === null ) $props = array();
47 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) $props['text'] = 'Lorem Ipsum';
48 if ( !isset( $props['comment'] ) ) $props['comment'] = 'just a test';
49 if ( !isset( $props['page'] ) ) $props['page'] = $this->the_page
->getId();
51 $rev = new Revision( $props );
53 $dbw = wfgetDB( DB_MASTER
);
54 $rev->insertOn( $dbw );
59 protected function createPage( $page, $text, $model = null ) {
60 if ( is_string( $page ) ) $page = Title
::newFromText( $page );
61 if ( $page instanceof Title
) $page = new WikiPage( $page );
63 if ( $page->exists() ) {
64 $page->doDeleteArticle( "done" );
67 $content = ContentHandler
::makeContent( $text, $page->getTitle(), $model );
68 $page->doEditContent( $content, "testing", EDIT_NEW
);
73 protected function assertRevEquals( Revision
$orig, Revision
$rev = null ) {
74 $this->assertNotNull( $rev, 'missing revision' );
76 $this->assertEquals( $orig->getId(), $rev->getId() );
77 $this->assertEquals( $orig->getPage(), $rev->getPage() );
78 $this->assertEquals( $orig->getTimestamp(), $rev->getTimestamp() );
79 $this->assertEquals( $orig->getUser(), $rev->getUser() );
80 $this->assertEquals( $orig->getContentModel(), $rev->getContentModel() );
81 $this->assertEquals( $orig->getContentFormat(), $rev->getContentFormat() );
82 $this->assertEquals( $orig->getSha1(), $rev->getSha1() );
86 * @covers Revision::__construct
88 public function testConstructFromRow()
90 $orig = $this->makeRevision();
92 $dbr = wfgetDB( DB_SLAVE
);
93 $res = $dbr->select( 'revision', '*', array( 'rev_id' => $orig->getId() ) );
94 $this->assertTrue( is_object( $res ), 'query failed' );
96 $row = $res->fetchObject();
99 $rev = new Revision( $row );
101 $this->assertRevEquals( $orig, $rev );
105 * @covers Revision::newFromRow
107 public function testNewFromRow()
109 $orig = $this->makeRevision();
111 $dbr = wfgetDB( DB_SLAVE
);
112 $res = $dbr->select( 'revision', '*', array( 'rev_id' => $orig->getId() ) );
113 $this->assertTrue( is_object( $res ), 'query failed' );
115 $row = $res->fetchObject();
118 $rev = Revision
::newFromRow( $row );
120 $this->assertRevEquals( $orig, $rev );
125 * @covers Revision::newFromArchiveRow
127 public function testNewFromArchiveRow()
129 $page = $this->createPage( 'RevisionStorageTest_testNewFromArchiveRow', 'Lorem Ipsum' );
130 $orig = $page->getRevision();
131 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
133 $dbr = wfgetDB( DB_SLAVE
);
134 $res = $dbr->select( 'archive', '*', array( 'ar_rev_id' => $orig->getId() ) );
135 $this->assertTrue( is_object( $res ), 'query failed' );
137 $row = $res->fetchObject();
140 $rev = Revision
::newFromArchiveRow( $row );
142 $this->assertRevEquals( $orig, $rev );
146 * @covers Revision::newFromId
148 public function testNewFromId()
150 $orig = $this->makeRevision();
152 $rev = Revision
::newFromId( $orig->getId() );
154 $this->assertRevEquals( $orig, $rev );
158 * @covers Revision::fetchRevision
160 public function testFetchRevision()
162 $page = $this->createPage( 'RevisionStorageTest_testFetchRevision', 'one' );
163 $id1 = $page->getRevision()->getId();
165 $page->doEdit( 'two', 'second rev' );
166 $id2 = $page->getRevision()->getId();
168 $res = Revision
::fetchRevision( $page->getTitle() );
170 #note: order is unspecified
172 while ( ( $row = $res->fetchObject() ) ) {
173 $rows[ $row->rev_id
]= $row;
176 $row = $res->fetchObject();
177 $this->assertEquals( 1, count($rows), 'expected exactly one revision' );
178 $this->assertArrayHasKey( $id2, $rows, 'missing revision with id ' . $id2 );
182 * @covers Revision::selectFields
184 public function testSelectFields()
186 $fields = Revision
::selectFields();
188 $this->assertTrue( in_array( 'rev_id', $fields ), 'missing rev_id in list of fields');
189 $this->assertTrue( in_array( 'rev_page', $fields ), 'missing rev_page in list of fields');
190 $this->assertTrue( in_array( 'rev_timestamp', $fields ), 'missing rev_timestamp in list of fields');
191 $this->assertTrue( in_array( 'rev_user', $fields ), 'missing rev_user in list of fields');
193 $this->assertTrue( in_array( 'rev_content_model', $fields ), 'missing rev_content_model in list of fields');
194 $this->assertTrue( in_array( 'rev_content_format', $fields ), 'missing rev_content_format in list of fields');
198 * @covers Revision::getPage
200 public function testGetPage()
202 $page = $this->the_page
;
204 $orig = $this->makeRevision( array( 'page' => $page->getId() ) );
205 $rev = Revision
::newFromId( $orig->getId() );
207 $this->assertEquals( $page->getId(), $rev->getPage() );
211 * @covers Revision::getText
213 public function testGetText()
215 $orig = $this->makeRevision( array( 'text' => 'hello hello.' ) );
216 $rev = Revision
::newFromId( $orig->getId() );
218 $this->assertEquals( 'hello hello.', $rev->getText() );
222 * @covers Revision::getContent
224 public function testGetContent()
226 $orig = $this->makeRevision( array( 'text' => 'hello hello.' ) );
227 $rev = Revision
::newFromId( $orig->getId() );
229 $this->assertEquals( 'hello hello.', $rev->getContent()->getNativeData() );
233 * @covers Revision::revText
235 public function testRevText()
237 $orig = $this->makeRevision( array( 'text' => 'hello hello rev.' ) );
238 $rev = Revision
::newFromId( $orig->getId() );
240 $this->assertEquals( 'hello hello rev.', $rev->revText() );
244 * @covers Revision::getRawText
246 public function testGetRawText()
248 $orig = $this->makeRevision( array( 'text' => 'hello hello raw.' ) );
249 $rev = Revision
::newFromId( $orig->getId() );
251 $this->assertEquals( 'hello hello raw.', $rev->getRawText() );
255 * @covers Revision::getContentModel
257 public function testGetContentModel()
259 $orig = $this->makeRevision( array( 'text' => 'hello hello.', 'content_model' => CONTENT_MODEL_JAVASCRIPT
) );
260 $rev = Revision
::newFromId( $orig->getId() );
262 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
266 * @covers Revision::getContentFormat
268 public function testGetContentFormat()
270 $orig = $this->makeRevision( array( 'text' => 'hello hello.', 'content_model' => CONTENT_MODEL_JAVASCRIPT
, 'content_format' => CONTENT_FORMAT_JAVASCRIPT
) );
271 $rev = Revision
::newFromId( $orig->getId() );
273 $this->assertEquals( CONTENT_FORMAT_JAVASCRIPT
, $rev->getContentFormat() );
277 * @covers Revision::isCurrent
279 public function testIsCurrent()
281 $page = $this->createPage( 'RevisionStorageTest_testIsCurrent', 'Lorem Ipsum' );
282 $rev1 = $page->getRevision();
284 # @todo: find out if this should be true
285 # $this->assertTrue( $rev1->isCurrent() );
287 $rev1x = Revision
::newFromId( $rev1->getId() );
288 $this->assertTrue( $rev1x->isCurrent() );
290 $page->doEditContent( ContentHandler
::makeContent( 'Bla bla', $page->getTitle() ), 'second rev' );
291 $rev2 = $page->getRevision();
293 # @todo: find out if this should be true
294 # $this->assertTrue( $rev2->isCurrent() );
296 $rev1x = Revision
::newFromId( $rev1->getId() );
297 $this->assertFalse( $rev1x->isCurrent() );
299 $rev2x = Revision
::newFromId( $rev2->getId() );
300 $this->assertTrue( $rev2x->isCurrent() );
304 * @covers Revision::getPrevious
306 public function testGetPrevious()
308 $page = $this->createPage( 'RevisionStorageTest_testGetPrevious', 'Lorem Ipsum testGetPrevious' );
309 $rev1 = $page->getRevision();
311 $this->assertNull( $rev1->getPrevious() );
313 $page->doEditContent( ContentHandler
::makeContent( 'Bla bla', $page->getTitle() ), 'second rev testGetPrevious' );
314 $rev2 = $page->getRevision();
316 $this->assertNotNull( $rev2->getPrevious() );
317 $this->assertEquals( $rev1->getId(), $rev2->getPrevious()->getId() );
321 * @covers Revision::getNext
323 public function testGetNext()
325 $page = $this->createPage( 'RevisionStorageTest_testGetNext', 'Lorem Ipsum testGetNext' );
326 $rev1 = $page->getRevision();
328 $this->assertNull( $rev1->getNext() );
330 $page->doEditContent( ContentHandler
::makeContent( 'Bla bla', $page->getTitle() ), 'second rev testGetNext' );
331 $rev2 = $page->getRevision();
333 $this->assertNotNull( $rev1->getNext() );
334 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
338 * @covers Revision::newNullRevision
340 public function testNewNullRevision()
342 $page = $this->createPage( 'RevisionStorageTest_testNewNullRevision', 'some testing text' );
343 $orig = $page->getRevision();
345 $dbw = wfGetDB( DB_MASTER
);
346 $rev = Revision
::newNullRevision( $dbw, $page->getId(), 'a null revision', false );
348 $this->assertNotEquals( $orig->getId(), $rev->getId(), 'new null revision shold have a different id from the original revision' );
349 $this->assertEquals( $orig->getTextId(), $rev->getTextId(), 'new null revision shold have the same text id as the original revision' );
350 $this->assertEquals( 'some testing text', $rev->getContent()->getNativeData() );