X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fapi%2Fv3%2FMailingTest.php;h=e72fc5097e39991fbb02f745b776bd875cf75ee3;hb=f38692208a64d52597c7f1377341c1ac6b080bd9;hp=5c7a16f9410cdc272e0488647cc8e616a3209bea;hpb=dc617c20a5a57ddc6f77b6c5404f5af65dfe58dd;p=civicrm-core.git diff --git a/tests/phpunit/api/v3/MailingTest.php b/tests/phpunit/api/v3/MailingTest.php index 5c7a16f941..e72fc5097e 100644 --- a/tests/phpunit/api/v3/MailingTest.php +++ b/tests/phpunit/api/v3/MailingTest.php @@ -47,14 +47,15 @@ class api_v3_MailingTest extends CiviUnitTestCase { public function setUp() { parent::setUp(); $this->useTransaction(); - CRM_Mailing_BAO_MailingJob::$mailsProcessed = 0; // DGW + // DGW + CRM_Mailing_BAO_MailingJob::$mailsProcessed = 0; $this->_contactID = $this->individualCreate(); $this->_groupID = $this->groupCreate(); $this->_email = 'test@test.test'; $this->_params = array( 'subject' => 'Hello {contact.display_name}', 'body_text' => "This is {contact.display_name}.\nhttps://civicrm.org\n{domain.address}{action.optOutUrl}", - 'body_html' => "

This is {contact.display_name}.

CiviCRM.org

{domain.address}{action.optOutUrl}

", + 'body_html' => "

Forward this emailForward this email with no protocolThis is {contact.display_name}.

CiviCRM.org

{domain.address}{action.optOutUrl}

", 'name' => 'mailing name', 'created_id' => $this->_contactID, 'header_id' => '', @@ -70,7 +71,8 @@ class api_v3_MailingTest extends CiviUnitTestCase { } public function tearDown() { - CRM_Mailing_BAO_MailingJob::$mailsProcessed = 0; // DGW + // DGW + CRM_Mailing_BAO_MailingJob::$mailsProcessed = 0; parent::tearDown(); } @@ -81,7 +83,8 @@ class api_v3_MailingTest extends CiviUnitTestCase { $result = $this->callAPIAndDocument('mailing', 'create', $this->_params + array('scheduled_date' => 'now'), __FUNCTION__, __FILE__); $jobs = $this->callAPISuccess('mailing_job', 'get', array('mailing_id' => $result['id'])); $this->assertEquals(1, $jobs['count']); - unset($this->_params['created_id']); // return isn't working on this in getAndCheck so lets not check it for now + // return isn't working on this in getAndCheck so lets not check it for now + unset($this->_params['created_id']); $this->getAndCheck($this->_params, $result['id'], 'mailing'); } @@ -91,7 +94,7 @@ class api_v3_MailingTest extends CiviUnitTestCase { public function testSkipAutoSchedule() { $this->callAPISuccess('Mailing', 'create', array_merge($this->_params, [ '_skip_evil_bao_auto_schedule_' => TRUE, - 'scheduled_date' => 'now' + 'scheduled_date' => 'now', ])); $this->callAPISuccessGetCount('Mailing', [], 1); $this->callAPISuccessGetCount('MailingJob', [], 0); @@ -107,7 +110,8 @@ class api_v3_MailingTest extends CiviUnitTestCase { $jobs = $this->callAPISuccess('mailing_job', 'get', array('mailing_id' => $result['id'])); $this->assertEquals(1, $jobs['count']); $this->assertEquals('Complete', $jobs['values'][$jobs['id']]['status']); - unset($this->_params['created_id']); // return isn't working on this in getAndCheck so lets not check it for now + // return isn't working on this in getAndCheck so lets not check it for now + unset($this->_params['created_id']); $this->getAndCheck($this->_params, $result['id'], 'mailing'); } @@ -175,22 +179,22 @@ class api_v3_MailingTest extends CiviUnitTestCase { $groupIDs['a'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group')); $groupIDs['b'] = $this->groupCreate(array('name' => 'Example exclude group', 'title' => 'Example exclude group')); $contactIDs['a'] = $this->individualCreate(array( - 'email' => 'include.me@example.org', - 'first_name' => 'Includer', - 'last_name' => 'Person', - )); + 'email' => 'include.me@example.org', + 'first_name' => 'Includer', + 'last_name' => 'Person', + )); $contactIDs['b'] = $this->individualCreate(array( - 'email' => 'exclude.me@example.org', - 'last_name' => 'Excluder', - )); + 'email' => 'exclude.me@example.org', + 'last_name' => 'Excluder', + )); $this->callAPISuccess('GroupContact', 'create', array( - 'group_id' => $groupIDs['a'], - 'contact_id' => $contactIDs['a'], - )); + 'group_id' => $groupIDs['a'], + 'contact_id' => $contactIDs['a'], + )); $this->callAPISuccess('GroupContact', 'create', array( - 'group_id' => $groupIDs['b'], - 'contact_id' => $contactIDs['b'], - )); + 'group_id' => $groupIDs['b'], + 'contact_id' => $contactIDs['b'], + )); // END SAMPLE DATA // ** Pass 1: Create @@ -257,15 +261,38 @@ class api_v3_MailingTest extends CiviUnitTestCase { 'recipient' => CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_recipients'), ); $result = $this->callAPISuccess('mailing', 'create', $params); - $this->assertDBQuery($maxIDs['mailing'], 'SELECT MAX(id) FROM civicrm_mailing'); // 'Preview should not create any mailing records' - $this->assertDBQuery($maxIDs['job'], 'SELECT MAX(id) FROM civicrm_mailing_job'); // 'Preview should not create any mailing_job record' - $this->assertDBQuery($maxIDs['group'], 'SELECT MAX(id) FROM civicrm_mailing_group'); // 'Preview should not create any mailing_group records' - $this->assertDBQuery($maxIDs['recipient'], 'SELECT MAX(id) FROM civicrm_mailing_recipients'); // 'Preview should not create any mailing_recipient records' - + // 'Preview should not create any mailing records' + $this->assertDBQuery($maxIDs['mailing'], 'SELECT MAX(id) FROM civicrm_mailing'); + // 'Preview should not create any mailing_job record' + $this->assertDBQuery($maxIDs['job'], 'SELECT MAX(id) FROM civicrm_mailing_job'); + // 'Preview should not create any mailing_group records' + $this->assertDBQuery($maxIDs['group'], 'SELECT MAX(id) FROM civicrm_mailing_group'); + // 'Preview should not create any mailing_recipient records' + $this->assertDBQuery($maxIDs['recipient'], 'SELECT MAX(id) FROM civicrm_mailing_recipients'); + $baseurl = CRM_Utils_System::baseCMSURL(); $previewResult = $result['values'][$result['id']]['api.Mailing.preview']; $this->assertEquals("Hello $displayName", $previewResult['values']['subject']); $this->assertContains("This is $displayName", $previewResult['values']['body_text']); $this->assertContains("

This is $displayName.

", $previewResult['values']['body_html']); + $this->assertContains('Forward this email with no protocol', $previewResult['values']['body_html']); + $this->assertNotContains("http://http://", $previewResult['values']['body_html']); + } + + public function testMailerPreviewUnknownContact() { + $params = $this->_params; + $params['api.Mailing.preview'] = array( + 'id' => '$value.id', + ); + + $result = $this->callAPISuccess('mailing', 'create', $params); + + // NOTE: It's highly debatable what's best to do with contact-tokens for an + // unknown-contact. However, changes should be purposeful, so we'll test + // for the current behavior (i.e. returning blanks). + $previewResult = $result['values'][$result['id']]['api.Mailing.preview']; + $this->assertEquals("Hello ", $previewResult['values']['subject']); + $this->assertContains("This is .", $previewResult['values']['body_text']); + $this->assertContains("

This is .

", $previewResult['values']['body_html']); } public function testMailerPreviewRecipients() { @@ -273,26 +300,26 @@ class api_v3_MailingTest extends CiviUnitTestCase { $groupIDs['inc'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group')); $groupIDs['exc'] = $this->groupCreate(array('name' => 'Example exclude group', 'title' => 'Example exclude group')); $contactIDs['include_me'] = $this->individualCreate(array( - 'email' => 'include.me@example.org', - 'first_name' => 'Includer', - 'last_name' => 'Person', - )); + 'email' => 'include.me@example.org', + 'first_name' => 'Includer', + 'last_name' => 'Person', + )); $contactIDs['exclude_me'] = $this->individualCreate(array( - 'email' => 'exclude.me@example.org', - 'last_name' => 'Excluder', - )); + 'email' => 'exclude.me@example.org', + 'last_name' => 'Excluder', + )); $this->callAPISuccess('GroupContact', 'create', array( - 'group_id' => $groupIDs['inc'], - 'contact_id' => $contactIDs['include_me'], - )); + 'group_id' => $groupIDs['inc'], + 'contact_id' => $contactIDs['include_me'], + )); $this->callAPISuccess('GroupContact', 'create', array( - 'group_id' => $groupIDs['inc'], - 'contact_id' => $contactIDs['exclude_me'], - )); + 'group_id' => $groupIDs['inc'], + 'contact_id' => $contactIDs['exclude_me'], + )); $this->callAPISuccess('GroupContact', 'create', array( - 'group_id' => $groupIDs['exc'], - 'contact_id' => $contactIDs['exclude_me'], - )); + 'group_id' => $groupIDs['exc'], + 'contact_id' => $contactIDs['exclude_me'], + )); $params = $this->_params; $params['groups']['include'] = array($groupIDs['inc']); @@ -316,8 +343,10 @@ class api_v3_MailingTest extends CiviUnitTestCase { 'group' => CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_group'), ); $create = $this->callAPIAndDocument('Mailing', 'create', $params, __FUNCTION__, __FILE__); - $this->assertDBQuery($maxIDs['mailing'], 'SELECT MAX(id) FROM civicrm_mailing'); // 'Preview should not create any mailing records' - $this->assertDBQuery($maxIDs['group'], 'SELECT MAX(id) FROM civicrm_mailing_group'); // 'Preview should not create any mailing_group records' + // 'Preview should not create any mailing records' + $this->assertDBQuery($maxIDs['mailing'], 'SELECT MAX(id) FROM civicrm_mailing'); + // 'Preview should not create any mailing_group records' + $this->assertDBQuery($maxIDs['group'], 'SELECT MAX(id) FROM civicrm_mailing_group'); $preview = $create['values'][$create['id']]['api.MailingRecipients.get']; $previewIds = array_values(CRM_Utils_Array::collect('contact_id', $preview['values'])); @@ -335,21 +364,21 @@ class api_v3_MailingTest extends CiviUnitTestCase { // BEGIN SAMPLE DATA $groupIDs['grp'] = $this->groupCreate(array('name' => 'Example group', 'title' => 'Example group')); $contactIDs['include_me'] = $this->individualCreate(array( - 'email' => 'include.me@example.org', - 'first_name' => 'Includer', - 'last_name' => 'Person', - )); + 'email' => 'include.me@example.org', + 'first_name' => 'Includer', + 'last_name' => 'Person', + )); $contactIDs['include_me_duplicate'] = $this->individualCreate(array( - 'email' => 'include.me@example.org', - 'first_name' => 'IncluderDuplicate', - 'last_name' => 'Person', - )); + 'email' => 'include.me@example.org', + 'first_name' => 'IncluderDuplicate', + 'last_name' => 'Person', + )); $contactIDs['include_me_onhold'] = $this->individualCreate(array( - 'email' => 'onholdinclude.me@example.org', - 'first_name' => 'Onhold', - 'last_name' => 'Person', - )); + 'email' => 'onholdinclude.me@example.org', + 'first_name' => 'Onhold', + 'last_name' => 'Person', + )); $emailId = $this->callAPISuccessGetValue('Email', array( 'return' => 'id', 'contact_id' => $contactIDs['include_me_onhold'], @@ -360,17 +389,17 @@ class api_v3_MailingTest extends CiviUnitTestCase { )); $this->callAPISuccess('GroupContact', 'create', array( - 'group_id' => $groupIDs['grp'], - 'contact_id' => $contactIDs['include_me'], - )); + 'group_id' => $groupIDs['grp'], + 'contact_id' => $contactIDs['include_me'], + )); $this->callAPISuccess('GroupContact', 'create', array( - 'group_id' => $groupIDs['grp'], - 'contact_id' => $contactIDs['include_me_duplicate'], - )); + 'group_id' => $groupIDs['grp'], + 'contact_id' => $contactIDs['include_me_duplicate'], + )); $this->callAPISuccess('GroupContact', 'create', array( - 'group_id' => $groupIDs['grp'], - 'contact_id' => $contactIDs['include_me_onhold'], - )); + 'group_id' => $groupIDs['grp'], + 'contact_id' => $contactIDs['include_me_onhold'], + )); $params = $this->_params; $params['groups']['include'] = array($groupIDs['grp']); @@ -402,10 +431,10 @@ class api_v3_MailingTest extends CiviUnitTestCase { */ public function testMailerSendTest_email() { $contactIDs['alice'] = $this->individualCreate(array( - 'email' => 'alice@example.org', - 'first_name' => 'Alice', - 'last_name' => 'Person', - )); + 'email' => 'alice@example.org', + 'first_name' => 'Alice', + 'last_name' => 'Person', + )); $mail = $this->callAPISuccess('mailing', 'create', $this->_params); @@ -414,7 +443,8 @@ class api_v3_MailingTest extends CiviUnitTestCase { // Per https://lab.civicrm.org/dev/mail/issues/32 test non-lowercase email $params['id'] = $mail['id']; $deliveredInfo = $this->callAPISuccess($this->_entity, 'send_test', $params); - $this->assertEquals(1, $deliveredInfo['count']); // verify mail has been sent to user by count + // verify mail has been sent to user by count + $this->assertEquals(1, $deliveredInfo['count']); $deliveredContacts = array_values(CRM_Utils_Array::collect('contact_id', $deliveredInfo['values'])); $this->assertEquals(array($contactIDs['alice']), $deliveredContacts); @@ -430,32 +460,32 @@ class api_v3_MailingTest extends CiviUnitTestCase { // BEGIN SAMPLE DATA $groupIDs['inc'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group')); $contactIDs['alice'] = $this->individualCreate(array( - 'email' => 'alice@example.org', - 'first_name' => 'Alice', - 'last_name' => 'Person', - )); + 'email' => 'alice@example.org', + 'first_name' => 'Alice', + 'last_name' => 'Person', + )); $contactIDs['bob'] = $this->individualCreate(array( - 'email' => 'bob@example.org', - 'first_name' => 'Bob', - 'last_name' => 'Person', - )); + 'email' => 'bob@example.org', + 'first_name' => 'Bob', + 'last_name' => 'Person', + )); $contactIDs['carol'] = $this->individualCreate(array( - 'email' => 'carol@example.org', - 'first_name' => 'Carol', - 'last_name' => 'Person', - )); + 'email' => 'carol@example.org', + 'first_name' => 'Carol', + 'last_name' => 'Person', + )); $this->callAPISuccess('GroupContact', 'create', array( - 'group_id' => $groupIDs['inc'], - 'contact_id' => $contactIDs['alice'], - )); + 'group_id' => $groupIDs['inc'], + 'contact_id' => $contactIDs['alice'], + )); $this->callAPISuccess('GroupContact', 'create', array( - 'group_id' => $groupIDs['inc'], - 'contact_id' => $contactIDs['bob'], - )); + 'group_id' => $groupIDs['inc'], + 'contact_id' => $contactIDs['bob'], + )); $this->callAPISuccess('GroupContact', 'create', array( - 'group_id' => $groupIDs['inc'], - 'contact_id' => $contactIDs['carol'], - )); + 'group_id' => $groupIDs['inc'], + 'contact_id' => $contactIDs['carol'], + )); // END SAMPLE DATA $mail = $this->callAPISuccess('mailing', 'create', $this->_params); @@ -464,7 +494,8 @@ class api_v3_MailingTest extends CiviUnitTestCase { 'test_email' => NULL, 'test_group' => $groupIDs['inc'], )); - $this->assertEquals(3, $deliveredInfo['count'], "in line " . __LINE__); // verify mail has been sent to user by count + // verify mail has been sent to user by count + $this->assertEquals(3, $deliveredInfo['count'], "in line " . __LINE__); $deliveredContacts = array_values(CRM_Utils_Array::collect('contact_id', $deliveredInfo['values'])); $this->assertEquals(array($contactIDs['alice'], $contactIDs['bob'], $contactIDs['carol']), $deliveredContacts); @@ -477,76 +508,117 @@ class api_v3_MailingTest extends CiviUnitTestCase { * @return array */ public function submitProvider() { - $cases = array(); // $useLogin, $params, $expectedFailure, $expectedJobCount + // $useLogin, $params, $expectedFailure, $expectedJobCount + $cases = array(); $cases[] = array( - TRUE, //useLogin - array(), // createParams + //useLogin + TRUE, + // createParams + array(), array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'), - FALSE, // expectedFailure - 1, // expectedJobCount + // expectedFailure + FALSE, + // expectedJobCount + 1, ); $cases[] = array( - FALSE, //useLogin - array(), // createParams + //useLogin + FALSE, + // createParams + array(), array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'), - "/Failed to determine current user/", // expectedFailure - 0, // expectedJobCount + // expectedFailure + "/Failed to determine current user/", + // expectedJobCount + 0, ); $cases[] = array( - TRUE, //useLogin - array(), // createParams + //useLogin + TRUE, + // createParams + array(), array('scheduled_date' => '2014-12-13 10:00:00'), - FALSE, // expectedFailure - 1, // expectedJobCount + // expectedFailure + FALSE, + // expectedJobCount + 1, ); $cases[] = array( - TRUE, //useLogin - array(), // createParams + //useLogin + TRUE, + // createParams + array(), array(), - "/Missing parameter scheduled_date and.or approval_date/", // expectedFailure - 0, // expectedJobCount + // expectedFailure + "/Missing parameter scheduled_date and.or approval_date/", + // expectedJobCount + 0, ); $cases[] = array( - TRUE, //useLogin - array('name' => ''), // createParams + //useLogin + TRUE, + // createParams + array('name' => ''), array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'), - "/Mailing cannot be sent. There are missing or invalid fields \\(name\\)./", // expectedFailure - 0, // expectedJobCount + // expectedFailure + "/Mailing cannot be sent. There are missing or invalid fields \\(name\\)./", + // expectedJobCount + 0, ); $cases[] = array( - TRUE, //useLogin - array('body_html' => '', 'body_text' => ''), // createParams + //useLogin + TRUE, + // createParams + array('body_html' => '', 'body_text' => ''), array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'), - "/Mailing cannot be sent. There are missing or invalid fields \\(body\\)./", // expectedFailure - 0, // expectedJobCount + // expectedFailure + "/Mailing cannot be sent. There are missing or invalid fields \\(body\\)./", + // expectedJobCount + 0, ); $cases[] = array( - TRUE, //useLogin - array('body_html' => 'Oops, did I leave my tokens at home?'), // createParams + //useLogin + TRUE, + // createParams + array('body_html' => 'Oops, did I leave my tokens at home?'), array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'), - "/Mailing cannot be sent. There are missing or invalid fields \\(.*body_html.*optOut.*\\)./", // expectedFailure - 0, // expectedJobCount + // expectedFailure + "/Mailing cannot be sent. There are missing or invalid fields \\(.*body_html.*optOut.*\\)./", + // expectedJobCount + 0, ); $cases[] = array( - TRUE, //useLogin - array('body_text' => 'Oops, did I leave my tokens at home?'), // createParams + //useLogin + TRUE, + // createParams + array('body_text' => 'Oops, did I leave my tokens at home?'), array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'), - "/Mailing cannot be sent. There are missing or invalid fields \\(.*body_text.*optOut.*\\)./", // expectedFailure - 0, // expectedJobCount + // expectedFailure + "/Mailing cannot be sent. There are missing or invalid fields \\(.*body_text.*optOut.*\\)./", + // expectedJobCount + 0, ); $cases[] = array( - TRUE, //useLogin - array('body_text' => 'Look ma, magic tokens in the text!', 'footer_id' => '%FOOTER%'), // createParams + //useLogin + TRUE, + // createParams + array('body_text' => 'Look ma, magic tokens in the text!', 'footer_id' => '%FOOTER%'), array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'), - FALSE, // expectedFailure - 1, // expectedJobCount + // expectedFailure + FALSE, + // expectedJobCount + 1, ); $cases[] = array( - TRUE, //useLogin - array('body_html' => '

Look ma, magic tokens in the markup!

', 'footer_id' => '%FOOTER%'), // createParams + //useLogin + TRUE, + // createParams + array('body_html' => '

Look ma, magic tokens in the markup!

', 'footer_id' => '%FOOTER%'), array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'), - FALSE, // expectedFailure - 1, // expectedJobCount + // expectedFailure + FALSE, + // expectedJobCount + 1, ); return $cases; } @@ -645,7 +717,8 @@ class api_v3_MailingTest extends CiviUnitTestCase { */ public function testMailerStats() { $result = $this->groupContactCreate($this->_groupID, 100); - $this->assertEquals(100, $result['added']); //verify if 100 contacts are added for group + //verify if 100 contacts are added for group + $this->assertEquals(100, $result['added']); //Create and send test mail first and change the mail job to live, //because stats api only works on live mail @@ -683,7 +756,8 @@ SELECT event_queue_id, time_stamp FROM mail_{$type}_temp"; $result = $this->callAPISuccess('mailing', 'stats', array('mailing_id' => $mail['id'])); $expectedResult = array( - 'Delivered' => 80, //since among 100 mails 20 has been bounced + //since among 100 mails 20 has been bounced + 'Delivered' => 80, 'Bounces' => 20, 'Opened' => 20, 'Unique Clicks' => 0, @@ -831,7 +905,6 @@ SELECT event_queue_id, time_stamp FROM mail_{$type}_temp"; ); } - //----------- civicrm_mailing_event_forward methods ---------- /** @@ -897,6 +970,10 @@ SELECT event_queue_id, time_stamp FROM mail_{$type}_temp"; $url = CRM_Mailing_Event_BAO_TrackableURLOpen::track($dao->queue_id, $dao->url_id); $this->assertContains('https://civicrm.org', $url); + + // Ensure that Google CSS link is not tracked. + $sql = "SELECT id FROM civicrm_mailing_trackable_url where url = 'https://fonts.googleapis.com/css?family=Roboto+Condensed:400,700|Zilla+Slab:500,700'"; + $this->assertEquals([], CRM_Core_DAO::executeQuery($sql)->fetchAll()); } /**