1297af3326108db27fcbf2f4b8b59f0e3d6489d8
[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 'dummy' => true,
20 'string' => 'NamedDummyAction',
21 'declared' => 'NonExistingClassName',
22 'callable' => array( $this, 'dummyActionCallback' ),
23 'object' => new InstantiatedDummyAction( $context->getWikiPage(), $context ),
24 ) );
25 }
26
27 private function getContext( $requestedAction = null ) {
28 $request = new FauxRequest( array( 'action' => $requestedAction ) );
29
30 $page = WikiPage::factory( Title::makeTitle( 0, 'Title' ) );
31
32 $context = new DerivativeContext( RequestContext::getMain() );
33 $context->setRequest( $request );
34 $context->setWikiPage( $page );
35
36 return $context;
37 }
38
39 public function actionProvider() {
40 return array(
41 array( 'dummy', 'DummyAction' ),
42 array( 'string', 'NamedDummyAction' ),
43 array( 'callable', 'CalledDummyAction' ),
44 array( 'object', 'InstantiatedDummyAction' ),
45
46 // Capitalization is ignored
47 array( 'DUMMY', 'DummyAction' ),
48 array( 'STRING', 'NamedDummyAction' ),
49
50 // Null and non-existing values
51 array( 'null', null ),
52 array( 'undeclared', null ),
53 array( '', null ),
54 array( null, null ),
55 array( false, null ),
56 );
57 }
58
59 /**
60 * @dataProvider actionProvider
61 * @param string $requestedAction
62 * @param string|null $expected
63 */
64 public function testActionExists( $requestedAction, $expected ) {
65 $exists = Action::exists( $requestedAction );
66
67 $this->assertSame( $expected !== null, $exists );
68 }
69
70 public function testActionExists_doesNotRequireInstantiation() {
71 // The method is not supposed to check if the action can be instantiated.
72 $exists = Action::exists( 'declared' );
73
74 $this->assertTrue( $exists );
75 }
76
77 /**
78 * @dataProvider actionProvider
79 * @param string $requestedAction
80 * @param string|null $expected
81 */
82 public function testGetActionName( $requestedAction, $expected ) {
83 $context = $this->getContext( $requestedAction );
84 $actionName = Action::getActionName( $context );
85
86 $this->assertEquals( $expected ?: 'nosuchaction', $actionName );
87 }
88
89 /**
90 * @dataProvider actionProvider
91 * @param string $requestedAction
92 * @param string|null $expected
93 */
94 public function testActionFactory( $requestedAction, $expected ) {
95 $context = $this->getContext();
96 $action = Action::factory( $requestedAction, $context->getWikiPage(), $context );
97
98 $this->assertType( $expected ?: 'null', $action );
99 }
100
101 public function dummyActionCallback() {
102 $context = $this->getContext();
103 return new CalledDummyAction( $context->getWikiPage(), $context );
104 }
105
106 }
107
108 class DummyAction extends Action {
109
110 public function getName() {
111 return get_called_class();
112 }
113
114 public function show() {
115 }
116
117 public function execute() {
118 }
119 }
120
121 class NamedDummyAction extends DummyAction {
122 }
123
124 class CalledDummyAction extends DummyAction {
125 }
126
127 class InstantiatedDummyAction extends DummyAction {
128 }