4 * Tests for MediaWiki api.php?action=edit.
6 * @author Daniel Kinzler
11 class ApiEditPageTest
extends ApiTestCase
{
13 public function setup() {
14 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
18 $wgExtraNamespaces[12312] = 'Dummy';
19 $wgExtraNamespaces[12313] = 'Dummy_talk';
21 $wgNamespaceContentModels[12312] = "testing";
22 $wgContentHandlers["testing"] = 'DummyContentHandlerForTesting';
24 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
25 $wgContLang->resetNamespaces(); # reset namespace cache
30 public function teardown() {
31 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
33 unset( $wgExtraNamespaces[12312] );
34 unset( $wgExtraNamespaces[12313] );
36 unset( $wgNamespaceContentModels[12312] );
37 unset( $wgContentHandlers["testing"] );
39 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
40 $wgContLang->resetNamespaces(); # reset namespace cache
45 function testEdit( ) {
46 $name = 'Help:ApiEditPageTest_testEdit'; // assume Help namespace to default to wikitext
48 // -- test new page --------------------------------------------
49 $apiResult = $this->doApiRequestWithToken( array(
52 'text' => 'some text', ) );
53 $apiResult = $apiResult[0];
55 // Validate API result data
56 $this->assertArrayHasKey( 'edit', $apiResult );
57 $this->assertArrayHasKey( 'result', $apiResult['edit'] );
58 $this->assertEquals( 'Success', $apiResult['edit']['result'] );
60 $this->assertArrayHasKey( 'new', $apiResult['edit'] );
61 $this->assertArrayNotHasKey( 'nochange', $apiResult['edit'] );
63 $this->assertArrayHasKey( 'pageid', $apiResult['edit'] );
65 // -- test existing page, no change ----------------------------
66 $data = $this->doApiRequestWithToken( array(
69 'text' => 'some text', ) );
71 $this->assertEquals( 'Success', $data[0]['edit']['result'] );
73 $this->assertArrayNotHasKey( 'new', $data[0]['edit'] );
74 $this->assertArrayHasKey( 'nochange', $data[0]['edit'] );
76 // -- test existing page, with change --------------------------
77 $data = $this->doApiRequestWithToken( array(
80 'text' => 'different text' ) );
82 $this->assertEquals( 'Success', $data[0]['edit']['result'] );
84 $this->assertArrayNotHasKey( 'new', $data[0]['edit'] );
85 $this->assertArrayNotHasKey( 'nochange', $data[0]['edit'] );
87 $this->assertArrayHasKey( 'oldrevid', $data[0]['edit'] );
88 $this->assertArrayHasKey( 'newrevid', $data[0]['edit'] );
89 $this->assertNotEquals(
90 $data[0]['edit']['newrevid'],
91 $data[0]['edit']['oldrevid'],
92 "revision id should change after edit"
96 function testNonTextEdit( ) {
97 $name = 'Dummy:ApiEditPageTest_testNonTextEdit';
98 $data = serialize( 'some bla bla text' );
100 // -- test new page --------------------------------------------
101 $apiResult = $this->doApiRequestWithToken( array(
104 'text' => $data, ) );
105 $apiResult = $apiResult[0];
107 // Validate API result data
108 $this->assertArrayHasKey( 'edit', $apiResult );
109 $this->assertArrayHasKey( 'result', $apiResult['edit'] );
110 $this->assertEquals( 'Success', $apiResult['edit']['result'] );
112 $this->assertArrayHasKey( 'new', $apiResult['edit'] );
113 $this->assertArrayNotHasKey( 'nochange', $apiResult['edit'] );
115 $this->assertArrayHasKey( 'pageid', $apiResult['edit'] );
117 // validate resulting revision
118 $page = WikiPage
::factory( Title
::newFromText( $name ) );
119 $this->assertEquals( "testing", $page->getContentModel() );
120 $this->assertEquals( $data, $page->getContent()->serialize() );
123 static function provideEditAppend() {
126 'foo', 'append', 'bar', "foobar"
129 'foo', 'prepend', 'bar', "barfoo"
131 array( #2: append to empty page
132 '', 'append', 'foo', "foo"
134 array( #3: prepend to empty page
135 '', 'prepend', 'foo', "foo"
137 array( #4: append to non-existing page
138 null, 'append', 'foo', "foo"
140 array( #5: prepend to non-existing page
141 null, 'prepend', 'foo', "foo"
147 * @dataProvider provideEditAppend
149 function testEditAppend( $text, $op, $append, $expected ) {
153 // assume NS_HELP defaults to wikitext
154 $name = "Help:ApiEditPageTest_testEditAppend_$count";
156 // -- create page (or not) -----------------------------------------
157 if ( $text !== null ) {
158 if ( $text === '' ) {
159 // can't create an empty page, so create it with some content
160 list( $re,, ) = $this->doApiRequestWithToken( array(
163 'text' => '(dummy)', ) );
166 list( $re,, ) = $this->doApiRequestWithToken( array(
169 'text' => $text, ) );
171 $this->assertEquals( 'Success', $re['edit']['result'] ); // sanity
174 // -- try append/prepend --------------------------------------------
175 list( $re,, ) = $this->doApiRequestWithToken( array(
178 $op . 'text' => $append, ) );
180 $this->assertEquals( 'Success', $re['edit']['result'] );
182 // -- validate -----------------------------------------------------
183 $page = new WikiPage( Title
::newFromText( $name ) );
184 $content = $page->getContent();
185 $this->assertNotNull( $content, 'Page should have been created' );
187 $text = $content->getNativeData();
189 $this->assertEquals( $expected, $text );
192 function testEditSection() {
193 $this->markTestIncomplete( "not yet implemented" );
196 function testUndo() {
197 $this->markTestIncomplete( "not yet implemented" );