7 * @author Thiemo Mättig
12 class ActionTest
extends MediaWikiTestCase
{
14 protected function setUp() {
17 $context = $this->getContext();
18 $this->setMwGlobals( 'wgActions', array(
23 'revisiondelete' => true,
25 'string' => 'NamedDummyAction',
26 'declared' => 'NonExistingClassName',
27 'callable' => array( $this, 'dummyActionCallback' ),
28 'object' => new InstantiatedDummyAction( $context->getWikiPage(), $context ),
32 private function getPage() {
33 return WikiPage
::factory( Title
::makeTitle( 0, 'Title' ) );
36 private function getContext( $requestedAction = null ) {
37 $request = new FauxRequest( array( 'action' => $requestedAction ) );
39 $context = new DerivativeContext( RequestContext
::getMain() );
40 $context->setRequest( $request );
41 $context->setWikiPage( $this->getPage() );
46 public function actionProvider() {
48 array( 'dummy', 'DummyAction' ),
49 array( 'string', 'NamedDummyAction' ),
50 array( 'callable', 'CalledDummyAction' ),
51 array( 'object', 'InstantiatedDummyAction' ),
53 // Capitalization is ignored
54 array( 'DUMMY', 'DummyAction' ),
55 array( 'STRING', 'NamedDummyAction' ),
57 // Null and non-existing values
58 array( 'null', null ),
59 array( 'undeclared', null ),
66 * @dataProvider actionProvider
67 * @param string $requestedAction
68 * @param string|null $expected
70 public function testActionExists( $requestedAction, $expected ) {
71 $exists = Action
::exists( $requestedAction );
73 $this->assertSame( $expected !== null, $exists );
76 public function testActionExists_doesNotRequireInstantiation() {
77 // The method is not supposed to check if the action can be instantiated.
78 $exists = Action
::exists( 'declared' );
80 $this->assertTrue( $exists );
84 * @dataProvider actionProvider
85 * @param string $requestedAction
86 * @param string|null $expected
88 public function testGetActionName( $requestedAction, $expected ) {
89 $context = $this->getContext( $requestedAction );
90 $actionName = Action
::getActionName( $context );
92 $this->assertEquals( $expected ?
: 'nosuchaction', $actionName );
95 public function testGetActionName_editredlinkWorkaround() {
96 // See https://bugzilla.wikimedia.org/show_bug.cgi?id=20966
97 $context = $this->getContext( 'editredlink' );
98 $actionName = Action
::getActionName( $context );
100 $this->assertEquals( 'edit', $actionName );
103 public function testGetActionName_historysubmitWorkaround() {
104 // See https://bugzilla.wikimedia.org/show_bug.cgi?id=20966
105 $context = $this->getContext( 'historysubmit' );
106 $actionName = Action
::getActionName( $context );
108 $this->assertEquals( 'view', $actionName );
111 public function testGetActionName_revisiondeleteWorkaround() {
112 // See https://bugzilla.wikimedia.org/show_bug.cgi?id=20966
113 $context = $this->getContext( 'historysubmit' );
114 $context->getRequest()->setVal( 'revisiondelete', true );
115 $actionName = Action
::getActionName( $context );
117 $this->assertEquals( 'revisiondelete', $actionName );
121 * @dataProvider actionProvider
122 * @param string $requestedAction
123 * @param string|null $expected
125 public function testActionFactory( $requestedAction, $expected ) {
126 $context = $this->getContext();
127 $action = Action
::factory( $requestedAction, $context->getWikiPage(), $context );
129 $this->assertType( $expected ?
: 'null', $action );
132 public function testNull_doesNotExist() {
133 $exists = Action
::exists( null );
135 $this->assertFalse( $exists );
138 public function testNull_defaultsToView() {
139 $context = $this->getContext( null );
140 $actionName = Action
::getActionName( $context );
142 $this->assertEquals( 'view', $actionName );
145 public function testNull_canNotBeInstantiated() {
146 $page = $this->getPage();
147 $action = Action
::factory( null, $page );
149 $this->assertNull( $action );
152 public function testDisabledAction_exists() {
153 $exists = Action
::exists( 'disabled' );
155 $this->assertTrue( $exists );
158 public function testDisabledAction_isNotResolved() {
159 $context = $this->getContext( 'disabled' );
160 $actionName = Action
::getActionName( $context );
162 $this->assertEquals( 'nosuchaction', $actionName );
165 public function testDisabledAction_factoryReturnsFalse() {
166 $page = $this->getPage();
167 $action = Action
::factory( 'disabled', $page );
169 $this->assertFalse( $action );
172 public function dummyActionCallback() {
173 $context = $this->getContext();
174 return new CalledDummyAction( $context->getWikiPage(), $context );
179 class DummyAction
extends Action
{
181 public function getName() {
182 return get_called_class();
185 public function show() {
188 public function execute() {
192 class NamedDummyAction
extends DummyAction
{
195 class CalledDummyAction
extends DummyAction
{
198 class InstantiatedDummyAction
extends DummyAction
{