ApiWatchTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * @group API
  4. * @group Database
  5. * @group medium
  6. * @todo This test suite is severly broken and need a full review
  7. *
  8. * @covers ApiWatch
  9. */
  10. class ApiWatchTest extends ApiTestCase {
  11. function getTokens() {
  12. return $this->getTokenList( self::$users['sysop'] );
  13. }
  14. public function testWatchEdit() {
  15. $tokens = $this->getTokens();
  16. $data = $this->doApiRequest( [
  17. 'action' => 'edit',
  18. 'title' => 'Help:UTPage', // Help namespace is hopefully wikitext
  19. 'text' => 'new text',
  20. 'token' => $tokens['edittoken'],
  21. 'watchlist' => 'watch' ] );
  22. $this->assertArrayHasKey( 'edit', $data[0] );
  23. $this->assertArrayHasKey( 'result', $data[0]['edit'] );
  24. $this->assertEquals( 'Success', $data[0]['edit']['result'] );
  25. return $data;
  26. }
  27. /**
  28. * @depends testWatchEdit
  29. */
  30. public function testWatchClear() {
  31. $tokens = $this->getTokens();
  32. $data = $this->doApiRequest( [
  33. 'action' => 'query',
  34. 'wllimit' => 'max',
  35. 'list' => 'watchlist' ] );
  36. if ( isset( $data[0]['query']['watchlist'] ) ) {
  37. $wl = $data[0]['query']['watchlist'];
  38. foreach ( $wl as $page ) {
  39. $data = $this->doApiRequest( [
  40. 'action' => 'watch',
  41. 'title' => $page['title'],
  42. 'unwatch' => true,
  43. 'token' => $tokens['watchtoken'] ] );
  44. }
  45. }
  46. $data = $this->doApiRequest( [
  47. 'action' => 'query',
  48. 'list' => 'watchlist' ], $data );
  49. $this->assertArrayHasKey( 'query', $data[0] );
  50. $this->assertArrayHasKey( 'watchlist', $data[0]['query'] );
  51. foreach ( $data[0]['query']['watchlist'] as $index => $item ) {
  52. // Previous tests may insert an invalid title
  53. // like ":ApiEditPageTest testNonTextEdit", which
  54. // can't be cleared.
  55. if ( strpos( $item['title'], ':' ) === 0 ) {
  56. unset( $data[0]['query']['watchlist'][$index] );
  57. }
  58. }
  59. $this->assertEquals( 0, count( $data[0]['query']['watchlist'] ) );
  60. return $data;
  61. }
  62. public function testWatchProtect() {
  63. $tokens = $this->getTokens();
  64. $data = $this->doApiRequest( [
  65. 'action' => 'protect',
  66. 'token' => $tokens['protecttoken'],
  67. 'title' => 'Help:UTPage',
  68. 'protections' => 'edit=sysop',
  69. 'watchlist' => 'unwatch' ] );
  70. $this->assertArrayHasKey( 'protect', $data[0] );
  71. $this->assertArrayHasKey( 'protections', $data[0]['protect'] );
  72. $this->assertEquals( 1, count( $data[0]['protect']['protections'] ) );
  73. $this->assertArrayHasKey( 'edit', $data[0]['protect']['protections'][0] );
  74. }
  75. public function testGetRollbackToken() {
  76. $this->getTokens();
  77. if ( !Title::newFromText( 'Help:UTPage' )->exists() ) {
  78. $this->markTestSkipped( "The article [[Help:UTPage]] does not exist" ); // TODO: just create it?
  79. }
  80. $data = $this->doApiRequest( [
  81. 'action' => 'query',
  82. 'prop' => 'revisions',
  83. 'titles' => 'Help:UTPage',
  84. 'rvtoken' => 'rollback' ] );
  85. $this->assertArrayHasKey( 'query', $data[0] );
  86. $this->assertArrayHasKey( 'pages', $data[0]['query'] );
  87. $keys = array_keys( $data[0]['query']['pages'] );
  88. $key = array_pop( $keys );
  89. if ( isset( $data[0]['query']['pages'][$key]['missing'] ) ) {
  90. $this->markTestSkipped( "Target page (Help:UTPage) doesn't exist" );
  91. }
  92. $this->assertArrayHasKey( 'pageid', $data[0]['query']['pages'][$key] );
  93. $this->assertArrayHasKey( 'revisions', $data[0]['query']['pages'][$key] );
  94. $this->assertArrayHasKey( 0, $data[0]['query']['pages'][$key]['revisions'] );
  95. $this->assertArrayHasKey( 'rollbacktoken', $data[0]['query']['pages'][$key]['revisions'][0] );
  96. return $data;
  97. }
  98. /**
  99. * @group Broken
  100. * Broken because there is currently no revision info in the $pageinfo
  101. *
  102. * @depends testGetRollbackToken
  103. */
  104. public function testWatchRollback( $data ) {
  105. $keys = array_keys( $data[0]['query']['pages'] );
  106. $key = array_pop( $keys );
  107. $pageinfo = $data[0]['query']['pages'][$key];
  108. $revinfo = $pageinfo['revisions'][0];
  109. try {
  110. $data = $this->doApiRequest( [
  111. 'action' => 'rollback',
  112. 'title' => 'Help:UTPage',
  113. 'user' => $revinfo['user'],
  114. 'token' => $pageinfo['rollbacktoken'],
  115. 'watchlist' => 'watch' ] );
  116. $this->assertArrayHasKey( 'rollback', $data[0] );
  117. $this->assertArrayHasKey( 'title', $data[0]['rollback'] );
  118. } catch ( ApiUsageException $ue ) {
  119. if ( self::apiExceptionHasCode( $ue, 'onlyauthor' ) ) {
  120. $this->markTestIncomplete( "Only one author to 'Help:UTPage', cannot test rollback" );
  121. } else {
  122. $this->fail( "Received error '" . $ue->getMessage() . "'" );
  123. }
  124. }
  125. }
  126. }