Merge pull request #16469 from civicrm/5.22
[civicrm-core.git] / tests / phpunit / api / v3 / LocBlockTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Class api_v3_LocBlockTest
14 *
15 * @group headless
16 */
17 class api_v3_LocBlockTest extends CiviUnitTestCase {
18
19 protected $_apiversion = 3;
20
21 protected $_entity = 'loc_block';
22
23 /**
24 * Set up.
25 */
26 public function setUp() {
27 parent::setUp();
28 $this->useTransaction(TRUE);
29 }
30
31 /**
32 * Test creating location block.
33 */
34 public function testCreateLocBlock() {
35 $email = $this->callAPISuccess('email', 'create', [
36 'contact_id' => 'null',
37 'email' => 'test@loc.block',
38 ]);
39 $phone = $this->callAPISuccess('phone', 'create', [
40 'contact_id' => 'null',
41 'location_type_id' => 1,
42 'phone' => '1234567',
43 ]);
44 $address = $this->callAPISuccess('address', 'create', [
45 'contact_id' => 'null',
46 'location_type_id' => 1,
47 'street_address' => '1234567',
48 ]);
49 $params = [
50 'address_id' => $address['id'],
51 'phone_id' => $phone['id'],
52 'email_id' => $email['id'],
53 ];
54 $description = 'Create locBlock with existing entities';
55 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, $description);
56 $id = $result['id'];
57 $this->assertEquals(1, $result['count']);
58 $this->assertNotNull($result['values'][$id]['id']);
59 $this->getAndCheck($params, $id, $this->_entity);
60 }
61
62 /**
63 * Test creating location block entities.
64 */
65 public function testCreateLocBlockEntities() {
66 $params = [
67 'email' => [
68 'location_type_id' => 1,
69 'email' => 'test2@loc.block',
70 ],
71 'phone' => [
72 'location_type_id' => 1,
73 'phone' => '987654321',
74 ],
75 'phone_2' => [
76 'location_type_id' => 1,
77 'phone' => '456-7890',
78 ],
79 'address' => [
80 'location_type_id' => 1,
81 'street_address' => '987654321',
82 ],
83 ];
84 $description = "Create entities and locBlock in 1 api call.";
85 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, $description, 'CreateEntities');
86 $id = $result['id'];
87 $this->assertEquals(1, $result['count']);
88
89 // Now check our results using the return param 'all'.
90 $getParams = [
91 'id' => $id,
92 'return' => 'all',
93 ];
94 // Can't use callAPISuccess with getsingle.
95 $result = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__, 'Get entities and location block in 1 api call');
96 $result = array_pop($result['values']);
97 $this->assertNotNull($result['email_id']);
98 $this->assertNotNull($result['phone_id']);
99 $this->assertNotNull($result['phone_2_id']);
100 $this->assertNotNull($result['address_id']);
101 $this->assertEquals($params['email']['email'], $result['email']['email']);
102 $this->assertEquals($params['phone_2']['phone'], $result['phone_2']['phone']);
103 $this->assertEquals($params['address']['street_address'], $result['address']['street_address']);
104
105 $this->callAPISuccess($this->_entity, 'delete', ['id' => $id]);
106 }
107
108 }