Add ActionTest for static Action methods
authorThiemo Mättig <thiemo.maettig@wikimedia.de>
Fri, 27 Jun 2014 13:39:23 +0000 (15:39 +0200)
committerThiemo Mättig <thiemo.maettig@wikimedia.de>
Fri, 18 Jul 2014 13:29:35 +0000 (15:29 +0200)
Change-Id: Ifea52280486b8aa29fbab42e4c7d3ea907849b52

tests/phpunit/includes/actions/ActionTest.php [new file with mode: 0644]

diff --git a/tests/phpunit/includes/actions/ActionTest.php b/tests/phpunit/includes/actions/ActionTest.php
new file mode 100644 (file)
index 0000000..1ebd908
--- /dev/null
@@ -0,0 +1,110 @@
+<?php
+
+/**
+ * @covers Action
+ *
+ * @licence GNU GPL v2+
+ * @author Thiemo Mättig
+ *
+ * @group Action
+ */
+class ActionTest extends MediaWikiTestCase {
+
+       protected function setUp() {
+               parent::setUp();
+
+               $this->setMwGlobals( 'wgActions', array(
+                       'null'     => null,
+                       'dummy'    => true,
+                       'string'   => 'NamedDummyAction',
+                       'callable' => array( $this, 'dummyActionCallback' ),
+                       'object'   => new InstantiatedDummyAction( $this->getPage(), $this->getContext() ),
+               ) );
+       }
+
+       private function getPage() {
+               return WikiPage::factory( Title::makeTitle( 0, 'Title' ) );
+       }
+
+       private function getContext() {
+               return new DerivativeContext( RequestContext::getMain() );
+       }
+
+       public function actionProvider() {
+               return array(
+                       array( 'dummy',    'DummyAction' ),
+                       array( 'string',   'NamedDummyAction' ),
+                       array( 'callable', 'CalledDummyAction' ),
+                       array( 'object',   'InstantiatedDummyAction' ),
+
+                       // Capitalization is ignored
+                       array( 'STRING',   'NamedDummyAction' ),
+
+                       // Null and non-existing values
+                       array( 'null',       null ),
+                       array( 'undeclared', null ),
+                       array( '',           null ),
+                       array( null,         null ),
+               );
+       }
+
+       /**
+        * @dataProvider actionProvider
+        * @param string $requestedAction
+        * @param string|null $expected
+        */
+       public function testActionExists( $requestedAction, $expected ) {
+               $exists = Action::exists( $requestedAction );
+
+               $this->assertEquals( isset( $expected ), $exists );
+       }
+
+       /**
+        * @dataProvider actionProvider
+        * @param string $requestedAction
+        * @param string|null $expected
+        */
+       public function testGetActionName( $requestedAction, $expected ) {
+               $context = $this->getContext();
+               $context->setWikiPage( $this->getPage() );
+               $context->setRequest( new FauxRequest( array( 'action' => $requestedAction ) ) );
+
+               $actionName = Action::getActionName( $context );
+
+               $this->assertEquals( isset( $expected ) ? $expected : 'nosuchaction', $actionName );
+       }
+
+       /**
+        * @dataProvider actionProvider
+        * @param string $requestedAction
+        * @param string|null $expected
+        */
+       public function testActionFactory( $requestedAction, $expected ) {
+               $action = Action::factory( $requestedAction, $this->getPage(), $this->getContext() );
+
+               $this->assertType( isset( $expected ) ? $expected : 'null', $action );
+       }
+
+       public function dummyActionCallback() {
+               return new CalledDummyAction( $this->getPage(), $this->getContext() );
+       }
+
+}
+
+class DummyAction extends Action {
+
+       public function getName() {
+               return get_called_class();
+       }
+
+       public function show() { }
+
+       public function execute() { }
+
+}
+
+class NamedDummyAction extends DummyAction { }
+
+class CalledDummyAction extends DummyAction { }
+
+class InstantiatedDummyAction extends DummyAction { }