Merge pull request #13232 from JO0st/core-574
[civicrm-core.git] / tests / phpunit / api / v4 / Entity / ContactJoinTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
11 */
12
13 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 * $Id$
18 *
19 */
20
21
22 namespace api\v4\Entity;
23
24 use Civi\Api4\Contact;
25 use Civi\Api4\OptionValue;
26 use api\v4\UnitTestCase;
27
28 /**
29 * @group headless
30 */
31 class ContactJoinTest extends UnitTestCase {
32
33 public function setUpHeadless() {
34 $relatedTables = [
35 'civicrm_address',
36 'civicrm_email',
37 'civicrm_phone',
38 'civicrm_openid',
39 'civicrm_im',
40 'civicrm_website',
41 'civicrm_activity',
42 'civicrm_activity_contact',
43 ];
44
45 $this->cleanup(['tablesToTruncate' => $relatedTables]);
46 $this->loadDataSet('SingleContact');
47
48 return parent::setUpHeadless();
49 }
50
51 public function testContactJoin() {
52
53 $contact = $this->getReference('test_contact_1');
54 $entitiesToTest = ['Address', 'OpenID', 'IM', 'Website', 'Email', 'Phone'];
55
56 foreach ($entitiesToTest as $entity) {
57 $results = civicrm_api4($entity, 'get', [
58 'where' => [['contact_id', '=', $contact['id']]],
59 'select' => ['contact.display_name', 'contact.id'],
60 ]);
61 foreach ($results as $result) {
62 $this->assertEquals($contact['id'], $result['contact.id']);
63 $this->assertEquals($contact['display_name'], $result['contact.display_name']);
64 }
65 }
66 }
67
68 public function testJoinToPCMWillReturnArray() {
69 $contact = Contact::create()->setValues([
70 'preferred_communication_method' => [1, 2, 3],
71 'contact_type' => 'Individual',
72 'first_name' => 'Test',
73 'last_name' => 'PCM',
74 ])->execute()->first();
75
76 $fetchedContact = Contact::get()
77 ->addWhere('id', '=', $contact['id'])
78 ->addSelect('preferred_communication_method')
79 ->execute()
80 ->first();
81
82 $this->assertCount(3, $fetchedContact["preferred_communication_method"]);
83 }
84
85 public function testJoinToPCMOptionValueWillShowLabel() {
86 $options = OptionValue::get()
87 ->addWhere('option_group.name', '=', 'preferred_communication_method')
88 ->execute()
89 ->getArrayCopy();
90
91 $optionValues = array_column($options, 'value');
92 $labels = array_column($options, 'label');
93
94 $contact = Contact::create()->setValues([
95 'preferred_communication_method' => $optionValues,
96 'contact_type' => 'Individual',
97 'first_name' => 'Test',
98 'last_name' => 'PCM',
99 ])->execute()->first();
100
101 $contact2 = Contact::create()->setValues([
102 'preferred_communication_method' => $optionValues,
103 'contact_type' => 'Individual',
104 'first_name' => 'Test',
105 'last_name' => 'PCM2',
106 ])->execute()->first();
107
108 $contactIds = array_column([$contact, $contact2], 'id');
109
110 $fetchedContact = Contact::get()
111 ->addWhere('id', 'IN', $contactIds)
112 ->addSelect('preferred_communication_method.label')
113 ->execute()
114 ->first();
115
116 $preferredMethod = $fetchedContact['preferred_communication_method'];
117 $returnedLabels = array_column($preferredMethod, 'label');
118
119 $this->assertEquals($labels, $returnedLabels);
120 }
121
122 }