123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- /**
- * @covers Action
- *
- * @group Action
- * @group Database
- *
- * @license GPL-2.0-or-later
- * @author Thiemo Kreuz
- */
- class ActionTest extends MediaWikiTestCase {
- protected function setUp() {
- parent::setUp();
- $context = $this->getContext();
- $this->setMwGlobals( 'wgActions', [
- 'null' => null,
- 'disabled' => false,
- 'view' => true,
- 'edit' => true,
- 'revisiondelete' => SpecialPageAction::class,
- 'dummy' => true,
- 'string' => 'NamedDummyAction',
- 'declared' => 'NonExistingClassName',
- 'callable' => [ $this, 'dummyActionCallback' ],
- 'object' => new InstantiatedDummyAction( $context->getWikiPage(), $context ),
- ] );
- }
- private function getPage() {
- return WikiPage::factory( Title::makeTitle( 0, 'Title' ) );
- }
- private function getContext( $requestedAction = null ) {
- $request = new FauxRequest( [ 'action' => $requestedAction ] );
- $context = new DerivativeContext( RequestContext::getMain() );
- $context->setRequest( $request );
- $context->setWikiPage( $this->getPage() );
- return $context;
- }
- public function actionProvider() {
- return [
- [ 'dummy', 'DummyAction' ],
- [ 'string', 'NamedDummyAction' ],
- [ 'callable', 'CalledDummyAction' ],
- [ 'object', 'InstantiatedDummyAction' ],
- // Capitalization is ignored
- [ 'DUMMY', 'DummyAction' ],
- [ 'STRING', 'NamedDummyAction' ],
- // Null and non-existing values
- [ 'null', null ],
- [ 'undeclared', null ],
- [ '', null ],
- [ false, null ],
- ];
- }
- /**
- * @dataProvider actionProvider
- * @param string $requestedAction
- * @param string|null $expected
- */
- public function testActionExists( $requestedAction, $expected ) {
- $exists = Action::exists( $requestedAction );
- $this->assertSame( $expected !== null, $exists );
- }
- public function testActionExists_doesNotRequireInstantiation() {
- // The method is not supposed to check if the action can be instantiated.
- $exists = Action::exists( 'declared' );
- $this->assertTrue( $exists );
- }
- /**
- * @dataProvider actionProvider
- * @param string $requestedAction
- * @param string|null $expected
- */
- public function testGetActionName( $requestedAction, $expected ) {
- $context = $this->getContext( $requestedAction );
- $actionName = Action::getActionName( $context );
- $this->assertEquals( $expected ?: 'nosuchaction', $actionName );
- }
- public function testGetActionName_editredlinkWorkaround() {
- // See https://phabricator.wikimedia.org/T22966
- $context = $this->getContext( 'editredlink' );
- $actionName = Action::getActionName( $context );
- $this->assertEquals( 'edit', $actionName );
- }
- public function testGetActionName_historysubmitWorkaround() {
- // See https://phabricator.wikimedia.org/T22966
- $context = $this->getContext( 'historysubmit' );
- $actionName = Action::getActionName( $context );
- $this->assertEquals( 'view', $actionName );
- }
- public function testGetActionName_revisiondeleteWorkaround() {
- // See https://phabricator.wikimedia.org/T22966
- $context = $this->getContext( 'historysubmit' );
- $context->getRequest()->setVal( 'revisiondelete', true );
- $actionName = Action::getActionName( $context );
- $this->assertEquals( 'revisiondelete', $actionName );
- }
- public function testGetActionName_whenCanNotUseWikiPage_defaultsToView() {
- $request = new FauxRequest( [ 'action' => 'edit' ] );
- $context = new DerivativeContext( RequestContext::getMain() );
- $context->setRequest( $request );
- $actionName = Action::getActionName( $context );
- $this->assertEquals( 'view', $actionName );
- }
- /**
- * @dataProvider actionProvider
- * @param string $requestedAction
- * @param string|null $expected
- */
- public function testActionFactory( $requestedAction, $expected ) {
- $context = $this->getContext();
- $action = Action::factory( $requestedAction, $context->getWikiPage(), $context );
- $this->assertType( $expected ?: 'null', $action );
- }
- public function testNull_doesNotExist() {
- $exists = Action::exists( null );
- $this->assertFalse( $exists );
- }
- public function testNull_defaultsToView() {
- $context = $this->getContext( null );
- $actionName = Action::getActionName( $context );
- $this->assertEquals( 'view', $actionName );
- }
- public function testNull_canNotBeInstantiated() {
- $page = $this->getPage();
- $action = Action::factory( null, $page );
- $this->assertNull( $action );
- }
- public function testDisabledAction_exists() {
- $exists = Action::exists( 'disabled' );
- $this->assertTrue( $exists );
- }
- public function testDisabledAction_isNotResolved() {
- $context = $this->getContext( 'disabled' );
- $actionName = Action::getActionName( $context );
- $this->assertEquals( 'nosuchaction', $actionName );
- }
- public function testDisabledAction_factoryReturnsFalse() {
- $page = $this->getPage();
- $action = Action::factory( 'disabled', $page );
- $this->assertFalse( $action );
- }
- public function dummyActionCallback() {
- $context = $this->getContext();
- return new CalledDummyAction( $context->getWikiPage(), $context );
- }
- }
- class DummyAction extends Action {
- public function getName() {
- return static::class;
- }
- public function show() {
- }
- public function execute() {
- }
- }
- class NamedDummyAction extends DummyAction {
- }
- class CalledDummyAction extends DummyAction {
- }
- class InstantiatedDummyAction extends DummyAction {
- }
|