File: ApiVisualEditorTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (136 lines) | stat: -rw-r--r-- 3,692 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php

namespace MediaWiki\Extension\VisualEditor\Tests;

use MediaWiki\Config\HashConfig;
use MediaWiki\Extension\VisualEditor\ApiVisualEditor;
use MediaWiki\Registration\ExtensionRegistry;
use MediaWiki\Tests\Api\ApiTestCase;
use Wikimedia\ScopedCallback;

/**
 * @group medium
 * @group Database
 *
 * @covers \MediaWiki\Extension\VisualEditor\ApiVisualEditor
 */
class ApiVisualEditorTest extends ApiTestCase {

	/** @var ScopedCallback|null */
	private $scopedCallback;

	protected function setUp(): void {
		parent::setUp();
		$this->scopedCallback = ExtensionRegistry::getInstance()->setAttributeForTest(
			'VisualEditorAvailableNamespaces',
			[ 'User' => true, 'Template_Talk' => true ]
		);
	}

	protected function tearDown(): void {
		$this->scopedCallback = null;
		parent::tearDown();
	}

	private function loadEditor( array $overrideParams = [] ): array {
		$params = array_merge( [
			'action' => 'visualeditor',
			'paction' => 'metadata',
			'page' => 'SomeTestPage',
		], $overrideParams );
		return $this->doApiRequestWithToken( $params );
	}

	public function testLoadEditorBasic() {
		$data = $this->loadEditor()[0]['visualeditor'];

		$this->assertSame( 'success', $data['result'] );

		$properties = [
			// When updating this, also update the sample response in
			// ve.init.mw.DesktopArticleTarget.test.js
			'result',
			'notices',
			'copyrightWarning',
			'checkboxesDef',
			'checkboxesMessages',
			'protectedClasses',
			'basetimestamp',
			'starttimestamp',
			'oldid',
			'blockinfo',
			'wouldautocreate',
			'canEdit',
			'content',
			'preloaded',
			// When updating this, also update the sample response in
			// ve.init.mw.DesktopArticleTarget.test.js
		];
		foreach ( $properties as $prop ) {
			$this->assertArrayHasKey( $prop, $data, "Result has key '$prop'" );
		}

		$this->assertSameSize( $properties, $data, "No other properties are expected" );
	}

	/**
	 * @dataProvider provideLoadEditorPreload
	 */
	public function testLoadEditorPreload( bool $useMyLanguage ) {
		$content = 'Some test page content';
		$pageTitle = 'Test VE preload';
		$this->editPage( $pageTitle, $content );
		$params = [
			'preload' => $useMyLanguage ? "Special:MyLanguage/$pageTitle" : $pageTitle,
			'paction' => 'wikitext',
		];
		// NB The page isn't actually translated, so we get the same content back.
		$this->assertSame(
			$content,
			$this->loadEditor( $params )[0]['visualeditor']['content']
		);
	}

	public static function provideLoadEditorPreload() {
		return [
			'load with preload content' => [ false ],
			'load with preload via Special:MyLanguage' => [ true ],
		];
	}

	public function testIsAllowedNamespace() {
		$config = new HashConfig( [ 'VisualEditorAvailableNamespaces' => [
			0 => true,
			1 => false,
		] ] );
		$this->assertTrue( ApiVisualEditor::isAllowedNamespace( $config, 0 ) );
		$this->assertFalse( ApiVisualEditor::isAllowedNamespace( $config, 1 ) );
	}

	public function testGetAvailableNamespaceIds() {
		$config = new HashConfig( [ 'VisualEditorAvailableNamespaces' => [
			0 => true,
			1 => false,
			-1 => true,
			999999 => true,
			2 => false,
			'Template' => true,
			'Foobar' => true,
		] ] );
		$this->assertSame(
			[ -1, 0, 10, 11 ],
			ApiVisualEditor::getAvailableNamespaceIds( $config )
		);
	}

	public function testIsAllowedContentType() {
		$config = new HashConfig( [ 'VisualEditorAvailableContentModels' => [
			'on' => true,
			'off' => false,
		] ] );
		$this->assertTrue( ApiVisualEditor::isAllowedContentType( $config, 'on' ) );
		$this->assertFalse( ApiVisualEditor::isAllowedContentType( $config, 'off' ) );
		$this->assertFalse( ApiVisualEditor::isAllowedContentType( $config, 'unknown' ) );
	}

}