first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+81
View File
@@ -0,0 +1,81 @@
@core @core_payment
Feature: Manage payment accounts
@javascript
Scenario: Creating and editing payment account
When I log in as "admin"
And I navigate to "Payments > Payment accounts" in site administration
And I follow "Manage payment gateways"
Then "Australian Dollar" "text" should exist in the "PayPal" "table_row"
And I follow "Payment accounts"
And I press "Create payment account"
And I set the field "Account name" to "TestAccount"
And I press "Save changes"
And I should see "PayPal" in the "TestAccount" "table_row"
And I open the action menu in "TestAccount" "table_row"
And I choose "Edit" in the open action menu
And I set the field "Account name" to "NewName"
And I press "Save changes"
And I should not see "TestAccount"
And I should see "PayPal" in the "NewName" "table_row"
@javascript
Scenario: Configuring gateways on payment accounts
Given the following "core_payment > payment accounts" exist:
| name |
| Account1 |
| Account2 |
When I log in as "admin"
And I navigate to "Payments > Payment accounts" in site administration
Then I should see "Not available" in the "Account1" "table_row"
And I click on "PayPal" "link" in the "Account1" "table_row"
And I set the field "Brand name" to "Test paypal"
And I set the following fields to these values:
| Brand name | Test paypal |
| Client ID | Test |
| Secret | Test |
| Enable | 1 |
And I press "Save changes"
And I should not see "Not available" in the "Account1" "table_row"
And I should see "PayPal" in the "Account1" "table_row"
@javascript
Scenario: Deleting payment accounts
Given the following "core_payment > payment accounts" exist:
| name |
| Account1 |
| Account2 |
When I log in as "admin"
And I navigate to "Payments > Payment accounts" in site administration
And I open the action menu in "Account1" "table_row"
And I choose "Delete or archive" in the open action menu
And I click on "Yes" "button" in the "Confirm" "dialogue"
Then I should not see "Account1"
And I should see "Account2"
@javascript
Scenario: Archiving and restoring accounts
Given the following "users" exist:
| username |
| user1 |
And the following "core_payment > payment accounts" exist:
| name |
| Account1 |
| Account2 |
And the following "core_payment > payments" exist:
| account | component | amount | user |
| Account1 | test | 10 | user1 |
| Account1 | test | 15 | user1 |
When I log in as "admin"
And I navigate to "Payments > Payment accounts" in site administration
And I open the action menu in "Account1" "table_row"
And I choose "Delete or archive" in the open action menu
And I click on "Yes" "button" in the "Confirm" "dialogue"
Then I should not see "Account1"
And I should see "Account2"
And I follow "Show archived"
And I should see "Archived" in the "Account1" "table_row"
And I open the action menu in "Account1" "table_row"
And I choose "Restore" in the open action menu
And I should not see "Archived" in the "Account1" "table_row"
And I log out
@@ -0,0 +1,19 @@
@core @core_payment @javascript
Feature: Verify the breadcrumbs in payment accounts site administration pages
Whenever I navigate to payment account page in site administration
As an admin
The breadcrumbs should be visible
Background:
Given I log in as "admin"
Scenario: Verify the breadcrumbs in payment account page as an admin
Given I navigate to "Payments > Payment accounts" in site administration
And I click on "Create payment account" "button"
And "Create payment account" "text" should exist in the ".breadcrumb" "css_element"
And "Payment accounts" "link" should exist in the ".breadcrumb" "css_element"
And "Payments" "link" should exist in the ".breadcrumb" "css_element"
And I press "Cancel"
When I click on "Show archived" "link"
Then "Payment accounts" "text" should exist in the ".breadcrumb" "css_element"
And "Payments" "link" should exist in the ".breadcrumb" "css_element"
@@ -0,0 +1,72 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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 3 of the License, or
// (at your option) any later version.
//
// Moodle 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.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Behat data generator for core_payment.
*
* @package core_payment
* @category test
* @copyright 2020 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Behat data generator for core_payment.
*
* @package core_payment
* @category test
* @copyright 2020 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_core_payment_generator extends behat_generator_base {
/**
* Get a list of the entities that can be created.
*
* @return array entity name => information about how to generate.
*/
protected function get_creatable_entities(): array {
return [
'payment accounts' => [
'singular' => 'payment account',
'datagenerator' => 'payment_account',
'required' => ['name'],
],
'payments' => [
'singular' => 'payment',
'datagenerator' => 'payment',
'required' => ['account', 'amount', 'user'],
'switchids' => ['account' => 'accountid', 'user' => 'userid'],
],
];
}
/**
* Look up the id of a account from its name.
*
* @param string $accountname
* @return int corresponding id.
*/
protected function get_account_id(string $accountname): int {
global $DB;
if (!$id = $DB->get_field('payment_accounts', 'id', ['name' => $accountname])) {
throw new Exception('There is no account with name "' . $accountname . '".');
}
return $id;
}
}
+82
View File
@@ -0,0 +1,82 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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 3 of the License, or
// (at your option) any later version.
//
// Moodle 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.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Payment module test data generator class
*
* @package core_payment
* @category test
* @copyright 2020 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_payment_generator extends component_generator_base {
/** @var int */
protected $accountcounter = 0;
/**
* Create a payment account
*
* @param array $data account data (name, idnumber, enabled) and additionally field 'gateways' that can include
* a list of gateways that should be mock-enabled for this account.
*/
public function create_payment_account(array $data = []): \core_payment\account {
$this->accountcounter++;
$gateways = [];
if (!empty($data['gateways'])) {
$gateways = preg_split('/,/', $data['gateways']);
}
unset($data['gateways']);
$account = \core_payment\helper::save_payment_account(
(object)($data + ['name' => 'Test '.$this->accountcounter, 'idnumber' => '', 'enabled' => 1]));
foreach ($gateways as $gateway) {
\core_payment\helper::save_payment_gateway(
(object)['accountid' => $account->get('id'), 'gateway' => $gateway, 'enabled' => 1]);
}
return $account;
}
/**
* Create a payment account
*
* @param array $data
*/
public function create_payment(array $data): int {
global $DB;
if (empty($data['accountid']) || !\core_payment\account::get_record(['id' => $data['accountid']])) {
throw new coding_exception('Account id is not specified or does not exist');
}
if (empty($data['amount'])) {
throw new coding_exception('Amount must be specified');
}
$gateways = \core\plugininfo\paygw::get_enabled_plugins();
if (empty($data['gateway'])) {
$data['gateway'] = reset($gateways);
}
$id = $DB->insert_record('payments', $data +
[
'component' => 'testcomponent',
'paymentarea' => 'teatarea',
'itemid' => 0,
'currency' => 'AUD',
]);
return $id;
}
}
+72
View File
@@ -0,0 +1,72 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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 3 of the License, or
// (at your option) any later version.
//
// Moodle 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.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Testing generator in payments API
*
* @package core_payment
* @category test
* @copyright 2020 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_payment;
/**
* Testing generator in payments API
*
* @package core_payment
* @category test
* @copyright 2020 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class generator_test extends \advanced_testcase {
public function test_create_account(): void {
global $DB;
$this->resetAfterTest();
/** @var \core_payment_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_payment');
$this->assertTrue($generator instanceof \core_payment_generator);
$account1 = $generator->create_payment_account();
$account2 = $generator->create_payment_account(['name' => 'My name', 'gateways' => 'paypal']);
$record1 = $DB->get_record('payment_accounts', ['id' => $account1->get('id')]);
$record2 = $DB->get_record('payment_accounts', ['id' => $account2->get('id')]);
$this->assertEquals(1, $record1->enabled);
$this->assertEquals('My name', $record2->name);
// First account does not have gateways configurations.
$this->assertEmpty($DB->get_records('payment_gateways', ['accountid' => $account1->get('id')]));
// Second account has.
$this->assertCount(1, $DB->get_records('payment_gateways', ['accountid' => $account2->get('id')]));
}
public function test_create_payment(): void {
global $DB;
$this->resetAfterTest();
/** @var \core_payment_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_payment');
$account = $generator->create_payment_account(['gateways' => 'paypal']);
$user = $this->getDataGenerator()->create_user();
$paymentid = $generator->create_payment(['accountid' => $account->get('id'), 'amount' => 10, 'userid' => $user->id]);
$this->assertEquals('testcomponent', $DB->get_field('payments', 'component', ['id' => $paymentid]));
}
}
+202
View File
@@ -0,0 +1,202 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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 3 of the License, or
// (at your option) any later version.
//
// Moodle 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.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Testing helper class methods in payments API
*
* @package core_payment
* @category test
* @copyright 2020 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_payment;
use advanced_testcase;
use core\plugininfo\paygw;
/**
* Testing helper class methods in payments API
*
* @package core_payment
* @category test
* @copyright 2020 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class helper_test extends advanced_testcase {
protected function enable_paypal_gateway(): bool {
if (!array_key_exists('paypal', \core_component::get_plugin_list('paygw'))) {
return false;
}
return true;
}
public function test_create_account(): void {
global $DB;
$this->resetAfterTest();
$account = helper::save_payment_account((object)['name' => 'Test 1', 'idnumber' => '']);
$this->assertNotEmpty($account->get('id'));
$this->assertEquals('Test 1', $DB->get_field('payment_accounts', 'name', ['id' => $account->get('id')]));
}
public function test_update_account_details(): void {
global $DB;
$this->resetAfterTest();
$account = helper::save_payment_account((object)['name' => 'Test 1', 'idnumber' => '']);
$record = $account->to_record();
$record->name = 'Edited name';
$editedaccount = helper::save_payment_account($record);
$this->assertEquals($account->get('id'), $editedaccount->get('id'));
$this->assertEquals('Edited name', $DB->get_field('payment_accounts', 'name', ['id' => $account->get('id')]));
}
public function test_update_account_gateways(): void {
global $DB;
if (!$this->enable_paypal_gateway()) {
$this->markTestSkipped('Paypal payment gateway plugin not found');
}
$this->resetAfterTest();
$account = helper::save_payment_account((object)['name' => 'Test 1', 'idnumber' => '']);
$gateway = helper::save_payment_gateway(
(object)['accountid' => $account->get('id'), 'gateway' => 'paypal', 'config' => 'T1']);
$this->assertNotEmpty($gateway->get('id'));
$this->assertEquals('T1', $DB->get_field('payment_gateways', 'config', ['id' => $gateway->get('id')]));
// Update by id.
$editedgateway = helper::save_payment_gateway(
(object)['id' => $gateway->get('id'), 'accountid' => $account->get('id'), 'gateway' => 'paypal', 'config' => 'T2']);
$this->assertEquals($gateway->get('id'), $editedgateway->get('id'));
$this->assertEquals('T2', $DB->get_field('payment_gateways', 'config', ['id' => $gateway->get('id')]));
// Update by account/gateway.
$editedgateway = helper::save_payment_gateway(
(object)['accountid' => $account->get('id'), 'gateway' => 'paypal', 'config' => 'T3']);
$this->assertEquals($gateway->get('id'), $editedgateway->get('id'));
$this->assertEquals('T3', $DB->get_field('payment_gateways', 'config', ['id' => $gateway->get('id')]));
}
public function test_delete_account(): void {
global $DB;
if (!$this->enable_paypal_gateway()) {
$this->markTestSkipped('Paypal payment gateway plugin not found');
}
$this->resetAfterTest();
// Delete account without payments, it will be deleted, gateways will also be deleted.
$account = helper::save_payment_account((object)['name' => 'Test 1', 'idnumber' => '']);
$gateway = helper::save_payment_gateway(
(object)['accountid' => $account->get('id'), 'gateway' => 'paypal', 'config' => 'T1']);
helper::delete_payment_account(account::get_record(['id' => $account->get('id')]));
$this->assertEmpty($DB->get_records('payment_accounts', ['id' => $account->get('id')]));
$this->assertEmpty($DB->get_records('payment_gateways', ['id' => $gateway->get('id')]));
}
public function test_archive_restore_account(): void {
global $DB, $USER;
$this->resetAfterTest();
// Delete account with payments - it will be archived.
$this->setAdminUser();
$account = helper::save_payment_account((object)['name' => 'Test 1', 'idnumber' => '']);
$DB->insert_record('payments', [
'accountid' => $account->get('id'),
'component' => 'test',
'paymentarea' => 'test',
'itemid' => 1,
'userid' => $USER->id,
]);
helper::delete_payment_account(account::get_record(['id' => $account->get('id')]));
$this->assertEquals(1, $DB->get_field('payment_accounts', 'archived', ['id' => $account->get('id')]));
// Restore account.
helper::restore_payment_account(account::get_record(['id' => $account->get('id')]));
$this->assertEquals(0, $DB->get_field('payment_accounts', 'archived', ['id' => $account->get('id')]));
}
/**
* Provider for format_cost test
*
* @return array
*/
public function get_rounded_cost_provider(): array {
return [
'IRR 0 surcharge' => [5.345, 'IRR', 0, 5],
'IRR 12% surcharge' => [5.345, 'IRR', 12, 6],
'USD 0 surcharge' => [5.345, 'USD', 0, 5.34],
'USD 1% surcharge' => [5.345, 'USD', 1, 5.4],
];
}
/**
* Provider for test_get_cost_as_string
*
* @return array[]
*/
public function get_cost_as_string_provider(): array {
return [
'IRR 0 surcharge' => [5.345, 'IRR', 0, 'IRR'."\xc2\xa0".'5'],
'IRR 12% surcharge' => [5.345, 'IRR', 12, 'IRR'."\xc2\xa0".'6'],
'USD 0 surcharge' => [5.345, 'USD', 0, 'USD'."\xc2\xa0".'5.34'],
'USD 1% surcharge' => [5.345, 'USD', 1, 'USD'."\xc2\xa0".'5.40'],
];
}
/**
* Test for test_format_cost function
*
* @dataProvider get_rounded_cost_provider
* @param float $amount
* @param string $currency
* @param float $surcharge
* @param string $expected
*/
public function test_get_rounded_cost(float $amount, string $currency, float $surcharge, float $expected): void {
$this->assertEquals($expected, helper::get_rounded_cost($amount, $currency, $surcharge));
}
/**
* Test for get_cost_as_string function
*
* @dataProvider get_cost_as_string_provider
* @param float $amount
* @param string $currency
* @param float $surcharge
* @param string $expected
*/
public function test_get_cost_as_string(float $amount, string $currency, float $surcharge, string $expected): void {
// Some old ICU versions have a bug, where they don't follow the CLDR and they are
// missing the non-breaking-space between the currency abbreviation and the value.
// i.e. it returns AUD50 instead of AU\xc2\xa050). See the following issues @ ICU:
// - https://unicode-org.atlassian.net/browse/ICU-6560
// - https://unicode-org.atlassian.net/browse/ICU-8853
// - https://unicode-org.atlassian.net/browse/ICU-8840
// It has been detected that versions prior to ICU-61.1 / ICU-62.1 come with this
// problem. Noticeably some CI images (as of December 2021) use buggy ICU-60.1.
// So, here, we are going to dynamically verify the behaviour and skip the
// test when buggy one is found. No need to apply this to code as dar as the real
// formatting is not critical for the functionality (just small glitch).
if ('IRR5' === (new \NumberFormatter('en-AU', \NumberFormatter::CURRENCY))->formatCurrency(5, 'IRR')) {
$this->markTestSkipped('Old ICU libraries behavior (ICU < 62), skipping this tests');
}
$this->assertEquals($expected, helper::get_cost_as_string($amount, $currency, $surcharge));
}
}