* @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();
}
* 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']);
}
/**
* 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']);
}
/**
* Test that the API returns a mailing properly when there is only one.
*/
public function testMailingContactDelivered() {
- $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_delivered.xml'
- )
- );
+ 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 = array(
- 'contact_id' => 23,
+ $params = [
+ 'contact_id' => $contactID,
'type' => 'Delivered',
- );
+ ];
$result = $this->callAPISuccess('MailingContact', 'get', $params);
$count = $this->callAPISuccess('MailingContact', 'getcount', $params);
$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];
}
}