38c87aa2 |
1 | <?php |
2 | |
3 | use Civi\Test\HeadlessInterface; |
4 | use Civi\Test\HookInterface; |
5 | use Civi\Test\TransactionalInterface; |
6 | |
7 | /** |
8 | * FIXME - Add test description. |
9 | * |
10 | * Tips: |
11 | * - With HookInterface, you may implement CiviCRM hooks directly in the test class. |
12 | * Simply create corresponding functions (e.g. "hook_civicrm_post(...)" or similar). |
13 | * - With TransactionalInterface, any data changes made by setUp() or test****() functions will |
14 | * rollback automatically -- as long as you don't manipulate schema or truncate tables. |
15 | * If this test needs to manipulate schema or truncate tables, then either: |
16 | * a. Do all that using setupHeadless() and Civi\Test. |
17 | * b. Disable TransactionalInterface, and handle all setup/teardown yourself. |
18 | * |
19 | * @group headless |
20 | */ |
21 | class SequentialcreditnotesTest extends \PHPUnit\Framework\TestCase implements HeadlessInterface, HookInterface, TransactionalInterface { |
22 | |
23 | use \Civi\Test\Api3TestTrait; |
24 | |
25 | /** |
26 | * Setup for headless test. |
27 | * |
28 | * @return \Civi\Test\CiviEnvBuilder |
29 | * |
30 | * @throws \CRM_Extension_Exception_ParseException |
31 | */ |
32 | public function setUpHeadless(): \Civi\Test\CiviEnvBuilder { |
33 | // Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile(). |
34 | // See: https://docs.civicrm.org/dev/en/latest/testing/phpunit/#civitest |
35 | return \Civi\Test::headless() |
36 | ->installMe(__DIR__) |
37 | ->apply(); |
38 | } |
39 | |
40 | /** |
41 | * Check credit note id creation |
42 | * when a contribution is cancelled or refunded |
43 | * createCreditNoteId(); |
44 | * |
45 | * @throws \CRM_Core_Exception |
46 | * @throws \CiviCRM_API3_Exception |
47 | */ |
48 | public function testCreateCreditNoteId() { |
49 | $this->_apiversion = 4; |
50 | $contactId = $this->callAPISuccess('Contact', 'create', ['contact_type' => 'Individual', 'email' => 'b@example.com'])['id']; |
51 | |
52 | $params = [ |
53 | 'contact_id' => $contactId, |
54 | 'currency' => 'USD', |
55 | 'financial_type_id' => 1, |
56 | 'contribution_status_id' => 3, |
57 | 'payment_instrument_id' => 1, |
58 | 'source' => 'STUDENT', |
59 | 'receive_date' => '20080522000000', |
60 | 'receipt_date' => '20080522000000', |
61 | 'non_deductible_amount' => 0.00, |
62 | 'total_amount' => 300.00, |
63 | 'fee_amount' => 5, |
64 | 'net_amount' => 295, |
65 | 'trxn_id' => '76ereeswww835', |
66 | 'invoice_id' => '93ed39a9e9hd621bs0eafe3da82', |
67 | 'thankyou_date' => '20080522', |
68 | 'sequential' => TRUE, |
69 | ]; |
70 | |
71 | $creditNoteId = sequentialcreditnotes_create_credit_note_id(); |
72 | $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0]; |
73 | $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.'); |
74 | $this->assertEquals($creditNoteId, $contribution['creditnote_id'], 'Check if credit note id is created correctly.'); |
75 | |
76 | $params['id'] = $contribution['id']; |
77 | $this->callAPISuccess('Contribution', 'create', $params); |
78 | $contribution = $this->callAPISuccessGetSingle('Contribution', ['id' => $params['id']]); |
79 | $this->assertEquals($creditNoteId, $contribution['creditnote_id'], 'Check if credit note id was not altered.'); |
80 | } |
81 | |
82 | } |