public function testWhetherCustomFieldValuesAreSubstituted()
 {
     global $wpdb;
     $currentTime = time();
     $custom_field_placeholder = "lname";
     $custom_field_value = "12345";
     //create autoresponder
     $createAutoresponderQuery = sprintf("INSERT INTO %swpr_autoresponders (nid, name) VALUES (%d, 'xperia');", $wpdb->prefix, $this->newsletter1_id);
     $this->assertEquals(1, $wpdb->query($createAutoresponderQuery));
     $autoresponder_id = $wpdb->insert_id;
     //create a custom field
     $createCustomFieldQuery = sprintf("INSERT INTO  %swpr_custom_fields (nid, type, name, label, enum) VALUES (%d, 'text', '{$custom_field_placeholder}', 'Last Name','');", $wpdb->prefix, $this->newsletter1_id);
     $this->assertEquals(1, $wpdb->query($createCustomFieldQuery));
     $custom_field_id = $wpdb->insert_id;
     //insert a subscriber
     $insertSubscriberQuery = sprintf("INSERT INTO %swpr_subscribers (`nid`, `name`, `email`, `date`, `active`, `confirmed`, `hash`) VALUES (%d, 'raj', '*****@*****.**', '324242424', 1, 1, '32asdf42');", $wpdb->prefix, $this->newsletter1_id);
     $this->assertEquals(1, $wpdb->query($insertSubscriberQuery));
     $subscriber_id = $wpdb->insert_id;
     //insert the value for the custom field for a specific subscriber
     $insertCustomFieldValue = sprintf("INSERT INTO  %swpr_custom_fields_values (`nid`, `sid`, `cid`, `value`) VALUES (%d, %d, %d, '{$custom_field_value}');", $wpdb->prefix, $this->newsletter1_id, $subscriber_id, $custom_field_id);
     $this->assertEquals(1, $wpdb->query($insertCustomFieldValue));
     //insert a message to the autoresponder with the custom field value in the html, text bodies and subject
     $insertAutoresponderMessageQuery = sprintf("INSERT INTO %swpr_autoresponder_messages (aid, `subject`, textbody, htmlbody, sequence) VALUES (%d, 'Subject [!%s!] [!name!]', '@@Text [!%s!] [!name!]@@', '@@Html [!%s!] [!name!]@@', 0)", $wpdb->prefix, $autoresponder_id, $custom_field_placeholder, $custom_field_placeholder, $custom_field_placeholder);
     $this->assertEquals(1, $wpdb->query($insertAutoresponderMessageQuery));
     //add a subscription for the above subscriber such that running the process will result in that message being enqueued.
     $insertSubscriptionQuery = sprintf("INSERT INTO %swpr_followup_subscriptions (eid, type, sid, doc, last_processed, last_date, sequence) VALUES (%d, 'autoresponder', %d, %d, %d, 0, -1);", $wpdb->prefix, $autoresponder_id, $subscriber_id, $currentTime, $currentTime);
     $this->assertEquals(1, $wpdb->query($insertSubscriptionQuery));
     //run the process
     $processor = AutoresponderProcessor::getProcessor();
     $processor->run();
     //fetch the delivered email for the target subscriber
     $getEmailsQuery = sprintf("SELECT * FROM %swpr_queue;", $wpdb->prefix);
     $emails = $wpdb->get_results($getEmailsQuery);
     $this->assertEquals(1, count($emails));
     $email = $emails[0];
     preg_match_all("#@@[^@]+@@#", $email->htmlbody, $matches);
     $match = $matches[0][0];
     $this->assertEquals(sprintf("@@Html %s raj@@", $custom_field_value), $match);
     preg_match_all("#@@[^@]+@@#", $email->textbody, $matches);
     $match = $matches[0][0];
     $this->assertEquals(sprintf("@@Text %s raj@@", $custom_field_value), $match);
     $this->assertEquals(sprintf("Subject %s raj", $custom_field_value), $email->subject);
     //assert whether that field was substituted in the delivered message
 }
 public function testWhetherRunningCronOnActivationFollowingDeactivationResultsInCronResumingFromLastProcessingPoint()
 {
     global $wpdb;
     $currentTime = time();
     $this->truncateQueue();
     $this->truncateMessagesDefinitions();
     $this->truncateSubscribers();
     $this->truncateSubscriptionsToFollowups();
     //print "Resume from last run... \r\n\r\n\r\n...";
     //create an autoresponder
     $createAutoresponderQuery = sprintf("INSERT INTO %swpr_autoresponders (nid, name) VALUES (%d, 'xperia');", $wpdb->prefix, $this->newsletter_id);
     $this->assertEquals(1, $wpdb->query($createAutoresponderQuery));
     $autoresponder_id = $wpdb->insert_id;
     //insert a subscriber
     $insertSubscriberQuery = sprintf("INSERT INTO %swpr_subscribers (`nid`, `name`, `email`, `date`, `active`, `confirmed`, `hash`) VALUES (%d, 'raj', '*****@*****.**', '324242424', 1, 1, '32ajkckfkfksdf42');", $wpdb->prefix, $this->newsletter_id);
     $this->assertEquals(1, $wpdb->query($insertSubscriberQuery));
     $subscriber_id = $wpdb->insert_id;
     $numberOfSubscribers = sprintf("SELECT * FROM %swpr_subscribers", $wpdb->prefix);
     $subscribers = $wpdb->get_results($numberOfSubscribers);
     $this->assertEquals(1, count($subscribers));
     //insert a message to that autoresponder for day 0 - immediately after subscription
     $message_ids = array();
     $insertAutoresponderMessageQuery = sprintf("INSERT INTO %swpr_autoresponder_messages (aid, `subject`, textbody, htmlbody, sequence) VALUES (%d, 'Subject 1', '@@Text 1@@', '@@Html 1@@', 0)", $wpdb->prefix, $autoresponder_id);
     $this->assertEquals(1, $wpdb->query($insertAutoresponderMessageQuery));
     $message_ids["0"] = $wpdb->insert_id;
     //insert a message to that autoresponder for day 1 - one day after subscription
     $insertAutoresponderMessageQuery = sprintf("INSERT INTO %swpr_autoresponder_messages (aid, `subject`, textbody, htmlbody, sequence) VALUES (%d, 'Subject 2', '@@Text @@', '@@Html @@', 1)", $wpdb->prefix, $autoresponder_id);
     $this->assertEquals(1, $wpdb->query($insertAutoresponderMessageQuery));
     $message_ids["1"] = $wpdb->insert_id;
     //inesrt a message to that autoresponder for day 5 - 5 days after subscription
     $insertAutoresponderMessageQuery = sprintf("INSERT INTO %swpr_autoresponder_messages (aid, `subject`, textbody, htmlbody, sequence) VALUES (%d, 'Subject 5', '@@Text 5@@', '@@Html 5@@', 5)", $wpdb->prefix, $autoresponder_id);
     $this->assertEquals(1, $wpdb->query($insertAutoresponderMessageQuery));
     $message_ids["5"] = $wpdb->insert_id;
     //add a subscription for the above subscriber such that running the process will result in that message being enqueued.
     $insertSubscriptionQuery = sprintf("INSERT INTO %swpr_followup_subscriptions (eid, type, sid, doc, last_processed, last_date, sequence) VALUES (%d, 'autoresponder', %d, %d, %d, 0, -1);", $wpdb->prefix, $autoresponder_id, $subscriber_id, $currentTime, $currentTime);
     $this->assertEquals(1, $wpdb->query($insertSubscriptionQuery));
     $processor = AutoresponderProcessor::getProcessor();
     $timeObject = new DateTime();
     $timeObject->setTimestamp($currentTime + 400);
     $processor->run_for_time($timeObject);
     //assert if this is the day zero email.
     $getQueueEmailQuery = sprintf("SELECT * FROM %swpr_queue;", $wpdb->prefix);
     $emails = $wpdb->get_results($getQueueEmailQuery);
     $this->assertEquals(1, count($emails));
     $first_email = $emails[0];
     $whetherMatches = preg_match(sprintf("#AR-%d-%d-%d-%d#", $autoresponder_id, $subscriber_id, $message_ids["0"], 0), $first_email->meta_key);
     $this->assertEquals(1, $whetherMatches);
     $this->truncateQueue();
     //run the cron after 7 days - simulated downtime. this should result in the delivery of email for day 1 when run on this day.
     $timeObject = new DateTime();
     $timeObject->setTimestamp($currentTime + 86400 * 7);
     $processor->run_for_time($timeObject);
     $getQueueEmailQuery = sprintf("SELECT * FROM %swpr_queue;", $wpdb->prefix);
     $emails = $wpdb->get_results($getQueueEmailQuery);
     $this->assertEquals(1, count($emails));
     $second_email = $emails[0];
     $this->assertEquals(sprintf("AR-%d-%d-%d-%d", $autoresponder_id, $subscriber_id, $message_ids["1"], 1), $second_email->meta_key);
     $this->truncateQueue();
     $getSequenceValueQuery = sprintf("SELECT sequence FROM %swpr_followup_subscriptions WHERE sid=%d AND eid=%d;", $wpdb->prefix, $subscriber_id, $autoresponder_id);
     $sequenceValueResults = $wpdb->get_results($getSequenceValueQuery);
     $value = $sequenceValueResults[0];
     $sequenceValue = $value->sequence;
     $this->assertEquals(1, $sequenceValue);
     //run the cron after 7 more days - that is the next email is on day 5 but we're again looking at a interim down time for 2 days past the intended date for next email
     $timeObject = new DateTime();
     $timeObject->setTimestamp($currentTime + 86400 * 14);
     $processor->run_for_time($timeObject);
     $getQueueEmailQuery = sprintf("SELECT * FROM %swpr_queue;", $wpdb->prefix);
     $emails = $wpdb->get_results($getQueueEmailQuery);
     $this->assertEquals(1, count($emails));
     $third_email = $emails[0];
     $this->assertEquals(sprintf("AR-%d-%d-%d-%d", $autoresponder_id, $subscriber_id, $message_ids["5"], 5), $third_email->meta_key);
 }
Beispiel #3
0
 public static function process_autoresponders()
 {
     $processor = AutoresponderProcessor::getProcessor();
     set_time_limit(WPR_MAX_AUTORESPONDER_PROCESS_EXECUTION_TIME);
     $timeOfStart = time();
     $currentTime = new DateTime(sprintf("@%d", $timeOfStart));
     $timeWhenAutoresponderProcessLastDidAHeartBeat = get_option("_wpr_autoresponder_process_status");
     if (self::whetherAnotherInstanceIsAlreadyRunning($timeOfStart, $timeWhenAutoresponderProcessLastDidAHeartBeat)) {
         return;
     }
     //set the time of ping for the autoresponder process status variable
     update_option("_wpr_autoresponder_process_status", $timeOfStart);
     //set the start time of the autoresponder process
     do_action("_wpr_autoresponder_process_start", $currentTime);
     //run the autoresponder processor
     $processor->run_for_time($currentTime);
     //call the hooks that need notification for the
     do_action('_wpr_autoresponder_process_end', $currentTime);
     update_option("_wpr_autoresponder_process_status", "stopped");
 }
 public static function getProcessor()
 {
     if (empty(AutoresponderProcessor::$processor)) {
         AutoresponderProcessor::$processor = new AutoresponderProcessor();
     }
     return AutoresponderProcessor::$processor;
 }