MDL-69166 core_payment: Add paymentarea to the payment subsystem
[moodle.git] / payment / tests / generator / lib.php
CommitLineData
9476b489
MG
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17defined('MOODLE_INTERNAL') || die();
18
19/**
20 * Quiz module test data generator class
21 *
22 * @package core_payment
23 * @category test
24 * @copyright 2020 Marina Glancy
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27class core_payment_generator extends component_generator_base {
28
29 protected $accountcounter = 0;
30
31 /**
32 * Create a payment account
33 *
34 * @param array $data account data (name, idnumber, enabled) and additionally field 'gateways' that can include
35 * a list of gateways that should be mock-enabled for this account.
36 */
37 public function create_payment_account(array $data): \core_payment\account {
38 $this->accountcounter++;
39 $gateways = [];
40 if (!empty($data['gateways'])) {
41 $gateways = preg_split('/,/', $data['gateways']);
42 }
43 unset($data['gateways']);
44 $account = \core_payment\helper::save_payment_account(
45 (object)($data + ['name' => 'Test '.$this->accountcounter, 'idnumber' => '', 'enabled' => 1]));
46 foreach ($gateways as $gateway) {
47 \core_payment\helper::save_payment_gateway(
48 (object)['accountid' => $account->get('id'), 'gateway' => $gateway, 'enabled' => 1]);
49 }
50 return $account;
51 }
52
53 /**
54 * Create a payment account
55 *
56 * @param array $data
57 */
58 public function create_payment(array $data): int {
59 global $DB;
60 if (empty($data['accountid']) || !\core_payment\account::get_record(['id' => $data['accountid']])) {
61 throw new coding_exception('Account id is not specified or does not exist');
62 }
63
64 if (empty($data['amount'])) {
65 throw new coding_exception('Amount must be specified');
66 }
67
68 $gateways = \core\plugininfo\pg::get_enabled_plugins();
69 if (empty($data['gateway'])) {
70 $data['gateway'] = reset($gateways);
71 }
72
7d10f352
SR
73 $id = $DB->insert_record('payments', $data + [
74 'component' => 'testcomponent',
75 'paymentarea' => 'teatarea',
76 'componentid' => 0,
77 'currency' => 'AUD'
78 ]);
9476b489
MG
79 return $id;
80 }
81
82}