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 ),
64 * @dataProvider actionProvider
65 * @param string $requestedAction
66 * @param string|null $expected
68 public function testActionExists( $requestedAction, $expected ) {
69 $exists = Action
::exists( $requestedAction );
71 $this->assertSame( $expected !== null, $exists );
74 public function testActionExists_doesNotRequireInstantiation() {
75 // The method is not supposed to check if the action can be instantiated.
76 $exists = Action
::exists( 'declared' );
78 $this->assertTrue( $exists );
82 * @dataProvider actionProvider
83 * @param string $requestedAction
84 * @param string|null $expected
86 public function testGetActionName( $requestedAction, $expected ) {
87 $context = $this->getContext( $requestedAction );
88 $actionName = Action
::getActionName( $context );
90 $this->assertEquals( $expected ?
: 'nosuchaction', $actionName );
93 public function testGetActionName_editredlinkWorkaround() {
94 // See https://bugzilla.wikimedia.org/show_bug.cgi?id=20966
95 $context = $this->getContext( 'editredlink' );
96 $actionName = Action
::getActionName( $context );
98 $this->assertEquals( 'edit', $actionName );
101 public function testGetActionName_historysubmitWorkaround() {
102 // See https://bugzilla.wikimedia.org/show_bug.cgi?id=20966
103 $context = $this->getContext( 'historysubmit' );
104 $actionName = Action
::getActionName( $context );
106 $this->assertEquals( 'view', $actionName );
109 public function testGetActionName_revisiondeleteWorkaround() {
110 // See https://bugzilla.wikimedia.org/show_bug.cgi?id=20966
111 $context = $this->getContext( 'historysubmit' );
112 $context->getRequest()->setVal( 'revisiondelete', true );
113 $actionName = Action
::getActionName( $context );
115 $this->assertEquals( 'revisiondelete', $actionName );
118 public function testGetActionName_whenCanNotUseWikiPage_defaultsToView() {
119 $request = new FauxRequest( array( 'action' => 'edit' ) );
120 $context = new DerivativeContext( RequestContext
::getMain() );
121 $context->setRequest( $request );
122 $actionName = Action
::getActionName( $context );
124 $this->assertEquals( 'view', $actionName );
128 * @dataProvider actionProvider
129 * @param string $requestedAction
130 * @param string|null $expected
132 public function testActionFactory( $requestedAction, $expected ) {
133 $context = $this->getContext();
134 $action = Action
::factory( $requestedAction, $context->getWikiPage(), $context );
136 $this->assertType( $expected ?
: 'null', $action );
139 public function emptyActionProvider() {
148 * @dataProvider emptyActionProvider
150 public function testEmptyAction_doesNotExist( $requestedAction ) {
151 $exists = Action
::exists( $requestedAction );
153 $this->assertFalse( $exists );
157 * @dataProvider emptyActionProvider
159 public function testEmptyAction_defaultsToView() {
160 $context = $this->getContext( null );
161 $actionName = Action
::getActionName( $context );
163 $this->assertEquals( 'view', $actionName );
167 * @dataProvider emptyActionProvider
169 public function testEmptyAction_canNotBeInstantiated() {
170 $page = $this->getPage();
171 $action = Action
::factory( null, $page );
173 $this->assertNull( $action );
176 public function testDisabledAction_exists() {
177 $exists = Action
::exists( 'disabled' );
179 $this->assertTrue( $exists );
182 public function testDisabledAction_isNotResolved() {
183 $context = $this->getContext( 'disabled' );
184 $actionName = Action
::getActionName( $context );
186 $this->assertEquals( 'nosuchaction', $actionName );
189 public function testDisabledAction_factoryReturnsFalse() {
190 $page = $this->getPage();
191 $action = Action
::factory( 'disabled', $page );
193 $this->assertFalse( $action );
196 public function dummyActionCallback() {
197 $context = $this->getContext();
198 return new CalledDummyAction( $context->getWikiPage(), $context );
203 class DummyAction
extends Action
{
205 public function getName() {
206 return get_called_class();
209 public function show() {
212 public function execute() {
216 class NamedDummyAction
extends DummyAction
{
219 class CalledDummyAction
extends DummyAction
{
222 class InstantiatedDummyAction
extends DummyAction
{