chs fixed1
[civicrm-core.git] / tests / phpunit / api / v3 / MailingTest.php
index 8d4a63101b253fa964431e4a6b37f21191ffae33..ea7d79c1a6d3b026c2b9c65b38ad0cce221aa77b 100644 (file)
@@ -37,8 +37,11 @@ class api_v3_MailingTest extends CiviUnitTestCase {
   protected $_apiversion = 3;
   protected $_params = array();
   protected $_entity = 'Mailing';
-  public $_eNoticeCompliant = TRUE;
 
+
+  /**
+   * @return array
+   */
   function get_info() {
     return array(
       'name' => 'Mailer',
@@ -49,8 +52,8 @@ class api_v3_MailingTest extends CiviUnitTestCase {
 
   function setUp() {
     parent::setUp();
-    $this->_groupID    = $this->groupCreate(NULL);
-    $this->_email      = 'test@test.test';
+    $this->_groupID = $this->groupCreate();
+    $this->_email = 'test@test.test';
     $this->_params = array(
       'subject' => 'maild',
       'body_text' => 'bdkfhdskfhduew',
@@ -70,10 +73,74 @@ class api_v3_MailingTest extends CiviUnitTestCase {
     $result = $this->callAPIAndDocument('mailing', 'create', $this->_params, __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
+    unset($this->_params['created_id']); // return isn't working on this in getAndCheck so lets not check it for now
     $this->getAndCheck($this->_params, $result['id'], 'mailing');
   }
 
+  public function testMailerPreview() {
+    $contactID =  $this->individualCreate();
+    $displayName = $this->callAPISuccess('contact', 'get', array('id' => $contactID));
+    $displayName = $displayName['values'][$contactID]['display_name'];
+    $result = $this->callAPISuccess('mailing', 'create', $this->_params);
+    $params = array('id' => $result['id'], 'contact_id' => $contactID);
+    $result = $this->callAPISuccess('mailing', 'preview', $params);
+    $text = $result['values']['text'];
+    print_r($result  );
+    print_r($text  );
+    $this->assertEquals("This is $displayName", $text); // verify the text returned is correct, with replaced token
+    $this->deleteMailing($result['id']);
+  }
+  public function testMailerSendTestMail() {
+    $contactID =  $this->individualCreate();
+    $result = $this->callAPISuccess('contact', 'get', array('id' => $contactID));
+    $email = $result['values'][$contactID]['email'];
+    $mail = $this->callAPISuccess('mailing', 'create', $this->_params);
+    $params = array('mailing_id' => $mail['id'], 'test_email' => $email, 'test_group' => NULL);
+    $deliveredInfo = $this->callAPISuccess($this->_entity, 'send_test', $params);
+    $this->assertEquals(1, $deliveredInfo['count'], "in line " . __LINE__); // verify mail has been sent to user by count
+    $this->assertEquals($contactID, $deliveredInfo['values'][$deliveredInfo['id']]['contact_id'], "in line " . __LINE__); //verify the contact_id of the recipient
+    $this->deleteMailing($mail['id']);
+  }
+  public function testMailerStats() {
+    $result = $this->groupContactCreate($this->_groupID, 100);
+    $this->assertEquals(100, $result['added']); //verify if 100 contacts are added for group
+    $mail = $this->callAPISuccess('mailing', 'create', $this->_params);
+    $params = array('mailing_id' => $mail['id'], 'test_email' => NULL, 'test_group' => $this->_groupID, 'is_test' => 0);
+    $deliveredInfo = $this->callAPISuccess($this->_entity, 'send_test', $params);
+    $deliveredIds  = implode(',', array_keys($deliveredInfo['values']));
+    foreach (array('bounce', 'unsubscribe', 'opened') as $type) {
+      $sql = "CREATE TEMPORARY TABLE mail_{$type}_temp
+(event_queue_id int, time_stamp datetime, delivered_id int)
+SELECT event_queue_id, time_stamp, id
+ FROM civicrm_mailing_event_delivered
+ WHERE id IN ($deliveredIds)
+ ORDER BY RAND() LIMIT 0,20;";
+      print_r($sql  );
+      CRM_Core_DAO::executeQuery($sql);
+      $sql = "DELETE FROM civicrm_mailing_event_delivered WHERE id IN (SELECT delivered_id FROM mail_{$type}_temp);";
+      CRM_Core_DAO::executeQuery($sql);
+      if ($type == 'unsubscribe') {
+        $sql = "INSERT INTO civicrm_mailing_event_{$type} (event_queue_id, time_stamp, org_unsubscribe)
+SELECT event_queue_id, time_stamp, 1 FROM mail_{$type}_temp";
+      }
+      else {
+        $sql = "INSERT INTO civicrm_mailing_event_{$type} (event_queue_id, time_stamp)
+SELECT event_queue_id, time_stamp FROM mail_{$type}_temp";
+      }
+      CRM_Core_DAO::executeQuery($sql);
+    }
+    $result = $this->callAPISuccess('mailing', 'stats', array('mailing_id' => $mail['id']));
+    $expectedResult = array(
+      'Delivered' => 80, //since among 100 mails 20 has been bounced
+      'Bounces' => 20,
+      'Opened' => 20,
+      'Unique Clicks' => 0,
+      'Unsubscribers' => 20
+    );
+
+    $this->checkArrayEquals($expectedResult, $result['values'][$mail['id']]);
+  }
+
   /**
    * Test civicrm_mailing_delete
    */
@@ -121,7 +188,7 @@ class api_v3_MailingTest extends CiviUnitTestCase {
       'hash' => 'Wrong Hash',
       'event_subscribe_id' => '123',
       'time_stamp' => '20111111010101',
-         );
+    );
     $result = $this->callAPIFailure('mailing_event', 'confirm', $params,
       'Confirmation failed'
     );
@@ -144,7 +211,7 @@ class api_v3_MailingTest extends CiviUnitTestCase {
       'bodyTxt' => 'Body...',
       'replyTo' => $this->_email,
       'time_stamp' => '20111111010101',
-         );
+    );
     $result = $this->callAPIFailure('mailing_event', 'reply', $params,
       'Queue event could not be found'
     );
@@ -166,13 +233,12 @@ class api_v3_MailingTest extends CiviUnitTestCase {
       'hash' => 'Wrong Hash',
       'email' => $this->_email,
       'time_stamp' => '20111111010101',
-         );
+    );
     $result = $this->callAPIFailure('mailing_event', 'forward', $params,
       'Queue event could not be found'
     );
   }
 
-
 //----------- civicrm_mailing_create ----------
 
 }