diff options
author | Andreas Gohr <andi@splitbrain.org> | 2019-05-19 10:48:22 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-19 10:48:22 +0200 |
commit | b8c09b6854c4210a9c9fef089a48f5320420d469 (patch) | |
tree | 75380e7d4dce97ebd1c8560e18cc26535c48a232 /_test | |
parent | 749c002394774d4323466fa0c9370aa913b1849d (diff) | |
parent | 451969ab7559b14e82720d165019a94010752965 (diff) | |
download | dokuwiki-b8c09b6854c4210a9c9fef089a48f5320420d469.tar.gz dokuwiki-b8c09b6854c4210a9c9fef089a48f5320420d469.zip |
Merge pull request #2759 from splitbrain/refactorSubscriptions
Refactor subscriptions
Diffstat (limited to '_test')
-rw-r--r-- | _test/mock/MailerMock.php | 16 | ||||
-rw-r--r-- | _test/tests/inc/Subscriptions/BulkSubscriptionsSenderTest.php | 110 | ||||
-rw-r--r-- | _test/tests/inc/Subscriptions/SubscriberManagerTest.php | 131 | ||||
-rw-r--r-- | _test/tests/inc/Subscriptions/SubscriberRegexBuilderTest.php | 52 | ||||
-rw-r--r-- | _test/tests/inc/subscription.test.php | 246 |
5 files changed, 309 insertions, 246 deletions
diff --git a/_test/mock/MailerMock.php b/_test/mock/MailerMock.php new file mode 100644 index 000000000..9d93bc25a --- /dev/null +++ b/_test/mock/MailerMock.php @@ -0,0 +1,16 @@ +<?php + +namespace dokuwiki\test\mock; + +class MailerMock extends \Mailer +{ + + public $mails = []; + + public function send() + { + $this->mails[] = $this->headers; + return true; + } + +} diff --git a/_test/tests/inc/Subscriptions/BulkSubscriptionsSenderTest.php b/_test/tests/inc/Subscriptions/BulkSubscriptionsSenderTest.php new file mode 100644 index 000000000..3bc6a9c2f --- /dev/null +++ b/_test/tests/inc/Subscriptions/BulkSubscriptionsSenderTest.php @@ -0,0 +1,110 @@ +<?php + +namespace tests\inc\Subscriptions; + +use dokuwiki\Subscriptions\BulkSubscriptionSender; +use dokuwiki\Subscriptions\SubscriberManager; +use dokuwiki\test\mock\MailerMock; +use DokuWikiTest; + +class BulkSubscriptionsSenderTest extends DokuWikiTest +{ + + private $originalSubscriptionConfig; + + public function setUp() + { + parent::setUp(); + global $conf; + $this->originalSubscriptionConfig = $conf['subscribers']; + $conf['subscribers'] = true; + } + + protected function tearDown() + { + global $conf; + $conf['subscribers'] = $this->originalSubscriptionConfig; + parent::tearDown(); + } + + public function testBulkdigest() + { + $mailerMock = new MailerMock(); + $sub = new BulkSubscriptionSender($mailerMock); + $manager = new SubscriberManager(); + + // let's start with nothing + $this->assertEquals(0, $sub->sendBulk('sub1:test')); + + // create a subscription + $manager->add('sub1:', 'testuser', 'digest', '978328800'); // last mod 2001-01-01 + + // now create change + $_SERVER['REMOTE_USER'] = 'someguy'; + saveWikiText('sub1:test', 'foo bar', 'a subscription change', false); + + // should trigger a mail + $this->assertEquals(1, $sub->sendBulk('sub1:test')); + $this->assertEquals(['arthur@example.com'], array_column($mailerMock->mails, 'Bcc')); + + $mailerMock->mails = []; + + // now create more changes + $_SERVER['REMOTE_USER'] = 'someguy'; + saveWikiText('sub1:sub2:test', 'foo bar', 'a subscription change', false); + saveWikiText('sub1:another_test', 'foo bar', 'a subscription change', false); + + // should not trigger a mail, because the subscription time has not been reached, yet + $this->assertEquals(0, $sub->sendBulk('sub1:test')); + $this->assertEquals([], array_column($mailerMock->mails, 'Bcc')); + + // reset the subscription time + $manager->add('sub1:', 'testuser', 'digest', '978328800'); // last mod 2001-01-01 + + // we now should get mails for three changes + $this->assertEquals(3, $sub->sendBulk('sub1:test')); + $this->assertEquals( + ['arthur@example.com', 'arthur@example.com', 'arthur@example.com'], + array_column($mailerMock->mails, 'Bcc') + ); + } + + public function testBulklist() + { + $mailerMock = new MailerMock(); + $sub = new BulkSubscriptionSender($mailerMock); + $manager = new SubscriberManager(); + + // let's start with nothing + $this->assertEquals(0, $sub->sendBulk('sub1:test')); + + // create a subscription + $manager->add('sub1:', 'testuser', 'list', '978328800'); // last mod 2001-01-01 + + // now create change + $_SERVER['REMOTE_USER'] = 'someguy'; + saveWikiText('sub1:test', 'foo bar', 'a subscription change', false); + + // should trigger a mail + $this->assertEquals(1, $sub->sendBulk('sub1:test')); + $this->assertEquals(['arthur@example.com'], array_column($mailerMock->mails, 'Bcc')); + + $mailerMock->mails = []; + + // now create more changes + $_SERVER['REMOTE_USER'] = 'someguy'; + saveWikiText('sub1:sub2:test', 'foo bar', 'a subscription change', false); + saveWikiText('sub1:another_test', 'foo bar', 'a subscription change', false); + + // should not trigger a mail, because the subscription time has not been reached, yet + $this->assertEquals(0, $sub->sendBulk('sub1:test')); + $this->assertEquals([], array_column($mailerMock->mails, 'Bcc')); + + // reset the subscription time + $manager->add('sub1:', 'testuser', 'list', '978328800'); // last mod 2001-01-01 + + // we now should get a single mail for all three changes + $this->assertEquals(1, $sub->sendBulk('sub1:test')); + $this->assertEquals(['arthur@example.com'], array_column($mailerMock->mails, 'Bcc')); + } +} diff --git a/_test/tests/inc/Subscriptions/SubscriberManagerTest.php b/_test/tests/inc/Subscriptions/SubscriberManagerTest.php new file mode 100644 index 000000000..1fcd7c84b --- /dev/null +++ b/_test/tests/inc/Subscriptions/SubscriberManagerTest.php @@ -0,0 +1,131 @@ +<?php + +namespace tests\inc\Subscriptions; + +use dokuwiki\Subscriptions\SubscriberManager; +use DokuWikiTest; + +class SubscriberManagerTest extends DokuWikiTest +{ + private $originalSubscriptionConfig; + + public function setUp() + { + parent::setUp(); + global $conf; + $this->originalSubscriptionConfig = $conf['subscribers']; + $conf['subscribers'] = true; + } + + protected function tearDown() + { + global $conf; + $conf['subscribers'] = $this->originalSubscriptionConfig; + parent::tearDown(); + } + + public function testAddremove() + { + $sub = new SubscriberManager(); + + // no subscriptions + $this->assertArrayNotHasKey( + 'wiki:dokuwiki', + $sub->subscribers('wiki:dokuwiki', null, ['every', 'list', 'digest']) + ); + + // add page subscription + $sub->add('wiki:dokuwiki', 'testuser', 'every'); + + // one subscription + $this->assertArrayHasKey( + 'wiki:dokuwiki', + $sub->subscribers('wiki:dokuwiki', null, ['every', 'list', 'digest']) + ); + + // remove page subscription + $sub->remove('wiki:dokuwiki', 'testuser'); + + // no subscription + $this->assertArrayNotHasKey( + 'wiki:dokuwiki', + $sub->subscribers('wiki:dokuwiki', null, ['every', 'list', 'digest']) + ); + + // add namespace subscription + $sub->add('wiki:', 'testuser', 'every'); + + // one subscription + $this->assertArrayHasKey( + 'wiki:', + $sub->subscribers('wiki:dokuwiki', null, ['every', 'list', 'digest']) + ); + + // remove (non existing) page subscription + $sub->remove('wiki:dokuwiki', 'testuser'); + + // still one subscription + $this->assertArrayHasKey( + 'wiki:', + $sub->subscribers('wiki:dokuwiki', null, ['every', 'list', 'digest']) + ); + + // change namespace subscription + $sub->add('wiki:', 'testuser', 'digest', '1234567'); + + // still one subscription + $this->assertArrayHasKey( + 'wiki:', + $sub->subscribers('wiki:dokuwiki', null, ['every', 'list', 'digest']) + ); + + // check contents + $this->assertEquals( + ['wiki:' => ['testuser' => ['digest', '1234567']]], + $sub->subscribers('wiki:dokuwiki', null, ['every', 'list', 'digest']) + ); + + // change subscription data + $sub->add('wiki:', 'testuser', 'digest', '7654321'); + + // still one subscription + $this->assertArrayHasKey( + 'wiki:', + $sub->subscribers('wiki:dokuwiki', null, ['every', 'list', 'digest']) + ); + + // check contents + $this->assertEquals( + ['wiki:' => ['testuser' => ['digest', '7654321']]], + $sub->subscribers('wiki:dokuwiki', null, ['every', 'list', 'digest']) + ); + } + + /** + * Tests, if overwriting subscriptions works even when subscriptions for the same + * user exist for two nested namespaces, this is a test for the bug described in FS#2580 + */ + public function testOverwrite() + { + $sub = new SubscriberManager(); + + $sub->add(':', 'admin', 'digest', '123456789'); + $sub->add(':wiki:', 'admin', 'digest', '123456789'); + $sub->add(':', 'admin', 'digest', '1234'); + $sub->add(':wiki:', 'admin', 'digest', '1234'); + + $subscriptions = $sub->subscribers(':wiki:', 'admin'); + + $this->assertCount( + 1, + $subscriptions[':'], + 'More than one subscription saved for the root namespace even though the old one should have been overwritten.' + ); + $this->assertCount( + 1, + $subscriptions[':wiki:'], + 'More than one subscription saved for the wiki namespace even though the old one should have been overwritten.' + ); + $this->assertCount(2, $subscriptions, 'Didn\'t find the expected two subscriptions'); + } +} diff --git a/_test/tests/inc/Subscriptions/SubscriberRegexBuilderTest.php b/_test/tests/inc/Subscriptions/SubscriberRegexBuilderTest.php new file mode 100644 index 000000000..f9b22724d --- /dev/null +++ b/_test/tests/inc/Subscriptions/SubscriberRegexBuilderTest.php @@ -0,0 +1,52 @@ +<?php + +namespace tests\inc\Subscriptions; + +use dokuwiki\Subscriptions\SubscriberRegexBuilder; +use DokuWikiTest; + +class SubscriberRegexBuilderTest extends DokuWikiTest +{ + + public function regexTestdataProvider() + { + return [ + ['Cold Fusion', null, null, 1], + ['casper', null, null, 1], + ['nope', null, null, 0], + ['lights', null, null, 0], + [['Cold Fusion', 'casper', 'nope'], null, null, 2], + [null, 'list', null, 1], + [null, 'every', null, 2], + [null, 'digest', null, 3], + [null, ['list', 'every'], null, 3], + ['casper', 'digest', null, 0], + ['casper', ['digest', 'every'], null, 1], + ['zioth', 'list', '1344691369', 1], + ['zioth', null, '1344691369', 1], + ['zioth', 'digest', '1344691369', 0], + ]; + } + + /** + * @dataProvider regexTestdataProvider + */ + public function testRegexp($user, $style, $inputData, $expectedNumResults) + { + // data to test against + $data = [ + "casper every\n", + "Andreas digest 1344689733", + "Cold%20Fusion every", + "zioth list 1344691369\n", + "nlights digest", + "rikblok\tdigest \t 1344716803", + ]; + + $sub = new SubscriberRegexBuilder(); + $re = $sub->buildRegex($user, $style, $inputData); + $this->assertFalse(empty($re)); + $result = preg_grep($re, $data); + $this->assertEquals($expectedNumResults, count($result)); + } +} diff --git a/_test/tests/inc/subscription.test.php b/_test/tests/inc/subscription.test.php deleted file mode 100644 index 34a7b9e4b..000000000 --- a/_test/tests/inc/subscription.test.php +++ /dev/null @@ -1,246 +0,0 @@ -<?php - -class subscription_test extends DokuWikiTest { - - function test_regexp() { - // data to test against - $data = array( - "casper every\n", - "Andreas digest 1344689733", - "Cold%20Fusion every", - "zioth list 1344691369\n", - "nlights digest", - "rikblok\tdigest \t 1344716803", - ); - - // user, style, data, expected number of results - $tests = array( - array('Cold Fusion', null, null, 1), - array('casper', null, null, 1), - array('nope', null, null, 0), - array('lights', null, null, 0), - array(array('Cold Fusion', 'casper', 'nope'), null, null, 2), - array(null, 'list', null, 1), - array(null, 'every', null, 2), - array(null, 'digest', null, 3), - array(null, array('list', 'every'), null, 3), - array('casper', 'digest', null, 0), - array('casper', array('digest', 'every'), null, 1), - array('zioth', 'list', '1344691369', 1), - array('zioth', null, '1344691369', 1), - array('zioth', 'digest', '1344691369', 0), - ); - - $sub = new MockupSubscription(); - - $row = 0; - foreach($tests as $test) { - $re = $sub->buildregex($test[0], $test[1], $test[2]); - $this->assertFalse(empty($re), "test line $row"); - $result = preg_grep($re, $data); - $this->assertEquals($test[3], count($result), "test line $row. $re got\n".print_r($result, true)); - - $row++; - } - } - - function test_addremove() { - $sub = new MockupSubscription(); - - // no subscriptions - $this->assertArrayNotHasKey( - 'wiki:dokuwiki', - $sub->subscribers('wiki:dokuwiki', null, array('every', 'list', 'digest')) - ); - - // add page subscription - $sub->add('wiki:dokuwiki', 'testuser', 'every'); - - // one subscription - $this->assertArrayHasKey( - 'wiki:dokuwiki', - $sub->subscribers('wiki:dokuwiki', null, array('every', 'list', 'digest')) - ); - - // remove page subscription - $sub->remove('wiki:dokuwiki', 'testuser'); - - // no subscription - $this->assertArrayNotHasKey( - 'wiki:dokuwiki', - $sub->subscribers('wiki:dokuwiki', null, array('every', 'list', 'digest')) - ); - - // add namespace subscription - $sub->add('wiki:', 'testuser', 'every'); - - // one subscription - $this->assertArrayHasKey( - 'wiki:', - $sub->subscribers('wiki:dokuwiki', null, array('every', 'list', 'digest')) - ); - - // remove (non existing) page subscription - $sub->remove('wiki:dokuwiki', 'testuser'); - - // still one subscription - $this->assertArrayHasKey( - 'wiki:', - $sub->subscribers('wiki:dokuwiki', null, array('every', 'list', 'digest')) - ); - - // change namespace subscription - $sub->add('wiki:', 'testuser', 'digest', '1234567'); - - // still one subscription - $this->assertArrayHasKey( - 'wiki:', - $sub->subscribers('wiki:dokuwiki', null, array('every', 'list', 'digest')) - ); - - // check contents - $this->assertEquals( - array('wiki:' => array('testuser' => array('digest', '1234567'))), - $sub->subscribers('wiki:dokuwiki', null, array('every', 'list', 'digest')) - ); - - // change subscription data - $sub->add('wiki:', 'testuser', 'digest', '7654321'); - - // still one subscription - $this->assertArrayHasKey( - 'wiki:', - $sub->subscribers('wiki:dokuwiki', null, array('every', 'list', 'digest')) - ); - - // check contents - $this->assertEquals( - array('wiki:' => array('testuser' => array('digest', '7654321'))), - $sub->subscribers('wiki:dokuwiki', null, array('every', 'list', 'digest')) - ); - } - - function test_bulkdigest() { - $sub = new MockupSubscription(); - - // let's start with nothing - $this->assertEquals(0, $sub->send_bulk('sub1:test')); - - // create a subscription - $sub->add('sub1:', 'testuser', 'digest', '978328800'); // last mod 2001-01-01 - - // now create change - $_SERVER['REMOTE_USER'] = 'someguy'; - saveWikiText('sub1:test', 'foo bar', 'a subscription change', false); - - // should trigger a mail - $this->assertEquals(1, $sub->send_bulk('sub1:test')); - $this->assertEquals(array('arthur@example.com'), $sub->mails); - - $sub->reset(); - - // now create more changes - $_SERVER['REMOTE_USER'] = 'someguy'; - saveWikiText('sub1:sub2:test', 'foo bar', 'a subscription change', false); - saveWikiText('sub1:another_test', 'foo bar', 'a subscription change', false); - - // should not trigger a mail, because the subscription time has not been reached, yet - $this->assertEquals(0, $sub->send_bulk('sub1:test')); - $this->assertEquals(array(), $sub->mails); - - // reset the subscription time - $sub->add('sub1:', 'testuser', 'digest', '978328800'); // last mod 2001-01-01 - - // we now should get mails for three changes - $this->assertEquals(3, $sub->send_bulk('sub1:test')); - $this->assertEquals(array('arthur@example.com', 'arthur@example.com', 'arthur@example.com'), $sub->mails); - } - - function test_bulklist() { - $sub = new MockupSubscription(); - - // let's start with nothing - $this->assertEquals(0, $sub->send_bulk('sub1:test')); - - // create a subscription - $sub->add('sub1:', 'testuser', 'list', '978328800'); // last mod 2001-01-01 - - // now create change - $_SERVER['REMOTE_USER'] = 'someguy'; - saveWikiText('sub1:test', 'foo bar', 'a subscription change', false); - - // should trigger a mail - $this->assertEquals(1, $sub->send_bulk('sub1:test')); - $this->assertEquals(array('arthur@example.com'), $sub->mails); - - $sub->reset(); - - // now create more changes - $_SERVER['REMOTE_USER'] = 'someguy'; - saveWikiText('sub1:sub2:test', 'foo bar', 'a subscription change', false); - saveWikiText('sub1:another_test', 'foo bar', 'a subscription change', false); - - // should not trigger a mail, because the subscription time has not been reached, yet - $this->assertEquals(0, $sub->send_bulk('sub1:test')); - $this->assertEquals(array(), $sub->mails); - - // reset the subscription time - $sub->add('sub1:', 'testuser', 'list', '978328800'); // last mod 2001-01-01 - - // we now should get a single mail for all three changes - $this->assertEquals(1, $sub->send_bulk('sub1:test')); - $this->assertEquals(array('arthur@example.com'), $sub->mails); - } - - /** - * Tests, if overwriting subscriptions works even when subscriptions for the same - * user exist for two nested namespaces, this is a test for the bug described in FS#2580 - */ - function test_overwrite() { - $sub = new MockupSubscription(); - - $sub->add(':', 'admin', 'digest', '123456789'); - $sub->add(':wiki:', 'admin', 'digest', '123456789'); - $sub->add(':', 'admin', 'digest', '1234'); - $sub->add(':wiki:', 'admin', 'digest', '1234'); - - $subscriptions = $sub->subscribers(':wiki:', 'admin'); - - $this->assertCount(1, $subscriptions[':'], 'More than one subscription saved for the root namespace even though the old one should have been overwritten.'); - $this->assertCount(1, $subscriptions[':wiki:'], 'More than one subscription saved for the wiki namespace even though the old one should have been overwritten.'); - $this->assertCount(2, $subscriptions, 'Didn\'t find the expected two subscriptions'); - } -} - -/** - * makes protected methods visible for testing - */ -class MockupSubscription extends Subscription { - public $mails; // we keep sent mails here - - public function __construct() { - $this->reset(); - } - - /** - * resets the mail array - */ - public function reset() { - $this->mails = array(); - } - - public function isenabled() { - return true; - } - - public function buildregex($user = null, $style = null, $data = null) { - return parent::buildregex($user, $style, $data); - } - - protected function send($subscriber_mail, $subject, $id, $template, $trep, $hrep = null, $headers = array()) { - $this->mails[] = $subscriber_mail; - return true; - } -} - -//Setup VIM: ex: et ts=4 : |