3 namespace api\v
4\Entity
;
6 use Civi\Api4\OptionValue
;
7 use api\v
4\UnitTestCase
;
12 class ContactJoinTest
extends UnitTestCase
{
14 public function setUpHeadless() {
23 'civicrm_activity_contact',
26 $this->cleanup(['tablesToTruncate' => $relatedTables]);
27 $this->loadDataSet('SingleContact');
29 return parent
::setUpHeadless();
32 public function testContactJoin() {
34 $contact = $this->getReference('test_contact_1');
35 $entitiesToTest = ['Address', 'OpenID', 'IM', 'Website', 'Email', 'Phone'];
37 foreach ($entitiesToTest as $entity) {
38 $results = civicrm_api4($entity, 'get', [
39 'where' => [['contact_id', '=', $contact['id']]],
40 'select' => ['contact.display_name', 'contact.id'],
42 foreach ($results as $result) {
43 $this->assertEquals($contact['id'], $result['contact.id']);
44 $this->assertEquals($contact['display_name'], $result['contact.display_name']);
49 public function testJoinToPCMWillReturnArray() {
50 $contact = Contact
::create()->setValues([
51 'preferred_communication_method' => [1, 2, 3],
52 'contact_type' => 'Individual',
53 'first_name' => 'Test',
55 ])->execute()->first();
57 $fetchedContact = Contact
::get()
58 ->addWhere('id', '=', $contact['id'])
59 ->addSelect('preferred_communication_method')
63 $this->assertCount(3, $fetchedContact["preferred_communication_method"]);
66 public function testJoinToPCMOptionValueWillShowLabel() {
67 $options = OptionValue
::get()
68 ->addWhere('option_group.name', '=', 'preferred_communication_method')
72 $optionValues = array_column($options, 'value');
73 $labels = array_column($options, 'label');
75 $contact = Contact
::create()->setValues([
76 'preferred_communication_method' => $optionValues,
77 'contact_type' => 'Individual',
78 'first_name' => 'Test',
80 ])->execute()->first();
82 $contact2 = Contact
::create()->setValues([
83 'preferred_communication_method' => $optionValues,
84 'contact_type' => 'Individual',
85 'first_name' => 'Test',
86 'last_name' => 'PCM2',
87 ])->execute()->first();
89 $contactIds = array_column([$contact, $contact2], 'id');
91 $fetchedContact = Contact
::get()
92 ->addWhere('id', 'IN', $contactIds)
93 ->addSelect('preferred_communication_method.label')
97 $preferredMethod = $fetchedContact['preferred_communication_method'];
98 $returnedLabels = array_column($preferredMethod, 'label');
100 $this->assertEquals($labels, $returnedLabels);