From 0bbeda3dd1aa8a527afeaae455f8c27be53d69dd Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 5 Sep 2023 17:36:07 +1200 Subject: [PATCH] Trivial cleanup in EmailProcessorTest --- .../CRM/Utils/Mail/EmailProcessorTest.php | 74 ++++++++++--------- 1 file changed, 40 insertions(+), 34 deletions(-) diff --git a/tests/phpunit/CRM/Utils/Mail/EmailProcessorTest.php b/tests/phpunit/CRM/Utils/Mail/EmailProcessorTest.php index f4b7272425..de3a686dcd 100644 --- a/tests/phpunit/CRM/Utils/Mail/EmailProcessorTest.php +++ b/tests/phpunit/CRM/Utils/Mail/EmailProcessorTest.php @@ -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'); @@ -39,6 +42,9 @@ class CRM_Utils_Mail_EmailProcessorTest extends CiviUnitTestCase { ]); } + /** + * @throws \CRM_Core_Exception + */ public function tearDown(): void { CRM_Utils_File::cleanDir(__DIR__ . '/data/mail'); parent::tearDown(); @@ -65,9 +71,9 @@ class CRM_Utils_Mail_EmailProcessorTest extends CiviUnitTestCase { $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); } @@ -80,7 +86,7 @@ class CRM_Utils_Mail_EmailProcessorTest extends CiviUnitTestCase { 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); } @@ -93,7 +99,7 @@ class CRM_Utils_Mail_EmailProcessorTest extends CiviUnitTestCase { 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); } @@ -108,14 +114,14 @@ class CRM_Utils_Mail_EmailProcessorTest extends CiviUnitTestCase { 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(); @@ -123,7 +129,7 @@ class CRM_Utils_Mail_EmailProcessorTest extends CiviUnitTestCase { 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); } @@ -150,30 +156,34 @@ class CRM_Utils_Mail_EmailProcessorTest extends CiviUnitTestCase { ]); 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', @@ -190,7 +200,7 @@ class CRM_Utils_Mail_EmailProcessorTest extends CiviUnitTestCase { * 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', @@ -212,9 +222,9 @@ class CRM_Utils_Mail_EmailProcessorTest extends CiviUnitTestCase { 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']); } @@ -228,9 +238,9 @@ class CRM_Utils_Mail_EmailProcessorTest extends CiviUnitTestCase { 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']); } @@ -239,7 +249,7 @@ class CRM_Utils_Mail_EmailProcessorTest extends CiviUnitTestCase { * 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', @@ -262,9 +272,9 @@ class CRM_Utils_Mail_EmailProcessorTest extends CiviUnitTestCase { 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']); } @@ -273,7 +283,7 @@ class CRM_Utils_Mail_EmailProcessorTest extends CiviUnitTestCase { * 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, @@ -310,13 +320,11 @@ class CRM_Utils_Mail_EmailProcessorTest extends CiviUnitTestCase { 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']); @@ -331,16 +339,14 @@ class CRM_Utils_Mail_EmailProcessorTest extends CiviUnitTestCase { * 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); } } -- 2.25.1