Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ref] Move copyCustomFields function from Event to Core_DAO for re-usability #14171

Merged
merged 2 commits into from
May 2, 2019

Conversation

eileenmcnaughton
Copy link
Contributor

@eileenmcnaughton eileenmcnaughton commented May 1, 2019

Overview

Minor refactor - move function per #13470

Before

Less readable

After

More readable

Technical Details

Note this is JUST a file move to keep it reviewable - I think the function should not be static & some genericisation should happen

Comments

@civibot
Copy link

civibot bot commented May 1, 2019

(Standard links)

@yashodha
Copy link
Contributor

yashodha commented May 1, 2019

@eileenmcnaughton this would break https://github.com/civicrm/civicrm-core/blob/master/CRM/Event/BAO/Event.php#L989 though now that the function has been moved.

@eileenmcnaughton
Copy link
Contributor Author

@yashodha - yeah - I guess it makes sense to make non-static - I just did that

But I realise the copy custom fields shouldn't be needed as it's also in generic. I think what happened is it got fixed in event first in an adhoc way
eileenmcnaughton@6350097

and then fixed generically in copyGeneric but we still have this adhoc stuff hanging around cos it wasn't done right the first time & now we have to clean up.

However the attempt was in 2016 & we have been raising the bar - I don't think that PR would have been accepted today without unit tests - #9407

@eileenmcnaughton
Copy link
Contributor Author

eileenmcnaughton commented May 1, 2019

@yashodha I added a second fix that brings back the genericness - hmm just checking it actually

OK that worked - it looks like with that PR merged then we actually only need to do this

--- a/CRM/Core/DAO.php
+++ b/CRM/Core/DAO.php
@@ -1670,9 +1670,7 @@ FROM civicrm_domain
}
}
$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);
    
    }

To make the custom fields work - I used the test from #13470 but without the tag handling as that would need revisiting -so the test looks like

 /**
   * 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);

    }
  }

@mattwire
Copy link
Contributor

mattwire commented May 2, 2019

Merging as test to follow in subsequent PR

@mattwire mattwire merged commit 5c050e1 into civicrm:master May 2, 2019
@eileenmcnaughton eileenmcnaughton deleted the copy_dao branch May 2, 2019 09:33
@mattwire mattwire changed the title [ref] Move copyCustomFields function from Event to Core_DAO for re-usablibilty [ref] Move copyCustomFields function from Event to Core_DAO for re-usabibilty May 2, 2019
@mattwire mattwire changed the title [ref] Move copyCustomFields function from Event to Core_DAO for re-usabibilty [ref] Move copyCustomFields function from Event to Core_DAO for re-usability May 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants