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

Trivial cleanup in EmailProcessorTest #27307

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 40 additions & 34 deletions tests/phpunit/CRM/Utils/Mail/EmailProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class CRM_Utils_Mail_EmailProcessorTest extends CiviUnitTestCase {
*/
protected $contactID;

/**
* @throws \CRM_Core_Exception
*/
public function setUp(): void {
parent::setUp();
CRM_Utils_File::cleanDir(__DIR__ . '/data/mail');
Expand All @@ -39,6 +42,9 @@ public function setUp(): void {
]);
}

/**
* @throws \CRM_Core_Exception
*/
public function tearDown(): void {
CRM_Utils_File::cleanDir(__DIR__ . '/data/mail');
parent::tearDown();
Expand All @@ -65,9 +71,9 @@ public function testBounceProcessing(): void {
$this->setUpMailing();

copy(__DIR__ . '/data/bounces/bounce_no_verp.txt', __DIR__ . '/data/mail/bounce_no_verp.txt');
$this->assertTrue(file_exists(__DIR__ . '/data/mail/bounce_no_verp.txt'));
$this->assertFileExists(__DIR__ . '/data/mail/bounce_no_verp.txt');
$this->callAPISuccess('job', 'fetch_bounces', []);
$this->assertFalse(file_exists(__DIR__ . '/data/mail/bounce_no_verp.txt'));
$this->assertFileDoesNotExist(__DIR__ . '/data/mail/bounce_no_verp.txt');
$this->checkMailingBounces(1);
}

Expand All @@ -80,7 +86,7 @@ public function testBounceProcessingInvalidCharacter(): void {

copy(__DIR__ . '/data/bounces/' . $mail, __DIR__ . '/data/mail/' . $mail);
$this->callAPISuccess('job', 'fetch_bounces', []);
$this->assertFalse(file_exists(__DIR__ . '/data/mail/' . $mail));
$this->assertFileDoesNotExist(__DIR__ . '/data/mail/' . $mail);
$this->checkMailingBounces(1);
}

Expand All @@ -93,7 +99,7 @@ public function testBounceProcessingUTF8mb4(): void {

copy(__DIR__ . '/data/bounces/' . $mail, __DIR__ . '/data/mail/' . $mail);
$this->callAPISuccess('job', 'fetch_bounces', []);
$this->assertFalse(file_exists(__DIR__ . '/data/mail/' . $mail));
$this->assertFileDoesNotExist(__DIR__ . '/data/mail/' . $mail);
$this->checkMailingBounces(1);
}

Expand All @@ -108,22 +114,22 @@ public function testProcessingMultipartRelatedEmail(): void {

copy(__DIR__ . '/data/bounces/' . $mail, __DIR__ . '/data/mail/' . $mail);
$this->callAPISuccess('job', 'fetch_bounces', []);
$this->assertFalse(file_exists(__DIR__ . '/data/mail/' . $mail));
$this->assertFileDoesNotExist(__DIR__ . '/data/mail/' . $mail);
$this->checkMailingBounces(1);
}

/**
* Tests that a nested multipart email does not cause pain & misery & fatal errors.
*
* Sample anonymized from an email that broke bounce processing at Wikimedia
* Sample based on an email that broke bounce processing at Wikimedia
*/
public function testProcessingNestedMultipartEmail(): void {
$this->setUpMailing();
$mail = 'test_nested_message.eml';

copy(__DIR__ . '/data/bounces/' . $mail, __DIR__ . '/data/mail/' . $mail);
$this->callAPISuccess('job', 'fetch_bounces', []);
$this->assertFalse(file_exists(__DIR__ . '/data/mail/' . $mail));
$this->assertFileDoesNotExist(__DIR__ . '/data/mail/' . $mail);
$this->checkMailingBounces(1);
}

Expand All @@ -150,30 +156,34 @@ public function testBounceProcessingDeletedEmail(): void {
]);

copy(__DIR__ . '/data/bounces/bounce_no_verp.txt', __DIR__ . '/data/mail/bounce_no_verp.txt');
$this->assertTrue(file_exists(__DIR__ . '/data/mail/bounce_no_verp.txt'));
$this->assertFileExists(__DIR__ . '/data/mail/bounce_no_verp.txt');
$this->callAPISuccess('job', 'fetch_bounces', []);
$this->assertFalse(file_exists(__DIR__ . '/data/mail/bounce_no_verp.txt'));
$this->assertFileDoesNotExist(__DIR__ . '/data/mail/bounce_no_verp.txt');
$this->checkMailingBounces(1);
}

/**
* Wrapper to check for mailing bounces.
*
* Normally we would call $this->callAPISuccessGetCount but there is not one & there is resistance to
* adding apis for 'convenience' so just adding a hacky function to get past the impasse.
* Normally we would call $this->callAPISuccessGetCount but there is not one
* & there is resistance to adding apis for 'convenience' so just adding a
* hacky function to get past the impasse.
*
* @param int $expectedCount
*
* @noinspection PhpUnhandledExceptionInspection
* @noinspection PhpDocMissingThrowsInspection
*/
public function checkMailingBounces($expectedCount) {
public function checkMailingBounces(int $expectedCount): void {
$this->assertEquals($expectedCount, CRM_Core_DAO::singleValueQuery(
"SELECT count(*) FROM civicrm_mailing_event_bounce"
'SELECT count(*) FROM civicrm_mailing_event_bounce'
));
}

/**
* Set up a mailing.
*/
public function setUpMailing() {
public function setUpMailing(): void {
$this->contactID = $this->individualCreate(['email' => 'undeliverable@example.com']);
$groupID = $this->callAPISuccess('Group', 'create', [
'title' => 'Mailing group',
Expand All @@ -190,7 +200,7 @@ public function setUpMailing() {
* Set up mail account with 'Skip emails which do not have a Case ID or
* Case hash' option enabled.
*/
public function setUpSkipNonCasesEmail() {
public function setUpSkipNonCasesEmail(): void {
$this->callAPISuccess('MailSettings', 'get', [
'api.MailSettings.create' => [
'name' => 'mailbox',
Expand All @@ -212,9 +222,9 @@ public function testInboundProcessingCaseEmail(): void {

copy(__DIR__ . '/data/inbound/' . $mail, __DIR__ . '/data/mail/' . $mail);
$this->callAPISuccess('job', 'fetch_activities', []);
$result = civicrm_api3('Activity', 'get', [
$result = $this->callAPISuccess('Activity', 'get', [
'sequential' => 1,
'subject' => ['LIKE' => "%[case #214bf6d]%"],
'subject' => ['LIKE' => '%[case #214bf6d]%'],
]);
$this->assertNotEmpty($result['values'][0]['id']);
}
Expand All @@ -228,9 +238,9 @@ public function testInboundProcessingNonCaseEmail(): void {

copy(__DIR__ . '/data/inbound/' . $mail, __DIR__ . '/data/mail/' . $mail);
$this->callAPISuccess('job', 'fetch_activities', []);
$result = civicrm_api3('Activity', 'get', [
$result = $this->callAPISuccess('Activity', 'get', [
'sequential' => 1,
'subject' => ['LIKE' => "%Love letter%"],
'subject' => ['LIKE' => '%Love letter%'],
]);
$this->assertEmpty($result['values']);
}
Expand All @@ -239,7 +249,7 @@ public function testInboundProcessingNonCaseEmail(): void {
* Set up mail account with 'Do not create new contacts when filing emails'
* option enabled.
*/
public function setUpDoNotCreateContact() {
public function setUpDoNotCreateContact(): void {
$this->callAPISuccess('MailSettings', 'get', [
'api.MailSettings.create' => [
'name' => 'mailbox',
Expand All @@ -262,9 +272,9 @@ public function testInboundProcessingDoNotCreateContact(): void {

copy(__DIR__ . '/data/inbound/' . $mail, __DIR__ . '/data/mail/' . $mail);
$this->callAPISuccess('job', 'fetch_activities', []);
$result = civicrm_api3('Contact', 'get', [
$result = $this->callAPISuccess('Contact', 'get', [
'sequential' => 1,
'email' => "from@test.test",
'email' => 'from@test.test',
]);
$this->assertEmpty($result['values']);
}
Expand All @@ -273,7 +283,7 @@ public function testInboundProcessingDoNotCreateContact(): void {
* Set up mail account with non-default activity options.
* return $params array
*/
public function setUpNonDefaultActivityOptions() {
public function setUpNonDefaultActivityOptions(): array {
$this->enableCiviCampaign();
$campaign = $this->civicrm_api('Campaign', 'create', [
'version' => $this->_apiversion,
Expand Down Expand Up @@ -310,13 +320,11 @@ public function testInboundProcessingNonDefaultActivityOptions(): void {

copy(__DIR__ . '/data/inbound/' . $mail, __DIR__ . '/data/mail/' . $mail);
$this->callAPISuccess('job', 'fetch_activities', []);
$result = civicrm_api3('Activity', 'get', [
'sequential' => 1,
'subject' => ['LIKE' => "%An email with two recipients%"],
'return' => ["assignee_contact_id", "target_contact_id", "activity_type_id", "status_id", "source_contact_name", "campaign_id"],
$activity = $this->callAPISuccessGetSingle('Activity', [
'subject' => ['LIKE' => '%An email with two recipients%'],
'return' => ['assignee_contact_id', 'target_contact_id', 'activity_type_id', 'status_id', 'source_contact_name', 'campaign_id'],
]);

$activity = $result['values'][0];
$this->assertEquals(3, $activity['activity_type_id']);
$this->assertEquals(CRM_Core_PseudoConstant::getKey('CRM_Activity_DAO_Activity', 'activity_status_id', 'Scheduled'), $activity['status_id']);
$this->assertEquals('to@test.test', $activity['source_contact_name']);
Expand All @@ -331,16 +339,14 @@ public function testInboundProcessingNonDefaultActivityOptions(): void {
* Test not creating a contact for an email field that is not used.
*/
public function testInboundProcessingNoUnusedContacts(): void {
$params = $this->setUpNonDefaultActivityOptions();
$this->setUpNonDefaultActivityOptions();
$mail = 'test_non_default_email.eml';

copy(__DIR__ . '/data/inbound/' . $mail, __DIR__ . '/data/mail/' . $mail);
$this->callAPISuccess('job', 'fetch_activities', []);
$result = civicrm_api3('Contact', 'get', [
'sequential' => 1,
'email' => "bcc@test.test",
]);
$this->assertEmpty($result['values']);
$this->callAPISuccessGetCount('Contact', [
'email' => 'bcc@test.test',
], 0);
}

}