Debian Codegraph @

tests/phpunit/includes/editpage/TextboxBuilderTest.php

raw symbols SCIP
1 <?php
2 /**
3 * Copyright (C) 2017 Kunal Mehta <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 */
20
21 namespace MediaWiki\Tests\EditPage;
22
23 use MediaWiki\EditPage\TextboxBuilder;
24 use MediaWiki\Language\Language;
25 use MediaWiki\MainConfigNames;
26 use MediaWiki\Page\PageIdentityValue;
27 use MediaWiki\Permissions\RestrictionStore;
28 use MediaWiki\Title\Title;
29 use MediaWiki\User\Options\StaticUserOptionsLookup;
30 use MediaWiki\User\UserIdentityValue;
31 use MediaWikiIntegrationTestCase;
32
33 /**
34 * See also unit tests at \MediaWiki\Tests\Unit\EditPage\TextboxBuilderTest
35 *
36 * @covers \MediaWiki\EditPage\TextboxBuilder
37 */
38 class TextboxBuilderTest extends MediaWikiIntegrationTestCase {
39
40 public static function provideGetTextboxProtectionCSSClasses() {
41 return [
42 [
43 [ '' ],
44 [ 'isProtected' ],
45 [],
46 ],
47 [
48 [ '', 'something' ],
49 [],
50 [],
51 ],
52 [
53 [ '', 'something' ],
54 [ 'isProtected' ],
55 [ 'mw-textarea-protected' ]
56 ],
57 [
58 [ '', 'something' ],
59 [ 'isProtected', 'isSemiProtected' ],
60 [ 'mw-textarea-sprotected' ],
61 ],
62 [
63 [ '', 'something' ],
64 [ 'isProtected', 'isCascadeProtected' ],
65 [ 'mw-textarea-protected', 'mw-textarea-cprotected' ],
66 ],
67 [
68 [ '', 'something' ],
69 [ 'isProtected', 'isCascadeProtected', 'isSemiProtected' ],
70 [ 'mw-textarea-sprotected', 'mw-textarea-cprotected' ],
71 ],
72 ];
73 }
74
75 /**
76 * @dataProvider provideGetTextboxProtectionCSSClasses
77 */
78 public function testGetTextboxProtectionCSSClasses(
79 $restrictionLevels,
80 $protectionModes,
81 $expected
82 ) {
83 $this->overrideConfigValue(
84 // set to trick PermissionManager::getNamespaceRestrictionLevels
85 MainConfigNames::RestrictionLevels, $restrictionLevels
86 );
87
88 $mockRestrictionStore = $this->createMock( RestrictionStore::class );
89 $pageIdValue = PageIdentityValue::localIdentity( 1, NS_MAIN, 'test' );
90
91 $mockRestrictionStore->method(
92 $this->logicalOr( ...array_map( [ $this, 'identicalTo' ], $protectionModes ) )
93 )->willReturn( true );
94
95 $this->setService( 'RestrictionStore', $mockRestrictionStore );
96
97 $builder = new TextboxBuilder();
98 $this->assertSame( $expected, $builder->getTextboxProtectionCSSClasses( $pageIdValue ) );
99 }
100
101 public function testBuildTextboxAttribs() {
102 $user = UserIdentityValue::newRegistered( 42, 'Test' );
103 $mockUserOptionsLookup = new StaticUserOptionsLookup( [
104 'Test' => [ 'editfont' => 'monospace' ],
105 ] );
106 $this->setService( 'UserOptionsLookup', $mockUserOptionsLookup );
107
108 $enLanguage = $this->createMock( Language::class );
109 $enLanguage->method( 'getHtmlCode' )->willReturn( 'en' );
110 $enLanguage->method( 'getDir' )->willReturn( 'ltr' );
111
112 $title = $this->createMock( Title::class );
113 $title->method( 'getPageLanguage' )->willReturn( $enLanguage );
114
115 $builder = new TextboxBuilder();
116 $attribs = $builder->buildTextboxAttribs(
117 'mw-textbox1',
118 [ 'class' => 'foo bar', 'data-foo' => '123', 'rows' => 30 ],
119 $user,
120 $title
121 );
122
123 $this->assertIsArray( $attribs );
124 // custom attrib showed up
125 $this->assertArrayHasKey( 'data-foo', $attribs );
126 // classes merged properly (string)
127 $this->assertSame( 'foo bar mw-editfont-monospace', $attribs['class'] );
128 // overrides in custom attrib worked
129 $this->assertSame( 30, $attribs['rows'] );
130 $this->assertSame( 'en', $attribs['lang'] );
131
132 $attribs2 = $builder->buildTextboxAttribs(
133 'mw-textbox2', [ 'class' => [ 'foo', 'bar' ] ], $user, $title
134 );
135 // classes merged properly (array)
136 $this->assertSame( [ 'foo', 'bar', 'mw-editfont-monospace' ], $attribs2['class'] );
137
138 $attribs3 = $builder->buildTextboxAttribs(
139 'mw-textbox3', [], $user, $title
140 );
141 // classes ok when nothing to be merged
142 $this->assertSame( 'mw-editfont-monospace', $attribs3['class'] );
143 }
144 }