7 * @author Thiemo Mättig
11 class ActionTest
extends MediaWikiTestCase
{
13 protected function setUp() {
16 $context = $this->getContext();
17 $this->setMwGlobals( 'wgActions', array(
22 'revisiondelete' => true,
24 'string' => 'NamedDummyAction',
25 'declared' => 'NonExistingClassName',
26 'callable' => array( $this, 'dummyActionCallback' ),
27 'object' => new InstantiatedDummyAction( $context->getWikiPage(), $context ),
31 private function getPage() {
32 return WikiPage
::factory( Title
::makeTitle( 0, 'Title' ) );
35 private function getContext( $requestedAction = null ) {
36 $request = new FauxRequest( array( 'action' => $requestedAction ) );
38 $context = new DerivativeContext( RequestContext
::getMain() );
39 $context->setRequest( $request );
40 $context->setWikiPage( $this->getPage() );
45 public function actionProvider() {
47 array( 'dummy', 'DummyAction' ),
48 array( 'string', 'NamedDummyAction' ),
49 array( 'callable', 'CalledDummyAction' ),
50 array( 'object', 'InstantiatedDummyAction' ),
52 // Capitalization is ignored
53 array( 'DUMMY', 'DummyAction' ),
54 array( 'STRING', 'NamedDummyAction' ),
56 // Null and non-existing values
57 array( 'null', null ),
58 array( 'undeclared', null ),
65 * @dataProvider actionProvider
66 * @param string $requestedAction
67 * @param string|null $expected
69 public function testActionExists( $requestedAction, $expected ) {
70 $exists = Action
::exists( $requestedAction );
72 $this->assertSame( $expected !== null, $exists );
75 public function testActionExists_doesNotRequireInstantiation() {
76 // The method is not supposed to check if the action can be instantiated.
77 $exists = Action
::exists( 'declared' );
79 $this->assertTrue( $exists );
83 * @dataProvider actionProvider
84 * @param string $requestedAction
85 * @param string|null $expected
87 public function testGetActionName( $requestedAction, $expected ) {
88 $context = $this->getContext( $requestedAction );
89 $actionName = Action
::getActionName( $context );
91 $this->assertEquals( $expected ?
: 'nosuchaction', $actionName );
94 public function testGetActionName_editredlinkWorkaround() {
95 // See https://bugzilla.wikimedia.org/show_bug.cgi?id=20966
96 $context = $this->getContext( 'editredlink' );
97 $actionName = Action
::getActionName( $context );
99 $this->assertEquals( 'edit', $actionName );
102 public function testGetActionName_historysubmitWorkaround() {
103 // See https://bugzilla.wikimedia.org/show_bug.cgi?id=20966
104 $context = $this->getContext( 'historysubmit' );
105 $actionName = Action
::getActionName( $context );
107 $this->assertEquals( 'view', $actionName );
110 public function testGetActionName_revisiondeleteWorkaround() {
111 // See https://bugzilla.wikimedia.org/show_bug.cgi?id=20966
112 $context = $this->getContext( 'historysubmit' );
113 $context->getRequest()->setVal( 'revisiondelete', true );
114 $actionName = Action
::getActionName( $context );
116 $this->assertEquals( 'revisiondelete', $actionName );
120 * @dataProvider actionProvider
121 * @param string $requestedAction
122 * @param string|null $expected
124 public function testActionFactory( $requestedAction, $expected ) {
125 $context = $this->getContext();
126 $action = Action
::factory( $requestedAction, $context->getWikiPage(), $context );
128 $this->assertType( $expected ?
: 'null', $action );
131 public function testNull_doesNotExist() {
132 $exists = Action
::exists( null );
134 $this->assertFalse( $exists );
137 public function testNull_defaultsToView() {
138 $context = $this->getContext( null );
139 $actionName = Action
::getActionName( $context );
141 $this->assertEquals( 'view', $actionName );
144 public function testNull_canNotBeInstantiated() {
145 $page = $this->getPage();
146 $action = Action
::factory( null, $page );
148 $this->assertNull( $action );
151 public function testDisabledAction_exists() {
152 $exists = Action
::exists( 'disabled' );
154 $this->assertTrue( $exists );
157 public function testDisabledAction_isNotResolved() {
158 $context = $this->getContext( 'disabled' );
159 $actionName = Action
::getActionName( $context );
161 $this->assertEquals( 'nosuchaction', $actionName );
164 public function testDisabledAction_factoryReturnsFalse() {
165 $page = $this->getPage();
166 $action = Action
::factory( 'disabled', $page );
168 $this->assertFalse( $action );
171 public function dummyActionCallback() {
172 $context = $this->getContext();
173 return new CalledDummyAction( $context->getWikiPage(), $context );
178 class DummyAction
extends Action
{
180 public function getName() {
181 return get_called_class();
184 public function show() {
187 public function execute() {
191 class NamedDummyAction
extends DummyAction
{
194 class CalledDummyAction
extends DummyAction
{
197 class InstantiatedDummyAction
extends DummyAction
{