WatchActionTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. /**
  3. * @covers WatchAction
  4. *
  5. * @group Action
  6. */
  7. class WatchActionTest extends MediaWikiTestCase {
  8. /**
  9. * @var WatchAction
  10. */
  11. private $watchAction;
  12. /**
  13. * @var WikiPage
  14. */
  15. private $testWikiPage;
  16. protected function setUp() {
  17. parent::setUp();
  18. $testTitle = Title::newFromText( 'UTTest' );
  19. $this->testWikiPage = new WikiPage( $testTitle );
  20. $testContext = new DerivativeContext( RequestContext::getMain() );
  21. $testContext->setTitle( $testTitle );
  22. $this->watchAction = new WatchAction( $this->testWikiPage, $testContext );
  23. }
  24. /**
  25. * @throws MWException
  26. */
  27. protected function tearDown() {
  28. parent::tearDown();
  29. Hooks::clear( 'WatchArticle' );
  30. Hooks::clear( 'UnwatchArticle' );
  31. }
  32. /**
  33. * @covers WatchAction::getName()
  34. */
  35. public function testGetName() {
  36. $this->assertEquals( 'watch', $this->watchAction->getName() );
  37. }
  38. /**
  39. * @covers WatchAction::requiresUnblock()
  40. */
  41. public function testRequiresUnlock() {
  42. $this->assertFalse( $this->watchAction->requiresUnblock() );
  43. }
  44. /**
  45. * @covers WatchAction::doesWrites()
  46. */
  47. public function testDoesWrites() {
  48. $this->assertTrue( $this->watchAction->doesWrites() );
  49. }
  50. /**
  51. * @covers WatchAction::onSubmit()
  52. * @covers WatchAction::doWatch()
  53. */
  54. public function testOnSubmit() {
  55. /** @var Status $actual */
  56. $actual = $this->watchAction->onSubmit( [] );
  57. $this->assertTrue( $actual->isGood() );
  58. }
  59. /**
  60. * @covers WatchAction::onSubmit()
  61. * @covers WatchAction::doWatch()
  62. */
  63. public function testOnSubmitHookAborted() {
  64. Hooks::register( 'WatchArticle', function () {
  65. return false;
  66. } );
  67. /** @var Status $actual */
  68. $actual = $this->watchAction->onSubmit( [] );
  69. $this->assertInstanceOf( Status::class, $actual );
  70. $this->assertTrue( $actual->hasMessage( 'hookaborted' ) );
  71. }
  72. /**
  73. * @covers WatchAction::checkCanExecute()
  74. */
  75. public function testShowUserNotLoggedIn() {
  76. $notLoggedInUser = new User();
  77. $testContext = new DerivativeContext( $this->watchAction->getContext() );
  78. $testContext->setUser( $notLoggedInUser );
  79. $watchAction = new WatchAction( $this->testWikiPage, $testContext );
  80. $this->setExpectedException( UserNotLoggedIn::class );
  81. $watchAction->show();
  82. }
  83. /**
  84. * @covers WatchAction::checkCanExecute()
  85. */
  86. public function testShowUserLoggedInNoException() {
  87. $loggedInUser = $this->getMock( User::class );
  88. $loggedInUser->method( 'isLoggedIn' )->willReturn( true );
  89. $testContext = new DerivativeContext( $this->watchAction->getContext() );
  90. $testContext->setUser( $loggedInUser );
  91. $watchAction = new WatchAction( $this->testWikiPage, $testContext );
  92. $exception = null;
  93. try {
  94. $watchAction->show();
  95. } catch ( UserNotLoggedIn $e ) {
  96. $exception = $e;
  97. }
  98. $this->assertNull( $exception,
  99. 'UserNotLoggedIn exception should not be thrown if user is logged in.' );
  100. }
  101. /**
  102. * @covers WatchAction::onSuccess()
  103. */
  104. public function testOnSuccessMainNamespaceTitle() {
  105. $testContext = $this->getMock(
  106. DerivativeContext::class,
  107. [ 'msg' ],
  108. [ $this->watchAction->getContext() ]
  109. );
  110. $testOutput = new OutputPage( $testContext );
  111. $testContext->setOutput( $testOutput );
  112. $testContext->method( 'msg' )->willReturnCallback( function ( $msgKey ) {
  113. return new RawMessage( $msgKey );
  114. } );
  115. $watchAction = new WatchAction( $this->testWikiPage, $testContext );
  116. $watchAction->onSuccess();
  117. $this->assertEquals( '<p>addedwatchtext
  118. </p>', $testOutput->getHTML() );
  119. }
  120. /**
  121. * @covers WatchAction::onSuccess()
  122. */
  123. public function testOnSuccessTalkPage() {
  124. $testContext = $this->getMock(
  125. DerivativeContext::class,
  126. [],
  127. [ $this->watchAction->getContext() ]
  128. );
  129. $testOutput = new OutputPage( $testContext );
  130. $testContext->method( 'getOutput' )->willReturn( $testOutput );
  131. $testContext->method( 'msg' )->willReturnCallback( function ( $msgKey ) {
  132. return new RawMessage( $msgKey );
  133. } );
  134. $talkPageTitle = Title::newFromText( 'Talk:UTTest' );
  135. $testContext->setTitle( $talkPageTitle );
  136. $watchAction = new WatchAction( new WikiPage( $talkPageTitle ), $testContext );
  137. $watchAction->onSuccess();
  138. $this->assertEquals( '<p>addedwatchtext-talk
  139. </p>', $testOutput->getHTML() );
  140. }
  141. /**
  142. * @covers WatchAction::doWatch()
  143. */
  144. public function testDoWatchNoCheckRights() {
  145. $notPermittedUser = $this->getMock( User::class );
  146. $notPermittedUser->method( 'isAllowed' )->willReturn( false );
  147. $actual = WatchAction::doWatch( $this->testWikiPage->getTitle(), $notPermittedUser, false );
  148. $this->assertTrue( $actual->isGood() );
  149. }
  150. /**
  151. * @covers WatchAction::doWatch()
  152. */
  153. public function testDoWatchUserNotPermittedStatusNotGood() {
  154. $notPermittedUser = $this->getMock( User::class );
  155. $notPermittedUser->method( 'isAllowed' )->willReturn( false );
  156. $actual = WatchAction::doWatch( $this->testWikiPage->getTitle(), $notPermittedUser, true );
  157. $this->assertFalse( $actual->isGood() );
  158. }
  159. /**
  160. * @covers WatchAction::doWatch()
  161. */
  162. public function testDoWatchCallsUserAddWatch() {
  163. $permittedUser = $this->getMock( User::class );
  164. $permittedUser->method( 'isAllowed' )->willReturn( true );
  165. $permittedUser->expects( $this->once() )
  166. ->method( 'addWatch' )
  167. ->with( $this->equalTo( $this->testWikiPage->getTitle() ), $this->equalTo( true ) );
  168. $actual = WatchAction::doWatch( $this->testWikiPage->getTitle(), $permittedUser );
  169. $this->assertTrue( $actual->isGood() );
  170. }
  171. /**
  172. * @covers WatchAction::doUnWatch()
  173. */
  174. public function testDoUnWatchWithoutRights() {
  175. $notPermittedUser = $this->getMock( User::class );
  176. $notPermittedUser->method( 'isAllowed' )->willReturn( false );
  177. $actual = WatchAction::doUnWatch( $this->testWikiPage->getTitle(), $notPermittedUser );
  178. $this->assertFalse( $actual->isGood() );
  179. }
  180. /**
  181. * @covers WatchAction::doUnWatch()
  182. */
  183. public function testDoUnWatchUserHookAborted() {
  184. $permittedUser = $this->getMock( User::class );
  185. $permittedUser->method( 'isAllowed' )->willReturn( true );
  186. Hooks::register( 'UnwatchArticle', function () {
  187. return false;
  188. } );
  189. $status = WatchAction::doUnWatch( $this->testWikiPage->getTitle(), $permittedUser );
  190. $this->assertFalse( $status->isGood() );
  191. $errors = $status->getErrors();
  192. $this->assertEquals( 1, count( $errors ) );
  193. $this->assertEquals( 'hookaborted', $errors[0]['message'] );
  194. }
  195. /**
  196. * @covers WatchAction::doUnWatch()
  197. */
  198. public function testDoUnWatchCallsUserRemoveWatch() {
  199. $permittedUser = $this->getMock( User::class );
  200. $permittedUser->method( 'isAllowed' )->willReturn( true );
  201. $permittedUser->expects( $this->once() )
  202. ->method( 'removeWatch' )
  203. ->with( $this->equalTo( $this->testWikiPage->getTitle() ) );
  204. $actual = WatchAction::doUnWatch( $this->testWikiPage->getTitle(), $permittedUser );
  205. $this->assertTrue( $actual->isGood() );
  206. }
  207. /**
  208. * @covers WatchAction::getWatchToken()
  209. */
  210. public function testGetWatchTokenNormalizesToWatch() {
  211. $user = $this->getMock( User::class );
  212. $user->expects( $this->once() )
  213. ->method( 'getEditToken' )
  214. ->with( $this->equalTo( 'watch' ) );
  215. WatchAction::getWatchToken( $this->watchAction->getTitle(), $user, 'INVALID_ACTION' );
  216. }
  217. /**
  218. * @covers WatchAction::getWatchToken()
  219. */
  220. public function testGetWatchTokenProxiesUserGetEditToken() {
  221. $user = $this->getMock( User::class );
  222. $user->expects( $this->once() )->method( 'getEditToken' );
  223. WatchAction::getWatchToken( $this->watchAction->getTitle(), $user );
  224. }
  225. /**
  226. * @covers WatchAction::getUnwatchToken()
  227. */
  228. public function testGetUnwatchToken() {
  229. $user = $this->getMock( User::class );
  230. $user->expects( $this->once() )->method( 'getEditToken' );
  231. $this->hideDeprecated( 'WatchAction::getUnwatchToken' );
  232. WatchAction::getUnWatchToken( $this->watchAction->getTitle(), $user );
  233. }
  234. /**
  235. * @covers WatchAction::doWatchOrUnwatch()
  236. */
  237. public function testDoWatchOrUnwatchUserNotLoggedIn() {
  238. $user = $this->getLoggedInIsWatchedUser( false );
  239. $user->expects( $this->never() )->method( 'removeWatch' );
  240. $user->expects( $this->never() )->method( 'addWatch' );
  241. $status = WatchAction::doWatchOrUnwatch( true, $this->watchAction->getTitle(), $user );
  242. $this->assertTrue( $status->isGood() );
  243. }
  244. /**
  245. * @covers WatchAction::doWatchOrUnwatch()
  246. */
  247. public function testDoWatchOrUnwatchSkipsIfAlreadyWatched() {
  248. $user = $this->getLoggedInIsWatchedUser();
  249. $user->expects( $this->never() )->method( 'removeWatch' );
  250. $user->expects( $this->never() )->method( 'addWatch' );
  251. $status = WatchAction::doWatchOrUnwatch( true, $this->watchAction->getTitle(), $user );
  252. $this->assertTrue( $status->isGood() );
  253. }
  254. /**
  255. * @covers WatchAction::doWatchOrUnwatch()
  256. */
  257. public function testDoWatchOrUnwatchSkipsIfAlreadyUnWatched() {
  258. $user = $this->getLoggedInIsWatchedUser( true, false );
  259. $user->expects( $this->never() )->method( 'removeWatch' );
  260. $user->expects( $this->never() )->method( 'addWatch' );
  261. $status = WatchAction::doWatchOrUnwatch( false, $this->watchAction->getTitle(), $user );
  262. $this->assertTrue( $status->isGood() );
  263. }
  264. /**
  265. * @covers WatchAction::doWatchOrUnwatch()
  266. */
  267. public function testDoWatchOrUnwatchWatchesIfWatch() {
  268. $user = $this->getLoggedInIsWatchedUser( true, false );
  269. $user->expects( $this->never() )->method( 'removeWatch' );
  270. $user->expects( $this->once() )
  271. ->method( 'addWatch' )
  272. ->with( $this->equalTo( $this->testWikiPage->getTitle() ), $this->equalTo( false ) );
  273. $status = WatchAction::doWatchOrUnwatch( true, $this->watchAction->getTitle(), $user );
  274. $this->assertTrue( $status->isGood() );
  275. }
  276. /**
  277. * @covers WatchAction::doWatchOrUnwatch()
  278. */
  279. public function testDoWatchOrUnwatchUnwatchesIfUnwatch() {
  280. $user = $this->getLoggedInIsWatchedUser();
  281. $user->method( 'isAllowed' )->willReturn( true );
  282. $user->expects( $this->never() )->method( 'addWatch' );
  283. $user->expects( $this->once() )
  284. ->method( 'removeWatch' )
  285. ->with( $this->equalTo( $this->testWikiPage->getTitle() ) );
  286. $status = WatchAction::doWatchOrUnwatch( false, $this->watchAction->getTitle(), $user );
  287. $this->assertTrue( $status->isGood() );
  288. }
  289. /**
  290. * @param bool $isLoggedIn Whether the user should be "marked" as logged in
  291. * @param bool $isWatched The value any call to isWatched should return
  292. * @return PHPUnit_Framework_MockObject_MockObject
  293. */
  294. private function getLoggedInIsWatchedUser( $isLoggedIn = true, $isWatched = true ) {
  295. $user = $this->getMock( User::class );
  296. $user->method( 'isLoggedIn' )->willReturn( $isLoggedIn );
  297. $user->method( 'isWatched' )->willReturn( $isWatched );
  298. return $user;
  299. }
  300. }