Merge pull request #16469 from civicrm/5.22
[civicrm-core.git] / tests / phpunit / api / v3 / ValidateTest.php
CommitLineData
6e4339c4
SB
1<?php
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
6e4339c4 5 | |
7d61e75f
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6e4339c4
SB
9 +--------------------------------------------------------------------+
10 */
11
12/**
13 * Tests for the generic validate API action.
14 *
15 * @package CiviCRM_APIv3
16 * @group headless
17 */
18class api_v3_ValidateTest extends CiviUnitTestCase {
39b959db 19
6e4339c4
SB
20 /**
21 * This method is called before a test is executed.
22 */
23 protected function setUp() {
24 parent::setUp();
25 }
26
27 public function testEmptyContactValidate() {
9099cab3
CW
28 $validation = $this->callAPISuccess('Contact', 'validate', ['action' => "create"]);
29 $expectedOut = [
30 'contact_type' => [
6e4339c4
SB
31 'message' => "Mandatory key(s) missing from params array: contact_type",
32 'code' => "mandatory_missing",
9099cab3
CW
33 ],
34 ];
6e4339c4
SB
35 $this->assertEquals($validation['values'][0], $expectedOut);
36 }
37
38 public function testContributionValidate() {
9099cab3
CW
39 $validation = $this->callAPISuccess('Contribution', 'validate', ['action' => "create", 'total_amount' => "100w"]);
40 $totalAmountErrors = [
6e4339c4
SB
41 'message' => "total_amount is not a valid amount: 100w",
42 'code' => "incorrect_value",
9099cab3 43 ];
6e4339c4 44
9099cab3 45 $contactIdErrors = [
6e4339c4
SB
46 'message' => "Mandatory key(s) missing from params array: contact_id",
47 'code' => "mandatory_missing",
9099cab3 48 ];
6e4339c4
SB
49
50 $this->assertEquals($validation['values'][0]['total_amount'], $totalAmountErrors);
51 $this->assertEquals($validation['values'][0]['contact_id'], $contactIdErrors);
52 }
53
46c3679e 54 public function testContributionDateValidate() {
9099cab3 55 $params = [
46c3679e
SB
56 'action' => "create",
57 'financial_type_id' => "1",
58 'total_amount' => "100",
59 'contact_id' => "1",
60 'receive_date' => 'abc',
9099cab3 61 ];
46c3679e
SB
62 $validation = $this->callAPISuccess('Contribution', 'validate', $params);
63
9099cab3
CW
64 $expectedOut = [
65 'receive_date' => [
46c3679e
SB
66 'message' => "receive_date is not a valid date: abc",
67 'code' => "incorrect_value",
9099cab3
CW
68 ],
69 ];
46c3679e
SB
70
71 $this->assertEquals($validation['values'][0], $expectedOut);
72 }
73
6e4339c4 74}