From 28743d2f5e649f1f39062672e602d9d5257cbb13 Mon Sep 17 00:00:00 2001 From: demeritcowboy Date: Sun, 27 Sep 2020 14:12:51 -0400 Subject: [PATCH] test activity contact variations --- .../phpunit/CRM/Activity/BAO/ActivityTest.php | 589 ++++++++++++++++++ 1 file changed, 589 insertions(+) diff --git a/tests/phpunit/CRM/Activity/BAO/ActivityTest.php b/tests/phpunit/CRM/Activity/BAO/ActivityTest.php index d89fd279a9..2e2666d1f9 100644 --- a/tests/phpunit/CRM/Activity/BAO/ActivityTest.php +++ b/tests/phpunit/CRM/Activity/BAO/ActivityTest.php @@ -8,6 +8,10 @@ class CRM_Activity_BAO_ActivityTest extends CiviUnitTestCase { private $allowedContactsACL = []; + private $loggedInUserId = NULL; + + private $someContacts = []; + /** * Set up for test. * @@ -1625,4 +1629,589 @@ $text Civi::$statics['CRM_Core_Component']['info']['CiviCase']->info['showActivitiesInCore'] = $val; } + /** + * Test multiple variations of target and assignee contacts in create + * and edit mode. + * + * @dataProvider targetAndAssigneeProvider + * @param array $do_first + * @param array $do_second + */ + public function testTargetAssigneeVariations(array $do_first, array $do_second) { + // Originally wanted to put this in setUp() but it broke other tests. + $this->loggedInUserId = $this->createLoggedInUser(); + for ($i = 1; $i <= 4; $i++) { + $this->someContacts[$i] = $this->individualCreate([], $i - 1, TRUE); + } + + $params = [ + 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Meeting'), + 'subject' => 'Test Meeting', + 'source_contact_id' => $this->loggedInUserId, + ]; + + // Create an activity first if specified. + $activity = NULL; + if (!empty($do_first)) { + if (!empty($do_first['targets'])) { + // e.g. if it is [1], then pick $someContacts[1]. If it's [1,2], then + // pick $someContacts[1] and $someContacts[2]. + $params['target_contact_id'] = array_values(array_intersect_key($this->someContacts, array_flip($do_first['targets']))); + } + if (!empty($do_first['assignees'])) { + $params['assignee_contact_id'] = array_values(array_intersect_key($this->someContacts, array_flip($do_first['assignees']))); + } + + $activity = CRM_Activity_BAO_Activity::create($params); + $this->assertNotEmpty($activity->id); + + $params['id'] = $activity->id; + } + + // Now do the second one, which will either create or update depending what + // we did first. + $params['target_contact_id'] = array_values(array_intersect_key($this->someContacts, array_flip($do_second['targets']))); + $params['assignee_contact_id'] = array_values(array_intersect_key($this->someContacts, array_flip($do_second['assignees']))); + $activity = CRM_Activity_BAO_Activity::create($params); + + // Check targets + $queryParams = [ + 1 => [$activity->id, 'Integer'], + 2 => [CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Targets'), 'Integer'], + ]; + $this->assertEquals($params['target_contact_id'], array_column(CRM_Core_DAO::executeQuery('SELECT contact_id FROM civicrm_activity_contact WHERE activity_id = %1 AND record_type_id = %2', $queryParams)->fetchAll(), 'contact_id')); + + // Check assignees + $queryParams = [ + 1 => [$activity->id, 'Integer'], + 2 => [CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Assignees'), 'Integer'], + ]; + $this->assertEquals($params['assignee_contact_id'], array_column(CRM_Core_DAO::executeQuery('SELECT contact_id FROM civicrm_activity_contact WHERE activity_id = %1 AND record_type_id = %2', $queryParams)->fetchAll(), 'contact_id')); + + // Clean up + foreach ($this->someContacts as $cid) { + $this->callAPISuccess('Contact', 'delete', ['id' => $cid]); + } + } + + /** + * Dataprovider for testTargetAssigneeVariations + * @return array + */ + public function targetAndAssigneeProvider():array { + return [ + // Explicit index so that it's easy to see which one has failed without + // having to finger count. + 0 => [ + 'do first' => [ + // Completely empty array means don't create any activity first, + // as opposed to the ones we do later where "do first" has member + // elements but those are empty, which means create an activity first + // but with no contacts. + ], + 'do second' => [ + 'targets' => [], + 'assignees' => [], + ], + ], + 1 => [ + 'do first' => [], + 'do second' => [ + 'targets' => [1], + 'assignees' => [], + ], + ], + 2 => [ + 'do first' => [], + 'do second' => [ + 'targets' => [1, 2], + 'assignees' => [], + ], + ], + 3 => [ + 'do first' => [], + 'do second' => [ + 'targets' => [], + 'assignees' => [3], + ], + ], + 4 => [ + 'do first' => [], + 'do second' => [ + 'targets' => [], + 'assignees' => [3, 4], + ], + ], + 5 => [ + 'do first' => [], + 'do second' => [ + 'targets' => [1], + 'assignees' => [3], + ], + ], + 6 => [ + 'do first' => [], + 'do second' => [ + 'targets' => [1, 2], + 'assignees' => [3], + ], + ], + 7 => [ + 'do first' => [], + 'do second' => [ + 'targets' => [1, 2], + 'assignees' => [3, 4], + ], + ], + // The next sets test the same thing again but updating an activity + // that has no contacts + 8 => [ + 'do first' => [ + 'targets' => [], + 'assignees' => [], + ], + 'do second' => [ + 'targets' => [], + 'assignees' => [], + ], + ], + 9 => [ + 'do first' => [ + 'targets' => [], + 'assignees' => [], + ], + 'do second' => [ + 'targets' => [1], + 'assignees' => [], + ], + ], + 10 => [ + 'do first' => [ + 'targets' => [], + 'assignees' => [], + ], + 'do second' => [ + 'targets' => [1, 2], + 'assignees' => [], + ], + ], + 11 => [ + 'do first' => [ + 'targets' => [], + 'assignees' => [], + ], + 'do second' => [ + 'targets' => [], + 'assignees' => [3], + ], + ], + 12 => [ + 'do first' => [ + 'targets' => [], + 'assignees' => [], + ], + 'do second' => [ + 'targets' => [], + 'assignees' => [3, 4], + ], + ], + 13 => [ + 'do first' => [ + 'targets' => [], + 'assignees' => [], + ], + 'do second' => [ + 'targets' => [1], + 'assignees' => [3], + ], + ], + 14 => [ + 'do first' => [ + 'targets' => [], + 'assignees' => [], + ], + 'do second' => [ + 'targets' => [1, 2], + 'assignees' => [3], + ], + ], + 15 => [ + 'do first' => [ + 'targets' => [], + 'assignees' => [], + ], + 'do second' => [ + 'targets' => [1, 2], + 'assignees' => [3, 4], + ], + ], + // And again but updating an activity with 1 contact + 16 => [ + 'do first' => [ + 'targets' => [1], + 'assignees' => [], + ], + 'do second' => [ + 'targets' => [], + 'assignees' => [], + ], + ], + 17 => [ + 'do first' => [ + 'targets' => [1], + 'assignees' => [], + ], + 'do second' => [ + 'targets' => [1], + 'assignees' => [], + ], + ], + 18 => [ + 'do first' => [ + 'targets' => [1], + 'assignees' => [], + ], + 'do second' => [ + 'targets' => [1, 2], + 'assignees' => [], + ], + ], + 19 => [ + 'do first' => [ + 'targets' => [1], + 'assignees' => [], + ], + 'do second' => [ + 'targets' => [], + 'assignees' => [3], + ], + ], + 20 => [ + 'do first' => [ + 'targets' => [1], + 'assignees' => [], + ], + 'do second' => [ + 'targets' => [], + 'assignees' => [3, 4], + ], + ], + 21 => [ + 'do first' => [ + 'targets' => [1], + 'assignees' => [], + ], + 'do second' => [ + 'targets' => [1], + 'assignees' => [3], + ], + ], + 22 => [ + 'do first' => [ + 'targets' => [1], + 'assignees' => [], + ], + 'do second' => [ + 'targets' => [1, 2], + 'assignees' => [3], + ], + ], + 23 => [ + 'do first' => [ + 'targets' => [1], + 'assignees' => [], + ], + 'do second' => [ + 'targets' => [1, 2], + 'assignees' => [3, 4], + ], + ], + 24 => [ + 'do first' => [ + 'targets' => [1], + 'assignees' => [], + ], + 'do second' => [ + // a little different variation where we're changing the target as + // opposed to adding one or deleting + 'targets' => [2], + 'assignees' => [], + ], + ], + // And again but updating an activity with 2 contacts + 25 => [ + 'do first' => [ + 'targets' => [1, 2], + 'assignees' => [], + ], + 'do second' => [ + 'targets' => [], + 'assignees' => [], + ], + ], + 26 => [ + 'do first' => [ + 'targets' => [1, 2], + 'assignees' => [], + ], + 'do second' => [ + 'targets' => [1], + 'assignees' => [], + ], + ], + 27 => [ + 'do first' => [ + 'targets' => [1, 2], + 'assignees' => [], + ], + 'do second' => [ + 'targets' => [1, 2], + 'assignees' => [], + ], + ], + 28 => [ + 'do first' => [ + 'targets' => [1, 2], + 'assignees' => [], + ], + 'do second' => [ + 'targets' => [], + 'assignees' => [3], + ], + ], + 29 => [ + 'do first' => [ + 'targets' => [1, 2], + 'assignees' => [], + ], + 'do second' => [ + 'targets' => [], + 'assignees' => [3, 4], + ], + ], + 30 => [ + 'do first' => [ + 'targets' => [1, 2], + 'assignees' => [], + ], + 'do second' => [ + 'targets' => [1], + 'assignees' => [3], + ], + ], + 31 => [ + 'do first' => [ + 'targets' => [1, 2], + 'assignees' => [], + ], + 'do second' => [ + 'targets' => [1, 2], + 'assignees' => [3], + ], + ], + 32 => [ + 'do first' => [ + 'targets' => [1, 2], + 'assignees' => [], + ], + 'do second' => [ + 'targets' => [1, 2], + 'assignees' => [3, 4], + ], + ], + 33 => [ + 'do first' => [ + 'targets' => [1, 2], + 'assignees' => [], + ], + 'do second' => [ + 'targets' => [2], + 'assignees' => [], + ], + ], + // And again but now start with assignees + 34 => [ + 'do first' => [ + 'targets' => [], + 'assignees' => [3], + ], + 'do second' => [ + 'targets' => [], + 'assignees' => [], + ], + ], + 35 => [ + 'do first' => [ + 'targets' => [], + 'assignees' => [3], + ], + 'do second' => [ + 'targets' => [1], + 'assignees' => [], + ], + ], + 36 => [ + 'do first' => [ + 'targets' => [], + 'assignees' => [3], + ], + 'do second' => [ + 'targets' => [1, 2], + 'assignees' => [], + ], + ], + 37 => [ + 'do first' => [ + 'targets' => [], + 'assignees' => [3], + ], + 'do second' => [ + 'targets' => [], + 'assignees' => [3], + ], + ], + 38 => [ + 'do first' => [ + 'targets' => [], + 'assignees' => [3], + ], + 'do second' => [ + 'targets' => [], + 'assignees' => [3, 4], + ], + ], + 39 => [ + 'do first' => [ + 'targets' => [], + 'assignees' => [3], + ], + 'do second' => [ + 'targets' => [1], + 'assignees' => [3], + ], + ], + 40 => [ + 'do first' => [ + 'targets' => [], + 'assignees' => [3], + ], + 'do second' => [ + 'targets' => [1, 2], + 'assignees' => [3], + ], + ], + 41 => [ + 'do first' => [ + 'targets' => [], + 'assignees' => [3], + ], + 'do second' => [ + 'targets' => [1, 2], + 'assignees' => [3, 4], + ], + ], + 42 => [ + 'do first' => [ + 'targets' => [], + 'assignees' => [3], + ], + 'do second' => [ + 'targets' => [], + 'assignees' => [4], + ], + ], + // And again but now start with 2 assignees + 43 => [ + 'do first' => [ + 'targets' => [], + 'assignees' => [3, 4], + ], + 'do second' => [ + 'targets' => [], + 'assignees' => [], + ], + ], + 44 => [ + 'do first' => [ + 'targets' => [], + 'assignees' => [3, 4], + ], + 'do second' => [ + 'targets' => [1], + 'assignees' => [], + ], + ], + 45 => [ + 'do first' => [ + 'targets' => [], + 'assignees' => [3, 4], + ], + 'do second' => [ + 'targets' => [1, 2], + 'assignees' => [], + ], + ], + 46 => [ + 'do first' => [ + 'targets' => [], + 'assignees' => [3, 4], + ], + 'do second' => [ + 'targets' => [], + 'assignees' => [3], + ], + ], + 47 => [ + 'do first' => [ + 'targets' => [], + 'assignees' => [3, 4], + ], + 'do second' => [ + 'targets' => [], + 'assignees' => [3, 4], + ], + ], + 48 => [ + 'do first' => [ + 'targets' => [], + 'assignees' => [3, 4], + ], + 'do second' => [ + 'targets' => [1], + 'assignees' => [3], + ], + ], + 49 => [ + 'do first' => [ + 'targets' => [], + 'assignees' => [3, 4], + ], + 'do second' => [ + 'targets' => [1, 2], + 'assignees' => [3], + ], + ], + 50 => [ + 'do first' => [ + 'targets' => [], + 'assignees' => [3, 4], + ], + 'do second' => [ + 'targets' => [1, 2], + 'assignees' => [3, 4], + ], + ], + 51 => [ + 'do first' => [ + 'targets' => [], + 'assignees' => [3, 4], + ], + 'do second' => [ + 'targets' => [], + 'assignees' => [4], + ], + ], + ]; + } + } -- 2.25.1