MDL-69166 core_payment: Add paymentarea to the payment subsystem
[moodle.git] / payment / tests / accounts_test.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
17/**
18 * Testing accounts management in payments API
19 *
20 * @package core_payment
21 * @category test
22 * @copyright 2020 Marina Glancy
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26namespace core_payment;
27
28use advanced_testcase;
29use core\plugininfo\pg;
30
31/**
32 * Testing accounts management in payments API
33 *
34 * @package core_payment
35 * @category test
36 * @copyright 2020 Marina Glancy
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class accounts_testcase extends advanced_testcase {
40
41 protected function enable_paypal_gateway(): bool {
42 if (!array_key_exists('paypal', \core_component::get_plugin_list('pg'))) {
43 return false;
44 }
45 pg::set_enabled_plugins('paypal');
46 return true;
47 }
48
49 public function test_create_account() {
50 global $DB;
51 $this->resetAfterTest();
52
53 $account = helper::save_payment_account((object)['name' => 'Test 1', 'idnumber' => '']);
54 $this->assertNotEmpty($account->get('id'));
55 $this->assertEquals('Test 1', $DB->get_field('payment_accounts', 'name', ['id' => $account->get('id')]));
56 }
57
58 public function test_update_account_details() {
59 global $DB;
60 $this->resetAfterTest();
61
62 $account = helper::save_payment_account((object)['name' => 'Test 1', 'idnumber' => '']);
63 $record = $account->to_record();
64 $record->name = 'Edited name';
65 $editedaccount = helper::save_payment_account($record);
66 $this->assertEquals($account->get('id'), $editedaccount->get('id'));
67 $this->assertEquals('Edited name', $DB->get_field('payment_accounts', 'name', ['id' => $account->get('id')]));
68 }
69
70 public function test_update_account_gateways() {
71 global $DB;
72 if (!$this->enable_paypal_gateway()) {
73 $this->markTestSkipped('Paypal payment gateway plugin not found');
74 }
75
76 $this->resetAfterTest();
77
78 $account = helper::save_payment_account((object)['name' => 'Test 1', 'idnumber' => '']);
79 $gateway = helper::save_payment_gateway(
80 (object)['accountid' => $account->get('id'), 'gateway' => 'paypal', 'config' => 'T1']);
81 $this->assertNotEmpty($gateway->get('id'));
82 $this->assertEquals('T1', $DB->get_field('payment_gateways', 'config', ['id' => $gateway->get('id')]));
83
84 // Update by id.
85 $editedgateway = helper::save_payment_gateway(
86 (object)['id' => $gateway->get('id'), 'accountid' => $account->get('id'), 'gateway' => 'paypal', 'config' => 'T2']);
87 $this->assertEquals($gateway->get('id'), $editedgateway->get('id'));
88 $this->assertEquals('T2', $DB->get_field('payment_gateways', 'config', ['id' => $gateway->get('id')]));
89
90 // Update by account/gateway.
91 $editedgateway = helper::save_payment_gateway(
92 (object)['accountid' => $account->get('id'), 'gateway' => 'paypal', 'config' => 'T3']);
93 $this->assertEquals($gateway->get('id'), $editedgateway->get('id'));
94 $this->assertEquals('T3', $DB->get_field('payment_gateways', 'config', ['id' => $gateway->get('id')]));
95 }
96
97 public function test_delete_account() {
98 global $DB;
99 if (!$this->enable_paypal_gateway()) {
100 $this->markTestSkipped('Paypal payment gateway plugin not found');
101 }
102 $this->resetAfterTest();
103
104 // Delete account without payments, it will be deleted, gateways will also be deleted.
105 $account = helper::save_payment_account((object)['name' => 'Test 1', 'idnumber' => '']);
106 $gateway = helper::save_payment_gateway(
107 (object)['accountid' => $account->get('id'), 'gateway' => 'paypal', 'config' => 'T1']);
108
109 helper::delete_payment_account(account::get_record(['id' => $account->get('id')]));
110 $this->assertEmpty($DB->get_records('payment_accounts', ['id' => $account->get('id')]));
111 $this->assertEmpty($DB->get_records('payment_gateways', ['id' => $gateway->get('id')]));
112 }
113
114 public function test_archive_restore_account() {
115 global $DB, $USER;
116 $this->resetAfterTest();
117
118 // Delete account with payments - it will be archived.
119 $this->setAdminUser();
120 $account = helper::save_payment_account((object)['name' => 'Test 1', 'idnumber' => '']);
7d10f352
SR
121 $DB->insert_record('payments', ['accountid' => $account->get('id'), 'component' => 'test', 'paymentarea' => 'test',
122 'componentid' => 1, 'userid' => $USER->id]);
9476b489
MG
123 helper::delete_payment_account(account::get_record(['id' => $account->get('id')]));
124 $this->assertEquals(1, $DB->get_field('payment_accounts', 'archived', ['id' => $account->get('id')]));
125
126 // Restore account.
127 helper::restore_payment_account(account::get_record(['id' => $account->get('id')]));
128 $this->assertEquals(0, $DB->get_field('payment_accounts', 'archived', ['id' => $account->get('id')]));
129 }
130}