From 00cb6250d344a4de5ecd69c2253785a0767c4b38 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 10 Feb 2015 04:47:22 -0800 Subject: [PATCH] CRM-15856 - Mailing BAO - Only validate tokens when calling through API The test suite relies on CRM_Mailing_BAO_Spool to log test messages -- which (surprisingly, bizarrely) puts them in civicrm_mailing; thus, enforcing required tokens broke lots of test cases that have nothing to do with CiviMail. --- CRM/Mailing/BAO/Mailing.php | 22 +++++++++++++++++++--- api/v3/Mailing.php | 1 + 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/CRM/Mailing/BAO/Mailing.php b/CRM/Mailing/BAO/Mailing.php index ee427d4c97..8fb0bb55b0 100644 --- a/CRM/Mailing/BAO/Mailing.php +++ b/CRM/Mailing/BAO/Mailing.php @@ -1583,6 +1583,20 @@ ORDER BY civicrm_email.is_bulkmail DESC * Construct a new mailing object, along with job and mailing_group * objects, from the form values of the create mailing wizard. * + * This function is a bit evil. It not only merges $params and saves + * the mailing -- it also schedules the mailing and chooses the recipients. + * Since it merges $params, it's also the only place to correctly trigger + * multi-field validation. It should be broken up. + * + * In the mean time, use-cases which break under the weight of this + * evil may find reprieve in these extra evil params: + * + * - _skip_evil_bao_auto_recipients_: bool + * - _skip_evil_bao_auto_schedule_: bool + * - _evil_bao_validator_: string|callable + * + * + * * @params array $params * Form values. * @@ -1591,6 +1605,7 @@ ORDER BY civicrm_email.is_bulkmail DESC * * @return object * $mailing The new mailing object + * @throws \Exception */ public static function create(&$params, $ids = array()) { @@ -1704,8 +1719,9 @@ ORDER BY civicrm_email.is_bulkmail DESC CRM_Core_BAO_File::processAttachment($params, 'civicrm_mailing', $mailing->id); // If we're going to autosend, then check validity before saving. - if (!empty($params['scheduled_date']) && $params['scheduled_date'] != 'null') { - $errors = static::checkSendable($mailing); + if (!empty($params['scheduled_date']) && $params['scheduled_date'] != 'null' && !empty($params['_evil_bao_validator_'])) { + $cb = Civi\Core\Resolver::singleton()->get($params['_evil_bao_validator_']); + $errors = call_user_func($cb, $mailing); if (!empty($errors)) { $fields = implode(',', array_keys($errors)); throw new CRM_Core_Exception("Mailing cannot be sent. There are missing or invalid fields ($fields).", 'cannot-send', $errors); @@ -1716,7 +1732,7 @@ ORDER BY civicrm_email.is_bulkmail DESC // Create parent job if not yet created. // Condition on the existence of a scheduled date. - if (!empty($params['scheduled_date']) && $params['scheduled_date'] != 'null') { + if (!empty($params['scheduled_date']) && $params['scheduled_date'] != 'null' && empty($params['_skip_evil_bao_auto_schedule_'])) { $job = new CRM_Mailing_BAO_MailingJob(); $job->mailing_id = $mailing->id; $job->status = 'Scheduled'; diff --git a/api/v3/Mailing.php b/api/v3/Mailing.php index 58d6213996..74cf9e209d 100755 --- a/api/v3/Mailing.php +++ b/api/v3/Mailing.php @@ -58,6 +58,7 @@ function civicrm_api3_mailing_create($params) { unset($params['approval_note']); } } + $params['_evil_bao_validator_'] = 'CRM_Mailing_BAO_Mailing::checkSendable'; return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params); } -- 2.25.1