2012-06-25 14:09:08 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
2012-08-29 08:07:10 +00:00
|
|
|
* Tests for MediaWiki api.php?action=edit.
|
|
|
|
|
*
|
|
|
|
|
* @author Daniel Kinzler
|
|
|
|
|
*
|
2012-06-25 14:09:08 +00:00
|
|
|
* @group API
|
|
|
|
|
* @group Database
|
|
|
|
|
*/
|
|
|
|
|
class ApiEditPageTest extends ApiTestCase {
|
|
|
|
|
|
2012-09-12 11:43:52 +00:00
|
|
|
public function setup() {
|
|
|
|
|
global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
|
|
|
|
|
|
|
|
|
|
parent::setup();
|
|
|
|
|
|
2012-10-08 15:26:11 +00:00
|
|
|
$wgExtraNamespaces[12312] = 'Dummy';
|
|
|
|
|
$wgExtraNamespaces[12313] = 'Dummy_talk';
|
2012-09-12 11:43:52 +00:00
|
|
|
|
2012-10-08 15:26:11 +00:00
|
|
|
$wgNamespaceContentModels[12312] = "testing";
|
|
|
|
|
$wgContentHandlers["testing"] = 'DummyContentHandlerForTesting';
|
2012-09-12 11:43:52 +00:00
|
|
|
|
|
|
|
|
MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
|
|
|
|
|
$wgContLang->resetNamespaces(); # reset namespace cache
|
|
|
|
|
|
2012-06-25 14:09:08 +00:00
|
|
|
$this->doLogin();
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-12 11:43:52 +00:00
|
|
|
public function teardown() {
|
|
|
|
|
global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
|
|
|
|
|
|
2012-10-08 15:26:11 +00:00
|
|
|
unset( $wgExtraNamespaces[12312] );
|
|
|
|
|
unset( $wgExtraNamespaces[12313] );
|
2012-09-12 11:43:52 +00:00
|
|
|
|
2012-10-08 15:26:11 +00:00
|
|
|
unset( $wgNamespaceContentModels[12312] );
|
|
|
|
|
unset( $wgContentHandlers["testing"] );
|
2012-09-12 11:43:52 +00:00
|
|
|
|
|
|
|
|
MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
|
|
|
|
|
$wgContLang->resetNamespaces(); # reset namespace cache
|
|
|
|
|
|
|
|
|
|
parent::teardown();
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-29 08:07:10 +00:00
|
|
|
function testEdit( ) {
|
2012-09-19 18:07:56 +00:00
|
|
|
$name = 'Help:ApiEditPageTest_testEdit'; // assume Help namespace to default to wikitext
|
2012-06-25 14:09:08 +00:00
|
|
|
|
2012-08-29 08:07:10 +00:00
|
|
|
// -- test new page --------------------------------------------
|
|
|
|
|
$apiResult = $this->doApiRequestWithToken( array(
|
2012-06-25 14:09:08 +00:00
|
|
|
'action' => 'edit',
|
|
|
|
|
'title' => $name,
|
2012-08-29 08:07:10 +00:00
|
|
|
'text' => 'some text', ) );
|
|
|
|
|
$apiResult = $apiResult[0];
|
2012-08-29 08:07:10 +00:00
|
|
|
|
2012-09-12 11:43:52 +00:00
|
|
|
// Validate API result data
|
2012-08-29 08:07:10 +00:00
|
|
|
$this->assertArrayHasKey( 'edit', $apiResult );
|
|
|
|
|
$this->assertArrayHasKey( 'result', $apiResult['edit'] );
|
|
|
|
|
$this->assertEquals( 'Success', $apiResult['edit']['result'] );
|
|
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'new', $apiResult['edit'] );
|
|
|
|
|
$this->assertArrayNotHasKey( 'nochange', $apiResult['edit'] );
|
2012-06-25 14:09:08 +00:00
|
|
|
|
2012-08-29 08:07:10 +00:00
|
|
|
$this->assertArrayHasKey( 'pageid', $apiResult['edit'] );
|
2012-06-25 14:09:08 +00:00
|
|
|
|
2012-08-29 08:07:10 +00:00
|
|
|
// -- test existing page, no change ----------------------------
|
|
|
|
|
$data = $this->doApiRequestWithToken( array(
|
2012-06-25 14:09:08 +00:00
|
|
|
'action' => 'edit',
|
|
|
|
|
'title' => $name,
|
2012-08-29 08:07:10 +00:00
|
|
|
'text' => 'some text', ) );
|
2012-06-25 14:09:08 +00:00
|
|
|
|
|
|
|
|
$this->assertEquals( 'Success', $data[0]['edit']['result'] );
|
|
|
|
|
|
|
|
|
|
$this->assertArrayNotHasKey( 'new', $data[0]['edit'] );
|
|
|
|
|
$this->assertArrayHasKey( 'nochange', $data[0]['edit'] );
|
|
|
|
|
|
2012-08-29 08:07:10 +00:00
|
|
|
// -- test existing page, with change --------------------------
|
|
|
|
|
$data = $this->doApiRequestWithToken( array(
|
2012-06-25 14:09:08 +00:00
|
|
|
'action' => 'edit',
|
|
|
|
|
'title' => $name,
|
2012-08-29 08:07:10 +00:00
|
|
|
'text' => 'different text' ) );
|
2012-06-25 14:09:08 +00:00
|
|
|
|
|
|
|
|
$this->assertEquals( 'Success', $data[0]['edit']['result'] );
|
|
|
|
|
|
|
|
|
|
$this->assertArrayNotHasKey( 'new', $data[0]['edit'] );
|
|
|
|
|
$this->assertArrayNotHasKey( 'nochange', $data[0]['edit'] );
|
|
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'oldrevid', $data[0]['edit'] );
|
|
|
|
|
$this->assertArrayHasKey( 'newrevid', $data[0]['edit'] );
|
2012-08-29 08:07:10 +00:00
|
|
|
$this->assertNotEquals(
|
|
|
|
|
$data[0]['edit']['newrevid'],
|
|
|
|
|
$data[0]['edit']['oldrevid'],
|
|
|
|
|
"revision id should change after edit"
|
|
|
|
|
);
|
2012-06-25 14:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
2012-09-12 11:43:52 +00:00
|
|
|
function testNonTextEdit( ) {
|
|
|
|
|
$name = 'Dummy:ApiEditPageTest_testNonTextEdit';
|
|
|
|
|
$data = serialize( 'some bla bla text' );
|
|
|
|
|
|
|
|
|
|
// -- test new page --------------------------------------------
|
|
|
|
|
$apiResult = $this->doApiRequestWithToken( array(
|
|
|
|
|
'action' => 'edit',
|
|
|
|
|
'title' => $name,
|
|
|
|
|
'text' => $data, ) );
|
|
|
|
|
$apiResult = $apiResult[0];
|
|
|
|
|
|
|
|
|
|
// Validate API result data
|
|
|
|
|
$this->assertArrayHasKey( 'edit', $apiResult );
|
|
|
|
|
$this->assertArrayHasKey( 'result', $apiResult['edit'] );
|
|
|
|
|
$this->assertEquals( 'Success', $apiResult['edit']['result'] );
|
|
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'new', $apiResult['edit'] );
|
|
|
|
|
$this->assertArrayNotHasKey( 'nochange', $apiResult['edit'] );
|
|
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'pageid', $apiResult['edit'] );
|
|
|
|
|
|
|
|
|
|
// validate resulting revision
|
|
|
|
|
$page = WikiPage::factory( Title::newFromText( $name ) );
|
|
|
|
|
$this->assertEquals( "testing", $page->getContentModel() );
|
|
|
|
|
$this->assertEquals( $data, $page->getContent()->serialize() );
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-10 19:30:40 +00:00
|
|
|
static function provideEditAppend() {
|
|
|
|
|
return array(
|
|
|
|
|
array( #0: append
|
|
|
|
|
'foo', 'append', 'bar', "foobar"
|
|
|
|
|
),
|
|
|
|
|
array( #1: prepend
|
|
|
|
|
'foo', 'prepend', 'bar', "barfoo"
|
|
|
|
|
),
|
|
|
|
|
array( #2: append to empty page
|
|
|
|
|
'', 'append', 'foo', "foo"
|
|
|
|
|
),
|
|
|
|
|
array( #3: prepend to empty page
|
|
|
|
|
'', 'prepend', 'foo', "foo"
|
|
|
|
|
),
|
|
|
|
|
array( #4: append to non-existing page
|
|
|
|
|
null, 'append', 'foo', "foo"
|
|
|
|
|
),
|
|
|
|
|
array( #5: prepend to non-existing page
|
|
|
|
|
null, 'prepend', 'foo', "foo"
|
|
|
|
|
),
|
|
|
|
|
);
|
2012-06-25 14:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-10 19:30:40 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideEditAppend
|
|
|
|
|
*/
|
|
|
|
|
function testEditAppend( $text, $op, $append, $expected ) {
|
|
|
|
|
static $count = 0;
|
|
|
|
|
$count++;
|
|
|
|
|
|
|
|
|
|
// assume NS_HELP defaults to wikitext
|
|
|
|
|
$name = "Help:ApiEditPageTest_testEditAppend_$count";
|
|
|
|
|
|
|
|
|
|
// -- create page (or not) -----------------------------------------
|
|
|
|
|
if ( $text !== null ) {
|
|
|
|
|
if ( $text === '' ) {
|
|
|
|
|
// can't create an empty page, so create it with some content
|
|
|
|
|
list( $re,, ) = $this->doApiRequestWithToken( array(
|
|
|
|
|
'action' => 'edit',
|
|
|
|
|
'title' => $name,
|
|
|
|
|
'text' => '(dummy)', ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list( $re,, ) = $this->doApiRequestWithToken( array(
|
|
|
|
|
'action' => 'edit',
|
|
|
|
|
'title' => $name,
|
|
|
|
|
'text' => $text, ) );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( 'Success', $re['edit']['result'] ); // sanity
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -- try append/prepend --------------------------------------------
|
|
|
|
|
list( $re,, ) = $this->doApiRequestWithToken( array(
|
|
|
|
|
'action' => 'edit',
|
|
|
|
|
'title' => $name,
|
|
|
|
|
$op . 'text' => $append, ) );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( 'Success', $re['edit']['result'] );
|
|
|
|
|
|
|
|
|
|
// -- validate -----------------------------------------------------
|
|
|
|
|
$page = new WikiPage( Title::newFromText( $name ) );
|
|
|
|
|
$content = $page->getContent();
|
|
|
|
|
$this->assertNotNull( $content, 'Page should have been created' );
|
|
|
|
|
|
|
|
|
|
$text = $content->getNativeData();
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( $expected, $text );
|
2012-06-25 14:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-10 19:30:40 +00:00
|
|
|
function testEditSection() {
|
2012-06-25 14:09:08 +00:00
|
|
|
$this->markTestIncomplete( "not yet implemented" );
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-10 19:30:40 +00:00
|
|
|
function testUndo() {
|
2012-06-25 14:09:08 +00:00
|
|
|
$this->markTestIncomplete( "not yet implemented" );
|
|
|
|
|
}
|
|
|
|
|
}
|