4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
22 namespace api\v
4\Entity
;
24 use Civi\Api4\Contact
;
25 use Civi\Api4\OptionValue
;
26 use api\v
4\UnitTestCase
;
31 class ContactJoinTest
extends UnitTestCase
{
33 public function setUpHeadless() {
42 'civicrm_activity_contact',
45 $this->cleanup(['tablesToTruncate' => $relatedTables]);
46 $this->loadDataSet('SingleContact');
48 return parent
::setUpHeadless();
51 public function testContactJoin() {
53 $contact = $this->getReference('test_contact_1');
54 $entitiesToTest = ['Address', 'OpenID', 'IM', 'Website', 'Email', 'Phone'];
56 foreach ($entitiesToTest as $entity) {
57 $results = civicrm_api4($entity, 'get', [
58 'where' => [['contact_id', '=', $contact['id']]],
59 'select' => ['contact.display_name', 'contact.id'],
61 foreach ($results as $result) {
62 $this->assertEquals($contact['id'], $result['contact.id']);
63 $this->assertEquals($contact['display_name'], $result['contact.display_name']);
68 public function testJoinToPCMWillReturnArray() {
69 $contact = Contact
::create()->setValues([
70 'preferred_communication_method' => [1, 2, 3],
71 'contact_type' => 'Individual',
72 'first_name' => 'Test',
74 ])->execute()->first();
76 $fetchedContact = Contact
::get()
77 ->addWhere('id', '=', $contact['id'])
78 ->addSelect('preferred_communication_method')
82 $this->assertCount(3, $fetchedContact["preferred_communication_method"]);
85 public function testJoinToPCMOptionValueWillShowLabel() {
86 $options = OptionValue
::get()
87 ->addWhere('option_group.name', '=', 'preferred_communication_method')
91 $optionValues = array_column($options, 'value');
92 $labels = array_column($options, 'label');
94 $contact = Contact
::create()->setValues([
95 'preferred_communication_method' => $optionValues,
96 'contact_type' => 'Individual',
97 'first_name' => 'Test',
99 ])->execute()->first();
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();
108 $contactIds = array_column([$contact, $contact2], 'id');
110 $fetchedContact = Contact
::get()
111 ->addWhere('id', 'IN', $contactIds)
112 ->addSelect('preferred_communication_method.label')
116 $preferredMethod = $fetchedContact['preferred_communication_method'];
117 $returnedLabels = array_column($preferredMethod, 'label');
119 $this->assertEquals($labels, $returnedLabels);