[MediaWiki-commits] [Gerrit] Unit testing for Newsletter extension - change (mediawiki...Newsletter)
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 000..f5f3b86 --- /dev/null +++ b/tests/ApiNewsletterTest.php @@ -0,0 +1,33 @@ +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 ___ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
[MediaWiki-commits] [Gerrit] Unit testing for Newsletter extension - change (mediawiki...Newsletter)
jenkins-bot has submitted this change and it was merged. Change subject: Unit testing for Newsletter extension .. Unit testing for Newsletter extension Version 1 of unit test to check the case of user trying to subscribe and unsubscribe to existing newsletters. Bug: T108202 Change-Id: I048e0feaf0879403bb7c4c28b076f323a17092e9 --- M Newsletter.hooks.php M Newsletter.php A tests/ApiNewsletterTest.php 3 files changed, 95 insertions(+), 0 deletions(-) Approvals: 01tonythomas: Looks good to me, approved jenkins-bot: Verified 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/Newsletter.php b/Newsletter.php index a5bb680..56c4237 100755 --- a/Newsletter.php +++ b/Newsletter.php @@ -76,3 +76,4 @@ $wgHooks['LoadExtensionSchemaUpdates'][] = 'NewsletterHooks::onLoadExtensionSchemaUpdates'; $wgHooks['BeforeCreateEchoEvent'][] = 'NewsletterHooks::onBeforeCreateEchoEvent'; $wgHooks['EchoGetDefaultNotifiedUsers'][] = 'NewsletterHooks::onEchoGetDefaultNotifiedUsers'; +$wgHooks['UnitTestsList'][] = 'NewsletterHooks::onUnitTestsList'; diff --git a/tests/ApiNewsletterTest.php b/tests/ApiNewsletterTest.php new file mode 100644 index 000..8475145 --- /dev/null +++ b/tests/ApiNewsletterTest.php @@ -0,0 +1,88 @@ +addToDatabase(); + + $rowData = array( + 'nl_name' => 'MyNewsletter', + 'nl_desc' => 'This is a newsletter', + 'nl_main_page_id' => 1, + 'nl_frequency' => 'monthly', + 'nl_owner_id' => $user->getId() + ); + $dbw->insert( 'nl_newsletters', $rowData, __METHOD__ ); + $this->tablesUsed = array( 'nl_newsletters' ); +} + + protected function getNewsletterId() { + $dbr = wfGetDB( DB_SLAVE ); + $res = $dbr->select( + 'nl_newsletters', + array( 'nl_id' ), + array( + 'nl_name' => 'MyNewsletter' + ), + __METHOD__ + ); + $newsletterId = null; + foreach ( $res as $row ) { + $newsletterId = $row->nl_id; + } + + return $newsletterId; + } + + function testApiNewsletterForSubscribingNewsletter() { + $this->doApiRequest( array( + 'action' => 'newsletterapi', + 'newsletterId' => $this->getNewsletterId(), + 'todo' => 'subscribe' + ) ); + + $dbr = wfGetDB( DB_SLAVE ); + $result = $dbr->selectRowCount( + 'nl_subscriptions', + array( 'subscriber_id' ), + array( + 'newsletter_id' => $this->getNewsletterId() + ), + __METHOD__ + ); + + $this->assertEquals( $result, 1 ); + } + + function testApiNewsletterForUnsubscribingNewsletter() { + $this->doApiRequest( array( + 'action' => 'newsletterapi', + 'newsletterId' => $this->getNewsletterId(), + 'todo' => 'unsubscribe' + ) ); + + $dbr = wfGetDB( DB_SLAVE ); + $result = $dbr->selectRowCount( + 'nl_subscriptions', + array( 'subscriber_id' ), + array( + 'newsletter_id' => $this->getNewsletterId() + ), + __METHOD__ + ); + + $this->assertEquals( $result, 0 ); + } +} \ 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: merged Gerrit-Change-Id: I048e0feaf0879403bb7c4c28b076f323a17092e9 Gerrit-PatchSet: 11 Gerrit-Project: mediawiki/extensions/Newsletter Gerrit-Branch: master Gerrit-Owner: Tinaj1234 Gerrit-Reviewer: 01tonythomas <01tonytho...@gmail.com> Gerrit-Reviewer: Hoo man Gerrit-Reviewer: Legoktm Gerrit-Reviewer: Qgil Gerrit-Reviewer: jenkins-bot <> ___ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits