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 MediaWikiTestCase
{
14 function __construct( $name = null, array $data = array(), $dataName = '' ) {
15 parent
::__construct( $name, $data, $dataName );
17 $this->tablesUsed
= array_merge( $this->tablesUsed
,
35 public function setUp() {
36 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
38 $wgExtraNamespaces[ 12312 ] = 'Dummy';
39 $wgExtraNamespaces[ 12313 ] = 'Dummy_talk';
41 $wgNamespaceContentModels[ 12312 ] = 'DUMMY';
42 $wgContentHandlers[ 'DUMMY' ] = 'DummyContentHandlerForTesting';
44 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
45 $wgContLang->resetNamespaces(); # reset namespace cache
47 if ( !$this->the_page
) {
48 $this->the_page
= $this->createPage( 'RevisionStorageTest_the_page', "just a dummy page" );
52 public function tearDown() {
53 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
55 unset( $wgExtraNamespaces[ 12312 ] );
56 unset( $wgExtraNamespaces[ 12313 ] );
58 unset( $wgNamespaceContentModels[ 12312 ] );
59 unset( $wgContentHandlers[ 'DUMMY' ] );
61 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
62 $wgContLang->resetNamespaces(); # reset namespace cache
65 protected function makeRevision( $props = null ) {
66 if ( $props === null ) $props = array();
68 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) $props['text'] = 'Lorem Ipsum';
69 if ( !isset( $props['comment'] ) ) $props['comment'] = 'just a test';
70 if ( !isset( $props['page'] ) ) $props['page'] = $this->the_page
->getId();
72 $rev = new Revision( $props );
74 $dbw = wfgetDB( DB_MASTER
);
75 $rev->insertOn( $dbw );
80 protected function createPage( $page, $text, $model = null ) {
81 if ( is_string( $page ) ) $page = Title
::newFromText( $page );
82 if ( $page instanceof Title
) $page = new WikiPage( $page );
84 if ( $page->exists() ) {
85 $page->doDeleteArticle( "done" );
88 $content = ContentHandler
::makeContent( $text, $page->getTitle(), $model );
89 $page->doEditContent( $content, "testing", EDIT_NEW
);
94 protected function assertRevEquals( Revision
$orig, Revision
$rev = null ) {
95 $this->assertNotNull( $rev, 'missing revision' );
97 $this->assertEquals( $orig->getId(), $rev->getId() );
98 $this->assertEquals( $orig->getPage(), $rev->getPage() );
99 $this->assertEquals( $orig->getTimestamp(), $rev->getTimestamp() );
100 $this->assertEquals( $orig->getUser(), $rev->getUser() );
101 $this->assertEquals( $orig->getContentModel(), $rev->getContentModel() );
102 $this->assertEquals( $orig->getContentFormat(), $rev->getContentFormat() );
103 $this->assertEquals( $orig->getSha1(), $rev->getSha1() );
107 * @covers Revision::__construct
109 public function testConstructFromRow()
111 $orig = $this->makeRevision();
113 $dbr = wfgetDB( DB_SLAVE
);
114 $res = $dbr->select( 'revision', '*', array( 'rev_id' => $orig->getId() ) );
115 $this->assertTrue( is_object( $res ), 'query failed' );
117 $row = $res->fetchObject();
120 $rev = new Revision( $row );
122 $this->assertRevEquals( $orig, $rev );
126 * @covers Revision::newFromRow
128 public function testNewFromRow()
130 $orig = $this->makeRevision();
132 $dbr = wfgetDB( DB_SLAVE
);
133 $res = $dbr->select( 'revision', '*', array( 'rev_id' => $orig->getId() ) );
134 $this->assertTrue( is_object( $res ), 'query failed' );
136 $row = $res->fetchObject();
139 $rev = Revision
::newFromRow( $row );
141 $this->assertRevEquals( $orig, $rev );
146 * @covers Revision::newFromArchiveRow
148 public function testNewFromArchiveRow()
150 $page = $this->createPage( 'RevisionStorageTest_testNewFromArchiveRow', 'Lorem Ipsum' );
151 $orig = $page->getRevision();
152 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
154 $dbr = wfgetDB( DB_SLAVE
);
155 $res = $dbr->select( 'archive', '*', array( 'ar_rev_id' => $orig->getId() ) );
156 $this->assertTrue( is_object( $res ), 'query failed' );
158 $row = $res->fetchObject();
161 $rev = Revision
::newFromArchiveRow( $row );
163 $this->assertRevEquals( $orig, $rev );
167 * @covers Revision::newFromId
169 public function testNewFromId()
171 $orig = $this->makeRevision();
173 $rev = Revision
::newFromId( $orig->getId() );
175 $this->assertRevEquals( $orig, $rev );
179 * @covers Revision::fetchRevision
181 public function testFetchRevision()
183 $page = $this->createPage( 'RevisionStorageTest_testFetchRevision', 'one' );
184 $id1 = $page->getRevision()->getId();
186 $page->doEdit( 'two', 'second rev' );
187 $id2 = $page->getRevision()->getId();
189 $res = Revision
::fetchRevision( $page->getTitle() );
191 #note: order is unspecified
193 while ( ( $row = $res->fetchObject() ) ) {
194 $rows[ $row->rev_id
]= $row;
197 $row = $res->fetchObject();
198 $this->assertEquals( 1, count($rows), 'expected exactly one revision' );
199 $this->assertArrayHasKey( $id2, $rows, 'missing revision with id ' . $id2 );
203 * @covers Revision::selectFields
205 public function testSelectFields()
207 $fields = Revision
::selectFields();
209 $this->assertTrue( in_array( 'rev_id', $fields ), 'missing rev_id in list of fields');
210 $this->assertTrue( in_array( 'rev_page', $fields ), 'missing rev_page in list of fields');
211 $this->assertTrue( in_array( 'rev_timestamp', $fields ), 'missing rev_timestamp in list of fields');
212 $this->assertTrue( in_array( 'rev_user', $fields ), 'missing rev_user in list of fields');
214 $this->assertTrue( in_array( 'rev_content_model', $fields ), 'missing rev_content_model in list of fields');
215 $this->assertTrue( in_array( 'rev_content_format', $fields ), 'missing rev_content_format in list of fields');
219 * @covers Revision::getPage
221 public function testGetPage()
223 $page = $this->the_page
;
225 $orig = $this->makeRevision( array( 'page' => $page->getId() ) );
226 $rev = Revision
::newFromId( $orig->getId() );
228 $this->assertEquals( $page->getId(), $rev->getPage() );
232 * @covers Revision::getText
234 public function testGetText()
236 $orig = $this->makeRevision( array( 'text' => 'hello hello.' ) );
237 $rev = Revision
::newFromId( $orig->getId() );
239 $this->assertEquals( 'hello hello.', $rev->getText() );
243 * @covers Revision::getContent
245 public function testGetContent()
247 $orig = $this->makeRevision( array( 'text' => 'hello hello.' ) );
248 $rev = Revision
::newFromId( $orig->getId() );
250 $this->assertEquals( 'hello hello.', $rev->getContent()->getNativeData() );
254 * @covers Revision::revText
256 public function testRevText()
258 $orig = $this->makeRevision( array( 'text' => 'hello hello rev.' ) );
259 $rev = Revision
::newFromId( $orig->getId() );
261 $this->assertEquals( 'hello hello rev.', $rev->revText() );
265 * @covers Revision::getRawText
267 public function testGetRawText()
269 $orig = $this->makeRevision( array( 'text' => 'hello hello raw.' ) );
270 $rev = Revision
::newFromId( $orig->getId() );
272 $this->assertEquals( 'hello hello raw.', $rev->getRawText() );
276 * @covers Revision::getContentModel
278 public function testGetContentModel()
280 $orig = $this->makeRevision( array( 'text' => 'hello hello.', 'content_model' => CONTENT_MODEL_JAVASCRIPT
) );
281 $rev = Revision
::newFromId( $orig->getId() );
283 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
287 * @covers Revision::getContentFormat
289 public function testGetContentFormat()
291 $orig = $this->makeRevision( array( 'text' => 'hello hello.', 'content_model' => CONTENT_MODEL_JAVASCRIPT
, 'content_format' => CONTENT_FORMAT_JAVASCRIPT
) );
292 $rev = Revision
::newFromId( $orig->getId() );
294 $this->assertEquals( CONTENT_FORMAT_JAVASCRIPT
, $rev->getContentFormat() );
298 * @covers Revision::isCurrent
300 public function testIsCurrent()
302 $page = $this->createPage( 'RevisionStorageTest_testIsCurrent', 'Lorem Ipsum' );
303 $rev1 = $page->getRevision();
305 # @todo: find out if this should be true
306 # $this->assertTrue( $rev1->isCurrent() );
308 $rev1x = Revision
::newFromId( $rev1->getId() );
309 $this->assertTrue( $rev1x->isCurrent() );
311 $page->doEditContent( ContentHandler
::makeContent( 'Bla bla', $page->getTitle() ), 'second rev' );
312 $rev2 = $page->getRevision();
314 # @todo: find out if this should be true
315 # $this->assertTrue( $rev2->isCurrent() );
317 $rev1x = Revision
::newFromId( $rev1->getId() );
318 $this->assertFalse( $rev1x->isCurrent() );
320 $rev2x = Revision
::newFromId( $rev2->getId() );
321 $this->assertTrue( $rev2x->isCurrent() );
325 * @covers Revision::getPrevious
327 public function testGetPrevious()
329 $page = $this->createPage( 'RevisionStorageTest_testGetPrevious', 'Lorem Ipsum testGetPrevious' );
330 $rev1 = $page->getRevision();
332 $this->assertNull( $rev1->getPrevious() );
334 $page->doEditContent( ContentHandler
::makeContent( 'Bla bla', $page->getTitle() ), 'second rev testGetPrevious' );
335 $rev2 = $page->getRevision();
337 $this->assertNotNull( $rev2->getPrevious() );
338 $this->assertEquals( $rev1->getId(), $rev2->getPrevious()->getId() );
342 * @covers Revision::getNext
344 public function testGetNext()
346 $page = $this->createPage( 'RevisionStorageTest_testGetNext', 'Lorem Ipsum testGetNext' );
347 $rev1 = $page->getRevision();
349 $this->assertNull( $rev1->getNext() );
351 $page->doEditContent( ContentHandler
::makeContent( 'Bla bla', $page->getTitle() ), 'second rev testGetNext' );
352 $rev2 = $page->getRevision();
354 $this->assertNotNull( $rev1->getNext() );
355 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
359 * @covers Revision::newNullRevision
361 public function testNewNullRevision()
363 $page = $this->createPage( 'RevisionStorageTest_testNewNullRevision', 'some testing text' );
364 $orig = $page->getRevision();
366 $dbw = wfGetDB( DB_MASTER
);
367 $rev = Revision
::newNullRevision( $dbw, $page->getId(), 'a null revision', false );
369 $this->assertNotEquals( $orig->getId(), $rev->getId(), 'new null revision shold have a different id from the original revision' );
370 $this->assertEquals( $orig->getTextId(), $rev->getTextId(), 'new null revision shold have the same text id as the original revision' );
371 $this->assertEquals( 'some testing text', $rev->getContent()->getNativeData() );