1<?php
2
3namespace dokuwiki\test\Subscriptions;
4
5use dokuwiki\Subscriptions\BulkSubscriptionSender;
6use dokuwiki\Subscriptions\SubscriberManager;
7use dokuwiki\test\mock\MailerMock;
8use DokuWikiTest;
9
10class BulkSubscriptionsSenderTest extends DokuWikiTest
11{
12
13    private $originalSubscriptionConfig;
14
15    public function setUp() : void
16    {
17        parent::setUp();
18        global $conf;
19        $this->originalSubscriptionConfig = $conf['subscribers'];
20        $conf['subscribers'] = true;
21        $conf['mailfromnobody'] = 'phpunit@example.com';
22    }
23
24    protected function tearDown() : void
25    {
26        global $conf;
27        $conf['subscribers'] = $this->originalSubscriptionConfig;
28        parent::tearDown();
29    }
30
31    public function testBulkdigest()
32    {
33        $mailerMock = new MailerMock();
34        $sub = new BulkSubscriptionSender($mailerMock);
35        $manager = new SubscriberManager();
36
37        // let's start with nothing
38        $this->assertEquals(0, $sub->sendBulk('sub1:test'));
39
40        // create a subscription
41        $manager->add('sub1:', 'testuser', 'digest', '978328800'); // last mod 2001-01-01
42
43        // now create change
44        $_SERVER['REMOTE_USER'] = 'someguy';
45        saveWikiText('sub1:test', 'foo bar', 'a subscription change', false);
46
47        // should trigger a mail
48        $this->assertEquals(1, $sub->sendBulk('sub1:test'));
49        $this->assertEquals(['arthur@example.com'], array_column($mailerMock->mails, 'Bcc'));
50
51        $mailerMock->mails = [];
52
53        // now create more changes
54        $_SERVER['REMOTE_USER'] = 'someguy';
55        saveWikiText('sub1:sub2:test', 'foo bar', 'a subscription change', false);
56        saveWikiText('sub1:another_test', 'foo bar', 'a subscription change', false);
57
58        // should not trigger a mail, because the subscription time has not been reached, yet
59        $this->assertEquals(0, $sub->sendBulk('sub1:test'));
60        $this->assertEquals([], array_column($mailerMock->mails, 'Bcc'));
61
62        // reset the subscription time
63        $manager->add('sub1:', 'testuser', 'digest', '978328800'); // last mod 2001-01-01
64
65        // we now should get mails for three changes
66        $this->assertEquals(3, $sub->sendBulk('sub1:test'));
67        $this->assertEquals(
68            ['arthur@example.com', 'arthur@example.com', 'arthur@example.com'],
69            array_column($mailerMock->mails, 'Bcc')
70        );
71    }
72
73    public function testBulklist()
74    {
75        $mailerMock = new MailerMock();
76        $sub = new BulkSubscriptionSender($mailerMock);
77        $manager = new SubscriberManager();
78
79        // let's start with nothing
80        $this->assertEquals(0, $sub->sendBulk('sub1:test'));
81
82        // create a subscription
83        $manager->add('sub1:', 'testuser', 'list', '978328800'); // last mod 2001-01-01
84
85        // now create change
86        $_SERVER['REMOTE_USER'] = 'someguy';
87        saveWikiText('sub1:test', 'foo bar', 'a subscription change', false);
88
89        // should trigger a mail
90        $this->assertEquals(1, $sub->sendBulk('sub1:test'));
91        $this->assertEquals(['arthur@example.com'], array_column($mailerMock->mails, 'Bcc'));
92
93        $mailerMock->mails = [];
94
95        // now create more changes
96        $_SERVER['REMOTE_USER'] = 'someguy';
97        saveWikiText('sub1:sub2:test', 'foo bar', 'a subscription change', false);
98        saveWikiText('sub1:another_test', 'foo bar', 'a subscription change', false);
99
100        // should not trigger a mail, because the subscription time has not been reached, yet
101        $this->assertEquals(0, $sub->sendBulk('sub1:test'));
102        $this->assertEquals([], array_column($mailerMock->mails, 'Bcc'));
103
104        // reset the subscription time
105        $manager->add('sub1:', 'testuser', 'list', '978328800'); // last mod 2001-01-01
106
107        // we now should get a single mail for all three changes
108        $this->assertEquals(1, $sub->sendBulk('sub1:test'));
109        $this->assertEquals(['arthur@example.com'], array_column($mailerMock->mails, 'Bcc'));
110    }
111}
112