Adamw has submitted this change and it was merged.
Change subject: (FR #987) Fix Luhn Check for Odd Numbered Cards
......................................................................
(FR #987) Fix Luhn Check for Odd Numbered Cards
Silly logic errors...
Change-Id: I4ca0483d2fd33343106501b94f87a75960f5c05e
---
M gateway_common/DataValidator.php
A tests/DataValidatorTestCase.php
2 files changed, 102 insertions(+), 2 deletions(-)
Approvals:
Adamw: Verified; Looks good to me, approved
jenkins-bot: Checked
diff --git a/gateway_common/DataValidator.php b/gateway_common/DataValidator.php
index 26f6e60..30e7649 100644
--- a/gateway_common/DataValidator.php
+++ b/gateway_common/DataValidator.php
@@ -861,14 +861,14 @@
* @return bool True if the number was valid according to the algorithm
*/
public static function luhn_check( $str ) {
- $odd = !strlen( $str ) % 2;
+ $odd = (strlen( $str ) % 2);
$sum = 0;
for( $i = 0; $i < strlen( $str ); $i++ ) {
if ( $odd ) {
$sum += $str[$i];
} else {
- if (( $str[$i] * 2 ) > 9 ) {
+ if ( ( $str[$i] * 2 ) > 9 ) {
$sum += $str[$i] * 2 - 9;
} else {
$sum += $str[$i] * 2;
diff --git a/tests/DataValidatorTestCase.php b/tests/DataValidatorTestCase.php
new file mode 100644
index 0000000..c119fc5
--- /dev/null
+++ b/tests/DataValidatorTestCase.php
@@ -0,0 +1,100 @@
+<?php
+/**
+ * Wikimedia Foundation
+ *
+ * LICENSE
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+/**
+ * @group Fundraising
+ * @category UnitTesting
+ */
+class DataValidatorTestCase extends PHPUnit_Framework_TestCase {
+ /**
+ * Test the Luhn check algorithm
+ * @dataProvider luhnDataProvider
+ */
+ public function testLuhnCheck( $number, $expresult ) {
+ $result = DataValidator::cc_number_exists_in_str( $number );
+ $this->assertEquals( $expresult, $result, "$number failed
expected luhn check" );
+ }
+
+ public function luhnDataProvider() {
+ return array(
+ // Mastercard
+ array(5333331605740535, true),
+ array("5143792293131636", true),
+ array("John Doe 5199122553269905 Random", true),
+ array("5497-8801-7320-5943", true),
+ array("5370 5369 5295 3903", true),
+ array(5295975049354398, true),
+ array(5122728197617259, true),
+ array(5372869474419840, true),
+ array(5479089850576103, true),
+ array(5375122664558457, true),
+
+ // VISA array(16), digit
+ array(4024007145540307, true),
+ array(4532676809474030, true),
+ array(4024007139174626, true),
+ array(4556384391069166, true),
+ array(4916423001204113, true),
+ array(4716409516522919, true),
+ array(4296465885589572, true),
+ array(4532969094459737, true),
+ array(4485480938896362, true),
+ array(4539357366702682, true),
+
+ // VISA array(13), digit
+ array(4916199124929, true),
+ array(4916237697951, true),
+ array(4929247091115, true),
+ array(4024007169572, true),
+ array(4716716919391, true),
+
+ // American Express
+ array(343114236688284, true),
+ array(379274178561225, true),
+
+ // Discover
+ array(6011013905647431, true),
+ array(6011045341391380, true),
+ array(6011324325736120, true),
+
+ // Diners Club
+ array(30343484937451, true),
+ array(30037415730064, true),
+ array(30392872026500, true),
+
+ // enRoute
+ array(201454799826249, true),
+ array(201498205795993, true),
+ array(214960886496931, true),
+
+ // JCB
+ array(3582219461343499, true),
+ array(3534022982879267, true),
+ array(3519002211673029, true),
+
+ // Voyager
+ array(869952786819898, true),
+ array(869967184704708, true),
+ array(869901879171733, true),
+
+ // Not credit cards
+ array("John Doe", false),
+ array("Peter 123456", false),
+ array(1234567, false)
+ );
+ }
+}
\ No newline at end of file
--
To view, visit https://gerrit.wikimedia.org/r/72661
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I4ca0483d2fd33343106501b94f87a75960f5c05e
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/DonationInterface
Gerrit-Branch: master
Gerrit-Owner: Mwalker <[email protected]>
Gerrit-Reviewer: Adamw <[email protected]>
Gerrit-Reviewer: Katie Horn <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits