| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Actions\ActionFactory; |
| 4 |
use MediaWiki\Context\IContextSource; |
| 5 |
use MediaWiki\Request\FauxRequest; |
| 6 |
use MediaWiki\Tests\Unit\DummyServicesTrait; |
| 7 |
use MediaWiki\Title\Title; |
| 8 |
use PHPUnit\Framework\MockObject\MockObject; |
| 9 |
use Psr\Log\LogLevel; |
| 10 |
use Psr\Log\NullLogger; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class ActionFactoryTest extends MediaWikiUnitTestCase { |
| 18 |
use DummyServicesTrait; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
private function getFactory( $overrides = [], $hooks = [] ) { |
| 26 |
$mock = $this->getMockBuilder( ActionFactory::class ) |
| 27 |
->setConstructorArgs( [ |
| 28 |
$overrides['actions'] ?? [], |
| 29 |
$overrides['logger'] ?? new NullLogger(), |
| 30 |
$this->getDummyObjectFactory(), |
| 31 |
$this->createHookContainer( $hooks ) |
| 32 |
] ) |
| 33 |
->onlyMethods( [ 'getArticle' ] ) |
| 34 |
->getMock(); |
| 35 |
|
| 36 |
|
| 37 |
$mock->method( 'getArticle' ) |
| 38 |
->willReturn( |
| 39 |
$overrides['article'] ?? $this->getArticle() |
| 40 |
); |
| 41 |
return $mock; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
private function getArticle( $overrides = [] ) { |
| 49 |
$article = $this->createMock( Article::class ); |
| 50 |
$article->method( 'getActionOverrides' ) |
| 51 |
->willReturn( $overrides ); |
| 52 |
return $article; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public function testGetActionInfo() { |
| 59 |
$article = $this->getArticle(); |
| 60 |
$theAction = $this->createMock( Action::class ); |
| 61 |
$theAction->method( 'getName' )->willReturn( 'test' ); |
| 62 |
$theAction->method( 'getRestriction' )->willReturn( 'testing' ); |
| 63 |
$theAction->method( 'needsReadRights' )->willReturn( true ); |
| 64 |
$theAction->method( 'requiresWrite' )->willReturn( true ); |
| 65 |
$theAction->method( 'requiresUnblock' )->willReturn( true ); |
| 66 |
|
| 67 |
$factory = $this->getFactory( [ |
| 68 |
'actions' => [ |
| 69 |
'view' => $theAction, |
| 70 |
'disabled' => false, |
| 71 |
] |
| 72 |
] ); |
| 73 |
|
| 74 |
$info = $factory->getActionInfo( 'view', $article ); |
| 75 |
$this->assertIsObject( $info ); |
| 76 |
|
| 77 |
$this->assertSame( 'test', $info->getName() ); |
| 78 |
$this->assertSame( 'testing', $info->getRestriction() ); |
| 79 |
$this->assertTrue( $info->needsReadRights() ); |
| 80 |
$this->assertTrue( $info->requiresWrite() ); |
| 81 |
$this->assertTrue( $info->requiresUnblock() ); |
| 82 |
|
| 83 |
$this->assertNull( |
| 84 |
$factory->getActionInfo( 'missing', $article ), |
| 85 |
'No ActionInfo should be returned for an unknown action' |
| 86 |
); |
| 87 |
$this->assertNull( |
| 88 |
$factory->getActionInfo( 'disabled', $article ), |
| 89 |
'No ActionInfo should be returned for a disabled action' |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public function testGetAction_simple() { |
| 97 |
$context = $this->createMock( IContextSource::class ); |
| 98 |
$article = $this->getArticle(); |
| 99 |
$theAction = $this->createMock( Action::class ); |
| 100 |
|
| 101 |
$factory = $this->getFactory( [ |
| 102 |
'actions' => [ |
| 103 |
'known' => $theAction, |
| 104 |
'disabled' => false, |
| 105 |
] |
| 106 |
] ); |
| 107 |
$this->assertSame( |
| 108 |
$theAction, |
| 109 |
$factory->getAction( 'known', $article, $context ), |
| 110 |
'The `known` action is known' |
| 111 |
); |
| 112 |
$this->assertNull( |
| 113 |
$factory->getAction( 'missing', $article, $context ), |
| 114 |
'The `missing` action is not defined' |
| 115 |
); |
| 116 |
$this->assertFalse( |
| 117 |
$factory->getAction( 'disabled', $article, $context ), |
| 118 |
'The `disabled` action is disabled' |
| 119 |
); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public function testGetAction_override() { |
| 126 |
$context = $this->createMock( IContextSource::class ); |
| 127 |
$factory = $this->getFactory( [ |
| 128 |
'actions' => [ |
| 129 |
'the-override' => [ |
| 130 |
'class' => Action::class, |
| 131 |
], |
| 132 |
] |
| 133 |
] ); |
| 134 |
|
| 135 |
$theOverrideAction = $this->createMock( Action::class ); |
| 136 |
$article = $this->getArticle( [ |
| 137 |
'the-override' => $theOverrideAction, |
| 138 |
] ); |
| 139 |
$this->assertSame( |
| 140 |
$theOverrideAction, |
| 141 |
$factory->getAction( 'the-override', $article, $context ), |
| 142 |
'Article::getActionOverrides can override configured actions' |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public function testActionForSpecialPage() { |
| 151 |
$context = $this->createMock( IContextSource::class ); |
| 152 |
$factory = $this->getFactory(); |
| 153 |
|
| 154 |
$article = Title::makeTitle( NS_SPECIAL, 'Blankpage' ); |
| 155 |
|
| 156 |
$this->assertNull( |
| 157 |
$factory->getActionInfo( 'edit', $article ), |
| 158 |
'Special pages do not support actions' |
| 159 |
); |
| 160 |
$this->assertNull( |
| 161 |
$factory->getAction( 'edit', $article, $context ), |
| 162 |
'Special pages do not support actions' |
| 163 |
); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public function testGetAction_overrideNonexistent() { |
| 170 |
$context = $this->createMock( IContextSource::class ); |
| 171 |
$factory = $this->getFactory( [] ); |
| 172 |
$theOverrideAction = $this->createMock( Action::class ); |
| 173 |
$article = $this->getArticle( [ |
| 174 |
'the-override' => $theOverrideAction, |
| 175 |
] ); |
| 176 |
$this->assertSame( |
| 177 |
$theOverrideAction, |
| 178 |
$factory->getAction( 'the-override', $article, $context ), |
| 179 |
'Article::getActionOverrides can override non-existent actions' |
| 180 |
); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public function testGetAction_missingClass() { |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
$logger = new TestLogger( |
| 191 |
true, |
| 192 |
static function ( $message, $level, $context ) { |
| 193 |
|
| 194 |
|
| 195 |
return $level === LogLevel::INFO ? $message : null; |
| 196 |
}, |
| 197 |
true |
| 198 |
); |
| 199 |
$factory = $this->getFactory( [ |
| 200 |
'actions' => [ |
| 201 |
'actionnamewithnoclass' => true, |
| 202 |
'anothermissingaction' => 'MissingClassName', |
| 203 |
], |
| 204 |
'logger' => $logger, |
| 205 |
] ); |
| 206 |
$context = $this->createMock( IContextSource::class ); |
| 207 |
$article = $this->getArticle(); |
| 208 |
$this->assertFalse( |
| 209 |
$factory->getAction( 'actionnamewithnoclass', $article, $context ) |
| 210 |
); |
| 211 |
$this->assertFalse( |
| 212 |
$factory->getAction( 'anothermissingaction', $article, $context ) |
| 213 |
); |
| 214 |
$this->assertSame( [ |
| 215 |
[ |
| 216 |
LogLevel::INFO, |
| 217 |
'Missing action class {actionClass}, treating as disabled', |
| 218 |
[ 'actionClass' => 'ActionnamewithnoclassAction' ] |
| 219 |
], |
| 220 |
[ |
| 221 |
LogLevel::INFO, |
| 222 |
'Missing action class {actionClass}, treating as disabled', |
| 223 |
[ 'actionClass' => 'MissingClassName' ] |
| 224 |
], |
| 225 |
], $logger->getBuffer() ); |
| 226 |
$logger->clearBuffer(); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
public function testGetAction_spec() { |
| 233 |
$context = $this->createMock( IContextSource::class ); |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
$factory = $this->getFactory( [ |
| 241 |
'actions' => [ |
| 242 |
'edit' => true, |
| 243 |
'info' => function ( Article $article, IContextSource $context ) { |
| 244 |
return $this->createMock( InfoAction::class ); |
| 245 |
}, |
| 246 |
] |
| 247 |
] ); |
| 248 |
$article = $this->getArticle(); |
| 249 |
$editAction = $factory->getAction( 'edit', $article, $context ); |
| 250 |
$this->assertInstanceOf( |
| 251 |
EditAction::class, |
| 252 |
$editAction, |
| 253 |
'Setting an action name to `true` and getting the class from the name' |
| 254 |
); |
| 255 |
$infoAction = $factory->getAction( 'info', $article, $context ); |
| 256 |
$this->assertInstanceOf( |
| 257 |
InfoAction::class, |
| 258 |
$infoAction, |
| 259 |
'Callable used as a factory' |
| 260 |
); |
| 261 |
} |
| 262 |
|
| 263 |
public static function provideGetActionName() { |
| 264 |
yield 'Disabled action' => [ |
| 265 |
'disabled', |
| 266 |
'nosuchaction', |
| 267 |
]; |
| 268 |
yield 'historysubmit falls back to view' => [ |
| 269 |
'historysubmit', |
| 270 |
'view', |
| 271 |
]; |
| 272 |
yield 'editredlink maps to edit' => [ |
| 273 |
'editredlink', |
| 274 |
'edit', |
| 275 |
]; |
| 276 |
yield 'unrecognized action' => [ |
| 277 |
'missing', |
| 278 |
'nosuchaction', |
| 279 |
]; |
| 280 |
yield 'hook overriding action' => [ |
| 281 |
'edit', |
| 282 |
'view', |
| 283 |
[ |
| 284 |
'GetActionName' => static function ( $context, &$action ) { |
| 285 |
$action = 'view'; |
| 286 |
return true; |
| 287 |
} |
| 288 |
] |
| 289 |
]; |
| 290 |
yield 'hook overriding to an unrecognized action' => [ |
| 291 |
'edit', |
| 292 |
'nosuchaction', |
| 293 |
[ |
| 294 |
'GetActionName' => static function ( $context, &$action ) { |
| 295 |
$action = 'missing'; |
| 296 |
return true; |
| 297 |
} |
| 298 |
] |
| 299 |
]; |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
public function testGetActionName( |
| 310 |
string $requestAction, |
| 311 |
string $expectedActionName, |
| 312 |
array $hooks = [] |
| 313 |
) { |
| 314 |
$context = $this->createMock( IContextSource::class ); |
| 315 |
$context->method( 'canUseWikiPage' )->willReturn( true ); |
| 316 |
|
| 317 |
$request = new FauxRequest( [ |
| 318 |
'action' => $requestAction, |
| 319 |
] ); |
| 320 |
$context->method( 'getRequest' )->willReturn( $request ); |
| 321 |
|
| 322 |
$factory = $this->getFactory( [ |
| 323 |
'actions' => [ |
| 324 |
'disabled' => false, |
| 325 |
] |
| 326 |
], $hooks ); |
| 327 |
$actionName = $factory->getActionName( $context ); |
| 328 |
$this->assertSame( $expectedActionName, $actionName ); |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
public function testGetActionName_noWikiPage() { |
| 335 |
$context = $this->createMock( IContextSource::class ); |
| 336 |
$context->method( 'canUseWikiPage' )->willReturn( false ); |
| 337 |
|
| 338 |
$factory = $this->getFactory(); |
| 339 |
$this->assertSame( |
| 340 |
'view', |
| 341 |
$factory->getActionName( $context ), |
| 342 |
'For contexts where a wiki page cannot be used, the action is always `view`' |
| 343 |
); |
| 344 |
} |
| 345 |
|
| 346 |
} |