api4 - Apply standard headers
[civicrm-core.git] / tests / phpunit / api / v4 / Entity / ContactJoinTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2019 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 /**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2019
33 * $Id$
34 *
35 */
36
37
38 namespace api\v4\Entity;
39
40 use Civi\Api4\Contact;
41 use Civi\Api4\OptionValue;
42 use api\v4\UnitTestCase;
43
44 /**
45 * @group headless
46 */
47 class ContactJoinTest extends UnitTestCase {
48
49 public function setUpHeadless() {
50 $relatedTables = [
51 'civicrm_address',
52 'civicrm_email',
53 'civicrm_phone',
54 'civicrm_openid',
55 'civicrm_im',
56 'civicrm_website',
57 'civicrm_activity',
58 'civicrm_activity_contact',
59 ];
60
61 $this->cleanup(['tablesToTruncate' => $relatedTables]);
62 $this->loadDataSet('SingleContact');
63
64 return parent::setUpHeadless();
65 }
66
67 public function testContactJoin() {
68
69 $contact = $this->getReference('test_contact_1');
70 $entitiesToTest = ['Address', 'OpenID', 'IM', 'Website', 'Email', 'Phone'];
71
72 foreach ($entitiesToTest as $entity) {
73 $results = civicrm_api4($entity, 'get', [
74 'where' => [['contact_id', '=', $contact['id']]],
75 'select' => ['contact.display_name', 'contact.id'],
76 ]);
77 foreach ($results as $result) {
78 $this->assertEquals($contact['id'], $result['contact.id']);
79 $this->assertEquals($contact['display_name'], $result['contact.display_name']);
80 }
81 }
82 }
83
84 public function testJoinToPCMWillReturnArray() {
85 $contact = Contact::create()->setValues([
86 'preferred_communication_method' => [1, 2, 3],
87 'contact_type' => 'Individual',
88 'first_name' => 'Test',
89 'last_name' => 'PCM',
90 ])->execute()->first();
91
92 $fetchedContact = Contact::get()
93 ->addWhere('id', '=', $contact['id'])
94 ->addSelect('preferred_communication_method')
95 ->execute()
96 ->first();
97
98 $this->assertCount(3, $fetchedContact["preferred_communication_method"]);
99 }
100
101 public function testJoinToPCMOptionValueWillShowLabel() {
102 $options = OptionValue::get()
103 ->addWhere('option_group.name', '=', 'preferred_communication_method')
104 ->execute()
105 ->getArrayCopy();
106
107 $optionValues = array_column($options, 'value');
108 $labels = array_column($options, 'label');
109
110 $contact = Contact::create()->setValues([
111 'preferred_communication_method' => $optionValues,
112 'contact_type' => 'Individual',
113 'first_name' => 'Test',
114 'last_name' => 'PCM',
115 ])->execute()->first();
116
117 $contact2 = Contact::create()->setValues([
118 'preferred_communication_method' => $optionValues,
119 'contact_type' => 'Individual',
120 'first_name' => 'Test',
121 'last_name' => 'PCM2',
122 ])->execute()->first();
123
124 $contactIds = array_column([$contact, $contact2], 'id');
125
126 $fetchedContact = Contact::get()
127 ->addWhere('id', 'IN', $contactIds)
128 ->addSelect('preferred_communication_method.label')
129 ->execute()
130 ->first();
131
132 $preferredMethod = $fetchedContact['preferred_communication_method'];
133 $returnedLabels = array_column($preferredMethod, 'label');
134
135 $this->assertEquals($labels, $returnedLabels);
136 }
137
138 }