Merge pull request #15309 from totten/master-api4
[civicrm-core.git] / tests / phpunit / api / v4 / Entity / ContactJoinTest.php
1 <?php
2
3 namespace api\v4\Entity;
4
5 use Civi\Api4\Contact;
6 use Civi\Api4\OptionValue;
7 use api\v4\UnitTestCase;
8
9 /**
10 * @group headless
11 */
12 class ContactJoinTest extends UnitTestCase {
13
14 public function setUpHeadless() {
15 $relatedTables = [
16 'civicrm_address',
17 'civicrm_email',
18 'civicrm_phone',
19 'civicrm_openid',
20 'civicrm_im',
21 'civicrm_website',
22 'civicrm_activity',
23 'civicrm_activity_contact',
24 ];
25
26 $this->cleanup(['tablesToTruncate' => $relatedTables]);
27 $this->loadDataSet('SingleContact');
28
29 return parent::setUpHeadless();
30 }
31
32 public function testContactJoin() {
33
34 $contact = $this->getReference('test_contact_1');
35 $entitiesToTest = ['Address', 'OpenID', 'IM', 'Website', 'Email', 'Phone'];
36
37 foreach ($entitiesToTest as $entity) {
38 $results = civicrm_api4($entity, 'get', [
39 'where' => [['contact_id', '=', $contact['id']]],
40 'select' => ['contact.display_name', 'contact.id'],
41 ]);
42 foreach ($results as $result) {
43 $this->assertEquals($contact['id'], $result['contact.id']);
44 $this->assertEquals($contact['display_name'], $result['contact.display_name']);
45 }
46 }
47 }
48
49 public function testJoinToPCMWillReturnArray() {
50 $contact = Contact::create()->setValues([
51 'preferred_communication_method' => [1, 2, 3],
52 'contact_type' => 'Individual',
53 'first_name' => 'Test',
54 'last_name' => 'PCM',
55 ])->execute()->first();
56
57 $fetchedContact = Contact::get()
58 ->addWhere('id', '=', $contact['id'])
59 ->addSelect('preferred_communication_method')
60 ->execute()
61 ->first();
62
63 $this->assertCount(3, $fetchedContact["preferred_communication_method"]);
64 }
65
66 public function testJoinToPCMOptionValueWillShowLabel() {
67 $options = OptionValue::get()
68 ->addWhere('option_group.name', '=', 'preferred_communication_method')
69 ->execute()
70 ->getArrayCopy();
71
72 $optionValues = array_column($options, 'value');
73 $labels = array_column($options, 'label');
74
75 $contact = Contact::create()->setValues([
76 'preferred_communication_method' => $optionValues,
77 'contact_type' => 'Individual',
78 'first_name' => 'Test',
79 'last_name' => 'PCM',
80 ])->execute()->first();
81
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();
88
89 $contactIds = array_column([$contact, $contact2], 'id');
90
91 $fetchedContact = Contact::get()
92 ->addWhere('id', 'IN', $contactIds)
93 ->addSelect('preferred_communication_method.label')
94 ->execute()
95 ->first();
96
97 $preferredMethod = $fetchedContact['preferred_communication_method'];
98 $returnedLabels = array_column($preferredMethod, 'label');
99
100 $this->assertEquals($labels, $returnedLabels);
101 }
102
103 }