Tinaj1234 has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/231599

Change subject: Unit testing for Newsletter extension
......................................................................

Unit testing for Newsletter extension

Version 1 of unit test to check the case of unconfirmed user trying
to subscribe to newsletters.

Bug: T108202
Change-Id: I048e0feaf0879403bb7c4c28b076f323a17092e9
---
M Newsletter.hooks.php
M includes/ApiNewsletter.php
A tests/ApiNewsletterTest.php
3 files changed, 51 insertions(+), 5 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Newsletter 
refs/changes/99/231599/1

diff --git a/Newsletter.hooks.php b/Newsletter.hooks.php
index 0ccfac6..b25e66f 100755
--- a/Newsletter.hooks.php
+++ b/Newsletter.hooks.php
@@ -73,4 +73,10 @@
 
                return true;
        }
+
+       public static function onUnitTestsList( &$files ) {
+               $files = array_merge( $files, glob( __DIR__ . 
'/tests/*Test.php' ) );
+
+               return true;
+       }
 }
diff --git a/includes/ApiNewsletter.php b/includes/ApiNewsletter.php
index f93d8de..842ccf1 100644
--- a/includes/ApiNewsletter.php
+++ b/includes/ApiNewsletter.php
@@ -2,13 +2,19 @@
 
 class ApiNewsletter extends ApiBase {
        public function execute() {
+               $res = $this->getResult();
                $dbw = wfGetDB( DB_MASTER );
                if ( $this->getMain()->getVal( 'todo' ) === 'subscribe' ) {
-                       $rowData = array(
-                               'newsletter_id' => $this->getMain()->getVal( 
'newsletterId' ),
-                               'subscriber_id' => $this->getUser()->getId()
-                       );
-                       $dbw->insert( 'nl_subscriptions', $rowData, __METHOD__ 
);
+                       if ( $this->getMain()->getVal('newsletterId' ) && 
$this->getUser()->isEmailConfirmed() ) {
+                               $rowData = array(
+                                       'newsletter_id' => 
$this->getMain()->getVal( 'newsletterId' ),
+                                       'subscriber_id' => 
$this->getUser()->getId()
+                               );
+                               $dbw->insert( 'nl_subscriptions', $rowData, 
__METHOD__ );
+                               $res->addValue( 'newsletterapi', 'insert', 'ok' 
);
+                       } else {
+                               $res->addValue( 'newsletterapi', 'insert', 
'fail' );
+                       }
                }
 
                if ( $this->getMain()->getVal( 'todo' ) === 'unsubscribe' ) {
@@ -18,6 +24,7 @@
                        );
                        $dbw->delete( 'nl_subscriptions', $rowData, __METHOD__ 
);
                }
+
        }
 
        public function getAllowedParams() {
diff --git a/tests/ApiNewsletterTest.php b/tests/ApiNewsletterTest.php
new file mode 100644
index 0000000..f5f3b86
--- /dev/null
+++ b/tests/ApiNewsletterTest.php
@@ -0,0 +1,33 @@
+<?php
+/**
+ * Unit test to test Api module - ApiNewsletter
+ *
+ */
+
+class ApiNewsletterTest extends ApiTestCase {
+
+        public function testApiNewsletter() {
+                $subscribed = array();
+                $dbr = wfGetDB( DB_SLAVE );
+
+                $query = $dbr->select(
+                                'nl_subscriptions',
+                                array( 'newsletter_id', 'subscriber_id'),
+                                array( ),
+                                __METHOD__,
+                                array()
+                );
+
+                foreach ($query as $row) {
+                    $subscribed[$row->newsletter_id] = $row->subscriber_id;
+                }
+
+                list( $apiResult ) = $this->doApiRequest( array(
+                                    'action' => 'newsletterapi',
+                                    'newsletterId' => array_keys( end( 
$subscribed ) ),
+                                    'todo' => 'subscribe'
+                ) );
+
+                $this->assertEquals( 'ok', 
$apiResult['newsletterapi']['insert'] );
+        }
+}
\ No newline at end of file

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I048e0feaf0879403bb7c4c28b076f323a17092e9
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Newsletter
Gerrit-Branch: master
Gerrit-Owner: Tinaj1234 <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to