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 );
119 * @dataProvider actionProvider
120 * @param string $requestedAction
121 * @param string|null $expected
123 public function testActionFactory( $requestedAction, $expected ) {
124 $context = $this->getContext();
125 $action = Action
::factory( $requestedAction, $context->getWikiPage(), $context );
127 $this->assertType( $expected ?
: 'null', $action );
130 public function emptyActionProvider() {
139 * @dataProvider emptyActionProvider
141 public function testEmptyAction_doesNotExist( $requestedAction ) {
142 $exists = Action
::exists( $requestedAction );
144 $this->assertFalse( $exists );
148 * @dataProvider emptyActionProvider
150 public function testEmptyAction_defaultsToView() {
151 $context = $this->getContext( null );
152 $actionName = Action
::getActionName( $context );
154 $this->assertEquals( 'view', $actionName );
158 * @dataProvider emptyActionProvider
160 public function testEmptyAction_canNotBeInstantiated() {
161 $page = $this->getPage();
162 $action = Action
::factory( null, $page );
164 $this->assertNull( $action );
167 public function testDisabledAction_exists() {
168 $exists = Action
::exists( 'disabled' );
170 $this->assertTrue( $exists );
173 public function testDisabledAction_isNotResolved() {
174 $context = $this->getContext( 'disabled' );
175 $actionName = Action
::getActionName( $context );
177 $this->assertEquals( 'nosuchaction', $actionName );
180 public function testDisabledAction_factoryReturnsFalse() {
181 $page = $this->getPage();
182 $action = Action
::factory( 'disabled', $page );
184 $this->assertFalse( $action );
187 public function dummyActionCallback() {
188 $context = $this->getContext();
189 return new CalledDummyAction( $context->getWikiPage(), $context );
194 class DummyAction
extends Action
{
196 public function getName() {
197 return get_called_class();
200 public function show() {
203 public function execute() {
207 class NamedDummyAction
extends DummyAction
{
210 class CalledDummyAction
extends DummyAction
{
213 class InstantiatedDummyAction
extends DummyAction
{