4 * Test class for Revision storage.
7 * ^--- important, causes temporary tables to be used instead of the real database
9 class RevisionStorageTest
extends MediaWikiTestCase
{
13 function __construct( $name = null, array $data = array(), $dataName = '' ) {
14 parent
::__construct( $name, $data, $dataName );
16 $this->tablesUsed
= array_merge( $this->tablesUsed
,
34 public function setUp() {
35 if ( !$this->the_page
) {
36 $this->the_page
= $this->createPage( 'RevisionStorageTest_the_page', "just a dummy page" );
40 protected function makeRevision( $props = null ) {
41 if ( $props === null ) $props = array();
43 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) $props['text'] = 'Lorem Ipsum';
44 if ( !isset( $props['comment'] ) ) $props['comment'] = 'just a test';
45 if ( !isset( $props['page'] ) ) $props['page'] = $this->the_page
->getId();
47 $rev = new Revision( $props );
49 $dbw = wfgetDB( DB_MASTER
);
50 $rev->insertOn( $dbw );
55 protected function createPage( $page, $text, $model = null ) {
56 if ( is_string( $page ) ) $page = Title
::newFromText( $page );
57 if ( $page instanceof Title
) $page = new WikiPage( $page );
59 if ( $page->exists() ) {
60 $page->doDeleteArticle( "done" );
63 $page->doEdit( $text, "testing", EDIT_NEW
);
68 protected function assertRevEquals( Revision
$orig, Revision
$rev = null ) {
69 $this->assertNotNull( $rev, 'missing revision' );
71 $this->assertEquals( $orig->getId(), $rev->getId() );
72 $this->assertEquals( $orig->getPage(), $rev->getPage() );
73 $this->assertEquals( $orig->getTimestamp(), $rev->getTimestamp() );
74 $this->assertEquals( $orig->getUser(), $rev->getUser() );
75 $this->assertEquals( $orig->getSha1(), $rev->getSha1() );
79 * @covers Revision::__construct
81 public function testConstructFromRow()
83 $orig = $this->makeRevision();
85 $dbr = wfgetDB( DB_SLAVE
);
86 $res = $dbr->select( 'revision', '*', array( 'rev_id' => $orig->getId() ) );
87 $this->assertTrue( is_object( $res ), 'query failed' );
89 $row = $res->fetchObject();
92 $rev = new Revision( $row );
94 $this->assertRevEquals( $orig, $rev );
98 * @covers Revision::newFromRow
100 public function testNewFromRow()
102 $orig = $this->makeRevision();
104 $dbr = wfgetDB( DB_SLAVE
);
105 $res = $dbr->select( 'revision', '*', array( 'rev_id' => $orig->getId() ) );
106 $this->assertTrue( is_object( $res ), 'query failed' );
108 $row = $res->fetchObject();
111 $rev = Revision
::newFromRow( $row );
113 $this->assertRevEquals( $orig, $rev );
118 * @covers Revision::newFromArchiveRow
120 public function testNewFromArchiveRow()
122 $page = $this->createPage( 'RevisionStorageTest_testNewFromArchiveRow', 'Lorem Ipsum' );
123 $orig = $page->getRevision();
124 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
126 $dbr = wfgetDB( DB_SLAVE
);
127 $res = $dbr->select( 'archive', '*', array( 'ar_rev_id' => $orig->getId() ) );
128 $this->assertTrue( is_object( $res ), 'query failed' );
130 $row = $res->fetchObject();
133 $rev = Revision
::newFromArchiveRow( $row );
135 $this->assertRevEquals( $orig, $rev );
139 * @covers Revision::newFromId
141 public function testNewFromId()
143 $orig = $this->makeRevision();
145 $rev = Revision
::newFromId( $orig->getId() );
147 $this->assertRevEquals( $orig, $rev );
151 * @covers Revision::fetchRevision
153 public function testFetchRevision()
155 $page = $this->createPage( 'RevisionStorageTest_testFetchRevision', 'one' );
156 $id1 = $page->getRevision()->getId();
158 $page->doEdit( 'two', 'second rev' );
159 $id2 = $page->getRevision()->getId();
161 $res = Revision
::fetchRevision( $page->getTitle() );
163 #note: order is unspecified
165 while ( ( $row = $res->fetchObject() ) ) {
166 $rows[ $row->rev_id
]= $row;
169 $row = $res->fetchObject();
170 $this->assertEquals( 1, count($rows), 'expected exactly one revision' );
171 $this->assertArrayHasKey( $id2, $rows, 'missing revision with id ' . $id2 );
175 * @covers Revision::selectFields
177 public function testSelectFields()
179 $fields = Revision
::selectFields();
181 $this->assertTrue( in_array( 'rev_id', $fields ), 'missing rev_id in list of fields');
182 $this->assertTrue( in_array( 'rev_page', $fields ), 'missing rev_page in list of fields');
183 $this->assertTrue( in_array( 'rev_timestamp', $fields ), 'missing rev_timestamp in list of fields');
184 $this->assertTrue( in_array( 'rev_user', $fields ), 'missing rev_user in list of fields');
188 * @covers Revision::getPage
190 public function testGetPage()
192 $page = $this->the_page
;
194 $orig = $this->makeRevision( array( 'page' => $page->getId() ) );
195 $rev = Revision
::newFromId( $orig->getId() );
197 $this->assertEquals( $page->getId(), $rev->getPage() );
201 * @covers Revision::getText
203 public function testGetText()
205 $orig = $this->makeRevision( array( 'text' => 'hello hello.' ) );
206 $rev = Revision
::newFromId( $orig->getId() );
208 $this->assertEquals( 'hello hello.', $rev->getText() );
212 * @covers Revision::revText
214 public function testRevText()
216 $orig = $this->makeRevision( array( 'text' => 'hello hello rev.' ) );
217 $rev = Revision
::newFromId( $orig->getId() );
219 $this->assertEquals( 'hello hello rev.', $rev->revText() );
223 * @covers Revision::getRawText
225 public function testGetRawText()
227 $orig = $this->makeRevision( array( 'text' => 'hello hello raw.' ) );
228 $rev = Revision
::newFromId( $orig->getId() );
230 $this->assertEquals( 'hello hello raw.', $rev->getRawText() );
233 * @covers Revision::isCurrent
235 public function testIsCurrent()
237 $page = $this->createPage( 'RevisionStorageTest_testIsCurrent', 'Lorem Ipsum' );
238 $rev1 = $page->getRevision();
240 # @todo: find out if this should be true
241 # $this->assertTrue( $rev1->isCurrent() );
243 $rev1x = Revision
::newFromId( $rev1->getId() );
244 $this->assertTrue( $rev1x->isCurrent() );
246 $page->doEdit( 'Bla bla', 'second rev' );
247 $rev2 = $page->getRevision();
249 # @todo: find out if this should be true
250 # $this->assertTrue( $rev2->isCurrent() );
252 $rev1x = Revision
::newFromId( $rev1->getId() );
253 $this->assertFalse( $rev1x->isCurrent() );
255 $rev2x = Revision
::newFromId( $rev2->getId() );
256 $this->assertTrue( $rev2x->isCurrent() );
260 * @covers Revision::getPrevious
262 public function testGetPrevious()
264 $page = $this->createPage( 'RevisionStorageTest_testGetPrevious', 'Lorem Ipsum testGetPrevious' );
265 $rev1 = $page->getRevision();
267 $this->assertNull( $rev1->getPrevious() );
269 $page->doEdit( 'Bla bla', 'second rev testGetPrevious' );
270 $rev2 = $page->getRevision();
272 $this->assertNotNull( $rev2->getPrevious() );
273 $this->assertEquals( $rev1->getId(), $rev2->getPrevious()->getId() );
277 * @covers Revision::getNext
279 public function testGetNext()
281 $page = $this->createPage( 'RevisionStorageTest_testGetNext', 'Lorem Ipsum testGetNext' );
282 $rev1 = $page->getRevision();
284 $this->assertNull( $rev1->getNext() );
286 $page->doEdit( 'Bla bla', 'second rev testGetNext' );
287 $rev2 = $page->getRevision();
289 $this->assertNotNull( $rev1->getNext() );
290 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
294 * @covers Revision::newNullRevision
296 public function testNewNullRevision()
298 $page = $this->createPage( 'RevisionStorageTest_testNewNullRevision', 'some testing text' );
299 $orig = $page->getRevision();
301 $dbw = wfGetDB( DB_MASTER
);
302 $rev = Revision
::newNullRevision( $dbw, $page->getId(), 'a null revision', false );
304 $this->assertNotEquals( $orig->getId(), $rev->getId(), 'new null revision shold have a different id from the original revision' );
305 $this->assertEquals( $orig->getTextId(), $rev->getTextId(), 'new null revision shold have the same text id as the original revision' );
306 $this->assertEquals( 'some testing text', $rev->getText() );