Debian Codegraph @

extensions/VisualEditor/tests/phpunit/ApiVisualEditorTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\VisualEditor\Tests;
4
5 use MediaWiki\Config\HashConfig;
6 use MediaWiki\Extension\VisualEditor\ApiVisualEditor;
7 use MediaWiki\Registration\ExtensionRegistry;
8 use MediaWiki\Tests\Api\ApiTestCase;
9 use Wikimedia\ScopedCallback;
10
11 /**
12 * @group medium
13 * @group Database
14 *
15 * @covers \MediaWiki\Extension\VisualEditor\ApiVisualEditor
16 */
17 class ApiVisualEditorTest extends ApiTestCase {
18
19 /** @var ScopedCallback|null */
20 private $scopedCallback;
21
22 protected function setUp(): void {
23 parent::setUp();
24 $this->scopedCallback = ExtensionRegistry::getInstance()->setAttributeForTest(
25 'VisualEditorAvailableNamespaces',
26 [ 'User' => true, 'Template_Talk' => true ]
27 );
28 }
29
30 protected function tearDown(): void {
31 $this->scopedCallback = null;
32 parent::tearDown();
33 }
34
35 private function loadEditor( array $overrideParams = [] ): array {
36 $params = array_merge( [
37 'action' => 'visualeditor',
38 'paction' => 'metadata',
39 'page' => 'SomeTestPage',
40 ], $overrideParams );
41 return $this->doApiRequestWithToken( $params );
42 }
43
44 public function testLoadEditorBasic() {
45 $data = $this->loadEditor()[0]['visualeditor'];
46
47 $this->assertSame( 'success', $data['result'] );
48
49 $properties = [
50 // When updating this, also update the sample response in
51 // ve.init.mw.DesktopArticleTarget.test.js
52 'result',
53 'notices',
54 'copyrightWarning',
55 'checkboxesDef',
56 'checkboxesMessages',
57 'protectedClasses',
58 'basetimestamp',
59 'starttimestamp',
60 'oldid',
61 'blockinfo',
62 'wouldautocreate',
63 'canEdit',
64 'content',
65 'preloaded',
66 // When updating this, also update the sample response in
67 // ve.init.mw.DesktopArticleTarget.test.js
68 ];
69 foreach ( $properties as $prop ) {
70 $this->assertArrayHasKey( $prop, $data, "Result has key '$prop'" );
71 }
72
73 $this->assertSameSize( $properties, $data, "No other properties are expected" );
74 }
75
76 /**
77 * @dataProvider provideLoadEditorPreload
78 */
79 public function testLoadEditorPreload( bool $useMyLanguage ) {
80 $content = 'Some test page content';
81 $pageTitle = 'Test VE preload';
82 $this->editPage( $pageTitle, $content );
83 $params = [
84 'preload' => $useMyLanguage ? "Special:MyLanguage/$pageTitle" : $pageTitle,
85 'paction' => 'wikitext',
86 ];
87 // NB The page isn't actually translated, so we get the same content back.
88 $this->assertSame(
89 $content,
90 $this->loadEditor( $params )[0]['visualeditor']['content']
91 );
92 }
93
94 public static function provideLoadEditorPreload() {
95 return [
96 'load with preload content' => [ false ],
97 'load with preload via Special:MyLanguage' => [ true ],
98 ];
99 }
100
101 public function testIsAllowedNamespace() {
102 $config = new HashConfig( [ 'VisualEditorAvailableNamespaces' => [
103 0 => true,
104 1 => false,
105 ] ] );
106 $this->assertTrue( ApiVisualEditor::isAllowedNamespace( $config, 0 ) );
107 $this->assertFalse( ApiVisualEditor::isAllowedNamespace( $config, 1 ) );
108 }
109
110 public function testGetAvailableNamespaceIds() {
111 $config = new HashConfig( [ 'VisualEditorAvailableNamespaces' => [
112 0 => true,
113 1 => false,
114 -1 => true,
115 999999 => true,
116 2 => false,
117 'Template' => true,
118 'Foobar' => true,
119 ] ] );
120 $this->assertSame(
121 [ -1, 0, 10, 11 ],
122 ApiVisualEditor::getAvailableNamespaceIds( $config )
123 );
124 }
125
126 public function testIsAllowedContentType() {
127 $config = new HashConfig( [ 'VisualEditorAvailableContentModels' => [
128 'on' => true,
129 'off' => false,
130 ] ] );
131 $this->assertTrue( ApiVisualEditor::isAllowedContentType( $config, 'on' ) );
132 $this->assertFalse( ApiVisualEditor::isAllowedContentType( $config, 'off' ) );
133 $this->assertFalse( ApiVisualEditor::isAllowedContentType( $config, 'unknown' ) );
134 }
135
136 }