4 * Test class for Revision storage.
7 * ^--- important, causes temporary tables to be used instead of the real database
9 class RevisionStorageTest
extends PHPUnit_Framework_TestCase
{
13 public function setUp() {
14 if ( !$this->the_page
) {
15 $this->the_page
= $this->createPage( 'RevisionStorageTest_the_page', "just a dummy page" );
19 protected function makeRevision( $props = null ) {
20 if ( $props === null ) $props = array();
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();
26 $rev = new Revision( $props );
28 $dbw = wfgetDB( DB_MASTER
);
29 $rev->insertOn( $dbw );
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 );
38 if ( $page->exists() ) {
39 $page->doDeleteArticle( "done" );
42 $page->doEdit( $text, "testing", EDIT_NEW
);
47 protected function assertRevEquals( Revision
$orig, Revision
$rev = null ) {
48 $this->assertNotNull( $rev, 'missing revision' );
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() );
58 * @covers Revision::__construct
60 public function testConstructFromRow()
62 $orig = $this->makeRevision();
64 $dbr = wfgetDB( DB_SLAVE
);
65 $res = $dbr->select( 'revision', '*', array( 'rev_id' => $orig->getId() ) );
66 $this->assertTrue( is_object( $res ), 'query failed' );
68 $row = $res->fetchObject();
71 $rev = new Revision( $row );
73 $this->assertRevEquals( $orig, $rev );
77 * @covers Revision::newFromRow
79 public function testNewFromRow()
81 $orig = $this->makeRevision();
83 $dbr = wfgetDB( DB_SLAVE
);
84 $res = $dbr->select( 'revision', '*', array( 'rev_id' => $orig->getId() ) );
85 $this->assertTrue( is_object( $res ), 'query failed' );
87 $row = $res->fetchObject();
90 $rev = Revision
::newFromRow( $row );
92 $this->assertRevEquals( $orig, $rev );
97 * @covers Revision::newFromArchiveRow
99 public function testNewFromArchiveRow()
101 $page = $this->createPage( 'RevisionStorageTest_testNewFromArchiveRow', 'Lorem Ipsum' );
102 $orig = $page->getRevision();
103 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
105 $dbr = wfgetDB( DB_SLAVE
);
106 $res = $dbr->select( 'archive', '*', array( 'ar_rev_id' => $orig->getId() ) );
107 $this->assertTrue( is_object( $res ), 'query failed' );
109 $row = $res->fetchObject();
112 $rev = Revision
::newFromArchiveRow( $row );
114 $this->assertRevEquals( $orig, $rev );
118 * @covers Revision::newFromId
120 public function testNewFromId()
122 $orig = $this->makeRevision();
124 $rev = Revision
::newFromId( $orig->getId() );
126 $this->assertRevEquals( $orig, $rev );
130 * @covers Revision::fetchRevision
132 public function testFetchRevision()
134 $page = $this->createPage( 'RevisionStorageTest_testFetchRevision', 'one' );
135 $id1 = $page->getRevision()->getId();
137 $page->doEdit( 'two', 'second rev' );
138 $id2 = $page->getRevision()->getId();
140 $res = Revision
::fetchRevision( $page->getTitle() );
142 #note: order is unspecified
144 while ( ( $row = $res->fetchObject() ) ) {
145 $rows[ $row->rev_id
]= $row;
148 $row = $res->fetchObject();
149 $this->assertEquals( 1, count($rows), 'expected exactly one revision' );
150 $this->assertArrayHasKey( $id2, $rows, 'missing revision with id ' . $id2 );
154 * @covers Revision::selectFields
156 public function testSelectFields()
158 $fields = Revision
::selectFields();
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');
167 * @covers Revision::getPage
169 public function testGetPage()
171 $page = $this->the_page
;
173 $orig = $this->makeRevision( array( 'page' => $page->getId() ) );
174 $rev = Revision
::newFromId( $orig->getId() );
176 $this->assertEquals( $page->getId(), $rev->getPage() );
180 * @covers Revision::getText
182 public function testGetText()
184 $orig = $this->makeRevision( array( 'text' => 'hello hello.' ) );
185 $rev = Revision
::newFromId( $orig->getId() );
187 $this->assertEquals( 'hello hello.', $rev->getText() );
191 * @covers Revision::revText
193 public function testRevText()
195 $orig = $this->makeRevision( array( 'text' => 'hello hello rev.' ) );
196 $rev = Revision
::newFromId( $orig->getId() );
198 $this->assertEquals( 'hello hello rev.', $rev->revText() );
202 * @covers Revision::getRawText
204 public function testGetRawText()
206 $orig = $this->makeRevision( array( 'text' => 'hello hello raw.' ) );
207 $rev = Revision
::newFromId( $orig->getId() );
209 $this->assertEquals( 'hello hello raw.', $rev->getRawText() );
212 * @covers Revision::isCurrent
214 public function testIsCurrent()
216 $page = $this->createPage( 'RevisionStorageTest_testIsCurrent', 'Lorem Ipsum' );
217 $rev1 = $page->getRevision();
219 # @todo: find out if this should be true
220 # $this->assertTrue( $rev1->isCurrent() );
222 $rev1x = Revision
::newFromId( $rev1->getId() );
223 $this->assertTrue( $rev1x->isCurrent() );
225 $page->doEdit( 'Bla bla', 'second rev' );
226 $rev2 = $page->getRevision();
228 # @todo: find out if this should be true
229 # $this->assertTrue( $rev2->isCurrent() );
231 $rev1x = Revision
::newFromId( $rev1->getId() );
232 $this->assertFalse( $rev1x->isCurrent() );
234 $rev2x = Revision
::newFromId( $rev2->getId() );
235 $this->assertTrue( $rev2x->isCurrent() );
239 * @covers Revision::getPrevious
241 public function testGetPrevious()
243 $page = $this->createPage( 'RevisionStorageTest_testGetPrevious', 'Lorem Ipsum testGetPrevious' );
244 $rev1 = $page->getRevision();
246 $this->assertNull( $rev1->getPrevious() );
248 $page->doEdit( 'Bla bla', 'second rev testGetPrevious' );
249 $rev2 = $page->getRevision();
251 $this->assertNotNull( $rev2->getPrevious() );
252 $this->assertEquals( $rev1->getId(), $rev2->getPrevious()->getId() );
256 * @covers Revision::getNext
258 public function testGetNext()
260 $page = $this->createPage( 'RevisionStorageTest_testGetNext', 'Lorem Ipsum testGetNext' );
261 $rev1 = $page->getRevision();
263 $this->assertNull( $rev1->getNext() );
265 $page->doEdit( 'Bla bla', 'second rev testGetNext' );
266 $rev2 = $page->getRevision();
268 $this->assertNotNull( $rev1->getNext() );
269 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
273 * @covers Revision::newNullRevision
275 public function testNewNullRevision()
277 $page = $this->createPage( 'RevisionStorageTest_testNewNullRevision', 'some testing text' );
278 $orig = $page->getRevision();
280 $dbw = wfGetDB( DB_MASTER
);
281 $rev = Revision
::newNullRevision( $dbw, $page->getId(), 'a null revision', false );
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() );