From 84557ad3471462963106d6edabceebc5dcb5c9fb Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 20 May 2019 17:25:58 +1200 Subject: [PATCH] [TEST] cleanup MailingContactTest to remove dbunit dependency --- tests/phpunit/api/v3/MailingContactTest.php | 84 ++++++++++++--------- 1 file changed, 47 insertions(+), 37 deletions(-) diff --git a/tests/phpunit/api/v3/MailingContactTest.php b/tests/phpunit/api/v3/MailingContactTest.php index 58613a063f..7ec969cac6 100644 --- a/tests/phpunit/api/v3/MailingContactTest.php +++ b/tests/phpunit/api/v3/MailingContactTest.php @@ -40,21 +40,20 @@ * @group headless */ class api_v3_MailingContactTest extends CiviUnitTestCase { - protected $_apiversion = 3; protected $_entity = 'mailing'; public function setUp() { parent::setUp(); - $params = array( + $params = [ 'first_name' => 'abc1', 'contact_type' => 'Individual', 'last_name' => 'xyz1', - ); + ]; $this->_contact = $this->callAPISuccess("contact", "create", $params); } public function tearDown() { - $this->callAPISuccess("contact", "delete", array('id' => $this->_contact['id'])); + $this->callAPISuccess("contact", "delete", ['id' => $this->_contact['id']]); parent::tearDown(); } @@ -88,10 +87,7 @@ class api_v3_MailingContactTest extends CiviUnitTestCase { * belongs in the SyntaxConformance class */ public function testMailingNoContactID() { - $params = array( - 'something' => 'This is not a real field', - ); - $this->callAPIFailure('MailingContact', 'get', $params); + $this->callAPIFailure('MailingContact', 'get', ['something' => 'This is not a real field']); } /** @@ -103,8 +99,7 @@ class api_v3_MailingContactTest extends CiviUnitTestCase { * belongs in the SyntaxConformance class */ public function testMailingContactInvalidContactID() { - $params = array('contact_id' => 'This is not a number'); - $this->callAPIFailure('MailingContact', 'get', $params); + $this->callAPIFailure('MailingContact', 'get', ['contact_id' => 'This is not a number']); } /** @@ -134,7 +129,8 @@ class api_v3_MailingContactTest extends CiviUnitTestCase { * Test that the API returns a mailing properly when there is only one. */ public function testMailingContactDelivered() { - $contactID = $this->individualCreate(['first_name' => 'Test']); + list($contactID, $mailingID, $eventQueueID) = $this->setupEventQueue(); + CRM_Core_DAO::executeQuery("INSERT INTO civicrm_mailing_event_delivered (event_queue_id) VALUES(%1)", [1 => [$eventQueueID, 'Integer']]); $params = [ 'contact_id' => $contactID, @@ -148,40 +144,54 @@ class api_v3_MailingContactTest extends CiviUnitTestCase { $this->assertFalse(empty($result['values'])); $this->assertEquals($result['values'][1]['mailing_id'], 1); $this->assertEquals($result['values'][1]['subject'], "Some Subject"); - $this->assertEquals($result['values'][1]['creator_id'], 3); - $this->assertEquals($result['values'][1]['creator_name'], "xyz1, abc1"); + $this->assertEquals(CRM_Core_Session::getLoggedInContactID(), $result['values'][1]['creator_id']); } /** - * Test that the API returns only the "Bounced" mailings when instructed to do so. + * Test that the API returns only the "Bounced" mailings when instructed to + * do so. + * + * @throws \Exception */ public function testMailingContactBounced() { - $op = new PHPUnit_Extensions_Database_Operation_Insert(); - // Create the User. - $op->execute($this->_dbconn, - $this->createXMLDataSet( - dirname(__FILE__) . '/dataset/mailing_contact.xml' - ) - ); - // Create the Mailing and connections to the user. - $op->execute($this->_dbconn, - $this->createXMLDataSet( - dirname(__FILE__) . '/dataset/mailing_bounced.xml' - ) - ); + list($contactID, $mailingID, $eventQueueID) = $this->setupEventQueue(); + CRM_Core_DAO::executeQuery("INSERT INTO civicrm_mailing_event_bounce (event_queue_id, bounce_type_id) VALUES(%1, 6)", [1 => [$eventQueueID, 'Integer']]); - $params = array( - 'contact_id' => 23, + $params = [ + 'contact_id' => $contactID, 'type' => 'Bounced', - ); + ]; - $result = $this->callAPISuccess('MailingContact', 'get', $params); - $this->assertEquals($result['count'], 1); - $this->assertFalse(empty($result['values'])); - $this->assertEquals($result['values'][2]['mailing_id'], 2); - $this->assertEquals($result['values'][2]['subject'], "Some Subject"); - $this->assertEquals($result['values'][2]['creator_id'], 3); - $this->assertEquals($result['values'][2]['creator_name'], "xyz1, abc1"); + $result = $this->callAPISuccess('MailingContact', 'get', $params)['values']; + $this->assertEquals(1, count($result)); + $this->assertEquals($mailingID, $result[$mailingID]['mailing_id']); + $this->assertEquals('Some Subject', $result[$mailingID]['subject']); + $this->assertEquals(CRM_Core_Session::getLoggedInContactID(), $result[$mailingID]['creator_id'], 3); + } + + /** + * @return array + * @throws \Exception + */ + public function setupEventQueue() { + $contactID = $this->individualCreate(['first_name' => 'Test']); + $emailID = $this->callAPISuccessGetValue('Email', [ + 'return' => 'id', + 'contact_id' => $contactID, + ]); + $this->createLoggedInUser(); + $mailingID = $this->callAPISuccess('Mailing', 'create', [ + 'name' => 'Test Mailing', + 'subject' => 'Some Subject', + ])['id']; + $mailingJobID = $this->callAPISuccess('MailingJob', 'create', ['mailing_id' => $mailingID])['id']; + $eventQueueID = $this->callAPISuccess('MailingEventQueue', 'create', [ + 'contact_id' => $contactID, + 'mailing_id' => $mailingID, + 'email_id' => $emailID, + 'job_id' => $mailingJobID, + ])['id']; + return [$contactID, $mailingID, $eventQueueID]; } } -- 2.25.1