Commit | Line | Data |
---|---|---|
a26a6848 RLAR |
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 | */ | |
18 | ||
19 | namespace api\v4\Entity; | |
20 | ||
21 | use Civi\Api4\Contact; | |
22 | use api\v4\UnitTestCase; | |
23 | use Civi\Api4\Activity; | |
24 | use Civi\Api4\ActivityContact; | |
25 | use Civi\Test\TransactionalInterface; | |
26 | ||
27 | /** | |
28 | * Assert that updating an activity does not affect the targets. | |
29 | * | |
30 | * This test was written specifically to test | |
31 | * https://lab.civicrm.org/dev/core/-/issues/1428 | |
32 | * | |
33 | * @group headless | |
34 | */ | |
35 | class ActivityTest extends UnitTestCase implements TransactionalInterface { | |
36 | ||
37 | public function testActivityUpdate() { | |
38 | ||
39 | $meetingActivityTypeID = \Civi\Api4\OptionValue::get() | |
40 | ->addSelect('value') | |
41 | ->addWhere('option_group_id:name', '=', 'activity_type') | |
42 | ->addWhere('name', '=', 'Meeting') | |
43 | ->execute()->first()['value']; | |
44 | ||
45 | $domainContactID = \CRM_Core_BAO_Domain::getDomain()->contact_id; | |
46 | $c1 = Contact::create(FALSE)->addValue('first_name', '1')->execute()->first()['id']; | |
47 | $c2 = Contact::create(FALSE)->addValue('first_name', '2')->execute()->first()['id']; | |
48 | ||
49 | $activityID = Activity::create(FALSE) | |
50 | ->setValues([ | |
51 | 'target_contact_id' => [$c1], | |
52 | 'assignee_contact_id' => [$c2], | |
53 | 'activity_type_id' => $meetingActivityTypeID, | |
54 | 'source_contact_id' => $domainContactID, | |
55 | 'subject' => 'test activity', | |
56 | ])->execute()->first()['id']; | |
57 | ||
58 | // Activity create does not return a full record, so get the ID then do another get call... | |
59 | $activity = Activity::get(FALSE) | |
60 | ->addSelect('id', 'subject', 'activity_type_id') | |
61 | ->addWhere('id', '=', $activityID) | |
62 | ->execute()->first(); | |
63 | $this->assertEquals($meetingActivityTypeID, $activity['activity_type_id']); | |
64 | $this->assertEquals('test activity', $activity['subject']); | |
65 | ||
66 | // Now check we have the correct target and assignees. | |
67 | $activityContacts = ActivityContact::get(FALSE) | |
68 | ->addWhere('activity_id', '=', $activityID) | |
69 | ->execute() | |
70 | ->indexBy('contact_id')->column('record_type_id'); | |
71 | ||
72 | // 1 is assignee | |
73 | // 2 is added | |
74 | // 3 is target/with | |
75 | $expectedActivityContacts = [$c1 => 3, $c2 => 1, $domainContactID => 2]; | |
76 | ksort($expectedActivityContacts); | |
77 | ksort($activityContacts); | |
78 | $this->assertEquals($expectedActivityContacts, $activityContacts, "ActivityContacts not as expected."); | |
79 | ||
80 | // Test we can update the activity. | |
81 | Activity::update(FALSE) | |
82 | ->addWhere('id', '=', $activityID) | |
83 | ->addValue('subject', 'updated subject') | |
84 | ->execute(); | |
85 | ||
86 | // Repeat the tests. | |
87 | $activity = Activity::get(FALSE) | |
88 | ->addSelect('id', 'subject', 'activity_type_id') | |
89 | ->addWhere('id', '=', $activityID) | |
90 | ->execute()->first(); | |
91 | $this->assertEquals($meetingActivityTypeID, $activity['activity_type_id']); | |
92 | $this->assertEquals('updated subject', $activity['subject'], "Activity subject was not updated correctly by Activity::update."); | |
93 | ||
94 | // Now check we have the correct target and assignees. | |
95 | $activityContacts = ActivityContact::get(FALSE) | |
96 | ->addWhere('activity_id', '=', $activityID) | |
97 | ->execute() | |
98 | ->indexBy('contact_id')->column('record_type_id'); | |
99 | ksort($activityContacts); | |
100 | $this->assertEquals($expectedActivityContacts, $activityContacts, "ActivityContacts not as expected after update."); | |
101 | } | |
102 | ||
103 | } |