Debian Codegraph @

extensions/Cite/tests/phpunit/CiteHooksTest.php

raw symbols SCIP
1 <?php
2
3 namespace Cite\Tests;
4
5 use Cite\Hooks\CiteHooks;
6 use Cite\ReferencePreviews\ReferencePreviewsGadgetsIntegration;
7 use MediaWiki\Api\ApiQuerySiteinfo;
8 use MediaWiki\Config\HashConfig;
9 use MediaWiki\Registration\ExtensionRegistry;
10 use MediaWiki\ResourceLoader\ResourceLoader;
11 use MediaWiki\User\Options\StaticUserOptionsLookup;
12 use MediaWiki\User\User;
13
14 /**
15 * @covers \Cite\Hooks\CiteHooks
16 * @license GPL-2.0-or-later
17 */
18 class CiteHooksTest extends \MediaWikiIntegrationTestCase {
19
20 /**
21 * @dataProvider provideBooleans
22 */
23 public function testOnResourceLoaderGetConfigVars( bool $enabled ) {
24 $vars = [];
25
26 $config = new HashConfig( [
27 'CiteVisualEditorOtherGroup' => $enabled,
28 'CiteResponsiveReferences' => $enabled,
29 'CiteBookReferencing' => $enabled,
30 ] );
31
32 ( new CiteHooks(
33 $this->getServiceContainer()->getService( 'Cite.ReferencePreviewsContext' ),
34 $this->getServiceContainer()->getService( 'Cite.GadgetsIntegration' ),
35 new StaticUserOptionsLookup( [] )
36 ) )
37 ->onResourceLoaderGetConfigVars( $vars, 'vector', $config );
38
39 $this->assertSame( [
40 'wgCiteVisualEditorOtherGroup' => $enabled,
41 'wgCiteResponsiveReferences' => $enabled,
42 'wgCiteSubReferencing' => $enabled,
43 ], $vars );
44 }
45
46 /**
47 * @dataProvider provideBooleans
48 */
49 public function testOnResourceLoaderRegisterModules( bool $enabled ) {
50 if ( ExtensionRegistry::getInstance()->isLoaded( 'WikiEditor' ) !== $enabled ) {
51 $this->markTestSkipped();
52 }
53
54 $rlModules = [];
55
56 $resourceLoader = $this->createMock( ResourceLoader::class );
57 $resourceLoader->method( 'getConfig' )
58 ->willReturn( new HashConfig( [ 'CiteReferencePreviews' => $enabled ] ) );
59 $resourceLoader->method( 'register' )
60 ->willReturnCallback( static function ( array $modules ) use ( &$rlModules ) {
61 $rlModules = array_merge( $rlModules, $modules );
62 } );
63
64 ( new CiteHooks(
65 $this->getServiceContainer()->getService( 'Cite.ReferencePreviewsContext' ),
66 $this->getServiceContainer()->getService( 'Cite.GadgetsIntegration' ),
67 new StaticUserOptionsLookup( [] )
68 ) )
69 ->onResourceLoaderRegisterModules( $resourceLoader );
70
71 if ( $enabled ) {
72 $this->assertArrayHasKey( 'ext.cite.wikiEditor', $rlModules );
73 } else {
74 $this->assertArrayNotHasKey( 'ext.cite.wikiEditor', $rlModules );
75 }
76 }
77
78 public function testOnResourceLoaderRegisterModulesLoadsVisualEditor() {
79 $rlModules = [];
80
81 $resourceLoader = $this->createMock( ResourceLoader::class );
82 $resourceLoader->method( 'getConfig' )
83 ->willReturn( new HashConfig( [ 'CiteReferencePreviews' => false ] ) );
84 $resourceLoader->method( 'register' )
85 ->willReturnCallback( static function ( array $modules ) use ( &$rlModules ) {
86 $rlModules = array_merge( $rlModules, $modules );
87 } );
88
89 ( new CiteHooks(
90 $this->getServiceContainer()->getService( 'Cite.ReferencePreviewsContext' ),
91 $this->getServiceContainer()->getService( 'Cite.GadgetsIntegration' ),
92 new StaticUserOptionsLookup( [] )
93 ) )
94 ->onResourceLoaderRegisterModules( $resourceLoader );
95
96 if ( ExtensionRegistry::getInstance()->isLoaded( 'VisualEditor' ) ) {
97 $this->assertArrayHasKey( 'ext.cite.visualEditor', $rlModules );
98 } else {
99 $this->assertArrayNotHasKey( 'ext.cite.visualEditor', $rlModules );
100 }
101 }
102
103 /**
104 * @dataProvider provideBooleans
105 */
106 public function testOnAPIQuerySiteInfoGeneralInfo( bool $enabled ) {
107 $api = $this->createMock( ApiQuerySiteinfo::class );
108 $api->expects( $this->once() )
109 ->method( 'getConfig' )
110 ->willReturn( new HashConfig( [ 'CiteResponsiveReferences' => $enabled ] ) );
111
112 $data = [];
113
114 ( new CiteHooks(
115 $this->getServiceContainer()->getService( 'Cite.ReferencePreviewsContext' ),
116 $this->getServiceContainer()->getService( 'Cite.GadgetsIntegration' ),
117 new StaticUserOptionsLookup( [] )
118 ) )
119 ->onAPIQuerySiteInfoGeneralInfo( $api, $data );
120
121 $this->assertSame( [ 'citeresponsivereferences' => $enabled ], $data );
122 }
123
124 public static function provideBooleans() {
125 yield [ true ];
126 yield [ false ];
127 }
128
129 public function testOnGetPreferences_noConflicts() {
130 $expected = [
131 'popups-reference-previews' => [
132 'type' => 'toggle',
133 'label-message' => 'popups-refpreview-user-preference-label',
134 'help-message' => 'popups-prefs-conflicting-gadgets-info',
135 'section' => 'rendering/reading'
136 ]
137 ];
138 $gadgetsIntegrationMock = $this->createMock( ReferencePreviewsGadgetsIntegration::class );
139 $prefs = [];
140 ( new CiteHooks(
141 $this->getServiceContainer()->getService( 'Cite.ReferencePreviewsContext' ),
142 $gadgetsIntegrationMock,
143 new StaticUserOptionsLookup( [] )
144 ) )
145 ->onGetPreferences( $this->createMock( User::class ), $prefs );
146 $this->assertEquals( $expected, $prefs );
147 }
148
149 public function testOnGetPreferences_conflictingGadget() {
150 $expected = [
151 'popups-reference-previews' => [
152 'type' => 'toggle',
153 'label-message' => 'popups-refpreview-user-preference-label',
154 // 'help-message' => 'popups-prefs-conflicting-gadgets-info',
155 'help-message' => [
156 'popups-prefs-navpopups-gadget-conflict-info',
157 'Special:Preferences#mw-prefsection-gadgets',
158 ],
159 'section' => 'rendering/reading',
160 'disabled' => true
161 ]
162 ];
163 $gadgetsIntegrationMock = $this->createMock( ReferencePreviewsGadgetsIntegration::class );
164 $gadgetsIntegrationMock->expects( $this->once() )
165 ->method( 'isNavPopupsGadgetEnabled' )
166 ->willReturn( true );
167 $prefs = [];
168 ( new CiteHooks(
169 $this->getServiceContainer()->getService( 'Cite.ReferencePreviewsContext' ),
170 $gadgetsIntegrationMock,
171 new StaticUserOptionsLookup( [] )
172 ) )
173 ->onGetPreferences( $this->createMock( User::class ), $prefs );
174 $this->assertEquals( $expected, $prefs );
175 }
176
177 public function testOnGetPreferences_redundantPreference() {
178 $prefs = [
179 'popups-reference-previews' => [
180 'type' => 'toggle',
181 'label-message' => 'from-another-extension',
182 ]
183 ];
184 $expected = $prefs;
185 ( new CiteHooks(
186 $this->getServiceContainer()->getService( 'Cite.ReferencePreviewsContext' ),
187 $this->getServiceContainer()->getService( 'Cite.GadgetsIntegration' ),
188 new StaticUserOptionsLookup( [] )
189 ) )
190 ->onGetPreferences( $this->createMock( User::class ), $prefs );
191 $this->assertEquals( $expected, $prefs );
192 }
193
194 }