2016-08-03 15:41:50 +00:00
|
|
|
<?php
|
|
|
|
|
2024-02-22 15:31:00 +00:00
|
|
|
namespace MediaWiki\Extension\Notifications\Test;
|
|
|
|
|
2024-11-07 20:16:46 +00:00
|
|
|
use MediaWiki\Deferred\DeferredUpdates;
|
2022-11-12 07:19:00 +00:00
|
|
|
use MediaWiki\Extension\Notifications\DbFactory;
|
2022-11-02 20:47:04 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Mapper\NotificationMapper;
|
2024-11-26 02:38:43 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Model\Notification;
|
2023-12-11 15:33:08 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2024-02-22 15:31:00 +00:00
|
|
|
use MediaWikiIntegrationTestCase;
|
2024-04-12 19:50:43 +00:00
|
|
|
use Wikimedia\Rdbms\Platform\ISQLPlatform;
|
2022-11-02 20:47:04 +00:00
|
|
|
|
2016-08-03 15:41:50 +00:00
|
|
|
/**
|
|
|
|
* @group Echo
|
2018-09-12 20:31:46 +00:00
|
|
|
* @group Database
|
2016-08-03 15:41:50 +00:00
|
|
|
*/
|
2023-08-12 19:07:15 +00:00
|
|
|
class ThankYouEditTest extends MediaWikiIntegrationTestCase {
|
2016-08-03 15:41:50 +00:00
|
|
|
|
2017-03-22 09:32:30 +00:00
|
|
|
private function deleteEchoData() {
|
2022-11-12 07:19:00 +00:00
|
|
|
$db = DbFactory::newFromDefault()->getEchoDb( DB_PRIMARY );
|
2024-04-12 19:50:43 +00:00
|
|
|
$db->newDeleteQueryBuilder()
|
|
|
|
->deleteFrom( 'echo_event' )
|
|
|
|
->where( ISQLPlatform::ALL_ROWS )
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
->execute();
|
|
|
|
$db->newDeleteQueryBuilder()
|
|
|
|
->deleteFrom( 'echo_notification' )
|
|
|
|
->where( ISQLPlatform::ALL_ROWS )
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
->execute();
|
2016-08-03 15:41:50 +00:00
|
|
|
}
|
|
|
|
|
2024-11-26 02:38:43 +00:00
|
|
|
public function provideFirstEditRequestModes() {
|
|
|
|
return [
|
|
|
|
[ 'web' ],
|
|
|
|
[ 'cli' ]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2018-06-14 15:38:07 +00:00
|
|
|
/**
|
2022-04-08 00:28:15 +00:00
|
|
|
* @covers \MediaWiki\Extension\Notifications\Hooks::onPageSaveComplete
|
2024-11-26 02:38:43 +00:00
|
|
|
* @dataProvider provideFirstEditRequestModes
|
|
|
|
* @param string $mode
|
2018-06-14 15:38:07 +00:00
|
|
|
*/
|
2024-11-26 02:38:43 +00:00
|
|
|
public function testFirstEdit( $mode ) {
|
|
|
|
// TODO: re-renable once I50aa9fe9387c9b7b7ff97dfd39a2830bce647db8 is merged.
|
|
|
|
// That is, after, endAtomic() in PageUpdater::doCreate() is tweaked
|
|
|
|
if ( $mode === 'cli' ) {
|
|
|
|
$this->markTestSkipped();
|
|
|
|
}
|
|
|
|
|
2016-08-03 15:41:50 +00:00
|
|
|
// setup
|
2017-03-22 09:32:30 +00:00
|
|
|
$this->deleteEchoData();
|
2016-08-03 15:41:50 +00:00
|
|
|
$user = $this->getMutableTestUser()->getUser();
|
|
|
|
$title = Title::newFromText( 'Help:MWEchoThankYouEditTest_testFirstEdit' );
|
|
|
|
|
|
|
|
// action
|
2024-11-26 02:38:43 +00:00
|
|
|
$db = $this->getDb();
|
|
|
|
// Web requests wrap the edit in a broader transaction via DBO_TRX and commit it in
|
|
|
|
// MediaWikiEntryPoint::commitMainTransaction(). We can largely simulate that by just
|
|
|
|
// using atomic sections.
|
|
|
|
$useAtomicSection = ( $mode === 'web' );
|
|
|
|
if ( $useAtomicSection ) {
|
|
|
|
$db->startAtomic( __METHOD__ );
|
|
|
|
}
|
2023-07-15 20:32:16 +00:00
|
|
|
$this->editPage( $title, 'this is my first edit', '', NS_MAIN, $user );
|
2024-11-26 02:38:43 +00:00
|
|
|
if ( $useAtomicSection ) {
|
|
|
|
$db->endAtomic( __METHOD__ );
|
|
|
|
}
|
2024-11-07 20:16:46 +00:00
|
|
|
DeferredUpdates::tryOpportunisticExecute();
|
2016-08-03 15:41:50 +00:00
|
|
|
|
|
|
|
// assertions
|
2022-11-02 20:47:04 +00:00
|
|
|
$notificationMapper = new NotificationMapper();
|
2016-12-05 18:51:07 +00:00
|
|
|
$notifications = $notificationMapper->fetchByUser( $user, 10, null, [ 'thank-you-edit' ] );
|
2016-08-03 15:41:50 +00:00
|
|
|
$this->assertCount( 1, $notifications );
|
|
|
|
|
2022-11-02 21:34:17 +00:00
|
|
|
/** @var Notification $notification */
|
2016-08-03 15:41:50 +00:00
|
|
|
$notification = reset( $notifications );
|
2019-10-23 10:28:30 +00:00
|
|
|
$this->assertSame( 1, $notification->getEvent()->getExtraParam( 'editCount', 'not found' ) );
|
2016-08-03 15:41:50 +00:00
|
|
|
}
|
|
|
|
|
2024-11-26 02:38:43 +00:00
|
|
|
public function provideTenthEditRequestModes() {
|
|
|
|
return [
|
|
|
|
[ 'web' ],
|
|
|
|
[ 'cli' ]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2018-06-14 15:38:07 +00:00
|
|
|
/**
|
2022-04-08 00:28:15 +00:00
|
|
|
* @covers \MediaWiki\Extension\Notifications\Hooks::onPageSaveComplete
|
2024-11-26 02:38:43 +00:00
|
|
|
* @dataProvider provideTenthEditRequestModes
|
|
|
|
* @param string $mode
|
2018-06-14 15:38:07 +00:00
|
|
|
*/
|
2024-11-26 02:38:43 +00:00
|
|
|
public function testTenthEdit( $mode ) {
|
|
|
|
// TODO: re-renable once endAtomic() in PageUpdater::doCreate() is tweaked
|
|
|
|
if ( $mode === 'cli' ) {
|
|
|
|
$this->markTestSkipped();
|
|
|
|
}
|
|
|
|
|
2016-08-03 15:41:50 +00:00
|
|
|
// setup
|
2017-03-22 09:32:30 +00:00
|
|
|
$this->deleteEchoData();
|
2016-08-03 15:41:50 +00:00
|
|
|
$user = $this->getMutableTestUser()->getUser();
|
|
|
|
$title = Title::newFromText( 'Help:MWEchoThankYouEditTest_testTenthEdit' );
|
2023-07-15 20:32:16 +00:00
|
|
|
$page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
|
2016-08-03 15:41:50 +00:00
|
|
|
|
|
|
|
// action
|
|
|
|
// we could fast-forward the edit-count to speed things up
|
|
|
|
// but this is the only way to make sure duplicate notifications
|
|
|
|
// are not generated
|
2024-11-26 02:38:43 +00:00
|
|
|
$db = $this->getDb();
|
|
|
|
// Web requests wrap the edit in a broader transaction via DBO_TRX and commit it in
|
|
|
|
// MediaWikiEntryPoint::commitMainTransaction(). We can largely simulate that by just
|
|
|
|
// using atomic sections.
|
|
|
|
$useAtomicSection = ( $mode === 'web' );
|
2016-08-03 15:41:50 +00:00
|
|
|
for ( $i = 0; $i < 12; $i++ ) {
|
2024-11-26 02:38:43 +00:00
|
|
|
if ( $useAtomicSection ) {
|
|
|
|
$db->startAtomic( __METHOD__ );
|
|
|
|
}
|
2023-07-15 20:32:16 +00:00
|
|
|
$this->editPage( $page, "this is edit #$i", '', NS_MAIN, $user );
|
2024-11-26 02:38:43 +00:00
|
|
|
if ( $useAtomicSection ) {
|
|
|
|
$db->endAtomic( __METHOD__ );
|
|
|
|
}
|
|
|
|
DeferredUpdates::tryOpportunisticExecute();
|
2016-08-03 15:41:50 +00:00
|
|
|
}
|
2023-07-15 20:32:16 +00:00
|
|
|
$user->clearInstanceCache();
|
2016-08-03 15:41:50 +00:00
|
|
|
|
|
|
|
// assertions
|
2022-11-02 20:47:04 +00:00
|
|
|
$notificationMapper = new NotificationMapper();
|
2016-12-05 18:51:07 +00:00
|
|
|
$notifications = $notificationMapper->fetchByUser( $user, 10, null, [ 'thank-you-edit' ] );
|
2016-08-03 15:41:50 +00:00
|
|
|
$this->assertCount( 2, $notifications );
|
|
|
|
|
2022-11-02 21:34:17 +00:00
|
|
|
/** @var Notification $notification */
|
2016-08-03 15:41:50 +00:00
|
|
|
$notification = reset( $notifications );
|
2019-10-23 10:28:30 +00:00
|
|
|
$this->assertSame( 10, $notification->getEvent()->getExtraParam( 'editCount', 'not found' ) );
|
2016-08-03 15:41:50 +00:00
|
|
|
}
|
|
|
|
}
|