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 );
120 public function testGetActionName_whenCanNotUseWikiPage_defaultsToView() {
121 $request = new FauxRequest( array( 'action' => 'edit' ) );
122 $context = new DerivativeContext( RequestContext
::getMain() );
123 $context->setRequest( $request );
124 $actionName = Action
::getActionName( $context );
126 $this->assertEquals( 'view', $actionName );
130 * @dataProvider actionProvider
131 * @param string $requestedAction
132 * @param string|null $expected
134 public function testActionFactory( $requestedAction, $expected ) {
135 $context = $this->getContext();
136 $action = Action
::factory( $requestedAction, $context->getWikiPage(), $context );
138 $this->assertType( $expected ?
: 'null', $action );
141 public function testNull_doesNotExist() {
142 $exists = Action
::exists( null );
144 $this->assertFalse( $exists );
147 public function testNull_defaultsToView() {
148 $context = $this->getContext( null );
149 $actionName = Action
::getActionName( $context );
151 $this->assertEquals( 'view', $actionName );
154 public function testNull_canNotBeInstantiated() {
155 $page = $this->getPage();
156 $action = Action
::factory( null, $page );
158 $this->assertNull( $action );
161 public function testDisabledAction_exists() {
162 $exists = Action
::exists( 'disabled' );
164 $this->assertTrue( $exists );
167 public function testDisabledAction_isNotResolved() {
168 $context = $this->getContext( 'disabled' );
169 $actionName = Action
::getActionName( $context );
171 $this->assertEquals( 'nosuchaction', $actionName );
174 public function testDisabledAction_factoryReturnsFalse() {
175 $page = $this->getPage();
176 $action = Action
::factory( 'disabled', $page );
178 $this->assertFalse( $action );
181 public function dummyActionCallback() {
182 $context = $this->getContext();
183 return new CalledDummyAction( $context->getWikiPage(), $context );
188 class DummyAction
extends Action
{
190 public function getName() {
191 return get_called_class();
194 public function show() {
197 public function execute() {
201 class NamedDummyAction
extends DummyAction
{
204 class CalledDummyAction
extends DummyAction
{
207 class InstantiatedDummyAction
extends DummyAction
{