*/
private $the_page;
- function __construct( $name = null, array $data = [], $dataName = '' ) {
+ public function __construct( $name = null, array $data = [], $dataName = '' ) {
parent::__construct( $name, $data, $dataName );
$this->tablesUsed = array_merge( $this->tablesUsed,
[ 'page',
'revision',
+ 'ip_changes',
'text',
'recentchanges',
}
protected function setUp() {
- global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
+ global $wgContLang;
parent::setUp();
- $wgExtraNamespaces[12312] = 'Dummy';
- $wgExtraNamespaces[12313] = 'Dummy_talk';
+ $this->mergeMwGlobalArrayValue(
+ 'wgExtraNamespaces',
+ [
+ 12312 => 'Dummy',
+ 12313 => 'Dummy_talk',
+ ]
+ );
+
+ $this->mergeMwGlobalArrayValue(
+ 'wgNamespaceContentModels',
+ [
+ 12312 => 'DUMMY',
+ ]
+ );
- $wgNamespaceContentModels[12312] = 'DUMMY';
- $wgContentHandlers['DUMMY'] = 'DummyContentHandlerForTesting';
+ $this->mergeMwGlobalArrayValue(
+ 'wgContentHandlers',
+ [
+ 'DUMMY' => 'DummyContentHandlerForTesting',
+ ]
+ );
- MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
- $wgContLang->resetNamespaces(); # reset namespace cache
+ MWNamespace::clearCaches();
+ // Reset namespace cache
+ $wgContLang->resetNamespaces();
if ( !$this->the_page ) {
$this->the_page = $this->createPage(
'RevisionStorageTest_the_page',
}
protected function tearDown() {
- global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
+ global $wgContLang;
parent::tearDown();
- unset( $wgExtraNamespaces[12312] );
- unset( $wgExtraNamespaces[12313] );
-
- unset( $wgNamespaceContentModels[12312] );
- unset( $wgContentHandlers['DUMMY'] );
-
- MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
- $wgContLang->resetNamespaces(); # reset namespace cache
+ MWNamespace::clearCaches();
+ // Reset namespace cache
+ $wgContLang->resetNamespaces();
}
protected function makeRevision( $props = null ) {
return $rev;
}
- protected function createPage( $page, $text, $model = null ) {
- if ( is_string( $page ) ) {
- if ( !preg_match( '/:/', $page ) &&
- ( $model === null || $model === CONTENT_MODEL_WIKITEXT )
- ) {
- $ns = $this->getDefaultWikitextNS();
- $page = MWNamespace::getCanonicalName( $ns ) . ':' . $page;
- }
-
- $page = Title::newFromText( $page );
+ /**
+ * @param string $titleString
+ * @param string $text
+ * @param string|null $model
+ *
+ * @return WikiPage
+ */
+ protected function createPage( $titleString, $text, $model = null ) {
+ if ( !preg_match( '/:/', $titleString ) &&
+ ( $model === null || $model === CONTENT_MODEL_WIKITEXT )
+ ) {
+ $ns = $this->getDefaultWikitextNS();
+ $titleString = MWNamespace::getCanonicalName( $ns ) . ':' . $titleString;
}
- if ( $page instanceof Title ) {
- $page = new WikiPage( $page );
- }
+ $title = Title::newFromText( $titleString );
+ $wikipage = new WikiPage( $title );
- if ( $page->exists() ) {
- $page->doDeleteArticle( "done" );
+ // Delete the article if it already exists
+ if ( $wikipage->exists() ) {
+ $wikipage->doDeleteArticle( "done" );
}
- $content = ContentHandler::makeContent( $text, $page->getTitle(), $model );
- $page->doEditContent( $content, "testing", EDIT_NEW );
+ $content = ContentHandler::makeContent( $text, $title, $model );
+ $wikipage->doEditContent( $content, __METHOD__, EDIT_NEW );
- return $page;
+ return $wikipage;
}
protected function assertRevEquals( Revision $orig, Revision $rev = null ) {
public function testConstructFromRow() {
$orig = $this->makeRevision();
- $dbr = wfGetDB( DB_SLAVE );
- $res = $dbr->select( 'revision', '*', [ 'rev_id' => $orig->getId() ] );
+ $dbr = wfGetDB( DB_REPLICA );
+ $res = $dbr->select( 'revision', Revision::selectFields(), [ 'rev_id' => $orig->getId() ] );
$this->assertTrue( is_object( $res ), 'query failed' );
$row = $res->fetchObject();
$this->assertRevEquals( $orig, $rev );
}
+ /**
+ * @covers Revision::newFromTitle
+ */
+ public function testNewFromTitle_withoutId() {
+ $page = $this->createPage(
+ __METHOD__,
+ 'GOAT',
+ CONTENT_MODEL_WIKITEXT
+ );
+ $latestRevId = $page->getLatest();
+
+ $rev = Revision::newFromTitle( $page->getTitle() );
+
+ $this->assertTrue( $page->getTitle()->equals( $rev->getTitle() ) );
+ $this->assertEquals( $latestRevId, $rev->getId() );
+ }
+
+ /**
+ * @covers Revision::newFromTitle
+ */
+ public function testNewFromTitle_withId() {
+ $page = $this->createPage(
+ __METHOD__,
+ 'GOAT',
+ CONTENT_MODEL_WIKITEXT
+ );
+ $latestRevId = $page->getLatest();
+
+ $rev = Revision::newFromTitle( $page->getTitle(), $latestRevId );
+
+ $this->assertTrue( $page->getTitle()->equals( $rev->getTitle() ) );
+ $this->assertEquals( $latestRevId, $rev->getId() );
+ }
+
+ /**
+ * @covers Revision::newFromTitle
+ */
+ public function testNewFromTitle_withBadId() {
+ $page = $this->createPage(
+ __METHOD__,
+ 'GOAT',
+ CONTENT_MODEL_WIKITEXT
+ );
+ $latestRevId = $page->getLatest();
+
+ $rev = Revision::newFromTitle( $page->getTitle(), $latestRevId + 1 );
+
+ $this->assertNull( $rev );
+ }
+
/**
* @covers Revision::newFromRow
*/
public function testNewFromRow() {
$orig = $this->makeRevision();
- $dbr = wfGetDB( DB_SLAVE );
- $res = $dbr->select( 'revision', '*', [ 'rev_id' => $orig->getId() ] );
+ $dbr = wfGetDB( DB_REPLICA );
+ $res = $dbr->select( 'revision', Revision::selectFields(), [ 'rev_id' => $orig->getId() ] );
$this->assertTrue( is_object( $res ), 'query failed' );
$row = $res->fetchObject();
$orig = $page->getRevision();
$page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
- $dbr = wfGetDB( DB_SLAVE );
- $res = $dbr->select( 'archive', '*', [ 'ar_rev_id' => $orig->getId() ] );
+ $dbr = wfGetDB( DB_REPLICA );
+ $res = $dbr->select(
+ 'archive', Revision::selectArchiveFields(), [ 'ar_rev_id' => $orig->getId() ]
+ );
$this->assertTrue( is_object( $res ), 'query failed' );
$row = $res->fetchObject();
$this->assertEquals( 'some testing text', $rev->getContent()->getNativeData() );
}
+ /**
+ * @covers Revision::insertOn
+ */
+ public function testInsertOn() {
+ $ip = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
+
+ $orig = $this->makeRevision( [
+ 'user_text' => $ip
+ ] );
+
+ // Make sure the revision was copied to ip_changes
+ $dbr = wfGetDB( DB_REPLICA );
+ $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $orig->getId() ] );
+ $row = $res->fetchObject();
+
+ $this->assertEquals( IP::toHex( $ip ), $row->ipc_hex );
+ $this->assertEquals( $orig->getTimestamp(), $row->ipc_rev_timestamp );
+ }
+
public static function provideUserWasLastToEdit() {
- return [
- [ # 0
- 3, true, # actually the last edit
- ],
- [ # 1
- 2, true, # not the current edit, but still by this user
- ],
- [ # 2
- 1, false, # edit by another user
- ],
- [ # 3
- 0, false, # first edit, by this user, but another user edited in the mean time
- ],
- ];
+ yield 'actually the last edit' => [ 3, true ];
+ yield 'not the current edit, but still by this user' => [ 2, true ];
+ yield 'edit by another user' => [ 1, false ];
+ yield 'first edit, by this user, but another user edited in the mean time' => [ 0, false ];
}
/**
'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
$page->insertOn( $dbw );
- # zero
$revisions[0] = new Revision( [
'page' => $page->getId(),
// we need the title to determine the page's default content model
] );
$revisions[0]->insertOn( $dbw );
- # one
$revisions[1] = new Revision( [
'page' => $page->getId(),
// still need the title, because $page->getId() is 0 (there's no entry in the page table)
] );
$revisions[1]->insertOn( $dbw );
- # two
$revisions[2] = new Revision( [
'page' => $page->getId(),
'title' => $page->getTitle(),
] );
$revisions[2]->insertOn( $dbw );
- # three
$revisions[3] = new Revision( [
'page' => $page->getId(),
'title' => $page->getTitle(),
] );
$revisions[3]->insertOn( $dbw );
- # four
$revisions[4] = new Revision( [
'page' => $page->getId(),
'title' => $page->getTitle(),