2 require_once 'CiviTest/CiviCaseTestCase.php';
5 * Class CRM_Upgrade_Incremental_php_FiveTwentyTest
8 class CRM_Upgrade_Incremental_php_FiveTwentyTest
extends CiviCaseTestCase
{
10 public function setUp() {
15 * Test that the upgrade task changes the direction but only
16 * for bidirectional relationship types that are b_a.
18 public function testChangeCaseTypeAutoassignee() {
20 // We don't know what the ids are for the relationship types since it
21 // seems to depend what ran before us, so retrieve them first and go by
23 // Also spouse might not exist.
25 $result = $this->callAPISuccess('RelationshipType', 'get', ['limit' => 0])['values'];
26 // Get list of ids keyed on name.
27 $relationshipTypeNames = array_column($result, 'id', 'name_b_a');
29 // Create spouse if none.
30 if (!isset($relationshipTypeNames['Spouse of'])) {
31 $spouseId = $this->relationshipTypeCreate([
32 'name_a_b' => 'Spouse of',
33 'name_b_a' => 'Spouse of',
34 'label_a_b' => 'Spouse of',
35 'label_b_a' => 'Spouse of',
36 'contact_type_a' => 'Individual',
37 'contact_type_b' => 'Individual',
39 $relationshipTypeNames['Spouse of'] = $spouseId;
41 // Maybe unnecessary but why not. Slightly different than an undefined
42 // index later when it doesn't exist at all.
43 $this->assertGreaterThan(0, $relationshipTypeNames['Spouse of']);
47 * In this sample case type for autoassignees we have:
48 * - a_b unidirectional: Case Coordinator is
49 * - b_a unidirectional: Benefits Specialist
50 * - Bidirectional the way it's stored in 5.16+: Spouse of
51 * - config entry pre-5.16, where it's a bidirectional relationship stored as b_a: Spouse of
53 * Also just for extra some non-ascii chars in here to show they
56 $newCaseTypeXml = <<<ENDXML
57 <?xml version="1.0" encoding="UTF-8"?>
59 <name>test_type</name>
62 <name>Open Case</name>
63 <max_instances>1</max_instances>
69 <name>Follow up</name>
75 <name>Phone Call</name>
82 <name>standard_timeline</name>
83 <label>Standard Timeline</label>
84 <timeline>true</timeline>
87 <name>Open Case</name>
88 <status>Completed</status>
89 <label>Open Case</label>
90 <default_assignee_type>1</default_assignee_type>
95 <name>timeline_1</name>
96 <label>AnotherTimeline</label>
97 <timeline>true</timeline>
100 <name>Follow up</name>
101 <label>Follow up</label>
102 <status>Scheduled</status>
103 <reference_activity>Open Case</reference_activity>
104 <reference_offset>7</reference_offset>
105 <reference_select>newest</reference_select>
106 <default_assignee_type>2</default_assignee_type>
107 <default_assignee_relationship>{$relationshipTypeNames['Senior Services Coordinator']}_b_a</default_assignee_relationship>
108 <default_assignee_contact></default_assignee_contact>
111 <name>Follow up</name>
112 <label>Follow up</label>
113 <status>Scheduled</status>
114 <reference_activity>Open Case</reference_activity>
115 <reference_offset>14</reference_offset>
116 <reference_select>newest</reference_select>
117 <default_assignee_type>2</default_assignee_type>
118 <default_assignee_relationship>{$relationshipTypeNames['Benefits Specialist']}_a_b</default_assignee_relationship>
119 <default_assignee_contact></default_assignee_contact>
122 <name>Follow up</name>
123 <label>Follow up</label>
124 <status>Scheduled</status>
125 <reference_activity>Open Case</reference_activity>
126 <reference_offset>21</reference_offset>
127 <reference_select>newest</reference_select>
128 <default_assignee_type>2</default_assignee_type>
129 <default_assignee_relationship>{$relationshipTypeNames['Spouse of']}_a_b</default_assignee_relationship>
130 <default_assignee_contact></default_assignee_contact>
133 <name>Follow up</name>
134 <label>Follow up</label>
135 <status>Scheduled</status>
136 <reference_activity>Open Case</reference_activity>
137 <reference_offset>28</reference_offset>
138 <reference_select>newest</reference_select>
139 <default_assignee_type>2</default_assignee_type>
140 <default_assignee_relationship>{$relationshipTypeNames['Spouse of']}_b_a</default_assignee_relationship>
141 <default_assignee_contact></default_assignee_contact>
148 <name>Senior Services Coordinator</name>
153 <name>Spouse of</name>
156 <name>Benefits Specialist is</name>
159 <RestrictActivityAsgmtToCmsUser>0</RestrictActivityAsgmtToCmsUser>
164 $dao = new CRM_Case_DAO_CaseType();
165 $dao->name
= 'test_type';
166 $dao->title
= 'Test Type';
168 $dao->definition
= $newCaseTypeXml;
171 $caseTypeId = $dao->id
;
174 $upgrader = new CRM_Upgrade_Incremental_php_FiveTwenty();
175 $upgrader->changeCaseTypeAutoassignee();
177 // Check if the case type is what we expect. It should be identical except
178 // the b_a spouse one should get converted to the a_b direction.
179 $expectedCaseTypeXml = str_replace(
180 "<default_assignee_relationship>{$relationshipTypeNames['Spouse of']}_b_a",
181 "<default_assignee_relationship>{$relationshipTypeNames['Spouse of']}_a_b",
185 //echo $expectedCaseTypeXml;
187 // Get the updated case type and check.
188 $dao = CRM_Core_DAO
::executeQuery("SELECT definition FROM civicrm_case_type WHERE id = {$caseTypeId}");
191 $this->assertEquals($expectedCaseTypeXml, $dao->definition
);
195 * Test that the upgrade task converts case role <name>'s that
196 * are labels to their name.
198 public function testConvertRoleLabelsToNames() {
200 // We don't know what the ids are for the relationship types since it
201 // seems to depend what ran before us, so retrieve them first and go by
203 // Also spouse might not exist.
205 $result = $this->callAPISuccess('RelationshipType', 'get', ['limit' => 0])['values'];
206 // Get list of ids keyed on name.
207 $relationshipTypeNames = array_column($result, 'id', 'name_b_a');
209 // Create spouse if none.
210 if (!isset($relationshipTypeNames['Spouse of'])) {
211 $spouseId = $this->relationshipTypeCreate([
212 'name_a_b' => 'Spouse of',
213 'name_b_a' => 'Spouse of',
214 'label_a_b' => 'Spouse of',
215 'label_b_a' => 'Spouse of',
216 'contact_type_a' => 'Individual',
217 'contact_type_b' => 'Individual',
219 $relationshipTypeNames['Spouse of'] = $spouseId;
221 // Maybe unnecessary but why not. Slightly different than an undefined
222 // index later when it doesn't exist at all.
223 $this->assertGreaterThan(0, $relationshipTypeNames['Spouse of']);
225 // Add one with changed labels
226 $id = $this->relationshipTypeCreate([
227 'name_a_b' => 'Wallet Inspector is',
228 'name_b_a' => 'Wallet Inspector',
229 'label_a_b' => 'has as Wallet Inspector',
230 'label_b_a' => 'is Wallet Inspector of',
231 'contact_type_a' => 'Individual',
232 'contact_type_b' => 'Individual',
234 $relationshipTypeNames['Wallet Inspector'] = $id;
236 // Add one with non-ascii characters.
237 $id = $this->relationshipTypeCreate([
238 'name_a_b' => 'абвгде is',
239 'name_b_a' => 'абвгде',
240 'label_a_b' => 'абвгде is',
241 'label_b_a' => 'абвгде',
242 'contact_type_a' => 'Individual',
243 'contact_type_b' => 'Individual',
245 $relationshipTypeNames['Ascii'] = $id;
247 // Add one with non-ascii characters changed labels.
248 $id = $this->relationshipTypeCreate([
249 'name_a_b' => 'αβγδ is',
250 'name_b_a' => 'αβγδ',
251 'label_a_b' => 'αβγδ is changed',
252 'label_b_a' => 'αβγδ changed',
253 'contact_type_a' => 'Individual',
254 'contact_type_b' => 'Individual',
256 $relationshipTypeNames['Ascii changed'] = $id;
258 // Create some case types
259 $caseTypes = $this->createCaseTypes($relationshipTypeNames, 1);
262 $upgrader = new CRM_Upgrade_Incremental_php_FiveTwenty();
263 // first, preupgrade messages should be blank here
264 $preupgradeMessages = $upgrader->_changeCaseTypeLabelToName(TRUE);
265 $this->assertEmpty($preupgradeMessages);
267 // Now the actual run
268 $upgrader->changeCaseTypeLabelToName();
270 // Get the updated case types and check.
272 1 => [implode(',', array_keys($caseTypes)), 'String'],
274 $dao = CRM_Core_DAO
::executeQuery("SELECT id, name, definition FROM civicrm_case_type WHERE id IN (%1)", $sqlParams);
275 while ($dao->fetch()) {
276 $this->assertEquals($caseTypes[$dao->id
]['expected'], $dao->definition
, "Case type {$dao->name}");
278 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_case_type WHERE id = {$dao->id}");
282 // Second pass, where we have some edge cases.
285 // Add a relationship type that has the same labels as another.
286 $id = $this->relationshipTypeCreate([
287 'name_a_b' => 'mixedupab',
288 'name_b_a' => 'mixedupba',
289 'label_a_b' => 'Benefits Specialist',
290 'label_b_a' => 'Benefits Specialist is',
291 'contact_type_a' => 'Individual',
292 'contact_type_b' => 'Individual',
294 $relationshipTypeNames['mixedup'] = $id;
296 // Add a relationship type that appears to be bidirectional but different
298 $id = $this->relationshipTypeCreate([
299 'name_a_b' => 'diffnameab',
300 'name_b_a' => 'diffnameba',
301 'label_a_b' => 'Archenemy of',
302 'label_b_a' => 'Archenemy of',
303 'contact_type_a' => 'Individual',
304 'contact_type_b' => 'Individual',
306 $relationshipTypeNames['diffname'] = $id;
308 // Second pass for case type creation.
309 $caseTypes = $this->createCaseTypes($relationshipTypeNames, 2);
312 $upgrader = new CRM_Upgrade_Incremental_php_FiveTwenty();
313 // first, check preupgrade messages
314 $preupgradeMessages = $upgrader->_changeCaseTypeLabelToName(TRUE);
315 $this->assertEquals($this->getExpectedUpgradeMessages(), $preupgradeMessages);
317 // Now the actual run
318 $upgrader->changeCaseTypeLabelToName();
320 // Get the updated case types and check.
322 1 => [implode(',', array_keys($caseTypes)), 'String'],
324 $dao = CRM_Core_DAO
::executeQuery("SELECT id, name, definition FROM civicrm_case_type WHERE id IN (%1)", $sqlParams);
325 while ($dao->fetch()) {
326 $this->assertEquals($caseTypes[$dao->id
]['expected'], $dao->definition
, "Case type {$dao->name}");
328 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_case_type WHERE id = {$dao->id}");
333 * Set up some original and expected xml pairs.
335 * @param $relationshipTypeNames array
337 * We run it in a couple passes because we want to test with and without
341 private function createCaseTypes($relationshipTypeNames, $stage) {
346 $newCaseTypeXml = <<<ENDXMLSIMPLE
347 <?xml version="1.0" encoding="UTF-8"?>
352 <name>Open Case</name>
353 <max_instances>1</max_instances>
359 <name>Follow up</name>
365 <name>Phone Call</name>
370 <name>standard_timeline</name>
371 <label>Standard Timeline</label>
372 <timeline>true</timeline>
375 <name>Open Case</name>
376 <status>Completed</status>
377 <label>Open Case</label>
378 <default_assignee_type>1</default_assignee_type>
383 <name>timeline_1</name>
384 <label>AnotherTimeline</label>
385 <timeline>true</timeline>
388 <name>Follow up</name>
389 <label>Follow up</label>
390 <status>Scheduled</status>
391 <reference_activity>Open Case</reference_activity>
392 <reference_offset>7</reference_offset>
393 <reference_select>newest</reference_select>
394 <default_assignee_type>2</default_assignee_type>
395 <default_assignee_relationship>{$relationshipTypeNames['Senior Services Coordinator']}_b_a</default_assignee_relationship>
396 <default_assignee_contact></default_assignee_contact>
403 <name>Senior Services Coordinator</name>
408 <name>Spouse of</name>
411 <name>Benefits Specialist is</name>
414 <name>is Wallet Inspector of</name>
417 <name>has as Wallet Inspector</name>
423 <name>αβγδ changed</name>
426 <RestrictActivityAsgmtToCmsUser>0</RestrictActivityAsgmtToCmsUser>
431 $expectedCaseTypeXml = <<<ENDXMLSIMPLEEXPECTED
432 <?xml version="1.0" encoding="UTF-8"?>
437 <name>Open Case</name>
438 <max_instances>1</max_instances>
444 <name>Follow up</name>
450 <name>Phone Call</name>
455 <name>standard_timeline</name>
456 <label>Standard Timeline</label>
457 <timeline>true</timeline>
460 <name>Open Case</name>
461 <status>Completed</status>
462 <label>Open Case</label>
463 <default_assignee_type>1</default_assignee_type>
468 <name>timeline_1</name>
469 <label>AnotherTimeline</label>
470 <timeline>true</timeline>
473 <name>Follow up</name>
474 <label>Follow up</label>
475 <status>Scheduled</status>
476 <reference_activity>Open Case</reference_activity>
477 <reference_offset>7</reference_offset>
478 <reference_select>newest</reference_select>
479 <default_assignee_type>2</default_assignee_type>
480 <default_assignee_relationship>{$relationshipTypeNames['Senior Services Coordinator']}_b_a</default_assignee_relationship>
481 <default_assignee_contact></default_assignee_contact>
488 <name>Senior Services Coordinator</name>
493 <name>Spouse of</name>
496 <name>Benefits Specialist is</name>
499 <name>Wallet Inspector</name>
502 <name>Wallet Inspector is</name>
511 <RestrictActivityAsgmtToCmsUser>0</RestrictActivityAsgmtToCmsUser>
514 ENDXMLSIMPLEEXPECTED;
516 $caseTypeId = $this->addCaseType('simple', $newCaseTypeXml);
517 $xmls[$caseTypeId] = [
519 'expected' => $expectedCaseTypeXml,
524 // Note for these ones the roles that have warnings should remain
525 // unchanged if they choose to continue with the upgrade.
527 $newCaseTypeXml = <<<ENDXMLMIXEDUP
528 <?xml version="1.0" encoding="UTF-8"?>
533 <name>Open Case</name>
534 <max_instances>1</max_instances>
540 <name>Follow up</name>
546 <name>Phone Call</name>
551 <name>standard_timeline</name>
552 <label>Standard Timeline</label>
553 <timeline>true</timeline>
556 <name>Open Case</name>
557 <status>Completed</status>
558 <label>Open Case</label>
559 <default_assignee_type>1</default_assignee_type>
564 <name>timeline_1</name>
565 <label>AnotherTimeline</label>
566 <timeline>true</timeline>
569 <name>Follow up</name>
570 <label>Follow up</label>
571 <status>Scheduled</status>
572 <reference_activity>Open Case</reference_activity>
573 <reference_offset>7</reference_offset>
574 <reference_select>newest</reference_select>
575 <default_assignee_type>2</default_assignee_type>
576 <default_assignee_relationship>{$relationshipTypeNames['Senior Services Coordinator']}_b_a</default_assignee_relationship>
577 <default_assignee_contact></default_assignee_contact>
584 <name>Senior Services Coordinator</name>
589 <name>Spouse of</name>
592 <name>Benefits Specialist is</name>
595 <name>is Wallet Inspector of</name>
598 <name>has as Wallet Inspector</name>
604 <name>αβγδ changed</name>
607 <name>Benefits Specialist</name>
610 <name>Mythical Unicorn</name>
613 <RestrictActivityAsgmtToCmsUser>0</RestrictActivityAsgmtToCmsUser>
618 $expectedCaseTypeXml = <<<ENDXMLMIXEDUPEXPECTED
619 <?xml version="1.0" encoding="UTF-8"?>
624 <name>Open Case</name>
625 <max_instances>1</max_instances>
631 <name>Follow up</name>
637 <name>Phone Call</name>
642 <name>standard_timeline</name>
643 <label>Standard Timeline</label>
644 <timeline>true</timeline>
647 <name>Open Case</name>
648 <status>Completed</status>
649 <label>Open Case</label>
650 <default_assignee_type>1</default_assignee_type>
655 <name>timeline_1</name>
656 <label>AnotherTimeline</label>
657 <timeline>true</timeline>
660 <name>Follow up</name>
661 <label>Follow up</label>
662 <status>Scheduled</status>
663 <reference_activity>Open Case</reference_activity>
664 <reference_offset>7</reference_offset>
665 <reference_select>newest</reference_select>
666 <default_assignee_type>2</default_assignee_type>
667 <default_assignee_relationship>{$relationshipTypeNames['Senior Services Coordinator']}_b_a</default_assignee_relationship>
668 <default_assignee_contact></default_assignee_contact>
675 <name>Senior Services Coordinator</name>
680 <name>Spouse of</name>
683 <name>Benefits Specialist is</name>
686 <name>Wallet Inspector</name>
689 <name>Wallet Inspector is</name>
698 <name>Benefits Specialist</name>
701 <name>Mythical Unicorn</name>
704 <RestrictActivityAsgmtToCmsUser>0</RestrictActivityAsgmtToCmsUser>
707 ENDXMLMIXEDUPEXPECTED;
709 $caseTypeId = $this->addCaseType('mixedup', $newCaseTypeXml);
710 $xmls[$caseTypeId] = [
712 'expected' => $expectedCaseTypeXml,
715 $newCaseTypeXml = <<<ENDXMLDIFFNAME
716 <?xml version="1.0" encoding="UTF-8"?>
718 <name>diffname</name>
721 <name>Open Case</name>
722 <max_instances>1</max_instances>
728 <name>Follow up</name>
734 <name>Phone Call</name>
739 <name>standard_timeline</name>
740 <label>Standard Timeline</label>
741 <timeline>true</timeline>
744 <name>Open Case</name>
745 <status>Completed</status>
746 <label>Open Case</label>
747 <default_assignee_type>1</default_assignee_type>
752 <name>timeline_1</name>
753 <label>AnotherTimeline</label>
754 <timeline>true</timeline>
757 <name>Follow up</name>
758 <label>Follow up</label>
759 <status>Scheduled</status>
760 <reference_activity>Open Case</reference_activity>
761 <reference_offset>7</reference_offset>
762 <reference_select>newest</reference_select>
763 <default_assignee_type>2</default_assignee_type>
764 <default_assignee_relationship>{$relationshipTypeNames['Senior Services Coordinator']}_b_a</default_assignee_relationship>
765 <default_assignee_contact></default_assignee_contact>
772 <name>Senior Services Coordinator</name>
777 <name>Spouse of</name>
780 <name>Benefits Specialist is</name>
783 <name>is Wallet Inspector of</name>
786 <name>has as Wallet Inspector</name>
792 <name>αβγδ changed</name>
795 <name>Archenemy of</name>
798 <RestrictActivityAsgmtToCmsUser>0</RestrictActivityAsgmtToCmsUser>
803 $expectedCaseTypeXml = <<<ENDXMLDIFFNAMEEXPECTED
804 <?xml version="1.0" encoding="UTF-8"?>
806 <name>diffname</name>
809 <name>Open Case</name>
810 <max_instances>1</max_instances>
816 <name>Follow up</name>
822 <name>Phone Call</name>
827 <name>standard_timeline</name>
828 <label>Standard Timeline</label>
829 <timeline>true</timeline>
832 <name>Open Case</name>
833 <status>Completed</status>
834 <label>Open Case</label>
835 <default_assignee_type>1</default_assignee_type>
840 <name>timeline_1</name>
841 <label>AnotherTimeline</label>
842 <timeline>true</timeline>
845 <name>Follow up</name>
846 <label>Follow up</label>
847 <status>Scheduled</status>
848 <reference_activity>Open Case</reference_activity>
849 <reference_offset>7</reference_offset>
850 <reference_select>newest</reference_select>
851 <default_assignee_type>2</default_assignee_type>
852 <default_assignee_relationship>{$relationshipTypeNames['Senior Services Coordinator']}_b_a</default_assignee_relationship>
853 <default_assignee_contact></default_assignee_contact>
860 <name>Senior Services Coordinator</name>
865 <name>Spouse of</name>
868 <name>Benefits Specialist is</name>
871 <name>Wallet Inspector</name>
874 <name>Wallet Inspector is</name>
883 <name>Archenemy of</name>
886 <RestrictActivityAsgmtToCmsUser>0</RestrictActivityAsgmtToCmsUser>
889 ENDXMLDIFFNAMEEXPECTED;
891 $caseTypeId = $this->addCaseType('diffname', $newCaseTypeXml);
892 $xmls[$caseTypeId] = [
894 'expected' => $expectedCaseTypeXml,
909 private function getExpectedUpgradeMessages() {
911 "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.",
913 "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.",
915 "Case Type 'mixedup', role 'Mythical Unicorn' doesn't seem to be a valid role. See the administration console status messages for more info.",
917 "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.",
919 "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.",
924 * Helper to add a case type to the database.
926 * @param $name string
931 private function addCaseType($name, $xml) {
932 $dao = new CRM_Case_DAO_CaseType();
936 $dao->definition
= $xml;