Skip to content

Commit

Permalink
Merge pull request #12221 from eileenmcnaughton/api_extend_mailing_job
Browse files Browse the repository at this point in the history
Add api testing / custom data support for MailingJob.
  • Loading branch information
monishdeb authored May 30, 2018
2 parents 8915563 + f487e35 commit 7f1a99f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 42 deletions.
59 changes: 22 additions & 37 deletions CRM/Mailing/BAO/MailingJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,20 @@ public function __construct() {
* @throws \CRM_Core_Exception
*/
static public function create($params) {
$job = new CRM_Mailing_BAO_MailingJob();
$job->mailing_id = $params['mailing_id'];
$job->status = $params['status'];
$job->scheduled_date = $params['scheduled_date'];
$job->is_test = $params['is_test'];
$job->save();
if ($params['mailing_id']) {
CRM_Mailing_BAO_Mailing::getRecipients($params['mailing_id']);
return $job;
}
else {
if (empty($params['id']) && empty($params['mailing_id'])) {
throw new CRM_Core_Exception("Failed to create job: Unknown mailing ID");
}
$op = empty($params['id']) ? 'create' : 'edit';
CRM_Utils_Hook::pre($op, 'MailingJob', CRM_Utils_Array::value('id', $params), $params);

$jobDAO = new CRM_Mailing_BAO_MailingJob();
$jobDAO->copyValues($params, TRUE);
$jobDAO->save();
if (!empty($params['mailing_id'])) {
CRM_Mailing_BAO_Mailing::getRecipients($params['mailing_id']);
}
CRM_Utils_Hook::post($op, 'MailingJob', $jobDAO->id, $jobDAO);
return $jobDAO;
}

/**
Expand All @@ -89,7 +90,6 @@ static public function create($params) {
public static function runJobs($testParams = NULL, $mode = NULL) {
$job = new CRM_Mailing_BAO_MailingJob();

$config = CRM_Core_Config::singleton();
$jobTable = CRM_Mailing_DAO_MailingJob::getTableName();
$mailingTable = CRM_Mailing_DAO_Mailing::getTableName();
$mailerBatchLimit = Civi::settings()->get('mailerBatchLimit');
Expand Down Expand Up @@ -173,12 +173,12 @@ public static function runJobs($testParams = NULL, $mode = NULL) {
// get the parent ID, and limit and offset
$job->queue($testParams);

// Mark up the starting time
$saveJob = new CRM_Mailing_DAO_MailingJob();
$saveJob->id = $job->id;
$saveJob->start_date = date('YmdHis');
$saveJob->status = 'Running';
$saveJob->save();
// Update to show job has started.
self::create([
'id' => $job->id,
'start_date' => date('YmdHis'),
'status' => 'Running',
]);

$transaction->commit();
}
Expand Down Expand Up @@ -206,13 +206,7 @@ public static function runJobs($testParams = NULL, $mode = NULL) {
// Finish the job.

$transaction = new CRM_Core_Transaction();

$saveJob = new CRM_Mailing_DAO_MailingJob();
$saveJob->id = $job->id;
$saveJob->end_date = date('YmdHis');
$saveJob->status = 'Complete';
$saveJob->save();

self::create(['id' => $job->id, 'end_date' => date('YmdHis'), 'status' => 'Complete']);
$transaction->commit();

// don't mark the mailing as complete
Expand Down Expand Up @@ -379,13 +373,8 @@ public static function runJobs_pre($offset = 200, $mode = NULL) {

$job->split_job($offset);

// update the status of the parent job
$saveJob = new CRM_Mailing_DAO_MailingJob();
$saveJob->id = $job->id;
$saveJob->start_date = date('YmdHis');
$saveJob->status = 'Running';
$saveJob->save();

// Update the status of the parent job
self::create(['id' => $job->id, 'start_date' => date('YmdHis'), 'status' => 'Running']);
$transaction->commit();

// Release the job lock
Expand Down Expand Up @@ -811,11 +800,7 @@ public static function cancel($mailingId) {
in_array($job->status, array('Scheduled', 'Running', 'Paused'))
) {

$newJob = new CRM_Mailing_BAO_MailingJob();
$newJob->id = $job->id;
$newJob->end_date = date('YmdHis');
$newJob->status = 'Canceled';
$newJob->save();
self::create(['id' => $job->id, 'end_date' => date('YmdHis'), 'status' => 'Canceled']);

// also cancel all child jobs
$sql = "
Expand Down
2 changes: 1 addition & 1 deletion api/v3/Mailing.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ function civicrm_api3_mailing_delete($params) {
* @return array
*/
function civicrm_api3_mailing_get($params) {
$result = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
$result = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'Mailing');
return _civicrm_api3_mailing_get_formatResult($result);
}

Expand Down
4 changes: 2 additions & 2 deletions api/v3/MailingJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* @throws \API_Exception
*/
function civicrm_api3_mailing_job_create($params) {
return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params);
return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'MailingJob');
}

/**
Expand All @@ -67,7 +67,7 @@ function _civicrm_api3_mailing_job_create_spec(&$params) {
* API return Array of matching mailing jobs.
*/
function civicrm_api3_mailing_job_get($params) {
return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'MailingJob');
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/api/v3/MailingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,7 @@ public function testClone() {
// END SAMPLE DATA

$create = $this->callAPISuccess('Mailing', 'create', $params);
$created = $this->callAPISuccess('Mailing', 'get', []);
$createId = $create['id'];
$this->createLoggedInUser();
$clone = $this->callAPIAndDocument('Mailing', 'clone', array('id' => $create['id']), __FUNCTION__, __FILE__);
Expand Down
2 changes: 0 additions & 2 deletions tests/phpunit/api/v3/SyntaxConformanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,6 @@ public static function toBeSkipped_custom_data_creatable($sequential = FALSE) {
'MailingEventResubscribe',
'MailingEventSubscribe',
'MailingEventUnsubscribe',
'MailingJob',
'MembershipPayment',
'SavedSearch',
'UFJoin',
Expand Down Expand Up @@ -488,7 +487,6 @@ public static function toBeSkipped_updatesingle($sequential = FALSE) {
// pseudo-entity; testUpdateSingleValueAlter doesn't introspect properly on it. Multiple magic fields
'Mailing',
'MailingGroup',
'MailingJob',
'Address',
'MailingEventUnsubscribe',
'MailingEventSubscribe',
Expand Down

0 comments on commit 7f1a99f

Please sign in to comment.