Skip to content

Commit

Permalink
Fix failure to copy custom fields & add test provided by Agileware
Browse files Browse the repository at this point in the history
Per #13470 custom fields are
inconsistently copied where copying entities. This makes the code from
BAO_Event called from the genericCopy function.

I did a bit of an audit and the places where this is currently called from don't appear
to call the copyGeneric function with the 'custom' param that would have activated the old
code. I also consistently removed the & when it was being called so I could take it
out of the signature.

The original PR handled tags as well, but not in a generic way. I've left that out of scope
but the test is present, commented out, so it would be easy enough to revist
  • Loading branch information
eileenmcnaughton committed May 6, 2019
1 parent 0938c0f commit 3fec1ad
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 21 deletions.
16 changes: 8 additions & 8 deletions CRM/Contribute/BAO/ContributionPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -672,33 +672,33 @@ public static function copy($id) {
'title' => ts('Copy of') . ' ',
],
];
$copy = &CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_ContributionPage', [
$copy = CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_ContributionPage', [
'id' => $id,
], NULL, $fieldsFix);

//copying all the blocks pertaining to the contribution page
$copyPledgeBlock = &CRM_Core_DAO::copyGeneric('CRM_Pledge_DAO_PledgeBlock', [
$copyPledgeBlock = CRM_Core_DAO::copyGeneric('CRM_Pledge_DAO_PledgeBlock', [
'entity_id' => $id,
'entity_table' => 'civicrm_contribution_page',
], [
'entity_id' => $copy->id,
]);

$copyMembershipBlock = &CRM_Core_DAO::copyGeneric('CRM_Member_DAO_MembershipBlock', [
$copyMembershipBlock = CRM_Core_DAO::copyGeneric('CRM_Member_DAO_MembershipBlock', [
'entity_id' => $id,
'entity_table' => 'civicrm_contribution_page',
], [
'entity_id' => $copy->id,
]);

$copyUFJoin = &CRM_Core_DAO::copyGeneric('CRM_Core_DAO_UFJoin', [
$copyUFJoin = CRM_Core_DAO::copyGeneric('CRM_Core_DAO_UFJoin', [
'entity_id' => $id,
'entity_table' => 'civicrm_contribution_page',
], [
'entity_id' => $copy->id,
]);

$copyWidget = &CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_Widget', [
$copyWidget = CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_Widget', [
'contribution_page_id' => $id,
], [
'contribution_page_id' => $copy->id,
Expand All @@ -707,22 +707,22 @@ public static function copy($id) {
//copy price sets
CRM_Price_BAO_PriceSet::copyPriceSet('civicrm_contribution_page', $id, $copy->id);

$copyTellFriend = &CRM_Core_DAO::copyGeneric('CRM_Friend_DAO_Friend', [
$copyTellFriend = CRM_Core_DAO::copyGeneric('CRM_Friend_DAO_Friend', [
'entity_id' => $id,
'entity_table' => 'civicrm_contribution_page',
], [
'entity_id' => $copy->id,
]);

$copyPersonalCampaignPages = &CRM_Core_DAO::copyGeneric('CRM_PCP_DAO_PCPBlock', [
$copyPersonalCampaignPages = CRM_Core_DAO::copyGeneric('CRM_PCP_DAO_PCPBlock', [
'entity_id' => $id,
'entity_table' => 'civicrm_contribution_page',
], [
'entity_id' => $copy->id,
'target_entity_id' => $copy->id,
]);

$copyPremium = &CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_Premium', [
$copyPremium = CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_Premium', [
'entity_id' => $id,
'entity_table' => 'civicrm_contribution_page',
], [
Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/BAO/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public static function copy($id, $params = []) {
],
'replace' => $params,
];
$copy = &CRM_Core_DAO::copyGeneric('CRM_Core_DAO_Job', ['id' => $id], NULL, $fieldsFix);
$copy = CRM_Core_DAO::copyGeneric('CRM_Core_DAO_Job', ['id' => $id], NULL, $fieldsFix);
$copy->save();
CRM_Utils_Hook::copy('Job', $copy);

Expand Down
3 changes: 2 additions & 1 deletion CRM/Core/BAO/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ public static function deleteLocationBlocks($contactId, $locationTypeId) {
* newly created/updated location block id.
*/
public static function copyLocBlock($locBlockId, $updateLocBlockId = NULL) {
CRM_Core_Error::deprecatedFunctionWarning('unused function which will be removed');
//get the location info.
$defaults = $updateValues = [];
$locBlock = ['id' => $locBlockId];
Expand Down Expand Up @@ -344,7 +345,7 @@ public static function copyLocBlock($locBlockId, $updateLocBlockId = NULL) {
}
}

$copyLocation = &CRM_Core_DAO::copyGeneric('CRM_Core_DAO_LocBlock',
$copyLocation = CRM_Core_DAO::copyGeneric('CRM_Core_DAO_LocBlock',
['id' => $locBlock['id']],
$copyLocationParams
);
Expand Down
6 changes: 3 additions & 3 deletions CRM/Core/BAO/UFGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -2692,7 +2692,7 @@ public static function copy($id) {
),
);

$copy = &CRM_Core_DAO::copyGeneric('CRM_Core_DAO_UFGroup',
$copy = CRM_Core_DAO::copyGeneric('CRM_Core_DAO_UFGroup',
array('id' => $id),
NULL,
$fieldsFix
Expand All @@ -2704,14 +2704,14 @@ public static function copy($id) {
$copy->name = CRM_Utils_String::munge($copy->name, '_', 56) . "_{$copy->id}";
$copy->save();

$copyUFJoin = &CRM_Core_DAO::copyGeneric('CRM_Core_DAO_UFJoin',
$copyUFJoin = CRM_Core_DAO::copyGeneric('CRM_Core_DAO_UFJoin',
array('uf_group_id' => $id),
array('uf_group_id' => $copy->id),
NULL,
'entity_table'
);

$copyUFField = &CRM_Core_DAO::copyGeneric('CRM_Core_BAO_UFField',
$copyUFField = CRM_Core_DAO::copyGeneric('CRM_Core_BAO_UFField',
array('uf_group_id' => $id),
array('uf_group_id' => $copy->id)
);
Expand Down
11 changes: 5 additions & 6 deletions CRM/Core/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -1600,11 +1600,12 @@ public static function freeResult($ids = NULL) {
* Fields that you want to block from.
* getting copied
*
* @return CRM_Core_DAO
* the newly created copy of the object
* @return CRM_Core_DAO|bool
* the newly created copy of the object. False if none created.
*/
public static function &copyGeneric($daoName, $criteria, $newData = NULL, $fieldsFix = NULL, $blockCopyOfDependencies = NULL) {
public static function copyGeneric($daoName, $criteria, $newData = NULL, $fieldsFix = NULL, $blockCopyOfDependencies = NULL) {
$object = new $daoName();
$newObject = FALSE;
if (!$newData) {
$object->id = $criteria['id'];
}
Expand Down Expand Up @@ -1670,9 +1671,7 @@ public static function &copyGeneric($daoName, $criteria, $newData = NULL, $field
}
}
$newObject->save();
if (!empty($newData['custom'])) {
CRM_Core_BAO_CustomValueTable::store($newData['custom'], $newObject::getTableName(), $newObject->id);
}
$newObject->copyCustomFields($object->id, $newObject->id);
CRM_Utils_Hook::post('create', CRM_Core_DAO_AllCoreTables::getBriefName($daoName), $newObject->id, $newObject);
}

Expand Down
1 change: 0 additions & 1 deletion CRM/Event/BAO/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,6 @@ public static function copy($id, $params = []) {
['entity_value' => $id, 'mapping_id' => $oldMapping->getId()],
['entity_value' => $copyEvent->id, 'mapping_id' => $copyMapping->getId()]
);
$copyEvent->copyCustomFields($id, $copyEvent->id);

$copyEvent->save();

Expand Down
2 changes: 1 addition & 1 deletion CRM/Price/BAO/PriceSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -1559,7 +1559,7 @@ public static function copyPriceSet($baoName, $id, $newId) {
CRM_Price_BAO_PriceSet::addTo($baoName, $newId, $copyPriceSet->id);
}
else {
$copyPriceSet = &CRM_Core_DAO::copyGeneric('CRM_Price_DAO_PriceSetEntity',
$copyPriceSet = CRM_Core_DAO::copyGeneric('CRM_Price_DAO_PriceSetEntity',
[
'entity_id' => $id,
'entity_table' => $baoName,
Expand Down
80 changes: 80 additions & 0 deletions tests/phpunit/CRM/Core/BAO/RecurringEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,84 @@ public function testEventGeneration() {
$this->assertDBCompareValues('CRM_Friend_DAO_Friend', $searchActParams, $compareActParams);
}

/**
* Testing Activity Generation through Entity Recursion with Custom Data and Tags.
*/
public function testRecurringEntityGenerationWithCustomDataAndTags() {

// Create custom group and field
$customGroup = $this->customGroupCreate([
'extends' => 'Activity',
]);
$customField = $this->customFieldCreate([
'custom_group_id' => $customGroup['id'],
'default_value' => '',
]
);

// Create activity Tag
$tag = $this->tagCreate([
'used_for' => 'Activities',
]);

// Create original activity
$customFieldValue = 'Custom Value';
$activityDateTime = date('YmdHis');
$activityId = $this->activityCreate([
'activity_date_time' => $activityDateTime,
'custom_' . $customField['id'] => $customFieldValue,
]);

$activityId = $activityId['id'];

// Assign tag to a activity.
$this->callAPISuccess('EntityTag', 'create', [
'entity_table' => 'civicrm_activity',
'entity_id' => $activityId,
'tag_id' => $tag['id'],
]);

// Create recurring activities.
$recursion = new CRM_Core_BAO_RecurringEntity();
$recursion->entity_id = $activityId;
$recursion->entity_table = 'civicrm_activity';
$recursion->dateColumns = ['activity_date_time'];
$recursion->schedule = [
'entity_value' => $activityId,
'start_action_date' => $activityDateTime,
'entity_status' => 'fourth saturday',
'repetition_frequency_unit' => 'month',
'repetition_frequency_interval' => 3,
'start_action_offset' => 3,
'used_for' => 'activity',
];

$generatedEntities = $recursion->generate();
$generatedActivities = $generatedEntities['civicrm_activity'];

$this->assertEquals(3, count($generatedActivities), "Check if number of iterations are 3");

foreach ($generatedActivities as $generatedActivityId) {

/* Validate tag in recurring activity
// @todo - refer https://github.com/civicrm/civicrm-core/pull/13470
$this->callAPISuccess('EntityTag', 'getsingle', [
'entity_table' => 'civicrm_activity',
'entity_id' => $generatedActivityId,
]);
*/

// Validate custom data in recurring activity
$activity = $this->callAPISuccess('activity', 'getsingle', [
'return' => [
'custom_' . $customField['id'],
],
'id' => $generatedActivityId,
]);

$this->assertEquals($customFieldValue, $activity['custom_' . $customField['id']], 'Custom field value should be ' . $customFieldValue);

}
}

}

0 comments on commit 3fec1ad

Please sign in to comment.