+ /**
+ * Test that caseRoles() doesn't have name and label mixed up.
+ *
+ * @param $key string The array key in the moreRelationshipTypes array that
+ * is the relationship type we're currently testing. So not necessarily
+ * unique for each entry in the dataprovider since want to test a given
+ * relationship type against multiple xml strings. It's not a test
+ * identifier, it's an array key to use to look up something.
+ * @param string $xmlString
+ * @param array $expected
+ * @param $dontcare array We're re-using the data provider for two tests and
+ * we don't care about those expected values.
+ *
+ * @dataProvider xmlCaseRoleDataProvider
+ */
+ public function testCaseRoles($key, $xmlString, $expected, $dontcare) {
+ $xmlObj = new SimpleXMLElement($xmlString);
+
+ // element 0 is direction (a_b), 1 is the text we want
+ $expectedArray = empty($expected) ? [] : ["{$this->moreRelationshipTypes[$key]['type_id']}_{$expected[0]}" => $expected[1]];
+
+ $this->assertEquals($expectedArray, $this->process->caseRoles($xmlObj->CaseRoles, FALSE));
+ }
+
+ /**
+ * Test that locateNameOrLabel doesn't have name and label mixed up.
+ *
+ * @param $key string The array key in the moreRelationshipTypes array that
+ * is the relationship type we're currently testing. So not necessarily
+ * unique for each entry in the dataprovider since want to test a given
+ * relationship type against multiple xml strings. It's not a test
+ * identifier, it's an array key to use to look up something.
+ * @param string $xmlString
+ * @param $dontcare array We're re-using the data provider for two tests and
+ * we don't care about those expected values.
+ * @param array $expected
+ *
+ * @dataProvider xmlCaseRoleDataProvider
+ */
+ public function testLocateNameOrLabel($key, $xmlString, $dontcare, $expected) {
+ $xmlObj = new SimpleXMLElement($xmlString);
+
+ // element 0 is direction (a_b), 1 is the text we want.
+ // In case of failure, the function is expected to return FALSE for the
+ // direction and then for the text it just gives us back the string we
+ // gave it.
+ $expectedArray = empty($expected[0])
+ ? [FALSE, $expected[1]]
+ : ["{$this->moreRelationshipTypes[$key]['type_id']}_{$expected[0]}", $expected[1]];
+
+ $this->assertEquals($expectedArray, $this->process->locateNameOrLabel($xmlObj->CaseRoles->RelationshipType));
+ }
+
+ /**
+ * Data provider for testCaseRoles and testLocateNameOrLabel
+ * @return array
+ */
+ public function xmlCaseRoleDataProvider() {
+ return [
+ // Simulate one that has been converted to the format it should be going
+ // forward, where name is the actual name, i.e. same as machineName.
+ [
+ // this is the array key in the $this->moreRelationshipTypes array
+ 'unidirectional_name_label_different',
+ // some xml
+ '<CaseType><CaseRoles><RelationshipType><name>jm7ba</name><creator>1</creator><manager>1</manager></RelationshipType></CaseRoles></CaseType>',
+ // this is the expected for testCaseRoles
+ ['a_b', 'Jedi Master is'],
+ // this is the expected for testLocateNameOrLabel
+ ['a_b', 'jm7ba'],
+ ],
+ // Simulate one that is still in label format, i.e. one that is still in
+ // xml files that haven't been updated, or in the db but upgrade script
+ // not run yet.
+ [
+ 'unidirectional_name_label_different',
+ '<CaseType><CaseRoles><RelationshipType><name>Jedi Master for</name><creator>1</creator><manager>1</manager></RelationshipType></CaseRoles></CaseType>',
+ ['a_b', 'Jedi Master is'],
+ ['a_b', 'jm7ba'],
+ ],
+ // Ditto but where we know name and label are the same in the db.
+ [
+ 'unidirectional_name_label_same',
+ '<CaseType><CaseRoles><RelationshipType><name>Quilt Maker for</name><creator>1</creator><manager>1</manager></RelationshipType></CaseRoles></CaseType>',
+ ['a_b', 'Quilt Maker is'],
+ ['a_b', 'Quilt Maker for'],
+ ],
+ // Simulate one that is messed up and should fail, e.g. like a typo
+ // in an xml file. Here we've made a typo on purpose.
+ [
+ 'unidirectional_name_label_different',
+ '<CaseType><CaseRoles><RelationshipType><name>Jedi Masterrrr for</name><creator>1</creator><manager>1</manager></RelationshipType></CaseRoles></CaseType>',
+ NULL,
+ [FALSE, 'Jedi Masterrrr for'],
+ ],
+ // Now some similar tests to above but for bidirectional relationships.
+ // Bidirectional relationship, name and label different, using machine name.
+ [
+ 'bidirectional_name_label_different',
+ '<CaseType><CaseRoles><RelationshipType><name>f12</name><creator>1</creator><manager>1</manager></RelationshipType></CaseRoles></CaseType>',
+ ['b_a', 'Friend of'],
+ ['b_a', 'f12'],
+ ],
+ // Bidirectional relationship, name and label different, using display label.
+ [
+ 'bidirectional_name_label_different',
+ '<CaseType><CaseRoles><RelationshipType><name>Friend of</name><creator>1</creator><manager>1</manager></RelationshipType></CaseRoles></CaseType>',
+ ['b_a', 'Friend of'],
+ ['b_a', 'f12'],
+ ],
+ // Bidirectional relationship, name and label same.
+ [
+ 'bidirectional_name_label_same',
+ '<CaseType><CaseRoles><RelationshipType><name>Enemy of</name><creator>1</creator><manager>1</manager></RelationshipType></CaseRoles></CaseType>',
+ ['b_a', 'Enemy of'],
+ ['b_a', 'Enemy of'],
+ ],
+ ];
+ }
+
+ /**
+ * Test XMLProcessor activityTypes()
+ */
+ public function testXmlProcessorActivityTypes() {
+ // First change an activity's label since we also test getting the labels.
+ // @todo Having a brain freeze or something - can't do this in one step?
+ $activity_type_id = $this->callApiSuccess('OptionValue', 'get', [
+ 'option_group_id' => 'activity_type',
+ 'name' => 'Medical evaluation',
+ ])['id'];
+ $this->callApiSuccess('OptionValue', 'create', [
+ 'id' => $activity_type_id,
+ 'label' => 'Medical evaluation changed',
+ ]);
+
+ $p = new CRM_Case_XMLProcessor_Process();
+ $xml = $p->retrieve('housing_support');
+
+ // Test getting the `name`s
+ $activityTypes = $p->activityTypes($xml->ActivityTypes, FALSE, FALSE, FALSE);
+ $this->assertEquals(
+ [
+ 13 => 'Open Case',
+ 55 => 'Medical evaluation',
+ 56 => 'Mental health evaluation',
+ 57 => 'Secure temporary housing',
+ 60 => 'Income and benefits stabilization',
+ 58 => 'Long-term housing plan',
+ 14 => 'Follow up',
+ 15 => 'Change Case Type',
+ 16 => 'Change Case Status',
+ 18 => 'Change Case Start Date',
+ 25 => 'Link Cases',
+ ],
+ $activityTypes
+ );
+
+ // While we're here and have the `name`s check the editable types in
+ // Settings.xml which is something that gets called reasonably often
+ // thru CRM_Case_XMLProcessor_Process::activityTypes().
+ $activityTypeValues = array_flip($activityTypes);
+ $xml = $p->retrieve('Settings');
+ $settings = $p->activityTypes($xml->ActivityTypes, FALSE, FALSE, 'edit');
+ $this->assertEquals(
+ [
+ 'edit' => [
+ 0 => $activityTypeValues['Change Case Status'],
+ 1 => $activityTypeValues['Change Case Start Date'],
+ ],
+ ],
+ $settings
+ );
+
+ // Now get `label`s
+ $xml = $p->retrieve('housing_support');
+ $activityTypes = $p->activityTypes($xml->ActivityTypes, FALSE, TRUE, FALSE);
+ $this->assertEquals(
+ [
+ 13 => 'Open Case',
+ 55 => 'Medical evaluation changed',
+ 56 => 'Mental health evaluation',
+ 57 => 'Secure temporary housing',
+ 60 => 'Income and benefits stabilization',
+ 58 => 'Long-term housing plan',
+ 14 => 'Follow up',
+ 15 => 'Change Case Type',
+ 16 => 'Change Case Status',
+ 18 => 'Change Case Start Date',
+ 25 => 'Link Cases',
+ ],
+ $activityTypes
+ );
+ }
+