jenkins-bot has submitted this change and it was merged.

Change subject: Add tests for thank you unsubscribe
......................................................................


Add tests for thank you unsubscribe

Move queue instantiation into a helper class so tests see the same
instance.

Use load_composer_autoloader manifest directive instead of
require_once.

Bug: T145419
Change-Id: I1c357794fc857c3ee32ba1e4c1c61f6c44aa8c39
---
M SpecialFundraiserUnsubscribe.php
M extension.json
A includes/FundraiserEmailQueue.php
M methods/FundraiserUnsubscribeThankYou.php
A tests/phpunit/UnsubscribeThankYouTest.php
5 files changed, 92 insertions(+), 15 deletions(-)

Approvals:
  Awight: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/SpecialFundraiserUnsubscribe.php b/SpecialFundraiserUnsubscribe.php
index 7ad4f1d..221c152 100644
--- a/SpecialFundraiserUnsubscribe.php
+++ b/SpecialFundraiserUnsubscribe.php
@@ -68,9 +68,6 @@
                global $wgFundraisingEmailUnsubscribeHelpEmail;
 
                $templateDir = __DIR__ . '/templates';
-
-               require_once( __DIR__ . '/vendor/autoload.php' );
-
                // Initiate logging. Although we generate the ID every time, we 
will reset to a stashed ID
                // in loadSessionData() if it exists.
                Logger::setBucket( 'FundraisingEmailUnsubscribe' );
diff --git a/extension.json b/extension.json
index a1b939b..7cb7d93 100644
--- a/extension.json
+++ b/extension.json
@@ -18,6 +18,7 @@
        "AutoloadClasses": {
                "SpecialFundraiserUnsubscribe": 
"SpecialFundraiserUnsubscribe.php",
                "Logger": "includes/Logger.php",
+               "FundraiserEmailQueue": "includes/FundraiserEmailQueue.php",
                "MediaWikiTwig": "includes/MediaWikiTwig.php",
                "MediaWikiTwigCallbacks": "includes/MediaWikiTwig.php",
                "XmlTransactionProcessor": 
"includes/XmlTransactionProcessor.php",
@@ -84,5 +85,6 @@
                "TwigCachePath": "/tmp/twig",
                "TwigCacheExpiry": 300
        },
+       "load_composer_autoloader": true,
        "manifest_version": 1
 }
diff --git a/includes/FundraiserEmailQueue.php 
b/includes/FundraiserEmailQueue.php
new file mode 100644
index 0000000..545c1c6
--- /dev/null
+++ b/includes/FundraiserEmailQueue.php
@@ -0,0 +1,29 @@
+<?php
+
+/**
+ * Helper to fetch and stash a PHPQueue backend
+ * Basically just holds a static reference so tests can look at the same
+ * backend instance as the class being tested
+ */
+class FundraiserEmailQueue {
+       static $instance;
+
+       /**
+        * @return PHPQueue\Interfaces\FifoQueueStore
+        */
+       public static function get() {
+               global $wgFundraisingEmailUnsubscribeQueueClass,
+                          $wgFundraisingEmailUnsubscribeQueueParameters;
+
+               if ( !self::$instance ) {
+                       if ( empty( 
$wgFundraisingEmailUnsubscribeQueueParameters['queue'] ) ) {
+                               
$wgFundraisingEmailUnsubscribeQueueParameters['queue'] = 'unsubscribe';
+                       }
+                       self::$instance = new 
$wgFundraisingEmailUnsubscribeQueueClass(
+                               $wgFundraisingEmailUnsubscribeQueueParameters
+                       );
+               }
+
+               return self::$instance;
+       }
+}
diff --git a/methods/FundraiserUnsubscribeThankYou.php 
b/methods/FundraiserUnsubscribeThankYou.php
index 89c243f..5adba37 100644
--- a/methods/FundraiserUnsubscribeThankYou.php
+++ b/methods/FundraiserUnsubscribeThankYou.php
@@ -63,9 +63,6 @@
        }
 
        public function unsubscribe( $requestID, $process, array $params ) {
-               global $wgFundraisingEmailUnsubscribeQueueClass,
-                          $wgFundraisingEmailUnsubscribeQueueParameters;
-
                Logger::pushLabel( 'UnsubThankYou' );
 
                $email = $params['email'];
@@ -76,17 +73,10 @@
                        'email' => $email,
                        'contribution-id' => $contribId
                );
+
                // Send to the queue
                Logger::log( 'Placing message in queue for email ' . 
json_encode( $email ) );
-
-               if ( empty( 
$wgFundraisingEmailUnsubscribeQueueParameters['queue'] ) ) {
-                       $wgFundraisingEmailUnsubscribeQueueParameters['queue'] 
= 'unsubscribe';
-               }
-               $queue = new $wgFundraisingEmailUnsubscribeQueueClass(
-                       $wgFundraisingEmailUnsubscribeQueueParameters
-               );
-               // Throws exception if it is unsuccessful
-               $queue->push( $message );
+               FundraiserEmailQueue::get()->push( $message );
 
                // Clean up and return
                Logger::popLabel();
diff --git a/tests/phpunit/UnsubscribeThankYouTest.php 
b/tests/phpunit/UnsubscribeThankYouTest.php
new file mode 100644
index 0000000..518b092
--- /dev/null
+++ b/tests/phpunit/UnsubscribeThankYouTest.php
@@ -0,0 +1,59 @@
+<?php
+
+/**
+ * @group FundraisingEmailUnsubscribe
+ */
+class UnsubscribeThankYouTest extends MediaWikiTestCase {
+       /**
+        * @var FundraiserUnsubscribeThankYou
+        */
+       protected $unsubscriber;
+
+       public function setUp() {
+               parent::setUp();
+               $this->setMwGlobals( array(
+                       'wgFundraisingEmailUnsubscribeQueueClass' => 
'\PHPQueue\Backend\PDO',
+                       'wgFundraisingEmailUnsubscribeQueueParameters' => array(
+                               'connection_string' => 'sqlite::memory:',
+                       ),
+                       'wgFundraisingEmailUnsubscribeHashSecretKey' => 
'Red/Fl4nn3l/#',
+               ) );
+               $this->unsubscriber = new FundraiserUnsubscribeThankYou();
+       }
+
+       public function testValidateBadHash() {
+               $result = $this->unsubscriber->validateRequest( array(
+                       'email' => 'peste...@example.com',
+                       'contribution-id' => '98765432',
+                       'hash' => 'a76f8eg86b87eg109c0d983d6a0e9a6d827e79c7',
+               ) );
+               $this->assertFalse( $result, 'Bogus hash considered valid' );
+       }
+
+       public function testValidateGoodHash() {
+               $result = $this->unsubscriber->validateRequest( array(
+                       'email' => 'peste...@example.com',
+                       'contribution-id' => '98765432',
+                       'hash' => '87b7b93fffbe72b61261e59941e3b6628b0e5689',
+               ) );
+               $this->assertTrue( $result, 'Good hash considered invalid' );
+       }
+
+       public function testSendMessage() {
+               $id = mt_rand();
+               $this->unsubscriber->unsubscribe(
+                       $id, 'unsubscribe', array(
+                               'email' => 'do...@example.com',
+                               'contribution-id' => '123456'
+                       )
+               );
+               $queue = FundraiserEmailQueue::get();
+               $message = $queue->pop();
+               $this->assertNotNull( $message );
+               $this->assertEquals( array(
+                       'email' => 'do...@example.com',
+                       'contribution-id' => '123456',
+                       'process' => 'unsubscribe',
+               ), $message );
+       }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/311174
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I1c357794fc857c3ee32ba1e4c1c61f6c44aa8c39
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/FundraisingEmailUnsubscribe
Gerrit-Branch: master
Gerrit-Owner: Ejegg <eeggles...@wikimedia.org>
Gerrit-Reviewer: AndyRussG <andrew.green...@gmail.com>
Gerrit-Reviewer: Awight <awi...@wikimedia.org>
Gerrit-Reviewer: Cdentinger <cdentin...@wikimedia.org>
Gerrit-Reviewer: Eileen <emcnaugh...@wikimedia.org>
Gerrit-Reviewer: Katie Horn <kh...@wikimedia.org>
Gerrit-Reviewer: Pcoombe <pcoo...@wikimedia.org>
Gerrit-Reviewer: XenoRyet <dkozlow...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to