aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/includes/mail/EmailNotificationTest.php
blob: 4718b635947b6dfccc9d48e625683f73f08bfae3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php

use MediaWiki\MainConfigNames;
use MediaWiki\Title\Title;

/**
 * @group Database
 * @group Mail
 * @covers \EmailNotification
 */
class EmailNotificationTest extends MediaWikiIntegrationTestCase {

	/** @var EmailNotification */
	protected $emailNotification;

	protected function setUp(): void {
		parent::setUp();

		$this->emailNotification = new EmailNotification();

		$this->overrideConfigValue( MainConfigNames::WatchlistExpiry, true );
	}

	public function testNotifyOnPageChange(): void {
		$store = $this->getServiceContainer()->getWatchedItemStore();

		// both Alice and Bob watch 'Foobar'
		$title = Title::makeTitle( NS_MAIN, 'Foobar' );
		$alice = $this->getTestSysop()->getUser();
		$store->addWatch( $alice, $title );
		$bob = $this->getTestUser()->getUser();
		$store->addWatch( $bob, $title );

		// Alice edits the page (doesn't actually have to edit in this test).
		// Bob (as in, not Alice) should have received an email notification.
		$row = (object)[
			'rc_timestamp' => '20200624000000',
			'rc_comment' => '',
			'rc_minor' => false,

			'rc_deleted' => false,
			'rc_last_oldid' => false,
			'rc_user' => $alice->getId(),
		];
		$rc = @RecentChange::newFromRow( $row );

		$sent = $this->emailNotification->notifyOnPageChange( $alice, $title, $rc );
		static::assertTrue( $sent );

		// Alice edits again, but Bob shouldn't be notified again
		// (only one email until Bob visits the page again).
		$sent = $this->emailNotification->notifyOnPageChange( $alice, $title, $rc );
		static::assertFalse( $sent );

		// Reset notification timestamp, simulating that Bob visited the page.
		$store->resetAllNotificationTimestampsForUser( $bob );

		// Bob re-watches temporarily. For testing purposes we use a past expiry,
		// so an email shouldn't be sent after Alice edits the page.
		$store->addWatch( $bob, $title, '20060123000000' );

		// Alice edits again, email should not be sent.
		$sent = $this->emailNotification->notifyOnPageChange( $alice, $title, $rc );
		static::assertFalse( $sent );
	}
}