MediaWiki
REL1_24
|
00001 <?php 00002 00012 class ActionTest extends MediaWikiTestCase { 00013 00014 protected function setUp() { 00015 parent::setUp(); 00016 00017 $context = $this->getContext(); 00018 $this->setMwGlobals( 'wgActions', array( 00019 'null' => null, 00020 'disabled' => false, 00021 'view' => true, 00022 'edit' => true, 00023 'revisiondelete' => true, 00024 'dummy' => true, 00025 'string' => 'NamedDummyAction', 00026 'declared' => 'NonExistingClassName', 00027 'callable' => array( $this, 'dummyActionCallback' ), 00028 'object' => new InstantiatedDummyAction( $context->getWikiPage(), $context ), 00029 ) ); 00030 } 00031 00032 private function getPage() { 00033 return WikiPage::factory( Title::makeTitle( 0, 'Title' ) ); 00034 } 00035 00036 private function getContext( $requestedAction = null ) { 00037 $request = new FauxRequest( array( 'action' => $requestedAction ) ); 00038 00039 $context = new DerivativeContext( RequestContext::getMain() ); 00040 $context->setRequest( $request ); 00041 $context->setWikiPage( $this->getPage() ); 00042 00043 return $context; 00044 } 00045 00046 public function actionProvider() { 00047 return array( 00048 array( 'dummy', 'DummyAction' ), 00049 array( 'string', 'NamedDummyAction' ), 00050 array( 'callable', 'CalledDummyAction' ), 00051 array( 'object', 'InstantiatedDummyAction' ), 00052 00053 // Capitalization is ignored 00054 array( 'DUMMY', 'DummyAction' ), 00055 array( 'STRING', 'NamedDummyAction' ), 00056 00057 // Null and non-existing values 00058 array( 'null', null ), 00059 array( 'undeclared', null ), 00060 array( '', null ), 00061 array( false, null ), 00062 ); 00063 } 00064 00070 public function testActionExists( $requestedAction, $expected ) { 00071 $exists = Action::exists( $requestedAction ); 00072 00073 $this->assertSame( $expected !== null, $exists ); 00074 } 00075 00076 public function testActionExists_doesNotRequireInstantiation() { 00077 // The method is not supposed to check if the action can be instantiated. 00078 $exists = Action::exists( 'declared' ); 00079 00080 $this->assertTrue( $exists ); 00081 } 00082 00088 public function testGetActionName( $requestedAction, $expected ) { 00089 $context = $this->getContext( $requestedAction ); 00090 $actionName = Action::getActionName( $context ); 00091 00092 $this->assertEquals( $expected ?: 'nosuchaction', $actionName ); 00093 } 00094 00095 public function testGetActionName_editredlinkWorkaround() { 00096 // See https://bugzilla.wikimedia.org/show_bug.cgi?id=20966 00097 $context = $this->getContext( 'editredlink' ); 00098 $actionName = Action::getActionName( $context ); 00099 00100 $this->assertEquals( 'edit', $actionName ); 00101 } 00102 00103 public function testGetActionName_historysubmitWorkaround() { 00104 // See https://bugzilla.wikimedia.org/show_bug.cgi?id=20966 00105 $context = $this->getContext( 'historysubmit' ); 00106 $actionName = Action::getActionName( $context ); 00107 00108 $this->assertEquals( 'view', $actionName ); 00109 } 00110 00111 public function testGetActionName_revisiondeleteWorkaround() { 00112 // See https://bugzilla.wikimedia.org/show_bug.cgi?id=20966 00113 $context = $this->getContext( 'historysubmit' ); 00114 $context->getRequest()->setVal( 'revisiondelete', true ); 00115 $actionName = Action::getActionName( $context ); 00116 00117 $this->assertEquals( 'revisiondelete', $actionName ); 00118 } 00119 00125 public function testActionFactory( $requestedAction, $expected ) { 00126 $context = $this->getContext(); 00127 $action = Action::factory( $requestedAction, $context->getWikiPage(), $context ); 00128 00129 $this->assertType( $expected ?: 'null', $action ); 00130 } 00131 00132 public function testNull_doesNotExist() { 00133 $exists = Action::exists( null ); 00134 00135 $this->assertFalse( $exists ); 00136 } 00137 00138 public function testNull_defaultsToView() { 00139 $context = $this->getContext( null ); 00140 $actionName = Action::getActionName( $context ); 00141 00142 $this->assertEquals( 'view', $actionName ); 00143 } 00144 00145 public function testNull_canNotBeInstantiated() { 00146 $page = $this->getPage(); 00147 $action = Action::factory( null, $page ); 00148 00149 $this->assertNull( $action ); 00150 } 00151 00152 public function testDisabledAction_exists() { 00153 $exists = Action::exists( 'disabled' ); 00154 00155 $this->assertTrue( $exists ); 00156 } 00157 00158 public function testDisabledAction_isNotResolved() { 00159 $context = $this->getContext( 'disabled' ); 00160 $actionName = Action::getActionName( $context ); 00161 00162 $this->assertEquals( 'nosuchaction', $actionName ); 00163 } 00164 00165 public function testDisabledAction_factoryReturnsFalse() { 00166 $page = $this->getPage(); 00167 $action = Action::factory( 'disabled', $page ); 00168 00169 $this->assertFalse( $action ); 00170 } 00171 00172 public function dummyActionCallback() { 00173 $context = $this->getContext(); 00174 return new CalledDummyAction( $context->getWikiPage(), $context ); 00175 } 00176 00177 } 00178 00179 class DummyAction extends Action { 00180 00181 public function getName() { 00182 return get_called_class(); 00183 } 00184 00185 public function show() { 00186 } 00187 00188 public function execute() { 00189 } 00190 } 00191 00192 class NamedDummyAction extends DummyAction { 00193 } 00194 00195 class CalledDummyAction extends DummyAction { 00196 } 00197 00198 class InstantiatedDummyAction extends DummyAction { 00199 }