2018-01-14 09:52:57 +00:00
|
|
|
<?php
|
|
|
|
|
|
2022-07-06 15:05:30 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2022-07-06 15:05:30 +00:00
|
|
|
|
2018-01-14 09:52:57 +00:00
|
|
|
/**
|
|
|
|
|
* Tests for MediaWiki api.php?action=delete.
|
|
|
|
|
*
|
|
|
|
|
* @author Yifei He
|
|
|
|
|
*
|
|
|
|
|
* @group API
|
|
|
|
|
* @group Database
|
|
|
|
|
* @group medium
|
|
|
|
|
*
|
|
|
|
|
* @covers ApiDelete
|
|
|
|
|
*/
|
|
|
|
|
class ApiDeleteTest extends ApiTestCase {
|
2018-09-04 19:50:46 +00:00
|
|
|
|
2021-07-22 03:11:47 +00:00
|
|
|
protected function setUp(): void {
|
2018-09-04 19:50:46 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
$this->tablesUsed = array_merge(
|
|
|
|
|
$this->tablesUsed,
|
2020-06-04 20:16:23 +00:00
|
|
|
[ 'change_tag', 'change_tag_def', 'logging', 'watchlist', 'watchlist_expiry' ]
|
2018-09-04 19:50:46 +00:00
|
|
|
);
|
2020-06-04 20:16:23 +00:00
|
|
|
|
2022-07-06 15:05:30 +00:00
|
|
|
$this->overrideConfigValue( MainConfigNames::WatchlistExpiry, true );
|
2018-09-04 19:50:46 +00:00
|
|
|
}
|
|
|
|
|
|
2018-01-14 09:52:57 +00:00
|
|
|
public function testDelete() {
|
2023-07-28 21:47:54 +00:00
|
|
|
$title = Title::makeTitle( NS_HELP, 'TestDelete' );
|
2018-01-14 09:52:57 +00:00
|
|
|
|
|
|
|
|
// create new page
|
2023-07-28 21:47:54 +00:00
|
|
|
$this->editPage( $title, 'Some text' );
|
2018-01-14 09:52:57 +00:00
|
|
|
|
|
|
|
|
// test deletion
|
|
|
|
|
$apiResult = $this->doApiRequestWithToken( [
|
|
|
|
|
'action' => 'delete',
|
2023-07-28 21:47:54 +00:00
|
|
|
'title' => $title->getPrefixedText(),
|
2018-03-18 19:58:02 +00:00
|
|
|
] )[0];
|
2018-01-14 09:52:57 +00:00
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'delete', $apiResult );
|
|
|
|
|
$this->assertArrayHasKey( 'title', $apiResult['delete'] );
|
2023-07-28 21:47:54 +00:00
|
|
|
$this->assertSame( $title->getPrefixedText(), $apiResult['delete']['title'] );
|
2018-01-14 09:52:57 +00:00
|
|
|
$this->assertArrayHasKey( 'logid', $apiResult['delete'] );
|
|
|
|
|
|
2023-07-28 21:47:54 +00:00
|
|
|
$this->assertFalse( $title->exists( Title::READ_LATEST ) );
|
2018-01-14 09:52:57 +00:00
|
|
|
}
|
|
|
|
|
|
2018-12-20 14:59:02 +00:00
|
|
|
public function testBatchedDelete() {
|
2022-07-06 15:05:30 +00:00
|
|
|
$this->overrideConfigValue(
|
|
|
|
|
MainConfigNames::DeleteRevisionsBatchSize, 1
|
|
|
|
|
);
|
2018-12-20 14:59:02 +00:00
|
|
|
|
2023-07-28 21:47:54 +00:00
|
|
|
$title = Title::makeTitle( NS_HELP, 'TestBatchedDelete' );
|
2018-12-20 14:59:02 +00:00
|
|
|
for ( $i = 1; $i <= 3; $i++ ) {
|
2023-07-28 21:47:54 +00:00
|
|
|
$this->editPage( $title, "Revision $i" );
|
2018-12-20 14:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$apiResult = $this->doApiRequestWithToken( [
|
|
|
|
|
'action' => 'delete',
|
2023-07-28 21:47:54 +00:00
|
|
|
'title' => $title->getPrefixedText(),
|
2018-12-20 14:59:02 +00:00
|
|
|
] )[0];
|
|
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'delete', $apiResult );
|
|
|
|
|
$this->assertArrayHasKey( 'title', $apiResult['delete'] );
|
2023-07-28 21:47:54 +00:00
|
|
|
$this->assertSame( $title->getPrefixedText(), $apiResult['delete']['title'] );
|
2018-12-20 14:59:02 +00:00
|
|
|
$this->assertArrayHasKey( 'scheduled', $apiResult['delete'] );
|
|
|
|
|
$this->assertTrue( $apiResult['delete']['scheduled'] );
|
|
|
|
|
$this->assertArrayNotHasKey( 'logid', $apiResult['delete'] );
|
|
|
|
|
|
|
|
|
|
// Run the jobs
|
2021-04-28 02:13:58 +00:00
|
|
|
$this->runJobs();
|
2018-12-20 14:59:02 +00:00
|
|
|
|
2023-07-28 21:47:54 +00:00
|
|
|
$this->assertFalse( $title->exists( Title::READ_LATEST ) );
|
2018-12-20 14:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
2018-03-18 19:58:02 +00:00
|
|
|
public function testDeleteNonexistent() {
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'missingtitle' );
|
2018-03-18 19:58:02 +00:00
|
|
|
|
|
|
|
|
$this->doApiRequestWithToken( [
|
|
|
|
|
'action' => 'delete',
|
|
|
|
|
'title' => 'This page deliberately left nonexistent',
|
|
|
|
|
] );
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-22 05:18:40 +00:00
|
|
|
public function testDeleteAssociatedTalkPage() {
|
2023-07-28 21:47:54 +00:00
|
|
|
$title = Title::makeTitle( NS_HELP, 'TestDeleteAssociatedTalkPage' );
|
|
|
|
|
$talkPage = $title->getTalkPageIfDefined();
|
2022-02-22 05:18:40 +00:00
|
|
|
$this->editPage( $title, 'Some text' );
|
|
|
|
|
$this->editPage( $talkPage, 'Some text' );
|
|
|
|
|
$apiResult = $this->doApiRequestWithToken( [
|
|
|
|
|
'action' => 'delete',
|
2023-07-28 21:47:54 +00:00
|
|
|
'title' => $title->getPrefixedText(),
|
2022-02-22 05:18:40 +00:00
|
|
|
'deletetalk' => true,
|
|
|
|
|
] )[0];
|
|
|
|
|
|
2023-07-28 21:47:54 +00:00
|
|
|
$this->assertSame( $title->getPrefixedText(), $apiResult['delete']['title'] );
|
|
|
|
|
$this->assertFalse( $talkPage->exists( Title::READ_LATEST ) );
|
2022-02-22 05:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testDeleteAssociatedTalkPageNonexistent() {
|
2023-07-28 21:47:54 +00:00
|
|
|
$title = Title::makeTitle( NS_HELP, 'TestDeleteAssociatedTalkPageNonexistent' );
|
2022-02-22 05:18:40 +00:00
|
|
|
$this->editPage( $title, 'Some text' );
|
|
|
|
|
$apiResult = $this->doApiRequestWithToken( [
|
|
|
|
|
'action' => 'delete',
|
2023-07-28 21:47:54 +00:00
|
|
|
'title' => $title->getPrefixedText(),
|
2022-02-22 05:18:40 +00:00
|
|
|
'deletetalk' => true,
|
|
|
|
|
] )[0];
|
|
|
|
|
|
2023-07-28 21:47:54 +00:00
|
|
|
$this->assertSame( $title->getPrefixedText(), $apiResult['delete']['title'] );
|
2022-02-22 05:18:40 +00:00
|
|
|
$this->assertArrayHasKey( 'warnings', $apiResult );
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-14 09:52:57 +00:00
|
|
|
public function testDeletionWithoutPermission() {
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'permissiondenied' );
|
2018-03-18 19:58:02 +00:00
|
|
|
|
2023-07-28 21:47:54 +00:00
|
|
|
$title = Title::makeTitle( NS_HELP, 'TestDeletionWithoutPermission' );
|
2018-01-14 09:52:57 +00:00
|
|
|
|
|
|
|
|
// create new page
|
2023-07-28 21:47:54 +00:00
|
|
|
$this->editPage( $title, 'Some text' );
|
2018-01-14 09:52:57 +00:00
|
|
|
|
|
|
|
|
// test deletion without permission
|
|
|
|
|
try {
|
|
|
|
|
$user = new User();
|
2023-03-24 03:21:20 +00:00
|
|
|
$this->doApiRequest( [
|
2018-01-14 09:52:57 +00:00
|
|
|
'action' => 'delete',
|
2023-07-28 21:47:54 +00:00
|
|
|
'title' => $title->getPrefixedText(),
|
2021-08-05 06:54:11 +00:00
|
|
|
'token' => $user->getEditToken(),
|
|
|
|
|
], null, null, $user );
|
2018-03-18 19:58:02 +00:00
|
|
|
} finally {
|
2023-07-28 21:47:54 +00:00
|
|
|
$this->assertTrue( $title->exists( Title::READ_LATEST ) );
|
2018-03-18 19:58:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testDeleteWithTag() {
|
2023-07-28 21:47:54 +00:00
|
|
|
$title = Title::makeTitle( NS_HELP, 'TestDeleteWithTag' );
|
2018-09-04 19:50:46 +00:00
|
|
|
|
2023-07-29 09:44:29 +00:00
|
|
|
$this->getServiceContainer()->getChangeTagsStore()->defineTag( 'custom tag' );
|
2018-09-04 19:50:46 +00:00
|
|
|
|
2023-07-28 21:47:54 +00:00
|
|
|
$this->editPage( $title, 'Some text' );
|
2018-09-04 19:50:46 +00:00
|
|
|
|
|
|
|
|
$this->doApiRequestWithToken( [
|
|
|
|
|
'action' => 'delete',
|
2023-07-28 21:47:54 +00:00
|
|
|
'title' => $title->getPrefixedText(),
|
2018-09-04 19:50:46 +00:00
|
|
|
'tags' => 'custom tag',
|
|
|
|
|
] );
|
|
|
|
|
|
2023-07-28 21:47:54 +00:00
|
|
|
$this->assertFalse( $title->exists( Title::READ_LATEST ) );
|
2018-09-04 19:50:46 +00:00
|
|
|
|
2021-04-29 02:37:11 +00:00
|
|
|
$dbw = wfGetDB( DB_PRIMARY );
|
2018-09-04 19:50:46 +00:00
|
|
|
$this->assertSame( 'custom tag', $dbw->selectField(
|
|
|
|
|
[ 'change_tag', 'logging', 'change_tag_def' ],
|
|
|
|
|
'ctd_name',
|
|
|
|
|
[
|
2023-07-28 21:47:54 +00:00
|
|
|
'log_namespace' => $title->getNamespace(),
|
|
|
|
|
'log_title' => $title->getDBkey(),
|
2018-09-04 19:50:46 +00:00
|
|
|
],
|
|
|
|
|
__METHOD__,
|
|
|
|
|
[],
|
|
|
|
|
[
|
2019-03-06 17:17:27 +00:00
|
|
|
'change_tag' => [ 'JOIN', 'ct_log_id = log_id' ],
|
|
|
|
|
'change_tag_def' => [ 'JOIN', 'ctd_id = ct_tag_id' ]
|
2018-09-04 19:50:46 +00:00
|
|
|
]
|
|
|
|
|
) );
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-18 19:58:02 +00:00
|
|
|
public function testDeleteWithoutTagPermission() {
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'tags-apply-no-permission' );
|
2018-03-18 19:58:02 +00:00
|
|
|
|
2023-07-28 21:47:54 +00:00
|
|
|
$title = Title::makeTitle( NS_HELP, 'TestDeleteWithoutTagPermission' );
|
2018-03-18 19:58:02 +00:00
|
|
|
|
2023-07-29 09:44:29 +00:00
|
|
|
$this->getServiceContainer()->getChangeTagsStore()->defineTag( 'custom tag' );
|
2022-07-06 15:05:30 +00:00
|
|
|
$this->overrideConfigValue(
|
|
|
|
|
MainConfigNames::RevokePermissions,
|
|
|
|
|
[ 'user' => [ 'applychangetags' => true ] ]
|
|
|
|
|
);
|
2018-03-18 19:58:02 +00:00
|
|
|
|
2023-07-28 21:47:54 +00:00
|
|
|
$this->editPage( $title, 'Some text' );
|
2018-03-18 19:58:02 +00:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$this->doApiRequestWithToken( [
|
|
|
|
|
'action' => 'delete',
|
2023-07-28 21:47:54 +00:00
|
|
|
'title' => $title->getPrefixedText(),
|
2018-03-18 19:58:02 +00:00
|
|
|
'tags' => 'custom tag',
|
|
|
|
|
] );
|
|
|
|
|
} finally {
|
2023-07-28 21:47:54 +00:00
|
|
|
$this->assertTrue( $title->exists( Title::READ_LATEST ) );
|
2018-03-18 19:58:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testDeleteAbortedByHook() {
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'delete-hook-aborted' );
|
2018-03-18 19:58:02 +00:00
|
|
|
|
2023-07-28 21:47:54 +00:00
|
|
|
$title = Title::makeTitle( NS_HELP, 'TestDeleteAbortedByHook' );
|
2018-03-18 19:58:02 +00:00
|
|
|
|
2023-07-28 21:47:54 +00:00
|
|
|
$this->editPage( $title, 'Some text' );
|
2018-03-18 19:58:02 +00:00
|
|
|
|
|
|
|
|
$this->setTemporaryHook( 'ArticleDelete',
|
2021-02-07 13:10:36 +00:00
|
|
|
static function () {
|
2018-03-18 19:58:02 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
try {
|
2023-07-28 21:47:54 +00:00
|
|
|
$this->doApiRequestWithToken( [ 'action' => 'delete', 'title' => $title->getPrefixedText() ] );
|
2018-03-18 19:58:02 +00:00
|
|
|
} finally {
|
2023-07-28 21:47:54 +00:00
|
|
|
$this->assertTrue( $title->exists( Title::READ_LATEST ) );
|
2018-01-14 09:52:57 +00:00
|
|
|
}
|
2018-03-18 19:58:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testDeleteWatch() {
|
2023-07-28 21:47:54 +00:00
|
|
|
$title = Title::makeTitle( NS_HELP, 'TestDeleteWatch' );
|
|
|
|
|
$page = $this->getExistingTestPage( $title );
|
2023-07-16 23:53:31 +00:00
|
|
|
$performer = $this->getTestSysop()->getUser();
|
2021-04-13 03:28:23 +00:00
|
|
|
$watchlistManager = $this->getServiceContainer()->getWatchlistManager();
|
2018-03-18 19:58:02 +00:00
|
|
|
|
2023-07-16 23:53:31 +00:00
|
|
|
$this->assertFalse( $watchlistManager->isWatched( $performer, $page ) );
|
2018-03-18 19:58:02 +00:00
|
|
|
|
2023-07-16 23:53:31 +00:00
|
|
|
$res = $this->doApiRequestWithToken(
|
|
|
|
|
[
|
|
|
|
|
'action' => 'delete',
|
2023-07-28 21:47:54 +00:00
|
|
|
'title' => $title->getPrefixedText(),
|
2023-07-16 23:53:31 +00:00
|
|
|
'watch' => '',
|
|
|
|
|
'watchlistexpiry' => '99990123000000',
|
|
|
|
|
],
|
|
|
|
|
null,
|
|
|
|
|
$performer
|
|
|
|
|
);
|
|
|
|
|
$this->assertArrayHasKey( 'delete', $res[0] );
|
|
|
|
|
$page->clear();
|
2018-01-14 09:52:57 +00:00
|
|
|
|
2023-07-16 23:53:31 +00:00
|
|
|
$this->assertFalse( $page->exists() );
|
|
|
|
|
$this->assertTrue( $watchlistManager->isWatched( $performer, $page ) );
|
|
|
|
|
$this->assertTrue( $watchlistManager->isTempWatched( $performer, $page ) );
|
2018-03-18 19:58:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testDeleteUnwatch() {
|
2023-07-28 21:47:54 +00:00
|
|
|
$title = Title::makeTitle( NS_HELP, 'TestDeleteUnwatch' );
|
2021-01-07 20:57:19 +00:00
|
|
|
$user = $this->getTestSysop()->getUser();
|
2018-03-18 19:58:02 +00:00
|
|
|
|
2023-07-28 21:47:54 +00:00
|
|
|
$this->editPage( $title, 'Some text' );
|
|
|
|
|
$this->assertTrue( $title->exists( Title::READ_LATEST ) );
|
2021-04-13 03:28:23 +00:00
|
|
|
$watchlistManager = $this->getServiceContainer()->getWatchlistManager();
|
2023-07-28 21:47:54 +00:00
|
|
|
$watchlistManager->addWatch( $user, $title );
|
|
|
|
|
$this->assertTrue( $watchlistManager->isWatched( $user, $title ) );
|
2018-03-18 19:58:02 +00:00
|
|
|
|
2020-06-04 20:16:23 +00:00
|
|
|
$this->doApiRequestWithToken( [
|
|
|
|
|
'action' => 'delete',
|
2023-07-28 21:47:54 +00:00
|
|
|
'title' => $title->getPrefixedText(),
|
2020-06-04 20:16:23 +00:00
|
|
|
'watchlist' => 'unwatch',
|
|
|
|
|
] );
|
2018-03-18 19:58:02 +00:00
|
|
|
|
2023-07-28 21:47:54 +00:00
|
|
|
$this->assertFalse( $title->exists( Title::READ_LATEST ) );
|
|
|
|
|
$this->assertFalse( $watchlistManager->isWatched( $user, $title ) );
|
2018-01-14 09:52:57 +00:00
|
|
|
}
|
|
|
|
|
}
|