merge latest master into Wikidata branch
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiEditPageTest.php
1 <?php
2
3 /**
4 * Tests for MediaWiki api.php?action=edit.
5 *
6 * @author Daniel Kinzler
7 *
8 * @group API
9 * @group Database
10 */
11 class ApiEditPageTest extends ApiTestCase {
12
13 public function setup() {
14 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
15
16 parent::setup();
17
18 $wgExtraNamespaces[ 12312 ] = 'Dummy';
19 $wgExtraNamespaces[ 12313 ] = 'Dummy_talk';
20
21 $wgNamespaceContentModels[ 12312 ] = "testing";
22 $wgContentHandlers[ "testing" ] = 'DummyContentHandlerForTesting';
23
24 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
25 $wgContLang->resetNamespaces(); # reset namespace cache
26
27 $this->doLogin();
28 }
29
30 public function teardown() {
31 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
32
33 unset( $wgExtraNamespaces[ 12312 ] );
34 unset( $wgExtraNamespaces[ 12313 ] );
35
36 unset( $wgNamespaceContentModels[ 12312 ] );
37 unset( $wgContentHandlers[ "testing" ] );
38
39 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
40 $wgContLang->resetNamespaces(); # reset namespace cache
41
42 parent::teardown();
43 }
44
45 function testEdit( ) {
46 $name = 'Help:ApiEditPageTest_testEdit'; // assume Help namespace to default to wikitext
47
48 // -- test new page --------------------------------------------
49 $apiResult = $this->doApiRequestWithToken( array(
50 'action' => 'edit',
51 'title' => $name,
52 'text' => 'some text', ) );
53 $apiResult = $apiResult[0];
54
55 // Validate API result data
56 $this->assertArrayHasKey( 'edit', $apiResult );
57 $this->assertArrayHasKey( 'result', $apiResult['edit'] );
58 $this->assertEquals( 'Success', $apiResult['edit']['result'] );
59
60 $this->assertArrayHasKey( 'new', $apiResult['edit'] );
61 $this->assertArrayNotHasKey( 'nochange', $apiResult['edit'] );
62
63 $this->assertArrayHasKey( 'pageid', $apiResult['edit'] );
64
65 // -- test existing page, no change ----------------------------
66 $data = $this->doApiRequestWithToken( array(
67 'action' => 'edit',
68 'title' => $name,
69 'text' => 'some text', ) );
70
71 $this->assertEquals( 'Success', $data[0]['edit']['result'] );
72
73 $this->assertArrayNotHasKey( 'new', $data[0]['edit'] );
74 $this->assertArrayHasKey( 'nochange', $data[0]['edit'] );
75
76 // -- test existing page, with change --------------------------
77 $data = $this->doApiRequestWithToken( array(
78 'action' => 'edit',
79 'title' => $name,
80 'text' => 'different text' ) );
81
82 $this->assertEquals( 'Success', $data[0]['edit']['result'] );
83
84 $this->assertArrayNotHasKey( 'new', $data[0]['edit'] );
85 $this->assertArrayNotHasKey( 'nochange', $data[0]['edit'] );
86
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"
93 );
94 }
95
96 function testNonTextEdit( ) {
97 $name = 'Dummy:ApiEditPageTest_testNonTextEdit';
98 $data = serialize( 'some bla bla text' );
99
100 // -- test new page --------------------------------------------
101 $apiResult = $this->doApiRequestWithToken( array(
102 'action' => 'edit',
103 'title' => $name,
104 'text' => $data, ) );
105 $apiResult = $apiResult[0];
106
107 // Validate API result data
108 $this->assertArrayHasKey( 'edit', $apiResult );
109 $this->assertArrayHasKey( 'result', $apiResult['edit'] );
110 $this->assertEquals( 'Success', $apiResult['edit']['result'] );
111
112 $this->assertArrayHasKey( 'new', $apiResult['edit'] );
113 $this->assertArrayNotHasKey( 'nochange', $apiResult['edit'] );
114
115 $this->assertArrayHasKey( 'pageid', $apiResult['edit'] );
116
117 // validate resulting revision
118 $page = WikiPage::factory( Title::newFromText( $name ) );
119 $this->assertEquals( "testing", $page->getContentModel() );
120 $this->assertEquals( $data, $page->getContent()->serialize() );
121 }
122
123 function testEditAppend() {
124 $this->markTestIncomplete( "not yet implemented" );
125 }
126
127 function testEditSection() {
128 $this->markTestIncomplete( "not yet implemented" );
129 }
130
131 function testUndo() {
132 $this->markTestIncomplete( "not yet implemented" );
133 }
134
135 function testEditNonText() {
136 $this->markTestIncomplete( "not yet implemented" );
137 }
138 }