babd1cec0fabeb1dda14764ab839aef72eefa605
[civicrm-core.git] / tests / phpunit / api / v4 / Action / FkJoinTest.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\Action;
23
24 use api\v4\UnitTestCase;
25 use Civi\Api4\Activity;
26 use Civi\Api4\Contact;
27
28 /**
29 * @group headless
30 */
31 class FkJoinTest extends UnitTestCase {
32
33 public function setUpHeadless() {
34 $relatedTables = [
35 'civicrm_activity',
36 'civicrm_phone',
37 'civicrm_activity_contact',
38 ];
39 $this->cleanup(['tablesToTruncate' => $relatedTables]);
40 $this->loadDataSet('DefaultDataSet');
41
42 return parent::setUpHeadless();
43 }
44
45 /**
46 * Fetch all phone call activities. Expects a single activity
47 * loaded from the data set.
48 */
49 public function testThreeLevelJoin() {
50 $results = Activity::get()
51 ->setCheckPermissions(FALSE)
52 ->addWhere('activity_type_id:name', '=', 'Phone Call')
53 ->execute();
54
55 $this->assertCount(1, $results);
56 }
57
58 public function testActivityContactJoin() {
59 $results = Activity::get()
60 ->setCheckPermissions(FALSE)
61 ->addSelect('assignees.id')
62 ->addSelect('assignees.first_name')
63 ->addSelect('assignees.display_name')
64 ->addWhere('assignees.first_name', '=', 'Phoney')
65 ->execute();
66
67 $firstResult = $results->first();
68
69 $this->assertCount(1, $results);
70 $this->assertTrue(is_array($firstResult['assignees']));
71
72 $firstAssignee = array_shift($firstResult['assignees']);
73 $this->assertEquals($firstAssignee['first_name'], 'Phoney');
74 }
75
76 public function testContactPhonesJoin() {
77 $testContact = $this->getReference('test_contact_1');
78 $testPhone = $this->getReference('test_phone_1');
79
80 $results = Contact::get()
81 ->setCheckPermissions(FALSE)
82 ->addSelect('phones.phone')
83 ->addWhere('id', '=', $testContact['id'])
84 ->addWhere('phones.location_type.name', '=', 'Home')
85 ->execute()
86 ->first();
87
88 $this->assertArrayHasKey('phones', $results);
89 $this->assertCount(1, $results['phones']);
90 $firstPhone = array_shift($results['phones']);
91 $this->assertEquals($testPhone['phone'], $firstPhone['phone']);
92 }
93
94 }