2 require_once 'CiviTest/CiviCaseTestCase.php';
5 * Class CRM_Upgrade_Incremental_php_FiveTwentyTest
8 class CRM_Upgrade_Incremental_php_FiveTwentyTest
extends CiviCaseTestCase
{
11 * Test that the upgrade task changes the direction but only
12 * for bidirectional relationship types that are b_a.
14 public function testChangeCaseTypeAutoassignee() {
16 // We don't know what the ids are for the relationship types since it
17 // seems to depend what ran before us, so retrieve them first and go by
19 // Also spouse might not exist.
21 $result = $this->callAPISuccess('RelationshipType', 'get', ['limit' => 0])['values'];
22 // Get list of ids keyed on name.
23 $relationshipTypeNames = array_column($result, 'id', 'name_b_a');
25 // Create spouse if none.
26 if (!isset($relationshipTypeNames['Spouse of'])) {
27 $spouseId = $this->relationshipTypeCreate([
28 'name_a_b' => 'Spouse of',
29 'name_b_a' => 'Spouse of',
30 'label_a_b' => 'Spouse of',
31 'label_b_a' => 'Spouse of',
32 'contact_type_a' => 'Individual',
33 'contact_type_b' => 'Individual',
35 $relationshipTypeNames['Spouse of'] = $spouseId;
37 // Maybe unnecessary but why not. Slightly different than an undefined
38 // index later when it doesn't exist at all.
39 $this->assertGreaterThan(0, $relationshipTypeNames['Spouse of']);
43 * In this sample case type for autoassignees we have:
44 * - a_b unidirectional: Case Coordinator is
45 * - b_a unidirectional: Benefits Specialist
46 * - Bidirectional the way it's stored in 5.16+: Spouse of
47 * - config entry pre-5.16, where it's a bidirectional relationship stored as b_a: Spouse of
49 * Also just for extra some non-ascii chars in here to show they
52 $newCaseTypeXml = <<<ENDXML
53 <?xml version="1.0" encoding="UTF-8"?>
55 <name>test_type</name>
58 <name>Open Case</name>
59 <max_instances>1</max_instances>
65 <name>Follow up</name>
71 <name>Phone Call</name>
78 <name>standard_timeline</name>
79 <label>Standard Timeline</label>
80 <timeline>true</timeline>
83 <name>Open Case</name>
84 <status>Completed</status>
85 <label>Open Case</label>
86 <default_assignee_type>1</default_assignee_type>
91 <name>timeline_1</name>
92 <label>AnotherTimeline</label>
93 <timeline>true</timeline>
96 <name>Follow up</name>
97 <label>Follow up</label>
98 <status>Scheduled</status>
99 <reference_activity>Open Case</reference_activity>
100 <reference_offset>7</reference_offset>
101 <reference_select>newest</reference_select>
102 <default_assignee_type>2</default_assignee_type>
103 <default_assignee_relationship>{$relationshipTypeNames['Senior Services Coordinator']}_b_a</default_assignee_relationship>
104 <default_assignee_contact></default_assignee_contact>
107 <name>Follow up</name>
108 <label>Follow up</label>
109 <status>Scheduled</status>
110 <reference_activity>Open Case</reference_activity>
111 <reference_offset>14</reference_offset>
112 <reference_select>newest</reference_select>
113 <default_assignee_type>2</default_assignee_type>
114 <default_assignee_relationship>{$relationshipTypeNames['Benefits Specialist']}_a_b</default_assignee_relationship>
115 <default_assignee_contact></default_assignee_contact>
118 <name>Follow up</name>
119 <label>Follow up</label>
120 <status>Scheduled</status>
121 <reference_activity>Open Case</reference_activity>
122 <reference_offset>21</reference_offset>
123 <reference_select>newest</reference_select>
124 <default_assignee_type>2</default_assignee_type>
125 <default_assignee_relationship>{$relationshipTypeNames['Spouse of']}_a_b</default_assignee_relationship>
126 <default_assignee_contact></default_assignee_contact>
129 <name>Follow up</name>
130 <label>Follow up</label>
131 <status>Scheduled</status>
132 <reference_activity>Open Case</reference_activity>
133 <reference_offset>28</reference_offset>
134 <reference_select>newest</reference_select>
135 <default_assignee_type>2</default_assignee_type>
136 <default_assignee_relationship>{$relationshipTypeNames['Spouse of']}_b_a</default_assignee_relationship>
137 <default_assignee_contact></default_assignee_contact>
144 <name>Senior Services Coordinator</name>
149 <name>Spouse of</name>
152 <name>Benefits Specialist is</name>
155 <RestrictActivityAsgmtToCmsUser>0</RestrictActivityAsgmtToCmsUser>
160 $dao = new CRM_Case_DAO_CaseType();
161 $dao->name
= 'test_type';
162 $dao->title
= 'Test Type';
164 $dao->definition
= $newCaseTypeXml;
167 $caseTypeId = $dao->id
;
170 $upgrader = new CRM_Upgrade_Incremental_php_FiveTwenty();
171 $upgrader->changeCaseTypeAutoassignee();
173 // Check if the case type is what we expect. It should be identical except
174 // the b_a spouse one should get converted to the a_b direction.
175 $expectedCaseTypeXml = str_replace(
176 "<default_assignee_relationship>{$relationshipTypeNames['Spouse of']}_b_a",
177 "<default_assignee_relationship>{$relationshipTypeNames['Spouse of']}_a_b",
181 //echo $expectedCaseTypeXml;
183 // Get the updated case type and check.
184 $dao = CRM_Core_DAO
::executeQuery("SELECT definition FROM civicrm_case_type WHERE id = {$caseTypeId}");
187 $this->assertEquals($expectedCaseTypeXml, $dao->definition
);
191 * Test that the upgrade task converts case role <name>'s that
192 * are labels to their name.
194 public function testConvertRoleLabelsToNames() {
196 // We don't know what the ids are for the relationship types since it
197 // seems to depend what ran before us, so retrieve them first and go by
199 // Also spouse might not exist.
201 $result = $this->callAPISuccess('RelationshipType', 'get', ['limit' => 0])['values'];
202 // Get list of ids keyed on name.
203 $relationshipTypeNames = array_column($result, 'id', 'name_b_a');
205 // Create spouse if none.
206 if (!isset($relationshipTypeNames['Spouse of'])) {
207 $spouseId = $this->relationshipTypeCreate([
208 'name_a_b' => 'Spouse of',
209 'name_b_a' => 'Spouse of',
210 'label_a_b' => 'Spouse of',
211 'label_b_a' => 'Spouse of',
212 'contact_type_a' => 'Individual',
213 'contact_type_b' => 'Individual',
215 $relationshipTypeNames['Spouse of'] = $spouseId;
217 // Maybe unnecessary but why not. Slightly different than an undefined
218 // index later when it doesn't exist at all.
219 $this->assertGreaterThan(0, $relationshipTypeNames['Spouse of']);
221 // Add one with changed labels
222 $id = $this->relationshipTypeCreate([
223 'name_a_b' => 'Wallet Inspector is',
224 'name_b_a' => 'Wallet Inspector',
225 'label_a_b' => 'has as Wallet Inspector',
226 'label_b_a' => 'is Wallet Inspector of',
227 'contact_type_a' => 'Individual',
228 'contact_type_b' => 'Individual',
230 $relationshipTypeNames['Wallet Inspector'] = $id;
232 // Add one with non-ascii characters.
233 $id = $this->relationshipTypeCreate([
234 'name_a_b' => 'абвгде is',
235 'name_b_a' => 'абвгде',
236 'label_a_b' => 'абвгде is',
237 'label_b_a' => 'абвгде',
238 'contact_type_a' => 'Individual',
239 'contact_type_b' => 'Individual',
241 $relationshipTypeNames['Ascii'] = $id;
243 // Add one with non-ascii characters changed labels.
244 $id = $this->relationshipTypeCreate([
245 'name_a_b' => 'αβγδ is',
246 'name_b_a' => 'αβγδ',
247 'label_a_b' => 'αβγδ is changed',
248 'label_b_a' => 'αβγδ changed',
249 'contact_type_a' => 'Individual',
250 'contact_type_b' => 'Individual',
252 $relationshipTypeNames['Ascii changed'] = $id;
254 // Create some case types
255 $caseTypes = $this->createCaseTypes($relationshipTypeNames, 1);
258 $upgrader = new CRM_Upgrade_Incremental_php_FiveTwenty();
259 // first, preupgrade messages should be blank here
260 $preupgradeMessages = $upgrader->_changeCaseTypeLabelToName(TRUE);
261 $this->assertEmpty($preupgradeMessages);
263 // Now the actual run
264 $upgrader->changeCaseTypeLabelToName();
266 // Get the updated case types and check.
268 1 => [implode(',', array_keys($caseTypes)), 'String'],
270 $dao = CRM_Core_DAO
::executeQuery("SELECT id, name, definition FROM civicrm_case_type WHERE id IN (%1)", $sqlParams);
271 while ($dao->fetch()) {
272 $this->assertEquals($caseTypes[$dao->id
]['expected'], $dao->definition
, "Case type {$dao->name}");
274 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_case_type WHERE id = {$dao->id}");
278 // Second pass, where we have some edge cases.
281 // Add a relationship type that has the same labels as another.
282 $id = $this->relationshipTypeCreate([
283 'name_a_b' => 'mixedupab',
284 'name_b_a' => 'mixedupba',
285 'label_a_b' => 'Benefits Specialist',
286 'label_b_a' => 'Benefits Specialist is',
287 'contact_type_a' => 'Individual',
288 'contact_type_b' => 'Individual',
290 $relationshipTypeNames['mixedup'] = $id;
292 // Add a relationship type that appears to be bidirectional but different
294 $id = $this->relationshipTypeCreate([
295 'name_a_b' => 'diffnameab',
296 'name_b_a' => 'diffnameba',
297 'label_a_b' => 'Archenemy of',
298 'label_b_a' => 'Archenemy of',
299 'contact_type_a' => 'Individual',
300 'contact_type_b' => 'Individual',
302 $relationshipTypeNames['diffname'] = $id;
304 // Second pass for case type creation.
305 $caseTypes = $this->createCaseTypes($relationshipTypeNames, 2);
308 $upgrader = new CRM_Upgrade_Incremental_php_FiveTwenty();
309 // first, check preupgrade messages
310 $preupgradeMessages = $upgrader->_changeCaseTypeLabelToName(TRUE);
311 $this->assertEquals($this->getExpectedUpgradeMessages(), $preupgradeMessages);
313 // Now the actual run
314 $upgrader->changeCaseTypeLabelToName();
316 // Get the updated case types and check.
318 1 => [implode(',', array_keys($caseTypes)), 'String'],
320 $dao = CRM_Core_DAO
::executeQuery("SELECT id, name, definition FROM civicrm_case_type WHERE id IN (%1)", $sqlParams);
321 while ($dao->fetch()) {
322 $this->assertEquals($caseTypes[$dao->id
]['expected'], $dao->definition
, "Case type {$dao->name}");
324 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_case_type WHERE id = {$dao->id}");
329 * Set up some original and expected xml pairs.
331 * @param $relationshipTypeNames array
333 * We run it in a couple passes because we want to test with and without
337 private function createCaseTypes($relationshipTypeNames, $stage) {
342 $newCaseTypeXml = <<<ENDXMLSIMPLE
343 <?xml version="1.0" encoding="UTF-8"?>
348 <name>Open Case</name>
349 <max_instances>1</max_instances>
355 <name>Follow up</name>
361 <name>Phone Call</name>
366 <name>standard_timeline</name>
367 <label>Standard Timeline</label>
368 <timeline>true</timeline>
371 <name>Open Case</name>
372 <status>Completed</status>
373 <label>Open Case</label>
374 <default_assignee_type>1</default_assignee_type>
379 <name>timeline_1</name>
380 <label>AnotherTimeline</label>
381 <timeline>true</timeline>
384 <name>Follow up</name>
385 <label>Follow up</label>
386 <status>Scheduled</status>
387 <reference_activity>Open Case</reference_activity>
388 <reference_offset>7</reference_offset>
389 <reference_select>newest</reference_select>
390 <default_assignee_type>2</default_assignee_type>
391 <default_assignee_relationship>{$relationshipTypeNames['Senior Services Coordinator']}_b_a</default_assignee_relationship>
392 <default_assignee_contact></default_assignee_contact>
399 <name>Senior Services Coordinator</name>
404 <name>Spouse of</name>
407 <name>Benefits Specialist is</name>
410 <name>is Wallet Inspector of</name>
413 <name>has as Wallet Inspector</name>
419 <name>αβγδ changed</name>
422 <RestrictActivityAsgmtToCmsUser>0</RestrictActivityAsgmtToCmsUser>
427 $expectedCaseTypeXml = <<<ENDXMLSIMPLEEXPECTED
428 <?xml version="1.0" encoding="UTF-8"?>
433 <name>Open Case</name>
434 <max_instances>1</max_instances>
440 <name>Follow up</name>
446 <name>Phone Call</name>
451 <name>standard_timeline</name>
452 <label>Standard Timeline</label>
453 <timeline>true</timeline>
456 <name>Open Case</name>
457 <status>Completed</status>
458 <label>Open Case</label>
459 <default_assignee_type>1</default_assignee_type>
464 <name>timeline_1</name>
465 <label>AnotherTimeline</label>
466 <timeline>true</timeline>
469 <name>Follow up</name>
470 <label>Follow up</label>
471 <status>Scheduled</status>
472 <reference_activity>Open Case</reference_activity>
473 <reference_offset>7</reference_offset>
474 <reference_select>newest</reference_select>
475 <default_assignee_type>2</default_assignee_type>
476 <default_assignee_relationship>{$relationshipTypeNames['Senior Services Coordinator']}_b_a</default_assignee_relationship>
477 <default_assignee_contact></default_assignee_contact>
484 <name>Senior Services Coordinator</name>
489 <name>Spouse of</name>
492 <name>Benefits Specialist is</name>
495 <name>Wallet Inspector</name>
498 <name>Wallet Inspector is</name>
507 <RestrictActivityAsgmtToCmsUser>0</RestrictActivityAsgmtToCmsUser>
510 ENDXMLSIMPLEEXPECTED;
512 $caseTypeId = $this->addCaseType('simple', $newCaseTypeXml);
513 $xmls[$caseTypeId] = [
515 'expected' => $expectedCaseTypeXml,
520 // Note for these ones the roles that have warnings should remain
521 // unchanged if they choose to continue with the upgrade.
523 $newCaseTypeXml = <<<ENDXMLMIXEDUP
524 <?xml version="1.0" encoding="UTF-8"?>
529 <name>Open Case</name>
530 <max_instances>1</max_instances>
536 <name>Follow up</name>
542 <name>Phone Call</name>
547 <name>standard_timeline</name>
548 <label>Standard Timeline</label>
549 <timeline>true</timeline>
552 <name>Open Case</name>
553 <status>Completed</status>
554 <label>Open Case</label>
555 <default_assignee_type>1</default_assignee_type>
560 <name>timeline_1</name>
561 <label>AnotherTimeline</label>
562 <timeline>true</timeline>
565 <name>Follow up</name>
566 <label>Follow up</label>
567 <status>Scheduled</status>
568 <reference_activity>Open Case</reference_activity>
569 <reference_offset>7</reference_offset>
570 <reference_select>newest</reference_select>
571 <default_assignee_type>2</default_assignee_type>
572 <default_assignee_relationship>{$relationshipTypeNames['Senior Services Coordinator']}_b_a</default_assignee_relationship>
573 <default_assignee_contact></default_assignee_contact>
580 <name>Senior Services Coordinator</name>
585 <name>Spouse of</name>
588 <name>Benefits Specialist is</name>
591 <name>is Wallet Inspector of</name>
594 <name>has as Wallet Inspector</name>
600 <name>αβγδ changed</name>
603 <name>Benefits Specialist</name>
606 <name>Mythical Unicorn</name>
609 <RestrictActivityAsgmtToCmsUser>0</RestrictActivityAsgmtToCmsUser>
614 $expectedCaseTypeXml = <<<ENDXMLMIXEDUPEXPECTED
615 <?xml version="1.0" encoding="UTF-8"?>
620 <name>Open Case</name>
621 <max_instances>1</max_instances>
627 <name>Follow up</name>
633 <name>Phone Call</name>
638 <name>standard_timeline</name>
639 <label>Standard Timeline</label>
640 <timeline>true</timeline>
643 <name>Open Case</name>
644 <status>Completed</status>
645 <label>Open Case</label>
646 <default_assignee_type>1</default_assignee_type>
651 <name>timeline_1</name>
652 <label>AnotherTimeline</label>
653 <timeline>true</timeline>
656 <name>Follow up</name>
657 <label>Follow up</label>
658 <status>Scheduled</status>
659 <reference_activity>Open Case</reference_activity>
660 <reference_offset>7</reference_offset>
661 <reference_select>newest</reference_select>
662 <default_assignee_type>2</default_assignee_type>
663 <default_assignee_relationship>{$relationshipTypeNames['Senior Services Coordinator']}_b_a</default_assignee_relationship>
664 <default_assignee_contact></default_assignee_contact>
671 <name>Senior Services Coordinator</name>
676 <name>Spouse of</name>
679 <name>Benefits Specialist is</name>
682 <name>Wallet Inspector</name>
685 <name>Wallet Inspector is</name>
694 <name>Benefits Specialist</name>
697 <name>Mythical Unicorn</name>
700 <RestrictActivityAsgmtToCmsUser>0</RestrictActivityAsgmtToCmsUser>
703 ENDXMLMIXEDUPEXPECTED;
705 $caseTypeId = $this->addCaseType('mixedup', $newCaseTypeXml);
706 $xmls[$caseTypeId] = [
708 'expected' => $expectedCaseTypeXml,
711 $newCaseTypeXml = <<<ENDXMLDIFFNAME
712 <?xml version="1.0" encoding="UTF-8"?>
714 <name>diffname</name>
717 <name>Open Case</name>
718 <max_instances>1</max_instances>
724 <name>Follow up</name>
730 <name>Phone Call</name>
735 <name>standard_timeline</name>
736 <label>Standard Timeline</label>
737 <timeline>true</timeline>
740 <name>Open Case</name>
741 <status>Completed</status>
742 <label>Open Case</label>
743 <default_assignee_type>1</default_assignee_type>
748 <name>timeline_1</name>
749 <label>AnotherTimeline</label>
750 <timeline>true</timeline>
753 <name>Follow up</name>
754 <label>Follow up</label>
755 <status>Scheduled</status>
756 <reference_activity>Open Case</reference_activity>
757 <reference_offset>7</reference_offset>
758 <reference_select>newest</reference_select>
759 <default_assignee_type>2</default_assignee_type>
760 <default_assignee_relationship>{$relationshipTypeNames['Senior Services Coordinator']}_b_a</default_assignee_relationship>
761 <default_assignee_contact></default_assignee_contact>
768 <name>Senior Services Coordinator</name>
773 <name>Spouse of</name>
776 <name>Benefits Specialist is</name>
779 <name>is Wallet Inspector of</name>
782 <name>has as Wallet Inspector</name>
788 <name>αβγδ changed</name>
791 <name>Archenemy of</name>
794 <RestrictActivityAsgmtToCmsUser>0</RestrictActivityAsgmtToCmsUser>
799 $expectedCaseTypeXml = <<<ENDXMLDIFFNAMEEXPECTED
800 <?xml version="1.0" encoding="UTF-8"?>
802 <name>diffname</name>
805 <name>Open Case</name>
806 <max_instances>1</max_instances>
812 <name>Follow up</name>
818 <name>Phone Call</name>
823 <name>standard_timeline</name>
824 <label>Standard Timeline</label>
825 <timeline>true</timeline>
828 <name>Open Case</name>
829 <status>Completed</status>
830 <label>Open Case</label>
831 <default_assignee_type>1</default_assignee_type>
836 <name>timeline_1</name>
837 <label>AnotherTimeline</label>
838 <timeline>true</timeline>
841 <name>Follow up</name>
842 <label>Follow up</label>
843 <status>Scheduled</status>
844 <reference_activity>Open Case</reference_activity>
845 <reference_offset>7</reference_offset>
846 <reference_select>newest</reference_select>
847 <default_assignee_type>2</default_assignee_type>
848 <default_assignee_relationship>{$relationshipTypeNames['Senior Services Coordinator']}_b_a</default_assignee_relationship>
849 <default_assignee_contact></default_assignee_contact>
856 <name>Senior Services Coordinator</name>
861 <name>Spouse of</name>
864 <name>Benefits Specialist is</name>
867 <name>Wallet Inspector</name>
870 <name>Wallet Inspector is</name>
879 <name>Archenemy of</name>
882 <RestrictActivityAsgmtToCmsUser>0</RestrictActivityAsgmtToCmsUser>
885 ENDXMLDIFFNAMEEXPECTED;
887 $caseTypeId = $this->addCaseType('diffname', $newCaseTypeXml);
888 $xmls[$caseTypeId] = [
890 'expected' => $expectedCaseTypeXml,
905 private function getExpectedUpgradeMessages() {
907 "Case Type 'mixedup', role 'Benefits Specialist is' has an ambiguous configuration where the role matches multiple labels and so can't be automatically updated. See the administration console status messages for more info.",
909 "Case Type 'mixedup', role 'Benefits Specialist' has an ambiguous configuration where the role matches multiple labels and so can't be automatically updated. See the administration console status messages for more info.",
911 "Case Type 'mixedup', role 'Mythical Unicorn' doesn't seem to be a valid role. See the administration console status messages for more info.",
913 "Case Type 'diffname', role 'Benefits Specialist is' has an ambiguous configuration where the role matches multiple labels and so can't be automatically updated. See the administration console status messages for more info.",
915 "Case Type 'diffname', role 'Archenemy of' has an ambiguous configuration and can't be automatically updated. See the administration console status messages for more info.",
920 * Helper to add a case type to the database.
922 * @param $name string
927 private function addCaseType($name, $xml) {
928 $dao = new CRM_Case_DAO_CaseType();
932 $dao->definition
= $xml;