ApiDeleteTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * Tests for MediaWiki api.php?action=delete.
  4. *
  5. * @author Yifei He
  6. *
  7. * @group API
  8. * @group Database
  9. * @group medium
  10. *
  11. * @covers ApiDelete
  12. */
  13. class ApiDeleteTest extends ApiTestCase {
  14. protected function setUp() {
  15. parent::setUp();
  16. $this->tablesUsed = array_merge(
  17. $this->tablesUsed,
  18. [ 'change_tag', 'change_tag_def', 'logging' ]
  19. );
  20. }
  21. public function testDelete() {
  22. $name = 'Help:' . ucfirst( __FUNCTION__ );
  23. // create new page
  24. $this->editPage( $name, 'Some text' );
  25. // test deletion
  26. $apiResult = $this->doApiRequestWithToken( [
  27. 'action' => 'delete',
  28. 'title' => $name,
  29. ] )[0];
  30. $this->assertArrayHasKey( 'delete', $apiResult );
  31. $this->assertArrayHasKey( 'title', $apiResult['delete'] );
  32. $this->assertSame( $name, $apiResult['delete']['title'] );
  33. $this->assertArrayHasKey( 'logid', $apiResult['delete'] );
  34. $this->assertFalse( Title::newFromText( $name )->exists() );
  35. }
  36. public function testDeleteNonexistent() {
  37. $this->setExpectedException( ApiUsageException::class,
  38. "The page you specified doesn't exist." );
  39. $this->doApiRequestWithToken( [
  40. 'action' => 'delete',
  41. 'title' => 'This page deliberately left nonexistent',
  42. ] );
  43. }
  44. public function testDeletionWithoutPermission() {
  45. $this->setExpectedException( ApiUsageException::class,
  46. 'The action you have requested is limited to users in the group:' );
  47. $name = 'Help:' . ucfirst( __FUNCTION__ );
  48. // create new page
  49. $this->editPage( $name, 'Some text' );
  50. // test deletion without permission
  51. try {
  52. $user = new User();
  53. $apiResult = $this->doApiRequest( [
  54. 'action' => 'delete',
  55. 'title' => $name,
  56. 'token' => $user->getEditToken(),
  57. ], null, null, $user );
  58. } finally {
  59. $this->assertTrue( Title::newFromText( $name )->exists() );
  60. }
  61. }
  62. public function testDeleteWithTag() {
  63. $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_WRITE_BOTH );
  64. $name = 'Help:' . ucfirst( __FUNCTION__ );
  65. ChangeTags::defineTag( 'custom tag' );
  66. $this->editPage( $name, 'Some text' );
  67. $this->doApiRequestWithToken( [
  68. 'action' => 'delete',
  69. 'title' => $name,
  70. 'tags' => 'custom tag',
  71. ] );
  72. $this->assertFalse( Title::newFromText( $name )->exists() );
  73. $dbw = wfGetDB( DB_MASTER );
  74. $this->assertSame( 'custom tag', $dbw->selectField(
  75. [ 'change_tag', 'logging' ],
  76. 'ct_tag',
  77. [
  78. 'log_namespace' => NS_HELP,
  79. 'log_title' => ucfirst( __FUNCTION__ ),
  80. ],
  81. __METHOD__,
  82. [],
  83. [ 'change_tag' => [ 'INNER JOIN', 'ct_log_id = log_id' ] ]
  84. ) );
  85. }
  86. public function testDeleteWithTagNewBackend() {
  87. $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_NEW );
  88. $name = 'Help:' . ucfirst( __FUNCTION__ );
  89. ChangeTags::defineTag( 'custom tag' );
  90. $this->editPage( $name, 'Some text' );
  91. $this->doApiRequestWithToken( [
  92. 'action' => 'delete',
  93. 'title' => $name,
  94. 'tags' => 'custom tag',
  95. ] );
  96. $this->assertFalse( Title::newFromText( $name )->exists() );
  97. $dbw = wfGetDB( DB_MASTER );
  98. $this->assertSame( 'custom tag', $dbw->selectField(
  99. [ 'change_tag', 'logging', 'change_tag_def' ],
  100. 'ctd_name',
  101. [
  102. 'log_namespace' => NS_HELP,
  103. 'log_title' => ucfirst( __FUNCTION__ ),
  104. ],
  105. __METHOD__,
  106. [],
  107. [
  108. 'change_tag' => [ 'INNER JOIN', 'ct_log_id = log_id' ],
  109. 'change_tag_def' => [ 'INNER JOIN', 'ctd_id = ct_tag_id' ]
  110. ]
  111. ) );
  112. }
  113. public function testDeleteWithoutTagPermission() {
  114. $this->setExpectedException( ApiUsageException::class,
  115. 'You do not have permission to apply change tags along with your changes.' );
  116. $name = 'Help:' . ucfirst( __FUNCTION__ );
  117. ChangeTags::defineTag( 'custom tag' );
  118. $this->setMwGlobals( 'wgRevokePermissions',
  119. [ 'user' => [ 'applychangetags' => true ] ] );
  120. $this->editPage( $name, 'Some text' );
  121. try {
  122. $this->doApiRequestWithToken( [
  123. 'action' => 'delete',
  124. 'title' => $name,
  125. 'tags' => 'custom tag',
  126. ] );
  127. } finally {
  128. $this->assertTrue( Title::newFromText( $name )->exists() );
  129. }
  130. }
  131. public function testDeleteAbortedByHook() {
  132. $this->setExpectedException( ApiUsageException::class,
  133. 'Deletion aborted by hook. It gave no explanation.' );
  134. $name = 'Help:' . ucfirst( __FUNCTION__ );
  135. $this->editPage( $name, 'Some text' );
  136. $this->setTemporaryHook( 'ArticleDelete',
  137. function () {
  138. return false;
  139. }
  140. );
  141. try {
  142. $this->doApiRequestWithToken( [ 'action' => 'delete', 'title' => $name ] );
  143. } finally {
  144. $this->assertTrue( Title::newFromText( $name )->exists() );
  145. }
  146. }
  147. public function testDeleteWatch() {
  148. $name = 'Help:' . ucfirst( __FUNCTION__ );
  149. $user = self::$users['sysop']->getUser();
  150. $this->editPage( $name, 'Some text' );
  151. $this->assertTrue( Title::newFromText( $name )->exists() );
  152. $this->assertFalse( $user->isWatched( Title::newFromText( $name ) ) );
  153. $this->doApiRequestWithToken( [ 'action' => 'delete', 'title' => $name, 'watch' => '' ] );
  154. $this->assertFalse( Title::newFromText( $name )->exists() );
  155. $this->assertTrue( $user->isWatched( Title::newFromText( $name ) ) );
  156. }
  157. public function testDeleteUnwatch() {
  158. $name = 'Help:' . ucfirst( __FUNCTION__ );
  159. $user = self::$users['sysop']->getUser();
  160. $this->editPage( $name, 'Some text' );
  161. $this->assertTrue( Title::newFromText( $name )->exists() );
  162. $user->addWatch( Title::newFromText( $name ) );
  163. $this->assertTrue( $user->isWatched( Title::newFromText( $name ) ) );
  164. $this->doApiRequestWithToken( [ 'action' => 'delete', 'title' => $name, 'unwatch' => '' ] );
  165. $this->assertFalse( Title::newFromText( $name )->exists() );
  166. $this->assertFalse( $user->isWatched( Title::newFromText( $name ) ) );
  167. }
  168. }