Debian Codegraph @

extensions/Cite/tests/phpunit/integration/ReferencePreviews/ReferencePreviewsGadgetsIntegrationTest.php

raw symbols SCIP
1 <?php
2
3 namespace Cite\Tests\Integration\ReferencePreviews;
4
5 use Cite\ReferencePreviews\ReferencePreviewsGadgetsIntegration;
6 use InvalidArgumentException;
7 use MediaWiki\Config\Config;
8 use MediaWiki\Config\HashConfig;
9 use MediaWiki\Extension\Gadgets\Gadget;
10 use MediaWiki\Extension\Gadgets\GadgetRepo;
11 use MediaWiki\User\UserIdentity;
12 use MediaWikiIntegrationTestCase;
13
14 /**
15 * @covers \Cite\ReferencePreviews\ReferencePreviewsGadgetsIntegration
16 * @license GPL-2.0-or-later
17 */
18 class ReferencePreviewsGadgetsIntegrationTest extends MediaWikiIntegrationTestCase {
19 /**
20 * Gadget name for testing
21 */
22 private const NAV_POPUPS_GADGET_NAME = 'navigation-test';
23
24 /**
25 * Helper constants for easier reading
26 */
27 private const GADGET_ENABLED = true;
28
29 /**
30 * Helper constants for easier reading
31 */
32 private const GADGET_DISABLED = false;
33
34 private function getConfig( ?string $gadgetName = self::NAV_POPUPS_GADGET_NAME ): Config {
35 return new HashConfig( [
36 ReferencePreviewsGadgetsIntegration::CONFIG_NAVIGATION_POPUPS_NAME => $gadgetName,
37 ReferencePreviewsGadgetsIntegration::CONFIG_REFERENCE_TOOLTIPS_NAME => $gadgetName,
38 ] );
39 }
40
41 public function testConflictsWithNavPopupsGadgetIfGadgetsExtensionIsNotLoaded() {
42 $this->assertFalse(
43 ( new ReferencePreviewsGadgetsIntegration(
44 $this->getConfig(),
45 null
46 ) )
47 ->isNavPopupsGadgetEnabled( $this->createNoOpMock( UserIdentity::class ) ),
48 'No conflict is identified.'
49 );
50 }
51
52 public function testConflictsWithNavPopupsGadgetIfGadgetNotExists() {
53 $this->markTestSkippedIfExtensionNotLoaded( 'Gadgets' );
54
55 $gadgetRepoMock = $this->createMock( GadgetRepo::class );
56 $gadgetRepoMock->expects( $this->once() )
57 ->method( 'getGadgetIds' )
58 ->willReturn( [] );
59
60 $this->executeIsNavPopupsGadgetEnabled(
61 $this->createNoOpMock( UserIdentity::class ),
62 $this->getConfig(),
63 $gadgetRepoMock,
64 self::GADGET_DISABLED
65 );
66 }
67
68 public function testConflictsWithNavPopupsGadgetIfGadgetExists() {
69 $this->markTestSkippedIfExtensionNotLoaded( 'Gadgets' );
70
71 $user = $this->createMock( UserIdentity::class );
72
73 $gadgetMock = $this->createMock( Gadget::class );
74 $gadgetMock->expects( $this->once() )
75 ->method( 'isEnabled' )
76 ->with( $user )
77 ->willReturn( self::GADGET_ENABLED );
78
79 $gadgetRepoMock = $this->createMock( GadgetRepo::class );
80 $gadgetRepoMock->expects( $this->once() )
81 ->method( 'getGadgetIds' )
82 ->willReturn( [ self::NAV_POPUPS_GADGET_NAME ] );
83 $gadgetRepoMock->expects( $this->once() )
84 ->method( 'getGadget' )
85 ->with( self::NAV_POPUPS_GADGET_NAME )
86 ->willReturn( $gadgetMock );
87
88 $this->executeIsNavPopupsGadgetEnabled(
89 $user,
90 $this->getConfig(),
91 $gadgetRepoMock,
92 self::GADGET_ENABLED
93 );
94 }
95
96 /**
97 * Test the edge case when GadgetsRepo::getGadget throws an exception
98 */
99 public function testConflictsWithNavPopupsGadgetWhenGadgetNotExists() {
100 $this->markTestSkippedIfExtensionNotLoaded( 'Gadgets' );
101
102 $gadgetRepoMock = $this->createMock( GadgetRepo::class );
103 $gadgetRepoMock->expects( $this->once() )
104 ->method( 'getGadgetIds' )
105 ->willReturn( [ self::NAV_POPUPS_GADGET_NAME ] );
106 $gadgetRepoMock->expects( $this->once() )
107 ->method( 'getGadget' )
108 ->with( self::NAV_POPUPS_GADGET_NAME )
109 ->willThrowException( new InvalidArgumentException() );
110
111 $this->executeIsNavPopupsGadgetEnabled(
112 $this->createNoOpMock( UserIdentity::class ),
113 $this->getConfig(),
114 $gadgetRepoMock,
115 self::GADGET_DISABLED
116 );
117 }
118
119 /**
120 * @dataProvider provideGadgetNamesWithSanitizedVersion
121 */
122 public function testConflictsWithNavPopupsGadgetNameSanitization( string $gadgetName, string $sanitized ) {
123 $this->markTestSkippedIfExtensionNotLoaded( 'Gadgets' );
124
125 $gadgetMock = $this->createMock( Gadget::class );
126 $gadgetMock->expects( $this->once() )
127 ->method( 'isEnabled' )
128 ->willReturn( self::GADGET_ENABLED );
129
130 $gadgetRepoMock = $this->createMock( GadgetRepo::class );
131 $gadgetRepoMock->expects( $this->once() )
132 ->method( 'getGadgetIds' )
133 ->willReturn( [ $sanitized ] );
134 $gadgetRepoMock->expects( $this->once() )
135 ->method( 'getGadget' )
136 ->with( $sanitized )
137 ->willReturn( $gadgetMock );
138
139 $this->executeIsNavPopupsGadgetEnabled(
140 $this->createNoOpMock( UserIdentity::class ),
141 $this->getConfig( $gadgetName ),
142 $gadgetRepoMock,
143 self::GADGET_ENABLED
144 );
145 }
146
147 public static function provideGadgetNamesWithSanitizedVersion() {
148 yield [ ' Popups ', 'Popups' ];
149 yield [ 'Navigation_popups-API', 'Navigation_popups-API' ];
150 yield [ 'Navigation popups ', 'Navigation_popups' ];
151 }
152
153 private function executeIsNavPopupsGadgetEnabled(
154 UserIdentity $user,
155 Config $config,
156 GadgetRepo $repoMock,
157 bool $expected
158 ): void {
159 $this->assertSame(
160 $expected,
161 ( new ReferencePreviewsGadgetsIntegration(
162 $config,
163 $repoMock
164 ) )
165 ->isNavPopupsGadgetEnabled( $user ),
166 ( $expected ? 'A' : 'No' ) . ' conflict is identified.'
167 );
168 }
169 }