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