Add tests for disabled actions in ActionTest
[lhc/web/wiklou.git] / tests / phpunit / includes / actions / ActionTest.php
1 <?php
2
3 /**
4 * @covers Action
5 *
6 * @licence GNU GPL v2+
7 * @author Thiemo Mättig
8 *
9 * @group Action
10 */
11 class ActionTest extends MediaWikiTestCase {
12
13 protected function setUp() {
14 parent::setUp();
15
16 $context = $this->getContext();
17 $this->setMwGlobals( 'wgActions', array(
18 'null' => null,
19 'disabled' => false,
20 'dummy' => true,
21 'string' => 'NamedDummyAction',
22 'declared' => 'NonExistingClassName',
23 'callable' => array( $this, 'dummyActionCallback' ),
24 'object' => new InstantiatedDummyAction( $context->getWikiPage(), $context ),
25 ) );
26 }
27
28 private function getContext( $requestedAction = null ) {
29 $request = new FauxRequest( array( 'action' => $requestedAction ) );
30
31 $page = WikiPage::factory( Title::makeTitle( 0, 'Title' ) );
32
33 $context = new DerivativeContext( RequestContext::getMain() );
34 $context->setRequest( $request );
35 $context->setWikiPage( $page );
36
37 return $context;
38 }
39
40 public function actionProvider() {
41 return array(
42 array( 'dummy', 'DummyAction' ),
43 array( 'string', 'NamedDummyAction' ),
44 array( 'callable', 'CalledDummyAction' ),
45 array( 'object', 'InstantiatedDummyAction' ),
46
47 // Capitalization is ignored
48 array( 'DUMMY', 'DummyAction' ),
49 array( 'STRING', 'NamedDummyAction' ),
50
51 // Null and non-existing values
52 array( 'null', null ),
53 array( 'undeclared', null ),
54 array( '', null ),
55 array( null, null ),
56 array( false, null ),
57 );
58 }
59
60 /**
61 * @dataProvider actionProvider
62 * @param string $requestedAction
63 * @param string|null $expected
64 */
65 public function testActionExists( $requestedAction, $expected ) {
66 $exists = Action::exists( $requestedAction );
67
68 $this->assertSame( $expected !== null, $exists );
69 }
70
71 public function testActionExists_doesNotRequireInstantiation() {
72 // The method is not supposed to check if the action can be instantiated.
73 $exists = Action::exists( 'declared' );
74
75 $this->assertTrue( $exists );
76 }
77
78 /**
79 * @dataProvider actionProvider
80 * @param string $requestedAction
81 * @param string|null $expected
82 */
83 public function testGetActionName( $requestedAction, $expected ) {
84 $context = $this->getContext( $requestedAction );
85 $actionName = Action::getActionName( $context );
86
87 $this->assertEquals( $expected ?: 'nosuchaction', $actionName );
88 }
89
90 /**
91 * @dataProvider actionProvider
92 * @param string $requestedAction
93 * @param string|null $expected
94 */
95 public function testActionFactory( $requestedAction, $expected ) {
96 $context = $this->getContext();
97 $action = Action::factory( $requestedAction, $context->getWikiPage(), $context );
98
99 $this->assertType( $expected ?: 'null', $action );
100 }
101
102 public function testDisabledAction_exists() {
103 $exists = Action::exists( 'disabled' );
104
105 $this->assertTrue( $exists );
106 }
107
108 public function testDisabledAction_isNotResolved() {
109 $context = $this->getContext( 'disabled' );
110 $actionName = Action::getActionName( $context );
111
112 $this->assertEquals( 'nosuchaction', $actionName );
113 }
114
115 public function testDisabledAction_factoryReturnsFalse() {
116 $context = $this->getContext( 'disabled' );
117 $action = Action::factory( 'disabled', $context->getWikiPage(), $context );
118
119 $this->assertFalse( $action );
120 }
121
122 public function dummyActionCallback() {
123 $context = $this->getContext();
124 return new CalledDummyAction( $context->getWikiPage(), $context );
125 }
126
127 }
128
129 class DummyAction extends Action {
130
131 public function getName() {
132 return get_called_class();
133 }
134
135 public function show() {
136 }
137
138 public function execute() {
139 }
140 }
141
142 class NamedDummyAction extends DummyAction {
143 }
144
145 class CalledDummyAction extends DummyAction {
146 }
147
148 class InstantiatedDummyAction extends DummyAction {
149 }