From cc8197671349fa0d23219431dbcb7dd029f06813 Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Tue, 19 Jun 2018 10:21:10 -0700 Subject: [PATCH 01/65] Enables for cron request on flywheel. --- functions.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/functions.php b/functions.php index 992c46d..a8c5190 100644 --- a/functions.php +++ b/functions.php @@ -15,3 +15,11 @@ require_once('addons/post-type-campaign.php'); require_once('addons/remove-wp-extras.php'); require_once('addons/sharing.php'); + +// added to run cron request on flywheel +add_filter( 'cron_request', function( $args ) { + + $args['url'] = set_url_scheme( $args['url'], 'http' ); + return $args; + + } ); From f18f21b57902f3bf5aa8bc8f91aa5c2764066161 Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Tue, 19 Jun 2018 10:28:56 -0700 Subject: [PATCH 02/65] Updated ak mailing list id. --- addons/mailings.php | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/addons/mailings.php b/addons/mailings.php index d79ed77..88ea928 100644 --- a/addons/mailings.php +++ b/addons/mailings.php @@ -24,11 +24,13 @@ function get_distributions() } // Get active campaigns + //pulling out campaigns from wp post what have post_type set as campaign and post_status set as publish $campaigns = array(); $results = new WP_Query(array( 'post_type' => 'campaign', 'post_status' => 'publish', )); + foreach ($results->posts as $campaign) { $id = $campaign->ID; $campaigns[$id] = array( @@ -42,6 +44,7 @@ function get_distributions() 'valid' => true, ); + // Get all subjects $subjects = $campaigns[$id]['fields']['subjects']; for ($i = 0; $i < count($subjects); $i++) { @@ -52,6 +55,7 @@ function get_distributions() ); } } + // echo '
' . var_export( $campaigns, true) . '
'; // Get campaign performance $overall = array( @@ -112,12 +116,15 @@ function get_distributions() // Calculate shares $campaign_rate_sum = 0; + + // echo '
' . var_export($campaign, true) . '
'; + foreach ($campaigns as $campaign_index => &$campaign) { // Subjects $fields = $campaign['fields']; $subject_rate_sum = 0; $valid_subjects = 0; - + foreach ($campaign['subjects'] as $subject_index => &$subject) { $enabled = $fields['subjects'][$subject_index]['enabled']; $subject['title'] = $fields['subjects'][$subject_index]['subject']; @@ -215,7 +222,7 @@ function get_distributions() return -1; } }); - + return array( 'campaigns' => $campaigns, 'overall' => $overall, @@ -331,6 +338,7 @@ function send($params) $html = $this->render($params); global $ak; + $response = $ak->request(array( 'path' => 'mailer', 'method' => 'post', @@ -340,7 +348,7 @@ function send($params) 'notes' => 'Generated by VictoryKit', 'emailwrapper' => 27, // Demand Progress wrapper 'includes' => array( - 'lists' => array(26), // VK list. TODO: store this as a constant somewhere + 'lists' => array(25), // VK list. TODO: store this as a constant somewhere 'users' => $params['subscribers'], // Subscribers ), 'limit' => $params['limit'], // Limit users per mailing @@ -363,6 +371,9 @@ function send($params) 'data' => array(), )); + // '
' . var_export($response , true) . '
'; + + $response['ak_mailing_id'] = $ak_mailing_id; global $wpdb; @@ -389,7 +400,7 @@ function vk_mailings_update_subscribed_users_count_action() { FROM core_subscription AS cs WHERE - cs.list_id = 26 + cs.list_id = 25 "; $response = $ak->query($sql); if ($response['success']) { @@ -416,7 +427,7 @@ function vk_mailings_sync_subscribers_action() { FROM core_subscription AS cs WHERE - cs.list_id = 26 + cs.list_id = 25 '; $response = $ak->query($sql, true); $ids = array_map(function($el) { @@ -482,7 +493,11 @@ function vk_mailings_create_new_mailings_action() { global $vk_mailings, $wpdb; $limit_per_day = get_option('subscribed_users') / 7; + $distributions = $vk_mailings->get_distributions(); + $allCampaigns = trim(preg_replace('/\s\s+/', ' ', var_export($distributions, true))); + + error_log('#distributionResults vk_mailings_create_new_mailings_action: '.$allCampaigns); foreach ($distributions['campaigns'] as $campaign) { $id = $campaign['id']; @@ -734,6 +749,7 @@ function vk_mailings_cron_test_action() { $count = get_option('cron_test', 0); update_option('cron_test', $count + 1); } + add_action('vk_mailings_cron_test', 'vk_mailings_cron_test_action'); @@ -747,4 +763,8 @@ function create_mailings_instance() { return $vk_mailings; } -create_mailings_instance(); + +$vkInstance = create_mailings_instance(); + +// $vkInstance->vk_mailings_create_new_mailings_action(); +// vk_mailings_create_new_mailings_action(); From 9702175b62cb051198d0eb378dc176ffe7df5455 Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Tue, 19 Jun 2018 10:29:13 -0700 Subject: [PATCH 03/65] Adding constant values. --- addons/constants.php | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 addons/constants.php diff --git a/addons/constants.php b/addons/constants.php new file mode 100644 index 0000000..c8de6fe --- /dev/null +++ b/addons/constants.php @@ -0,0 +1,3 @@ + Date: Tue, 19 Jun 2018 10:34:05 -0700 Subject: [PATCH 04/65] Updated vk_mailings list id. --- addons/mailings.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/addons/mailings.php b/addons/mailings.php index 88ea928..6fa92c5 100644 --- a/addons/mailings.php +++ b/addons/mailings.php @@ -2,7 +2,7 @@ // Exit if accessed directly if(!defined('ABSPATH')) exit; - +require_once(__DIR__. '/../constants.php'); // Display errors ini_set('display_errors', 1); ini_set('display_startup_errors', 1); @@ -348,7 +348,7 @@ function send($params) 'notes' => 'Generated by VictoryKit', 'emailwrapper' => 27, // Demand Progress wrapper 'includes' => array( - 'lists' => array(25), // VK list. TODO: store this as a constant somewhere + 'lists' => array(VK_LIST_ID), // VK list. TODO: store this as a constant somewhere 'users' => $params['subscribers'], // Subscribers ), 'limit' => $params['limit'], // Limit users per mailing @@ -400,8 +400,7 @@ function vk_mailings_update_subscribed_users_count_action() { FROM core_subscription AS cs WHERE - cs.list_id = 25 - "; + cs.list_id ="+ VK_LIST_ID; $response = $ak->query($sql); if ($response['success']) { $count = $response['data']['user_count']; @@ -427,8 +426,7 @@ function vk_mailings_sync_subscribers_action() { FROM core_subscription AS cs WHERE - cs.list_id = 25 - '; + cs.list_id ='+VK_LIST_ID; $response = $ak->query($sql, true); $ids = array_map(function($el) { return +$el['user_id']; From c3b6376ef1bb1e06b6699ccad4ac3094ea8bcd2c Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Tue, 19 Jun 2018 10:41:43 -0700 Subject: [PATCH 05/65] Moved constants file out of folder. --- addons/constants.php | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 addons/constants.php diff --git a/addons/constants.php b/addons/constants.php deleted file mode 100644 index c8de6fe..0000000 --- a/addons/constants.php +++ /dev/null @@ -1,3 +0,0 @@ - Date: Tue, 19 Jun 2018 10:42:01 -0700 Subject: [PATCH 06/65] Added constants file from folder. --- constants.php | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 constants.php diff --git a/constants.php b/constants.php new file mode 100644 index 0000000..c8de6fe --- /dev/null +++ b/constants.php @@ -0,0 +1,3 @@ + Date: Tue, 19 Jun 2018 10:44:22 -0700 Subject: [PATCH 07/65] Added errorLogger for distributions. --- addons/mailings.php | 33 ++++++++------------------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/addons/mailings.php b/addons/mailings.php index 6fa92c5..711671b 100644 --- a/addons/mailings.php +++ b/addons/mailings.php @@ -24,13 +24,11 @@ function get_distributions() } // Get active campaigns - //pulling out campaigns from wp post what have post_type set as campaign and post_status set as publish $campaigns = array(); $results = new WP_Query(array( 'post_type' => 'campaign', 'post_status' => 'publish', )); - foreach ($results->posts as $campaign) { $id = $campaign->ID; $campaigns[$id] = array( @@ -44,7 +42,6 @@ function get_distributions() 'valid' => true, ); - // Get all subjects $subjects = $campaigns[$id]['fields']['subjects']; for ($i = 0; $i < count($subjects); $i++) { @@ -55,7 +52,6 @@ function get_distributions() ); } } - // echo '
' . var_export( $campaigns, true) . '
'; // Get campaign performance $overall = array( @@ -116,15 +112,12 @@ function get_distributions() // Calculate shares $campaign_rate_sum = 0; - - // echo '
' . var_export($campaign, true) . '
'; - foreach ($campaigns as $campaign_index => &$campaign) { // Subjects $fields = $campaign['fields']; $subject_rate_sum = 0; $valid_subjects = 0; - + foreach ($campaign['subjects'] as $subject_index => &$subject) { $enabled = $fields['subjects'][$subject_index]['enabled']; $subject['title'] = $fields['subjects'][$subject_index]['subject']; @@ -222,7 +215,7 @@ function get_distributions() return -1; } }); - + return array( 'campaigns' => $campaigns, 'overall' => $overall, @@ -338,7 +331,6 @@ function send($params) $html = $this->render($params); global $ak; - $response = $ak->request(array( 'path' => 'mailer', 'method' => 'post', @@ -371,9 +363,6 @@ function send($params) 'data' => array(), )); - // '
' . var_export($response , true) . '
'; - - $response['ak_mailing_id'] = $ak_mailing_id; global $wpdb; @@ -394,13 +383,13 @@ function send($params) // Run every 12 hours function vk_mailings_update_subscribed_users_count_action() { global $ak; - $sql = " + $sql = ' SELECT COUNT(DISTINCT cs.id) AS user_count FROM core_subscription AS cs WHERE - cs.list_id ="+ VK_LIST_ID; + cs.list_id ='+VK_LIST_ID; $response = $ak->query($sql); if ($response['success']) { $count = $response['data']['user_count']; @@ -491,12 +480,11 @@ function vk_mailings_create_new_mailings_action() { global $vk_mailings, $wpdb; $limit_per_day = get_option('subscribed_users') / 7; - $distributions = $vk_mailings->get_distributions(); - $allCampaigns = trim(preg_replace('/\s\s+/', ' ', var_export($distributions, true))); - - error_log('#distributionResults vk_mailings_create_new_mailings_action: '.$allCampaigns); + $allCampaigns = trim(preg_replace('/\s\s+/', ' ', var_export($distributions, true))); + error_log('#distributionResults vk_mailings_create_new_mailings_action: '.$allCampaigns); + foreach ($distributions['campaigns'] as $campaign) { $id = $campaign['id']; $fields = $campaign['fields']; @@ -747,7 +735,6 @@ function vk_mailings_cron_test_action() { $count = get_option('cron_test', 0); update_option('cron_test', $count + 1); } - add_action('vk_mailings_cron_test', 'vk_mailings_cron_test_action'); @@ -761,8 +748,4 @@ function create_mailings_instance() { return $vk_mailings; } - -$vkInstance = create_mailings_instance(); - -// $vkInstance->vk_mailings_create_new_mailings_action(); -// vk_mailings_create_new_mailings_action(); +create_mailings_instance(); From d211177cbd570abf4ea5b7f45254a516e218f47d Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Tue, 19 Jun 2018 10:49:31 -0700 Subject: [PATCH 08/65] Update boost constant as string value. --- constants.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/constants.php b/constants.php index c8de6fe..aca9c84 100644 --- a/constants.php +++ b/constants.php @@ -1,3 +1,3 @@ Date: Tue, 19 Jun 2018 10:49:58 -0700 Subject: [PATCH 09/65] Add boost variable. --- addons/mailings.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/addons/mailings.php b/addons/mailings.php index 711671b..0c01f4a 100644 --- a/addons/mailings.php +++ b/addons/mailings.php @@ -101,7 +101,7 @@ function get_distributions() // It probably is not needed if we are going to be sending each new campaign to more than several hundred people, // but it helps for testing with smaller amounts of people because it basically starts off the campaign at the same // rate as the overall campaign success rate and slightly adjusts from there - $boost = 500.0; + $boost = BOOST; $overall['boost'] = $boost; // Overall rate @@ -484,7 +484,7 @@ function vk_mailings_create_new_mailings_action() { $allCampaigns = trim(preg_replace('/\s\s+/', ' ', var_export($distributions, true))); error_log('#distributionResults vk_mailings_create_new_mailings_action: '.$allCampaigns); - + foreach ($distributions['campaigns'] as $campaign) { $id = $campaign['id']; $fields = $campaign['fields']; @@ -748,4 +748,5 @@ function create_mailings_instance() { return $vk_mailings; } + create_mailings_instance(); From 21ebcfdb59a8c2f2810579e95f30ebf8e993eaec Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Tue, 19 Jun 2018 11:48:03 -0700 Subject: [PATCH 10/65] Update all campaigns return value to show entire string returned. --- addons/mailings.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/addons/mailings.php b/addons/mailings.php index 0c01f4a..c4906c5 100644 --- a/addons/mailings.php +++ b/addons/mailings.php @@ -215,11 +215,11 @@ function get_distributions() return -1; } }); - + return array( 'campaigns' => $campaigns, 'overall' => $overall, - ); + ); } // Get all subscribers who have not been mailed for this campaign AND have not been mailed for any campaign within the last week @@ -482,7 +482,7 @@ function vk_mailings_create_new_mailings_action() { $limit_per_day = get_option('subscribed_users') / 7; $distributions = $vk_mailings->get_distributions(); - $allCampaigns = trim(preg_replace('/\s\s+/', ' ', var_export($distributions, true))); + $allCampaigns = trim(preg_replace('/\s+/', ' ',var_export($distributions, true))); error_log('#distributionResults vk_mailings_create_new_mailings_action: '.$allCampaigns); foreach ($distributions['campaigns'] as $campaign) { From 97563731751f2d359e28dccfceb64220422a3b15 Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Tue, 19 Jun 2018 14:00:24 -0700 Subject: [PATCH 11/65] Add one test for vk_mailings_create_new_mailings_action. --- addons/tests/arrayCampaigns.php | 165 ++++++++++++++++++++++++++++++++ addons/tests/mailing_test.php | 28 ++++++ 2 files changed, 193 insertions(+) create mode 100644 addons/tests/arrayCampaigns.php create mode 100644 addons/tests/mailing_test.php diff --git a/addons/tests/arrayCampaigns.php b/addons/tests/arrayCampaigns.php new file mode 100644 index 0000000..51e3eb2 --- /dev/null +++ b/addons/tests/arrayCampaigns.php @@ -0,0 +1,165 @@ + + array ( + 0 => array ( + 'conversions' => 0, + 'fields' => array ( + 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + 'from_line' => '41', + 'subjects' => array ( + 0 => array ( + 'subject' => 'Tell Senate: No attacks on immigrants', + 'enabled' => true, + ), 1 => array ( + 'subject' => "Block Trump's attacks on immigrants", + 'enabled' => true, + ), 2 => array ( + 'subject' => 'Protect immigrants', + 'enabled' => true, + ) + ), 'salutation' => '{{ user.first_name|default:"Hi" }},', + 'body' => '

bodyText

', + 'petition_text' => '

“Block funding for Trump’s attacks on immigrants, including his deportation force and his border wall. “

', + 'disclaimer' => '

disclaimer text

', + 'share_titles' => array ( + 0 => array ( + 'title' => 'Senate Democrats: Block Trump’s attacks on immigrants', + 'enabled' => true, + ), 1 => array ( + 'title' => 'Block the border wall!', + 'enabled' => true, ) + ), + 'share_descriptions' => array ( + 0 => array ( + 'description' => "There's new legislation to block Trump's border wall -- tell the Senate to pass it.", + 'enabled' => true, + ), 1 => array ( + 'description' => "The Senate can block Trump's anti-immigrant agenda, including the border wall.", + 'enabled' => true, ), + ), + 'share_images' => array ( + 0 => array ( + 'image' => array ( + 'ID' => 203, + 'id' => 203, + 'title' => 'Logo-square', + 'filename' => 'Logo-square.png', + 'url' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', 'alt' => '', + 'author' => '5', + 'description' => '', + 'caption' => '', + 'name' => 'logo-square', + 'date' => '2017-03-01 04:24:22', + 'modified' => '2017-03-01 04:24:22', + 'mime_type' => 'image/png', + 'type' => 'image', + 'icon' => 'http://victorykit.local/wp-includes/images/media/default.png', 'width' => 132, + 'height' => 125, + 'sizes' => array ( + 'thumbnail' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', 'thumbnail-width' => 132, + 'thumbnail-height' => 125, + 'medium' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', 'medium-width' => 132, + 'medium-height' => 125, + 'medium_large' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', 'medium_large-width' => 132, + 'medium_large-height' => 125, + 'large' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', 'large-width' => 132, + 'large-height' => 125, + ), + ), 'enabled' => true, + ), + ), + 'after_action_email_content' => '

Thank you for taking action. Now please share this important campaign with your friends, and ask them to join in it. This will make it much more likely we can win!

', + ), + 'id' => 219, + 'losses' => 0, + 'sent' => 4, + 'subjects' => array ( + 0 => array ( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 2, + 'title' => 'Tell Senate: No attacks on immigrants', + 'rate' => 0.98615439233166335, + 'share' => 0.33288948069241009, + ), 1 => array ( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 2, + 'title' => "Block Trump's attacks on immigrants", + 'rate' => 0.98615439233166335, + 'share' => 0.33288948069241009, ), + 2 => array ( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 0, + 'title' => 'Protect immigrants', + 'rate' => 0.99009900990099009, + 'share' => 0.33422103861517977, + ), + ), + 'title' => 'No wall testing', + 'valid' => true, + 'rate' => 0.9822410812509822, + 'share' => 0.0, 'limit' => 0, + ), 1 => array ( + 'conversions' => 0, + 'fields' => array ( + 'subjects' => array ( + 0 => array ( + 'subject' => 'Protect the Supreme Court', + 'enabled' => true, ), + 1 => array ( + 'subject' => "Stop Trump's takeover of the Supreme Court", + 'enabled' => true, ), + ), + 'salutation' => '{{ user.first_name|default:"Hi" }','body' => '

bodyText

', 'call_to_action_intro' => '

Submit a public comment to the CFPB: Issue the strongest possible rules to crack down on predatory payday lenders!

', + 'call_to_action_button' => 'SIGN NOW', 'petition_text' => '

“Defend the Supreme Court from Donald Trump’s racist, fascist agenda. Do not vote to confirm Neil Gorsuch or for cloture to end Sen. Merkley’s filibuster of his confirmation.”

', + 'statement_headline' => '', + 'statement_text' => '', + 'form_headline' => '', + 'disclaimer' => '', + 'share_titles' => array ( + 0 => array ( + 'title' => 'Tell the Senate: Block Gorsuch!', + 'enabled' => true, + ), 1 => array ( + 'title' => "Stop Trump's takeover of the Supreme Court!", + 'enabled' => true, + ), 2 => array ( + 'title' => 'Block Gorsuch!', 'enabled' => true, + ), 3 => array ( + 'title' => "Tell the Senate: Stop Trump's takeover of the Supreme Court!", + 'enabled' => true, ), + ), + 'share_descriptions' => array ( + 0 => array ( + 'description' => 'Neil Gorsuch will swing the Supreme Court to the far right. We can stop him.', + 'enabled' => true, + ), 1 => array ( + 'description' => 'Democrats need to stand strong and prevent the Supreme Court from taking a sharp right turn', 'enabled' => true, ), + ), + 'share_images' => + array ( 0 => array ( 'image' => false, 'enabled' => true, + ), + ), + 'petition_headline' => 'Testing', + 'from_line' => '41', 'after_action_email_content' => '

Thank you for taking action. Now please share this important campaign with your friends, and ask them to join in it. This will make it much more likely we can win!

', + ), 'id' => 212, + 'losses' => 0, + 'sent' => 1, + 'subjects' => array ( + 0 => array ( 'conversions' => 0, 'losses' => 0, + 'sent' => 1, + 'title' => 'Protect the Supreme Court', 'rate' => 0.98812276437224555, 'share' => 0.49950049950049946, + ), 1 => array ( 'conversions' => 0, 'losses' => 0, + 'sent' => 0, + 'title' => "Stop Trump's takeover of the Supreme Court", 'rate' => 0.99009900990099009, 'share' => 0.50049950049950054, + ), ), + 'title' => 'stop the fcc', + 'valid' => true, 'rate' => 0.98812276437224555, 'share' => 0.0, 'limit' => 0, ), + ), 'overall' => + array ( + 'conversions' => 0, + 'losses' => 0, 'sent' => 5, 'boost' => 500, 'rate' => 0.99009900990099009, ), + ); \ No newline at end of file diff --git a/addons/tests/mailing_test.php b/addons/tests/mailing_test.php new file mode 100644 index 0000000..18ab28b --- /dev/null +++ b/addons/tests/mailing_test.php @@ -0,0 +1,28 @@ +createMock(Mailings::class); + $vk_mailings->expects($this->once()) + ->method('get_distributions') + ->willReturn(array ( 'campaigns' => array ( ), 'overall' => array ( ), )); + + $result = vk_mailings_create_new_mailings_action(); + + $this->assertTrue($result === array ( 'campaigns' => array ( ), 'overall' => array ( ), )); + } + } \ No newline at end of file From 70661358fdf8467336ea6b991b06660cab57a751 Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Tue, 19 Jun 2018 14:41:57 -0700 Subject: [PATCH 12/65] Add campaign array from file. --- addons/tests/mailing_test.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/addons/tests/mailing_test.php b/addons/tests/mailing_test.php index 18ab28b..68aa745 100644 --- a/addons/tests/mailing_test.php +++ b/addons/tests/mailing_test.php @@ -15,13 +15,16 @@ final class mailingsClass extends TestCase { public function test_vk_mailings_create_new_mailings_action(): void { - global $vk_mailings, $wpdb; + global $vk_mailings, $wpdb, $CampaignArray; $vk_mailings = $this->createMock(Mailings::class); $vk_mailings->expects($this->once()) ->method('get_distributions') - ->willReturn(array ( 'campaigns' => array ( ), 'overall' => array ( ), )); - - $result = vk_mailings_create_new_mailings_action(); + ->willReturn($CampaignArray); + + $vk_mailings->expects($this->once()) + ->method('send'); + + $result = vk_mailings_create_new_mailings_action(); $this->assertTrue($result === array ( 'campaigns' => array ( ), 'overall' => array ( ), )); } From a2f8222091c461f80076d9b18fde55c76db8d162 Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Wed, 20 Jun 2018 09:30:06 -0700 Subject: [PATCH 13/65] Add argument to create mailings function. --- addons/mailings.php | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/addons/mailings.php b/addons/mailings.php index c4906c5..856900a 100644 --- a/addons/mailings.php +++ b/addons/mailings.php @@ -215,11 +215,16 @@ function get_distributions() return -1; } }); - - return array( + + + $allInfo = array( 'campaigns' => $campaigns, 'overall' => $overall, - ); + ); + + // echo '
'; print_r($allInfo); echo '
'; + + return $allInfo; } // Get all subscribers who have not been mailed for this campaign AND have not been mailed for any campaign within the last week @@ -476,14 +481,14 @@ function vk_mailings_update_mailing_stats_action() { // Create new mailings based on the currently running campaigns // Run once a day at 8am -function vk_mailings_create_new_mailings_action() { +function vk_mailings_create_new_mailings_action($get_distributions) { global $vk_mailings, $wpdb; $limit_per_day = get_option('subscribed_users') / 7; - $distributions = $vk_mailings->get_distributions(); - - $allCampaigns = trim(preg_replace('/\s+/', ' ',var_export($distributions, true))); - error_log('#distributionResults vk_mailings_create_new_mailings_action: '.$allCampaigns); + // $distributions = $vk_mailings->get_distributions(); + $distributions = $vk_mailings->$get_distributions(); + $allCampaigns = trim(preg_replace('/\s+/', ' ',var_export($distributions, true))); + error_log('#distributionResults line491 vk_mailings_create_new_mailings_action: '.$allCampaigns); foreach ($distributions['campaigns'] as $campaign) { $id = $campaign['id']; @@ -495,10 +500,12 @@ function vk_mailings_create_new_mailings_action() { // Out of users to send to? if (count($fresh_ids) == 0) { continue; - } + } + // Send mailing for each enabled subject foreach ($campaign['subjects'] as $index => $subject) { + $share = round($subject['share'] * $limit_per_campaign); // Skip disabled subjects @@ -544,6 +551,7 @@ function vk_mailings_create_new_mailings_action() { } } } + return $distributions; } add_action('vk_mailings_create_new_mailings', 'vk_mailings_create_new_mailings_action'); From 6c565b61756cc61be9ca8d69296e52f4821f6b8e Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Wed, 20 Jun 2018 15:49:20 -0700 Subject: [PATCH 14/65] Concated victory kit list id. --- addons/mailings.php | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/addons/mailings.php b/addons/mailings.php index 856900a..7ba790b 100644 --- a/addons/mailings.php +++ b/addons/mailings.php @@ -17,7 +17,7 @@ function __construct() function get_distributions() { global $wpdb; - + if (!get_option('subscribed_users')) { // no subscribed users in DB yet return array('campaigns' => array(), 'overall' => array()); @@ -184,7 +184,7 @@ function get_distributions() // Limit share percentages, based on subscriber availability $campaign_share_sum = 1; $limit_per_day = get_option('subscribed_users') / 7; - + foreach ($campaigns as $campaign_index => &$campaign) { $limit_per_campaign = round($campaign['share'] * $limit_per_day); $fresh_ids = count($this->get_fresh_subscribers_for_campaign($campaign['id'], $limit_per_campaign)); @@ -254,6 +254,8 @@ function get_fresh_subscribers_for_campaign($campaign_id, $limit) $ids = array_map(function($el) { return +$el; }, $response); + error_log('inside get_fresh_subscribers_for_campaign'); + var_dump($ids); return $ids; } @@ -394,7 +396,7 @@ function vk_mailings_update_subscribed_users_count_action() { FROM core_subscription AS cs WHERE - cs.list_id ='+VK_LIST_ID; + cs.list_id ='.VK_LIST_ID; $response = $ak->query($sql); if ($response['success']) { $count = $response['data']['user_count']; @@ -420,7 +422,7 @@ function vk_mailings_sync_subscribers_action() { FROM core_subscription AS cs WHERE - cs.list_id ='+VK_LIST_ID; + cs.list_id ='.VK_LIST_ID; $response = $ak->query($sql, true); $ids = array_map(function($el) { return +$el['user_id']; @@ -484,25 +486,30 @@ function vk_mailings_update_mailing_stats_action() { function vk_mailings_create_new_mailings_action($get_distributions) { global $vk_mailings, $wpdb; - $limit_per_day = get_option('subscribed_users') / 7; - // $distributions = $vk_mailings->get_distributions(); - $distributions = $vk_mailings->$get_distributions(); - $allCampaigns = trim(preg_replace('/\s+/', ' ',var_export($distributions, true))); - error_log('#distributionResults line491 vk_mailings_create_new_mailings_action: '.$allCampaigns); + $limit_per_day = get_option('subscribed_users') / 7; + $distributions = $vk_mailings->$get_distributions(); + // $allCampaigns = trim(preg_replace('/\s+/', ' ',var_export($distributions, true))); + // error_log('#distributionResults line491 vk_mailings_create_new_mailings_action: '.$allCampaigns); + foreach ($distributions['campaigns'] as $campaign) { + $id = $campaign['id']; $fields = $campaign['fields']; $url = get_permalink($id); $limit_per_campaign = round($campaign['share'] * $limit_per_day); + $fresh_ids = $vk_mailings->get_fresh_subscribers_for_campaign($id, $limit_per_campaign); - + + $all_ids = trim(preg_replace('/\s+/', ' ',var_export($distributions, true))); + error_log('#distributionResults line491 vk_mailings_create_new_mailings_action: '.$all_ids); + // Out of users to send to? if (count($fresh_ids) == 0) { continue; } - - + + // Send mailing for each enabled subject foreach ($campaign['subjects'] as $index => $subject) { From f7490045f4a7da15b490dcc33b440d2131cb62ac Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Thu, 21 Jun 2018 13:57:44 -0700 Subject: [PATCH 15/65] Added functions and variables to complete get_distributions function. --- addons/tests/mailing_test.php | 39 +++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/addons/tests/mailing_test.php b/addons/tests/mailing_test.php index 68aa745..41c9555 100644 --- a/addons/tests/mailing_test.php +++ b/addons/tests/mailing_test.php @@ -1,13 +1,29 @@ createMock(Mailings::class); - $vk_mailings->expects($this->once()) + global $CampaignArray; + + $vk_mailings_mock = $this->createMock(Mailings::class); + $vk_mailings_mock->expects($this->once()) ->method('get_distributions') ->willReturn($CampaignArray); - $vk_mailings->expects($this->once()) - ->method('send'); - - $result = vk_mailings_create_new_mailings_action(); + // $vk_mailings->expects($this->exactly(2)) + // ->method('get_fresh_subscribers_for_campaign') + // ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + + // $vk_mailings->expects($this->exactly(2)) + // ->method('send'); + + $result = vk_mailings_create_new_mailings_action(); - $this->assertTrue($result === array ( 'campaigns' => array ( ), 'overall' => array ( ), )); + $this->assertFalse($result === false); } } \ No newline at end of file From b3aa0b40cd780cd66faf03bd5379d4554699b574 Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Thu, 21 Jun 2018 14:11:21 -0700 Subject: [PATCH 16/65] Update function to pass in mock from mailing test for get distribution function. --- addons/mailings.php | 151 +++++++++++++++++++++++--------------------- 1 file changed, 80 insertions(+), 71 deletions(-) diff --git a/addons/mailings.php b/addons/mailings.php index 7ba790b..2e925c2 100644 --- a/addons/mailings.php +++ b/addons/mailings.php @@ -71,8 +71,9 @@ function get_distributions() GROUP BY vkm.campaign_id, vkm.variation_subject ', ARRAY_A); - + foreach ($mailings as $mailing) { + $id = $mailing['campaign_id']; // Make sure to only include currently published campaigns in overall data @@ -254,8 +255,10 @@ function get_fresh_subscribers_for_campaign($campaign_id, $limit) $ids = array_map(function($el) { return +$el; }, $response); - error_log('inside get_fresh_subscribers_for_campaign'); - var_dump($ids); + + $all_ids = trim(preg_replace('/\s+/', ' ',var_export($ids, true))); + error_log('#distributionResults line259 get_fresh_subscribers_for_campaign: '.$all_ids); + return $ids; } @@ -483,82 +486,88 @@ function vk_mailings_update_mailing_stats_action() { // Create new mailings based on the currently running campaigns // Run once a day at 8am -function vk_mailings_create_new_mailings_action($get_distributions) { +function vk_mailings_create_new_mailings_action($vk_mailings_instance) { global $vk_mailings, $wpdb; - + $limit_per_day = get_option('subscribed_users') / 7; - - $distributions = $vk_mailings->$get_distributions(); - // $allCampaigns = trim(preg_replace('/\s+/', ' ',var_export($distributions, true))); - // error_log('#distributionResults line491 vk_mailings_create_new_mailings_action: '.$allCampaigns); - foreach ($distributions['campaigns'] as $campaign) { - - $id = $campaign['id']; - $fields = $campaign['fields']; - $url = get_permalink($id); - $limit_per_campaign = round($campaign['share'] * $limit_per_day); - - $fresh_ids = $vk_mailings->get_fresh_subscribers_for_campaign($id, $limit_per_campaign); + if ($vk_mailings_instance === ''){ + $distributions = $vk_mailings->get_distributions(); + } else { + $distributions = $vk_mailings_instance->get_distributions(); + } + + $allCampaigns = trim(preg_replace('/\s+/', ' ',var_export($distributions, true))); + error_log('#distributionResults line494 vk_mailings_create_new_mailings_action: '.$allCampaigns); + +// foreach ($distributions['campaigns'] as $campaign) { + +// $id = $campaign['id']; +// $fields = $campaign['fields']; +// $url = get_permalink($id); +// $limit_per_campaign = round($campaign['share'] * $limit_per_day); - $all_ids = trim(preg_replace('/\s+/', ' ',var_export($distributions, true))); - error_log('#distributionResults line491 vk_mailings_create_new_mailings_action: '.$all_ids); +// $fresh_ids = $vk_mailings->get_fresh_subscribers_for_campaign($id, $limit_per_campaign); - // Out of users to send to? - if (count($fresh_ids) == 0) { - continue; - } +// // $all_ids = trim(preg_replace('/\s+/', ' ',var_export($distributions, true))); +// // error_log('#distributionResults line491 vk_mailings_create_new_mailings_action: '.$all_ids); +// // Out of users to send to? +// //code to stop count--> currently stops entire function from running +// if (count($fresh_ids) == 0) { +// continue; +// } - // Send mailing for each enabled subject - foreach ($campaign['subjects'] as $index => $subject) { + +// // Send mailing for each enabled subject +// foreach ($campaign['subjects'] as $index => $subject) { - $share = round($subject['share'] * $limit_per_campaign); - - // Skip disabled subjects - if ($share == 0) { - continue; - } - - // Claim IDs - $subscribers = array_splice($fresh_ids, 0, $share); - - // Create mailing - $params = array( - 'from_line' => $fields['from_line'], - 'body' => str_replace('”', '"', $fields['body']), - 'petition_headline' => str_replace('”', '"', $fields['petition_headline']), - 'campaign_id' => $id, - 'limit' => $share, - 'salutation' => str_replace('”', '"', $fields['salutation']), - 'subject' => $fields['subjects'][$index]['subject'], - 'subscribers' => $subscribers, - 'url' => $url, - 'variation_subject' => $index, - ); - - $response = $vk_mailings->send($params); - - // Save mailing records for VK - $ak_mailing_id = $response['ak_mailing_id']; - $subscriber_chunks = array_chunk($subscribers, 1000); // Chunk so SQL queries dont get too big - foreach ($subscriber_chunks as $subscriber_chunk) { - $values = array(); - foreach ($subscriber_chunk as $subscriber) { - $values[] = "($subscriber, $ak_mailing_id, $id)"; - } - $values = join(', ', $values); - $sql = " - INSERT INTO vk_subscriber_mailing - (ak_user_id, ak_mailing_id, campaign_id) - VALUES - $values; - "; - $wpdb->query($sql); - } - } - } - return $distributions; +// $share = round($subject['share'] * $limit_per_campaign); + +// // Skip disabled subjects +// if ($share == 0) { +// continue; +// } + +// // Claim IDs +// $subscribers = array_splice($fresh_ids, 0, $share); + +// // Create mailing +// $params = array( +// 'from_line' => $fields['from_line'], +// 'body' => str_replace('”', '"', $fields['body']), +// 'petition_headline' => str_replace('”', '"', $fields['petition_headline']), +// 'campaign_id' => $id, +// 'limit' => $share, +// 'salutation' => str_replace('”', '"', $fields['salutation']), +// 'subject' => $fields['subjects'][$index]['subject'], +// 'subscribers' => $subscribers, +// 'url' => $url, +// 'variation_subject' => $index, +// ); + +// $response = $vk_mailings->send($params); + +// // Save mailing records for VK +// $ak_mailing_id = $response['ak_mailing_id']; +// $subscriber_chunks = array_chunk($subscribers, 1000); // Chunk so SQL queries dont get too big +// foreach ($subscriber_chunks as $subscriber_chunk) { +// $values = array(); +// foreach ($subscriber_chunk as $subscriber) { +// $values[] = "($subscriber, $ak_mailing_id, $id)"; +// } +// $values = join(', ', $values); +// $sql = " +// INSERT INTO vk_subscriber_mailing +// (ak_user_id, ak_mailing_id, campaign_id) +// VALUES +// $values; +// "; +// $wpdb->query($sql); +// } +// } +// } +// return $distributions; } add_action('vk_mailings_create_new_mailings', 'vk_mailings_create_new_mailings_action'); From d4f3000141db2e03aabbb904d37972d1f478753d Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Thu, 21 Jun 2018 14:11:56 -0700 Subject: [PATCH 17/65] Passed in mailings mock to create mailings function. --- addons/tests/mailing_test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/tests/mailing_test.php b/addons/tests/mailing_test.php index 41c9555..44f7f6b 100644 --- a/addons/tests/mailing_test.php +++ b/addons/tests/mailing_test.php @@ -45,7 +45,7 @@ public function test_vk_mailings_create_new_mailings_action(): void // $vk_mailings->expects($this->exactly(2)) // ->method('send'); - $result = vk_mailings_create_new_mailings_action(); + $result = vk_mailings_create_new_mailings_action($vk_mailings_mock); $this->assertFalse($result === false); } From e7f797fdad86b907d92653e22075ab1b271cf06b Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Thu, 21 Jun 2018 15:59:58 -0700 Subject: [PATCH 18/65] Tracking down sql bug. --- addons/mailings.php | 248 +++++++++++++++++++++++--------------------- 1 file changed, 130 insertions(+), 118 deletions(-) diff --git a/addons/mailings.php b/addons/mailings.php index 2e925c2..aa52114 100644 --- a/addons/mailings.php +++ b/addons/mailings.php @@ -232,7 +232,7 @@ function get_distributions() function get_fresh_subscribers_for_campaign($campaign_id, $limit) { global $wpdb; - + $sql = " SELECT vks.ak_user_id @@ -251,13 +251,20 @@ function get_fresh_subscribers_for_campaign($campaign_id, $limit) vksm.ak_user_id IS NULL LIMIT $limit; "; + $sqlReturn = trim(preg_replace('/\s+/', ' ',var_export($sql, true))); + error_log('#sql return line 255: '.$sqlReturn); + $response = $wpdb->get_col($sql, 0); + + $responseWP = trim(preg_replace('/\s+/', ' ',var_export($response, true))); + error_log('#get_col reponse line 260'.$responseWP); + $ids = array_map(function($el) { return +$el; }, $response); - $all_ids = trim(preg_replace('/\s+/', ' ',var_export($ids, true))); - error_log('#distributionResults line259 get_fresh_subscribers_for_campaign: '.$all_ids); + // $all_ids = trim(preg_replace('/\s+/', ' ',var_export($ids, true))); + // error_log('#distributionResults line259 get_fresh_subscribers_for_campaign: '.$all_ids); return $ids; } @@ -340,50 +347,50 @@ function send($params) // Render $html = $this->render($params); - global $ak; - $response = $ak->request(array( - 'path' => 'mailer', - 'method' => 'post', - 'data' => array( - 'fromline' => "/rest/v1/fromline/{$params['from_line']}/", - 'subjects' => array($params['subject']), - 'notes' => 'Generated by VictoryKit', - 'emailwrapper' => 27, // Demand Progress wrapper - 'includes' => array( - 'lists' => array(VK_LIST_ID), // VK list. TODO: store this as a constant somewhere - 'users' => $params['subscribers'], // Subscribers - ), - 'limit' => $params['limit'], // Limit users per mailing - // 'excludes' => array( - // 'mailings' => $mailings, // Array of mailing IDs, for avoiding multiple sends - // ), - 'tags' => array('victorykit'), - 'html' => $html, - 'sort_by' => 'random', - ), - )); - - $location = $response['headers']->getAll()['location']; - preg_match('%/(\d+)/$%', $location, $matches); - $ak_mailing_id = +$matches[1]; - - $response = $ak->request(array( - 'path' => "mailer/$ak_mailing_id/rebuild", - 'method' => 'post', - 'data' => array(), - )); - - $response['ak_mailing_id'] = $ak_mailing_id; - - global $wpdb; - $wpdb->insert('vk_mailing', array( - 'ak_mailing_id' => $ak_mailing_id, - 'campaign_id' => $params['campaign_id'], - 'status' => 'rebuilding', - 'variation_subject' => $params['variation_subject'], - )); - - return $response; + // global $ak; + // $response = $ak->request(array( + // 'path' => 'mailer', + // 'method' => 'post', + // 'data' => array( + // 'fromline' => "/rest/v1/fromline/{$params['from_line']}/", + // 'subjects' => array($params['subject']), + // 'notes' => 'Generated by VictoryKit', + // 'emailwrapper' => 27, // Demand Progress wrapper + // 'includes' => array( + // 'lists' => array(VK_LIST_ID), // VK list. TODO: store this as a constant somewhere + // 'users' => $params['subscribers'], // Subscribers + // ), + // 'limit' => $params['limit'], // Limit users per mailing + // // 'excludes' => array( + // // 'mailings' => $mailings, // Array of mailing IDs, for avoiding multiple sends + // // ), + // 'tags' => array('victorykit'), + // 'html' => $html, + // 'sort_by' => 'random', + // ), + // )); + + // $location = $response['headers']->getAll()['location']; + // preg_match('%/(\d+)/$%', $location, $matches); + // $ak_mailing_id = +$matches[1]; + + // $response = $ak->request(array( + // 'path' => "mailer/$ak_mailing_id/rebuild", + // 'method' => 'post', + // 'data' => array(), + // )); + + // $response['ak_mailing_id'] = $ak_mailing_id; + + // global $wpdb; + // $wpdb->insert('vk_mailing', array( + // 'ak_mailing_id' => $ak_mailing_id, + // 'campaign_id' => $params['campaign_id'], + // 'status' => 'rebuilding', + // 'variation_subject' => $params['variation_subject'], + // )); + + // return $response; } } @@ -488,85 +495,90 @@ function vk_mailings_update_mailing_stats_action() { // Run once a day at 8am function vk_mailings_create_new_mailings_action($vk_mailings_instance) { global $vk_mailings, $wpdb; - + + if ($vk_mailings_instance !== ''){ + $vk_mailings = $vk_mailings_instance; + } + $limit_per_day = get_option('subscribed_users') / 7; - - if ($vk_mailings_instance === ''){ - $distributions = $vk_mailings->get_distributions(); - } else { - $distributions = $vk_mailings_instance->get_distributions(); - } - + error_log('limit per day'.$limit_per_day ); + $distributions = $vk_mailings->get_distributions(); $allCampaigns = trim(preg_replace('/\s+/', ' ',var_export($distributions, true))); error_log('#distributionResults line494 vk_mailings_create_new_mailings_action: '.$allCampaigns); -// foreach ($distributions['campaigns'] as $campaign) { + foreach ($distributions['campaigns'] as $campaign) { -// $id = $campaign['id']; -// $fields = $campaign['fields']; -// $url = get_permalink($id); -// $limit_per_campaign = round($campaign['share'] * $limit_per_day); - -// $fresh_ids = $vk_mailings->get_fresh_subscribers_for_campaign($id, $limit_per_campaign); - -// // $all_ids = trim(preg_replace('/\s+/', ' ',var_export($distributions, true))); -// // error_log('#distributionResults line491 vk_mailings_create_new_mailings_action: '.$all_ids); + $id = $campaign['id']; + $fields = $campaign['fields']; + $url = get_permalink($id); + + print($campaign['share']); + print(' %%%%% '); + print($limit_per_day); + $limit_per_campaign = round($campaign['share'] * $limit_per_day); -// // Out of users to send to? -// //code to stop count--> currently stops entire function from running -// if (count($fresh_ids) == 0) { -// continue; -// } + $fresh_ids = $vk_mailings->get_fresh_subscribers_for_campaign($id, $limit_per_campaign); + + // $all_ids = trim(preg_replace('/\s+/', ' ',var_export($distributions, true))); + // error_log('#distributionResults line491 vk_mailings_create_new_mailings_action: '.$all_ids); + // Out of users to send to? + //code to stop count--> currently stops entire function from running + if (count($fresh_ids) == 0) { + continue; + } -// // Send mailing for each enabled subject -// foreach ($campaign['subjects'] as $index => $subject) { - -// $share = round($subject['share'] * $limit_per_campaign); - -// // Skip disabled subjects -// if ($share == 0) { -// continue; -// } - -// // Claim IDs -// $subscribers = array_splice($fresh_ids, 0, $share); - -// // Create mailing -// $params = array( -// 'from_line' => $fields['from_line'], -// 'body' => str_replace('”', '"', $fields['body']), -// 'petition_headline' => str_replace('”', '"', $fields['petition_headline']), -// 'campaign_id' => $id, -// 'limit' => $share, -// 'salutation' => str_replace('”', '"', $fields['salutation']), -// 'subject' => $fields['subjects'][$index]['subject'], -// 'subscribers' => $subscribers, -// 'url' => $url, -// 'variation_subject' => $index, -// ); - -// $response = $vk_mailings->send($params); - -// // Save mailing records for VK -// $ak_mailing_id = $response['ak_mailing_id']; -// $subscriber_chunks = array_chunk($subscribers, 1000); // Chunk so SQL queries dont get too big -// foreach ($subscriber_chunks as $subscriber_chunk) { -// $values = array(); -// foreach ($subscriber_chunk as $subscriber) { -// $values[] = "($subscriber, $ak_mailing_id, $id)"; -// } -// $values = join(', ', $values); -// $sql = " -// INSERT INTO vk_subscriber_mailing -// (ak_user_id, ak_mailing_id, campaign_id) -// VALUES -// $values; -// "; -// $wpdb->query($sql); -// } -// } -// } + // Send mailing for each enabled subject + foreach ($campaign['subjects'] as $index => $subject) { + + $share = round($subject['share'] * $limit_per_campaign); + + // Skip disabled subjects + // print($subject['share']); + // print(' %%%%% '); + // print($limit_per_campaign); + if ($share == 0) { + continue; + } + + // Claim IDs + $subscribers = array_splice($fresh_ids, 0, $share); + + // Create mailing + $params = array( + 'from_line' => $fields['from_line'], + 'body' => str_replace('”', '"', $fields['body']), + 'petition_headline' => str_replace('”', '"', $fields['petition_headline']), + 'campaign_id' => $id, + 'limit' => $share, + 'salutation' => str_replace('”', '"', $fields['salutation']), + 'subject' => $fields['subjects'][$index]['subject'], + 'subscribers' => $subscribers, + 'url' => $url, + 'variation_subject' => $index, + ); + + $response = $vk_mailings->send($params); + + // Save mailing records for VK + $ak_mailing_id = $response['ak_mailing_id']; + $subscriber_chunks = array_chunk($subscribers, 1000); // Chunk so SQL queries dont get too big + foreach ($subscriber_chunks as $subscriber_chunk) { + $values = array(); + foreach ($subscriber_chunk as $subscriber) { + $values[] = "($subscriber, $ak_mailing_id, $id)"; + } + $values = join(', ', $values); + $sql = " + INSERT INTO vk_subscriber_mailing + (ak_user_id, ak_mailing_id, campaign_id) + VALUES + $values; + "; + $wpdb->query($sql); + } + } + } // return $distributions; } add_action('vk_mailings_create_new_mailings', 'vk_mailings_create_new_mailings_action'); From 59046fcf45aaae1689767fc009d0b3adfc892f79 Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Fri, 20 Jul 2018 11:24:15 -0700 Subject: [PATCH 19/65] Added single campaign object. --- addons/tests/singleCampaign.php | 118 ++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 addons/tests/singleCampaign.php diff --git a/addons/tests/singleCampaign.php b/addons/tests/singleCampaign.php new file mode 100644 index 0000000..a950bc6 --- /dev/null +++ b/addons/tests/singleCampaign.php @@ -0,0 +1,118 @@ + array ( + 0 => array ( + 'conversions' => 0, + 'fields' => array ( + 'subjects' => array ( + 0 => array ( + 'subject' => 'Tell Senate: No attacks on immigrants', + 'enabled' => true, + ), + 1 => array ( + 'subject' => "Block Trump's attacks on immigrants", + 'enabled' => true, + ), + ), + 'salutation' => '{{ user.first_name|default:"Hi" }},', + 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

Already he has banned Muslims and refugees, threatened sanctuary cities and criminalized millions of undocumented immigrants. His Immigration and Custom Enforcement (ICE) and Border Patrol agents are separating parents from their children, deporting innocent people and spreading fear in immigrant communities across the country. The budget he just released slashes funding for the social safety net in order to spend billions of dollars building his wall, increasing his deportation force and expediting deportations.

Senate Democrats have the power to block funding for Trump’s wall and his massive deportation force. Recent reports show they may be willing to fight to stop Trump’s hateful agenda. Can you help make sure they stand strong?

We cannot let any Senate Democrat cave to political pressure from the right-wing and give Republicans the 60 votes they need to fund Trump’s hate. That is where progressives come in. We have to show Democratic senators that we will have their backs if they act boldly and that we will hold them accountable if they fail to stand up for immigrants.

Tell Senate Democrats: Block funding for Trump’s attacks on immigrants. Click here to sign the petition.

Trump wants to harass, deport and reject people who have come or are dreaming of coming to the United States seeking refuge from conflict, lives free from persecution and better economic opportunities for their families. For as long as Trump is president and extremist right-wing Republicans are willing to enable his agenda, Senate Democrats will have a choice: enable Trump’s agenda of criminalization and deportations, or resist and obstruct his racist regime at every turn. It is up to make sure they do the right thing.

Tell Senate Democrats: Block funding for Trump’s attacks on immigrants.

', + 'petition_text' => '

“Block funding for Trump’s attacks on immigrants, including his deportation force and his border wall. “

', + 'disclaimer' => '

We do not share your email address without your permission. We may send you updates on this and other important campaigns by email. If at any time you would like to unsubscribe from our email list, you may do so.

', + 'share_titles' => array ( + 0 => array ( + 'title' => 'Senate Democrats: Block Trump’s attacks on immigrants', + 'enabled' => true, + ), + 1 => array ( + 'title' => 'Block the border wall!', + 'enabled' => true, + ), + ), + 'share_descriptions' => array ( + 0 => array ( + 'description' => "There's new legislation to block Trump's border wall -- tell the Senate to pass it.", + 'enabled' => true, ), + 1 => array ( + 'description' => "The Senate can block Trump's anti-immigrant agenda, including the border wall.", + 'enabled' => true, + ), + ), + 'share_images' => array ( + 0 => array ( + 'image' => array ( + 'ID' => 203, + 'id' => 203, + 'title' => 'Logo-square', + 'filename' => 'Logo-square.png', + 'url' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', + 'alt' => '', + 'author' => '5', + 'description' => '', + 'caption' => '', + 'name' => 'logo-square', + 'date' => '2017-03-01 04:24:22', + 'modified' => '2017-03-01 04:24:22', + 'mime_type' => 'image/png', + 'type' => 'image', + 'icon' => 'http://victorykit.local/wp-includes/images/media/default.png', + 'width' => 132, + 'height' => 125, + 'sizes' => array ( + 'thumbnail' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', + 'thumbnail-width' => 132, + 'thumbnail-height' => 125, + 'medium' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', + 'medium-width' => 132, + 'medium-height' => 125, + 'medium_large' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', + 'medium_large-width' => 132, + 'medium_large-height' => 125, + 'large' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', + 'large-width' => 132, + 'large-height' => 125, + ), + ), + 'enabled' => true, + ), + ), + 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + 'from_line' => '41', + 'landing_page_body' => '', + 'after_action_email_content' => '

Thank you for taking action. Now please share this important campaign with your friends, and ask them to join in it. This will make it much more likely we can win!

', + ), + 'id' => 263, + 'losses' => 0, + 'sent' => 0, + 'subjects' => array ( + 0 => array ( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 0, + 'title' => 'Tell Senate: No attacks on immigrants', + 'rate' => 1.0, + 'share' => 0.5, + ), + 1 => array ( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 0, + 'title' => "Block Trump's attacks on immigrants", + 'rate' => 1.0, + 'share' => 0.5, + ), + ), + 'title' => 'No wall testing', + 'valid' => true, + 'rate' => 1.0, + 'share' => 1.0, + 'limit' => 4.0, + ), + ), + 'overall' => array ( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 0, + 'boost' => 500.0, + 'rate' => 1.0, + ), + ); \ No newline at end of file From 7707b439c770650f531793c6bc3abfce6f376fe2 Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Fri, 20 Jul 2018 11:24:35 -0700 Subject: [PATCH 20/65] Updated name of campaign object. --- addons/tests/twoCampaigns.php | 183 ++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 addons/tests/twoCampaigns.php diff --git a/addons/tests/twoCampaigns.php b/addons/tests/twoCampaigns.php new file mode 100644 index 0000000..e10d0f4 --- /dev/null +++ b/addons/tests/twoCampaigns.php @@ -0,0 +1,183 @@ + + array ( + 0 => array ( + 'conversions' => 0, + 'fields' => array ( + 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + 'from_line' => '41', + 'subjects' => array ( + 0 => array ( + 'subject' => 'Tell Senate: No attacks on immigrants', + 'enabled' => true, + ), 1 => array ( + 'subject' => "Block Trump's attacks on immigrants", + 'enabled' => true, + ), 2 => array ( + 'subject' => 'Protect immigrants', + 'enabled' => true, + ) + ), 'salutation' => '{{ user.first_name|default:"Hi" }},', + 'body' => '

bodyText

', + 'petition_text' => '

“Block funding for Trump’s attacks on immigrants, including his deportation force and his border wall. “

', + 'disclaimer' => '

disclaimer text

', + 'share_titles' => array ( + 0 => array ( + 'title' => 'Senate Democrats: Block Trump’s attacks on immigrants', + 'enabled' => true, + ), 1 => array ( + 'title' => 'Block the border wall!', + 'enabled' => true, ) + ), + 'share_descriptions' => array ( + 0 => array ( + 'description' => "There's new legislation to block Trump's border wall -- tell the Senate to pass it.", + 'enabled' => true, + ), 1 => array ( + 'description' => "The Senate can block Trump's anti-immigrant agenda, including the border wall.", + 'enabled' => true, ), + ), + 'share_images' => array ( + 0 => array ( + 'image' => array ( + 'ID' => 203, + 'id' => 203, + 'title' => 'Logo-square', + 'filename' => 'Logo-square.png', + 'url' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', 'alt' => '', + 'author' => '5', + 'description' => '', + 'caption' => '', + 'name' => 'logo-square', + 'date' => '2017-03-01 04:24:22', + 'modified' => '2017-03-01 04:24:22', + 'mime_type' => 'image/png', + 'type' => 'image', + 'icon' => 'http://victorykit.local/wp-includes/images/media/default.png', 'width' => 132, + 'height' => 125, + 'sizes' => array ( + 'thumbnail' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', 'thumbnail-width' => 132, + 'thumbnail-height' => 125, + 'medium' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', 'medium-width' => 132, + 'medium-height' => 125, + 'medium_large' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', 'medium_large-width' => 132, + 'medium_large-height' => 125, + 'large' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', 'large-width' => 132, + 'large-height' => 125, + ), + ), 'enabled' => true, + ), + ), + 'after_action_email_content' => '

Thank you for taking action. Now please share this important campaign with your friends, and ask them to join in it. This will make it much more likely we can win!

', + ), + 'id' => 219, + 'losses' => 0, + 'sent' => 4, + 'subjects' => array ( + 0 => array ( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 2, + 'title' => 'Tell Senate: No attacks on immigrants', + 'rate' => 0.98615439233166335, + 'share' => 0.33288948069241009, + ), 1 => array ( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 2, + 'title' => "Block Trump's attacks on immigrants", + 'rate' => 0.98615439233166335, + 'share' => 0.33288948069241009, ), + 2 => array ( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 0, + 'title' => 'Protect immigrants', + 'rate' => 0.99009900990099009, + 'share' => 0.33422103861517977, + ), + ), + 'title' => 'No wall testing', + 'valid' => true, + 'rate' => 0.9822410812509822, + 'share' => 10.0, + 'limit' => 0, + ), 1 => array ( + 'conversions' => 0, + 'fields' => array ( + 'subjects' => array ( + 0 => array ( + 'subject' => 'Protect the Supreme Court', + 'enabled' => true, ), + 1 => array ( + 'subject' => "Stop Trump's takeover of the Supreme Court", + 'enabled' => true, ), + ), + 'salutation' => '{{ user.first_name|default:"Hi" }','body' => '

bodyText

', 'call_to_action_intro' => '

Submit a public comment to the CFPB: Issue the strongest possible rules to crack down on predatory payday lenders!

', + 'call_to_action_button' => 'SIGN NOW', 'petition_text' => '

“Defend the Supreme Court from Donald Trump’s racist, fascist agenda. Do not vote to confirm Neil Gorsuch or for cloture to end Sen. Merkley’s filibuster of his confirmation.”

', + 'statement_headline' => '', + 'statement_text' => '', + 'form_headline' => '', + 'disclaimer' => '', + 'share_titles' => array ( + 0 => array ( + 'title' => 'Tell the Senate: Block Gorsuch!', + 'enabled' => true, + ), 1 => array ( + 'title' => "Stop Trump's takeover of the Supreme Court!", + 'enabled' => true, + ), 2 => array ( + 'title' => 'Block Gorsuch!', 'enabled' => true, + ), 3 => array ( + 'title' => "Tell the Senate: Stop Trump's takeover of the Supreme Court!", + 'enabled' => true, ), + ), + 'share_descriptions' => array ( + 0 => array ( + 'description' => 'Neil Gorsuch will swing the Supreme Court to the far right. We can stop him.', + 'enabled' => true, + ), 1 => array ( + 'description' => 'Democrats need to stand strong and prevent the Supreme Court from taking a sharp right turn', 'enabled' => true, ), + ), + 'share_images' => + array ( + 0 => array ( + 'image' => false, + 'enabled' => true, + ), + ), + 'petition_headline' => 'Testing', + 'from_line' => '41', + 'after_action_email_content' => '

Thank you for taking action. Now please share this important campaign with your friends, and ask them to join in it. This will make it much more likely we can win!

', + ), + 'id' => 212, + 'losses' => 0, + 'sent' => 1, + 'subjects' => array ( + 0 => array ( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 1, + 'title' => 'Protect the Supreme Court', + 'rate' => 0.98812276437224555, + 'share' => 0.49950049950049946, + ), + 1 => array ( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 0, + 'title' => "Stop Trump's takeover of the Supreme Court", + 'rate' => 0.99009900990099009, + 'share' => 0.50049950049950054, + ), + ), + 'title' => 'stop the fcc', + 'valid' => true, 'rate' => 0.98812276437224555, 'share' => 0.0, + 'limit' => 0, + ), + ), 'overall' => + array ( + 'conversions' => 0, + 'losses' => 0, 'sent' => 5, 'boost' => 500, 'rate' => 0.99009900990099009, ), + ); \ No newline at end of file From f920461c9155508c1028fc1d62a2404467eba40c Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Fri, 20 Jul 2018 11:27:33 -0700 Subject: [PATCH 21/65] Updated file name. --- addons/tests/arrayCampaigns.php | 165 -------------------------------- 1 file changed, 165 deletions(-) delete mode 100644 addons/tests/arrayCampaigns.php diff --git a/addons/tests/arrayCampaigns.php b/addons/tests/arrayCampaigns.php deleted file mode 100644 index 51e3eb2..0000000 --- a/addons/tests/arrayCampaigns.php +++ /dev/null @@ -1,165 +0,0 @@ - - array ( - 0 => array ( - 'conversions' => 0, - 'fields' => array ( - 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', - 'from_line' => '41', - 'subjects' => array ( - 0 => array ( - 'subject' => 'Tell Senate: No attacks on immigrants', - 'enabled' => true, - ), 1 => array ( - 'subject' => "Block Trump's attacks on immigrants", - 'enabled' => true, - ), 2 => array ( - 'subject' => 'Protect immigrants', - 'enabled' => true, - ) - ), 'salutation' => '{{ user.first_name|default:"Hi" }},', - 'body' => '

bodyText

', - 'petition_text' => '

“Block funding for Trump’s attacks on immigrants, including his deportation force and his border wall. “

', - 'disclaimer' => '

disclaimer text

', - 'share_titles' => array ( - 0 => array ( - 'title' => 'Senate Democrats: Block Trump’s attacks on immigrants', - 'enabled' => true, - ), 1 => array ( - 'title' => 'Block the border wall!', - 'enabled' => true, ) - ), - 'share_descriptions' => array ( - 0 => array ( - 'description' => "There's new legislation to block Trump's border wall -- tell the Senate to pass it.", - 'enabled' => true, - ), 1 => array ( - 'description' => "The Senate can block Trump's anti-immigrant agenda, including the border wall.", - 'enabled' => true, ), - ), - 'share_images' => array ( - 0 => array ( - 'image' => array ( - 'ID' => 203, - 'id' => 203, - 'title' => 'Logo-square', - 'filename' => 'Logo-square.png', - 'url' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', 'alt' => '', - 'author' => '5', - 'description' => '', - 'caption' => '', - 'name' => 'logo-square', - 'date' => '2017-03-01 04:24:22', - 'modified' => '2017-03-01 04:24:22', - 'mime_type' => 'image/png', - 'type' => 'image', - 'icon' => 'http://victorykit.local/wp-includes/images/media/default.png', 'width' => 132, - 'height' => 125, - 'sizes' => array ( - 'thumbnail' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', 'thumbnail-width' => 132, - 'thumbnail-height' => 125, - 'medium' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', 'medium-width' => 132, - 'medium-height' => 125, - 'medium_large' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', 'medium_large-width' => 132, - 'medium_large-height' => 125, - 'large' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', 'large-width' => 132, - 'large-height' => 125, - ), - ), 'enabled' => true, - ), - ), - 'after_action_email_content' => '

Thank you for taking action. Now please share this important campaign with your friends, and ask them to join in it. This will make it much more likely we can win!

', - ), - 'id' => 219, - 'losses' => 0, - 'sent' => 4, - 'subjects' => array ( - 0 => array ( - 'conversions' => 0, - 'losses' => 0, - 'sent' => 2, - 'title' => 'Tell Senate: No attacks on immigrants', - 'rate' => 0.98615439233166335, - 'share' => 0.33288948069241009, - ), 1 => array ( - 'conversions' => 0, - 'losses' => 0, - 'sent' => 2, - 'title' => "Block Trump's attacks on immigrants", - 'rate' => 0.98615439233166335, - 'share' => 0.33288948069241009, ), - 2 => array ( - 'conversions' => 0, - 'losses' => 0, - 'sent' => 0, - 'title' => 'Protect immigrants', - 'rate' => 0.99009900990099009, - 'share' => 0.33422103861517977, - ), - ), - 'title' => 'No wall testing', - 'valid' => true, - 'rate' => 0.9822410812509822, - 'share' => 0.0, 'limit' => 0, - ), 1 => array ( - 'conversions' => 0, - 'fields' => array ( - 'subjects' => array ( - 0 => array ( - 'subject' => 'Protect the Supreme Court', - 'enabled' => true, ), - 1 => array ( - 'subject' => "Stop Trump's takeover of the Supreme Court", - 'enabled' => true, ), - ), - 'salutation' => '{{ user.first_name|default:"Hi" }','body' => '

bodyText

', 'call_to_action_intro' => '

Submit a public comment to the CFPB: Issue the strongest possible rules to crack down on predatory payday lenders!

', - 'call_to_action_button' => 'SIGN NOW', 'petition_text' => '

“Defend the Supreme Court from Donald Trump’s racist, fascist agenda. Do not vote to confirm Neil Gorsuch or for cloture to end Sen. Merkley’s filibuster of his confirmation.”

', - 'statement_headline' => '', - 'statement_text' => '', - 'form_headline' => '', - 'disclaimer' => '', - 'share_titles' => array ( - 0 => array ( - 'title' => 'Tell the Senate: Block Gorsuch!', - 'enabled' => true, - ), 1 => array ( - 'title' => "Stop Trump's takeover of the Supreme Court!", - 'enabled' => true, - ), 2 => array ( - 'title' => 'Block Gorsuch!', 'enabled' => true, - ), 3 => array ( - 'title' => "Tell the Senate: Stop Trump's takeover of the Supreme Court!", - 'enabled' => true, ), - ), - 'share_descriptions' => array ( - 0 => array ( - 'description' => 'Neil Gorsuch will swing the Supreme Court to the far right. We can stop him.', - 'enabled' => true, - ), 1 => array ( - 'description' => 'Democrats need to stand strong and prevent the Supreme Court from taking a sharp right turn', 'enabled' => true, ), - ), - 'share_images' => - array ( 0 => array ( 'image' => false, 'enabled' => true, - ), - ), - 'petition_headline' => 'Testing', - 'from_line' => '41', 'after_action_email_content' => '

Thank you for taking action. Now please share this important campaign with your friends, and ask them to join in it. This will make it much more likely we can win!

', - ), 'id' => 212, - 'losses' => 0, - 'sent' => 1, - 'subjects' => array ( - 0 => array ( 'conversions' => 0, 'losses' => 0, - 'sent' => 1, - 'title' => 'Protect the Supreme Court', 'rate' => 0.98812276437224555, 'share' => 0.49950049950049946, - ), 1 => array ( 'conversions' => 0, 'losses' => 0, - 'sent' => 0, - 'title' => "Stop Trump's takeover of the Supreme Court", 'rate' => 0.99009900990099009, 'share' => 0.50049950049950054, - ), ), - 'title' => 'stop the fcc', - 'valid' => true, 'rate' => 0.98812276437224555, 'share' => 0.0, 'limit' => 0, ), - ), 'overall' => - array ( - 'conversions' => 0, - 'losses' => 0, 'sent' => 5, 'boost' => 500, 'rate' => 0.99009900990099009, ), - ); \ No newline at end of file From 873b235a1b669eec3931141a4042a11ac8848ea7 Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Fri, 20 Jul 2018 11:29:37 -0700 Subject: [PATCH 22/65] Added test for single campaign. --- addons/tests/mailing_test.php | 56 +++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 13 deletions(-) diff --git a/addons/tests/mailing_test.php b/addons/tests/mailing_test.php index 44f7f6b..6759d14 100644 --- a/addons/tests/mailing_test.php +++ b/addons/tests/mailing_test.php @@ -5,10 +5,11 @@ require_once(__DIR__. '/../actionkit.php'); require_once(__DIR__. '/../mailings.php'); -require_once(__DIR__. '/arrayCampaigns.php'); +require_once(__DIR__. '/twoCampaigns.php'); +require_once(__DIR__. '/singleCampaign.php'); function get_option(){ - return true; + return 25; } function add_action(){} function get_permalink(){} @@ -21,6 +22,12 @@ class wpdb { function get_results(){ return array(); } + + function get_col(){ + return array(); + } + + function query(){} }; $wpdb = new wpdb; @@ -31,22 +38,45 @@ final class mailingsClass extends TestCase { public function test_vk_mailings_create_new_mailings_action(): void { - global $CampaignArray; + global $TwoCampaigns; $vk_mailings_mock = $this->createMock(Mailings::class); + $vk_mailings_mock->expects($this->once()) - ->method('get_distributions') - ->willReturn($CampaignArray); + ->method('get_distributions') + ->willReturn($TwoCampaigns); - // $vk_mailings->expects($this->exactly(2)) - // ->method('get_fresh_subscribers_for_campaign') - // ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + $vk_mailings_mock->expects($this->exactly(2)) + ->method('get_fresh_subscribers_for_campaign') + ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + + $vk_mailings_mock->expects($this->exactly(3)) + ->method('send'); - // $vk_mailings->expects($this->exactly(2)) - // ->method('send'); - $result = vk_mailings_create_new_mailings_action($vk_mailings_mock); - - $this->assertFalse($result === false); + + // $this->assertTrue($result === null); } + + public function test_get_distribution_one_campaign(): void + { + global $singleCampaign; + + $vk_mailings_mock = $this->createMock(Mailings::class); + + $vk_mailings_mock->expects($this->once()) + ->method('get_distributions') + ->willReturn($singleCampaign); + + $vk_mailings_mock->expects($this->exactly(1)) + ->method('get_fresh_subscribers_for_campaign') + ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + + $vk_mailings_mock->expects($this->exactly(2)) + ->method('send'); + + vk_mailings_create_new_mailings_action($vk_mailings_mock); + + // $this->assertFalse($result === false); + } } \ No newline at end of file From 3f0769ca4ac3123ada1dabc637a7387d477d93b6 Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Mon, 23 Jul 2018 11:16:59 -0700 Subject: [PATCH 23/65] Update vk_mailings_create_new_mailings_action to take two parameters. --- addons/mailings.php | 228 ++++++++++++++++++++++++-------------------- 1 file changed, 125 insertions(+), 103 deletions(-) diff --git a/addons/mailings.php b/addons/mailings.php index aa52114..4b12eb3 100644 --- a/addons/mailings.php +++ b/addons/mailings.php @@ -17,7 +17,7 @@ function __construct() function get_distributions() { global $wpdb; - + if (!get_option('subscribed_users')) { // no subscribed users in DB yet return array('campaigns' => array(), 'overall' => array()); @@ -29,6 +29,7 @@ function get_distributions() 'post_type' => 'campaign', 'post_status' => 'publish', )); + foreach ($results->posts as $campaign) { $id = $campaign->ID; $campaigns[$id] = array( @@ -71,9 +72,9 @@ function get_distributions() GROUP BY vkm.campaign_id, vkm.variation_subject ', ARRAY_A); - + + foreach ($mailings as $mailing) { - $id = $mailing['campaign_id']; // Make sure to only include currently published campaigns in overall data @@ -107,19 +108,29 @@ function get_distributions() // Overall rate $overall['rate'] = ($overall['conversions'] - $overall['losses'] + $boost) / ($overall['sent'] + $boost); + + // $camp = trim(preg_replace('/\s+/', ' ',var_export( $campaigns, true))); + // error_log('line 199 campaigns setting stats overall with boost '.$camp); + // $arrayThing = ARRAY_A; + // $aThing = trim(preg_replace('/\s+/', ' ',var_export( $arrayThing, true))); + // error_log('line 80 Array_A value '.$aThing); + if ($overall['rate'] < 0) { $overall['rate'] = 0; // TODO: why would we not track negative results? } // Calculate shares $campaign_rate_sum = 0; + + foreach ($campaigns as $campaign_index => &$campaign) { // Subjects $fields = $campaign['fields']; $subject_rate_sum = 0; $valid_subjects = 0; - foreach ($campaign['subjects'] as $subject_index => &$subject) { + + foreach ($campaign['subjects'] as $subject_index => &$subject) { $enabled = $fields['subjects'][$subject_index]['enabled']; $subject['title'] = $fields['subjects'][$subject_index]['subject']; if (!$enabled) { @@ -185,12 +196,16 @@ function get_distributions() // Limit share percentages, based on subscriber availability $campaign_share_sum = 1; $limit_per_day = get_option('subscribed_users') / 7; - + foreach ($campaigns as $campaign_index => &$campaign) { $limit_per_campaign = round($campaign['share'] * $limit_per_day); + // $limit = trim(preg_replace('/\s+/', ' ',var_export($limit_per_campaign, true))); + // error_log('line 192 limit_per_campaign '.$limit); $fresh_ids = count($this->get_fresh_subscribers_for_campaign($campaign['id'], $limit_per_campaign)); - + // $fresh = trim(preg_replace('/\s+/', ' ',var_export($fresh_ids, true))); + // error_log('line 195 fresh ids '.$fresh); // Plenty? Great. + //fresh ids should not be 0 if ($fresh_ids >= $limit_per_campaign) { $campaign['limit'] = $limit_per_campaign; continue; @@ -198,9 +213,17 @@ function get_distributions() // Shortage of fresh IDs. Calculations are required. $campaign['limit'] = $fresh_ids; + //CURRENTLY RETURNING 0 FOR $SHARE THIS IS MAKING A NAN VALUE $share = $fresh_ids / $limit_per_day; + $campaign_share_sum += $share - $campaign['share']; + $campaign['share'] = $share; + + // $campShare = $campaign['share']; + // $allsql = trim(preg_replace('/\s+/', ' ',var_export( $campShare, true))); + // error_log('line 225 $campaign_share_sum '.$campaign_share_sum); + // error_log('line 226 variable campaing[share] '.$share); } foreach ($campaigns as $campaign_index => &$campaign) { $campaign['share'] = $campaign['share'] / $campaign_share_sum; @@ -216,23 +239,18 @@ function get_distributions() return -1; } }); - - $allInfo = array( + return array( 'campaigns' => $campaigns, 'overall' => $overall, ); - - // echo '
'; print_r($allInfo); echo '
'; - - return $allInfo; } // Get all subscribers who have not been mailed for this campaign AND have not been mailed for any campaign within the last week function get_fresh_subscribers_for_campaign($campaign_id, $limit) { global $wpdb; - + $sql = " SELECT vks.ak_user_id @@ -251,21 +269,15 @@ function get_fresh_subscribers_for_campaign($campaign_id, $limit) vksm.ak_user_id IS NULL LIMIT $limit; "; - $sqlReturn = trim(preg_replace('/\s+/', ' ',var_export($sql, true))); - error_log('#sql return line 255: '.$sqlReturn); - - $response = $wpdb->get_col($sql, 0); - $responseWP = trim(preg_replace('/\s+/', ' ',var_export($response, true))); - error_log('#get_col reponse line 260'.$responseWP); + // $allsql = trim(preg_replace('/\s+/', ' ',var_export( $sql, true))); + // error_log('line 260 sql query '.$allsql); + + $response = $wpdb->get_col($sql, 0); $ids = array_map(function($el) { return +$el; }, $response); - - // $all_ids = trim(preg_replace('/\s+/', ' ',var_export($ids, true))); - // error_log('#distributionResults line259 get_fresh_subscribers_for_campaign: '.$all_ids); - return $ids; } @@ -343,54 +355,56 @@ function send($params) ) { return; } - + // $distributions_results = trim(preg_replace('/\s+/', ' ',var_export( $params, true))); + // error_log('@@@ line 359 '.$distributions_results); // Render $html = $this->render($params); - // global $ak; - // $response = $ak->request(array( - // 'path' => 'mailer', - // 'method' => 'post', - // 'data' => array( - // 'fromline' => "/rest/v1/fromline/{$params['from_line']}/", - // 'subjects' => array($params['subject']), - // 'notes' => 'Generated by VictoryKit', - // 'emailwrapper' => 27, // Demand Progress wrapper - // 'includes' => array( - // 'lists' => array(VK_LIST_ID), // VK list. TODO: store this as a constant somewhere - // 'users' => $params['subscribers'], // Subscribers - // ), - // 'limit' => $params['limit'], // Limit users per mailing - // // 'excludes' => array( - // // 'mailings' => $mailings, // Array of mailing IDs, for avoiding multiple sends - // // ), - // 'tags' => array('victorykit'), - // 'html' => $html, - // 'sort_by' => 'random', - // ), - // )); - - // $location = $response['headers']->getAll()['location']; - // preg_match('%/(\d+)/$%', $location, $matches); - // $ak_mailing_id = +$matches[1]; - - // $response = $ak->request(array( - // 'path' => "mailer/$ak_mailing_id/rebuild", - // 'method' => 'post', - // 'data' => array(), - // )); - - // $response['ak_mailing_id'] = $ak_mailing_id; - - // global $wpdb; - // $wpdb->insert('vk_mailing', array( - // 'ak_mailing_id' => $ak_mailing_id, - // 'campaign_id' => $params['campaign_id'], - // 'status' => 'rebuilding', - // 'variation_subject' => $params['variation_subject'], - // )); - - // return $response; + global $ak; + error_log('@@@ limit_per_day variable '.$ak); + $response = $ak->request(array( + 'path' => 'mailer', + 'method' => 'post', + 'data' => array( + 'fromline' => "/rest/v1/fromline/{$params['from_line']}/", + 'subjects' => array($params['subject']), + 'notes' => 'Generated by VictoryKit', + 'emailwrapper' => 27, // Demand Progress wrapper + 'includes' => array( + 'lists' => array(VK_LIST_ID), // VK list. TODO: store this as a constant somewhere + 'users' => $params['subscribers'], // Subscribers + ), + 'limit' => $params['limit'], // Limit users per mailing + // 'excludes' => array( + // 'mailings' => $mailings, // Array of mailing IDs, for avoiding multiple sends + // ), + 'tags' => array('victorykit'), + 'html' => $html, + 'sort_by' => 'random', + ), + )); + + $location = $response['headers']->getAll()['location']; + preg_match('%/(\d+)/$%', $location, $matches); + $ak_mailing_id = +$matches[1]; + + $response = $ak->request(array( + 'path' => "mailer/$ak_mailing_id/rebuild", + 'method' => 'post', + 'data' => array(), + )); + + $response['ak_mailing_id'] = $ak_mailing_id; + + global $wpdb; + $wpdb->insert('vk_mailing', array( + 'ak_mailing_id' => $ak_mailing_id, + 'campaign_id' => $params['campaign_id'], + 'status' => 'rebuilding', + 'variation_subject' => $params['variation_subject'], + )); + + return $response; } } @@ -399,15 +413,18 @@ function send($params) // Update total count of subscribers in VictoryKit list // Run every 12 hours function vk_mailings_update_subscribed_users_count_action() { + global $ak; - $sql = ' + + $sql = " SELECT COUNT(DISTINCT cs.id) AS user_count FROM core_subscription AS cs WHERE - cs.list_id ='.VK_LIST_ID; + cs.list_id =".VK_LIST_ID; $response = $ak->query($sql); + if ($response['success']) { $count = $response['data']['user_count']; } else { @@ -432,7 +449,8 @@ function vk_mailings_sync_subscribers_action() { FROM core_subscription AS cs WHERE - cs.list_id ='.VK_LIST_ID; + cs.list_id ='.VK_LIST_ID + ; $response = $ak->query($sql, true); $ids = array_map(function($el) { return +$el['user_id']; @@ -493,50 +511,45 @@ function vk_mailings_update_mailing_stats_action() { // Create new mailings based on the currently running campaigns // Run once a day at 8am -function vk_mailings_create_new_mailings_action($vk_mailings_instance) { +function vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock) { global $vk_mailings, $wpdb; - if ($vk_mailings_instance !== ''){ - $vk_mailings = $vk_mailings_instance; - } - - $limit_per_day = get_option('subscribed_users') / 7; - error_log('limit per day'.$limit_per_day ); - $distributions = $vk_mailings->get_distributions(); - $allCampaigns = trim(preg_replace('/\s+/', ' ',var_export($distributions, true))); - error_log('#distributionResults line494 vk_mailings_create_new_mailings_action: '.$allCampaigns); - - foreach ($distributions['campaigns'] as $campaign) { + if($vk_mailings_mock){ + $vk_mailings = $vk_mailings_mock; + $wpdb = $wpdb_mock; + } + $limit_per_day = get_option('subscribed_users') / 7; + + $distributions = $vk_mailings->get_distributions(); + // $distributions_results = trim(preg_replace('/\s+/', ' ',var_export( $distributions, true))); + // $distributions_results = sizeof($distributions_results); + // error_log('@@@ limit_per_day variable '.$distributions_results); + foreach ($distributions['campaigns'] as $campaign) { $id = $campaign['id']; $fields = $campaign['fields']; $url = get_permalink($id); - - print($campaign['share']); - print(' %%%%% '); - print($limit_per_day); + //need to print out why $limit_per_campaign NAN -> this is breaking everything + // $allsql = trim(preg_replace('/\s+/', ' ',var_export( $campaign, true))); + // error_log('%%%%%%%%%%% '); + // error_log('line 531 $campaign '.$allsql); + $shares = $campaign['share']; + // $sCamp = trim(preg_replace('/\s+/', ' ',var_export( $shares, true))); + // error_log('line 535 campaign[share] variable '.$sCamp); $limit_per_campaign = round($campaign['share'] * $limit_per_day); - + $fresh_ids = $vk_mailings->get_fresh_subscribers_for_campaign($id, $limit_per_campaign); - - // $all_ids = trim(preg_replace('/\s+/', ' ',var_export($distributions, true))); - // error_log('#distributionResults line491 vk_mailings_create_new_mailings_action: '.$all_ids); - + // Out of users to send to? - //code to stop count--> currently stops entire function from running if (count($fresh_ids) == 0) { continue; - } - + } + // Send mailing for each enabled subject foreach ($campaign['subjects'] as $index => $subject) { - $share = round($subject['share'] * $limit_per_campaign); - + // Skip disabled subjects - // print($subject['share']); - // print(' %%%%% '); - // print($limit_per_campaign); if ($share == 0) { continue; } @@ -557,13 +570,19 @@ function vk_mailings_create_new_mailings_action($vk_mailings_instance) { 'url' => $url, 'variation_subject' => $index, ); - - $response = $vk_mailings->send($params); + // $allsql = trim(preg_replace('/\s+/', ' ',var_export( $params, true))); + // error_log('s line 563: '.$allsql); + // $sCamp = trim(preg_replace('/\s+/', ' ',var_export( $params, true))); + // error_log('$$$$$$$$'); + // error_log('line 550 $subject variable '.$sCamp); + $response = $vk_mailings->send($params); + // Save mailing records for VK $ak_mailing_id = $response['ak_mailing_id']; $subscriber_chunks = array_chunk($subscribers, 1000); // Chunk so SQL queries dont get too big foreach ($subscriber_chunks as $subscriber_chunk) { + $values = array(); foreach ($subscriber_chunk as $subscriber) { $values[] = "($subscriber, $ak_mailing_id, $id)"; @@ -579,7 +598,6 @@ function vk_mailings_create_new_mailings_action($vk_mailings_instance) { } } } -// return $distributions; } add_action('vk_mailings_create_new_mailings', 'vk_mailings_create_new_mailings_action'); @@ -786,3 +804,7 @@ function create_mailings_instance() { } create_mailings_instance(); + +// do_action('vk_mailings_create_new_mailings'); +// $allsql = trim(preg_replace('/\s+/', ' ',var_export( $mailings, true))); +// error_log('sql mailing_stats line 468: '.$allsql); \ No newline at end of file From 2117ceee082b6e98af26bd200d8ee72d117f8e7f Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Mon, 23 Jul 2018 11:21:13 -0700 Subject: [PATCH 24/65] Two subject campaign. --- addons/tests/mailing_test.php | 110 ++++++++++++++++++++++------------ 1 file changed, 71 insertions(+), 39 deletions(-) diff --git a/addons/tests/mailing_test.php b/addons/tests/mailing_test.php index 6759d14..563f794 100644 --- a/addons/tests/mailing_test.php +++ b/addons/tests/mailing_test.php @@ -7,6 +7,7 @@ require_once(__DIR__. '/../mailings.php'); require_once(__DIR__. '/twoCampaigns.php'); require_once(__DIR__. '/singleCampaign.php'); +require_once(__DIR__. '/twoSubjectCampaign.php'); function get_option(){ return 25; @@ -19,64 +20,95 @@ class WP_Query { } class wpdb { - function get_results(){ - return array(); - } + function get_results(){return array();} - function get_col(){ - return array(); - } + function get_col(){return array();} function query(){} }; -$wpdb = new wpdb; +// $wpdb = new wpdb; + +// class ak { + +// }; + +// $ak = new ak; use PHPUnit\Framework\TestCase; final class mailingsClass extends TestCase - { - public function test_vk_mailings_create_new_mailings_action(): void - { - global $TwoCampaigns; - - $vk_mailings_mock = $this->createMock(Mailings::class); - - $vk_mailings_mock->expects($this->once()) - ->method('get_distributions') - ->willReturn($TwoCampaigns); - - $vk_mailings_mock->expects($this->exactly(2)) - ->method('get_fresh_subscribers_for_campaign') - ->willReturn(array( 0 => 6630470, 1 => 6630472,)); - - $vk_mailings_mock->expects($this->exactly(3)) - ->method('send'); - - $result = vk_mailings_create_new_mailings_action($vk_mailings_mock); - - // $this->assertTrue($result === null); - } + { + public function test_vk_mailings_create_new_mailings_action_two_campaigns(): void { + global $TwoCampaigns; - public function test_get_distribution_one_campaign(): void - { + $vk_mailings_mock = $this->createMock(Mailings::class); + $wpdb_mock = $this->createMock(wpdb::class); + + $vk_mailings_mock->expects($this->once()) + ->method('get_distributions') + ->willReturn($TwoCampaigns); + + $vk_mailings_mock->expects($this->exactly(2)) + ->method('get_fresh_subscribers_for_campaign') + ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + + $vk_mailings_mock->expects($this->exactly(3)) + ->method('send'); + + $wpdb_mock->expects($this->exactly(1)) + ->method('query'); + + $result = vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock); + // $this->assertTrue($result === null); + } + + public function test_vk_mailings_create_new_mailings_action_one_campaigns(): void { global $singleCampaign; $vk_mailings_mock = $this->createMock(Mailings::class); + $wpdb_mock = $this->createMock(wpdb::class); $vk_mailings_mock->expects($this->once()) - ->method('get_distributions') - ->willReturn($singleCampaign); - + ->method('get_distributions') + ->willReturn($singleCampaign); + $vk_mailings_mock->expects($this->exactly(1)) - ->method('get_fresh_subscribers_for_campaign') - ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + ->method('get_fresh_subscribers_for_campaign') + ->willReturn(array( 0 => 6630470, 1 => 6630472,)); $vk_mailings_mock->expects($this->exactly(2)) - ->method('send'); + ->method('send'); + + $wpdb_mock->expects($this->exactly(1)) + ->method('query'); + + vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock); + // $this->assertFalse($result === false); + } + + public function test_get_distribution_two_subject_campaign(): void { + global $twoSubjectCampaign; + + $vk_mailings_mock = $this->createMock(Mailings::class); + $wpdb_mock = $this->createMock(wpdb::class); + + $vk_mailings_mock->expects($this->once()) + ->method('get_distributions') + ->willReturn($twoSubjectCampaign); + + $vk_mailings_mock->expects($this->exactly(1)) + ->method('get_fresh_subscribers_for_campaign') + ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + + $vk_mailings_mock->expects($this->exactly(1)) + ->method('send') + ->willReturn(array ( 'ak_mailing_id' => 1)); + + $wpdb_mock->expects($this->exactly(1)) + ->method('query'); - vk_mailings_create_new_mailings_action($vk_mailings_mock); - + vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock); // $this->assertFalse($result === false); } } \ No newline at end of file From 8242d8feb9f1208de6a65fade2612b64eb6542bf Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Mon, 23 Jul 2018 11:23:44 -0700 Subject: [PATCH 25/65] Added two subject campaign object. --- addons/tests/twoCampaigns.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/addons/tests/twoCampaigns.php b/addons/tests/twoCampaigns.php index e10d0f4..5baf521 100644 --- a/addons/tests/twoCampaigns.php +++ b/addons/tests/twoCampaigns.php @@ -175,9 +175,14 @@ 'title' => 'stop the fcc', 'valid' => true, 'rate' => 0.98812276437224555, 'share' => 0.0, 'limit' => 0, + ), ), - ), 'overall' => - array ( + 'overall' => + array ( 'conversions' => 0, - 'losses' => 0, 'sent' => 5, 'boost' => 500, 'rate' => 0.99009900990099009, ), + 'losses' => 0, + 'sent' => 5, + 'boost' => 500, + 'rate' => 0.99009900990099009, + ), ); \ No newline at end of file From e6ae01d8104c9bd7882a81801939d780d9d626ca Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Mon, 23 Jul 2018 11:24:36 -0700 Subject: [PATCH 26/65] Added two subject campaign object. --- addons/tests/twoSubjectCampaign.php | 118 ++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 addons/tests/twoSubjectCampaign.php diff --git a/addons/tests/twoSubjectCampaign.php b/addons/tests/twoSubjectCampaign.php new file mode 100644 index 0000000..7eb92d4 --- /dev/null +++ b/addons/tests/twoSubjectCampaign.php @@ -0,0 +1,118 @@ + array ( + 0 => array ( + 'conversions' => 0, + 'fields' => array ( + 'subjects' => array ( + 0 => array ( + 'subject' => 'Tell Senate: No attacks on immigrants', + 'enabled' => true, + ), + 1 => array ( + 'subject' => "Block Trump's attacks on immigrants", + 'enabled' => true, + ), + ), + 'salutation' => '{{ user.first_name|default:"Hi" }},', + 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

Already he has banned Muslims and refugees, threatened sanctuary cities and criminalized millions of undocumented immigrants. His Immigration and Custom Enforcement (ICE) and Border Patrol agents are separating parents from their children, deporting innocent people and spreading fear in immigrant communities across the country. The budget he just released slashes funding for the social safety net in order to spend billions of dollars building his wall, increasing his deportation force and expediting deportations.

Senate Democrats have the power to block funding for Trump’s wall and his massive deportation force. Recent reports show they may be willing to fight to stop Trump’s hateful agenda. Can you help make sure they stand strong?

We cannot let any Senate Democrat cave to political pressure from the right-wing and give Republicans the 60 votes they need to fund Trump’s hate. That is where progressives come in. We have to show Democratic senators that we will have their backs if they act boldly and that we will hold them accountable if they fail to stand up for immigrants.

Tell Senate Democrats: Block funding for Trump’s attacks on immigrants. Click here to sign the petition.

Trump wants to harass, deport and reject people who have come or are dreaming of coming to the United States seeking refuge from conflict, lives free from persecution and better economic opportunities for their families. For as long as Trump is president and extremist right-wing Republicans are willing to enable his agenda, Senate Democrats will have a choice: enable Trump’s agenda of criminalization and deportations, or resist and obstruct his racist regime at every turn. It is up to make sure they do the right thing.

Tell Senate Democrats: Block funding for Trump’s attacks on immigrants.

', + 'petition_text' => '

“Block funding for Trump’s attacks on immigrants, including his deportation force and his border wall. “

', + 'disclaimer' => '

We do not share your email address without your permission. We may send you updates on this and other important campaigns by email. If at any time you would like to unsubscribe from our email list, you may do so.

', + 'share_titles' => array ( + 0 => array ( + 'title' => 'Senate Democrats: Block Trump’s attacks on immigrants', + 'enabled' => true, + ), + 1 => array ( + 'title' => 'Block the border wall!', + 'enabled' => true, + ), + ), + 'share_descriptions' => array ( + 0 => array ( + 'description' => "There's new legislation to block Trump's border wall -- tell the Senate to pass it.", + 'enabled' => true, ), + 1 => array ( + 'description' => "The Senate can block Trump's anti-immigrant agenda, including the border wall.", + 'enabled' => true, + ), + ), + 'share_images' => array ( + 0 => array ( + 'image' => array ( + 'ID' => 203, + 'id' => 203, + 'title' => 'Logo-square', + 'filename' => 'Logo-square.png', + 'url' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', + 'alt' => '', + 'author' => '5', + 'description' => '', + 'caption' => '', + 'name' => 'logo-square', + 'date' => '2017-03-01 04:24:22', + 'modified' => '2017-03-01 04:24:22', + 'mime_type' => 'image/png', + 'type' => 'image', + 'icon' => 'http://victorykit.local/wp-includes/images/media/default.png', + 'width' => 132, + 'height' => 125, + 'sizes' => array ( + 'thumbnail' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', + 'thumbnail-width' => 132, + 'thumbnail-height' => 125, + 'medium' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', + 'medium-width' => 132, + 'medium-height' => 125, + 'medium_large' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', + 'medium_large-width' => 132, + 'medium_large-height' => 125, + 'large' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', + 'large-width' => 132, + 'large-height' => 125, + ), + ), + 'enabled' => true, + ), + ), + 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + 'from_line' => '41', + 'landing_page_body' => '', + 'after_action_email_content' => '

Thank you for taking action. Now please share this important campaign with your friends, and ask them to join in it. This will make it much more likely we can win!

', + ), + 'id' => 263, + 'losses' => 0, + 'sent' => 0, + 'subjects' => array ( + 0 => array ( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 0, + 'title' => 'Tell Senate: No attacks on immigrants', + 'rate' => 1.0, + 'share' => 0, + ), + 1 => array ( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 0, + 'title' => "Block Trump's attacks on immigrants", + 'rate' => 1.0, + 'share' => 0.5, + ), + ), + 'title' => 'No wall testing', + 'valid' => true, + 'rate' => 1.0, + 'share' => 1.0, + 'limit' => 4.0, + ), + ), + 'overall' => array ( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 0, + 'boost' => 500.0, + 'rate' => 1.0, + ), + ); \ No newline at end of file From bf660f525d8a98df340d408970bcb6161a5dc31d Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Tue, 9 Oct 2018 13:50:03 -0700 Subject: [PATCH 27/65] Add get_distributions and wp_query_post test. --- addons/tests/mailing_test.php | 134 +++++++++++++++++++++------------- 1 file changed, 82 insertions(+), 52 deletions(-) diff --git a/addons/tests/mailing_test.php b/addons/tests/mailing_test.php index 563f794..7e25eb5 100644 --- a/addons/tests/mailing_test.php +++ b/addons/tests/mailing_test.php @@ -8,6 +8,7 @@ require_once(__DIR__. '/twoCampaigns.php'); require_once(__DIR__. '/singleCampaign.php'); require_once(__DIR__. '/twoSubjectCampaign.php'); +require_once(__DIR__. '/../mockClasses/mailingsHelpers.php'); function get_option(){ return 25; @@ -39,76 +40,105 @@ function query(){} final class mailingsClass extends TestCase { - public function test_vk_mailings_create_new_mailings_action_two_campaigns(): void { - global $TwoCampaigns; + // public function test_vk_mailings_create_new_mailings_action_two_campaigns(): void { + // global $TwoCampaigns; - $vk_mailings_mock = $this->createMock(Mailings::class); - $wpdb_mock = $this->createMock(wpdb::class); + // $vk_mailings_mock = $this->createMock(Mailings::class); + // $wpdb_mock = $this->createMock(wpdb::class); - $vk_mailings_mock->expects($this->once()) - ->method('get_distributions') - ->willReturn($TwoCampaigns); + // $vk_mailings_mock->expects($this->once()) + // ->method('get_distributions') + // ->willReturn($TwoCampaigns); - $vk_mailings_mock->expects($this->exactly(2)) - ->method('get_fresh_subscribers_for_campaign') - ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + // $vk_mailings_mock->expects($this->exactly(2)) + // ->method('get_fresh_subscribers_for_campaign') + // ->willReturn(array( 0 => 6630470, 1 => 6630472,)); - $vk_mailings_mock->expects($this->exactly(3)) - ->method('send'); + // $vk_mailings_mock->expects($this->exactly(3)) + // ->method('send'); - $wpdb_mock->expects($this->exactly(1)) - ->method('query'); + // $wpdb_mock->expects($this->exactly(1)) + // ->method('query'); - $result = vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock); - // $this->assertTrue($result === null); - } + // $result = vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock); + // // $this->assertTrue($result === null); + // } - public function test_vk_mailings_create_new_mailings_action_one_campaigns(): void { - global $singleCampaign; + // public function test_vk_mailings_create_new_mailings_action_one_campaigns(): void { + // global $singleCampaign; - $vk_mailings_mock = $this->createMock(Mailings::class); - $wpdb_mock = $this->createMock(wpdb::class); + // $vk_mailings_mock = $this->createMock(Mailings::class); + // $wpdb_mock = $this->createMock(wpdb::class); - $vk_mailings_mock->expects($this->once()) - ->method('get_distributions') - ->willReturn($singleCampaign); + // $vk_mailings_mock->expects($this->once()) + // ->method('get_distributions') + // ->willReturn($singleCampaign); - $vk_mailings_mock->expects($this->exactly(1)) - ->method('get_fresh_subscribers_for_campaign') - ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + // $vk_mailings_mock->expects($this->exactly(1)) + // ->method('get_fresh_subscribers_for_campaign') + // ->willReturn(array( 0 => 6630470, 1 => 6630472,)); - $vk_mailings_mock->expects($this->exactly(2)) - ->method('send'); + // $vk_mailings_mock->expects($this->exactly(2)) + // ->method('send'); - $wpdb_mock->expects($this->exactly(1)) - ->method('query'); + // $wpdb_mock->expects($this->exactly(1)) + // ->method('query'); - vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock); - // $this->assertFalse($result === false); - } + // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock); + // } - public function test_get_distribution_two_subject_campaign(): void { - global $twoSubjectCampaign; + // public function test_get_distribution_two_subject_campaign(): void { + // global $twoSubjectCampaign; - $vk_mailings_mock = $this->createMock(Mailings::class); - $wpdb_mock = $this->createMock(wpdb::class); + // $vk_mailings_mock = $this->createMock(Mailings::class); + // $wpdb_mock = $this->createMock(wpdb::class); - $vk_mailings_mock->expects($this->once()) - ->method('get_distributions') - ->willReturn($twoSubjectCampaign); + // $vk_mailings_mock->expects($this->once()) + // ->method('get_distributions') + // ->willReturn($twoSubjectCampaign); - $vk_mailings_mock->expects($this->exactly(1)) - ->method('get_fresh_subscribers_for_campaign') - ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + // $vk_mailings_mock->expects($this->exactly(1)) + // ->method('get_fresh_subscribers_for_campaign') + // ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + + // $vk_mailings_mock->expects($this->exactly(1)) + // ->method('send') + // ->willReturn(array ( 'ak_mailing_id' => 1)); + + // $wpdb_mock->expects($this->exactly(1)) + // ->method('query'); + + // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock); + // } + + public function test_get_distributions(): void + { + $mailingsMock = $this->createMock(Mailings::class); + $mhInstance = new MailingsHelpers(); + $wpdb = new wpdb(); + + $mailingsMock->expects($this->once()) + ->method('get_distributions'); + + $mailingsMock->get_distributions($wpdb, $mhInstance); + } - $vk_mailings_mock->expects($this->exactly(1)) - ->method('send') - ->willReturn(array ( 'ak_mailing_id' => 1)); + public function test_get_distributions_wp_query_posts_method(): void + { + $wpdb = new wpdb(); + + $postObject = new stdClass(); + $postObject->posts = array(0 => (object) ['ID' => 'Here we go']); - $wpdb_mock->expects($this->exactly(1)) - ->method('query'); + $mhMock = $this->createMock(MailingsHelpers::class); + $mhMock->expects($this->once()) + ->method('wp_query_posts') + ->willReturn($postObject); + + $mailingsInstance = new Mailings(); - vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock); - // $this->assertFalse($result === false); + $mailingsInstance->get_distributions($wpdb, $mhMock); } - } \ No newline at end of file + } + + // 'posts' => array ( 0 => WP_Post::__set_state(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), ), 'post_count' => 1, 'current_post' => -1, 'in_the_loop' => false, 'post' => WP_Post::__set_state(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), 'comment_count' => 0, 'current_comment' => -1, 'found_posts' => '1', 'max_num_pages' => 1.0, 'max_num_comment_pages' => 0, 'is_single' => false, 'is_preview' => false, 'is_page' => false, 'is_archive' => true, 'is_date' => false, 'is_year' => false, 'is_month' => false, 'is_day' => false, 'is_time' => false, 'is_author' => false, 'is_category' => false, 'is_tag' => false, 'is_tax' => false, 'is_search' => false, 'is_feed' => false, 'is_comment_feed' => false, 'is_trackback' => false, 'is_home' => false, 'is_404' => false, 'is_embed' => false, 'is_paged' => false, 'is_admin' => false, 'is_attachment' => false, 'is_singular' => false, 'is_robots' => false, 'is_posts_page' => false, 'is_post_type_archive' => true, 'query_vars_hash' => 'e36a12d927198b4f30d55d60b891715f', 'query_vars_changed' => false, 'thumbnails_cached' => false, 'stopwords' => NULL, 'compat_fields' => array ( 0 => 'query_vars_hash', 1 => 'query_vars_changed', ), 'compat_methods' => array ( 0 => 'init_query_flags', 1 => 'parse_tax_query', ), )) \ No newline at end of file From a792be4c57a2cb52f536e1b2495d04974a184ab1 Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Thu, 11 Oct 2018 14:39:46 -0700 Subject: [PATCH 28/65] Test get_fields function in MailingsHelper class. --- addons/tests/mailing_test.php | 106 ++++++++++++++++++++++++++++------ 1 file changed, 88 insertions(+), 18 deletions(-) diff --git a/addons/tests/mailing_test.php b/addons/tests/mailing_test.php index 7e25eb5..ef5e3cd 100644 --- a/addons/tests/mailing_test.php +++ b/addons/tests/mailing_test.php @@ -111,34 +111,104 @@ final class mailingsClass extends TestCase // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock); // } - public function test_get_distributions(): void - { - $mailingsMock = $this->createMock(Mailings::class); - $mhInstance = new MailingsHelpers(); - $wpdb = new wpdb(); + // public function test_mailingsHelpers_wp_query_posts_method(): void + // { + // $mailingsMock = new Mailings; + // $mhInstance = $this->createMock(MailingsHelpers::class); + // $wpdb = new wpdb(); - $mailingsMock->expects($this->once()) - ->method('get_distributions'); + // $mhInstance->expects($this->once()) + // ->method('wp_query_posts'); + + // $mailingsMock->get_distributions($wpdb, $mhInstance); + // } + + + + - $mailingsMock->get_distributions($wpdb, $mhInstance); - } - public function test_get_distributions_wp_query_posts_method(): void + + //++++++++++++TESTING IN PROGRESS BELOW++++++++ + + + public function test_get_fields(): void { $wpdb = new wpdb(); - $postObject = new stdClass(); - $postObject->posts = array(0 => (object) ['ID' => 'Here we go']); + $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - $mhMock = $this->createMock(MailingsHelpers::class); - $mhMock->expects($this->once()) - ->method('wp_query_posts') - ->willReturn($postObject); + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['wp_query_posts', 'getFields']) + ->getMock(); - $mailingsInstance = new Mailings(); + $mhMock->expects($this->once()) + ->method('wp_query_posts') + ->will($this->returnValue($postObject)); - $mailingsInstance->get_distributions($wpdb, $mhMock); + $mhMock->expects($this->once()) + ->method('getFields'); + + $subject = new Mailings(); + + $subject->get_distributions($wpdb, $mhMock); } + + // public function test_mailingsHelpers_setUpCampaigns(): void + // { + // $wpdb = new wpdb(); + // // $postObject = new stdClass(); + // // $postObject->posts = array(0 => (object) ['ID' => 'Here we go']); + + // $mhMock= $this->getMockBuilder(MailingsHelpers::class) + // ->setMethods(['wp_query_posts', 'setUpCampaigns']) + // ->getMock(); + + // $mhMock->expects($this->once()) + // ->method('wp_query_posts') + // ->will($this->returnValue($postObject)); + + // $mhMock->expects($this->once()) + // ->method('setUpCampaigns'); + + // $mailings = new Mailings(); + + // $mailings->get_distributions($wpdb, $mhMock); + // } + + // public function test_mailingsHelpers_wp_query_posts_method(): void + // { + // $wpdb = new wpdb(); + + // $postObject = new stdClass(); + // $postObject->posts = array(0 => (object) ['ID' => 'Here we go']); + + // $mhMock = new MailingsHelpers(); + // $mhMock->expects($this->once()) + // ->method('wp_query_posts') + // ->willReturn($postObject); + + // $mailingsInstance = new Mailings(); + + // $mailingsInstance->get_distributions($wpdb, $mhMock); + // } + + // public function test_mailingsHelpers_setUpCampaign_method(): void + // { + // $wpdb = new wpdb(); + + // // $postObject = new stdClass(); + // // $postObject->posts = array(0 => (object) ['ID' => 'Here we go']); + + // $mhMock = $this->createMock(MailingsHelpers::class); + // $mhMock->expects($this->once()) + // ->method('setUpCampaigns'); + // // ->willReturn($postObject); + + // $mailingsInstance = new Mailings(); + + // $mailingsInstance->get_distributions($wpdb, $mhMock); + // } } // 'posts' => array ( 0 => WP_Post::__set_state(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), ), 'post_count' => 1, 'current_post' => -1, 'in_the_loop' => false, 'post' => WP_Post::__set_state(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), 'comment_count' => 0, 'current_comment' => -1, 'found_posts' => '1', 'max_num_pages' => 1.0, 'max_num_comment_pages' => 0, 'is_single' => false, 'is_preview' => false, 'is_page' => false, 'is_archive' => true, 'is_date' => false, 'is_year' => false, 'is_month' => false, 'is_day' => false, 'is_time' => false, 'is_author' => false, 'is_category' => false, 'is_tag' => false, 'is_tax' => false, 'is_search' => false, 'is_feed' => false, 'is_comment_feed' => false, 'is_trackback' => false, 'is_home' => false, 'is_404' => false, 'is_embed' => false, 'is_paged' => false, 'is_admin' => false, 'is_attachment' => false, 'is_singular' => false, 'is_robots' => false, 'is_posts_page' => false, 'is_post_type_archive' => true, 'query_vars_hash' => 'e36a12d927198b4f30d55d60b891715f', 'query_vars_changed' => false, 'thumbnails_cached' => false, 'stopwords' => NULL, 'compat_fields' => array ( 0 => 'query_vars_hash', 1 => 'query_vars_changed', ), 'compat_methods' => array ( 0 => 'init_query_flags', 1 => 'parse_tax_query', ), )) \ No newline at end of file From 354be57eb2da86286f5e25ee7cfc506bf2088c8b Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Thu, 11 Oct 2018 14:41:03 -0700 Subject: [PATCH 29/65] Add setUpCampaigns into MailingsHelper class. --- addons/mailings.php | 79 +++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 43 deletions(-) diff --git a/addons/mailings.php b/addons/mailings.php index 4b12eb3..0cedb61 100644 --- a/addons/mailings.php +++ b/addons/mailings.php @@ -3,6 +3,7 @@ // Exit if accessed directly if(!defined('ABSPATH')) exit; require_once(__DIR__. '/../constants.php'); +require_once(__DIR__. '/mockClasses/mailingsHelpers.php'); // Display errors ini_set('display_errors', 1); ini_set('display_startup_errors', 1); @@ -14,9 +15,15 @@ function __construct() // ... } - function get_distributions() + function get_distributions($wpdb_mock, $mh_mock) { global $wpdb; + global $mh; + print('in on line 22 get_distributions@@@@ '); + if($wpdb_mock){ + $wpdb = $wpdb_mock; + $mh = $mh_mock; + } if (!get_option('subscribed_users')) { // no subscribed users in DB yet @@ -25,34 +32,14 @@ function get_distributions() // Get active campaigns $campaigns = array(); - $results = new WP_Query(array( - 'post_type' => 'campaign', - 'post_status' => 'publish', - )); - foreach ($results->posts as $campaign) { - $id = $campaign->ID; - $campaigns[$id] = array( - 'conversions' => 0, - 'fields' => get_fields($id), - 'id' => $id, - 'losses' => 0, - 'sent' => 0, - 'subjects' => array(), - 'title' => $campaign->post_title, - 'valid' => true, - ); + $results = $mh->wp_query_posts(); + + // $results1 = trim(preg_replace('/\s+/', ' ',var_export( $results, true))); + // var_dump($results1); - // Get all subjects - $subjects = $campaigns[$id]['fields']['subjects']; - for ($i = 0; $i < count($subjects); $i++) { - $campaigns[$id]['subjects'][$i] = array( - 'conversions' => 0, - 'losses' => 0, - 'sent' => 0, - ); - } - } + // var_dump($mh); + $campaigns = $mh->setUpCampaigns($results, $campaigns, $mh_mock); // Get campaign performance $overall = array( @@ -60,20 +47,13 @@ function get_distributions() 'losses' => 0, 'sent' => 0, ); - $mailings = $wpdb->get_results(' - SELECT - vkm.campaign_id, - vkm.variation_subject, - SUM(vkm.conversions) as conversions, - SUM(vkm.losses) as losses, - SUM(vkm.sent) as sent - FROM - vk_mailing AS vkm - GROUP BY - vkm.campaign_id, vkm.variation_subject - ', ARRAY_A); + $mailings = $mh->get_mailings_results_wpdb($wpdb); + //need to print error log here + $mailingsPrint = trim(preg_replace('/\s+/', ' ',var_export( $mailings, true))); + error_log('line 54 return value of get_results '.$mailingsPrint); + foreach ($mailings as $mailing) { $id = $mailing['campaign_id']; @@ -122,7 +102,6 @@ function get_distributions() // Calculate shares $campaign_rate_sum = 0; - foreach ($campaigns as $campaign_index => &$campaign) { // Subjects $fields = $campaign['fields']; @@ -187,6 +166,9 @@ function get_distributions() return $campaign['valid']; }); + $campaigns1 = trim(preg_replace('/\s+/', ' ',var_export( $campaigns, true))); + error_log('line 197 campaigns after array_filter function '.$campaigns1); + // Get share percentages foreach ($campaigns as $campaign_index => &$campaign) { $share = $campaign_rate_sum ? $campaign['rate'] / $campaign_rate_sum : 0; @@ -197,13 +179,18 @@ function get_distributions() $campaign_share_sum = 1; $limit_per_day = get_option('subscribed_users') / 7; + $limit_per_day1 = trim(preg_replace('/\s+/', ' ',var_export( $limit_per_day, true))); + error_log('line 210 return value of get_options function'.$limit_per_day1); + foreach ($campaigns as $campaign_index => &$campaign) { $limit_per_campaign = round($campaign['share'] * $limit_per_day); // $limit = trim(preg_replace('/\s+/', ' ',var_export($limit_per_campaign, true))); // error_log('line 192 limit_per_campaign '.$limit); $fresh_ids = count($this->get_fresh_subscribers_for_campaign($campaign['id'], $limit_per_campaign)); - // $fresh = trim(preg_replace('/\s+/', ' ',var_export($fresh_ids, true))); - // error_log('line 195 fresh ids '.$fresh); + + $fresh_ids1 = trim(preg_replace('/\s+/', ' ',var_export( $fresh_ids, true))); + error_log('line 222 return value of get_fresh_subscribers_for_campaign function'.$fresh_ids1); + // Plenty? Great. //fresh ids should not be 0 if ($fresh_ids >= $limit_per_campaign) { @@ -240,6 +227,12 @@ function get_distributions() } }); + $campaigns1 = trim(preg_replace('/\s+/', ' ',var_export( $campaigns, true))); + error_log('line 262 $campaigns'.$campaigns1); + + $overall1 = trim(preg_replace('/\s+/', ' ',var_export( $overall, true))); + error_log('line 264 $overall'.$overall1); + return array( 'campaigns' => $campaigns, 'overall' => $overall, @@ -521,7 +514,7 @@ function vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock) { $limit_per_day = get_option('subscribed_users') / 7; - $distributions = $vk_mailings->get_distributions(); + $distributions = $vk_mailings->get_distributions(0, 0); // $distributions_results = trim(preg_replace('/\s+/', ' ',var_export( $distributions, true))); // $distributions_results = sizeof($distributions_results); // error_log('@@@ limit_per_day variable '.$distributions_results); From cf44fa87fe36cb9bf818b16d181ecc810e0d33c4 Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Thu, 11 Oct 2018 14:42:01 -0700 Subject: [PATCH 30/65] Passing arguments to get_distribution function. --- addons/analytics.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/analytics.php b/addons/analytics.php index aff3017..2b6d617 100644 --- a/addons/analytics.php +++ b/addons/analytics.php @@ -21,7 +21,7 @@ function vk_analytics_admin_page_render() { global $vk_mailings; // Calculate distributions - $distributions = $vk_mailings->get_distributions(); + $distributions = $vk_mailings->get_distributions(0, 0); foreach ($distributions['campaigns'] as &$campaign) { unset($campaign['fields']); } From 08b0c0ae92b88d1b24f5bb565da45de00a834edb Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Thu, 11 Oct 2018 14:42:06 -0700 Subject: [PATCH 31/65] Passing arguments to get_distribution function. --- addons/distributions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/distributions.php b/addons/distributions.php index 9a403e8..aa61ae3 100644 --- a/addons/distributions.php +++ b/addons/distributions.php @@ -31,7 +31,7 @@ function vk_distributions_admin_page_render() { ); // Calculate distributions - $distributions = $vk_mailings->get_distributions(); + $distributions = $vk_mailings->get_distributions(0, 0); foreach ($distributions['campaigns'] as &$campaign) { unset($campaign['fields']); } From 361bd6b9242b0740453db7253ba32f4df848a6fa Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Thu, 11 Oct 2018 14:42:53 -0700 Subject: [PATCH 32/65] Add MailingsHelpers class. --- addons/mockClasses/mailingsHelpers.php | 109 +++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 addons/mockClasses/mailingsHelpers.php diff --git a/addons/mockClasses/mailingsHelpers.php b/addons/mockClasses/mailingsHelpers.php new file mode 100644 index 0000000..e0baddf --- /dev/null +++ b/addons/mockClasses/mailingsHelpers.php @@ -0,0 +1,109 @@ + 'campaign', + 'post_status' => 'publish', + )); + } + + function getFields($id, $mh_mock){ + if($mh_mock){ + print('in side get fields'); + } else { + return get_fields($id); + } + } + + function setUpCampaigns($results, $campaigns, $mh_mock){ + print('inside setUpCampaigns func'); + $campaigns = $campaigns; + foreach ($results->posts as $campaign) { + $id = $campaign->ID; + $campaigns[$id] = array( + 'conversions' => 0, + 'fields' => $this->getFields($id, $mh_mock), + 'id' => $id, + 'losses' => 0, + 'sent' => 0, + 'subjects' => array(), + 'title' => $campaign->post_title, + 'valid' => true, + ); + + // Get all subjects + $subjects = $campaigns[$id]['fields']['subjects']; + for ($i = 0; $i < count($subjects); $i++) { + $campaigns[$id]['subjects'][$i] = array( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 0, + ); + } + } + return $campaigns; + } + + function get_mailings_results_wpdb($db){ + print('inside get mailings result wpdb'); + return $db->get_results(' + SELECT + vkm.campaign_id, + vkm.variation_subject, + SUM(vkm.conversions) as conversions, + SUM(vkm.losses) as losses, + SUM(vkm.sent) as sent + FROM + vk_mailing AS vkm + GROUP BY + vkm.campaign_id, vkm.variation_subject + ', ARRAY_A); + } +// function mailingStats($mailings, $campaigns, $overall){ +// $results = array(); + +// foreach ($mailings as $mailing) { +// $id = $mailing['campaign_id']; + +// // Make sure to only include currently published campaigns in overall data +// // TODO: why not just check for campaign published in query above? +// if (!isset($campaigns[$id])) { +// continue; +// } + +// $overall['conversions'] += $mailing['conversions']; +// $overall['losses'] += $mailing['losses']; +// $overall['sent'] += $mailing['sent']; + +// $campaigns[$id]['conversions'] += $mailing['conversions']; +// $campaigns[$id]['losses'] += $mailing['losses']; +// $campaigns[$id]['sent'] += $mailing['sent']; + +// $subject = $mailing['variation_subject']; +// $campaigns[$id]['subjects'][$subject] = array( +// 'conversions' => +$mailing['conversions'], +// 'losses' => +$mailing['losses'], +// 'sent' => +$mailing['sent'], +// ); +// } +// $results['campaign_result'] = $campaigns; +// $results['overall_result'] = $overall; +// return $results; +// } + + +} + +function mh() { + global $mh; + + if(!isset($mh)) { + $mh = new mailingsHelpers(); + } + + return $mh; +} + +// Initialize +mh(); \ No newline at end of file From 6599842852eb7c78a7df3e59dc9e9ed0561efe79 Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Fri, 12 Oct 2018 10:43:09 -0700 Subject: [PATCH 33/65] Add setUpCampaigns test. --- addons/tests/mailing_test.php | 87 +++++++++-------------------------- 1 file changed, 22 insertions(+), 65 deletions(-) diff --git a/addons/tests/mailing_test.php b/addons/tests/mailing_test.php index ef5e3cd..1212b20 100644 --- a/addons/tests/mailing_test.php +++ b/addons/tests/mailing_test.php @@ -123,23 +123,36 @@ final class mailingsClass extends TestCase // $mailingsMock->get_distributions($wpdb, $mhInstance); // } + // public function test_get_fields(): void + // { + // $wpdb = new wpdb(); + // $postObject = new stdClass(); + // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + // $mhMock= $this->getMockBuilder(MailingsHelpers::class) + // ->setMethods(['wp_query_posts', 'getFields']) + // ->getMock(); + + // $mhMock->expects($this->once()) + // ->method('wp_query_posts') + // ->will($this->returnValue($postObject)); + // $mhMock->expects($this->once()) + // ->method('getFields'); + + // $subject = new Mailings(); + + // $subject->get_distributions($wpdb, $mhMock); + // } - - - - //++++++++++++TESTING IN PROGRESS BELOW++++++++ - - - public function test_get_fields(): void + public function test_mailingsHelpers_setUpCampaigns(): void { $wpdb = new wpdb(); $postObject = new stdClass(); $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); $mhMock= $this->getMockBuilder(MailingsHelpers::class) - ->setMethods(['wp_query_posts', 'getFields']) + ->setMethods(['wp_query_posts', 'setUpCampaigns']) ->getMock(); $mhMock->expects($this->once()) @@ -147,68 +160,12 @@ public function test_get_fields(): void ->will($this->returnValue($postObject)); $mhMock->expects($this->once()) - ->method('getFields'); + ->method('setUpCampaigns'); $subject = new Mailings(); $subject->get_distributions($wpdb, $mhMock); } - - // public function test_mailingsHelpers_setUpCampaigns(): void - // { - // $wpdb = new wpdb(); - // // $postObject = new stdClass(); - // // $postObject->posts = array(0 => (object) ['ID' => 'Here we go']); - - // $mhMock= $this->getMockBuilder(MailingsHelpers::class) - // ->setMethods(['wp_query_posts', 'setUpCampaigns']) - // ->getMock(); - - // $mhMock->expects($this->once()) - // ->method('wp_query_posts') - // ->will($this->returnValue($postObject)); - - // $mhMock->expects($this->once()) - // ->method('setUpCampaigns'); - - // $mailings = new Mailings(); - - // $mailings->get_distributions($wpdb, $mhMock); - // } - - // public function test_mailingsHelpers_wp_query_posts_method(): void - // { - // $wpdb = new wpdb(); - - // $postObject = new stdClass(); - // $postObject->posts = array(0 => (object) ['ID' => 'Here we go']); - - // $mhMock = new MailingsHelpers(); - // $mhMock->expects($this->once()) - // ->method('wp_query_posts') - // ->willReturn($postObject); - - // $mailingsInstance = new Mailings(); - - // $mailingsInstance->get_distributions($wpdb, $mhMock); - // } - - // public function test_mailingsHelpers_setUpCampaign_method(): void - // { - // $wpdb = new wpdb(); - - // // $postObject = new stdClass(); - // // $postObject->posts = array(0 => (object) ['ID' => 'Here we go']); - - // $mhMock = $this->createMock(MailingsHelpers::class); - // $mhMock->expects($this->once()) - // ->method('setUpCampaigns'); - // // ->willReturn($postObject); - - // $mailingsInstance = new Mailings(); - - // $mailingsInstance->get_distributions($wpdb, $mhMock); - // } } // 'posts' => array ( 0 => WP_Post::__set_state(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), ), 'post_count' => 1, 'current_post' => -1, 'in_the_loop' => false, 'post' => WP_Post::__set_state(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), 'comment_count' => 0, 'current_comment' => -1, 'found_posts' => '1', 'max_num_pages' => 1.0, 'max_num_comment_pages' => 0, 'is_single' => false, 'is_preview' => false, 'is_page' => false, 'is_archive' => true, 'is_date' => false, 'is_year' => false, 'is_month' => false, 'is_day' => false, 'is_time' => false, 'is_author' => false, 'is_category' => false, 'is_tag' => false, 'is_tax' => false, 'is_search' => false, 'is_feed' => false, 'is_comment_feed' => false, 'is_trackback' => false, 'is_home' => false, 'is_404' => false, 'is_embed' => false, 'is_paged' => false, 'is_admin' => false, 'is_attachment' => false, 'is_singular' => false, 'is_robots' => false, 'is_posts_page' => false, 'is_post_type_archive' => true, 'query_vars_hash' => 'e36a12d927198b4f30d55d60b891715f', 'query_vars_changed' => false, 'thumbnails_cached' => false, 'stopwords' => NULL, 'compat_fields' => array ( 0 => 'query_vars_hash', 1 => 'query_vars_changed', ), 'compat_methods' => array ( 0 => 'init_query_flags', 1 => 'parse_tax_query', ), )) \ No newline at end of file From b0874b05e205ce6ce4cdf3b89a4e5770d10fa4ea Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Fri, 12 Oct 2018 11:39:10 -0700 Subject: [PATCH 34/65] Update get_mailings_results and setupcampaigns test, all passing. --- addons/tests/mailing_test.php | 188 ++++++++++++++++++++-------------- 1 file changed, 109 insertions(+), 79 deletions(-) diff --git a/addons/tests/mailing_test.php b/addons/tests/mailing_test.php index 1212b20..721e7b0 100644 --- a/addons/tests/mailing_test.php +++ b/addons/tests/mailing_test.php @@ -40,116 +40,126 @@ function query(){} final class mailingsClass extends TestCase { - // public function test_vk_mailings_create_new_mailings_action_two_campaigns(): void { - // global $TwoCampaigns; + public function test_vk_mailings_create_new_mailings_action_two_campaigns(): void + { + global $TwoCampaigns; - // $vk_mailings_mock = $this->createMock(Mailings::class); - // $wpdb_mock = $this->createMock(wpdb::class); + $vk_mailings_mock = $this->createMock(Mailings::class); + $wpdb_mock = $this->createMock(wpdb::class); - // $vk_mailings_mock->expects($this->once()) - // ->method('get_distributions') - // ->willReturn($TwoCampaigns); + $vk_mailings_mock->expects($this->once()) + ->method('get_distributions') + ->willReturn($TwoCampaigns); - // $vk_mailings_mock->expects($this->exactly(2)) - // ->method('get_fresh_subscribers_for_campaign') - // ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + $vk_mailings_mock->expects($this->exactly(2)) + ->method('get_fresh_subscribers_for_campaign') + ->willReturn(array( 0 => 6630470, 1 => 6630472,)); - // $vk_mailings_mock->expects($this->exactly(3)) - // ->method('send'); + $vk_mailings_mock->expects($this->exactly(3)) + ->method('send'); - // $wpdb_mock->expects($this->exactly(1)) - // ->method('query'); + $wpdb_mock->expects($this->exactly(1)) + ->method('query'); - // $result = vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock); - // // $this->assertTrue($result === null); - // } + $result = vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock); + // $this->assertTrue($result === null); + } - // public function test_vk_mailings_create_new_mailings_action_one_campaigns(): void { - // global $singleCampaign; + public function test_vk_mailings_create_new_mailings_action_one_campaigns(): void + { + global $singleCampaign; - // $vk_mailings_mock = $this->createMock(Mailings::class); - // $wpdb_mock = $this->createMock(wpdb::class); + $vk_mailings_mock = $this->createMock(Mailings::class); + $wpdb_mock = $this->createMock(wpdb::class); - // $vk_mailings_mock->expects($this->once()) - // ->method('get_distributions') - // ->willReturn($singleCampaign); + $vk_mailings_mock->expects($this->once()) + ->method('get_distributions') + ->willReturn($singleCampaign); - // $vk_mailings_mock->expects($this->exactly(1)) - // ->method('get_fresh_subscribers_for_campaign') - // ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + $vk_mailings_mock->expects($this->exactly(1)) + ->method('get_fresh_subscribers_for_campaign') + ->willReturn(array( 0 => 6630470, 1 => 6630472,)); - // $vk_mailings_mock->expects($this->exactly(2)) - // ->method('send'); + $vk_mailings_mock->expects($this->exactly(2)) + ->method('send'); - // $wpdb_mock->expects($this->exactly(1)) - // ->method('query'); + $wpdb_mock->expects($this->exactly(1)) + ->method('query'); - // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock); - // } + vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock); + } - // public function test_get_distribution_two_subject_campaign(): void { - // global $twoSubjectCampaign; + public function test_get_distribution_two_subject_campaign(): void + { + global $twoSubjectCampaign; - // $vk_mailings_mock = $this->createMock(Mailings::class); - // $wpdb_mock = $this->createMock(wpdb::class); + $vk_mailings_mock = $this->createMock(Mailings::class); + $wpdb_mock = $this->createMock(wpdb::class); - // $vk_mailings_mock->expects($this->once()) - // ->method('get_distributions') - // ->willReturn($twoSubjectCampaign); + $vk_mailings_mock->expects($this->once()) + ->method('get_distributions') + ->willReturn($twoSubjectCampaign); - // $vk_mailings_mock->expects($this->exactly(1)) - // ->method('get_fresh_subscribers_for_campaign') - // ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + $vk_mailings_mock->expects($this->exactly(1)) + ->method('get_fresh_subscribers_for_campaign') + ->willReturn(array( 0 => 6630470, 1 => 6630472,)); - // $vk_mailings_mock->expects($this->exactly(1)) - // ->method('send') - // ->willReturn(array ( 'ak_mailing_id' => 1)); + $vk_mailings_mock->expects($this->exactly(1)) + ->method('send') + ->willReturn(array ( 'ak_mailing_id' => 1)); - // $wpdb_mock->expects($this->exactly(1)) - // ->method('query'); + $wpdb_mock->expects($this->exactly(1)) + ->method('query'); - // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock); - // } + vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock); + } - // public function test_mailingsHelpers_wp_query_posts_method(): void - // { - // $mailingsMock = new Mailings; - // $mhInstance = $this->createMock(MailingsHelpers::class); - // $wpdb = new wpdb(); - - // $mhInstance->expects($this->once()) - // ->method('wp_query_posts'); + public function test_mailingsHelpers_wp_query_posts_method(): void + { + $mailingsMock = new Mailings(); + $wpdb = new wpdb(); + $postObject = new stdClass(); + $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - // $mailingsMock->get_distributions($wpdb, $mhInstance); - // } + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['wp_query_posts']) + ->getMock(); - // public function test_get_fields(): void - // { - // $wpdb = new wpdb(); - // $postObject = new stdClass(); - // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + $mhMock->expects($this->once()) + ->method('wp_query_posts') + ->will($this->returnValue($postObject)); - // $mhMock= $this->getMockBuilder(MailingsHelpers::class) - // ->setMethods(['wp_query_posts', 'getFields']) - // ->getMock(); + $mailingsMock->get_distributions($wpdb, $mhMock); + } + + public function test_get_fields(): void + { + $wpdb = new wpdb(); + $postObject = new stdClass(); + $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['wp_query_posts', 'getFields']) + ->getMock(); - // $mhMock->expects($this->once()) - // ->method('wp_query_posts') - // ->will($this->returnValue($postObject)); + $mhMock->expects($this->once()) + ->method('wp_query_posts') + ->will($this->returnValue($postObject)); - // $mhMock->expects($this->once()) - // ->method('getFields'); + $mhMock->expects($this->once()) + ->method('getFields'); - // $subject = new Mailings(); + $subject = new Mailings(); - // $subject->get_distributions($wpdb, $mhMock); - // } + $subject->get_distributions($wpdb, $mhMock); + } public function test_mailingsHelpers_setUpCampaigns(): void { $wpdb = new wpdb(); - $postObject = new stdClass(); - $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + // $postObject = new stdClass(); + $postObject = array(0 => (object) ['ID' => '', 'post_title'=> '']); + $campaigns = array( 0 => array( 'fields'=> '', 'campaign_id' => '', 'subjects' => array())); $mhMock= $this->getMockBuilder(MailingsHelpers::class) ->setMethods(['wp_query_posts', 'setUpCampaigns']) @@ -160,7 +170,27 @@ public function test_mailingsHelpers_setUpCampaigns(): void ->will($this->returnValue($postObject)); $mhMock->expects($this->once()) - ->method('setUpCampaigns'); + ->method('setUpCampaigns') + ->will($this->returnValue($campaigns)); + + $subject = new Mailings(); + + $subject->get_distributions($wpdb, $mhMock); + } + + public function test_mailingsHelpers_get_mailings_results_wpdb(): void + { + $wpdb = new wpdb(); + // $postObject = new stdClass(); + $postObject = array( 0 => array( 'campaign_id' => '1')); + + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['get_mailings_results_wpdb']) + ->getMock(); + + $mhMock->expects($this->once()) + ->method('get_mailings_results_wpdb') + ->will($this->returnValue($postObject)); $subject = new Mailings(); From 390be245e9939370bab6ee3f938dd901c85000de Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Tue, 16 Oct 2018 15:13:21 -0700 Subject: [PATCH 35/65] testing test_mailings_distribution_post_method in progress. --- addons/tests/mailing_test.php | 290 +++++++++++++++++++--------------- 1 file changed, 161 insertions(+), 129 deletions(-) diff --git a/addons/tests/mailing_test.php b/addons/tests/mailing_test.php index 721e7b0..8434810 100644 --- a/addons/tests/mailing_test.php +++ b/addons/tests/mailing_test.php @@ -28,143 +28,194 @@ function get_col(){return array();} function query(){} }; -// $wpdb = new wpdb; - -// class ak { - -// }; - -// $ak = new ak; - use PHPUnit\Framework\TestCase; final class mailingsClass extends TestCase { - public function test_vk_mailings_create_new_mailings_action_two_campaigns(): void - { - global $TwoCampaigns; + // public function test_vk_mailings_create_new_mailings_action_two_campaigns(): void + // { + // global $TwoCampaigns; - $vk_mailings_mock = $this->createMock(Mailings::class); - $wpdb_mock = $this->createMock(wpdb::class); + // $vk_mailings_mock = $this->createMock(Mailings::class); + // $wpdb_mock = $this->createMock(wpdb::class); - $vk_mailings_mock->expects($this->once()) - ->method('get_distributions') - ->willReturn($TwoCampaigns); + // $vk_mailings_mock->expects($this->once()) + // ->method('get_distributions') + // ->willReturn($TwoCampaigns); - $vk_mailings_mock->expects($this->exactly(2)) - ->method('get_fresh_subscribers_for_campaign') - ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + // $vk_mailings_mock->expects($this->exactly(2)) + // ->method('get_fresh_subscribers_for_campaign') + // ->willReturn(array( 0 => 6630470, 1 => 6630472,)); - $vk_mailings_mock->expects($this->exactly(3)) - ->method('send'); + // $vk_mailings_mock->expects($this->exactly(3)) + // ->method('send'); - $wpdb_mock->expects($this->exactly(1)) - ->method('query'); + // $wpdb_mock->expects($this->exactly(1)) + // ->method('query'); - $result = vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock); - // $this->assertTrue($result === null); - } + // $result = vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock); + // // $this->assertTrue($result === null); + // } - public function test_vk_mailings_create_new_mailings_action_one_campaigns(): void - { - global $singleCampaign; + // public function test_vk_mailings_create_new_mailings_action_one_campaigns(): void + // { + // global $singleCampaign; - $vk_mailings_mock = $this->createMock(Mailings::class); - $wpdb_mock = $this->createMock(wpdb::class); + // $vk_mailings_mock = $this->createMock(Mailings::class); + // $wpdb_mock = $this->createMock(wpdb::class); - $vk_mailings_mock->expects($this->once()) - ->method('get_distributions') - ->willReturn($singleCampaign); + // $vk_mailings_mock->expects($this->once()) + // ->method('get_distributions') + // ->willReturn($singleCampaign); - $vk_mailings_mock->expects($this->exactly(1)) - ->method('get_fresh_subscribers_for_campaign') - ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + // $vk_mailings_mock->expects($this->exactly(1)) + // ->method('get_fresh_subscribers_for_campaign') + // ->willReturn(array( 0 => 6630470, 1 => 6630472,)); - $vk_mailings_mock->expects($this->exactly(2)) - ->method('send'); + // $vk_mailings_mock->expects($this->exactly(2)) + // ->method('send'); - $wpdb_mock->expects($this->exactly(1)) - ->method('query'); + // $wpdb_mock->expects($this->exactly(1)) + // ->method('query'); - vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock); - } + // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock); + // } - public function test_get_distribution_two_subject_campaign(): void - { - global $twoSubjectCampaign; + // public function test_get_distribution_two_subject_campaign(): void + // { + // global $twoSubjectCampaign; - $vk_mailings_mock = $this->createMock(Mailings::class); - $wpdb_mock = $this->createMock(wpdb::class); + // $vk_mailings_mock = $this->createMock(Mailings::class); + // $wpdb_mock = $this->createMock(wpdb::class); - $vk_mailings_mock->expects($this->once()) - ->method('get_distributions') - ->willReturn($twoSubjectCampaign); + // $vk_mailings_mock->expects($this->once()) + // ->method('get_distributions') + // ->willReturn($twoSubjectCampaign); - $vk_mailings_mock->expects($this->exactly(1)) - ->method('get_fresh_subscribers_for_campaign') - ->willReturn(array( 0 => 6630470, 1 => 6630472,)); - - $vk_mailings_mock->expects($this->exactly(1)) - ->method('send') - ->willReturn(array ( 'ak_mailing_id' => 1)); - - $wpdb_mock->expects($this->exactly(1)) - ->method('query'); - - vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock); - } - - public function test_mailingsHelpers_wp_query_posts_method(): void - { - $mailingsMock = new Mailings(); - $wpdb = new wpdb(); - $postObject = new stdClass(); - $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - - $mhMock= $this->getMockBuilder(MailingsHelpers::class) - ->setMethods(['wp_query_posts']) - ->getMock(); - - $mhMock->expects($this->once()) - ->method('wp_query_posts') - ->will($this->returnValue($postObject)); + // $vk_mailings_mock->expects($this->once()) + // ->method('get_fresh_subscribers_for_campaign') + // ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + + // $vk_mailings_mock->expects($this->once()) + // ->method('send') + // ->willReturn(array ( 'ak_mailing_id' => 1)); + + // $wpdb_mock->expects($this->once()) + // ->method('query'); + + // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock); + // } + + // public function test_mailingsHelpers_wp_query_posts_method(): void + // { + // $mailingsMock = new Mailings(); + // $wpdb = new wpdb(); + // $postObject = new stdClass(); + // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + + // $mhMock= $this->getMockBuilder(MailingsHelpers::class) + // ->setMethods(['wp_query_posts']) + // ->getMock(); + + // $mhMock->expects($this->once()) + // ->method('wp_query_posts') + // ->will($this->returnValue($postObject)); + + // $mailingsMock->get_distributions($wpdb, $mhMock); + // } + + // public function test_get_fields(): void + // { + // $wpdb = new wpdb(); + // $postObject = new stdClass(); + // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + + // $mhMock= $this->getMockBuilder(MailingsHelpers::class) + // ->setMethods(['wp_query_posts', 'getFields']) + // ->getMock(); + + // $mhMock->expects($this->once()) + // ->method('wp_query_posts') + // ->will($this->returnValue($postObject)); - $mailingsMock->get_distributions($wpdb, $mhMock); - } + // $mhMock->expects($this->once()) + // ->method('getFields'); + + // $subject = new Mailings(); + + // $subject->get_distributions($wpdb, $mhMock); + // } + + // public function test_mailingsHelpers_setUpCampaigns(): void + // { + // $wpdb = new wpdb(); + // // $postObject = new stdClass(); + // $postObject = array(0 => (object) ['ID' => '', 'post_title'=> '']); + // $campaigns = array( 0 => array( 'fields'=> '', 'campaign_id' => '', 'subjects' => array())); + + // $mhMock= $this->getMockBuilder(MailingsHelpers::class) + // ->setMethods(['wp_query_posts', 'setUpCampaigns']) + // ->getMock(); + + // $mhMock->expects($this->once()) + // ->method('wp_query_posts') + // ->will($this->returnValue($postObject)); - public function test_get_fields(): void - { - $wpdb = new wpdb(); - $postObject = new stdClass(); - $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + // $mhMock->expects($this->once()) + // ->method('setUpCampaigns') + // ->will($this->returnValue($campaigns)); - $mhMock= $this->getMockBuilder(MailingsHelpers::class) - ->setMethods(['wp_query_posts', 'getFields']) - ->getMock(); + // $subject = new Mailings(); + + // $subject->get_distributions($wpdb, $mhMock); + // } + + // public function test_mailingsHelpers_get_mailings_results_wpdb(): void + // { + // $wpdb = new wpdb(); + // // $postObject = new stdClass(); + // $postObject = array( 0 => array( 'campaign_id' => '1')); + + // $mhMock= $this->getMockBuilder(MailingsHelpers::class) + // ->setMethods(['get_mailings_results_wpdb']) + // ->getMock(); - $mhMock->expects($this->once()) - ->method('wp_query_posts') - ->will($this->returnValue($postObject)); - - $mhMock->expects($this->once()) - ->method('getFields'); + // $mhMock->expects($this->once()) + // ->method('get_mailings_results_wpdb') + // ->will($this->returnValue($postObject)); - $subject = new Mailings(); + // $subject = new Mailings(); - $subject->get_distributions($wpdb, $mhMock); - } + // $subject->get_distributions($wpdb, $mhMock); + // } - public function test_mailingsHelpers_setUpCampaigns(): void + public function test_mailings_distribution_post_method(): void { + $mailingsMock = new Mailings(); $wpdb = new wpdb(); - // $postObject = new stdClass(); - $postObject = array(0 => (object) ['ID' => '', 'post_title'=> '']); - $campaigns = array( 0 => array( 'fields'=> '', 'campaign_id' => '', 'subjects' => array())); - + $postObject = new stdClass(); + $postObject->posts = array ( 0 => (object)(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), ); + + $campaigns = array( + 263 => array( + "conversions" => 0, + "fields" => array( + "subjects" => array() + ), + "id" => 263, + "losses" => 0, + "sent" => 0, + "subjects" => array(), + "title" => "No wall testing", + "valid" => true + ) + ); + // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + $mhMock= $this->getMockBuilder(MailingsHelpers::class) - ->setMethods(['wp_query_posts', 'setUpCampaigns']) - ->getMock(); - + ->setMethods(['wp_query_posts', 'setUpCampaigns']) + ->getMock(); + $mhMock->expects($this->once()) ->method('wp_query_posts') ->will($this->returnValue($postObject)); @@ -172,30 +223,11 @@ public function test_mailingsHelpers_setUpCampaigns(): void $mhMock->expects($this->once()) ->method('setUpCampaigns') ->will($this->returnValue($campaigns)); - - $subject = new Mailings(); - - $subject->get_distributions($wpdb, $mhMock); - } - - public function test_mailingsHelpers_get_mailings_results_wpdb(): void - { - $wpdb = new wpdb(); - // $postObject = new stdClass(); - $postObject = array( 0 => array( 'campaign_id' => '1')); - - $mhMock= $this->getMockBuilder(MailingsHelpers::class) - ->setMethods(['get_mailings_results_wpdb']) - ->getMock(); - - $mhMock->expects($this->once()) - ->method('get_mailings_results_wpdb') - ->will($this->returnValue($postObject)); - - $subject = new Mailings(); - - $subject->get_distributions($wpdb, $mhMock); + + $mailingsMock->get_distributions($wpdb, $mhMock); } } + // $posts = array( 'query' => array ( 'post_type' => 'campaign', 'post_status' => 'publish', ), 'query_vars' => array ( 'post_type' => 'campaign', 'post_status' => 'publish', 'error' => '', 'm' => '', 'p' => 0, 'post_parent' => '', 'subpost' => '', 'subpost_id' => '', 'attachment' => '', 'attachment_id' => 0, 'name' => '', 'static' => '', 'pagename' => '', 'page_id' => 0, 'second' => '', 'minute' => '', 'hour' => '', 'day' => 0, 'monthnum' => 0, 'year' => 0, 'w' => 0, 'category_name' => '', 'tag' => '', 'cat' => '', 'tag_id' => '', 'author' => '', 'author_name' => '', 'feed' => '', 'tb' => '', 'paged' => 0, 'meta_key' => '', 'meta_value' => '', 'preview' => '', 's' => '', 'sentence' => '', 'title' => '', 'fields' => '', 'menu_order' => '', 'embed' => '', 'category__in' => array ( ), 'category__not_in' => array ( ), 'category__and' => array ( ), 'post__in' => array ( ), 'post__not_in' => array ( ), 'post_name__in' => array ( ), 'tag__in' => array ( ), 'tag__not_in' => array ( ), 'tag__and' => array ( ), 'tag_slug__in' => array ( ), 'tag_slug__and' => array ( ), 'post_parent__in' => array ( ), 'post_parent__not_in' => array ( ), 'author__in' => array ( ), 'author__not_in' => array ( ), 'ignore_sticky_posts' => false, 'suppress_filters' => false, 'cache_results' => true, 'update_post_term_cache' => true, 'lazy_load_term_meta' => true, 'update_post_meta_cache' => true, 'posts_per_page' => 10, 'nopaging' => false, 'comments_per_page' => '50', 'no_found_rows' => false, 'order' => 'DESC', ), 'tax_query' => WP_Tax_Query::__set_state(array( 'queries' => array ( ), 'relation' => 'AND', 'table_aliases' => array ( ), 'queried_terms' => array ( ), 'primary_table' => 'wp_posts', 'primary_id_column' => 'ID', )), 'meta_query' => WP_Meta_Query::__set_state(array( 'queries' => array ( ), 'relation' => NULL, 'meta_table' => NULL, 'meta_id_column' => NULL, 'primary_table' => NULL, 'primary_id_column' => NULL, 'table_aliases' => array ( ), 'clauses' => array ( ), 'has_or_relation' => false, )), 'date_query' => false, 'request' => 'SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'campaign' AND ((wp_posts.post_status = 'publish')) ORDER BY wp_posts.post_date DESC LIMIT 0, 10', 'posts' => array ( 0 => WP_Post::__set_state(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), ), 'post_count' => 1, 'current_post' => -1, 'in_the_loop' => false, 'post' => WP_Post::__set_state(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), 'comment_count' => 0, 'current_comment' => -1, 'found_posts' => '1', 'max_num_pages' => 1.0, 'max_num_comment_pages' => 0, 'is_single' => false, 'is_preview' => false, 'is_page' => false, 'is_archive' => true, 'is_date' => false, 'is_year' => false, 'is_month' => false, 'is_day' => false, 'is_time' => false, 'is_author' => false, 'is_category' => false, 'is_tag' => false, 'is_tax' => false, 'is_search' => false, 'is_feed' => false, 'is_comment_feed' => false, 'is_trackback' => false, 'is_home' => false, 'is_404' => false, 'is_embed' => false, 'is_paged' => false, 'is_admin' => false, 'is_attachment' => false, 'is_singular' => false, 'is_robots' => false, 'is_posts_page' => false, 'is_post_type_archive' => true, 'query_vars_hash' => 'e36a12d927198b4f30d55d60b891715f', 'query_vars_changed' => false, 'thumbnails_cached' => false, 'stopwords' => NULL, 'compat_fields' => array ( 0 => 'query_vars_hash', 1 => 'query_vars_changed', ), 'compat_methods' => array ( 0 => 'init_query_flags', 1 => 'parse_tax_query', ), ); + // 'posts' => array ( 0 => WP_Post::__set_state(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), ), 'post_count' => 1, 'current_post' => -1, 'in_the_loop' => false, 'post' => WP_Post::__set_state(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), 'comment_count' => 0, 'current_comment' => -1, 'found_posts' => '1', 'max_num_pages' => 1.0, 'max_num_comment_pages' => 0, 'is_single' => false, 'is_preview' => false, 'is_page' => false, 'is_archive' => true, 'is_date' => false, 'is_year' => false, 'is_month' => false, 'is_day' => false, 'is_time' => false, 'is_author' => false, 'is_category' => false, 'is_tag' => false, 'is_tax' => false, 'is_search' => false, 'is_feed' => false, 'is_comment_feed' => false, 'is_trackback' => false, 'is_home' => false, 'is_404' => false, 'is_embed' => false, 'is_paged' => false, 'is_admin' => false, 'is_attachment' => false, 'is_singular' => false, 'is_robots' => false, 'is_posts_page' => false, 'is_post_type_archive' => true, 'query_vars_hash' => 'e36a12d927198b4f30d55d60b891715f', 'query_vars_changed' => false, 'thumbnails_cached' => false, 'stopwords' => NULL, 'compat_fields' => array ( 0 => 'query_vars_hash', 1 => 'query_vars_changed', ), 'compat_methods' => array ( 0 => 'init_query_flags', 1 => 'parse_tax_query', ), )) \ No newline at end of file From 9f45e4196c6d24cf19c18857910f4f41c135d4ae Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Tue, 16 Oct 2018 15:42:20 -0700 Subject: [PATCH 36/65] Add get_mailings_results_wpdb tests. --- addons/tests/mailing_test.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/addons/tests/mailing_test.php b/addons/tests/mailing_test.php index 8434810..68c10ff 100644 --- a/addons/tests/mailing_test.php +++ b/addons/tests/mailing_test.php @@ -211,9 +211,17 @@ public function test_mailings_distribution_post_method(): void ) ); // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + + $mailings_stats = array(0 => [ + 'campaign_id' => 263, + 'conversions' => 0, + 'losses' => 0, + 'sent' => 0, + 'variation_subject' => 0 + ]); $mhMock= $this->getMockBuilder(MailingsHelpers::class) - ->setMethods(['wp_query_posts', 'setUpCampaigns']) + ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb']) ->getMock(); $mhMock->expects($this->once()) @@ -223,6 +231,10 @@ public function test_mailings_distribution_post_method(): void $mhMock->expects($this->once()) ->method('setUpCampaigns') ->will($this->returnValue($campaigns)); + + $mhMock->expects($this->once()) + ->method('get_mailings_results_wpdb') + ->will($this->returnValue($mailings_stats)); $mailingsMock->get_distributions($wpdb, $mhMock); } From 7174eee21383c305f2370df5d34a7b7b655ce52c Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Wed, 17 Oct 2018 13:28:41 -0700 Subject: [PATCH 37/65] Add test to check return value of get_distributions. --- addons/tests/mailing_test.php | 72 +++++++++++++++++++++++++++++++---- 1 file changed, 65 insertions(+), 7 deletions(-) diff --git a/addons/tests/mailing_test.php b/addons/tests/mailing_test.php index 68c10ff..ebebe20 100644 --- a/addons/tests/mailing_test.php +++ b/addons/tests/mailing_test.php @@ -191,21 +191,24 @@ final class mailingsClass extends TestCase public function test_mailings_distribution_post_method(): void { - $mailingsMock = new Mailings(); + $mailings = new Mailings(); $wpdb = new wpdb(); $postObject = new stdClass(); + $postObject->posts = array ( 0 => (object)(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), ); - + + //** must update return value from campaigns + $campaigns = array( 263 => array( "conversions" => 0, "fields" => array( - "subjects" => array() + "subjects" => array ( 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ),) ), "id" => 263, "losses" => 0, "sent" => 0, - "subjects" => array(), + "subjects" => array (), "title" => "No wall testing", "valid" => true ) @@ -221,7 +224,7 @@ public function test_mailings_distribution_post_method(): void ]); $mhMock= $this->getMockBuilder(MailingsHelpers::class) - ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb']) + ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) ->getMock(); $mhMock->expects($this->once()) @@ -235,11 +238,66 @@ public function test_mailings_distribution_post_method(): void $mhMock->expects($this->once()) ->method('get_mailings_results_wpdb') ->will($this->returnValue($mailings_stats)); - - $mailingsMock->get_distributions($wpdb, $mhMock); + + $mhMock->expects($this->once()) + ->method('get_fresh_subscribers_for_campaign') + ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); + + $this->assertEquals(array( + 'campaigns' => Array( + 0 => Array( + 'conversions' => 0, + 'fields' => Array( + 'subjects' => Array( + 0 => Array( + 'subject' => 'Tell Senate: No attacks on immigrants', + 'enabled' => true + ), + 1 => Array( + 'subject' => "Block Trump's attacks on immigrants", + 'enabled' => true + ) + ) + ), + 'id' => 263, + 'losses' => 0, + 'sent' => 0, + 'subjects' => Array( + 0 => Array( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 0, + 'title' => 'Tell Senate: No attacks on immigrants', + 'rate' => 1, + 'share' => 1 + ) + ), + 'title' => 'No wall testing', + 'valid' => true, + 'rate' => 1, + 'share' => 1, + 'limit' => 4.0 + ) + ), + 'overall' => Array( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 0, + 'boost' => 500, + 'rate' => 1 + ) + ), $mailings->get_distributions($wpdb, $mhMock)); + + } } + + // ( 0 => array ( 'conversions' => 0, 'fields' => array ( 'subjects' => array ( 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), 1 => array ( 'subject' => 'Block Trump's attacks on immigrants', 'enabled' => true, ), ), 'salutation' => '{{ user.first_name|default:"Hi" }},', 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

Already he has banned Muslims and refugees, threatened sanctuary cities and criminalized millions of undocumented immigrants. His Immigration and Custom Enforcement (ICE) and Border Patrol agents are separating parents from their children, deporting innocent people and spreading fear in immigrant communities across the country. The budget he just released slashes funding for the social safety net in order to spend billions of dollars building his wall, increasing his deportation force and expediting deportations.

Senate Democrats have the power to block funding for Trump’s wall and his massive deportation force. Recent reports show they may be willing to fight to stop Trump’s hateful agenda. Can you help make sure they stand strong?

We cannot let any Senate Democrat cave to political pressure from the right-wing and give Republicans the 60 votes they need to fund Trump’s hate. That is where progressives come in. We have to show Democratic senators that we will have their backs if they act boldly and that we will hold them accountable if they fail to stand up for immigrants.

Tell Senate Democrats: Block funding for Trump’s attacks on immigrants. Click here to sign the petition.

Trump wants to harass, deport and reject people who have come or are dreaming of coming to the United States seeking refuge from conflict, lives free from persecution and better economic opportunities for their families. For as long as Trump is president and extremist right-wing Republicans are willing to enable his agenda, Senate Democrats will have a choice: enable Trump’s agenda of criminalization and deportations, or resist and obstruct his racist regime at every turn. It is up to make sure they do the right thing.

Tell Senate Democrats: Block funding for Trump’s attacks on immigrants.

', 'petition_text' => '

“Block funding for Trump’s attacks on immigrants, including his deportation force and his border wall. “

', 'disclaimer' => '

We do not share your email address without your permission. We may send you updates on this and other important campaigns by email. If at any time you would like to unsubscribe from our email list, you may do so.

', 'share_titles' => array ( 0 => array ( 'title' => 'Senate Democrats: Block Trump’s attacks on immigrants', 'enabled' => true, ), 1 => array ( 'title' => 'Block the border wall!', 'enabled' => true, ), ), 'share_descriptions' => array ( 0 => array ( 'description' => 'There's new legislation to block Trump's border wall -- tell the Senate to pass it.', 'enabled' => true, ), 1 => array ( 'description' => 'The Senate can block Trump's anti-immigrant agenda, including the border wall.', 'enabled' => true, ), ), 'share_images' => array ( 0 => array ( 'image' => array ( 'ID' => 203, 'id' => 203, 'title' => 'Logo-square', 'filename' => 'Logo-square.png', 'url' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', 'alt' => '', 'author' => '5', 'description' => '', 'caption' => '', 'name' => 'logo-square', 'date' => '2017-03-01 04:24:22', 'modified' => '2017-03-01 04:24:22', 'mime_type' => 'image/png', 'type' => 'image', 'icon' => 'https://victorykit.local/wp-includes/images/media/default.png', 'width' => 132, 'height' => 125, 'sizes' => array ( 'thumbnail' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', 'thumbnail-width' => 132, 'thumbnail-height' => 125, 'medium' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', 'medium-width' => 132, 'medium-height' => 125, 'medium_large' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', 'medium_large-width' => 132, 'medium_large-height' => 125, 'large' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', 'large-width' => 132, 'large-height' => 125, ), ), 'enabled' => true, ), ), 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', 'from_line' => '41', 'landing_page_body' => '', 'after_action_email_content' => '

Thank you for taking action. Now please share this important campaign with your friends, and ask them to join in it. This will make it much more likely we can win!

', ), 'id' => 263, 'losses' => 0, 'sent' => 0, 'subjects' => array ( 0 => array ( 'conversions' => 0, 'losses' => 0, 'sent' => 0, 'title' => 'Tell Senate: No attacks on immigrants', 'rate' => 1, 'share' => 0.5, ), 1 => array ( 'conversions' => 0, 'losses' => 0, 'sent' => 0, 'title' => 'Block Trump's attacks on immigrants', 'rate' => 1, 'share' => 0.5, ), ), 'title' => 'No wall testing', 'valid' => true, 'rate' => 1, 'share' => 1, 'limit' => 4.0, ), ) + + + + // $posts = array( 'query' => array ( 'post_type' => 'campaign', 'post_status' => 'publish', ), 'query_vars' => array ( 'post_type' => 'campaign', 'post_status' => 'publish', 'error' => '', 'm' => '', 'p' => 0, 'post_parent' => '', 'subpost' => '', 'subpost_id' => '', 'attachment' => '', 'attachment_id' => 0, 'name' => '', 'static' => '', 'pagename' => '', 'page_id' => 0, 'second' => '', 'minute' => '', 'hour' => '', 'day' => 0, 'monthnum' => 0, 'year' => 0, 'w' => 0, 'category_name' => '', 'tag' => '', 'cat' => '', 'tag_id' => '', 'author' => '', 'author_name' => '', 'feed' => '', 'tb' => '', 'paged' => 0, 'meta_key' => '', 'meta_value' => '', 'preview' => '', 's' => '', 'sentence' => '', 'title' => '', 'fields' => '', 'menu_order' => '', 'embed' => '', 'category__in' => array ( ), 'category__not_in' => array ( ), 'category__and' => array ( ), 'post__in' => array ( ), 'post__not_in' => array ( ), 'post_name__in' => array ( ), 'tag__in' => array ( ), 'tag__not_in' => array ( ), 'tag__and' => array ( ), 'tag_slug__in' => array ( ), 'tag_slug__and' => array ( ), 'post_parent__in' => array ( ), 'post_parent__not_in' => array ( ), 'author__in' => array ( ), 'author__not_in' => array ( ), 'ignore_sticky_posts' => false, 'suppress_filters' => false, 'cache_results' => true, 'update_post_term_cache' => true, 'lazy_load_term_meta' => true, 'update_post_meta_cache' => true, 'posts_per_page' => 10, 'nopaging' => false, 'comments_per_page' => '50', 'no_found_rows' => false, 'order' => 'DESC', ), 'tax_query' => WP_Tax_Query::__set_state(array( 'queries' => array ( ), 'relation' => 'AND', 'table_aliases' => array ( ), 'queried_terms' => array ( ), 'primary_table' => 'wp_posts', 'primary_id_column' => 'ID', )), 'meta_query' => WP_Meta_Query::__set_state(array( 'queries' => array ( ), 'relation' => NULL, 'meta_table' => NULL, 'meta_id_column' => NULL, 'primary_table' => NULL, 'primary_id_column' => NULL, 'table_aliases' => array ( ), 'clauses' => array ( ), 'has_or_relation' => false, )), 'date_query' => false, 'request' => 'SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'campaign' AND ((wp_posts.post_status = 'publish')) ORDER BY wp_posts.post_date DESC LIMIT 0, 10', 'posts' => array ( 0 => WP_Post::__set_state(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), ), 'post_count' => 1, 'current_post' => -1, 'in_the_loop' => false, 'post' => WP_Post::__set_state(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), 'comment_count' => 0, 'current_comment' => -1, 'found_posts' => '1', 'max_num_pages' => 1.0, 'max_num_comment_pages' => 0, 'is_single' => false, 'is_preview' => false, 'is_page' => false, 'is_archive' => true, 'is_date' => false, 'is_year' => false, 'is_month' => false, 'is_day' => false, 'is_time' => false, 'is_author' => false, 'is_category' => false, 'is_tag' => false, 'is_tax' => false, 'is_search' => false, 'is_feed' => false, 'is_comment_feed' => false, 'is_trackback' => false, 'is_home' => false, 'is_404' => false, 'is_embed' => false, 'is_paged' => false, 'is_admin' => false, 'is_attachment' => false, 'is_singular' => false, 'is_robots' => false, 'is_posts_page' => false, 'is_post_type_archive' => true, 'query_vars_hash' => 'e36a12d927198b4f30d55d60b891715f', 'query_vars_changed' => false, 'thumbnails_cached' => false, 'stopwords' => NULL, 'compat_fields' => array ( 0 => 'query_vars_hash', 1 => 'query_vars_changed', ), 'compat_methods' => array ( 0 => 'init_query_flags', 1 => 'parse_tax_query', ), ); // 'posts' => array ( 0 => WP_Post::__set_state(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), ), 'post_count' => 1, 'current_post' => -1, 'in_the_loop' => false, 'post' => WP_Post::__set_state(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), 'comment_count' => 0, 'current_comment' => -1, 'found_posts' => '1', 'max_num_pages' => 1.0, 'max_num_comment_pages' => 0, 'is_single' => false, 'is_preview' => false, 'is_page' => false, 'is_archive' => true, 'is_date' => false, 'is_year' => false, 'is_month' => false, 'is_day' => false, 'is_time' => false, 'is_author' => false, 'is_category' => false, 'is_tag' => false, 'is_tax' => false, 'is_search' => false, 'is_feed' => false, 'is_comment_feed' => false, 'is_trackback' => false, 'is_home' => false, 'is_404' => false, 'is_embed' => false, 'is_paged' => false, 'is_admin' => false, 'is_attachment' => false, 'is_singular' => false, 'is_robots' => false, 'is_posts_page' => false, 'is_post_type_archive' => true, 'query_vars_hash' => 'e36a12d927198b4f30d55d60b891715f', 'query_vars_changed' => false, 'thumbnails_cached' => false, 'stopwords' => NULL, 'compat_fields' => array ( 0 => 'query_vars_hash', 1 => 'query_vars_changed', ), 'compat_methods' => array ( 0 => 'init_query_flags', 1 => 'parse_tax_query', ), )) \ No newline at end of file From 4cae4f16f87c98b574839b1851e68bda40e58f86 Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Wed, 17 Oct 2018 14:43:40 -0700 Subject: [PATCH 38/65] Two campaigns test updated with mailings helper mock. --- addons/tests/mailing_test.php | 222 +++++++++++++++++----------------- 1 file changed, 112 insertions(+), 110 deletions(-) diff --git a/addons/tests/mailing_test.php b/addons/tests/mailing_test.php index ebebe20..8a9343e 100644 --- a/addons/tests/mailing_test.php +++ b/addons/tests/mailing_test.php @@ -32,30 +32,31 @@ function query(){} final class mailingsClass extends TestCase { - // public function test_vk_mailings_create_new_mailings_action_two_campaigns(): void - // { - // global $TwoCampaigns; + public function test_vk_mailings_create_new_mailings_action_two_campaigns(): void + { + global $TwoCampaigns; - // $vk_mailings_mock = $this->createMock(Mailings::class); - // $wpdb_mock = $this->createMock(wpdb::class); + $vk_mailings_mock = $this->createMock(Mailings::class); + $wpdb_mock = $this->createMock(wpdb::class); + $mhMock= $this->createMock(MailingsHelpers::class); - // $vk_mailings_mock->expects($this->once()) - // ->method('get_distributions') - // ->willReturn($TwoCampaigns); + $vk_mailings_mock->expects($this->once()) + ->method('get_distributions') + ->willReturn($TwoCampaigns); - // $vk_mailings_mock->expects($this->exactly(2)) - // ->method('get_fresh_subscribers_for_campaign') - // ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + $mhMock->expects($this->exactly(2)) + ->method('get_fresh_subscribers_for_campaign') + ->willReturn(array( 0 => 6630470, 1 => 6630472,)); - // $vk_mailings_mock->expects($this->exactly(3)) - // ->method('send'); + $vk_mailings_mock->expects($this->exactly(3)) + ->method('send'); - // $wpdb_mock->expects($this->exactly(1)) - // ->method('query'); + $wpdb_mock->expects($this->exactly(1)) + ->method('query'); - // $result = vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock); - // // $this->assertTrue($result === null); - // } + $result = vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); + // $this->assertTrue($result === null); + } // public function test_vk_mailings_create_new_mailings_action_one_campaigns(): void // { @@ -63,12 +64,13 @@ final class mailingsClass extends TestCase // $vk_mailings_mock = $this->createMock(Mailings::class); // $wpdb_mock = $this->createMock(wpdb::class); + // $mhMock = $this->createMock(MailingsHelpers::class); // $vk_mailings_mock->expects($this->once()) // ->method('get_distributions') // ->willReturn($singleCampaign); - // $vk_mailings_mock->expects($this->exactly(1)) + // $mhMock->expects($this->exactly(1)) // ->method('get_fresh_subscribers_for_campaign') // ->willReturn(array( 0 => 6630470, 1 => 6630472,)); @@ -87,12 +89,13 @@ final class mailingsClass extends TestCase // $vk_mailings_mock = $this->createMock(Mailings::class); // $wpdb_mock = $this->createMock(wpdb::class); + // $mhMock = $this->createMock(MailingsHelpers::class); // $vk_mailings_mock->expects($this->once()) // ->method('get_distributions') // ->willReturn($twoSubjectCampaign); - // $vk_mailings_mock->expects($this->once()) + // $mhMock->expects($this->once()) // ->method('get_fresh_subscribers_for_campaign') // ->willReturn(array( 0 => 6630470, 1 => 6630472,)); @@ -189,107 +192,106 @@ final class mailingsClass extends TestCase // $subject->get_distributions($wpdb, $mhMock); // } - public function test_mailings_distribution_post_method(): void - { - $mailings = new Mailings(); - $wpdb = new wpdb(); - $postObject = new stdClass(); + // public function test_mailings_distribution_post_method(): void + // { + // $mailings = new Mailings(); + // $wpdb = new wpdb(); + // $postObject = new stdClass(); - $postObject->posts = array ( 0 => (object)(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), ); + // $postObject->posts = array ( 0 => (object)(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), ); - //** must update return value from campaigns + // //** must update return value from campaigns - $campaigns = array( - 263 => array( - "conversions" => 0, - "fields" => array( - "subjects" => array ( 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ),) - ), - "id" => 263, - "losses" => 0, - "sent" => 0, - "subjects" => array (), - "title" => "No wall testing", - "valid" => true - ) - ); - // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + // $campaigns = array( + // 263 => array( + // "conversions" => 0, + // "fields" => array( + // "subjects" => array ( 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ),) + // ), + // "id" => 263, + // "losses" => 0, + // "sent" => 0, + // "subjects" => array (), + // "title" => "No wall testing", + // "valid" => true + // ) + // ); + // // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - $mailings_stats = array(0 => [ - 'campaign_id' => 263, - 'conversions' => 0, - 'losses' => 0, - 'sent' => 0, - 'variation_subject' => 0 - ]); + // $mailings_stats = array(0 => [ + // 'campaign_id' => 263, + // 'conversions' => 0, + // 'losses' => 0, + // 'sent' => 0, + // 'variation_subject' => 0 + // ]); - $mhMock= $this->getMockBuilder(MailingsHelpers::class) - ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) - ->getMock(); + // $mhMock= $this->getMockBuilder(MailingsHelpers::class) + // ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) + // ->getMock(); - $mhMock->expects($this->once()) - ->method('wp_query_posts') - ->will($this->returnValue($postObject)); + // $mhMock->expects($this->once()) + // ->method('wp_query_posts') + // ->will($this->returnValue($postObject)); - $mhMock->expects($this->once()) - ->method('setUpCampaigns') - ->will($this->returnValue($campaigns)); + // $mhMock->expects($this->once()) + // ->method('setUpCampaigns') + // ->will($this->returnValue($campaigns)); - $mhMock->expects($this->once()) - ->method('get_mailings_results_wpdb') - ->will($this->returnValue($mailings_stats)); + // $mhMock->expects($this->once()) + // ->method('get_mailings_results_wpdb') + // ->will($this->returnValue($mailings_stats)); - $mhMock->expects($this->once()) - ->method('get_fresh_subscribers_for_campaign') - ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); + // $mhMock->expects($this->once()) + // ->method('get_fresh_subscribers_for_campaign') + // ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); - $this->assertEquals(array( - 'campaigns' => Array( - 0 => Array( - 'conversions' => 0, - 'fields' => Array( - 'subjects' => Array( - 0 => Array( - 'subject' => 'Tell Senate: No attacks on immigrants', - 'enabled' => true - ), - 1 => Array( - 'subject' => "Block Trump's attacks on immigrants", - 'enabled' => true - ) - ) - ), - 'id' => 263, - 'losses' => 0, - 'sent' => 0, - 'subjects' => Array( - 0 => Array( - 'conversions' => 0, - 'losses' => 0, - 'sent' => 0, - 'title' => 'Tell Senate: No attacks on immigrants', - 'rate' => 1, - 'share' => 1 - ) - ), - 'title' => 'No wall testing', - 'valid' => true, - 'rate' => 1, - 'share' => 1, - 'limit' => 4.0 - ) - ), - 'overall' => Array( - 'conversions' => 0, - 'losses' => 0, - 'sent' => 0, - 'boost' => 500, - 'rate' => 1 - ) - ), $mailings->get_distributions($wpdb, $mhMock)); + // $this->assertEquals(array( + // 'campaigns' => Array( + // 0 => Array( + // 'conversions' => 0, + // 'fields' => Array( + // 'subjects' => Array( + // 0 => Array( + // 'subject' => 'Tell Senate: No attacks on immigrants', + // 'enabled' => true + // ), + // 1 => Array( + // 'subject' => "Block Trump's attacks on immigrants", + // 'enabled' => true + // ) + // ) + // ), + // 'id' => 263, + // 'losses' => 0, + // 'sent' => 0, + // 'subjects' => Array( + // 0 => Array( + // 'conversions' => 0, + // 'losses' => 0, + // 'sent' => 0, + // 'title' => 'Tell Senate: No attacks on immigrants', + // 'rate' => 1, + // 'share' => 1 + // ) + // ), + // 'title' => 'No wall testing', + // 'valid' => true, + // 'rate' => 1, + // 'share' => 1, + // 'limit' => 4.0 + // ) + // ), + // 'overall' => Array( + // 'conversions' => 0, + // 'losses' => 0, + // 'sent' => 0, + // 'boost' => 500, + // 'rate' => 1 + // ) + // ), $mailings->get_distributions($wpdb, $mhMock)); - - } + // } } From a048b0faebd9bed466ddff4d091e1d282d819cc8 Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Wed, 17 Oct 2018 14:46:50 -0700 Subject: [PATCH 39/65] Add mailings helper class. --- addons/analytics.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/addons/analytics.php b/addons/analytics.php index 2b6d617..a9cc31c 100644 --- a/addons/analytics.php +++ b/addons/analytics.php @@ -4,6 +4,7 @@ if(!defined('ABSPATH')) exit; add_action('admin_menu', 'vk_analytics_admin_page'); +require_once(__DIR__. '/mockClasses/mailingsHelpers.php'); function vk_analytics_admin_page() { add_menu_page( @@ -18,7 +19,7 @@ function vk_analytics_admin_page() { } function vk_analytics_admin_page_render() { - global $vk_mailings; + global $vk_mailings, $mh; // Calculate distributions $distributions = $vk_mailings->get_distributions(0, 0); @@ -33,7 +34,7 @@ function vk_analytics_admin_page_render() { //$fields = $campaign['fields']; // $url = get_permalink($id); $limit_per_campaign = round($campaign['share'] * $limit_per_day); - $campaign['num_fresh_ids'] = sizeof($vk_mailings->get_fresh_subscribers_for_campaign($id, $limit_per_campaign)); + $campaign['num_fresh_ids'] = sizeof($mh->get_fresh_subscribers_for_campaign($id, $limit_per_campaign, 0)); } // Render From 66ebb01c9d5e50f22e7df53731c27aec4b4a1379 Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Wed, 17 Oct 2018 14:48:13 -0700 Subject: [PATCH 40/65] Add get_fresh_subscribers_for_campaign to mailings helpers class. --- addons/mockClasses/mailingsHelpers.php | 76 ++++++++++++++------------ 1 file changed, 41 insertions(+), 35 deletions(-) diff --git a/addons/mockClasses/mailingsHelpers.php b/addons/mockClasses/mailingsHelpers.php index e0baddf..f29af2e 100644 --- a/addons/mockClasses/mailingsHelpers.php +++ b/addons/mockClasses/mailingsHelpers.php @@ -1,7 +1,7 @@ 'campaign', 'post_status' => 'publish', @@ -10,14 +10,14 @@ function wp_query_posts(){ function getFields($id, $mh_mock){ if($mh_mock){ - print('in side get fields'); + return array('subjects' => array()); } else { return get_fields($id); } } function setUpCampaigns($results, $campaigns, $mh_mock){ - print('inside setUpCampaigns func'); + $campaigns = $campaigns; foreach ($results->posts as $campaign) { $id = $campaign->ID; @@ -46,7 +46,6 @@ function setUpCampaigns($results, $campaigns, $mh_mock){ } function get_mailings_results_wpdb($db){ - print('inside get mailings result wpdb'); return $db->get_results(' SELECT vkm.campaign_id, @@ -60,39 +59,46 @@ function get_mailings_results_wpdb($db){ vkm.campaign_id, vkm.variation_subject ', ARRAY_A); } -// function mailingStats($mailings, $campaigns, $overall){ -// $results = array(); - -// foreach ($mailings as $mailing) { -// $id = $mailing['campaign_id']; - -// // Make sure to only include currently published campaigns in overall data -// // TODO: why not just check for campaign published in query above? -// if (!isset($campaigns[$id])) { -// continue; -// } - -// $overall['conversions'] += $mailing['conversions']; -// $overall['losses'] += $mailing['losses']; -// $overall['sent'] += $mailing['sent']; - -// $campaigns[$id]['conversions'] += $mailing['conversions']; -// $campaigns[$id]['losses'] += $mailing['losses']; -// $campaigns[$id]['sent'] += $mailing['sent']; - -// $subject = $mailing['variation_subject']; -// $campaigns[$id]['subjects'][$subject] = array( -// 'conversions' => +$mailing['conversions'], -// 'losses' => +$mailing['losses'], -// 'sent' => +$mailing['sent'], -// ); -// } -// $results['campaign_result'] = $campaigns; -// $results['overall_result'] = $overall; -// return $results; -// } + // Get all subscribers who have not been mailed for this campaign AND have not been mailed for any campaign within the last week + function get_fresh_subscribers_for_campaign($campaign_id, $limit, $wpdb_mock) + { + global $wpdb; + + if($wpdb_mock){ + $wpdb = $wpdb_mock; + } + + $sql = " + SELECT + vks.ak_user_id + FROM + vk_subscriber AS vks + LEFT JOIN + vk_subscriber_mailing AS vksm + ON + vks.ak_user_id = vksm.ak_user_id AND + ( + vksm.campaign_id = $campaign_id + OR + vksm.created_at > DATE_SUB(NOW(), INTERVAL 1 WEEK) + ) + WHERE + vksm.ak_user_id IS NULL + LIMIT $limit; + "; + // $allsql = trim(preg_replace('/\s+/', ' ',var_export( $sql, true))); + // error_log('line 260 sql query '.$allsql); + + $response = $wpdb->get_col($sql, 0); + + $ids = array_map(function($el) { + return +$el; + }, $response); + + return $ids; + } } function mh() { From 8619f3234641e14206363d4ce4bd9d4668b4820f Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Wed, 17 Oct 2018 14:49:10 -0700 Subject: [PATCH 41/65] Remove console logs and update functions with parameters. --- addons/mailings.php | 86 +++++++++++---------------------------------- 1 file changed, 20 insertions(+), 66 deletions(-) diff --git a/addons/mailings.php b/addons/mailings.php index 0cedb61..f99e501 100644 --- a/addons/mailings.php +++ b/addons/mailings.php @@ -19,7 +19,7 @@ function get_distributions($wpdb_mock, $mh_mock) { global $wpdb; global $mh; - print('in on line 22 get_distributions@@@@ '); + if($wpdb_mock){ $wpdb = $wpdb_mock; $mh = $mh_mock; @@ -33,7 +33,8 @@ function get_distributions($wpdb_mock, $mh_mock) // Get active campaigns $campaigns = array(); - $results = $mh->wp_query_posts(); + //**********investigate what comes back from wp_query_posts + $results = $mh->wp_query_posts($mh_mock); // $results1 = trim(preg_replace('/\s+/', ' ',var_export( $results, true))); // var_dump($results1); @@ -50,10 +51,9 @@ function get_distributions($wpdb_mock, $mh_mock) $mailings = $mh->get_mailings_results_wpdb($wpdb); - //need to print error log here - $mailingsPrint = trim(preg_replace('/\s+/', ' ',var_export( $mailings, true))); - error_log('line 54 return value of get_results '.$mailingsPrint); - + // need to print error log here + // $mailingsPrint = trim(preg_replace('/\s+/', ' ',var_export( $mailings, true))); + // error_log('$$$line 54 return value of get_results '.$mailingsPrint); foreach ($mailings as $mailing) { $id = $mailing['campaign_id']; @@ -62,6 +62,7 @@ function get_distributions($wpdb_mock, $mh_mock) if (!isset($campaigns[$id])) { continue; } + $overall['conversions'] += $mailing['conversions']; $overall['losses'] += $mailing['losses']; @@ -107,7 +108,6 @@ function get_distributions($wpdb_mock, $mh_mock) $fields = $campaign['fields']; $subject_rate_sum = 0; $valid_subjects = 0; - foreach ($campaign['subjects'] as $subject_index => &$subject) { $enabled = $fields['subjects'][$subject_index]['enabled']; @@ -166,9 +166,6 @@ function get_distributions($wpdb_mock, $mh_mock) return $campaign['valid']; }); - $campaigns1 = trim(preg_replace('/\s+/', ' ',var_export( $campaigns, true))); - error_log('line 197 campaigns after array_filter function '.$campaigns1); - // Get share percentages foreach ($campaigns as $campaign_index => &$campaign) { $share = $campaign_rate_sum ? $campaign['rate'] / $campaign_rate_sum : 0; @@ -178,26 +175,20 @@ function get_distributions($wpdb_mock, $mh_mock) // Limit share percentages, based on subscriber availability $campaign_share_sum = 1; $limit_per_day = get_option('subscribed_users') / 7; - - $limit_per_day1 = trim(preg_replace('/\s+/', ' ',var_export( $limit_per_day, true))); - error_log('line 210 return value of get_options function'.$limit_per_day1); - + foreach ($campaigns as $campaign_index => &$campaign) { + $limit_per_campaign = round($campaign['share'] * $limit_per_day); // $limit = trim(preg_replace('/\s+/', ' ',var_export($limit_per_campaign, true))); // error_log('line 192 limit_per_campaign '.$limit); - $fresh_ids = count($this->get_fresh_subscribers_for_campaign($campaign['id'], $limit_per_campaign)); - - $fresh_ids1 = trim(preg_replace('/\s+/', ' ',var_export( $fresh_ids, true))); - error_log('line 222 return value of get_fresh_subscribers_for_campaign function'.$fresh_ids1); - + $fresh_ids = count($mh->get_fresh_subscribers_for_campaign($campaign['id'], $limit_per_campaign, $wpdb_mock)); // Plenty? Great. //fresh ids should not be 0 + if ($fresh_ids >= $limit_per_campaign) { $campaign['limit'] = $limit_per_campaign; continue; } - // Shortage of fresh IDs. Calculations are required. $campaign['limit'] = $fresh_ids; //CURRENTLY RETURNING 0 FOR $SHARE THIS IS MAKING A NAN VALUE @@ -206,16 +197,17 @@ function get_distributions($wpdb_mock, $mh_mock) $campaign_share_sum += $share - $campaign['share']; $campaign['share'] = $share; - + // $campShare = $campaign['share']; // $allsql = trim(preg_replace('/\s+/', ' ',var_export( $campShare, true))); // error_log('line 225 $campaign_share_sum '.$campaign_share_sum); // error_log('line 226 variable campaing[share] '.$share); } + foreach ($campaigns as $campaign_index => &$campaign) { $campaign['share'] = $campaign['share'] / $campaign_share_sum; } - + usort($campaigns, function($a, $b) { $difference = $b['share'] - $a['share']; if ($difference == 0) { @@ -227,52 +219,13 @@ function get_distributions($wpdb_mock, $mh_mock) } }); - $campaigns1 = trim(preg_replace('/\s+/', ' ',var_export( $campaigns, true))); - error_log('line 262 $campaigns'.$campaigns1); - - $overall1 = trim(preg_replace('/\s+/', ' ',var_export( $overall, true))); - error_log('line 264 $overall'.$overall1); - return array( 'campaigns' => $campaigns, 'overall' => $overall, ); } - // Get all subscribers who have not been mailed for this campaign AND have not been mailed for any campaign within the last week - function get_fresh_subscribers_for_campaign($campaign_id, $limit) - { - global $wpdb; - - $sql = " - SELECT - vks.ak_user_id - FROM - vk_subscriber AS vks - LEFT JOIN - vk_subscriber_mailing AS vksm - ON - vks.ak_user_id = vksm.ak_user_id AND - ( - vksm.campaign_id = $campaign_id - OR - vksm.created_at > DATE_SUB(NOW(), INTERVAL 1 WEEK) - ) - WHERE - vksm.ak_user_id IS NULL - LIMIT $limit; - "; - - // $allsql = trim(preg_replace('/\s+/', ' ',var_export( $sql, true))); - // error_log('line 260 sql query '.$allsql); - - $response = $wpdb->get_col($sql, 0); - - $ids = array_map(function($el) { - return +$el; - }, $response); - return $ids; - } + // Update conversion stats from ActionKit for a campaign mailing function get_mailing_stats_from_ak($ak_mailing_id) @@ -504,17 +457,18 @@ function vk_mailings_update_mailing_stats_action() { // Create new mailings based on the currently running campaigns // Run once a day at 8am -function vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock) { - global $vk_mailings, $wpdb; +function vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock) { + global $vk_mailings, $wpdb, $mh; if($vk_mailings_mock){ $vk_mailings = $vk_mailings_mock; $wpdb = $wpdb_mock; + $mh = $mhMock; } $limit_per_day = get_option('subscribed_users') / 7; - $distributions = $vk_mailings->get_distributions(0, 0); + $distributions = $vk_mailings->get_distributions($vk_mailings_mock, $wpdb_mock, $mhMock); // $distributions_results = trim(preg_replace('/\s+/', ' ',var_export( $distributions, true))); // $distributions_results = sizeof($distributions_results); // error_log('@@@ limit_per_day variable '.$distributions_results); @@ -531,7 +485,7 @@ function vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock) { // error_log('line 535 campaign[share] variable '.$sCamp); $limit_per_campaign = round($campaign['share'] * $limit_per_day); - $fresh_ids = $vk_mailings->get_fresh_subscribers_for_campaign($id, $limit_per_campaign); + $fresh_ids = $mh->get_fresh_subscribers_for_campaign($id, $limit_per_campaign, $wpdb); // Out of users to send to? if (count($fresh_ids) == 0) { From 2da88a68bc8ab181f92f376e7c7d845bbf4628ac Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Thu, 18 Oct 2018 09:41:44 -0700 Subject: [PATCH 42/65] Update vk_mailings_create_new_mailings_action to take three arguments. --- addons/tests/mailing_test.php | 407 +++++++++++++++++----------------- 1 file changed, 203 insertions(+), 204 deletions(-) diff --git a/addons/tests/mailing_test.php b/addons/tests/mailing_test.php index 8a9343e..e4f90e0 100644 --- a/addons/tests/mailing_test.php +++ b/addons/tests/mailing_test.php @@ -54,244 +54,243 @@ public function test_vk_mailings_create_new_mailings_action_two_campaigns(): voi $wpdb_mock->expects($this->exactly(1)) ->method('query'); - $result = vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); - // $this->assertTrue($result === null); + vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); } - // public function test_vk_mailings_create_new_mailings_action_one_campaigns(): void - // { - // global $singleCampaign; + public function test_vk_mailings_create_new_mailings_action_one_campaigns(): void + { + global $singleCampaign; - // $vk_mailings_mock = $this->createMock(Mailings::class); - // $wpdb_mock = $this->createMock(wpdb::class); - // $mhMock = $this->createMock(MailingsHelpers::class); + $vk_mailings_mock = $this->createMock(Mailings::class); + $wpdb_mock = $this->createMock(wpdb::class); + $mhMock = $this->createMock(MailingsHelpers::class); - // $vk_mailings_mock->expects($this->once()) - // ->method('get_distributions') - // ->willReturn($singleCampaign); + $vk_mailings_mock->expects($this->once()) + ->method('get_distributions') + ->willReturn($singleCampaign); - // $mhMock->expects($this->exactly(1)) - // ->method('get_fresh_subscribers_for_campaign') - // ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + $mhMock->expects($this->exactly(1)) + ->method('get_fresh_subscribers_for_campaign') + ->willReturn(array( 0 => 6630470, 1 => 6630472,)); - // $vk_mailings_mock->expects($this->exactly(2)) - // ->method('send'); + $vk_mailings_mock->expects($this->exactly(2)) + ->method('send'); - // $wpdb_mock->expects($this->exactly(1)) - // ->method('query'); + $wpdb_mock->expects($this->exactly(1)) + ->method('query'); - // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock); - // } + vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); + } - // public function test_get_distribution_two_subject_campaign(): void - // { - // global $twoSubjectCampaign; + public function test_get_distribution_two_subject_campaign(): void + { + global $twoSubjectCampaign; - // $vk_mailings_mock = $this->createMock(Mailings::class); - // $wpdb_mock = $this->createMock(wpdb::class); - // $mhMock = $this->createMock(MailingsHelpers::class); + $vk_mailings_mock = $this->createMock(Mailings::class); + $wpdb_mock = $this->createMock(wpdb::class); + $mhMock = $this->createMock(MailingsHelpers::class); - // $vk_mailings_mock->expects($this->once()) - // ->method('get_distributions') - // ->willReturn($twoSubjectCampaign); + $vk_mailings_mock->expects($this->once()) + ->method('get_distributions') + ->willReturn($twoSubjectCampaign); - // $mhMock->expects($this->once()) - // ->method('get_fresh_subscribers_for_campaign') - // ->willReturn(array( 0 => 6630470, 1 => 6630472,)); - - // $vk_mailings_mock->expects($this->once()) - // ->method('send') - // ->willReturn(array ( 'ak_mailing_id' => 1)); - - // $wpdb_mock->expects($this->once()) - // ->method('query'); - - // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock); - // } - - // public function test_mailingsHelpers_wp_query_posts_method(): void - // { - // $mailingsMock = new Mailings(); - // $wpdb = new wpdb(); - // $postObject = new stdClass(); - // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - - // $mhMock= $this->getMockBuilder(MailingsHelpers::class) - // ->setMethods(['wp_query_posts']) - // ->getMock(); - - // $mhMock->expects($this->once()) - // ->method('wp_query_posts') - // ->will($this->returnValue($postObject)); - - // $mailingsMock->get_distributions($wpdb, $mhMock); - // } - - // public function test_get_fields(): void - // { - // $wpdb = new wpdb(); - // $postObject = new stdClass(); - // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - - // $mhMock= $this->getMockBuilder(MailingsHelpers::class) - // ->setMethods(['wp_query_posts', 'getFields']) - // ->getMock(); + $mhMock->expects($this->once()) + ->method('get_fresh_subscribers_for_campaign') + ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + + $vk_mailings_mock->expects($this->once()) + ->method('send') + ->willReturn(array ( 'ak_mailing_id' => 1)); + + $wpdb_mock->expects($this->once()) + ->method('query'); + + vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); + } + + public function test_mailingsHelpers_wp_query_posts_method(): void + { + $mailingsMock = new Mailings(); + $wpdb = new wpdb(); + $postObject = new stdClass(); + $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['wp_query_posts']) + ->getMock(); + + $mhMock->expects($this->once()) + ->method('wp_query_posts') + ->will($this->returnValue($postObject)); + + $mailingsMock->get_distributions($wpdb, $mhMock); + } + + public function test_get_fields(): void + { + $wpdb = new wpdb(); + $postObject = new stdClass(); + $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['wp_query_posts', 'getFields']) + ->getMock(); - // $mhMock->expects($this->once()) - // ->method('wp_query_posts') - // ->will($this->returnValue($postObject)); + $mhMock->expects($this->once()) + ->method('wp_query_posts') + ->will($this->returnValue($postObject)); - // $mhMock->expects($this->once()) - // ->method('getFields'); + $mhMock->expects($this->once()) + ->method('getFields'); - // $subject = new Mailings(); + $subject = new Mailings(); - // $subject->get_distributions($wpdb, $mhMock); - // } - - // public function test_mailingsHelpers_setUpCampaigns(): void - // { - // $wpdb = new wpdb(); - // // $postObject = new stdClass(); - // $postObject = array(0 => (object) ['ID' => '', 'post_title'=> '']); - // $campaigns = array( 0 => array( 'fields'=> '', 'campaign_id' => '', 'subjects' => array())); - - // $mhMock= $this->getMockBuilder(MailingsHelpers::class) - // ->setMethods(['wp_query_posts', 'setUpCampaigns']) - // ->getMock(); + $subject->get_distributions($wpdb, $mhMock); + } + + public function test_mailingsHelpers_setUpCampaigns(): void + { + $wpdb = new wpdb(); + // $postObject = new stdClass(); + $postObject = array(0 => (object) ['ID' => '', 'post_title'=> '']); + $campaigns = array( 0 => array( 'fields'=> '', 'campaign_id' => '', 'subjects' => array())); + + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['wp_query_posts', 'setUpCampaigns']) + ->getMock(); - // $mhMock->expects($this->once()) - // ->method('wp_query_posts') - // ->will($this->returnValue($postObject)); + $mhMock->expects($this->once()) + ->method('wp_query_posts') + ->will($this->returnValue($postObject)); - // $mhMock->expects($this->once()) - // ->method('setUpCampaigns') - // ->will($this->returnValue($campaigns)); + $mhMock->expects($this->once()) + ->method('setUpCampaigns') + ->will($this->returnValue($campaigns)); - // $subject = new Mailings(); + $subject = new Mailings(); - // $subject->get_distributions($wpdb, $mhMock); - // } - - // public function test_mailingsHelpers_get_mailings_results_wpdb(): void - // { - // $wpdb = new wpdb(); - // // $postObject = new stdClass(); - // $postObject = array( 0 => array( 'campaign_id' => '1')); - - // $mhMock= $this->getMockBuilder(MailingsHelpers::class) - // ->setMethods(['get_mailings_results_wpdb']) - // ->getMock(); + $subject->get_distributions($wpdb, $mhMock); + } + + public function test_mailingsHelpers_get_mailings_results_wpdb(): void + { + $wpdb = new wpdb(); + // $postObject = new stdClass(); + $postObject = array( 0 => array( 'campaign_id' => '1')); + + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['get_mailings_results_wpdb']) + ->getMock(); - // $mhMock->expects($this->once()) - // ->method('get_mailings_results_wpdb') - // ->will($this->returnValue($postObject)); + $mhMock->expects($this->once()) + ->method('get_mailings_results_wpdb') + ->will($this->returnValue($postObject)); - // $subject = new Mailings(); + $subject = new Mailings(); - // $subject->get_distributions($wpdb, $mhMock); - // } - - // public function test_mailings_distribution_post_method(): void - // { - // $mailings = new Mailings(); - // $wpdb = new wpdb(); - // $postObject = new stdClass(); + $subject->get_distributions($wpdb, $mhMock); + } + + public function test_mailings_distribution_post_method(): void + { + $mailings = new Mailings(); + $wpdb = new wpdb(); + $postObject = new stdClass(); - // $postObject->posts = array ( 0 => (object)(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), ); + $postObject->posts = array ( 0 => (object)(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), ); - // //** must update return value from campaigns + //** must update return value from campaigns - // $campaigns = array( - // 263 => array( - // "conversions" => 0, - // "fields" => array( - // "subjects" => array ( 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ),) - // ), - // "id" => 263, - // "losses" => 0, - // "sent" => 0, - // "subjects" => array (), - // "title" => "No wall testing", - // "valid" => true - // ) - // ); - // // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + $campaigns = array( + 263 => array( + "conversions" => 0, + "fields" => array( + "subjects" => array ( 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ),) + ), + "id" => 263, + "losses" => 0, + "sent" => 0, + "subjects" => array (), + "title" => "No wall testing", + "valid" => true + ) + ); + // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - // $mailings_stats = array(0 => [ - // 'campaign_id' => 263, - // 'conversions' => 0, - // 'losses' => 0, - // 'sent' => 0, - // 'variation_subject' => 0 - // ]); + $mailings_stats = array(0 => [ + 'campaign_id' => 263, + 'conversions' => 0, + 'losses' => 0, + 'sent' => 0, + 'variation_subject' => 0 + ]); - // $mhMock= $this->getMockBuilder(MailingsHelpers::class) - // ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) - // ->getMock(); + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) + ->getMock(); - // $mhMock->expects($this->once()) - // ->method('wp_query_posts') - // ->will($this->returnValue($postObject)); + $mhMock->expects($this->once()) + ->method('wp_query_posts') + ->will($this->returnValue($postObject)); - // $mhMock->expects($this->once()) - // ->method('setUpCampaigns') - // ->will($this->returnValue($campaigns)); + $mhMock->expects($this->once()) + ->method('setUpCampaigns') + ->will($this->returnValue($campaigns)); - // $mhMock->expects($this->once()) - // ->method('get_mailings_results_wpdb') - // ->will($this->returnValue($mailings_stats)); + $mhMock->expects($this->once()) + ->method('get_mailings_results_wpdb') + ->will($this->returnValue($mailings_stats)); - // $mhMock->expects($this->once()) - // ->method('get_fresh_subscribers_for_campaign') - // ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); + $mhMock->expects($this->once()) + ->method('get_fresh_subscribers_for_campaign') + ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); - // $this->assertEquals(array( - // 'campaigns' => Array( - // 0 => Array( - // 'conversions' => 0, - // 'fields' => Array( - // 'subjects' => Array( - // 0 => Array( - // 'subject' => 'Tell Senate: No attacks on immigrants', - // 'enabled' => true - // ), - // 1 => Array( - // 'subject' => "Block Trump's attacks on immigrants", - // 'enabled' => true - // ) - // ) - // ), - // 'id' => 263, - // 'losses' => 0, - // 'sent' => 0, - // 'subjects' => Array( - // 0 => Array( - // 'conversions' => 0, - // 'losses' => 0, - // 'sent' => 0, - // 'title' => 'Tell Senate: No attacks on immigrants', - // 'rate' => 1, - // 'share' => 1 - // ) - // ), - // 'title' => 'No wall testing', - // 'valid' => true, - // 'rate' => 1, - // 'share' => 1, - // 'limit' => 4.0 - // ) - // ), - // 'overall' => Array( - // 'conversions' => 0, - // 'losses' => 0, - // 'sent' => 0, - // 'boost' => 500, - // 'rate' => 1 - // ) - // ), $mailings->get_distributions($wpdb, $mhMock)); + $this->assertEquals(array( + 'campaigns' => Array( + 0 => Array( + 'conversions' => 0, + 'fields' => Array( + 'subjects' => Array( + 0 => Array( + 'subject' => 'Tell Senate: No attacks on immigrants', + 'enabled' => true + ), + 1 => Array( + 'subject' => "Block Trump's attacks on immigrants", + 'enabled' => true + ) + ) + ), + 'id' => 263, + 'losses' => 0, + 'sent' => 0, + 'subjects' => Array( + 0 => Array( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 0, + 'title' => 'Tell Senate: No attacks on immigrants', + 'rate' => 1, + 'share' => 1 + ) + ), + 'title' => 'No wall testing', + 'valid' => true, + 'rate' => 1, + 'share' => 1, + 'limit' => 4.0 + ) + ), + 'overall' => Array( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 0, + 'boost' => 500, + 'rate' => 1 + ) + ), $mailings->get_distributions($wpdb, $mhMock)); - // } + } } From 10dc9ffd02565b2c656b38820e5a85ad89536f00 Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Mon, 22 Oct 2018 15:23:08 -0700 Subject: [PATCH 43/65] get_distribution test in progress. --- addons/tests/mailing_test.php | 109 +++++++++++++++++++++++++++++++++- 1 file changed, 108 insertions(+), 1 deletion(-) diff --git a/addons/tests/mailing_test.php b/addons/tests/mailing_test.php index e4f90e0..948e311 100644 --- a/addons/tests/mailing_test.php +++ b/addons/tests/mailing_test.php @@ -288,8 +288,115 @@ public function test_mailings_distribution_post_method(): void 'boost' => 500, 'rate' => 1 ) - ), $mailings->get_distributions($wpdb, $mhMock)); + ), $mailings->get_distributions($wpdb, $mhMock)); + } + + public function test_get_distributions_method(): void + { + $mailings = new Mailings(); + $wpdb = new wpdb(); + $postObject = new stdClass(); + + $postObject->posts = array (); + + //** must update return value from campaigns + + $campaigns = array( + 263 => array( + "conversions" => 0, + "fields" => array( + "subjects" => array ( 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ),) + ), + "id" => 263, + "losses" => 0, + "sent" => 0, + "subjects" => array ( 0 => array ( 'conversions' => 0, 'losses' => 0, 'sent' => 0, 'title' => 'Tell Senate: No attacks on immigrants', 'rate' => 1, 'share' => 0.5, ), 1 => array ( 'conversions' => 0, 'losses' => 0, 'sent' => 0, 'title' => "Block Trump's attacks on immigrants", 'rate' => 1, 'share' => 0.5)), + "title" => "No wall testing", + "valid" => true + ) + ); + // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + $mailings_stats = array(0 => [ + 'campaign_id' => 263, + 'conversions' => 2, + 'losses' => 2, + 'sent' => 4, + 'variation_subject' => 0 + ]); + + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) + ->getMock(); + + $mhMock->expects($this->once()) + ->method('wp_query_posts') + ->will($this->returnValue($postObject)); + + $mhMock->expects($this->once()) + ->method('setUpCampaigns') + ->will($this->returnValue($campaigns)); + + $mhMock->expects($this->once()) + ->method('get_mailings_results_wpdb') + ->will($this->returnValue($mailings_stats)); + + $mhMock->expects($this->once()) + ->method('get_fresh_subscribers_for_campaign') + ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); + + $this->assertEquals(array( + 'campaigns' => Array( + 0 => Array( + 'conversions' => 2, + 'fields' => Array( + 'subjects' => Array( + 0 => Array( + 'subject' => 'Tell Senate: No attacks on immigrants', + 'enabled' => true + ), + 1 => Array( + 'subject' => "Block Trump's attacks on immigrants", + 'enabled' => true + ) + ) + ), + 'id' => 263, + 'losses' => 2, + 'sent' => 4, + 'subjects' => Array( + 0 => Array( + 'conversions' => 2, + 'losses' => 2, + 'sent' => 4, + 'title' => 'Tell Senate: No attacks on immigrants', + 'rate' => 0.9841899722852103, + 'share' => 0.49800796812749004 + ), + 1 => Array( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 0, + 'title' => "Block Trump's attacks on immigrants", + 'rate' => 0.9920634920634921, + 'share' => 0.50199203187251 + ) + ), + 'title' => 'No wall testing', + 'valid' => true, + 'rate' => 0.9841899722852103, + 'share' => 1.0, + 'limit' => 4.0 + ) + ), + 'overall' => Array( + 'conversions' => 2, + 'losses' => 2, + 'sent' => 4, + 'boost' => 500, + 'rate' => 0.9920634920634921 + ) + ), $mailings->get_distributions($wpdb, $mhMock)); } } From a8d4868f484b0c3759e6ba80e36e249fd48c5c8f Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Tue, 30 Oct 2018 09:15:58 -0700 Subject: [PATCH 44/65] Add one campaign test with one subject greater than another subject. --- addons/tests/mailing_test.php | 599 ++++++++++++++++++++-------------- 1 file changed, 360 insertions(+), 239 deletions(-) diff --git a/addons/tests/mailing_test.php b/addons/tests/mailing_test.php index 948e311..9d8c3eb 100644 --- a/addons/tests/mailing_test.php +++ b/addons/tests/mailing_test.php @@ -32,266 +32,380 @@ function query(){} final class mailingsClass extends TestCase { - public function test_vk_mailings_create_new_mailings_action_two_campaigns(): void - { - global $TwoCampaigns; + // public function test_vk_mailings_create_new_mailings_action_two_campaigns(): void + // { + // global $TwoCampaigns; - $vk_mailings_mock = $this->createMock(Mailings::class); - $wpdb_mock = $this->createMock(wpdb::class); - $mhMock= $this->createMock(MailingsHelpers::class); + // $vk_mailings_mock = $this->createMock(Mailings::class); + // $wpdb_mock = $this->createMock(wpdb::class); + // $mhMock= $this->createMock(MailingsHelpers::class); - $vk_mailings_mock->expects($this->once()) - ->method('get_distributions') - ->willReturn($TwoCampaigns); + // $vk_mailings_mock->expects($this->once()) + // ->method('get_distributions') + // ->willReturn($TwoCampaigns); - $mhMock->expects($this->exactly(2)) - ->method('get_fresh_subscribers_for_campaign') - ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + // $mhMock->expects($this->exactly(2)) + // ->method('get_fresh_subscribers_for_campaign') + // ->willReturn(array( 0 => 6630470, 1 => 6630472,)); - $vk_mailings_mock->expects($this->exactly(3)) - ->method('send'); + // $vk_mailings_mock->expects($this->exactly(3)) + // ->method('send'); - $wpdb_mock->expects($this->exactly(1)) - ->method('query'); + // $wpdb_mock->expects($this->exactly(1)) + // ->method('query'); - vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); - } + // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); + // } - public function test_vk_mailings_create_new_mailings_action_one_campaigns(): void - { - global $singleCampaign; + // public function test_vk_mailings_create_new_mailings_action_one_campaigns(): void + // { + // global $singleCampaign; - $vk_mailings_mock = $this->createMock(Mailings::class); - $wpdb_mock = $this->createMock(wpdb::class); - $mhMock = $this->createMock(MailingsHelpers::class); + // $vk_mailings_mock = $this->createMock(Mailings::class); + // $wpdb_mock = $this->createMock(wpdb::class); + // $mhMock = $this->createMock(MailingsHelpers::class); - $vk_mailings_mock->expects($this->once()) - ->method('get_distributions') - ->willReturn($singleCampaign); + // $vk_mailings_mock->expects($this->once()) + // ->method('get_distributions') + // ->willReturn($singleCampaign); - $mhMock->expects($this->exactly(1)) - ->method('get_fresh_subscribers_for_campaign') - ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + // $mhMock->expects($this->exactly(1)) + // ->method('get_fresh_subscribers_for_campaign') + // ->willReturn(array( 0 => 6630470, 1 => 6630472,)); - $vk_mailings_mock->expects($this->exactly(2)) - ->method('send'); + // $vk_mailings_mock->expects($this->exactly(2)) + // ->method('send'); - $wpdb_mock->expects($this->exactly(1)) - ->method('query'); + // $wpdb_mock->expects($this->exactly(1)) + // ->method('query'); - vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); - } + // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); + // } - public function test_get_distribution_two_subject_campaign(): void - { - global $twoSubjectCampaign; + // public function test_get_distribution_two_subject_campaign(): void + // { + // global $twoSubjectCampaign; - $vk_mailings_mock = $this->createMock(Mailings::class); - $wpdb_mock = $this->createMock(wpdb::class); - $mhMock = $this->createMock(MailingsHelpers::class); + // $vk_mailings_mock = $this->createMock(Mailings::class); + // $wpdb_mock = $this->createMock(wpdb::class); + // $mhMock = $this->createMock(MailingsHelpers::class); - $vk_mailings_mock->expects($this->once()) - ->method('get_distributions') - ->willReturn($twoSubjectCampaign); + // $vk_mailings_mock->expects($this->once()) + // ->method('get_distributions') + // ->willReturn($twoSubjectCampaign); - $mhMock->expects($this->once()) - ->method('get_fresh_subscribers_for_campaign') - ->willReturn(array( 0 => 6630470, 1 => 6630472,)); - - $vk_mailings_mock->expects($this->once()) - ->method('send') - ->willReturn(array ( 'ak_mailing_id' => 1)); - - $wpdb_mock->expects($this->once()) - ->method('query'); - - vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); - } - - public function test_mailingsHelpers_wp_query_posts_method(): void - { - $mailingsMock = new Mailings(); - $wpdb = new wpdb(); - $postObject = new stdClass(); - $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - - $mhMock= $this->getMockBuilder(MailingsHelpers::class) - ->setMethods(['wp_query_posts']) - ->getMock(); - - $mhMock->expects($this->once()) - ->method('wp_query_posts') - ->will($this->returnValue($postObject)); - - $mailingsMock->get_distributions($wpdb, $mhMock); - } - - public function test_get_fields(): void - { - $wpdb = new wpdb(); - $postObject = new stdClass(); - $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - - $mhMock= $this->getMockBuilder(MailingsHelpers::class) - ->setMethods(['wp_query_posts', 'getFields']) - ->getMock(); + // $mhMock->expects($this->once()) + // ->method('get_fresh_subscribers_for_campaign') + // ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + + // $vk_mailings_mock->expects($this->once()) + // ->method('send') + // ->willReturn(array ( 'ak_mailing_id' => 1)); + + // $wpdb_mock->expects($this->once()) + // ->method('query'); + + // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); + // } + + // public function test_mailingsHelpers_wp_query_posts_method(): void + // { + // $mailingsMock = new Mailings(); + // $wpdb = new wpdb(); + // $postObject = new stdClass(); + // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + + // $mhMock= $this->getMockBuilder(MailingsHelpers::class) + // ->setMethods(['wp_query_posts']) + // ->getMock(); + + // $mhMock->expects($this->once()) + // ->method('wp_query_posts') + // ->will($this->returnValue($postObject)); + + // $mailingsMock->get_distributions($wpdb, $mhMock); + // } + + // public function test_get_fields(): void + // { + // $wpdb = new wpdb(); + // $postObject = new stdClass(); + // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + + // $mhMock= $this->getMockBuilder(MailingsHelpers::class) + // ->setMethods(['wp_query_posts', 'getFields']) + // ->getMock(); - $mhMock->expects($this->once()) - ->method('wp_query_posts') - ->will($this->returnValue($postObject)); + // $mhMock->expects($this->once()) + // ->method('wp_query_posts') + // ->will($this->returnValue($postObject)); - $mhMock->expects($this->once()) - ->method('getFields'); + // $mhMock->expects($this->once()) + // ->method('getFields'); - $subject = new Mailings(); + // $subject = new Mailings(); - $subject->get_distributions($wpdb, $mhMock); - } - - public function test_mailingsHelpers_setUpCampaigns(): void - { - $wpdb = new wpdb(); - // $postObject = new stdClass(); - $postObject = array(0 => (object) ['ID' => '', 'post_title'=> '']); - $campaigns = array( 0 => array( 'fields'=> '', 'campaign_id' => '', 'subjects' => array())); - - $mhMock= $this->getMockBuilder(MailingsHelpers::class) - ->setMethods(['wp_query_posts', 'setUpCampaigns']) - ->getMock(); + // $subject->get_distributions($wpdb, $mhMock); + // } + + // public function test_mailingsHelpers_setUpCampaigns(): void + // { + // $wpdb = new wpdb(); + // // $postObject = new stdClass(); + // $postObject = array(0 => (object) ['ID' => '', 'post_title'=> '']); + // $campaigns = array( 0 => array( 'fields'=> '', 'campaign_id' => '', 'subjects' => array())); + + // $mhMock= $this->getMockBuilder(MailingsHelpers::class) + // ->setMethods(['wp_query_posts', 'setUpCampaigns']) + // ->getMock(); - $mhMock->expects($this->once()) - ->method('wp_query_posts') - ->will($this->returnValue($postObject)); + // $mhMock->expects($this->once()) + // ->method('wp_query_posts') + // ->will($this->returnValue($postObject)); - $mhMock->expects($this->once()) - ->method('setUpCampaigns') - ->will($this->returnValue($campaigns)); + // $mhMock->expects($this->once()) + // ->method('setUpCampaigns') + // ->will($this->returnValue($campaigns)); - $subject = new Mailings(); + // $subject = new Mailings(); - $subject->get_distributions($wpdb, $mhMock); - } - - public function test_mailingsHelpers_get_mailings_results_wpdb(): void - { - $wpdb = new wpdb(); - // $postObject = new stdClass(); - $postObject = array( 0 => array( 'campaign_id' => '1')); - - $mhMock= $this->getMockBuilder(MailingsHelpers::class) - ->setMethods(['get_mailings_results_wpdb']) - ->getMock(); + // $subject->get_distributions($wpdb, $mhMock); + // } + + // public function test_mailingsHelpers_get_mailings_results_wpdb(): void + // { + // $wpdb = new wpdb(); + // // $postObject = new stdClass(); + // $postObject = array( 0 => array( 'campaign_id' => '1')); + + // $mhMock= $this->getMockBuilder(MailingsHelpers::class) + // ->setMethods(['get_mailings_results_wpdb']) + // ->getMock(); - $mhMock->expects($this->once()) - ->method('get_mailings_results_wpdb') - ->will($this->returnValue($postObject)); + // $mhMock->expects($this->once()) + // ->method('get_mailings_results_wpdb') + // ->will($this->returnValue($postObject)); - $subject = new Mailings(); + // $subject = new Mailings(); - $subject->get_distributions($wpdb, $mhMock); - } - - public function test_mailings_distribution_post_method(): void - { - $mailings = new Mailings(); - $wpdb = new wpdb(); - $postObject = new stdClass(); + // $subject->get_distributions($wpdb, $mhMock); + // } + + // public function test_mailings_distribution_post_method(): void + // { + // $mailings = new Mailings(); + // $wpdb = new wpdb(); + // $postObject = new stdClass(); - $postObject->posts = array ( 0 => (object)(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), ); + // $postObject->posts = array ( 0 => (object)(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), ); - //** must update return value from campaigns + // //** must update return value from campaigns - $campaigns = array( - 263 => array( - "conversions" => 0, - "fields" => array( - "subjects" => array ( 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ),) - ), - "id" => 263, - "losses" => 0, - "sent" => 0, - "subjects" => array (), - "title" => "No wall testing", - "valid" => true - ) - ); - // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + // $campaigns = array( + // 263 => array( + // "conversions" => 0, + // "fields" => array( + // "subjects" => array ( 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ),) + // ), + // "id" => 263, + // "losses" => 0, + // "sent" => 0, + // "subjects" => array (), + // "title" => "No wall testing", + // "valid" => true + // ) + // ); + // // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - $mailings_stats = array(0 => [ - 'campaign_id' => 263, - 'conversions' => 0, - 'losses' => 0, - 'sent' => 0, - 'variation_subject' => 0 - ]); + // $mailings_stats = array(0 => [ + // 'campaign_id' => 263, + // 'conversions' => 0, + // 'losses' => 0, + // 'sent' => 0, + // 'variation_subject' => 0 + // ]); - $mhMock= $this->getMockBuilder(MailingsHelpers::class) - ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) - ->getMock(); + // $mhMock= $this->getMockBuilder(MailingsHelpers::class) + // ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) + // ->getMock(); - $mhMock->expects($this->once()) - ->method('wp_query_posts') - ->will($this->returnValue($postObject)); + // $mhMock->expects($this->once()) + // ->method('wp_query_posts') + // ->will($this->returnValue($postObject)); - $mhMock->expects($this->once()) - ->method('setUpCampaigns') - ->will($this->returnValue($campaigns)); + // $mhMock->expects($this->once()) + // ->method('setUpCampaigns') + // ->will($this->returnValue($campaigns)); - $mhMock->expects($this->once()) - ->method('get_mailings_results_wpdb') - ->will($this->returnValue($mailings_stats)); + // $mhMock->expects($this->once()) + // ->method('get_mailings_results_wpdb') + // ->will($this->returnValue($mailings_stats)); - $mhMock->expects($this->once()) - ->method('get_fresh_subscribers_for_campaign') - ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); + // $mhMock->expects($this->once()) + // ->method('get_fresh_subscribers_for_campaign') + // ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); - $this->assertEquals(array( - 'campaigns' => Array( - 0 => Array( - 'conversions' => 0, - 'fields' => Array( - 'subjects' => Array( - 0 => Array( - 'subject' => 'Tell Senate: No attacks on immigrants', - 'enabled' => true - ), - 1 => Array( - 'subject' => "Block Trump's attacks on immigrants", - 'enabled' => true - ) - ) - ), - 'id' => 263, - 'losses' => 0, - 'sent' => 0, - 'subjects' => Array( - 0 => Array( - 'conversions' => 0, - 'losses' => 0, - 'sent' => 0, - 'title' => 'Tell Senate: No attacks on immigrants', - 'rate' => 1, - 'share' => 1 - ) - ), - 'title' => 'No wall testing', - 'valid' => true, - 'rate' => 1, - 'share' => 1, - 'limit' => 4.0 - ) - ), - 'overall' => Array( - 'conversions' => 0, - 'losses' => 0, - 'sent' => 0, - 'boost' => 500, - 'rate' => 1 - ) - ), $mailings->get_distributions($wpdb, $mhMock)); - } + // $this->assertEquals(array( + // 'campaigns' => Array( + // 0 => Array( + // 'conversions' => 0, + // 'fields' => Array( + // 'subjects' => Array( + // 0 => Array( + // 'subject' => 'Tell Senate: No attacks on immigrants', + // 'enabled' => true + // ), + // 1 => Array( + // 'subject' => "Block Trump's attacks on immigrants", + // 'enabled' => true + // ) + // ) + // ), + // 'id' => 263, + // 'losses' => 0, + // 'sent' => 0, + // 'subjects' => Array( + // 0 => Array( + // 'conversions' => 0, + // 'losses' => 0, + // 'sent' => 0, + // 'title' => 'Tell Senate: No attacks on immigrants', + // 'rate' => 1, + // 'share' => 1 + // ) + // ), + // 'title' => 'No wall testing', + // 'valid' => true, + // 'rate' => 1, + // 'share' => 1, + // 'limit' => 4.0 + // ) + // ), + // 'overall' => Array( + // 'conversions' => 0, + // 'losses' => 0, + // 'sent' => 0, + // 'boost' => 500, + // 'rate' => 1 + // ) + // ), $mailings->get_distributions($wpdb, $mhMock)); + // } + + // public function test_get_distributions_one_campaign_method(): void + // { + // $mailings = new Mailings(); + // $wpdb = new wpdb(); + // $postObject = new stdClass(); + + // $postObject->posts = array (); + + // //** must update return value from campaigns + + // $campaigns = array( + // 263 => array( + // "conversions" => 0, + // "fields" => array( + // "subjects" => array ( 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ),) + // ), + // "id" => 263, + // "losses" => 0, + // "sent" => 0, + // "subjects" => array (), + // "title" => "No wall testing", + // "valid" => true + // ) + // ); + // // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + + // $mailings_stats = array ( + // 0 => array ( + // 'campaign_id' => '263', + // 'variation_subject' => '0', + // 'conversions' => '1', + // 'losses' => '0', + // 'sent' => '2', ), + // 1 => array ( + // 'campaign_id' => '263', + // 'variation_subject' => '1', + // 'conversions' => '1', + // 'losses' => '0', + // 'sent' => '2', ) + // ); + + // $mhMock= $this->getMockBuilder(MailingsHelpers::class) + // ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) + // ->getMock(); - public function test_get_distributions_method(): void + // $mhMock->expects($this->once()) + // ->method('wp_query_posts') + // ->will($this->returnValue($postObject)); + + // $mhMock->expects($this->once()) + // ->method('setUpCampaigns') + // ->will($this->returnValue($campaigns)); + + // $mhMock->expects($this->once()) + // ->method('get_mailings_results_wpdb') + // ->will($this->returnValue($mailings_stats)); + + // $mhMock->expects($this->once()) + // ->method('get_fresh_subscribers_for_campaign') + // ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); + + // $this->assertEquals(array( + // 'campaigns' => Array( + // 0 => Array( + // 'conversions' => 2, + // 'fields' => Array( + // 'subjects' => Array( + // 0 => Array( + // 'subject' => 'Tell Senate: No attacks on immigrants', + // 'enabled' => true + // ), + // 1 => Array( + // 'subject' => "Block Trump's attacks on immigrants", + // 'enabled' => true + // ) + // ) + // ), + // 'id' => 263, + // 'losses' => 0, + // 'sent' => 4, + // 'subjects' => Array( + // 0 => Array( + // 'conversions' => 1, + // 'losses' => 0, + // 'sent' => 2, + // 'title' => 'Tell Senate: No attacks on immigrants', + // 'rate' => 0.994055523936002, + // 'share' => 0.5 + // ), + // 1 => Array( + // 'conversions' => 1, + // 'losses' => 0, + // 'sent' => 2, + // 'title' => "Block Trump's attacks on immigrants", + // 'rate' => 0.994055523936002, + // 'share' => 0.5 + // ) + // ), + // 'title' => 'No wall testing', + // 'valid' => true, + // 'rate' => 0.9920949861426052, + // 'share' => 1.0, + // 'limit' => 4.0 + // ) + // ), + // 'overall' => Array( + // 'conversions' => 2, + // 'losses' => 0, + // 'sent' => 4, + // 'boost' => 500, + // 'rate' => 0.996031746031746 + // ) + // ), $mailings->get_distributions($wpdb, $mhMock)); + // } + public function test_get_distributions_one_campaign_one_greater_conversions_method(): void { $mailings = new Mailings(); $wpdb = new wpdb(); @@ -310,20 +424,27 @@ public function test_get_distributions_method(): void "id" => 263, "losses" => 0, "sent" => 0, - "subjects" => array ( 0 => array ( 'conversions' => 0, 'losses' => 0, 'sent' => 0, 'title' => 'Tell Senate: No attacks on immigrants', 'rate' => 1, 'share' => 0.5, ), 1 => array ( 'conversions' => 0, 'losses' => 0, 'sent' => 0, 'title' => "Block Trump's attacks on immigrants", 'rate' => 1, 'share' => 0.5)), + "subjects" => array (), "title" => "No wall testing", "valid" => true ) ); // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - $mailings_stats = array(0 => [ - 'campaign_id' => 263, - 'conversions' => 2, - 'losses' => 2, - 'sent' => 4, - 'variation_subject' => 0 - ]); + $mailings_stats = array ( + 0 => array ( + 'campaign_id' => '263', + 'variation_subject' => '0', + 'conversions' => '2', + 'losses' => '0', + 'sent' => '2', ), + 1 => array ( + 'campaign_id' => '263', + 'variation_subject' => '1', + 'conversions' => '0', + 'losses' => '0', + 'sent' => '2', ) + ); $mhMock= $this->getMockBuilder(MailingsHelpers::class) ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) @@ -362,39 +483,39 @@ public function test_get_distributions_method(): void ) ), 'id' => 263, - 'losses' => 2, + 'losses' => 0, 'sent' => 4, 'subjects' => Array( 0 => Array( 'conversions' => 2, - 'losses' => 2, - 'sent' => 4, + 'losses' => 0, + 'sent' => 2, 'title' => 'Tell Senate: No attacks on immigrants', - 'rate' => 0.9841899722852103, - 'share' => 0.49800796812749004 + 'rate' => 0.996047555808512, + 'share' => 0.501001972135632 ), 1 => Array( 'conversions' => 0, 'losses' => 0, - 'sent' => 0, + 'sent' => 2, 'title' => "Block Trump's attacks on immigrants", 'rate' => 0.9920634920634921, - 'share' => 0.50199203187251 + 'share' => 0.498998027864368 ) ), 'title' => 'No wall testing', 'valid' => true, - 'rate' => 0.9841899722852103, + 'rate' => 0.9920949861426052, 'share' => 1.0, 'limit' => 4.0 ) ), 'overall' => Array( 'conversions' => 2, - 'losses' => 2, + 'losses' => 0, 'sent' => 4, 'boost' => 500, - 'rate' => 0.9920634920634921 + 'rate' => 0.996031746031746 ) ), $mailings->get_distributions($wpdb, $mhMock)); } From 9b9a5e278170d4d47c6febad89ba73b19a22a53e Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Wed, 31 Oct 2018 12:09:13 -0700 Subject: [PATCH 45/65] Add send function test. --- addons/tests/mailing_test.php | 750 +++++++++++++++++++--------------- 1 file changed, 425 insertions(+), 325 deletions(-) diff --git a/addons/tests/mailing_test.php b/addons/tests/mailing_test.php index 9d8c3eb..aed5eb3 100644 --- a/addons/tests/mailing_test.php +++ b/addons/tests/mailing_test.php @@ -32,379 +32,391 @@ function query(){} final class mailingsClass extends TestCase { - // public function test_vk_mailings_create_new_mailings_action_two_campaigns(): void - // { - // global $TwoCampaigns; + public function test_vk_mailings_create_new_mailings_action_two_campaigns(): void + { + global $TwoCampaigns; - // $vk_mailings_mock = $this->createMock(Mailings::class); - // $wpdb_mock = $this->createMock(wpdb::class); - // $mhMock= $this->createMock(MailingsHelpers::class); + $vk_mailings_mock = $this->createMock(Mailings::class); + $wpdb_mock = $this->createMock(wpdb::class); + $mhMock= $this->createMock(MailingsHelpers::class); - // $vk_mailings_mock->expects($this->once()) - // ->method('get_distributions') - // ->willReturn($TwoCampaigns); + $vk_mailings_mock->expects($this->once()) + ->method('get_distributions') + ->willReturn($TwoCampaigns); - // $mhMock->expects($this->exactly(2)) - // ->method('get_fresh_subscribers_for_campaign') - // ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + $mhMock->expects($this->exactly(2)) + ->method('get_fresh_subscribers_for_campaign') + ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + + $mhMock->expects($this->once()) + ->method('getoption') + ->willReturn(4); - // $vk_mailings_mock->expects($this->exactly(3)) - // ->method('send'); + $mhMock->expects($this->exactly(3)) + ->method('send'); - // $wpdb_mock->expects($this->exactly(1)) - // ->method('query'); + $wpdb_mock->expects($this->exactly(1)) + ->method('query'); - // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); - // } + vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); + } - // public function test_vk_mailings_create_new_mailings_action_one_campaigns(): void - // { - // global $singleCampaign; + public function test_vk_mailings_create_new_mailings_action_one_campaigns(): void + { + global $singleCampaign; - // $vk_mailings_mock = $this->createMock(Mailings::class); - // $wpdb_mock = $this->createMock(wpdb::class); - // $mhMock = $this->createMock(MailingsHelpers::class); + $vk_mailings_mock = $this->createMock(Mailings::class); + $wpdb_mock = $this->createMock(wpdb::class); + $mhMock = $this->createMock(MailingsHelpers::class); - // $vk_mailings_mock->expects($this->once()) - // ->method('get_distributions') - // ->willReturn($singleCampaign); + $vk_mailings_mock->expects($this->once()) + ->method('get_distributions') + ->willReturn($singleCampaign); - // $mhMock->expects($this->exactly(1)) - // ->method('get_fresh_subscribers_for_campaign') - // ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + $mhMock->expects($this->exactly(1)) + ->method('get_fresh_subscribers_for_campaign') + ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + + $mhMock->expects($this->once()) + ->method('getoption') + ->willReturn(4); - // $vk_mailings_mock->expects($this->exactly(2)) - // ->method('send'); + $mhMock->expects($this->exactly(2)) + ->method('send'); - // $wpdb_mock->expects($this->exactly(1)) - // ->method('query'); + $wpdb_mock->expects($this->exactly(1)) + ->method('query'); - // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); - // } + vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); + } - // public function test_get_distribution_two_subject_campaign(): void - // { - // global $twoSubjectCampaign; + public function test_get_distribution_two_subject_campaign(): void + { + global $twoSubjectCampaign; - // $vk_mailings_mock = $this->createMock(Mailings::class); - // $wpdb_mock = $this->createMock(wpdb::class); - // $mhMock = $this->createMock(MailingsHelpers::class); + $vk_mailings_mock = $this->createMock(Mailings::class); + $wpdb_mock = $this->createMock(wpdb::class); + $mhMock = $this->createMock(MailingsHelpers::class); - // $vk_mailings_mock->expects($this->once()) - // ->method('get_distributions') - // ->willReturn($twoSubjectCampaign); + $vk_mailings_mock->expects($this->once()) + ->method('get_distributions') + ->willReturn($twoSubjectCampaign); - // $mhMock->expects($this->once()) - // ->method('get_fresh_subscribers_for_campaign') - // ->willReturn(array( 0 => 6630470, 1 => 6630472,)); - - // $vk_mailings_mock->expects($this->once()) - // ->method('send') - // ->willReturn(array ( 'ak_mailing_id' => 1)); - - // $wpdb_mock->expects($this->once()) - // ->method('query'); - - // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); - // } - - // public function test_mailingsHelpers_wp_query_posts_method(): void - // { - // $mailingsMock = new Mailings(); - // $wpdb = new wpdb(); - // $postObject = new stdClass(); - // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - - // $mhMock= $this->getMockBuilder(MailingsHelpers::class) - // ->setMethods(['wp_query_posts']) - // ->getMock(); - - // $mhMock->expects($this->once()) - // ->method('wp_query_posts') - // ->will($this->returnValue($postObject)); - - // $mailingsMock->get_distributions($wpdb, $mhMock); - // } - - // public function test_get_fields(): void - // { - // $wpdb = new wpdb(); - // $postObject = new stdClass(); - // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - - // $mhMock= $this->getMockBuilder(MailingsHelpers::class) - // ->setMethods(['wp_query_posts', 'getFields']) - // ->getMock(); + $mhMock->expects($this->once()) + ->method('get_fresh_subscribers_for_campaign') + ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + + $mhMock->expects($this->once()) + ->method('getoption') + ->willReturn(4); + + $mhMock->expects($this->once()) + ->method('send') + ->willReturn(array ( 'ak_mailing_id' => 1)); - // $mhMock->expects($this->once()) - // ->method('wp_query_posts') - // ->will($this->returnValue($postObject)); + $wpdb_mock->expects($this->once()) + ->method('query'); + + vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); + } - // $mhMock->expects($this->once()) - // ->method('getFields'); + public function test_mailingsHelpers_wp_query_posts_method(): void + { + $mailingsMock = new Mailings(); + $wpdb = new wpdb(); + $postObject = new stdClass(); + $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['wp_query_posts']) + ->getMock(); + + $mhMock->expects($this->once()) + ->method('wp_query_posts') + ->will($this->returnValue($postObject)); + + $mailingsMock->get_distributions($wpdb, $mhMock); + } + + public function test_get_fields(): void + { + $wpdb = new wpdb(); + $postObject = new stdClass(); + $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['wp_query_posts', 'getFields']) + ->getMock(); + + $mhMock->expects($this->once()) + ->method('wp_query_posts') + ->will($this->returnValue($postObject)); + + $mhMock->expects($this->once()) + ->method('getFields'); - // $subject = new Mailings(); + $subject = new Mailings(); - // $subject->get_distributions($wpdb, $mhMock); - // } - - // public function test_mailingsHelpers_setUpCampaigns(): void - // { - // $wpdb = new wpdb(); - // // $postObject = new stdClass(); - // $postObject = array(0 => (object) ['ID' => '', 'post_title'=> '']); - // $campaigns = array( 0 => array( 'fields'=> '', 'campaign_id' => '', 'subjects' => array())); - - // $mhMock= $this->getMockBuilder(MailingsHelpers::class) - // ->setMethods(['wp_query_posts', 'setUpCampaigns']) - // ->getMock(); + $subject->get_distributions($wpdb, $mhMock); + } + + public function test_mailingsHelpers_setUpCampaigns(): void + { + $wpdb = new wpdb(); + // $postObject = new stdClass(); + $postObject = array(0 => (object) ['ID' => '', 'post_title'=> '']); + $campaigns = array( 0 => array( 'fields'=> '', 'campaign_id' => '', 'subjects' => array())); + + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['wp_query_posts', 'setUpCampaigns']) + ->getMock(); - // $mhMock->expects($this->once()) - // ->method('wp_query_posts') - // ->will($this->returnValue($postObject)); + $mhMock->expects($this->once()) + ->method('wp_query_posts') + ->will($this->returnValue($postObject)); - // $mhMock->expects($this->once()) - // ->method('setUpCampaigns') - // ->will($this->returnValue($campaigns)); + $mhMock->expects($this->once()) + ->method('setUpCampaigns') + ->will($this->returnValue($campaigns)); - // $subject = new Mailings(); + $subject = new Mailings(); - // $subject->get_distributions($wpdb, $mhMock); - // } - - // public function test_mailingsHelpers_get_mailings_results_wpdb(): void - // { - // $wpdb = new wpdb(); - // // $postObject = new stdClass(); - // $postObject = array( 0 => array( 'campaign_id' => '1')); - - // $mhMock= $this->getMockBuilder(MailingsHelpers::class) - // ->setMethods(['get_mailings_results_wpdb']) - // ->getMock(); + $subject->get_distributions($wpdb, $mhMock); + } + + public function test_mailingsHelpers_get_mailings_results_wpdb(): void + { + $wpdb = new wpdb(); + // $postObject = new stdClass(); + $postObject = array( 0 => array( 'campaign_id' => '1')); + + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['get_mailings_results_wpdb']) + ->getMock(); - // $mhMock->expects($this->once()) - // ->method('get_mailings_results_wpdb') - // ->will($this->returnValue($postObject)); + $mhMock->expects($this->once()) + ->method('get_mailings_results_wpdb') + ->will($this->returnValue($postObject)); - // $subject = new Mailings(); + $subject = new Mailings(); - // $subject->get_distributions($wpdb, $mhMock); - // } - - // public function test_mailings_distribution_post_method(): void - // { - // $mailings = new Mailings(); - // $wpdb = new wpdb(); - // $postObject = new stdClass(); + $subject->get_distributions($wpdb, $mhMock); + } + + public function test_mailings_distribution_post_method(): void + { + $mailings = new Mailings(); + $wpdb = new wpdb(); + $postObject = new stdClass(); - // $postObject->posts = array ( 0 => (object)(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), ); + $postObject->posts = array ( 0 => (object)(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), ); - // //** must update return value from campaigns + //** must update return value from campaigns - // $campaigns = array( - // 263 => array( - // "conversions" => 0, - // "fields" => array( - // "subjects" => array ( 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ),) - // ), - // "id" => 263, - // "losses" => 0, - // "sent" => 0, - // "subjects" => array (), - // "title" => "No wall testing", - // "valid" => true - // ) - // ); - // // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + $campaigns = array( + 263 => array( + "conversions" => 0, + "fields" => array( + "subjects" => array ( 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ),) + ), + "id" => 263, + "losses" => 0, + "sent" => 0, + "subjects" => array (), + "title" => "No wall testing", + "valid" => true + ) + ); + // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - // $mailings_stats = array(0 => [ - // 'campaign_id' => 263, - // 'conversions' => 0, - // 'losses' => 0, - // 'sent' => 0, - // 'variation_subject' => 0 - // ]); + $mailings_stats = array(0 => [ + 'campaign_id' => 263, + 'conversions' => 0, + 'losses' => 0, + 'sent' => 0, + 'variation_subject' => 0 + ]); - // $mhMock= $this->getMockBuilder(MailingsHelpers::class) - // ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) - // ->getMock(); + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) + ->getMock(); - // $mhMock->expects($this->once()) - // ->method('wp_query_posts') - // ->will($this->returnValue($postObject)); + $mhMock->expects($this->once()) + ->method('wp_query_posts') + ->will($this->returnValue($postObject)); - // $mhMock->expects($this->once()) - // ->method('setUpCampaigns') - // ->will($this->returnValue($campaigns)); + $mhMock->expects($this->once()) + ->method('setUpCampaigns') + ->will($this->returnValue($campaigns)); - // $mhMock->expects($this->once()) - // ->method('get_mailings_results_wpdb') - // ->will($this->returnValue($mailings_stats)); + $mhMock->expects($this->once()) + ->method('get_mailings_results_wpdb') + ->will($this->returnValue($mailings_stats)); - // $mhMock->expects($this->once()) - // ->method('get_fresh_subscribers_for_campaign') - // ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); + $mhMock->expects($this->once()) + ->method('get_fresh_subscribers_for_campaign') + ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); - // $this->assertEquals(array( - // 'campaigns' => Array( - // 0 => Array( - // 'conversions' => 0, - // 'fields' => Array( - // 'subjects' => Array( - // 0 => Array( - // 'subject' => 'Tell Senate: No attacks on immigrants', - // 'enabled' => true - // ), - // 1 => Array( - // 'subject' => "Block Trump's attacks on immigrants", - // 'enabled' => true - // ) - // ) - // ), - // 'id' => 263, - // 'losses' => 0, - // 'sent' => 0, - // 'subjects' => Array( - // 0 => Array( - // 'conversions' => 0, - // 'losses' => 0, - // 'sent' => 0, - // 'title' => 'Tell Senate: No attacks on immigrants', - // 'rate' => 1, - // 'share' => 1 - // ) - // ), - // 'title' => 'No wall testing', - // 'valid' => true, - // 'rate' => 1, - // 'share' => 1, - // 'limit' => 4.0 - // ) - // ), - // 'overall' => Array( - // 'conversions' => 0, - // 'losses' => 0, - // 'sent' => 0, - // 'boost' => 500, - // 'rate' => 1 - // ) - // ), $mailings->get_distributions($wpdb, $mhMock)); - // } - - // public function test_get_distributions_one_campaign_method(): void - // { - // $mailings = new Mailings(); - // $wpdb = new wpdb(); - // $postObject = new stdClass(); + $this->assertEquals(array( + 'campaigns' => Array( + 0 => Array( + 'conversions' => 0, + 'fields' => Array( + 'subjects' => Array( + 0 => Array( + 'subject' => 'Tell Senate: No attacks on immigrants', + 'enabled' => true + ), + 1 => Array( + 'subject' => "Block Trump's attacks on immigrants", + 'enabled' => true + ) + ) + ), + 'id' => 263, + 'losses' => 0, + 'sent' => 0, + 'subjects' => Array( + 0 => Array( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 0, + 'title' => 'Tell Senate: No attacks on immigrants', + 'rate' => 1, + 'share' => 1 + ) + ), + 'title' => 'No wall testing', + 'valid' => true, + 'rate' => 1, + 'share' => 1, + 'limit' => 4.0 + ) + ), + 'overall' => Array( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 0, + 'boost' => 500, + 'rate' => 1 + ) + ), $mailings->get_distributions($wpdb, $mhMock)); + } + + public function test_get_distributions_one_campaign_method(): void + { + $mailings = new Mailings(); + $wpdb = new wpdb(); + $postObject = new stdClass(); - // $postObject->posts = array (); + $postObject->posts = array (); - // //** must update return value from campaigns + //** must update return value from campaigns - // $campaigns = array( - // 263 => array( - // "conversions" => 0, - // "fields" => array( - // "subjects" => array ( 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ),) - // ), - // "id" => 263, - // "losses" => 0, - // "sent" => 0, - // "subjects" => array (), - // "title" => "No wall testing", - // "valid" => true - // ) - // ); - // // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + $campaigns = array( + 263 => array( + "conversions" => 0, + "fields" => array( + "subjects" => array ( 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ),) + ), + "id" => 263, + "losses" => 0, + "sent" => 0, + "subjects" => array (), + "title" => "No wall testing", + "valid" => true + ) + ); + // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - // $mailings_stats = array ( - // 0 => array ( - // 'campaign_id' => '263', - // 'variation_subject' => '0', - // 'conversions' => '1', - // 'losses' => '0', - // 'sent' => '2', ), - // 1 => array ( - // 'campaign_id' => '263', - // 'variation_subject' => '1', - // 'conversions' => '1', - // 'losses' => '0', - // 'sent' => '2', ) - // ); + $mailings_stats = array ( + 0 => array ( + 'campaign_id' => '263', + 'variation_subject' => '0', + 'conversions' => '1', + 'losses' => '0', + 'sent' => '2', ), + 1 => array ( + 'campaign_id' => '263', + 'variation_subject' => '1', + 'conversions' => '1', + 'losses' => '0', + 'sent' => '2', ) + ); - // $mhMock= $this->getMockBuilder(MailingsHelpers::class) - // ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) - // ->getMock(); + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) + ->getMock(); - // $mhMock->expects($this->once()) - // ->method('wp_query_posts') - // ->will($this->returnValue($postObject)); + $mhMock->expects($this->once()) + ->method('wp_query_posts') + ->will($this->returnValue($postObject)); - // $mhMock->expects($this->once()) - // ->method('setUpCampaigns') - // ->will($this->returnValue($campaigns)); + $mhMock->expects($this->once()) + ->method('setUpCampaigns') + ->will($this->returnValue($campaigns)); - // $mhMock->expects($this->once()) - // ->method('get_mailings_results_wpdb') - // ->will($this->returnValue($mailings_stats)); + $mhMock->expects($this->once()) + ->method('get_mailings_results_wpdb') + ->will($this->returnValue($mailings_stats)); - // $mhMock->expects($this->once()) - // ->method('get_fresh_subscribers_for_campaign') - // ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); + $mhMock->expects($this->once()) + ->method('get_fresh_subscribers_for_campaign') + ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); - // $this->assertEquals(array( - // 'campaigns' => Array( - // 0 => Array( - // 'conversions' => 2, - // 'fields' => Array( - // 'subjects' => Array( - // 0 => Array( - // 'subject' => 'Tell Senate: No attacks on immigrants', - // 'enabled' => true - // ), - // 1 => Array( - // 'subject' => "Block Trump's attacks on immigrants", - // 'enabled' => true - // ) - // ) - // ), - // 'id' => 263, - // 'losses' => 0, - // 'sent' => 4, - // 'subjects' => Array( - // 0 => Array( - // 'conversions' => 1, - // 'losses' => 0, - // 'sent' => 2, - // 'title' => 'Tell Senate: No attacks on immigrants', - // 'rate' => 0.994055523936002, - // 'share' => 0.5 - // ), - // 1 => Array( - // 'conversions' => 1, - // 'losses' => 0, - // 'sent' => 2, - // 'title' => "Block Trump's attacks on immigrants", - // 'rate' => 0.994055523936002, - // 'share' => 0.5 - // ) - // ), - // 'title' => 'No wall testing', - // 'valid' => true, - // 'rate' => 0.9920949861426052, - // 'share' => 1.0, - // 'limit' => 4.0 - // ) - // ), - // 'overall' => Array( - // 'conversions' => 2, - // 'losses' => 0, - // 'sent' => 4, - // 'boost' => 500, - // 'rate' => 0.996031746031746 - // ) - // ), $mailings->get_distributions($wpdb, $mhMock)); - // } + $this->assertEquals(array( + 'campaigns' => Array( + 0 => Array( + 'conversions' => 2, + 'fields' => Array( + 'subjects' => Array( + 0 => Array( + 'subject' => 'Tell Senate: No attacks on immigrants', + 'enabled' => true + ), + 1 => Array( + 'subject' => "Block Trump's attacks on immigrants", + 'enabled' => true + ) + ) + ), + 'id' => 263, + 'losses' => 0, + 'sent' => 4, + 'subjects' => Array( + 0 => Array( + 'conversions' => 1, + 'losses' => 0, + 'sent' => 2, + 'title' => 'Tell Senate: No attacks on immigrants', + 'rate' => 0.994055523936002, + 'share' => 0.5 + ), + 1 => Array( + 'conversions' => 1, + 'losses' => 0, + 'sent' => 2, + 'title' => "Block Trump's attacks on immigrants", + 'rate' => 0.994055523936002, + 'share' => 0.5 + ) + ), + 'title' => 'No wall testing', + 'valid' => true, + 'rate' => 0.9920949861426052, + 'share' => 1.0, + 'limit' => 4.0 + ) + ), + 'overall' => Array( + 'conversions' => 2, + 'losses' => 0, + 'sent' => 4, + 'boost' => 500, + 'rate' => 0.996031746031746 + ) + ), $mailings->get_distributions($wpdb, $mhMock)); + } public function test_get_distributions_one_campaign_one_greater_conversions_method(): void { $mailings = new Mailings(); @@ -519,6 +531,94 @@ public function test_get_distributions_one_campaign_one_greater_conversions_meth ) ), $mailings->get_distributions($wpdb, $mhMock)); } + + public function test_send_function_with_one_campaign_one_subject_greater_conversion(): void + { + global $singleCampaign; + $vk_mailings_mock = $this->createMock(Mailings::class); + $wpdb_mock = $this->createMock(wpdb::class); + $mhMock = $this->createMock(MailingsHelpers::class); + + $vk_mailings_mock->expects($this->once()) + ->method('get_distributions') + ->willReturn(Array( + 'campaigns' => Array( + 0 => Array( + 'conversions' => 2, + 'fields' => Array( + 'subjects' => Array( + 0 => Array( + 'subject' => 'Tell Senate: No attacks on immigrants', + 'enabled' => true + ), + 1 => Array( + 'subject' => "Block Trump's attacks on immigrants", + 'enabled' => true + ) + ), + 'from_line' => '41', + 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

Already he has banned Muslims and refugees, threatened sanctuary cities and criminalized millions of undocumented immigrants. His Immigration and Custom Enforcement (ICE) and Border Patrol agents are separating parents from their children, deporting innocent people and spreading fear in immigrant communities across the country. The budget he just released slashes funding for the social safety net in order to spend billions of dollars building his wall, increasing his deportation force and expediting deportations.

Senate Democrats have the power to block funding for Trump’s wall and his massive deportation force. Recent reports show they may be willing to fight to stop Trump’s hateful agenda. Can you help make sure they stand strong?

We cannot let any Senate Democrat cave to political pressure from the right-wing and give Republicans the 60 votes they need to fund Trump’s hate. That is where progressives come in. We have to show Democratic senators that we will have their backs if they act boldly and that we will hold them accountable if they fail to stand up for immigrants.

Tell Senate Democrats: Block funding for Trump’s attacks on immigrants. Click here to sign the petition.

Trump wants to harass, deport and reject people who have come or are dreaming of coming to the United States seeking refuge from conflict, lives free from persecution and better economic opportunities for their families. For as long as Trump is president and extremist right-wing Republicans are willing to enable his agenda, Senate Democrats will have a choice: enable Trump’s agenda of criminalization and deportations, or resist and obstruct his racist regime at every turn. It is up to make sure they do the right thing.

Tell Senate Democrats: Block funding for Trump’s attacks on immigrants.

', + 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + 'salutation' => '{{ user.first_name|default:"Hi" }},', + + ), + 'id' => 263, + 'losses' => 0, + 'sent' => 4, + 'subjects' => Array( + 0 => Array( + 'conversions' => 2, + 'losses' => 0, + 'sent' => 2, + 'title' => 'Tell Senate: No attacks on immigrants', + 'rate' => 0.996047555808512, + 'share' => 0.501001972135632 + ), + 1 => Array( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 2, + 'title' => "Block Trump's attacks on immigrants", + 'rate' => 0.9920634920634921, + 'share' => 0.498998027864368 + ) + ), + 'title' => 'No wall testing', + 'valid' => true, + 'rate' => 0.9920949861426052, + 'share' => 1.0, + 'limit' => 4.0 + ) + ), + 'overall' => Array( + 'conversions' => 2, + 'losses' => 0, + 'sent' => 4, + 'boost' => 500, + 'rate' => 0.996031746031746 + ) + ) + ); + + $mhMock->expects($this->once()) + ->method('getoption') + ->willReturn(3.5714285714285716); + + $mhMock->expects($this->once()) + ->method('get_url') + ->willReturn('https://victorykit.local/c/no-wall-testing-2/'); + + + $mhMock->expects($this->exactly(1)) + ->method('get_fresh_subscribers_for_campaign') + ->willReturn(array( 0 => 6630470, 1 => 6630472, 2 => 6630478, + 3 => 6630479)); + + $mhMock->expects($this->exactly(2)) + ->method('send'); + + vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); + } } From 961e9a21326ac471dbb050ca139dcc3db10f6fb2 Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Wed, 31 Oct 2018 12:10:33 -0700 Subject: [PATCH 46/65] Moved render, sent and get_options function to mailing helpers class. --- addons/mailings.php | 102 ++++++-------------------------------------- 1 file changed, 12 insertions(+), 90 deletions(-) diff --git a/addons/mailings.php b/addons/mailings.php index f99e501..68aa91f 100644 --- a/addons/mailings.php +++ b/addons/mailings.php @@ -63,7 +63,6 @@ function get_distributions($wpdb_mock, $mh_mock) continue; } - $overall['conversions'] += $mailing['conversions']; $overall['losses'] += $mailing['losses']; $overall['sent'] += $mailing['sent']; @@ -73,12 +72,15 @@ function get_distributions($wpdb_mock, $mh_mock) $campaigns[$id]['sent'] += $mailing['sent']; $subject = $mailing['variation_subject']; + $campaigns[$id]['subjects'][$subject] = array( 'conversions' => +$mailing['conversions'], 'losses' => +$mailing['losses'], 'sent' => +$mailing['sent'], ); } + // $campaignsPrint = trim(preg_replace('/\s+/', ' ',var_export( $campaigns, true))); + // error_log('$$$line 84 value of campaigns after loop '.$campaignsPrint); // This allows brand new campaigns to have a chance to succeed. // It probably is not needed if we are going to be sending each new campaign to more than several hundred people, @@ -270,88 +272,6 @@ function get_mailing_stats_from_ak($ak_mailing_id) 'sent' => $sent, ); } - - function render($params) - { - $html = file_get_contents(dirname(__FILE__) . '/email-template.html'); - $html = str_replace('$body', $params['body'], $html); // Turn newlines into in body - $html = str_replace('$petition_headline', $params['petition_headline'], $html); - $html = str_replace('$salutation', $params['salutation'], $html); - $html = str_replace('$url', $params['url'], $html); - - if (!isset($params['wrap'])) { - return $html; - } - - $wrapper = file_get_contents(dirname(__FILE__) . '/email-template-wrapper.html'); - $html = str_replace('$content', $html, $wrapper); - - return $html; - } - - function send($params) - { - // Verify targeting - if ( - !$params['subscribers'] - || - !is_array($params['subscribers']) - || - count($params['subscribers']) < 1 - ) { - return; - } - // $distributions_results = trim(preg_replace('/\s+/', ' ',var_export( $params, true))); - // error_log('@@@ line 359 '.$distributions_results); - // Render - $html = $this->render($params); - - global $ak; - error_log('@@@ limit_per_day variable '.$ak); - $response = $ak->request(array( - 'path' => 'mailer', - 'method' => 'post', - 'data' => array( - 'fromline' => "/rest/v1/fromline/{$params['from_line']}/", - 'subjects' => array($params['subject']), - 'notes' => 'Generated by VictoryKit', - 'emailwrapper' => 27, // Demand Progress wrapper - 'includes' => array( - 'lists' => array(VK_LIST_ID), // VK list. TODO: store this as a constant somewhere - 'users' => $params['subscribers'], // Subscribers - ), - 'limit' => $params['limit'], // Limit users per mailing - // 'excludes' => array( - // 'mailings' => $mailings, // Array of mailing IDs, for avoiding multiple sends - // ), - 'tags' => array('victorykit'), - 'html' => $html, - 'sort_by' => 'random', - ), - )); - - $location = $response['headers']->getAll()['location']; - preg_match('%/(\d+)/$%', $location, $matches); - $ak_mailing_id = +$matches[1]; - - $response = $ak->request(array( - 'path' => "mailer/$ak_mailing_id/rebuild", - 'method' => 'post', - 'data' => array(), - )); - - $response['ak_mailing_id'] = $ak_mailing_id; - - global $wpdb; - $wpdb->insert('vk_mailing', array( - 'ak_mailing_id' => $ak_mailing_id, - 'campaign_id' => $params['campaign_id'], - 'status' => 'rebuilding', - 'variation_subject' => $params['variation_subject'], - )); - - return $response; - } } /**************** Actions *****************/ @@ -466,7 +386,7 @@ function vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $ $mh = $mhMock; } - $limit_per_day = get_option('subscribed_users') / 7; + $limit_per_day = $mh->getoption(); $distributions = $vk_mailings->get_distributions($vk_mailings_mock, $wpdb_mock, $mhMock); // $distributions_results = trim(preg_replace('/\s+/', ' ',var_export( $distributions, true))); @@ -475,7 +395,7 @@ function vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $ foreach ($distributions['campaigns'] as $campaign) { $id = $campaign['id']; $fields = $campaign['fields']; - $url = get_permalink($id); + $url = $mh->get_url($id); //need to print out why $limit_per_campaign NAN -> this is breaking everything // $allsql = trim(preg_replace('/\s+/', ' ',var_export( $campaign, true))); // error_log('%%%%%%%%%%% '); @@ -491,16 +411,16 @@ function vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $ if (count($fresh_ids) == 0) { continue; } - + // Send mailing for each enabled subject foreach ($campaign['subjects'] as $index => $subject) { $share = round($subject['share'] * $limit_per_campaign); - + // Skip disabled subjects if ($share == 0) { continue; } - + // Claim IDs $subscribers = array_splice($fresh_ids, 0, $share); @@ -517,17 +437,19 @@ function vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $ 'url' => $url, 'variation_subject' => $index, ); - + // print('######'); + // var_dump($params); // $allsql = trim(preg_replace('/\s+/', ' ',var_export( $params, true))); // error_log('s line 563: '.$allsql); // $sCamp = trim(preg_replace('/\s+/', ' ',var_export( $params, true))); // error_log('$$$$$$$$'); // error_log('line 550 $subject variable '.$sCamp); - $response = $vk_mailings->send($params); + $response = $mh->send($params); // Save mailing records for VK $ak_mailing_id = $response['ak_mailing_id']; $subscriber_chunks = array_chunk($subscribers, 1000); // Chunk so SQL queries dont get too big + foreach ($subscriber_chunks as $subscriber_chunk) { $values = array(); From 10346369efdf87d93d0e7de175e0c2541f3cc53f Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Wed, 31 Oct 2018 12:11:19 -0700 Subject: [PATCH 47/65] Add send, render and getoptions functions. --- addons/mockClasses/mailingsHelpers.php | 90 ++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/addons/mockClasses/mailingsHelpers.php b/addons/mockClasses/mailingsHelpers.php index f29af2e..da86b85 100644 --- a/addons/mockClasses/mailingsHelpers.php +++ b/addons/mockClasses/mailingsHelpers.php @@ -99,6 +99,96 @@ function get_fresh_subscribers_for_campaign($campaign_id, $limit, $wpdb_mock) return $ids; } + + function render($params) + { + $html = file_get_contents(dirname(__FILE__) . '/email-template.html'); + $html = str_replace('$body', $params['body'], $html); // Turn newlines into in body + $html = str_replace('$petition_headline', $params['petition_headline'], $html); + $html = str_replace('$salutation', $params['salutation'], $html); + $html = str_replace('$url', $params['url'], $html); + + if (!isset($params['wrap'])) { + return $html; + } + + $wrapper = file_get_contents(dirname(__FILE__) . '/email-template-wrapper.html'); + $html = str_replace('$content', $html, $wrapper); + + return $html; + } + + function send($params) + { + // Verify targeting + if ( + !$params['subscribers'] + || + !is_array($params['subscribers']) + || + count($params['subscribers']) < 1 + ) { + return; + } + // $distributions_results = trim(preg_replace('/\s+/', ' ',var_export( $params, true))); + // error_log('@@@ line 359 '.$distributions_results); + // Render + $html = $this->render($params); + + global $ak; + // error_log('@@@ limit_per_day variable '.$ak); + $response = $ak->request(array( + 'path' => 'mailer', + 'method' => 'post', + 'data' => array( + 'fromline' => "/rest/v1/fromline/{$params['from_line']}/", + 'subjects' => array($params['subject']), + 'notes' => 'Generated by VictoryKit', + 'emailwrapper' => 27, // Demand Progress wrapper + 'includes' => array( + 'lists' => array(VK_LIST_ID), // VK list. TODO: store this as a constant somewhere + 'users' => $params['subscribers'], // Subscribers + ), + 'limit' => $params['limit'], // Limit users per mailing + // 'excludes' => array( + // 'mailings' => $mailings, // Array of mailing IDs, for avoiding multiple sends + // ), + 'tags' => array('victorykit'), + 'html' => $html, + 'sort_by' => 'random', + ), + )); + + $location = $response['headers']->getAll()['location']; + preg_match('%/(\d+)/$%', $location, $matches); + $ak_mailing_id = +$matches[1]; + + $response = $ak->request(array( + 'path' => "mailer/$ak_mailing_id/rebuild", + 'method' => 'post', + 'data' => array(), + )); + + $response['ak_mailing_id'] = $ak_mailing_id; + + global $wpdb; + $wpdb->insert('vk_mailing', array( + 'ak_mailing_id' => $ak_mailing_id, + 'campaign_id' => $params['campaign_id'], + 'status' => 'rebuilding', + 'variation_subject' => $params['variation_subject'], + )); + + return $response; + } + + function get_url($id){ + return get_permalink($id); + } + + function getoption(){ + return get_option('subscribed_users') / 7; + } } function mh() { From abf131ddf61dd0210af4738fc5d26e13f415f4e7 Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Wed, 31 Oct 2018 14:42:39 -0700 Subject: [PATCH 48/65] Test one subject with 100% conversion rate to another with none. --- addons/tests/mailing_test.php | 1024 ++++++++++++++++++--------------- 1 file changed, 571 insertions(+), 453 deletions(-) diff --git a/addons/tests/mailing_test.php b/addons/tests/mailing_test.php index aed5eb3..047468b 100644 --- a/addons/tests/mailing_test.php +++ b/addons/tests/mailing_test.php @@ -32,509 +32,595 @@ function query(){} final class mailingsClass extends TestCase { - public function test_vk_mailings_create_new_mailings_action_two_campaigns(): void - { - global $TwoCampaigns; + // public function test_vk_mailings_create_new_mailings_action_two_campaigns(): void + // { + // global $TwoCampaigns; - $vk_mailings_mock = $this->createMock(Mailings::class); - $wpdb_mock = $this->createMock(wpdb::class); - $mhMock= $this->createMock(MailingsHelpers::class); + // $vk_mailings_mock = $this->createMock(Mailings::class); + // $wpdb_mock = $this->createMock(wpdb::class); + // $mhMock= $this->createMock(MailingsHelpers::class); - $vk_mailings_mock->expects($this->once()) - ->method('get_distributions') - ->willReturn($TwoCampaigns); + // $vk_mailings_mock->expects($this->once()) + // ->method('get_distributions') + // ->willReturn($TwoCampaigns); - $mhMock->expects($this->exactly(2)) - ->method('get_fresh_subscribers_for_campaign') - ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + // $mhMock->expects($this->exactly(2)) + // ->method('get_fresh_subscribers_for_campaign') + // ->willReturn(array( 0 => 6630470, 1 => 6630472,)); - $mhMock->expects($this->once()) - ->method('getoption') - ->willReturn(4); + // $mhMock->expects($this->once()) + // ->method('getoption') + // ->willReturn(4); - $mhMock->expects($this->exactly(3)) - ->method('send'); + // $mhMock->expects($this->exactly(3)) + // ->method('send'); - $wpdb_mock->expects($this->exactly(1)) - ->method('query'); + // $wpdb_mock->expects($this->exactly(1)) + // ->method('query'); - vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); - } + // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); + // } - public function test_vk_mailings_create_new_mailings_action_one_campaigns(): void - { - global $singleCampaign; + // public function test_vk_mailings_create_new_mailings_action_one_campaigns(): void + // { + // global $singleCampaign; - $vk_mailings_mock = $this->createMock(Mailings::class); - $wpdb_mock = $this->createMock(wpdb::class); - $mhMock = $this->createMock(MailingsHelpers::class); + // $vk_mailings_mock = $this->createMock(Mailings::class); + // $wpdb_mock = $this->createMock(wpdb::class); + // $mhMock = $this->createMock(MailingsHelpers::class); - $vk_mailings_mock->expects($this->once()) - ->method('get_distributions') - ->willReturn($singleCampaign); + // $vk_mailings_mock->expects($this->once()) + // ->method('get_distributions') + // ->willReturn($singleCampaign); - $mhMock->expects($this->exactly(1)) - ->method('get_fresh_subscribers_for_campaign') - ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + // $mhMock->expects($this->exactly(1)) + // ->method('get_fresh_subscribers_for_campaign') + // ->willReturn(array( 0 => 6630470, 1 => 6630472,)); - $mhMock->expects($this->once()) - ->method('getoption') - ->willReturn(4); + // $mhMock->expects($this->once()) + // ->method('getoption') + // ->willReturn(4); - $mhMock->expects($this->exactly(2)) - ->method('send'); + // $mhMock->expects($this->exactly(2)) + // ->method('send'); - $wpdb_mock->expects($this->exactly(1)) - ->method('query'); + // $wpdb_mock->expects($this->exactly(1)) + // ->method('query'); - vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); - } + // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); + // } - public function test_get_distribution_two_subject_campaign(): void - { - global $twoSubjectCampaign; + // public function test_get_distribution_two_subject_campaign(): void + // { + // global $twoSubjectCampaign; - $vk_mailings_mock = $this->createMock(Mailings::class); - $wpdb_mock = $this->createMock(wpdb::class); - $mhMock = $this->createMock(MailingsHelpers::class); + // $vk_mailings_mock = $this->createMock(Mailings::class); + // $wpdb_mock = $this->createMock(wpdb::class); + // $mhMock = $this->createMock(MailingsHelpers::class); - $vk_mailings_mock->expects($this->once()) - ->method('get_distributions') - ->willReturn($twoSubjectCampaign); + // $vk_mailings_mock->expects($this->once()) + // ->method('get_distributions') + // ->willReturn($twoSubjectCampaign); - $mhMock->expects($this->once()) - ->method('get_fresh_subscribers_for_campaign') - ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + // $mhMock->expects($this->once()) + // ->method('get_fresh_subscribers_for_campaign') + // ->willReturn(array( 0 => 6630470, 1 => 6630472,)); - $mhMock->expects($this->once()) - ->method('getoption') - ->willReturn(4); + // $mhMock->expects($this->once()) + // ->method('getoption') + // ->willReturn(4); - $mhMock->expects($this->once()) - ->method('send') - ->willReturn(array ( 'ak_mailing_id' => 1)); + // $mhMock->expects($this->once()) + // ->method('send') + // ->willReturn(array ( 'ak_mailing_id' => 1)); - $wpdb_mock->expects($this->once()) - ->method('query'); - - vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); - } - - public function test_mailingsHelpers_wp_query_posts_method(): void - { - $mailingsMock = new Mailings(); - $wpdb = new wpdb(); - $postObject = new stdClass(); - $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - - $mhMock= $this->getMockBuilder(MailingsHelpers::class) - ->setMethods(['wp_query_posts']) - ->getMock(); - - $mhMock->expects($this->once()) - ->method('wp_query_posts') - ->will($this->returnValue($postObject)); - - $mailingsMock->get_distributions($wpdb, $mhMock); - } - - public function test_get_fields(): void - { - $wpdb = new wpdb(); - $postObject = new stdClass(); - $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - - $mhMock= $this->getMockBuilder(MailingsHelpers::class) - ->setMethods(['wp_query_posts', 'getFields']) - ->getMock(); + // $wpdb_mock->expects($this->once()) + // ->method('query'); + + // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); + // } + + // public function test_mailingsHelpers_wp_query_posts_method(): void + // { + // $mailingsMock = new Mailings(); + // $wpdb = new wpdb(); + // $postObject = new stdClass(); + // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + + // $mhMock= $this->getMockBuilder(MailingsHelpers::class) + // ->setMethods(['wp_query_posts']) + // ->getMock(); + + // $mhMock->expects($this->once()) + // ->method('wp_query_posts') + // ->will($this->returnValue($postObject)); + + // $mailingsMock->get_distributions($wpdb, $mhMock); + // } + + // public function test_get_fields(): void + // { + // $wpdb = new wpdb(); + // $postObject = new stdClass(); + // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + + // $mhMock= $this->getMockBuilder(MailingsHelpers::class) + // ->setMethods(['wp_query_posts', 'getFields']) + // ->getMock(); - $mhMock->expects($this->once()) - ->method('wp_query_posts') - ->will($this->returnValue($postObject)); + // $mhMock->expects($this->once()) + // ->method('wp_query_posts') + // ->will($this->returnValue($postObject)); - $mhMock->expects($this->once()) - ->method('getFields'); + // $mhMock->expects($this->once()) + // ->method('getFields'); - $subject = new Mailings(); + // $subject = new Mailings(); - $subject->get_distributions($wpdb, $mhMock); - } - - public function test_mailingsHelpers_setUpCampaigns(): void - { - $wpdb = new wpdb(); - // $postObject = new stdClass(); - $postObject = array(0 => (object) ['ID' => '', 'post_title'=> '']); - $campaigns = array( 0 => array( 'fields'=> '', 'campaign_id' => '', 'subjects' => array())); - - $mhMock= $this->getMockBuilder(MailingsHelpers::class) - ->setMethods(['wp_query_posts', 'setUpCampaigns']) - ->getMock(); + // $subject->get_distributions($wpdb, $mhMock); + // } + + // public function test_mailingsHelpers_setUpCampaigns(): void + // { + // $wpdb = new wpdb(); + // // $postObject = new stdClass(); + // $postObject = array(0 => (object) ['ID' => '', 'post_title'=> '']); + // $campaigns = array( 0 => array( 'fields'=> '', 'campaign_id' => '', 'subjects' => array())); + + // $mhMock= $this->getMockBuilder(MailingsHelpers::class) + // ->setMethods(['wp_query_posts', 'setUpCampaigns']) + // ->getMock(); - $mhMock->expects($this->once()) - ->method('wp_query_posts') - ->will($this->returnValue($postObject)); + // $mhMock->expects($this->once()) + // ->method('wp_query_posts') + // ->will($this->returnValue($postObject)); - $mhMock->expects($this->once()) - ->method('setUpCampaigns') - ->will($this->returnValue($campaigns)); + // $mhMock->expects($this->once()) + // ->method('setUpCampaigns') + // ->will($this->returnValue($campaigns)); - $subject = new Mailings(); + // $subject = new Mailings(); - $subject->get_distributions($wpdb, $mhMock); - } - - public function test_mailingsHelpers_get_mailings_results_wpdb(): void - { - $wpdb = new wpdb(); - // $postObject = new stdClass(); - $postObject = array( 0 => array( 'campaign_id' => '1')); - - $mhMock= $this->getMockBuilder(MailingsHelpers::class) - ->setMethods(['get_mailings_results_wpdb']) - ->getMock(); + // $subject->get_distributions($wpdb, $mhMock); + // } + + // public function test_mailingsHelpers_get_mailings_results_wpdb(): void + // { + // $wpdb = new wpdb(); + // // $postObject = new stdClass(); + // $postObject = array( 0 => array( 'campaign_id' => '1')); + + // $mhMock= $this->getMockBuilder(MailingsHelpers::class) + // ->setMethods(['get_mailings_results_wpdb']) + // ->getMock(); - $mhMock->expects($this->once()) - ->method('get_mailings_results_wpdb') - ->will($this->returnValue($postObject)); + // $mhMock->expects($this->once()) + // ->method('get_mailings_results_wpdb') + // ->will($this->returnValue($postObject)); - $subject = new Mailings(); + // $subject = new Mailings(); - $subject->get_distributions($wpdb, $mhMock); - } - - public function test_mailings_distribution_post_method(): void - { - $mailings = new Mailings(); - $wpdb = new wpdb(); - $postObject = new stdClass(); + // $subject->get_distributions($wpdb, $mhMock); + // } + + // public function test_mailings_distribution_post_method(): void + // { + // $mailings = new Mailings(); + // $wpdb = new wpdb(); + // $postObject = new stdClass(); - $postObject->posts = array ( 0 => (object)(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), ); + // $postObject->posts = array ( 0 => (object)(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), ); - //** must update return value from campaigns + // //** must update return value from campaigns - $campaigns = array( - 263 => array( - "conversions" => 0, - "fields" => array( - "subjects" => array ( 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ),) - ), - "id" => 263, - "losses" => 0, - "sent" => 0, - "subjects" => array (), - "title" => "No wall testing", - "valid" => true - ) - ); - // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + // $campaigns = array( + // 263 => array( + // "conversions" => 0, + // "fields" => array( + // "subjects" => array ( 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ),) + // ), + // "id" => 263, + // "losses" => 0, + // "sent" => 0, + // "subjects" => array (), + // "title" => "No wall testing", + // "valid" => true + // ) + // ); + // // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - $mailings_stats = array(0 => [ - 'campaign_id' => 263, - 'conversions' => 0, - 'losses' => 0, - 'sent' => 0, - 'variation_subject' => 0 - ]); + // $mailings_stats = array(0 => [ + // 'campaign_id' => 263, + // 'conversions' => 0, + // 'losses' => 0, + // 'sent' => 0, + // 'variation_subject' => 0 + // ]); - $mhMock= $this->getMockBuilder(MailingsHelpers::class) - ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) - ->getMock(); + // $mhMock= $this->getMockBuilder(MailingsHelpers::class) + // ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) + // ->getMock(); - $mhMock->expects($this->once()) - ->method('wp_query_posts') - ->will($this->returnValue($postObject)); + // $mhMock->expects($this->once()) + // ->method('wp_query_posts') + // ->will($this->returnValue($postObject)); - $mhMock->expects($this->once()) - ->method('setUpCampaigns') - ->will($this->returnValue($campaigns)); + // $mhMock->expects($this->once()) + // ->method('setUpCampaigns') + // ->will($this->returnValue($campaigns)); - $mhMock->expects($this->once()) - ->method('get_mailings_results_wpdb') - ->will($this->returnValue($mailings_stats)); + // $mhMock->expects($this->once()) + // ->method('get_mailings_results_wpdb') + // ->will($this->returnValue($mailings_stats)); - $mhMock->expects($this->once()) - ->method('get_fresh_subscribers_for_campaign') - ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); + // $mhMock->expects($this->once()) + // ->method('get_fresh_subscribers_for_campaign') + // ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); - $this->assertEquals(array( - 'campaigns' => Array( - 0 => Array( - 'conversions' => 0, - 'fields' => Array( - 'subjects' => Array( - 0 => Array( - 'subject' => 'Tell Senate: No attacks on immigrants', - 'enabled' => true - ), - 1 => Array( - 'subject' => "Block Trump's attacks on immigrants", - 'enabled' => true - ) - ) - ), - 'id' => 263, - 'losses' => 0, - 'sent' => 0, - 'subjects' => Array( - 0 => Array( - 'conversions' => 0, - 'losses' => 0, - 'sent' => 0, - 'title' => 'Tell Senate: No attacks on immigrants', - 'rate' => 1, - 'share' => 1 - ) - ), - 'title' => 'No wall testing', - 'valid' => true, - 'rate' => 1, - 'share' => 1, - 'limit' => 4.0 - ) - ), - 'overall' => Array( - 'conversions' => 0, - 'losses' => 0, - 'sent' => 0, - 'boost' => 500, - 'rate' => 1 - ) - ), $mailings->get_distributions($wpdb, $mhMock)); - } - - public function test_get_distributions_one_campaign_method(): void - { - $mailings = new Mailings(); - $wpdb = new wpdb(); - $postObject = new stdClass(); + // $this->assertEquals(array( + // 'campaigns' => Array( + // 0 => Array( + // 'conversions' => 0, + // 'fields' => Array( + // 'subjects' => Array( + // 0 => Array( + // 'subject' => 'Tell Senate: No attacks on immigrants', + // 'enabled' => true + // ), + // 1 => Array( + // 'subject' => "Block Trump's attacks on immigrants", + // 'enabled' => true + // ) + // ) + // ), + // 'id' => 263, + // 'losses' => 0, + // 'sent' => 0, + // 'subjects' => Array( + // 0 => Array( + // 'conversions' => 0, + // 'losses' => 0, + // 'sent' => 0, + // 'title' => 'Tell Senate: No attacks on immigrants', + // 'rate' => 1, + // 'share' => 1 + // ) + // ), + // 'title' => 'No wall testing', + // 'valid' => true, + // 'rate' => 1, + // 'share' => 1, + // 'limit' => 4.0 + // ) + // ), + // 'overall' => Array( + // 'conversions' => 0, + // 'losses' => 0, + // 'sent' => 0, + // 'boost' => 500, + // 'rate' => 1 + // ) + // ), $mailings->get_distributions($wpdb, $mhMock)); + // } + + // public function test_get_distributions_one_campaign_method(): void + // { + // $mailings = new Mailings(); + // $wpdb = new wpdb(); + // $postObject = new stdClass(); - $postObject->posts = array (); + // $postObject->posts = array (); - //** must update return value from campaigns + // //** must update return value from campaigns - $campaigns = array( - 263 => array( - "conversions" => 0, - "fields" => array( - "subjects" => array ( 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ),) - ), - "id" => 263, - "losses" => 0, - "sent" => 0, - "subjects" => array (), - "title" => "No wall testing", - "valid" => true - ) - ); - // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + // $campaigns = array( + // 263 => array( + // "conversions" => 0, + // "fields" => array( + // "subjects" => array ( 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ),) + // ), + // "id" => 263, + // "losses" => 0, + // "sent" => 0, + // "subjects" => array (), + // "title" => "No wall testing", + // "valid" => true + // ) + // ); + // // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - $mailings_stats = array ( - 0 => array ( - 'campaign_id' => '263', - 'variation_subject' => '0', - 'conversions' => '1', - 'losses' => '0', - 'sent' => '2', ), - 1 => array ( - 'campaign_id' => '263', - 'variation_subject' => '1', - 'conversions' => '1', - 'losses' => '0', - 'sent' => '2', ) - ); + // $mailings_stats = array ( + // 0 => array ( + // 'campaign_id' => '263', + // 'variation_subject' => '0', + // 'conversions' => '1', + // 'losses' => '0', + // 'sent' => '2', ), + // 1 => array ( + // 'campaign_id' => '263', + // 'variation_subject' => '1', + // 'conversions' => '1', + // 'losses' => '0', + // 'sent' => '2', ) + // ); - $mhMock= $this->getMockBuilder(MailingsHelpers::class) - ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) - ->getMock(); + // $mhMock= $this->getMockBuilder(MailingsHelpers::class) + // ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) + // ->getMock(); - $mhMock->expects($this->once()) - ->method('wp_query_posts') - ->will($this->returnValue($postObject)); + // $mhMock->expects($this->once()) + // ->method('wp_query_posts') + // ->will($this->returnValue($postObject)); - $mhMock->expects($this->once()) - ->method('setUpCampaigns') - ->will($this->returnValue($campaigns)); + // $mhMock->expects($this->once()) + // ->method('setUpCampaigns') + // ->will($this->returnValue($campaigns)); - $mhMock->expects($this->once()) - ->method('get_mailings_results_wpdb') - ->will($this->returnValue($mailings_stats)); + // $mhMock->expects($this->once()) + // ->method('get_mailings_results_wpdb') + // ->will($this->returnValue($mailings_stats)); - $mhMock->expects($this->once()) - ->method('get_fresh_subscribers_for_campaign') - ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); + // $mhMock->expects($this->once()) + // ->method('get_fresh_subscribers_for_campaign') + // ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); - $this->assertEquals(array( - 'campaigns' => Array( - 0 => Array( - 'conversions' => 2, - 'fields' => Array( - 'subjects' => Array( - 0 => Array( - 'subject' => 'Tell Senate: No attacks on immigrants', - 'enabled' => true - ), - 1 => Array( - 'subject' => "Block Trump's attacks on immigrants", - 'enabled' => true - ) - ) - ), - 'id' => 263, - 'losses' => 0, - 'sent' => 4, - 'subjects' => Array( - 0 => Array( - 'conversions' => 1, - 'losses' => 0, - 'sent' => 2, - 'title' => 'Tell Senate: No attacks on immigrants', - 'rate' => 0.994055523936002, - 'share' => 0.5 - ), - 1 => Array( - 'conversions' => 1, - 'losses' => 0, - 'sent' => 2, - 'title' => "Block Trump's attacks on immigrants", - 'rate' => 0.994055523936002, - 'share' => 0.5 - ) - ), - 'title' => 'No wall testing', - 'valid' => true, - 'rate' => 0.9920949861426052, - 'share' => 1.0, - 'limit' => 4.0 - ) - ), - 'overall' => Array( - 'conversions' => 2, - 'losses' => 0, - 'sent' => 4, - 'boost' => 500, - 'rate' => 0.996031746031746 - ) - ), $mailings->get_distributions($wpdb, $mhMock)); - } - public function test_get_distributions_one_campaign_one_greater_conversions_method(): void - { - $mailings = new Mailings(); - $wpdb = new wpdb(); - $postObject = new stdClass(); + // $this->assertEquals(array( + // 'campaigns' => Array( + // 0 => Array( + // 'conversions' => 2, + // 'fields' => Array( + // 'subjects' => Array( + // 0 => Array( + // 'subject' => 'Tell Senate: No attacks on immigrants', + // 'enabled' => true + // ), + // 1 => Array( + // 'subject' => "Block Trump's attacks on immigrants", + // 'enabled' => true + // ) + // ) + // ), + // 'id' => 263, + // 'losses' => 0, + // 'sent' => 4, + // 'subjects' => Array( + // 0 => Array( + // 'conversions' => 1, + // 'losses' => 0, + // 'sent' => 2, + // 'title' => 'Tell Senate: No attacks on immigrants', + // 'rate' => 0.994055523936002, + // 'share' => 0.5 + // ), + // 1 => Array( + // 'conversions' => 1, + // 'losses' => 0, + // 'sent' => 2, + // 'title' => "Block Trump's attacks on immigrants", + // 'rate' => 0.994055523936002, + // 'share' => 0.5 + // ) + // ), + // 'title' => 'No wall testing', + // 'valid' => true, + // 'rate' => 0.9920949861426052, + // 'share' => 1.0, + // 'limit' => 4.0 + // ) + // ), + // 'overall' => Array( + // 'conversions' => 2, + // 'losses' => 0, + // 'sent' => 4, + // 'boost' => 500, + // 'rate' => 0.996031746031746 + // ) + // ), $mailings->get_distributions($wpdb, $mhMock)); + // } + // public function test_get_distributions_one_campaign_one_greater_conversions_method(): void + // { + // $mailings = new Mailings(); + // $wpdb = new wpdb(); + // $postObject = new stdClass(); - $postObject->posts = array (); + // $postObject->posts = array (); - //** must update return value from campaigns + // //** must update return value from campaigns - $campaigns = array( - 263 => array( - "conversions" => 0, - "fields" => array( - "subjects" => array ( 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ),) - ), - "id" => 263, - "losses" => 0, - "sent" => 0, - "subjects" => array (), - "title" => "No wall testing", - "valid" => true - ) - ); - // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + // $campaigns = array( + // 263 => array( + // "conversions" => 0, + // "fields" => array( + // "subjects" => array ( 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ),) + // ), + // "id" => 263, + // "losses" => 0, + // "sent" => 0, + // "subjects" => array (), + // "title" => "No wall testing", + // "valid" => true + // ) + // ); + // // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - $mailings_stats = array ( - 0 => array ( - 'campaign_id' => '263', - 'variation_subject' => '0', - 'conversions' => '2', - 'losses' => '0', - 'sent' => '2', ), - 1 => array ( - 'campaign_id' => '263', - 'variation_subject' => '1', - 'conversions' => '0', - 'losses' => '0', - 'sent' => '2', ) - ); + // $mailings_stats = array ( + // 0 => array ( + // 'campaign_id' => '263', + // 'variation_subject' => '0', + // 'conversions' => '2', + // 'losses' => '0', + // 'sent' => '2', ), + // 1 => array ( + // 'campaign_id' => '263', + // 'variation_subject' => '1', + // 'conversions' => '0', + // 'losses' => '0', + // 'sent' => '2', ) + // ); - $mhMock= $this->getMockBuilder(MailingsHelpers::class) - ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) - ->getMock(); + // $mhMock= $this->getMockBuilder(MailingsHelpers::class) + // ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) + // ->getMock(); - $mhMock->expects($this->once()) - ->method('wp_query_posts') - ->will($this->returnValue($postObject)); + // $mhMock->expects($this->once()) + // ->method('wp_query_posts') + // ->will($this->returnValue($postObject)); - $mhMock->expects($this->once()) - ->method('setUpCampaigns') - ->will($this->returnValue($campaigns)); + // $mhMock->expects($this->once()) + // ->method('setUpCampaigns') + // ->will($this->returnValue($campaigns)); - $mhMock->expects($this->once()) - ->method('get_mailings_results_wpdb') - ->will($this->returnValue($mailings_stats)); + // $mhMock->expects($this->once()) + // ->method('get_mailings_results_wpdb') + // ->will($this->returnValue($mailings_stats)); - $mhMock->expects($this->once()) - ->method('get_fresh_subscribers_for_campaign') - ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); + // $mhMock->expects($this->once()) + // ->method('get_fresh_subscribers_for_campaign') + // ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); - $this->assertEquals(array( - 'campaigns' => Array( - 0 => Array( - 'conversions' => 2, - 'fields' => Array( - 'subjects' => Array( - 0 => Array( - 'subject' => 'Tell Senate: No attacks on immigrants', - 'enabled' => true - ), - 1 => Array( - 'subject' => "Block Trump's attacks on immigrants", - 'enabled' => true - ) - ) - ), - 'id' => 263, - 'losses' => 0, - 'sent' => 4, - 'subjects' => Array( - 0 => Array( - 'conversions' => 2, - 'losses' => 0, - 'sent' => 2, - 'title' => 'Tell Senate: No attacks on immigrants', - 'rate' => 0.996047555808512, - 'share' => 0.501001972135632 - ), - 1 => Array( - 'conversions' => 0, - 'losses' => 0, - 'sent' => 2, - 'title' => "Block Trump's attacks on immigrants", - 'rate' => 0.9920634920634921, - 'share' => 0.498998027864368 - ) - ), - 'title' => 'No wall testing', - 'valid' => true, - 'rate' => 0.9920949861426052, - 'share' => 1.0, - 'limit' => 4.0 - ) - ), - 'overall' => Array( - 'conversions' => 2, - 'losses' => 0, - 'sent' => 4, - 'boost' => 500, - 'rate' => 0.996031746031746 - ) - ), $mailings->get_distributions($wpdb, $mhMock)); - } + // $this->assertEquals(array( + // 'campaigns' => Array( + // 0 => Array( + // 'conversions' => 2, + // 'fields' => Array( + // 'subjects' => Array( + // 0 => Array( + // 'subject' => 'Tell Senate: No attacks on immigrants', + // 'enabled' => true + // ), + // 1 => Array( + // 'subject' => "Block Trump's attacks on immigrants", + // 'enabled' => true + // ) + // ) + // ), + // 'id' => 263, + // 'losses' => 0, + // 'sent' => 4, + // 'subjects' => Array( + // 0 => Array( + // 'conversions' => 2, + // 'losses' => 0, + // 'sent' => 2, + // 'title' => 'Tell Senate: No attacks on immigrants', + // 'rate' => 0.996047555808512, + // 'share' => 0.501001972135632 + // ), + // 1 => Array( + // 'conversions' => 0, + // 'losses' => 0, + // 'sent' => 2, + // 'title' => "Block Trump's attacks on immigrants", + // 'rate' => 0.9920634920634921, + // 'share' => 0.498998027864368 + // ) + // ), + // 'title' => 'No wall testing', + // 'valid' => true, + // 'rate' => 0.9920949861426052, + // 'share' => 1.0, + // 'limit' => 4.0 + // ) + // ), + // 'overall' => Array( + // 'conversions' => 2, + // 'losses' => 0, + // 'sent' => 4, + // 'boost' => 500, + // 'rate' => 0.996031746031746 + // ) + // ), $mailings->get_distributions($wpdb, $mhMock)); + // } + + // public function test_send_function_with_one_campaign_one_subject_greater_conversion(): void + // { + // $vk_mailings_mock = $this->createMock(Mailings::class); + // $wpdb_mock = $this->createMock(wpdb::class); + // $mhMock = $this->createMock(MailingsHelpers::class); + + // $vk_mailings_mock->expects($this->once()) + // ->method('get_distributions') + // ->willReturn(Array( + // 'campaigns' => Array( + // 0 => Array( + // 'conversions' => 2, + // 'fields' => Array( + // 'subjects' => Array( + // 0 => Array( + // 'subject' => 'Tell Senate: No attacks on immigrants', + // 'enabled' => true + // ), + // 1 => Array( + // 'subject' => "Block Trump's attacks on immigrants", + // 'enabled' => true + // ) + // ), + // 'from_line' => '41', + // 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', + // 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + // 'salutation' => '{{ user.first_name|default:"Hi" }},', + + // ), + // 'id' => 263, + // 'losses' => 0, + // 'sent' => 4, + // 'subjects' => Array( + // 0 => Array( + // 'conversions' => 2, + // 'losses' => 0, + // 'sent' => 2, + // 'title' => 'Tell Senate: No attacks on immigrants', + // 'rate' => 0.996047555808512, + // 'share' => 0.501001972135632 + // ), + // 1 => Array( + // 'conversions' => 0, + // 'losses' => 0, + // 'sent' => 2, + // 'title' => "Block Trump's attacks on immigrants", + // 'rate' => 0.9920634920634921, + // 'share' => 0.498998027864368 + // ) + // ), + // 'title' => 'No wall testing', + // 'valid' => true, + // 'rate' => 0.9920949861426052, + // 'share' => 1.0, + // 'limit' => 4.0 + // ) + // ), + // 'overall' => Array( + // 'conversions' => 2, + // 'losses' => 0, + // 'sent' => 4, + // 'boost' => 500, + // 'rate' => 0.996031746031746 + // ) + // ) + // ); + + // $mhMock->expects($this->once()) + // ->method('getoption') + // ->willReturn(3.5714285714285716); + + // $mhMock->expects($this->once()) + // ->method('get_url') + // ->willReturn('https://victorykit.local/c/no-wall-testing-2/'); + + + // $mhMock->expects($this->exactly(1)) + // ->method('get_fresh_subscribers_for_campaign') + // ->willReturn(array( 0 => 6630470, 1 => 6630472, 2 => 6630478, + // 3 => 6630479)); - public function test_send_function_with_one_campaign_one_subject_greater_conversion(): void + // $mhMock->expects($this->exactly(2)) + // ->method('send'); + + // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); + // } + + public function test_one_subject_greater_conversion(): void { - global $singleCampaign; $vk_mailings_mock = $this->createMock(Mailings::class); $wpdb_mock = $this->createMock(wpdb::class); $mhMock = $this->createMock(MailingsHelpers::class); @@ -544,7 +630,7 @@ public function test_send_function_with_one_campaign_one_subject_greater_convers ->willReturn(Array( 'campaigns' => Array( 0 => Array( - 'conversions' => 2, + 'conversions' => 500, 'fields' => Array( 'subjects' => Array( 0 => Array( @@ -557,45 +643,45 @@ public function test_send_function_with_one_campaign_one_subject_greater_convers ) ), 'from_line' => '41', - 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

Already he has banned Muslims and refugees, threatened sanctuary cities and criminalized millions of undocumented immigrants. His Immigration and Custom Enforcement (ICE) and Border Patrol agents are separating parents from their children, deporting innocent people and spreading fear in immigrant communities across the country. The budget he just released slashes funding for the social safety net in order to spend billions of dollars building his wall, increasing his deportation force and expediting deportations.

Senate Democrats have the power to block funding for Trump’s wall and his massive deportation force. Recent reports show they may be willing to fight to stop Trump’s hateful agenda. Can you help make sure they stand strong?

We cannot let any Senate Democrat cave to political pressure from the right-wing and give Republicans the 60 votes they need to fund Trump’s hate. That is where progressives come in. We have to show Democratic senators that we will have their backs if they act boldly and that we will hold them accountable if they fail to stand up for immigrants.

Tell Senate Democrats: Block funding for Trump’s attacks on immigrants. Click here to sign the petition.

Trump wants to harass, deport and reject people who have come or are dreaming of coming to the United States seeking refuge from conflict, lives free from persecution and better economic opportunities for their families. For as long as Trump is president and extremist right-wing Republicans are willing to enable his agenda, Senate Democrats will have a choice: enable Trump’s agenda of criminalization and deportations, or resist and obstruct his racist regime at every turn. It is up to make sure they do the right thing.

Tell Senate Democrats: Block funding for Trump’s attacks on immigrants.

', + 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', 'salutation' => '{{ user.first_name|default:"Hi" }},', ), 'id' => 263, 'losses' => 0, - 'sent' => 4, + 'sent' => 1000, 'subjects' => Array( 0 => Array( - 'conversions' => 2, + 'conversions' => 500, 'losses' => 0, - 'sent' => 2, + 'sent' => 500, 'title' => 'Tell Senate: No attacks on immigrants', - 'rate' => 0.996047555808512, - 'share' => 0.501001972135632 + 'rate' => 0.8333333333333333, + 'share' => 0.7142857142857143 ), 1 => Array( 'conversions' => 0, 'losses' => 0, - 'sent' => 2, + 'sent' => 500, 'title' => "Block Trump's attacks on immigrants", - 'rate' => 0.9920634920634921, - 'share' => 0.498998027864368 + 'rate' => 0.3333333333333333, + 'share' => 0.28571428571428575 ) ), 'title' => 'No wall testing', 'valid' => true, - 'rate' => 0.9920949861426052, + 'rate' => 0.5555555555555555, 'share' => 1.0, 'limit' => 4.0 ) ), 'overall' => Array( - 'conversions' => 2, + 'conversions' => 500, 'losses' => 0, - 'sent' => 4, + 'sent' => 1000, 'boost' => 500, - 'rate' => 0.996031746031746 + 'rate' => 0.6666666666666666 ) ) ); @@ -617,7 +703,39 @@ public function test_send_function_with_one_campaign_one_subject_greater_convers $mhMock->expects($this->exactly(2)) ->method('send'); - vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); + $this->assertEquals(Array( + 0 => Array( + 'from_line' => '41', + 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', + 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + 'campaign_id' => 263, + 'limit' => 3.0, + 'salutation' => '{{ user.first_name|default:"Hi" }},', + 'subject' => 'Tell Senate: No attacks on immigrants', + 'subscribers' => Array ( + 0 => 6630470, + 1 => 6630472, + 2 => 6630478 + ), + 'url' => 'https://victorykit.local/c/no-wall-testing-2/', + 'variation_subject' => 0, + + ), + 1 => Array( + 'from_line' => '41', + 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', + 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + 'campaign_id' => 263, + 'limit' => 1.0, + 'salutation' => '{{ user.first_name|default:"Hi" }},', + 'subject' => "Block Trump's attacks on immigrants", + 'subscribers' => Array ( + 0 => 6630479 + ), + 'url' => 'https://victorykit.local/c/no-wall-testing-2/', + 'variation_subject' => 1, + ) + ), vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock)); } } From 3281207604c768962009e61b6180a3cdfd14552a Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Wed, 31 Oct 2018 14:43:21 -0700 Subject: [PATCH 49/65] Add return value to create mailings action function. --- addons/mailings.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/addons/mailings.php b/addons/mailings.php index 68aa91f..d9f3cc5 100644 --- a/addons/mailings.php +++ b/addons/mailings.php @@ -396,6 +396,7 @@ function vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $ $id = $campaign['id']; $fields = $campaign['fields']; $url = $mh->get_url($id); + $paramsList = Array(); //need to print out why $limit_per_campaign NAN -> this is breaking everything // $allsql = trim(preg_replace('/\s+/', ' ',var_export( $campaign, true))); // error_log('%%%%%%%%%%% '); @@ -437,8 +438,7 @@ function vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $ 'url' => $url, 'variation_subject' => $index, ); - // print('######'); - // var_dump($params); + array_push($paramsList,$params); // $allsql = trim(preg_replace('/\s+/', ' ',var_export( $params, true))); // error_log('s line 563: '.$allsql); // $sCamp = trim(preg_replace('/\s+/', ' ',var_export( $params, true))); @@ -467,6 +467,7 @@ function vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $ } } } + return $paramsList; } add_action('vk_mailings_create_new_mailings', 'vk_mailings_create_new_mailings_action'); From aa0413fb0d9daa97ab041463869e56ac0803fd4b Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Fri, 2 Nov 2018 13:42:49 -0700 Subject: [PATCH 50/65] Add test for create new mailings return value. --- addons/tests/mailing_test.php | 322 +++++++++++++++++++++++----------- 1 file changed, 218 insertions(+), 104 deletions(-) diff --git a/addons/tests/mailing_test.php b/addons/tests/mailing_test.php index 047468b..3428558 100644 --- a/addons/tests/mailing_test.php +++ b/addons/tests/mailing_test.php @@ -619,123 +619,237 @@ final class mailingsClass extends TestCase // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); // } - public function test_one_subject_greater_conversion(): void + // + //****play with mailing stats**** + // public function test_get_distributions_one_campaign_method(): void + // { + // $mailings = new Mailings(); + // $wpdb = new wpdb(); + // $postObject = new stdClass(); + + // $postObject->posts = array (); + + // //** must update return value from campaigns + + // $campaigns = array( + // 263 => array( + // "conversions" => 0, + // "fields" => array( + // "subjects" => array ( + // 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), + // 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ), + // ) + // ), + // "id" => 263, + // "losses" => 0, + // "sent" => 0, + // "subjects" => array (), + // "title" => "No wall testing", + // "valid" => true + // ) + // ); + // // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + + // $mailings_stats = array ( + // 0 => array ( + // 'campaign_id' => '263', + // 'variation_subject' => '0', + // 'conversions' => '500', + // 'losses' => '0', + // 'sent' => '500', ), + // 1 => array ( + // 'campaign_id' => '263', + // 'variation_subject' => '1', + // 'conversions' => '0', + // 'losses' => '0', + // 'sent' => '500', ) + // ); + + // $mhMock= $this->getMockBuilder(MailingsHelpers::class) + // ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) + // ->getMock(); + + // $mhMock->expects($this->once()) + // ->method('wp_query_posts') + // ->will($this->returnValue($postObject)); + + // $mhMock->expects($this->once()) + // ->method('setUpCampaigns') + // ->will($this->returnValue($campaigns)); + + // $mhMock->expects($this->once()) + // ->method('get_mailings_results_wpdb') + // ->will($this->returnValue($mailings_stats)); + + // $mhMock->expects($this->once()) + // ->method('get_fresh_subscribers_for_campaign') + // ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); + + // $this->assertEquals(array( + // 'campaigns' => Array( + // 0 => Array( + // 'conversions' => 500, + // 'fields' => Array( + // 'subjects' => Array( + // 0 => Array( + // 'subject' => 'Tell Senate: No attacks on immigrants', + // 'enabled' => true + // ), + // 1 => Array( + // 'subject' => "Block Trump's attacks on immigrants", + // 'enabled' => true + // ) + // ) + // ), + // 'id' => 263, + // 'losses' => 0, + // 'sent' => 1000, + // 'subjects' => Array( + // 0 => Array( + // 'conversions' => 500, + // 'losses' => 0, + // 'sent' => 500, + // 'title' => 'Tell Senate: No attacks on immigrants', + // 'rate' => 0.8333333333333333, + // 'share' => 0.7142857142857143 + // ), + // 1 => Array( + // 'conversions' => 0, + // 'losses' => 0, + // 'sent' => 500, + // 'title' => "Block Trump's attacks on immigrants", + // 'rate' => 0.3333333333333333, + // 'share' => 0.28571428571428575 + // ) + // ), + // 'title' => 'No wall testing', + // 'valid' => true, + // 'rate' => 0.5555555555555555, + // 'share' => 1.0, + // 'limit' => 4.0 + // ) + // ), + // 'overall' => Array( + // 'conversions' => 500, + // 'losses' => 0, + // 'sent' => 1000, + // 'boost' => 500, + // 'rate' => 0.6666666666666666 + // ) + // ), $mailings->get_distributions($wpdb, $mhMock)); + // } + public function test_vk_mailings_create_new_mailings_action_return_value(): void { $vk_mailings_mock = $this->createMock(Mailings::class); - $wpdb_mock = $this->createMock(wpdb::class); - $mhMock = $this->createMock(MailingsHelpers::class); + $wpdb = new wpdb(); + + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign', 'send', 'get_url']) + ->getMock(); + $mhMock->expects($this->once()) + ->method('get_fresh_subscribers_for_campaign') + ->will($this->returnValue(['6630475', '6630477', '6630478','6630479','6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479'])); + + $mhMock->expects($this->exactly(2)) + ->method('send'); + + $mhMock->expects($this->once()) + ->method('get_url') + ->willReturn('https://victorykit.local/c/no-wall-testing-2/'); + $vk_mailings_mock->expects($this->once()) ->method('get_distributions') - ->willReturn(Array( - 'campaigns' => Array( - 0 => Array( - 'conversions' => 500, - 'fields' => Array( - 'subjects' => Array( - 0 => Array( - 'subject' => 'Tell Senate: No attacks on immigrants', - 'enabled' => true - ), - 1 => Array( - 'subject' => "Block Trump's attacks on immigrants", - 'enabled' => true - ) - ), - 'from_line' => '41', - 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', - 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', - 'salutation' => '{{ user.first_name|default:"Hi" }},', - - ), - 'id' => 263, - 'losses' => 0, - 'sent' => 1000, - 'subjects' => Array( + ->will($this->returnValue(Array( + 'campaigns' => Array( 0 => Array( 'conversions' => 500, + 'fields' => Array( + 'subjects' => Array( + 0 => Array( + 'subject' => 'Tell Senate: No attacks on immigrants', + 'enabled' => true + ), + 1 => Array( + 'subject' => "Block Trump's attacks on immigrants", + 'enabled' => true + ), + ), + 'from_line' => 41, + 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', + 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + 'salutation' => '{{ user.first_name|default:"Hi" }},', + ), + 'id' => 263, 'losses' => 0, - 'sent' => 500, - 'title' => 'Tell Senate: No attacks on immigrants', - 'rate' => 0.8333333333333333, - 'share' => 0.7142857142857143 - ), - 1 => Array( - 'conversions' => 0, - 'losses' => 0, - 'sent' => 500, - 'title' => "Block Trump's attacks on immigrants", - 'rate' => 0.3333333333333333, - 'share' => 0.28571428571428575 + 'sent' => 1000, + 'subjects' => Array( + 0 => Array( + 'conversions' => 500, + 'losses' => 0, + 'sent' => 500, + 'title' => 'Tell Senate: No attacks on immigrants', + 'rate' => 0.8333333333333333, + 'share' => 0.7142857142857143 + ), + 1 => Array( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 500, + 'title' => "Block Trump's attacks on immigrants", + 'rate' => 0.3333333333333333, + 'share' => 0.28571428571428575 + ) + ), + 'title' => 'No wall testing', + 'valid' => true, + 'rate' => 0.5555555555555555, + 'share' => 1.0, + 'limit' => 4.0 ) ), - 'title' => 'No wall testing', - 'valid' => true, - 'rate' => 0.5555555555555555, - 'share' => 1.0, - 'limit' => 4.0 + 'overall' => Array( + 'conversions' => 500, + 'losses' => 0, + 'sent' => 1000, + 'boost' => 500, + 'rate' => 0.6666666666666666 + ) ) - ), - 'overall' => Array( - 'conversions' => 500, - 'losses' => 0, - 'sent' => 1000, - 'boost' => 500, - 'rate' => 0.6666666666666666 ) - ) - ); - - $mhMock->expects($this->once()) - ->method('getoption') - ->willReturn(3.5714285714285716); - - $mhMock->expects($this->once()) - ->method('get_url') - ->willReturn('https://victorykit.local/c/no-wall-testing-2/'); - - - $mhMock->expects($this->exactly(1)) - ->method('get_fresh_subscribers_for_campaign') - ->willReturn(array( 0 => 6630470, 1 => 6630472, 2 => 6630478, - 3 => 6630479)); - - $mhMock->expects($this->exactly(2)) - ->method('send'); - - $this->assertEquals(Array( - 0 => Array( - 'from_line' => '41', - 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', - 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', - 'campaign_id' => 263, - 'limit' => 3.0, - 'salutation' => '{{ user.first_name|default:"Hi" }},', - 'subject' => 'Tell Senate: No attacks on immigrants', - 'subscribers' => Array ( - 0 => 6630470, - 1 => 6630472, - 2 => 6630478 + ); + $this->assertEquals(Array( + 0 => Array( + 'from_line' => 41, + 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', + 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + 'campaign_id' => 263, + 'limit' => 3.0, + 'salutation' => '{{ user.first_name|default:"Hi" }},', + 'subject' => 'Tell Senate: No attacks on immigrants', + 'subscribers' => Array ( + 0 => '6630475', + 1 => '6630477', + 2 => '6630478' + ), + 'url' => 'https://victorykit.local/c/no-wall-testing-2/', + 'variation_subject' => 0 ), - 'url' => 'https://victorykit.local/c/no-wall-testing-2/', - 'variation_subject' => 0, - - ), - 1 => Array( - 'from_line' => '41', - 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', - 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', - 'campaign_id' => 263, - 'limit' => 1.0, - 'salutation' => '{{ user.first_name|default:"Hi" }},', - 'subject' => "Block Trump's attacks on immigrants", - 'subscribers' => Array ( - 0 => 6630479 - ), - 'url' => 'https://victorykit.local/c/no-wall-testing-2/', - 'variation_subject' => 1, - ) - ), vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock)); + 1 => Array( + 'from_line' => 41, + 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', + 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + 'campaign_id' => 263, + 'limit' => 1.0, + 'salutation' => '{{ user.first_name|default:"Hi" }},', + 'subject' => "Block Trump's attacks on immigrants", + 'subscribers' => Array ( + 0 => '6630479' + ), + 'url' => 'https://victorykit.local/c/no-wall-testing-2/', + 'variation_subject' => 1 + ) + ),vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock)); } } From 02ea0c7e79f072d867f53f3e6db3dbf0a290f3f0 Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Fri, 2 Nov 2018 14:11:22 -0700 Subject: [PATCH 51/65] Test campaign with on subject no conversion and another with 100% loss. --- addons/tests/mailing_test.php | 370 ++++++++++++++++++++++++++-------- 1 file changed, 285 insertions(+), 85 deletions(-) diff --git a/addons/tests/mailing_test.php b/addons/tests/mailing_test.php index 3428558..256deb7 100644 --- a/addons/tests/mailing_test.php +++ b/addons/tests/mailing_test.php @@ -620,7 +620,7 @@ final class mailingsClass extends TestCase // } // - //****play with mailing stats**** + // ****play with mailing stats**** // public function test_get_distributions_one_campaign_method(): void // { // $mailings = new Mailings(); @@ -738,7 +738,242 @@ final class mailingsClass extends TestCase // ) // ), $mailings->get_distributions($wpdb, $mhMock)); // } - public function test_vk_mailings_create_new_mailings_action_return_value(): void + + // ****play with mailing stats**** + // public function test_create_mailings_mailing_no_conversions_only_losses(): void + // { + // $mailings = new Mailings(); + // $wpdb = new wpdb(); + // $postObject = new stdClass(); + + // $postObject->posts = array (); + + // //** must update return value from campaigns + + // $campaigns = array( + // 263 => array( + // "conversions" => 0, + // "fields" => array( + // "subjects" => array ( + // 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), + // 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ), + // ) + // ), + // "id" => 263, + // "losses" => 0, + // "sent" => 0, + // "subjects" => array (), + // "title" => "No wall testing", + // "valid" => true + // ) + // ); + // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + + // $mailings_stats = array ( + // 0 => array ( + // 'campaign_id' => '263', + // 'variation_subject' => '0', + // 'conversions' => '0', + // 'losses' => '0', + // 'sent' => '500', ), + // 1 => array ( + // 'campaign_id' => '263', + // 'variation_subject' => '1', + // 'conversions' => '0', + // 'losses' => '500', + // 'sent' => '500', ) + // ); + + // $mhMock= $this->getMockBuilder(MailingsHelpers::class) + // ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) + // ->getMock(); + + // $mhMock->expects($this->once()) + // ->method('wp_query_posts') + // ->will($this->returnValue($postObject)); + + // $mhMock->expects($this->once()) + // ->method('setUpCampaigns') + // ->will($this->returnValue($campaigns)); + + // $mhMock->expects($this->once()) + // ->method('get_mailings_results_wpdb') + // ->will($this->returnValue($mailings_stats)); + + // $mhMock->expects($this->once()) + // ->method('get_fresh_subscribers_for_campaign') + // ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); + + // $this->assertEquals(array( + // 'campaigns' => Array( + // 0 => Array( + // 'conversions' => 0, + // 'fields' => Array( + // 'subjects' => Array( + // 0 => Array( + // 'subject' => 'Tell Senate: No attacks on immigrants', + // 'enabled' => true + // ), + // 1 => Array( + // 'subject' => "Block Trump's attacks on immigrants", + // 'enabled' => true + // ) + // ) + // ), + // 'id' => 263, + // 'losses' => 500, + // 'sent' => 1000, + // 'subjects' => Array( + // 0 => Array( + // 'conversions' => 0, + // 'losses' => 0, + // 'sent' => 500, + // 'title' => 'Tell Senate: No attacks on immigrants', + // 'rate' => 0, + // 'share' => 0 + // ), + // 1 => Array( + // 'conversions' => 0, + // 'losses' => 500, + // 'sent' => 500, + // 'title' => "Block Trump's attacks on immigrants", + // 'rate' => 0, + // 'share' => 0 + // ) + // ), + // 'title' => 'No wall testing', + // 'valid' => true, + // 'rate' => 0, + // 'share' => 0, + // 'limit' => 0.0 + // ) + // ), + // 'overall' => Array( + // 'conversions' => 0, + // 'losses' => 500, + // 'sent' => 1000, + // 'boost' => 500, + // 'rate' => 0 + // ) + // ), $mailings->get_distributions($wpdb, $mhMock)); + + // // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock); + // } + + // public function test_vk_mailings_create_new_mailings_action_return_value(): void + // { + // $vk_mailings_mock = $this->createMock(Mailings::class); + // $wpdb = new wpdb(); + + // $mhMock= $this->getMockBuilder(MailingsHelpers::class) + // ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign', 'send', 'get_url']) + // ->getMock(); + + // $mhMock->expects($this->once()) + // ->method('get_fresh_subscribers_for_campaign') + // ->will($this->returnValue(['6630475', '6630477', '6630478','6630479','6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479'])); + + // $mhMock->expects($this->exactly(2)) + // ->method('send'); + + // $mhMock->expects($this->once()) + // ->method('get_url') + // ->willReturn('https://victorykit.local/c/no-wall-testing-2/'); + + // $vk_mailings_mock->expects($this->once()) + // ->method('get_distributions') + // ->will($this->returnValue(Array( + // 'campaigns' => Array( + // 0 => Array( + // 'conversions' => 500, + // 'fields' => Array( + // 'subjects' => Array( + // 0 => Array( + // 'subject' => 'Tell Senate: No attacks on immigrants', + // 'enabled' => true + // ), + // 1 => Array( + // 'subject' => "Block Trump's attacks on immigrants", + // 'enabled' => true + // ), + // ), + // 'from_line' => 41, + // 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', + // 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + // 'salutation' => '{{ user.first_name|default:"Hi" }},', + // ), + // 'id' => 263, + // 'losses' => 0, + // 'sent' => 1000, + // 'subjects' => Array( + // 0 => Array( + // 'conversions' => 500, + // 'losses' => 0, + // 'sent' => 500, + // 'title' => 'Tell Senate: No attacks on immigrants', + // 'rate' => 0.8333333333333333, + // 'share' => 0.7142857142857143 + // ), + // 1 => Array( + // 'conversions' => 0, + // 'losses' => 0, + // 'sent' => 500, + // 'title' => "Block Trump's attacks on immigrants", + // 'rate' => 0.3333333333333333, + // 'share' => 0.28571428571428575 + // ) + // ), + // 'title' => 'No wall testing', + // 'valid' => true, + // 'rate' => 0.5555555555555555, + // 'share' => 1.0, + // 'limit' => 4.0 + // ) + // ), + // 'overall' => Array( + // 'conversions' => 500, + // 'losses' => 0, + // 'sent' => 1000, + // 'boost' => 500, + // 'rate' => 0.6666666666666666 + // ) + // ) + // ) + // ); + // $this->assertEquals(Array( + // 0 => Array( + // 'from_line' => 41, + // 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', + // 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + // 'campaign_id' => 263, + // 'limit' => 3.0, + // 'salutation' => '{{ user.first_name|default:"Hi" }},', + // 'subject' => 'Tell Senate: No attacks on immigrants', + // 'subscribers' => Array ( + // 0 => '6630475', + // 1 => '6630477', + // 2 => '6630478' + // ), + // 'url' => 'https://victorykit.local/c/no-wall-testing-2/', + // 'variation_subject' => 0 + // ), + // 1 => Array( + // 'from_line' => 41, + // 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', + // 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + // 'campaign_id' => 263, + // 'limit' => 1.0, + // 'salutation' => '{{ user.first_name|default:"Hi" }},', + // 'subject' => "Block Trump's attacks on immigrants", + // 'subscribers' => Array ( + // 0 => '6630479' + // ), + // 'url' => 'https://victorykit.local/c/no-wall-testing-2/', + // 'variation_subject' => 1 + // ) + // ),vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock)); + // } + public function test_vk_mailings_create_new_mailings_both_mailings_failing(): void { $vk_mailings_mock = $this->createMock(Mailings::class); $wpdb = new wpdb(); @@ -751,7 +986,7 @@ public function test_vk_mailings_create_new_mailings_action_return_value(): void ->method('get_fresh_subscribers_for_campaign') ->will($this->returnValue(['6630475', '6630477', '6630478','6630479','6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479'])); - $mhMock->expects($this->exactly(2)) + $mhMock->expects($this->exactly(0)) ->method('send'); $mhMock->expects($this->once()) @@ -761,95 +996,60 @@ public function test_vk_mailings_create_new_mailings_action_return_value(): void $vk_mailings_mock->expects($this->once()) ->method('get_distributions') ->will($this->returnValue(Array( - 'campaigns' => Array( - 0 => Array( - 'conversions' => 500, - 'fields' => Array( - 'subjects' => Array( - 0 => Array( - 'subject' => 'Tell Senate: No attacks on immigrants', - 'enabled' => true - ), - 1 => Array( - 'subject' => "Block Trump's attacks on immigrants", - 'enabled' => true - ), - ), - 'from_line' => 41, - 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', - 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', - 'salutation' => '{{ user.first_name|default:"Hi" }},', + 'campaigns' => Array( + 0 => Array( + 'conversions' => 0, + 'fields' => Array( + 'subjects' => Array( + 0 => Array( + 'subject' => 'Tell Senate: No attacks on immigrants', + 'enabled' => true ), - 'id' => 263, + 1 => Array( + 'subject' => "Block Trump's attacks on immigrants", + 'enabled' => true + ) + ) + ), + 'id' => 263, + 'losses' => 500, + 'sent' => 1000, + 'subjects' => Array( + 0 => Array( + 'conversions' => 0, 'losses' => 0, - 'sent' => 1000, - 'subjects' => Array( - 0 => Array( - 'conversions' => 500, - 'losses' => 0, - 'sent' => 500, - 'title' => 'Tell Senate: No attacks on immigrants', - 'rate' => 0.8333333333333333, - 'share' => 0.7142857142857143 - ), - 1 => Array( - 'conversions' => 0, - 'losses' => 0, - 'sent' => 500, - 'title' => "Block Trump's attacks on immigrants", - 'rate' => 0.3333333333333333, - 'share' => 0.28571428571428575 - ) - ), - 'title' => 'No wall testing', - 'valid' => true, - 'rate' => 0.5555555555555555, - 'share' => 1.0, - 'limit' => 4.0 + 'sent' => 500, + 'title' => 'Tell Senate: No attacks on immigrants', + 'rate' => 0, + 'share' => 0 + ), + 1 => Array( + 'conversions' => 0, + 'losses' => 500, + 'sent' => 500, + 'title' => "Block Trump's attacks on immigrants", + 'rate' => 0, + 'share' => 0 ) ), - 'overall' => Array( - 'conversions' => 500, - 'losses' => 0, - 'sent' => 1000, - 'boost' => 500, - 'rate' => 0.6666666666666666 - ) + 'title' => 'No wall testing', + 'valid' => true, + 'rate' => 0, + 'share' => 0, + 'limit' => 0.0 ) - ) - ); - $this->assertEquals(Array( - 0 => Array( - 'from_line' => 41, - 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', - 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', - 'campaign_id' => 263, - 'limit' => 3.0, - 'salutation' => '{{ user.first_name|default:"Hi" }},', - 'subject' => 'Tell Senate: No attacks on immigrants', - 'subscribers' => Array ( - 0 => '6630475', - 1 => '6630477', - 2 => '6630478' - ), - 'url' => 'https://victorykit.local/c/no-wall-testing-2/', - 'variation_subject' => 0 ), - 1 => Array( - 'from_line' => 41, - 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', - 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', - 'campaign_id' => 263, - 'limit' => 1.0, - 'salutation' => '{{ user.first_name|default:"Hi" }},', - 'subject' => "Block Trump's attacks on immigrants", - 'subscribers' => Array ( - 0 => '6630479' - ), - 'url' => 'https://victorykit.local/c/no-wall-testing-2/', - 'variation_subject' => 1 + 'overall' => Array( + 'conversions' => 0, + 'losses' => 500, + 'sent' => 1000, + 'boost' => 500, + 'rate' => 0 ) - ),vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock)); + ) + ) + ); + $this->assertEquals(Array(),vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock)); } } From 5acc1a0c242b6980828fdf8c4e233c82704500fd Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Fri, 2 Nov 2018 14:25:25 -0700 Subject: [PATCH 52/65] Add testing of campaign with 50% success rate. --- addons/tests/mailing_test.php | 430 +++++++++++++++++++++------------- 1 file changed, 272 insertions(+), 158 deletions(-) diff --git a/addons/tests/mailing_test.php b/addons/tests/mailing_test.php index 256deb7..f253bda 100644 --- a/addons/tests/mailing_test.php +++ b/addons/tests/mailing_test.php @@ -740,125 +740,125 @@ final class mailingsClass extends TestCase // } // ****play with mailing stats**** - // public function test_create_mailings_mailing_no_conversions_only_losses(): void - // { - // $mailings = new Mailings(); - // $wpdb = new wpdb(); - // $postObject = new stdClass(); + public function test_create_mailings_mailing_no_conversions_only_losses(): void + { + $mailings = new Mailings(); + $wpdb = new wpdb(); + $postObject = new stdClass(); - // $postObject->posts = array (); + $postObject->posts = array (); - // //** must update return value from campaigns + //** must update return value from campaigns - // $campaigns = array( - // 263 => array( - // "conversions" => 0, - // "fields" => array( - // "subjects" => array ( - // 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), - // 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ), - // ) - // ), - // "id" => 263, - // "losses" => 0, - // "sent" => 0, - // "subjects" => array (), - // "title" => "No wall testing", - // "valid" => true - // ) - // ); - // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + $campaigns = array( + 263 => array( + "conversions" => 0, + "fields" => array( + "subjects" => array ( + 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), + 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ), + ) + ), + "id" => 263, + "losses" => 0, + "sent" => 0, + "subjects" => array (), + "title" => "No wall testing", + "valid" => true + ) + ); + $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - // $mailings_stats = array ( - // 0 => array ( - // 'campaign_id' => '263', - // 'variation_subject' => '0', - // 'conversions' => '0', - // 'losses' => '0', - // 'sent' => '500', ), - // 1 => array ( - // 'campaign_id' => '263', - // 'variation_subject' => '1', - // 'conversions' => '0', - // 'losses' => '500', - // 'sent' => '500', ) - // ); + $mailings_stats = array ( + 0 => array ( + 'campaign_id' => '263', + 'variation_subject' => '0', + 'conversions' => '250', + 'losses' => '250', + 'sent' => '500', ), + 1 => array ( + 'campaign_id' => '263', + 'variation_subject' => '1', + 'conversions' => '0', + 'losses' => '0', + 'sent' => '500', ) + ); - // $mhMock= $this->getMockBuilder(MailingsHelpers::class) - // ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) - // ->getMock(); + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) + ->getMock(); - // $mhMock->expects($this->once()) - // ->method('wp_query_posts') - // ->will($this->returnValue($postObject)); + $mhMock->expects($this->once()) + ->method('wp_query_posts') + ->will($this->returnValue($postObject)); - // $mhMock->expects($this->once()) - // ->method('setUpCampaigns') - // ->will($this->returnValue($campaigns)); + $mhMock->expects($this->once()) + ->method('setUpCampaigns') + ->will($this->returnValue($campaigns)); - // $mhMock->expects($this->once()) - // ->method('get_mailings_results_wpdb') - // ->will($this->returnValue($mailings_stats)); + $mhMock->expects($this->once()) + ->method('get_mailings_results_wpdb') + ->will($this->returnValue($mailings_stats)); - // $mhMock->expects($this->once()) - // ->method('get_fresh_subscribers_for_campaign') - // ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); + $mhMock->expects($this->once()) + ->method('get_fresh_subscribers_for_campaign') + ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); - // $this->assertEquals(array( - // 'campaigns' => Array( - // 0 => Array( - // 'conversions' => 0, - // 'fields' => Array( - // 'subjects' => Array( - // 0 => Array( - // 'subject' => 'Tell Senate: No attacks on immigrants', - // 'enabled' => true - // ), - // 1 => Array( - // 'subject' => "Block Trump's attacks on immigrants", - // 'enabled' => true - // ) - // ) - // ), - // 'id' => 263, - // 'losses' => 500, - // 'sent' => 1000, - // 'subjects' => Array( - // 0 => Array( - // 'conversions' => 0, - // 'losses' => 0, - // 'sent' => 500, - // 'title' => 'Tell Senate: No attacks on immigrants', - // 'rate' => 0, - // 'share' => 0 - // ), - // 1 => Array( - // 'conversions' => 0, - // 'losses' => 500, - // 'sent' => 500, - // 'title' => "Block Trump's attacks on immigrants", - // 'rate' => 0, - // 'share' => 0 - // ) - // ), - // 'title' => 'No wall testing', - // 'valid' => true, - // 'rate' => 0, - // 'share' => 0, - // 'limit' => 0.0 - // ) - // ), - // 'overall' => Array( - // 'conversions' => 0, - // 'losses' => 500, - // 'sent' => 1000, - // 'boost' => 500, - // 'rate' => 0 - // ) - // ), $mailings->get_distributions($wpdb, $mhMock)); + $this->assertEquals(array( + 'campaigns' => Array( + 0 => Array( + 'conversions' => 250, + 'fields' => Array( + 'subjects' => Array( + 0 => Array( + 'subject' => 'Tell Senate: No attacks on immigrants', + 'enabled' => true + ), + 1 => Array( + 'subject' => "Block Trump's attacks on immigrants", + 'enabled' => true + ) + ) + ), + 'id' => 263, + 'losses' => 250, + 'sent' => 1000, + 'subjects' => Array( + 0 => Array( + 'conversions' => 250, + 'losses' => 250, + 'sent' => 500, + 'title' => 'Tell Senate: No attacks on immigrants', + 'rate' => 0.16666666666666666, + 'share' => 0.5 + ), + 1 => Array( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 500, + 'title' => "Block Trump's attacks on immigrants", + 'rate' => 0.16666666666666666, + 'share' => 0.5 + ) + ), + 'title' => 'No wall testing', + 'valid' => true, + 'rate' => 0.1111111111111111, + 'share' => 1.0, + 'limit' => 4.0 + ) + ), + 'overall' => Array( + 'conversions' => 250, + 'losses' => 250, + 'sent' => 1000, + 'boost' => 500, + 'rate' => 0.3333333333333333 + ) + ), $mailings->get_distributions($wpdb, $mhMock)); - // // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock); - // } + // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock); + } // public function test_vk_mailings_create_new_mailings_action_return_value(): void // { @@ -973,7 +973,86 @@ final class mailingsClass extends TestCase // ) // ),vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock)); // } - public function test_vk_mailings_create_new_mailings_both_mailings_failing(): void + // public function test_vk_mailings_create_new_mailings_both_mailings_failing(): void + // { + // $vk_mailings_mock = $this->createMock(Mailings::class); + // $wpdb = new wpdb(); + + // $mhMock= $this->getMockBuilder(MailingsHelpers::class) + // ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign', 'send', 'get_url']) + // ->getMock(); + + // $mhMock->expects($this->once()) + // ->method('get_fresh_subscribers_for_campaign') + // ->will($this->returnValue(['6630475', '6630477', '6630478','6630479','6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479'])); + + // $mhMock->expects($this->exactly(0)) + // ->method('send'); + + // $mhMock->expects($this->once()) + // ->method('get_url') + // ->willReturn('https://victorykit.local/c/no-wall-testing-2/'); + + // $vk_mailings_mock->expects($this->once()) + // ->method('get_distributions') + // ->will($this->returnValue(Array( + // 'campaigns' => Array( + // 0 => Array( + // 'conversions' => 0, + // 'fields' => Array( + // 'subjects' => Array( + // 0 => Array( + // 'subject' => 'Tell Senate: No attacks on immigrants', + // 'enabled' => true + // ), + // 1 => Array( + // 'subject' => "Block Trump's attacks on immigrants", + // 'enabled' => true + // ) + // ) + // ), + // 'id' => 263, + // 'losses' => 500, + // 'sent' => 1000, + // 'subjects' => Array( + // 0 => Array( + // 'conversions' => 0, + // 'losses' => 0, + // 'sent' => 500, + // 'title' => 'Tell Senate: No attacks on immigrants', + // 'rate' => 0, + // 'share' => 0 + // ), + // 1 => Array( + // 'conversions' => 0, + // 'losses' => 500, + // 'sent' => 500, + // 'title' => "Block Trump's attacks on immigrants", + // 'rate' => 0, + // 'share' => 0 + // ) + // ), + // 'title' => 'No wall testing', + // 'valid' => true, + // 'rate' => 0, + // 'share' => 0, + // 'limit' => 0.0 + // ) + // ), + // 'overall' => Array( + // 'conversions' => 0, + // 'losses' => 500, + // 'sent' => 1000, + // 'boost' => 500, + // 'rate' => 0 + // ) + // ) + // ) + // ); + // $this->assertEquals(Array(),vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock)); + // } + + public function test_vk_mailings_create_new_mailings_action_one_subject_50_percent_success_rate(): void { $vk_mailings_mock = $this->createMock(Mailings::class); $wpdb = new wpdb(); @@ -986,7 +1065,7 @@ public function test_vk_mailings_create_new_mailings_both_mailings_failing(): vo ->method('get_fresh_subscribers_for_campaign') ->will($this->returnValue(['6630475', '6630477', '6630478','6630479','6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479'])); - $mhMock->expects($this->exactly(0)) + $mhMock->expects($this->exactly(2)) ->method('send'); $mhMock->expects($this->once()) @@ -996,60 +1075,95 @@ public function test_vk_mailings_create_new_mailings_both_mailings_failing(): vo $vk_mailings_mock->expects($this->once()) ->method('get_distributions') ->will($this->returnValue(Array( - 'campaigns' => Array( - 0 => Array( - 'conversions' => 0, - 'fields' => Array( - 'subjects' => Array( - 0 => Array( - 'subject' => 'Tell Senate: No attacks on immigrants', - 'enabled' => true - ), - 1 => Array( - 'subject' => "Block Trump's attacks on immigrants", - 'enabled' => true - ) - ) - ), - 'id' => 263, - 'losses' => 500, - 'sent' => 1000, - 'subjects' => Array( + 'campaigns' => Array( 0 => Array( - 'conversions' => 0, - 'losses' => 0, - 'sent' => 500, - 'title' => 'Tell Senate: No attacks on immigrants', - 'rate' => 0, - 'share' => 0 - ), - 1 => Array( - 'conversions' => 0, - 'losses' => 500, - 'sent' => 500, - 'title' => "Block Trump's attacks on immigrants", - 'rate' => 0, - 'share' => 0 + 'conversions' => 250, + 'fields' => Array( + 'subjects' => Array( + 0 => Array( + 'subject' => 'Tell Senate: No attacks on immigrants', + 'enabled' => true + ), + 1 => Array( + 'subject' => "Block Trump's attacks on immigrants", + 'enabled' => true + ) + ), + 'from_line' => 41, + 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', + 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + 'salutation' => '{{ user.first_name|default:"Hi" }},', + ), + 'id' => 263, + 'losses' => 250, + 'sent' => 1000, + 'subjects' => Array( + 0 => Array( + 'conversions' => 250, + 'losses' => 250, + 'sent' => 500, + 'title' => 'Tell Senate: No attacks on immigrants', + 'rate' => 0.16666666666666666, + 'share' => 0.5 + ), + 1 => Array( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 500, + 'title' => "Block Trump's attacks on immigrants", + 'rate' => 0.16666666666666666, + 'share' => 0.5 + ) + ), + 'title' => 'No wall testing', + 'valid' => true, + 'rate' => 0.1111111111111111, + 'share' => 1.0, + 'limit' => 4.0 ) ), - 'title' => 'No wall testing', - 'valid' => true, - 'rate' => 0, - 'share' => 0, - 'limit' => 0.0 + 'overall' => Array( + 'conversions' => 250, + 'losses' => 250, + 'sent' => 1000, + 'boost' => 500, + 'rate' => 0.3333333333333333 + ) ) - ), - 'overall' => Array( - 'conversions' => 0, - 'losses' => 500, - 'sent' => 1000, - 'boost' => 500, - 'rate' => 0 - ) - ) ) ); - $this->assertEquals(Array(),vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock)); + $this->assertEquals(Array( + 0 => Array( + 'from_line' => 41, + 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', + 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + 'campaign_id' => 263, + 'limit' => 2.0, + 'salutation' => '{{ user.first_name|default:"Hi" }},', + 'subject' => 'Tell Senate: No attacks on immigrants', + 'subscribers' => Array ( + 0 => '6630475', + 1 => '6630477' + ), + 'url' => 'https://victorykit.local/c/no-wall-testing-2/', + 'variation_subject' => 0 + ), + 1 => Array( + 'from_line' => 41, + 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', + 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + 'campaign_id' => 263, + 'limit' => 2.0, + 'salutation' => '{{ user.first_name|default:"Hi" }},', + 'subject' => "Block Trump's attacks on immigrants", + 'subscribers' => Array ( + 0 => '6630478', + 1 => '6630479' + ), + 'url' => 'https://victorykit.local/c/no-wall-testing-2/', + 'variation_subject' => 1 + ) + ),vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock)); } } From 54f7dd08973414a3903996438ba7382acf7623bf Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Tue, 6 Nov 2018 09:45:56 -0800 Subject: [PATCH 53/65] Add create mailings test for two campaigns. --- addons/tests/mailing_test.php | 669 +++++++++++++++++++++++----------- 1 file changed, 463 insertions(+), 206 deletions(-) diff --git a/addons/tests/mailing_test.php b/addons/tests/mailing_test.php index f253bda..72bcde7 100644 --- a/addons/tests/mailing_test.php +++ b/addons/tests/mailing_test.php @@ -738,127 +738,126 @@ final class mailingsClass extends TestCase // ) // ), $mailings->get_distributions($wpdb, $mhMock)); // } - - // ****play with mailing stats**** - public function test_create_mailings_mailing_no_conversions_only_losses(): void - { - $mailings = new Mailings(); - $wpdb = new wpdb(); - $postObject = new stdClass(); + + // public function test_create_mailings_mailing_no_conversions_only_losses(): void + // { + // $mailings = new Mailings(); + // $wpdb = new wpdb(); + // $postObject = new stdClass(); - $postObject->posts = array (); + // $postObject->posts = array (); - //** must update return value from campaigns + // //** must update return value from campaigns - $campaigns = array( - 263 => array( - "conversions" => 0, - "fields" => array( - "subjects" => array ( - 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), - 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ), - ) - ), - "id" => 263, - "losses" => 0, - "sent" => 0, - "subjects" => array (), - "title" => "No wall testing", - "valid" => true - ) - ); - $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + // $campaigns = array( + // 263 => array( + // "conversions" => 0, + // "fields" => array( + // "subjects" => array ( + // 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), + // 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ), + // ) + // ), + // "id" => 263, + // "losses" => 0, + // "sent" => 0, + // "subjects" => array (), + // "title" => "No wall testing", + // "valid" => true + // ) + // ); + // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - $mailings_stats = array ( - 0 => array ( - 'campaign_id' => '263', - 'variation_subject' => '0', - 'conversions' => '250', - 'losses' => '250', - 'sent' => '500', ), - 1 => array ( - 'campaign_id' => '263', - 'variation_subject' => '1', - 'conversions' => '0', - 'losses' => '0', - 'sent' => '500', ) - ); + // $mailings_stats = array ( + // 0 => array ( + // 'campaign_id' => '263', + // 'variation_subject' => '0', + // 'conversions' => '250', + // 'losses' => '250', + // 'sent' => '500', ), + // 1 => array ( + // 'campaign_id' => '263', + // 'variation_subject' => '1', + // 'conversions' => '0', + // 'losses' => '0', + // 'sent' => '500', ) + // ); - $mhMock= $this->getMockBuilder(MailingsHelpers::class) - ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) - ->getMock(); + // $mhMock= $this->getMockBuilder(MailingsHelpers::class) + // ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) + // ->getMock(); - $mhMock->expects($this->once()) - ->method('wp_query_posts') - ->will($this->returnValue($postObject)); + // $mhMock->expects($this->once()) + // ->method('wp_query_posts') + // ->will($this->returnValue($postObject)); - $mhMock->expects($this->once()) - ->method('setUpCampaigns') - ->will($this->returnValue($campaigns)); + // $mhMock->expects($this->once()) + // ->method('setUpCampaigns') + // ->will($this->returnValue($campaigns)); - $mhMock->expects($this->once()) - ->method('get_mailings_results_wpdb') - ->will($this->returnValue($mailings_stats)); + // $mhMock->expects($this->once()) + // ->method('get_mailings_results_wpdb') + // ->will($this->returnValue($mailings_stats)); - $mhMock->expects($this->once()) - ->method('get_fresh_subscribers_for_campaign') - ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); + // $mhMock->expects($this->once()) + // ->method('get_fresh_subscribers_for_campaign') + // ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); - $this->assertEquals(array( - 'campaigns' => Array( - 0 => Array( - 'conversions' => 250, - 'fields' => Array( - 'subjects' => Array( - 0 => Array( - 'subject' => 'Tell Senate: No attacks on immigrants', - 'enabled' => true - ), - 1 => Array( - 'subject' => "Block Trump's attacks on immigrants", - 'enabled' => true - ) - ) - ), - 'id' => 263, - 'losses' => 250, - 'sent' => 1000, - 'subjects' => Array( - 0 => Array( - 'conversions' => 250, - 'losses' => 250, - 'sent' => 500, - 'title' => 'Tell Senate: No attacks on immigrants', - 'rate' => 0.16666666666666666, - 'share' => 0.5 - ), - 1 => Array( - 'conversions' => 0, - 'losses' => 0, - 'sent' => 500, - 'title' => "Block Trump's attacks on immigrants", - 'rate' => 0.16666666666666666, - 'share' => 0.5 - ) - ), - 'title' => 'No wall testing', - 'valid' => true, - 'rate' => 0.1111111111111111, - 'share' => 1.0, - 'limit' => 4.0 - ) - ), - 'overall' => Array( - 'conversions' => 250, - 'losses' => 250, - 'sent' => 1000, - 'boost' => 500, - 'rate' => 0.3333333333333333 - ) - ), $mailings->get_distributions($wpdb, $mhMock)); + // $this->assertEquals(array( + // 'campaigns' => Array( + // 0 => Array( + // 'conversions' => 250, + // 'fields' => Array( + // 'subjects' => Array( + // 0 => Array( + // 'subject' => 'Tell Senate: No attacks on immigrants', + // 'enabled' => true + // ), + // 1 => Array( + // 'subject' => "Block Trump's attacks on immigrants", + // 'enabled' => true + // ) + // ) + // ), + // 'id' => 263, + // 'losses' => 250, + // 'sent' => 1000, + // 'subjects' => Array( + // 0 => Array( + // 'conversions' => 250, + // 'losses' => 250, + // 'sent' => 500, + // 'title' => 'Tell Senate: No attacks on immigrants', + // 'rate' => 0.16666666666666666, + // 'share' => 0.5 + // ), + // 1 => Array( + // 'conversions' => 0, + // 'losses' => 0, + // 'sent' => 500, + // 'title' => "Block Trump's attacks on immigrants", + // 'rate' => 0.16666666666666666, + // 'share' => 0.5 + // ) + // ), + // 'title' => 'No wall testing', + // 'valid' => true, + // 'rate' => 0.1111111111111111, + // 'share' => 1.0, + // 'limit' => 4.0 + // ) + // ), + // 'overall' => Array( + // 'conversions' => 250, + // 'losses' => 250, + // 'sent' => 1000, + // 'boost' => 500, + // 'rate' => 0.3333333333333333 + // ) + // ), $mailings->get_distributions($wpdb, $mhMock)); - // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock); - } + // // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock); + // } // public function test_vk_mailings_create_new_mailings_action_return_value(): void // { @@ -1052,118 +1051,376 @@ public function test_create_mailings_mailing_no_conversions_only_losses(): void // $this->assertEquals(Array(),vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock)); // } - public function test_vk_mailings_create_new_mailings_action_one_subject_50_percent_success_rate(): void + // public function test_vk_mailings_create_new_mailings_action_one_subject_50_percent_success_rate(): void + // { + // $vk_mailings_mock = $this->createMock(Mailings::class); + // $wpdb = new wpdb(); + + // $mhMock= $this->getMockBuilder(MailingsHelpers::class) + // ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign', 'send', 'get_url']) + // ->getMock(); + + // $mhMock->expects($this->once()) + // ->method('get_fresh_subscribers_for_campaign') + // ->will($this->returnValue(['6630475', '6630477', '6630478','6630479','6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479'])); + + // $mhMock->expects($this->exactly(2)) + // ->method('send'); + + // $mhMock->expects($this->once()) + // ->method('get_url') + // ->willReturn('https://victorykit.local/c/no-wall-testing-2/'); + + // $vk_mailings_mock->expects($this->once()) + // ->method('get_distributions') + // ->will($this->returnValue(Array( + // 'campaigns' => Array( + // 0 => Array( + // 'conversions' => 250, + // 'fields' => Array( + // 'subjects' => Array( + // 0 => Array( + // 'subject' => 'Tell Senate: No attacks on immigrants', + // 'enabled' => true + // ), + // 1 => Array( + // 'subject' => "Block Trump's attacks on immigrants", + // 'enabled' => true + // ) + // ), + // 'from_line' => 41, + // 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', + // 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + // 'salutation' => '{{ user.first_name|default:"Hi" }},', + // ), + // 'id' => 263, + // 'losses' => 250, + // 'sent' => 1000, + // 'subjects' => Array( + // 0 => Array( + // 'conversions' => 250, + // 'losses' => 250, + // 'sent' => 500, + // 'title' => 'Tell Senate: No attacks on immigrants', + // 'rate' => 0.16666666666666666, + // 'share' => 0.5 + // ), + // 1 => Array( + // 'conversions' => 0, + // 'losses' => 0, + // 'sent' => 500, + // 'title' => "Block Trump's attacks on immigrants", + // 'rate' => 0.16666666666666666, + // 'share' => 0.5 + // ) + // ), + // 'title' => 'No wall testing', + // 'valid' => true, + // 'rate' => 0.1111111111111111, + // 'share' => 1.0, + // 'limit' => 4.0 + // ) + // ), + // 'overall' => Array( + // 'conversions' => 250, + // 'losses' => 250, + // 'sent' => 1000, + // 'boost' => 500, + // 'rate' => 0.3333333333333333 + // ) + // ) + // ) + // ); + // $this->assertEquals(Array( + // 0 => Array( + // 'from_line' => 41, + // 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', + // 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + // 'campaign_id' => 263, + // 'limit' => 2.0, + // 'salutation' => '{{ user.first_name|default:"Hi" }},', + // 'subject' => 'Tell Senate: No attacks on immigrants', + // 'subscribers' => Array ( + // 0 => '6630475', + // 1 => '6630477' + // ), + // 'url' => 'https://victorykit.local/c/no-wall-testing-2/', + // 'variation_subject' => 0 + // ), + // 1 => Array( + // 'from_line' => 41, + // 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', + // 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + // 'campaign_id' => 263, + // 'limit' => 2.0, + // 'salutation' => '{{ user.first_name|default:"Hi" }},', + // 'subject' => "Block Trump's attacks on immigrants", + // 'subscribers' => Array ( + // 0 => '6630478', + // 1 => '6630479' + // ), + // 'url' => 'https://victorykit.local/c/no-wall-testing-2/', + // 'variation_subject' => 1 + // ) + // ),vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock)); + // } + + // ****function below plays with mailing stats**** + //next move to test with two campaigns + //each campaign below will only have one subject + //make one campaign not successful and another very successful + //make both campaigns equally sucessful + + // public function test_two_campaigns_one_subject_one_campaign_not_successful(): void + // { + // $vk_mailings_mock = $this->createMock(Mailings::class); + // $wpdb = new wpdb(); + + // $mhMock= $this->getMockBuilder(MailingsHelpers::class) + // ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign', 'send', 'get_url']) + // ->getMock(); + + // $mhMock->expects($this->once()) + // ->method('get_fresh_subscribers_for_campaign') + // ->will($this->returnValue(['6630475', '6630477', '6630478','6630479','6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479'])); + + // $mhMock->expects($this->exactly(2)) + // ->method('send'); + + // $mhMock->expects($this->once()) + // ->method('get_url') + // ->willReturn('https://victorykit.local/c/no-wall-testing-2/'); + + // $vk_mailings_mock->expects($this->once()) + // ->method('get_distributions') + // ->will($this->returnValue(Array( + // 'campaigns' => Array( + // 0 => Array( + // 'conversions' => 250, + // 'fields' => Array( + // 'subjects' => Array( + // 0 => Array( + // 'subject' => 'Tell Senate: No attacks on immigrants', + // 'enabled' => true + // ) + // ), + // 'from_line' => 41, + // 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', + // 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + // 'salutation' => '{{ user.first_name|default:"Hi" }},', + // ), + // 'id' => 263, + // 'losses' => 250, + // 'sent' => 1000, + // 'subjects' => Array( + // 0 => Array( + // 'conversions' => 250, + // 'losses' => 250, + // 'sent' => 500, + // 'title' => 'Tell Senate: No attacks on immigrants', + // 'rate' => 0.16666666666666666, + // 'share' => 0.5 + // ) + // ), + // 'title' => 'No wall testing', + // 'valid' => true, + // 'rate' => 0.1111111111111111, + // 'share' => 1.0, + // 'limit' => 4.0 + // ) + // ), + // 'overall' => Array( + // 'conversions' => 250, + // 'losses' => 250, + // 'sent' => 1000, + // 'boost' => 500, + // 'rate' => 0.3333333333333333 + // ) + // ) + // ) + // ); + // $this->assertEquals(Array( + // 0 => Array( + // 'from_line' => 41, + // 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', + // 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + // 'campaign_id' => 263, + // 'limit' => 2.0, + // 'salutation' => '{{ user.first_name|default:"Hi" }},', + // 'subject' => 'Tell Senate: No attacks on immigrants', + // 'subscribers' => Array ( + // 0 => '6630475', + // 1 => '6630477' + // ), + // 'url' => 'https://victorykit.local/c/no-wall-testing-2/', + // 'variation_subject' => 0 + // ), + // 1 => Array( + // 'from_line' => 41, + // 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', + // 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + // 'campaign_id' => 263, + // 'limit' => 2.0, + // 'salutation' => '{{ user.first_name|default:"Hi" }},', + // 'subject' => "Block Trump's attacks on immigrants", + // 'subscribers' => Array ( + // 0 => '6630478', + // 1 => '6630479' + // ), + // 'url' => 'https://victorykit.local/c/no-wall-testing-2/', + // 'variation_subject' => 1 + // ) + // ),vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock)); + // } + + public function test_create_mailings_mailing_two_campaigns(): void { - $vk_mailings_mock = $this->createMock(Mailings::class); + $mailings = new Mailings(); $wpdb = new wpdb(); + $postObject = new stdClass(); + + $postObject->posts = array (); + + //** must update return value from campaigns + + $campaigns = array( + 263 => array( + "conversions" => 0, + "fields" => array( + "subjects" => array ( + 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ) + ) + ), + "id" => 263, + "losses" => 0, + "sent" => 0, + "subjects" => array (), + "title" => "No wall testing", + "valid" => true + ), + 267 => array( + "conversions" => 0, + "fields" => array( + "subjects" => array ( + 0 => array ( 'subject' => 'Tell Congress: Keep my boss out of my DNA!', 'enabled' => true, ) + ) + ), + "id" => 267, + "losses" => 0, + "sent" => 0, + "subjects" => array (), + "title" => 'Genetic testing', + "valid" => true + ) + ); + $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + + $mailings_stats = array ( + 0 => array ( + 'campaign_id' => '263', + 'variation_subject' => '0', + 'conversions' => '500', + 'losses' => '0', + 'sent' => '500', ), + 1 => array ( + 'campaign_id' => '267', + 'variation_subject' => '0', + 'conversions' => '0', + 'losses' => '500', + 'sent' => '500', ) + ); $mhMock= $this->getMockBuilder(MailingsHelpers::class) - ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign', 'send', 'get_url']) + ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) ->getMock(); $mhMock->expects($this->once()) - ->method('get_fresh_subscribers_for_campaign') - ->will($this->returnValue(['6630475', '6630477', '6630478','6630479','6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479'])); - - $mhMock->expects($this->exactly(2)) - ->method('send'); + ->method('wp_query_posts') + ->will($this->returnValue($postObject)); $mhMock->expects($this->once()) - ->method('get_url') - ->willReturn('https://victorykit.local/c/no-wall-testing-2/'); + ->method('setUpCampaigns') + ->will($this->returnValue($campaigns)); + + $mhMock->expects($this->once()) + ->method('get_mailings_results_wpdb') + ->will($this->returnValue($mailings_stats)); + + $mhMock->expects($this->exactly(2)) + ->method('get_fresh_subscribers_for_campaign') + ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); - $vk_mailings_mock->expects($this->once()) - ->method('get_distributions') - ->will($this->returnValue(Array( - 'campaigns' => Array( - 0 => Array( - 'conversions' => 250, - 'fields' => Array( - 'subjects' => Array( - 0 => Array( - 'subject' => 'Tell Senate: No attacks on immigrants', - 'enabled' => true - ), - 1 => Array( - 'subject' => "Block Trump's attacks on immigrants", - 'enabled' => true - ) - ), - 'from_line' => 41, - 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', - 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', - 'salutation' => '{{ user.first_name|default:"Hi" }},', - ), - 'id' => 263, - 'losses' => 250, - 'sent' => 1000, - 'subjects' => Array( - 0 => Array( - 'conversions' => 250, - 'losses' => 250, - 'sent' => 500, - 'title' => 'Tell Senate: No attacks on immigrants', - 'rate' => 0.16666666666666666, - 'share' => 0.5 - ), - 1 => Array( - 'conversions' => 0, - 'losses' => 0, - 'sent' => 500, - 'title' => "Block Trump's attacks on immigrants", - 'rate' => 0.16666666666666666, - 'share' => 0.5 - ) - ), - 'title' => 'No wall testing', - 'valid' => true, - 'rate' => 0.1111111111111111, - 'share' => 1.0, - 'limit' => 4.0 + $this->assertEquals(Array( + 'campaigns' => Array( + 0 => Array( + 'conversions' => 500, + 'fields' => Array( + 'subjects' => Array( + 0 => Array ( + 'subject' => 'Tell Senate: No attacks on immigrants', + 'enabled' => true ) - ), - 'overall' => Array( - 'conversions' => 250, - 'losses' => 250, - 'sent' => 1000, - 'boost' => 500, - 'rate' => 0.3333333333333333 ) - ) - ) - ); - $this->assertEquals(Array( - 0 => Array( - 'from_line' => 41, - 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', - 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', - 'campaign_id' => 263, - 'limit' => 2.0, - 'salutation' => '{{ user.first_name|default:"Hi" }},', - 'subject' => 'Tell Senate: No attacks on immigrants', - 'subscribers' => Array ( - 0 => '6630475', - 1 => '6630477' ), - 'url' => 'https://victorykit.local/c/no-wall-testing-2/', - 'variation_subject' => 0 + 'id' => 263, + 'losses' => 0, + 'sent' => 500, + 'subjects' => Array( + 0 => Array( + 'conversions' => 500, + 'losses' => 0, + 'sent' => 500, + 'title' => 'Tell Senate: No attacks on immigrants', + 'rate' => 0.6666666666666666, + 'share' => 1.0 + ) + ), + 'title' => 'No wall testing', + 'valid' => true, + 'rate' => 0.6666666666666666, + 'share' => 1.0, + 'limit' => 4.0 ), 1 => Array( - 'from_line' => 41, - 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', - 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', - 'campaign_id' => 263, - 'limit' => 2.0, - 'salutation' => '{{ user.first_name|default:"Hi" }},', - 'subject' => "Block Trump's attacks on immigrants", - 'subscribers' => Array ( - 0 => '6630478', - 1 => '6630479' + 'conversions' => 0, + 'fields' => Array ( + 'subjects' => Array( + 0 => Array( + 'subject' => 'Tell Congress: Keep my boss out of my DNA!', + 'enabled' => true + ) + ) + ), + 'id' => 267, + 'losses' => 500, + 'sent' => 500, + 'subjects' => Array ( + 0 => Array( + 'conversions' => 0, + 'losses' => 500, + 'sent' => 500, + 'title' => 'Tell Congress: Keep my boss out of my DNA!', + 'rate' => 0, + 'share' => 0 + ) ), - 'url' => 'https://victorykit.local/c/no-wall-testing-2/', - 'variation_subject' => 1 - ) - ),vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock)); + 'title' => 'Genetic testing', + 'valid' => true, + 'rate' => 0, + 'share' => 0.0, + 'limit' => 0.0 + ), + ), + 'overall' => Array( + 'conversions' => 500, + 'losses' => 500, + 'sent' => 1000, + 'boost' => 500, + 'rate' => 0.3333333333333333 + ) + ), $mailings->get_distributions($wpdb, $mhMock)); + + // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock); } } From f9e7f22d4a4f9bc8b7a0695152cc56995acb9b95 Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Tue, 6 Nov 2018 15:56:13 -0800 Subject: [PATCH 54/65] Add test for two campaigns and one campaign failing. --- addons/tests/mailing_test.php | 467 ++++++++++++++++++---------------- 1 file changed, 241 insertions(+), 226 deletions(-) diff --git a/addons/tests/mailing_test.php b/addons/tests/mailing_test.php index 72bcde7..9019e87 100644 --- a/addons/tests/mailing_test.php +++ b/addons/tests/mailing_test.php @@ -1165,262 +1165,277 @@ final class mailingsClass extends TestCase // ),vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock)); // } - // ****function below plays with mailing stats**** - //next move to test with two campaigns - //each campaign below will only have one subject - //make one campaign not successful and another very successful - //make both campaigns equally sucessful - - // public function test_two_campaigns_one_subject_one_campaign_not_successful(): void + + // public function test_create_mailings_mailing_two_campaigns(): void // { - // $vk_mailings_mock = $this->createMock(Mailings::class); + // $mailings = new Mailings(); // $wpdb = new wpdb(); + // $postObject = new stdClass(); + + // $postObject->posts = array (); + + // $campaigns = array( + // 263 => array( + // "conversions" => 0, + // "fields" => array( + // "subjects" => array ( + // 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ) + // ) + // ), + // "id" => 263, + // "losses" => 0, + // "sent" => 0, + // "subjects" => array (), + // "title" => "No wall testing", + // "valid" => true + // ), + // 267 => array( + // "conversions" => 0, + // "fields" => array( + // "subjects" => array ( + // 0 => array ( 'subject' => 'Tell Congress: Keep my boss out of my DNA!', 'enabled' => true, ) + // ) + // ), + // "id" => 267, + // "losses" => 0, + // "sent" => 0, + // "subjects" => array (), + // "title" => 'Genetic testing', + // "valid" => true + // ) + // ); + // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + + // $mailings_stats = array ( + // 0 => array ( + // 'campaign_id' => '263', + // 'variation_subject' => '0', + // 'conversions' => '500', + // 'losses' => '0', + // 'sent' => '500', ), + // 1 => array ( + // 'campaign_id' => '267', + // 'variation_subject' => '0', + // 'conversions' => '0', + // 'losses' => '500', + // 'sent' => '500', ) + // ); // $mhMock= $this->getMockBuilder(MailingsHelpers::class) - // ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign', 'send', 'get_url']) + // ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) // ->getMock(); // $mhMock->expects($this->once()) - // ->method('get_fresh_subscribers_for_campaign') - // ->will($this->returnValue(['6630475', '6630477', '6630478','6630479','6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479'])); - - // $mhMock->expects($this->exactly(2)) - // ->method('send'); + // ->method('wp_query_posts') + // ->will($this->returnValue($postObject)); // $mhMock->expects($this->once()) - // ->method('get_url') - // ->willReturn('https://victorykit.local/c/no-wall-testing-2/'); + // ->method('setUpCampaigns') + // ->will($this->returnValue($campaigns)); + + // $mhMock->expects($this->once()) + // ->method('get_mailings_results_wpdb') + // ->will($this->returnValue($mailings_stats)); + + // $mhMock->expects($this->exactly(2)) + // ->method('get_fresh_subscribers_for_campaign') + // ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); - // $vk_mailings_mock->expects($this->once()) - // ->method('get_distributions') - // ->will($this->returnValue(Array( - // 'campaigns' => Array( - // 0 => Array( - // 'conversions' => 250, - // 'fields' => Array( - // 'subjects' => Array( - // 0 => Array( - // 'subject' => 'Tell Senate: No attacks on immigrants', - // 'enabled' => true - // ) - // ), - // 'from_line' => 41, - // 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', - // 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', - // 'salutation' => '{{ user.first_name|default:"Hi" }},', - // ), - // 'id' => 263, - // 'losses' => 250, - // 'sent' => 1000, - // 'subjects' => Array( - // 0 => Array( - // 'conversions' => 250, - // 'losses' => 250, - // 'sent' => 500, - // 'title' => 'Tell Senate: No attacks on immigrants', - // 'rate' => 0.16666666666666666, - // 'share' => 0.5 - // ) - // ), - // 'title' => 'No wall testing', - // 'valid' => true, - // 'rate' => 0.1111111111111111, - // 'share' => 1.0, - // 'limit' => 4.0 + // $this->assertEquals(Array( + // 'campaigns' => Array( + // 0 => Array( + // 'conversions' => 500, + // 'fields' => Array( + // 'subjects' => Array( + // 0 => Array ( + // 'subject' => 'Tell Senate: No attacks on immigrants', + // 'enabled' => true // ) - // ), - // 'overall' => Array( - // 'conversions' => 250, - // 'losses' => 250, - // 'sent' => 1000, - // 'boost' => 500, - // 'rate' => 0.3333333333333333 // ) - // ) - // ) - // ); - // $this->assertEquals(Array( - // 0 => Array( - // 'from_line' => 41, - // 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', - // 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', - // 'campaign_id' => 263, - // 'limit' => 2.0, - // 'salutation' => '{{ user.first_name|default:"Hi" }},', - // 'subject' => 'Tell Senate: No attacks on immigrants', - // 'subscribers' => Array ( - // 0 => '6630475', - // 1 => '6630477' // ), - // 'url' => 'https://victorykit.local/c/no-wall-testing-2/', - // 'variation_subject' => 0 + // 'id' => 263, + // 'losses' => 0, + // 'sent' => 500, + // 'subjects' => Array( + // 0 => Array( + // 'conversions' => 500, + // 'losses' => 0, + // 'sent' => 500, + // 'title' => 'Tell Senate: No attacks on immigrants', + // 'rate' => 0.6666666666666666, + // 'share' => 1.0 + // ) + // ), + // 'title' => 'No wall testing', + // 'valid' => true, + // 'rate' => 0.6666666666666666, + // 'share' => 1.0, + // 'limit' => 4.0 // ), // 1 => Array( - // 'from_line' => 41, - // 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', - // 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', - // 'campaign_id' => 263, - // 'limit' => 2.0, - // 'salutation' => '{{ user.first_name|default:"Hi" }},', - // 'subject' => "Block Trump's attacks on immigrants", - // 'subscribers' => Array ( - // 0 => '6630478', - // 1 => '6630479' + // 'conversions' => 0, + // 'fields' => Array ( + // 'subjects' => Array( + // 0 => Array( + // 'subject' => 'Tell Congress: Keep my boss out of my DNA!', + // 'enabled' => true + // ) + // ) // ), - // 'url' => 'https://victorykit.local/c/no-wall-testing-2/', - // 'variation_subject' => 1 - // ) - // ),vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock)); + // 'id' => 267, + // 'losses' => 500, + // 'sent' => 500, + // 'subjects' => Array ( + // 0 => Array( + // 'conversions' => 0, + // 'losses' => 500, + // 'sent' => 500, + // 'title' => 'Tell Congress: Keep my boss out of my DNA!', + // 'rate' => 0, + // 'share' => 0 + // ) + // ), + // 'title' => 'Genetic testing', + // 'valid' => true, + // 'rate' => 0, + // 'share' => 0.0, + // 'limit' => 0.0 + // ), + // ), + // 'overall' => Array( + // 'conversions' => 500, + // 'losses' => 500, + // 'sent' => 1000, + // 'boost' => 500, + // 'rate' => 0.3333333333333333 + // ) + // ), $mailings->get_distributions($wpdb, $mhMock)); // } - public function test_create_mailings_mailing_two_campaigns(): void + + // ****function below plays with mailing stats**** + //next move to test with two campaigns + //each campaign below will only have one subject + //make one campaign not successful and another very successful + //make both campaigns equally sucessful + + public function test_two_campaigns_one_subject_one_campaign_not_successful(): void { - $mailings = new Mailings(); + $vk_mailings_mock = $this->createMock(Mailings::class); $wpdb = new wpdb(); - $postObject = new stdClass(); - - $postObject->posts = array (); - - //** must update return value from campaigns - - $campaigns = array( - 263 => array( - "conversions" => 0, - "fields" => array( - "subjects" => array ( - 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ) - ) - ), - "id" => 263, - "losses" => 0, - "sent" => 0, - "subjects" => array (), - "title" => "No wall testing", - "valid" => true - ), - 267 => array( - "conversions" => 0, - "fields" => array( - "subjects" => array ( - 0 => array ( 'subject' => 'Tell Congress: Keep my boss out of my DNA!', 'enabled' => true, ) - ) - ), - "id" => 267, - "losses" => 0, - "sent" => 0, - "subjects" => array (), - "title" => 'Genetic testing', - "valid" => true - ) - ); - $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - - $mailings_stats = array ( - 0 => array ( - 'campaign_id' => '263', - 'variation_subject' => '0', - 'conversions' => '500', - 'losses' => '0', - 'sent' => '500', ), - 1 => array ( - 'campaign_id' => '267', - 'variation_subject' => '0', - 'conversions' => '0', - 'losses' => '500', - 'sent' => '500', ) - ); $mhMock= $this->getMockBuilder(MailingsHelpers::class) - ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) + ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign', 'send', 'get_url']) ->getMock(); + $mhMock->expects($this->exactly(2)) + ->method('get_fresh_subscribers_for_campaign') + ->will($this->returnValue(['6630475', '6630477', '6630478','6630479','6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479'])); + $mhMock->expects($this->once()) - ->method('wp_query_posts') - ->will($this->returnValue($postObject)); - - $mhMock->expects($this->once()) - ->method('setUpCampaigns') - ->will($this->returnValue($campaigns)); - - $mhMock->expects($this->once()) - ->method('get_mailings_results_wpdb') - ->will($this->returnValue($mailings_stats)); + ->method('send'); $mhMock->expects($this->exactly(2)) - ->method('get_fresh_subscribers_for_campaign') - ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); + ->method('get_url') + ->willReturn('https://victorykit.local/c/no-wall-testing-2/'); - $this->assertEquals(Array( - 'campaigns' => Array( + $vk_mailings_mock->expects($this->once()) + ->method('get_distributions') + ->will($this->returnValue( + Array('campaigns' => Array( + 0 => Array( + 'conversions' => 500, + 'fields' => Array( + 'from_line' => '41', + 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', + 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + 'salutation' => '{{ user.first_name|default:"Hi" }},', + 'subjects' => Array( + 0 => Array ( + 'subject' => 'Tell Senate: No attacks on immigrants', + 'enabled' => true + ) + ) + ), + 'id' => 263, + 'losses' => 0, + 'sent' => 500, + 'subjects' => Array( + 0 => Array( + 'conversions' => 500, + 'losses' => 0, + 'sent' => 500, + 'title' => 'Tell Senate: No attacks on immigrants', + 'rate' => 0.6666666666666666, + 'share' => 1.0 + ) + ), + 'title' => 'No wall testing', + 'valid' => true, + 'rate' => 0.6666666666666666, + 'share' => 1.0, + 'limit' => 4.0 + ), + 1 => Array( + 'conversions' => 0, + 'fields' => Array ( + 'subjects' => Array( + 0 => Array( + 'subject' => 'Tell Congress: Keep my boss out of my DNA!', + 'enabled' => true + ) + ) + ), + 'id' => 267, + 'losses' => 500, + 'sent' => 500, + 'subjects' => Array ( + 0 => Array( + 'conversions' => 0, + 'losses' => 500, + 'sent' => 500, + 'title' => 'Tell Congress: Keep my boss out of my DNA!', + 'rate' => 0, + 'share' => 0 + ) + ), + 'title' => 'Genetic testing', + 'valid' => true, + 'rate' => 0, + 'share' => 0.0, + 'limit' => 0.0 + ), + ), + 'overall' => Array( + 'conversions' => 500, + 'losses' => 500, + 'sent' => 1000, + 'boost' => 500, + 'rate' => 0.3333333333333333 + ) + ) + ) + ); + $this->assertEquals(Array( 0 => Array( - 'conversions' => 500, - 'fields' => Array( - 'subjects' => Array( - 0 => Array ( - 'subject' => 'Tell Senate: No attacks on immigrants', - 'enabled' => true - ) - ) - ), - 'id' => 263, - 'losses' => 0, - 'sent' => 500, - 'subjects' => Array( - 0 => Array( - 'conversions' => 500, - 'losses' => 0, - 'sent' => 500, - 'title' => 'Tell Senate: No attacks on immigrants', - 'rate' => 0.6666666666666666, - 'share' => 1.0 - ) + 'from_line' => '41', + 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', + 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + 'campaign_id' => 263, + 'limit' => 4.0, + 'salutation' => '{{ user.first_name|default:"Hi" }},', + 'subject' => 'Tell Senate: No attacks on immigrants', + 'subscribers' => Array ( + 0 => '6630475', + 1 => '6630477', + 2 => '6630478', + 3 => '6630479' ), - 'title' => 'No wall testing', - 'valid' => true, - 'rate' => 0.6666666666666666, - 'share' => 1.0, - 'limit' => 4.0 - ), - 1 => Array( - 'conversions' => 0, - 'fields' => Array ( - 'subjects' => Array( - 0 => Array( - 'subject' => 'Tell Congress: Keep my boss out of my DNA!', - 'enabled' => true - ) - ) - ), - 'id' => 267, - 'losses' => 500, - 'sent' => 500, - 'subjects' => Array ( - 0 => Array( - 'conversions' => 0, - 'losses' => 500, - 'sent' => 500, - 'title' => 'Tell Congress: Keep my boss out of my DNA!', - 'rate' => 0, - 'share' => 0 - ) - ), - 'title' => 'Genetic testing', - 'valid' => true, - 'rate' => 0, - 'share' => 0.0, - 'limit' => 0.0 - ), - ), - 'overall' => Array( - 'conversions' => 500, - 'losses' => 500, - 'sent' => 1000, - 'boost' => 500, - 'rate' => 0.3333333333333333 - ) - ), $mailings->get_distributions($wpdb, $mhMock)); - - // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock); + 'url' => 'https://victorykit.local/c/no-wall-testing-2/', + 'variation_subject' => 0 + + ) + ),vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock)); } } From 27eda6b9eb34b8120c2674ee19edc7a2e8b0b717 Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Wed, 7 Nov 2018 09:14:32 -0800 Subject: [PATCH 55/65] Add test for two campaigns and one campaign not successful. --- addons/tests/mailing_test.php | 2467 +++++++++++++++++---------------- 1 file changed, 1292 insertions(+), 1175 deletions(-) diff --git a/addons/tests/mailing_test.php b/addons/tests/mailing_test.php index 9019e87..e8b252a 100644 --- a/addons/tests/mailing_test.php +++ b/addons/tests/mailing_test.php @@ -32,1292 +32,1281 @@ function query(){} final class mailingsClass extends TestCase { - // public function test_vk_mailings_create_new_mailings_action_two_campaigns(): void - // { - // global $TwoCampaigns; + public function test_vk_mailings_create_new_mailings_action_two_campaigns(): void + { + global $TwoCampaigns; - // $vk_mailings_mock = $this->createMock(Mailings::class); - // $wpdb_mock = $this->createMock(wpdb::class); - // $mhMock= $this->createMock(MailingsHelpers::class); + $vk_mailings_mock = $this->createMock(Mailings::class); + $wpdb_mock = $this->createMock(wpdb::class); + $mhMock= $this->createMock(MailingsHelpers::class); - // $vk_mailings_mock->expects($this->once()) - // ->method('get_distributions') - // ->willReturn($TwoCampaigns); + $vk_mailings_mock->expects($this->once()) + ->method('get_distributions') + ->willReturn($TwoCampaigns); - // $mhMock->expects($this->exactly(2)) - // ->method('get_fresh_subscribers_for_campaign') - // ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + $mhMock->expects($this->exactly(2)) + ->method('get_fresh_subscribers_for_campaign') + ->willReturn(array( 0 => 6630470, 1 => 6630472,)); - // $mhMock->expects($this->once()) - // ->method('getoption') - // ->willReturn(4); + $mhMock->expects($this->once()) + ->method('getoption') + ->willReturn(4); - // $mhMock->expects($this->exactly(3)) - // ->method('send'); + $mhMock->expects($this->exactly(3)) + ->method('send'); - // $wpdb_mock->expects($this->exactly(1)) - // ->method('query'); + $wpdb_mock->expects($this->exactly(1)) + ->method('query'); - // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); - // } + vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); + } - // public function test_vk_mailings_create_new_mailings_action_one_campaigns(): void - // { - // global $singleCampaign; + public function test_vk_mailings_create_new_mailings_action_one_campaigns(): void + { + global $singleCampaign; - // $vk_mailings_mock = $this->createMock(Mailings::class); - // $wpdb_mock = $this->createMock(wpdb::class); - // $mhMock = $this->createMock(MailingsHelpers::class); + $vk_mailings_mock = $this->createMock(Mailings::class); + $wpdb_mock = $this->createMock(wpdb::class); + $mhMock = $this->createMock(MailingsHelpers::class); - // $vk_mailings_mock->expects($this->once()) - // ->method('get_distributions') - // ->willReturn($singleCampaign); + $vk_mailings_mock->expects($this->once()) + ->method('get_distributions') + ->willReturn($singleCampaign); - // $mhMock->expects($this->exactly(1)) - // ->method('get_fresh_subscribers_for_campaign') - // ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + $mhMock->expects($this->exactly(1)) + ->method('get_fresh_subscribers_for_campaign') + ->willReturn(array( 0 => 6630470, 1 => 6630472,)); - // $mhMock->expects($this->once()) - // ->method('getoption') - // ->willReturn(4); + $mhMock->expects($this->once()) + ->method('getoption') + ->willReturn(4); - // $mhMock->expects($this->exactly(2)) - // ->method('send'); + $mhMock->expects($this->exactly(2)) + ->method('send'); - // $wpdb_mock->expects($this->exactly(1)) - // ->method('query'); + $wpdb_mock->expects($this->exactly(1)) + ->method('query'); - // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); - // } + vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); + } - // public function test_get_distribution_two_subject_campaign(): void - // { - // global $twoSubjectCampaign; + public function test_get_distribution_two_subject_campaign(): void + { + global $twoSubjectCampaign; - // $vk_mailings_mock = $this->createMock(Mailings::class); - // $wpdb_mock = $this->createMock(wpdb::class); - // $mhMock = $this->createMock(MailingsHelpers::class); + $vk_mailings_mock = $this->createMock(Mailings::class); + $wpdb_mock = $this->createMock(wpdb::class); + $mhMock = $this->createMock(MailingsHelpers::class); - // $vk_mailings_mock->expects($this->once()) - // ->method('get_distributions') - // ->willReturn($twoSubjectCampaign); + $vk_mailings_mock->expects($this->once()) + ->method('get_distributions') + ->willReturn($twoSubjectCampaign); - // $mhMock->expects($this->once()) - // ->method('get_fresh_subscribers_for_campaign') - // ->willReturn(array( 0 => 6630470, 1 => 6630472,)); + $mhMock->expects($this->once()) + ->method('get_fresh_subscribers_for_campaign') + ->willReturn(array( 0 => 6630470, 1 => 6630472,)); - // $mhMock->expects($this->once()) - // ->method('getoption') - // ->willReturn(4); + $mhMock->expects($this->once()) + ->method('getoption') + ->willReturn(4); - // $mhMock->expects($this->once()) - // ->method('send') - // ->willReturn(array ( 'ak_mailing_id' => 1)); + $mhMock->expects($this->once()) + ->method('send') + ->willReturn(array ( 'ak_mailing_id' => 1)); - // $wpdb_mock->expects($this->once()) - // ->method('query'); - - // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); - // } - - // public function test_mailingsHelpers_wp_query_posts_method(): void - // { - // $mailingsMock = new Mailings(); - // $wpdb = new wpdb(); - // $postObject = new stdClass(); - // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - - // $mhMock= $this->getMockBuilder(MailingsHelpers::class) - // ->setMethods(['wp_query_posts']) - // ->getMock(); - - // $mhMock->expects($this->once()) - // ->method('wp_query_posts') - // ->will($this->returnValue($postObject)); - - // $mailingsMock->get_distributions($wpdb, $mhMock); - // } - - // public function test_get_fields(): void - // { - // $wpdb = new wpdb(); - // $postObject = new stdClass(); - // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - - // $mhMock= $this->getMockBuilder(MailingsHelpers::class) - // ->setMethods(['wp_query_posts', 'getFields']) - // ->getMock(); + $wpdb_mock->expects($this->once()) + ->method('query'); + + vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); + } + + public function test_mailingsHelpers_wp_query_posts_method(): void + { + $mailingsMock = new Mailings(); + $wpdb = new wpdb(); + $postObject = new stdClass(); + $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['wp_query_posts']) + ->getMock(); + + $mhMock->expects($this->once()) + ->method('wp_query_posts') + ->will($this->returnValue($postObject)); + + $mailingsMock->get_distributions($wpdb, $mhMock); + } + + public function test_get_fields(): void + { + $wpdb = new wpdb(); + $postObject = new stdClass(); + $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['wp_query_posts', 'getFields']) + ->getMock(); - // $mhMock->expects($this->once()) - // ->method('wp_query_posts') - // ->will($this->returnValue($postObject)); + $mhMock->expects($this->once()) + ->method('wp_query_posts') + ->will($this->returnValue($postObject)); - // $mhMock->expects($this->once()) - // ->method('getFields'); + $mhMock->expects($this->once()) + ->method('getFields'); - // $subject = new Mailings(); + $subject = new Mailings(); - // $subject->get_distributions($wpdb, $mhMock); - // } - - // public function test_mailingsHelpers_setUpCampaigns(): void - // { - // $wpdb = new wpdb(); - // // $postObject = new stdClass(); - // $postObject = array(0 => (object) ['ID' => '', 'post_title'=> '']); - // $campaigns = array( 0 => array( 'fields'=> '', 'campaign_id' => '', 'subjects' => array())); - - // $mhMock= $this->getMockBuilder(MailingsHelpers::class) - // ->setMethods(['wp_query_posts', 'setUpCampaigns']) - // ->getMock(); + $subject->get_distributions($wpdb, $mhMock); + } + + public function test_mailingsHelpers_setUpCampaigns(): void + { + $wpdb = new wpdb(); + // $postObject = new stdClass(); + $postObject = array(0 => (object) ['ID' => '', 'post_title'=> '']); + $campaigns = array( 0 => array( 'fields'=> '', 'campaign_id' => '', 'subjects' => array())); + + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['wp_query_posts', 'setUpCampaigns']) + ->getMock(); - // $mhMock->expects($this->once()) - // ->method('wp_query_posts') - // ->will($this->returnValue($postObject)); + $mhMock->expects($this->once()) + ->method('wp_query_posts') + ->will($this->returnValue($postObject)); - // $mhMock->expects($this->once()) - // ->method('setUpCampaigns') - // ->will($this->returnValue($campaigns)); + $mhMock->expects($this->once()) + ->method('setUpCampaigns') + ->will($this->returnValue($campaigns)); - // $subject = new Mailings(); + $subject = new Mailings(); - // $subject->get_distributions($wpdb, $mhMock); - // } - - // public function test_mailingsHelpers_get_mailings_results_wpdb(): void - // { - // $wpdb = new wpdb(); - // // $postObject = new stdClass(); - // $postObject = array( 0 => array( 'campaign_id' => '1')); - - // $mhMock= $this->getMockBuilder(MailingsHelpers::class) - // ->setMethods(['get_mailings_results_wpdb']) - // ->getMock(); + $subject->get_distributions($wpdb, $mhMock); + } + + public function test_mailingsHelpers_get_mailings_results_wpdb(): void + { + $wpdb = new wpdb(); + // $postObject = new stdClass(); + $postObject = array( 0 => array( 'campaign_id' => '1')); + + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['get_mailings_results_wpdb']) + ->getMock(); - // $mhMock->expects($this->once()) - // ->method('get_mailings_results_wpdb') - // ->will($this->returnValue($postObject)); + $mhMock->expects($this->once()) + ->method('get_mailings_results_wpdb') + ->will($this->returnValue($postObject)); - // $subject = new Mailings(); + $subject = new Mailings(); - // $subject->get_distributions($wpdb, $mhMock); - // } - - // public function test_mailings_distribution_post_method(): void - // { - // $mailings = new Mailings(); - // $wpdb = new wpdb(); - // $postObject = new stdClass(); + $subject->get_distributions($wpdb, $mhMock); + } + + public function test_mailings_distribution_post_method(): void + { + $mailings = new Mailings(); + $wpdb = new wpdb(); + $postObject = new stdClass(); - // $postObject->posts = array ( 0 => (object)(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), ); + $postObject->posts = array ( 0 => (object)(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), ); - // //** must update return value from campaigns + //** must update return value from campaigns - // $campaigns = array( - // 263 => array( - // "conversions" => 0, - // "fields" => array( - // "subjects" => array ( 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ),) - // ), - // "id" => 263, - // "losses" => 0, - // "sent" => 0, - // "subjects" => array (), - // "title" => "No wall testing", - // "valid" => true - // ) - // ); - // // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + $campaigns = array( + 263 => array( + "conversions" => 0, + "fields" => array( + "subjects" => array ( 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ),) + ), + "id" => 263, + "losses" => 0, + "sent" => 0, + "subjects" => array (), + "title" => "No wall testing", + "valid" => true + ) + ); + // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - // $mailings_stats = array(0 => [ - // 'campaign_id' => 263, - // 'conversions' => 0, - // 'losses' => 0, - // 'sent' => 0, - // 'variation_subject' => 0 - // ]); + $mailings_stats = array(0 => [ + 'campaign_id' => 263, + 'conversions' => 0, + 'losses' => 0, + 'sent' => 0, + 'variation_subject' => 0 + ]); - // $mhMock= $this->getMockBuilder(MailingsHelpers::class) - // ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) - // ->getMock(); + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) + ->getMock(); - // $mhMock->expects($this->once()) - // ->method('wp_query_posts') - // ->will($this->returnValue($postObject)); + $mhMock->expects($this->once()) + ->method('wp_query_posts') + ->will($this->returnValue($postObject)); - // $mhMock->expects($this->once()) - // ->method('setUpCampaigns') - // ->will($this->returnValue($campaigns)); + $mhMock->expects($this->once()) + ->method('setUpCampaigns') + ->will($this->returnValue($campaigns)); - // $mhMock->expects($this->once()) - // ->method('get_mailings_results_wpdb') - // ->will($this->returnValue($mailings_stats)); + $mhMock->expects($this->once()) + ->method('get_mailings_results_wpdb') + ->will($this->returnValue($mailings_stats)); - // $mhMock->expects($this->once()) - // ->method('get_fresh_subscribers_for_campaign') - // ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); + $mhMock->expects($this->once()) + ->method('get_fresh_subscribers_for_campaign') + ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); - // $this->assertEquals(array( - // 'campaigns' => Array( - // 0 => Array( - // 'conversions' => 0, - // 'fields' => Array( - // 'subjects' => Array( - // 0 => Array( - // 'subject' => 'Tell Senate: No attacks on immigrants', - // 'enabled' => true - // ), - // 1 => Array( - // 'subject' => "Block Trump's attacks on immigrants", - // 'enabled' => true - // ) - // ) - // ), - // 'id' => 263, - // 'losses' => 0, - // 'sent' => 0, - // 'subjects' => Array( - // 0 => Array( - // 'conversions' => 0, - // 'losses' => 0, - // 'sent' => 0, - // 'title' => 'Tell Senate: No attacks on immigrants', - // 'rate' => 1, - // 'share' => 1 - // ) - // ), - // 'title' => 'No wall testing', - // 'valid' => true, - // 'rate' => 1, - // 'share' => 1, - // 'limit' => 4.0 - // ) - // ), - // 'overall' => Array( - // 'conversions' => 0, - // 'losses' => 0, - // 'sent' => 0, - // 'boost' => 500, - // 'rate' => 1 - // ) - // ), $mailings->get_distributions($wpdb, $mhMock)); - // } - - // public function test_get_distributions_one_campaign_method(): void - // { - // $mailings = new Mailings(); - // $wpdb = new wpdb(); - // $postObject = new stdClass(); + $this->assertEquals(array( + 'campaigns' => Array( + 0 => Array( + 'conversions' => 0, + 'fields' => Array( + 'subjects' => Array( + 0 => Array( + 'subject' => 'Tell Senate: No attacks on immigrants', + 'enabled' => true + ), + 1 => Array( + 'subject' => "Block Trump's attacks on immigrants", + 'enabled' => true + ) + ) + ), + 'id' => 263, + 'losses' => 0, + 'sent' => 0, + 'subjects' => Array( + 0 => Array( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 0, + 'title' => 'Tell Senate: No attacks on immigrants', + 'rate' => 1, + 'share' => 1 + ) + ), + 'title' => 'No wall testing', + 'valid' => true, + 'rate' => 1, + 'share' => 1, + 'limit' => 4.0 + ) + ), + 'overall' => Array( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 0, + 'boost' => 500, + 'rate' => 1 + ) + ), $mailings->get_distributions($wpdb, $mhMock)); + } + + public function test_get_distributions_one_campaign_method(): void + { + $mailings = new Mailings(); + $wpdb = new wpdb(); + $postObject = new stdClass(); - // $postObject->posts = array (); + $postObject->posts = array (); - // //** must update return value from campaigns + //** must update return value from campaigns - // $campaigns = array( - // 263 => array( - // "conversions" => 0, - // "fields" => array( - // "subjects" => array ( 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ),) - // ), - // "id" => 263, - // "losses" => 0, - // "sent" => 0, - // "subjects" => array (), - // "title" => "No wall testing", - // "valid" => true - // ) - // ); - // // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + $campaigns = array( + 263 => array( + "conversions" => 0, + "fields" => array( + "subjects" => array ( 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ),) + ), + "id" => 263, + "losses" => 0, + "sent" => 0, + "subjects" => array (), + "title" => "No wall testing", + "valid" => true + ) + ); + // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - // $mailings_stats = array ( - // 0 => array ( - // 'campaign_id' => '263', - // 'variation_subject' => '0', - // 'conversions' => '1', - // 'losses' => '0', - // 'sent' => '2', ), - // 1 => array ( - // 'campaign_id' => '263', - // 'variation_subject' => '1', - // 'conversions' => '1', - // 'losses' => '0', - // 'sent' => '2', ) - // ); + $mailings_stats = array ( + 0 => array ( + 'campaign_id' => '263', + 'variation_subject' => '0', + 'conversions' => '1', + 'losses' => '0', + 'sent' => '2', ), + 1 => array ( + 'campaign_id' => '263', + 'variation_subject' => '1', + 'conversions' => '1', + 'losses' => '0', + 'sent' => '2', ) + ); - // $mhMock= $this->getMockBuilder(MailingsHelpers::class) - // ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) - // ->getMock(); + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) + ->getMock(); - // $mhMock->expects($this->once()) - // ->method('wp_query_posts') - // ->will($this->returnValue($postObject)); + $mhMock->expects($this->once()) + ->method('wp_query_posts') + ->will($this->returnValue($postObject)); - // $mhMock->expects($this->once()) - // ->method('setUpCampaigns') - // ->will($this->returnValue($campaigns)); + $mhMock->expects($this->once()) + ->method('setUpCampaigns') + ->will($this->returnValue($campaigns)); - // $mhMock->expects($this->once()) - // ->method('get_mailings_results_wpdb') - // ->will($this->returnValue($mailings_stats)); + $mhMock->expects($this->once()) + ->method('get_mailings_results_wpdb') + ->will($this->returnValue($mailings_stats)); - // $mhMock->expects($this->once()) - // ->method('get_fresh_subscribers_for_campaign') - // ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); + $mhMock->expects($this->once()) + ->method('get_fresh_subscribers_for_campaign') + ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); - // $this->assertEquals(array( - // 'campaigns' => Array( - // 0 => Array( - // 'conversions' => 2, - // 'fields' => Array( - // 'subjects' => Array( - // 0 => Array( - // 'subject' => 'Tell Senate: No attacks on immigrants', - // 'enabled' => true - // ), - // 1 => Array( - // 'subject' => "Block Trump's attacks on immigrants", - // 'enabled' => true - // ) - // ) - // ), - // 'id' => 263, - // 'losses' => 0, - // 'sent' => 4, - // 'subjects' => Array( - // 0 => Array( - // 'conversions' => 1, - // 'losses' => 0, - // 'sent' => 2, - // 'title' => 'Tell Senate: No attacks on immigrants', - // 'rate' => 0.994055523936002, - // 'share' => 0.5 - // ), - // 1 => Array( - // 'conversions' => 1, - // 'losses' => 0, - // 'sent' => 2, - // 'title' => "Block Trump's attacks on immigrants", - // 'rate' => 0.994055523936002, - // 'share' => 0.5 - // ) - // ), - // 'title' => 'No wall testing', - // 'valid' => true, - // 'rate' => 0.9920949861426052, - // 'share' => 1.0, - // 'limit' => 4.0 - // ) - // ), - // 'overall' => Array( - // 'conversions' => 2, - // 'losses' => 0, - // 'sent' => 4, - // 'boost' => 500, - // 'rate' => 0.996031746031746 - // ) - // ), $mailings->get_distributions($wpdb, $mhMock)); - // } - // public function test_get_distributions_one_campaign_one_greater_conversions_method(): void - // { - // $mailings = new Mailings(); - // $wpdb = new wpdb(); - // $postObject = new stdClass(); + $this->assertEquals(array( + 'campaigns' => Array( + 0 => Array( + 'conversions' => 2, + 'fields' => Array( + 'subjects' => Array( + 0 => Array( + 'subject' => 'Tell Senate: No attacks on immigrants', + 'enabled' => true + ), + 1 => Array( + 'subject' => "Block Trump's attacks on immigrants", + 'enabled' => true + ) + ) + ), + 'id' => 263, + 'losses' => 0, + 'sent' => 4, + 'subjects' => Array( + 0 => Array( + 'conversions' => 1, + 'losses' => 0, + 'sent' => 2, + 'title' => 'Tell Senate: No attacks on immigrants', + 'rate' => 0.994055523936002, + 'share' => 0.5 + ), + 1 => Array( + 'conversions' => 1, + 'losses' => 0, + 'sent' => 2, + 'title' => "Block Trump's attacks on immigrants", + 'rate' => 0.994055523936002, + 'share' => 0.5 + ) + ), + 'title' => 'No wall testing', + 'valid' => true, + 'rate' => 0.9920949861426052, + 'share' => 1.0, + 'limit' => 4.0 + ) + ), + 'overall' => Array( + 'conversions' => 2, + 'losses' => 0, + 'sent' => 4, + 'boost' => 500, + 'rate' => 0.996031746031746 + ) + ), $mailings->get_distributions($wpdb, $mhMock)); + } + public function test_get_distributions_one_campaign_one_greater_conversions_method(): void + { + $mailings = new Mailings(); + $wpdb = new wpdb(); + $postObject = new stdClass(); - // $postObject->posts = array (); + $postObject->posts = array (); - // //** must update return value from campaigns + //** must update return value from campaigns - // $campaigns = array( - // 263 => array( - // "conversions" => 0, - // "fields" => array( - // "subjects" => array ( 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ),) - // ), - // "id" => 263, - // "losses" => 0, - // "sent" => 0, - // "subjects" => array (), - // "title" => "No wall testing", - // "valid" => true - // ) - // ); - // // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + $campaigns = array( + 263 => array( + "conversions" => 0, + "fields" => array( + "subjects" => array ( 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ),) + ), + "id" => 263, + "losses" => 0, + "sent" => 0, + "subjects" => array (), + "title" => "No wall testing", + "valid" => true + ) + ); + // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - // $mailings_stats = array ( - // 0 => array ( - // 'campaign_id' => '263', - // 'variation_subject' => '0', - // 'conversions' => '2', - // 'losses' => '0', - // 'sent' => '2', ), - // 1 => array ( - // 'campaign_id' => '263', - // 'variation_subject' => '1', - // 'conversions' => '0', - // 'losses' => '0', - // 'sent' => '2', ) - // ); + $mailings_stats = array ( + 0 => array ( + 'campaign_id' => '263', + 'variation_subject' => '0', + 'conversions' => '2', + 'losses' => '0', + 'sent' => '2', ), + 1 => array ( + 'campaign_id' => '263', + 'variation_subject' => '1', + 'conversions' => '0', + 'losses' => '0', + 'sent' => '2', ) + ); - // $mhMock= $this->getMockBuilder(MailingsHelpers::class) - // ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) - // ->getMock(); + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) + ->getMock(); - // $mhMock->expects($this->once()) - // ->method('wp_query_posts') - // ->will($this->returnValue($postObject)); + $mhMock->expects($this->once()) + ->method('wp_query_posts') + ->will($this->returnValue($postObject)); - // $mhMock->expects($this->once()) - // ->method('setUpCampaigns') - // ->will($this->returnValue($campaigns)); + $mhMock->expects($this->once()) + ->method('setUpCampaigns') + ->will($this->returnValue($campaigns)); - // $mhMock->expects($this->once()) - // ->method('get_mailings_results_wpdb') - // ->will($this->returnValue($mailings_stats)); + $mhMock->expects($this->once()) + ->method('get_mailings_results_wpdb') + ->will($this->returnValue($mailings_stats)); - // $mhMock->expects($this->once()) - // ->method('get_fresh_subscribers_for_campaign') - // ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); + $mhMock->expects($this->once()) + ->method('get_fresh_subscribers_for_campaign') + ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); - // $this->assertEquals(array( - // 'campaigns' => Array( - // 0 => Array( - // 'conversions' => 2, - // 'fields' => Array( - // 'subjects' => Array( - // 0 => Array( - // 'subject' => 'Tell Senate: No attacks on immigrants', - // 'enabled' => true - // ), - // 1 => Array( - // 'subject' => "Block Trump's attacks on immigrants", - // 'enabled' => true - // ) - // ) - // ), - // 'id' => 263, - // 'losses' => 0, - // 'sent' => 4, - // 'subjects' => Array( - // 0 => Array( - // 'conversions' => 2, - // 'losses' => 0, - // 'sent' => 2, - // 'title' => 'Tell Senate: No attacks on immigrants', - // 'rate' => 0.996047555808512, - // 'share' => 0.501001972135632 - // ), - // 1 => Array( - // 'conversions' => 0, - // 'losses' => 0, - // 'sent' => 2, - // 'title' => "Block Trump's attacks on immigrants", - // 'rate' => 0.9920634920634921, - // 'share' => 0.498998027864368 - // ) - // ), - // 'title' => 'No wall testing', - // 'valid' => true, - // 'rate' => 0.9920949861426052, - // 'share' => 1.0, - // 'limit' => 4.0 - // ) - // ), - // 'overall' => Array( - // 'conversions' => 2, - // 'losses' => 0, - // 'sent' => 4, - // 'boost' => 500, - // 'rate' => 0.996031746031746 - // ) - // ), $mailings->get_distributions($wpdb, $mhMock)); - // } - - // public function test_send_function_with_one_campaign_one_subject_greater_conversion(): void - // { - // $vk_mailings_mock = $this->createMock(Mailings::class); - // $wpdb_mock = $this->createMock(wpdb::class); - // $mhMock = $this->createMock(MailingsHelpers::class); - - // $vk_mailings_mock->expects($this->once()) - // ->method('get_distributions') - // ->willReturn(Array( - // 'campaigns' => Array( - // 0 => Array( - // 'conversions' => 2, - // 'fields' => Array( - // 'subjects' => Array( - // 0 => Array( - // 'subject' => 'Tell Senate: No attacks on immigrants', - // 'enabled' => true - // ), - // 1 => Array( - // 'subject' => "Block Trump's attacks on immigrants", - // 'enabled' => true - // ) - // ), - // 'from_line' => '41', - // 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', - // 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', - // 'salutation' => '{{ user.first_name|default:"Hi" }},', + $this->assertEquals(array( + 'campaigns' => Array( + 0 => Array( + 'conversions' => 2, + 'fields' => Array( + 'subjects' => Array( + 0 => Array( + 'subject' => 'Tell Senate: No attacks on immigrants', + 'enabled' => true + ), + 1 => Array( + 'subject' => "Block Trump's attacks on immigrants", + 'enabled' => true + ) + ) + ), + 'id' => 263, + 'losses' => 0, + 'sent' => 4, + 'subjects' => Array( + 0 => Array( + 'conversions' => 2, + 'losses' => 0, + 'sent' => 2, + 'title' => 'Tell Senate: No attacks on immigrants', + 'rate' => 0.996047555808512, + 'share' => 0.501001972135632 + ), + 1 => Array( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 2, + 'title' => "Block Trump's attacks on immigrants", + 'rate' => 0.9920634920634921, + 'share' => 0.498998027864368 + ) + ), + 'title' => 'No wall testing', + 'valid' => true, + 'rate' => 0.9920949861426052, + 'share' => 1.0, + 'limit' => 4.0 + ) + ), + 'overall' => Array( + 'conversions' => 2, + 'losses' => 0, + 'sent' => 4, + 'boost' => 500, + 'rate' => 0.996031746031746 + ) + ), $mailings->get_distributions($wpdb, $mhMock)); + } + + public function test_send_function_with_one_campaign_one_subject_greater_conversion(): void + { + $vk_mailings_mock = $this->createMock(Mailings::class); + $wpdb_mock = $this->createMock(wpdb::class); + $mhMock = $this->createMock(MailingsHelpers::class); + + $vk_mailings_mock->expects($this->once()) + ->method('get_distributions') + ->willReturn(Array( + 'campaigns' => Array( + 0 => Array( + 'conversions' => 2, + 'fields' => Array( + 'subjects' => Array( + 0 => Array( + 'subject' => 'Tell Senate: No attacks on immigrants', + 'enabled' => true + ), + 1 => Array( + 'subject' => "Block Trump's attacks on immigrants", + 'enabled' => true + ) + ), + 'from_line' => '41', + 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', + 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + 'salutation' => '{{ user.first_name|default:"Hi" }},', - // ), - // 'id' => 263, - // 'losses' => 0, - // 'sent' => 4, - // 'subjects' => Array( - // 0 => Array( - // 'conversions' => 2, - // 'losses' => 0, - // 'sent' => 2, - // 'title' => 'Tell Senate: No attacks on immigrants', - // 'rate' => 0.996047555808512, - // 'share' => 0.501001972135632 - // ), - // 1 => Array( - // 'conversions' => 0, - // 'losses' => 0, - // 'sent' => 2, - // 'title' => "Block Trump's attacks on immigrants", - // 'rate' => 0.9920634920634921, - // 'share' => 0.498998027864368 - // ) - // ), - // 'title' => 'No wall testing', - // 'valid' => true, - // 'rate' => 0.9920949861426052, - // 'share' => 1.0, - // 'limit' => 4.0 - // ) - // ), - // 'overall' => Array( - // 'conversions' => 2, - // 'losses' => 0, - // 'sent' => 4, - // 'boost' => 500, - // 'rate' => 0.996031746031746 - // ) - // ) - // ); - - // $mhMock->expects($this->once()) - // ->method('getoption') - // ->willReturn(3.5714285714285716); + ), + 'id' => 263, + 'losses' => 0, + 'sent' => 4, + 'subjects' => Array( + 0 => Array( + 'conversions' => 2, + 'losses' => 0, + 'sent' => 2, + 'title' => 'Tell Senate: No attacks on immigrants', + 'rate' => 0.996047555808512, + 'share' => 0.501001972135632 + ), + 1 => Array( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 2, + 'title' => "Block Trump's attacks on immigrants", + 'rate' => 0.9920634920634921, + 'share' => 0.498998027864368 + ) + ), + 'title' => 'No wall testing', + 'valid' => true, + 'rate' => 0.9920949861426052, + 'share' => 1.0, + 'limit' => 4.0 + ) + ), + 'overall' => Array( + 'conversions' => 2, + 'losses' => 0, + 'sent' => 4, + 'boost' => 500, + 'rate' => 0.996031746031746 + ) + ) + ); + + $mhMock->expects($this->once()) + ->method('getoption') + ->willReturn(3.5714285714285716); - // $mhMock->expects($this->once()) - // ->method('get_url') - // ->willReturn('https://victorykit.local/c/no-wall-testing-2/'); + $mhMock->expects($this->once()) + ->method('get_url') + ->willReturn('https://victorykit.local/c/no-wall-testing-2/'); - // $mhMock->expects($this->exactly(1)) - // ->method('get_fresh_subscribers_for_campaign') - // ->willReturn(array( 0 => 6630470, 1 => 6630472, 2 => 6630478, - // 3 => 6630479)); + $mhMock->expects($this->exactly(1)) + ->method('get_fresh_subscribers_for_campaign') + ->willReturn(array( 0 => 6630470, 1 => 6630472, 2 => 6630478, + 3 => 6630479)); - // $mhMock->expects($this->exactly(2)) - // ->method('send'); + $mhMock->expects($this->exactly(2)) + ->method('send'); - // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); - // } + vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock); + } - // - // ****play with mailing stats**** - // public function test_get_distributions_one_campaign_method(): void - // { - // $mailings = new Mailings(); - // $wpdb = new wpdb(); - // $postObject = new stdClass(); + public function test_get_distributions_one_campaign_one_succesful_subject_method(): void + { + $mailings = new Mailings(); + $wpdb = new wpdb(); + $postObject = new stdClass(); - // $postObject->posts = array (); + $postObject->posts = array (); - // //** must update return value from campaigns + //** must update return value from campaigns - // $campaigns = array( - // 263 => array( - // "conversions" => 0, - // "fields" => array( - // "subjects" => array ( - // 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), - // 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ), - // ) - // ), - // "id" => 263, - // "losses" => 0, - // "sent" => 0, - // "subjects" => array (), - // "title" => "No wall testing", - // "valid" => true - // ) - // ); - // // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + $campaigns = array( + 263 => array( + "conversions" => 0, + "fields" => array( + "subjects" => array ( + 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), + 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ), + ) + ), + "id" => 263, + "losses" => 0, + "sent" => 0, + "subjects" => array (), + "title" => "No wall testing", + "valid" => true + ) + ); + // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - // $mailings_stats = array ( - // 0 => array ( - // 'campaign_id' => '263', - // 'variation_subject' => '0', - // 'conversions' => '500', - // 'losses' => '0', - // 'sent' => '500', ), - // 1 => array ( - // 'campaign_id' => '263', - // 'variation_subject' => '1', - // 'conversions' => '0', - // 'losses' => '0', - // 'sent' => '500', ) - // ); + $mailings_stats = array ( + 0 => array ( + 'campaign_id' => '263', + 'variation_subject' => '0', + 'conversions' => '500', + 'losses' => '0', + 'sent' => '500', ), + 1 => array ( + 'campaign_id' => '263', + 'variation_subject' => '1', + 'conversions' => '0', + 'losses' => '0', + 'sent' => '500', ) + ); - // $mhMock= $this->getMockBuilder(MailingsHelpers::class) - // ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) - // ->getMock(); + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) + ->getMock(); - // $mhMock->expects($this->once()) - // ->method('wp_query_posts') - // ->will($this->returnValue($postObject)); + $mhMock->expects($this->once()) + ->method('wp_query_posts') + ->will($this->returnValue($postObject)); - // $mhMock->expects($this->once()) - // ->method('setUpCampaigns') - // ->will($this->returnValue($campaigns)); + $mhMock->expects($this->once()) + ->method('setUpCampaigns') + ->will($this->returnValue($campaigns)); - // $mhMock->expects($this->once()) - // ->method('get_mailings_results_wpdb') - // ->will($this->returnValue($mailings_stats)); + $mhMock->expects($this->once()) + ->method('get_mailings_results_wpdb') + ->will($this->returnValue($mailings_stats)); - // $mhMock->expects($this->once()) - // ->method('get_fresh_subscribers_for_campaign') - // ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); + $mhMock->expects($this->once()) + ->method('get_fresh_subscribers_for_campaign') + ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); - // $this->assertEquals(array( - // 'campaigns' => Array( - // 0 => Array( - // 'conversions' => 500, - // 'fields' => Array( - // 'subjects' => Array( - // 0 => Array( - // 'subject' => 'Tell Senate: No attacks on immigrants', - // 'enabled' => true - // ), - // 1 => Array( - // 'subject' => "Block Trump's attacks on immigrants", - // 'enabled' => true - // ) - // ) - // ), - // 'id' => 263, - // 'losses' => 0, - // 'sent' => 1000, - // 'subjects' => Array( - // 0 => Array( - // 'conversions' => 500, - // 'losses' => 0, - // 'sent' => 500, - // 'title' => 'Tell Senate: No attacks on immigrants', - // 'rate' => 0.8333333333333333, - // 'share' => 0.7142857142857143 - // ), - // 1 => Array( - // 'conversions' => 0, - // 'losses' => 0, - // 'sent' => 500, - // 'title' => "Block Trump's attacks on immigrants", - // 'rate' => 0.3333333333333333, - // 'share' => 0.28571428571428575 - // ) - // ), - // 'title' => 'No wall testing', - // 'valid' => true, - // 'rate' => 0.5555555555555555, - // 'share' => 1.0, - // 'limit' => 4.0 - // ) - // ), - // 'overall' => Array( - // 'conversions' => 500, - // 'losses' => 0, - // 'sent' => 1000, - // 'boost' => 500, - // 'rate' => 0.6666666666666666 - // ) - // ), $mailings->get_distributions($wpdb, $mhMock)); - // } + $this->assertEquals(array( + 'campaigns' => Array( + 0 => Array( + 'conversions' => 500, + 'fields' => Array( + 'subjects' => Array( + 0 => Array( + 'subject' => 'Tell Senate: No attacks on immigrants', + 'enabled' => true + ), + 1 => Array( + 'subject' => "Block Trump's attacks on immigrants", + 'enabled' => true + ) + ) + ), + 'id' => 263, + 'losses' => 0, + 'sent' => 1000, + 'subjects' => Array( + 0 => Array( + 'conversions' => 500, + 'losses' => 0, + 'sent' => 500, + 'title' => 'Tell Senate: No attacks on immigrants', + 'rate' => 0.8333333333333333, + 'share' => 0.7142857142857143 + ), + 1 => Array( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 500, + 'title' => "Block Trump's attacks on immigrants", + 'rate' => 0.3333333333333333, + 'share' => 0.28571428571428575 + ) + ), + 'title' => 'No wall testing', + 'valid' => true, + 'rate' => 0.5555555555555555, + 'share' => 1.0, + 'limit' => 4.0 + ) + ), + 'overall' => Array( + 'conversions' => 500, + 'losses' => 0, + 'sent' => 1000, + 'boost' => 500, + 'rate' => 0.6666666666666666 + ) + ), $mailings->get_distributions($wpdb, $mhMock)); + } - // public function test_create_mailings_mailing_no_conversions_only_losses(): void - // { - // $mailings = new Mailings(); - // $wpdb = new wpdb(); - // $postObject = new stdClass(); + public function test_create_mailings_mailing_no_conversions_only_losses(): void + { + $mailings = new Mailings(); + $wpdb = new wpdb(); + $postObject = new stdClass(); - // $postObject->posts = array (); + $postObject->posts = array (); - // //** must update return value from campaigns + //** must update return value from campaigns - // $campaigns = array( - // 263 => array( - // "conversions" => 0, - // "fields" => array( - // "subjects" => array ( - // 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), - // 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ), - // ) - // ), - // "id" => 263, - // "losses" => 0, - // "sent" => 0, - // "subjects" => array (), - // "title" => "No wall testing", - // "valid" => true - // ) - // ); - // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + $campaigns = array( + 263 => array( + "conversions" => 0, + "fields" => array( + "subjects" => array ( + 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), + 1 => array ( 'subject' => "Block Trump's attacks on immigrants", 'enabled' => true, ), + ) + ), + "id" => 263, + "losses" => 0, + "sent" => 0, + "subjects" => array (), + "title" => "No wall testing", + "valid" => true + ) + ); + $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - // $mailings_stats = array ( - // 0 => array ( - // 'campaign_id' => '263', - // 'variation_subject' => '0', - // 'conversions' => '250', - // 'losses' => '250', - // 'sent' => '500', ), - // 1 => array ( - // 'campaign_id' => '263', - // 'variation_subject' => '1', - // 'conversions' => '0', - // 'losses' => '0', - // 'sent' => '500', ) - // ); + $mailings_stats = array ( + 0 => array ( + 'campaign_id' => '263', + 'variation_subject' => '0', + 'conversions' => '250', + 'losses' => '250', + 'sent' => '500', ), + 1 => array ( + 'campaign_id' => '263', + 'variation_subject' => '1', + 'conversions' => '0', + 'losses' => '0', + 'sent' => '500', ) + ); - // $mhMock= $this->getMockBuilder(MailingsHelpers::class) - // ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) - // ->getMock(); + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) + ->getMock(); - // $mhMock->expects($this->once()) - // ->method('wp_query_posts') - // ->will($this->returnValue($postObject)); + $mhMock->expects($this->once()) + ->method('wp_query_posts') + ->will($this->returnValue($postObject)); - // $mhMock->expects($this->once()) - // ->method('setUpCampaigns') - // ->will($this->returnValue($campaigns)); + $mhMock->expects($this->once()) + ->method('setUpCampaigns') + ->will($this->returnValue($campaigns)); - // $mhMock->expects($this->once()) - // ->method('get_mailings_results_wpdb') - // ->will($this->returnValue($mailings_stats)); + $mhMock->expects($this->once()) + ->method('get_mailings_results_wpdb') + ->will($this->returnValue($mailings_stats)); - // $mhMock->expects($this->once()) - // ->method('get_fresh_subscribers_for_campaign') - // ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); + $mhMock->expects($this->once()) + ->method('get_fresh_subscribers_for_campaign') + ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); - // $this->assertEquals(array( - // 'campaigns' => Array( - // 0 => Array( - // 'conversions' => 250, - // 'fields' => Array( - // 'subjects' => Array( - // 0 => Array( - // 'subject' => 'Tell Senate: No attacks on immigrants', - // 'enabled' => true - // ), - // 1 => Array( - // 'subject' => "Block Trump's attacks on immigrants", - // 'enabled' => true - // ) - // ) - // ), - // 'id' => 263, - // 'losses' => 250, - // 'sent' => 1000, - // 'subjects' => Array( - // 0 => Array( - // 'conversions' => 250, - // 'losses' => 250, - // 'sent' => 500, - // 'title' => 'Tell Senate: No attacks on immigrants', - // 'rate' => 0.16666666666666666, - // 'share' => 0.5 - // ), - // 1 => Array( - // 'conversions' => 0, - // 'losses' => 0, - // 'sent' => 500, - // 'title' => "Block Trump's attacks on immigrants", - // 'rate' => 0.16666666666666666, - // 'share' => 0.5 - // ) - // ), - // 'title' => 'No wall testing', - // 'valid' => true, - // 'rate' => 0.1111111111111111, - // 'share' => 1.0, - // 'limit' => 4.0 - // ) - // ), - // 'overall' => Array( - // 'conversions' => 250, - // 'losses' => 250, - // 'sent' => 1000, - // 'boost' => 500, - // 'rate' => 0.3333333333333333 - // ) - // ), $mailings->get_distributions($wpdb, $mhMock)); - - // // vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock); - // } - - // public function test_vk_mailings_create_new_mailings_action_return_value(): void - // { - // $vk_mailings_mock = $this->createMock(Mailings::class); - // $wpdb = new wpdb(); + $this->assertEquals(array( + 'campaigns' => Array( + 0 => Array( + 'conversions' => 250, + 'fields' => Array( + 'subjects' => Array( + 0 => Array( + 'subject' => 'Tell Senate: No attacks on immigrants', + 'enabled' => true + ), + 1 => Array( + 'subject' => "Block Trump's attacks on immigrants", + 'enabled' => true + ) + ) + ), + 'id' => 263, + 'losses' => 250, + 'sent' => 1000, + 'subjects' => Array( + 0 => Array( + 'conversions' => 250, + 'losses' => 250, + 'sent' => 500, + 'title' => 'Tell Senate: No attacks on immigrants', + 'rate' => 0.16666666666666666, + 'share' => 0.5 + ), + 1 => Array( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 500, + 'title' => "Block Trump's attacks on immigrants", + 'rate' => 0.16666666666666666, + 'share' => 0.5 + ) + ), + 'title' => 'No wall testing', + 'valid' => true, + 'rate' => 0.1111111111111111, + 'share' => 1.0, + 'limit' => 4.0 + ) + ), + 'overall' => Array( + 'conversions' => 250, + 'losses' => 250, + 'sent' => 1000, + 'boost' => 500, + 'rate' => 0.3333333333333333 + ) + ), $mailings->get_distributions($wpdb, $mhMock)); + } + + public function test_vk_mailings_create_new_mailings_action_return_value(): void + { + $vk_mailings_mock = $this->createMock(Mailings::class); + $wpdb = new wpdb(); - // $mhMock= $this->getMockBuilder(MailingsHelpers::class) - // ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign', 'send', 'get_url']) - // ->getMock(); + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign', 'send', 'get_url']) + ->getMock(); - // $mhMock->expects($this->once()) - // ->method('get_fresh_subscribers_for_campaign') - // ->will($this->returnValue(['6630475', '6630477', '6630478','6630479','6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479'])); + $mhMock->expects($this->once()) + ->method('get_fresh_subscribers_for_campaign') + ->will($this->returnValue(['6630475', '6630477', '6630478','6630479','6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479'])); - // $mhMock->expects($this->exactly(2)) - // ->method('send'); + $mhMock->expects($this->exactly(2)) + ->method('send'); - // $mhMock->expects($this->once()) - // ->method('get_url') - // ->willReturn('https://victorykit.local/c/no-wall-testing-2/'); + $mhMock->expects($this->once()) + ->method('get_url') + ->willReturn('https://victorykit.local/c/no-wall-testing-2/'); - // $vk_mailings_mock->expects($this->once()) - // ->method('get_distributions') - // ->will($this->returnValue(Array( - // 'campaigns' => Array( - // 0 => Array( - // 'conversions' => 500, - // 'fields' => Array( - // 'subjects' => Array( - // 0 => Array( - // 'subject' => 'Tell Senate: No attacks on immigrants', - // 'enabled' => true - // ), - // 1 => Array( - // 'subject' => "Block Trump's attacks on immigrants", - // 'enabled' => true - // ), - // ), - // 'from_line' => 41, - // 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', - // 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', - // 'salutation' => '{{ user.first_name|default:"Hi" }},', - // ), - // 'id' => 263, - // 'losses' => 0, - // 'sent' => 1000, - // 'subjects' => Array( - // 0 => Array( - // 'conversions' => 500, - // 'losses' => 0, - // 'sent' => 500, - // 'title' => 'Tell Senate: No attacks on immigrants', - // 'rate' => 0.8333333333333333, - // 'share' => 0.7142857142857143 - // ), - // 1 => Array( - // 'conversions' => 0, - // 'losses' => 0, - // 'sent' => 500, - // 'title' => "Block Trump's attacks on immigrants", - // 'rate' => 0.3333333333333333, - // 'share' => 0.28571428571428575 - // ) - // ), - // 'title' => 'No wall testing', - // 'valid' => true, - // 'rate' => 0.5555555555555555, - // 'share' => 1.0, - // 'limit' => 4.0 - // ) - // ), - // 'overall' => Array( - // 'conversions' => 500, - // 'losses' => 0, - // 'sent' => 1000, - // 'boost' => 500, - // 'rate' => 0.6666666666666666 - // ) - // ) - // ) - // ); - // $this->assertEquals(Array( - // 0 => Array( - // 'from_line' => 41, - // 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', - // 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', - // 'campaign_id' => 263, - // 'limit' => 3.0, - // 'salutation' => '{{ user.first_name|default:"Hi" }},', - // 'subject' => 'Tell Senate: No attacks on immigrants', - // 'subscribers' => Array ( - // 0 => '6630475', - // 1 => '6630477', - // 2 => '6630478' - // ), - // 'url' => 'https://victorykit.local/c/no-wall-testing-2/', - // 'variation_subject' => 0 - // ), - // 1 => Array( - // 'from_line' => 41, - // 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', - // 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', - // 'campaign_id' => 263, - // 'limit' => 1.0, - // 'salutation' => '{{ user.first_name|default:"Hi" }},', - // 'subject' => "Block Trump's attacks on immigrants", - // 'subscribers' => Array ( - // 0 => '6630479' - // ), - // 'url' => 'https://victorykit.local/c/no-wall-testing-2/', - // 'variation_subject' => 1 - // ) - // ),vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock)); - // } - // public function test_vk_mailings_create_new_mailings_both_mailings_failing(): void - // { - // $vk_mailings_mock = $this->createMock(Mailings::class); - // $wpdb = new wpdb(); + $vk_mailings_mock->expects($this->once()) + ->method('get_distributions') + ->will($this->returnValue(Array( + 'campaigns' => Array( + 0 => Array( + 'conversions' => 500, + 'fields' => Array( + 'subjects' => Array( + 0 => Array( + 'subject' => 'Tell Senate: No attacks on immigrants', + 'enabled' => true + ), + 1 => Array( + 'subject' => "Block Trump's attacks on immigrants", + 'enabled' => true + ), + ), + 'from_line' => 41, + 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', + 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + 'salutation' => '{{ user.first_name|default:"Hi" }},', + ), + 'id' => 263, + 'losses' => 0, + 'sent' => 1000, + 'subjects' => Array( + 0 => Array( + 'conversions' => 500, + 'losses' => 0, + 'sent' => 500, + 'title' => 'Tell Senate: No attacks on immigrants', + 'rate' => 0.8333333333333333, + 'share' => 0.7142857142857143 + ), + 1 => Array( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 500, + 'title' => "Block Trump's attacks on immigrants", + 'rate' => 0.3333333333333333, + 'share' => 0.28571428571428575 + ) + ), + 'title' => 'No wall testing', + 'valid' => true, + 'rate' => 0.5555555555555555, + 'share' => 1.0, + 'limit' => 4.0 + ) + ), + 'overall' => Array( + 'conversions' => 500, + 'losses' => 0, + 'sent' => 1000, + 'boost' => 500, + 'rate' => 0.6666666666666666 + ) + ) + ) + ); + $this->assertEquals(Array( + 0 => Array( + 'from_line' => 41, + 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', + 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + 'campaign_id' => 263, + 'limit' => 3.0, + 'salutation' => '{{ user.first_name|default:"Hi" }},', + 'subject' => 'Tell Senate: No attacks on immigrants', + 'subscribers' => Array ( + 0 => '6630475', + 1 => '6630477', + 2 => '6630478' + ), + 'url' => 'https://victorykit.local/c/no-wall-testing-2/', + 'variation_subject' => 0 + ), + 1 => Array( + 'from_line' => 41, + 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', + 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + 'campaign_id' => 263, + 'limit' => 1.0, + 'salutation' => '{{ user.first_name|default:"Hi" }},', + 'subject' => "Block Trump's attacks on immigrants", + 'subscribers' => Array ( + 0 => '6630479' + ), + 'url' => 'https://victorykit.local/c/no-wall-testing-2/', + 'variation_subject' => 1 + ) + ),vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock)); + } + public function test_vk_mailings_create_new_mailings_both_mailings_failing(): void + { + $vk_mailings_mock = $this->createMock(Mailings::class); + $wpdb = new wpdb(); - // $mhMock= $this->getMockBuilder(MailingsHelpers::class) - // ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign', 'send', 'get_url']) - // ->getMock(); + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign', 'send', 'get_url']) + ->getMock(); - // $mhMock->expects($this->once()) - // ->method('get_fresh_subscribers_for_campaign') - // ->will($this->returnValue(['6630475', '6630477', '6630478','6630479','6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479'])); + $mhMock->expects($this->once()) + ->method('get_fresh_subscribers_for_campaign') + ->will($this->returnValue(['6630475', '6630477', '6630478','6630479','6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479'])); - // $mhMock->expects($this->exactly(0)) - // ->method('send'); + $mhMock->expects($this->exactly(0)) + ->method('send'); - // $mhMock->expects($this->once()) - // ->method('get_url') - // ->willReturn('https://victorykit.local/c/no-wall-testing-2/'); + $mhMock->expects($this->once()) + ->method('get_url') + ->willReturn('https://victorykit.local/c/no-wall-testing-2/'); - // $vk_mailings_mock->expects($this->once()) - // ->method('get_distributions') - // ->will($this->returnValue(Array( - // 'campaigns' => Array( - // 0 => Array( - // 'conversions' => 0, - // 'fields' => Array( - // 'subjects' => Array( - // 0 => Array( - // 'subject' => 'Tell Senate: No attacks on immigrants', - // 'enabled' => true - // ), - // 1 => Array( - // 'subject' => "Block Trump's attacks on immigrants", - // 'enabled' => true - // ) - // ) - // ), - // 'id' => 263, - // 'losses' => 500, - // 'sent' => 1000, - // 'subjects' => Array( - // 0 => Array( - // 'conversions' => 0, - // 'losses' => 0, - // 'sent' => 500, - // 'title' => 'Tell Senate: No attacks on immigrants', - // 'rate' => 0, - // 'share' => 0 - // ), - // 1 => Array( - // 'conversions' => 0, - // 'losses' => 500, - // 'sent' => 500, - // 'title' => "Block Trump's attacks on immigrants", - // 'rate' => 0, - // 'share' => 0 - // ) - // ), - // 'title' => 'No wall testing', - // 'valid' => true, - // 'rate' => 0, - // 'share' => 0, - // 'limit' => 0.0 - // ) - // ), - // 'overall' => Array( - // 'conversions' => 0, - // 'losses' => 500, - // 'sent' => 1000, - // 'boost' => 500, - // 'rate' => 0 - // ) - // ) - // ) - // ); - // $this->assertEquals(Array(),vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock)); - // } - - // public function test_vk_mailings_create_new_mailings_action_one_subject_50_percent_success_rate(): void - // { - // $vk_mailings_mock = $this->createMock(Mailings::class); - // $wpdb = new wpdb(); + $vk_mailings_mock->expects($this->once()) + ->method('get_distributions') + ->will($this->returnValue(Array( + 'campaigns' => Array( + 0 => Array( + 'conversions' => 0, + 'fields' => Array( + 'subjects' => Array( + 0 => Array( + 'subject' => 'Tell Senate: No attacks on immigrants', + 'enabled' => true + ), + 1 => Array( + 'subject' => "Block Trump's attacks on immigrants", + 'enabled' => true + ) + ) + ), + 'id' => 263, + 'losses' => 500, + 'sent' => 1000, + 'subjects' => Array( + 0 => Array( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 500, + 'title' => 'Tell Senate: No attacks on immigrants', + 'rate' => 0, + 'share' => 0 + ), + 1 => Array( + 'conversions' => 0, + 'losses' => 500, + 'sent' => 500, + 'title' => "Block Trump's attacks on immigrants", + 'rate' => 0, + 'share' => 0 + ) + ), + 'title' => 'No wall testing', + 'valid' => true, + 'rate' => 0, + 'share' => 0, + 'limit' => 0.0 + ) + ), + 'overall' => Array( + 'conversions' => 0, + 'losses' => 500, + 'sent' => 1000, + 'boost' => 500, + 'rate' => 0 + ) + ) + ) + ); + $this->assertEquals(Array(),vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock)); + } + + public function test_vk_mailings_create_new_mailings_action_one_subject_50_percent_success_rate(): void + { + $vk_mailings_mock = $this->createMock(Mailings::class); + $wpdb = new wpdb(); - // $mhMock= $this->getMockBuilder(MailingsHelpers::class) - // ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign', 'send', 'get_url']) - // ->getMock(); + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign', 'send', 'get_url']) + ->getMock(); - // $mhMock->expects($this->once()) - // ->method('get_fresh_subscribers_for_campaign') - // ->will($this->returnValue(['6630475', '6630477', '6630478','6630479','6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479'])); + $mhMock->expects($this->once()) + ->method('get_fresh_subscribers_for_campaign') + ->will($this->returnValue(['6630475', '6630477', '6630478','6630479','6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479'])); - // $mhMock->expects($this->exactly(2)) - // ->method('send'); + $mhMock->expects($this->exactly(2)) + ->method('send'); - // $mhMock->expects($this->once()) - // ->method('get_url') - // ->willReturn('https://victorykit.local/c/no-wall-testing-2/'); + $mhMock->expects($this->once()) + ->method('get_url') + ->willReturn('https://victorykit.local/c/no-wall-testing-2/'); - // $vk_mailings_mock->expects($this->once()) - // ->method('get_distributions') - // ->will($this->returnValue(Array( - // 'campaigns' => Array( - // 0 => Array( - // 'conversions' => 250, - // 'fields' => Array( - // 'subjects' => Array( - // 0 => Array( - // 'subject' => 'Tell Senate: No attacks on immigrants', - // 'enabled' => true - // ), - // 1 => Array( - // 'subject' => "Block Trump's attacks on immigrants", - // 'enabled' => true - // ) - // ), - // 'from_line' => 41, - // 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', - // 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', - // 'salutation' => '{{ user.first_name|default:"Hi" }},', - // ), - // 'id' => 263, - // 'losses' => 250, - // 'sent' => 1000, - // 'subjects' => Array( - // 0 => Array( - // 'conversions' => 250, - // 'losses' => 250, - // 'sent' => 500, - // 'title' => 'Tell Senate: No attacks on immigrants', - // 'rate' => 0.16666666666666666, - // 'share' => 0.5 - // ), - // 1 => Array( - // 'conversions' => 0, - // 'losses' => 0, - // 'sent' => 500, - // 'title' => "Block Trump's attacks on immigrants", - // 'rate' => 0.16666666666666666, - // 'share' => 0.5 - // ) - // ), - // 'title' => 'No wall testing', - // 'valid' => true, - // 'rate' => 0.1111111111111111, - // 'share' => 1.0, - // 'limit' => 4.0 - // ) - // ), - // 'overall' => Array( - // 'conversions' => 250, - // 'losses' => 250, - // 'sent' => 1000, - // 'boost' => 500, - // 'rate' => 0.3333333333333333 - // ) - // ) - // ) - // ); - // $this->assertEquals(Array( - // 0 => Array( - // 'from_line' => 41, - // 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', - // 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', - // 'campaign_id' => 263, - // 'limit' => 2.0, - // 'salutation' => '{{ user.first_name|default:"Hi" }},', - // 'subject' => 'Tell Senate: No attacks on immigrants', - // 'subscribers' => Array ( - // 0 => '6630475', - // 1 => '6630477' - // ), - // 'url' => 'https://victorykit.local/c/no-wall-testing-2/', - // 'variation_subject' => 0 - // ), - // 1 => Array( - // 'from_line' => 41, - // 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', - // 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', - // 'campaign_id' => 263, - // 'limit' => 2.0, - // 'salutation' => '{{ user.first_name|default:"Hi" }},', - // 'subject' => "Block Trump's attacks on immigrants", - // 'subscribers' => Array ( - // 0 => '6630478', - // 1 => '6630479' - // ), - // 'url' => 'https://victorykit.local/c/no-wall-testing-2/', - // 'variation_subject' => 1 - // ) - // ),vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock)); - // } + $vk_mailings_mock->expects($this->once()) + ->method('get_distributions') + ->will($this->returnValue(Array( + 'campaigns' => Array( + 0 => Array( + 'conversions' => 250, + 'fields' => Array( + 'subjects' => Array( + 0 => Array( + 'subject' => 'Tell Senate: No attacks on immigrants', + 'enabled' => true + ), + 1 => Array( + 'subject' => "Block Trump's attacks on immigrants", + 'enabled' => true + ) + ), + 'from_line' => 41, + 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', + 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + 'salutation' => '{{ user.first_name|default:"Hi" }},', + ), + 'id' => 263, + 'losses' => 250, + 'sent' => 1000, + 'subjects' => Array( + 0 => Array( + 'conversions' => 250, + 'losses' => 250, + 'sent' => 500, + 'title' => 'Tell Senate: No attacks on immigrants', + 'rate' => 0.16666666666666666, + 'share' => 0.5 + ), + 1 => Array( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 500, + 'title' => "Block Trump's attacks on immigrants", + 'rate' => 0.16666666666666666, + 'share' => 0.5 + ) + ), + 'title' => 'No wall testing', + 'valid' => true, + 'rate' => 0.1111111111111111, + 'share' => 1.0, + 'limit' => 4.0 + ) + ), + 'overall' => Array( + 'conversions' => 250, + 'losses' => 250, + 'sent' => 1000, + 'boost' => 500, + 'rate' => 0.3333333333333333 + ) + ) + ) + ); + $this->assertEquals(Array( + 0 => Array( + 'from_line' => 41, + 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', + 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + 'campaign_id' => 263, + 'limit' => 2.0, + 'salutation' => '{{ user.first_name|default:"Hi" }},', + 'subject' => 'Tell Senate: No attacks on immigrants', + 'subscribers' => Array ( + 0 => '6630475', + 1 => '6630477' + ), + 'url' => 'https://victorykit.local/c/no-wall-testing-2/', + 'variation_subject' => 0 + ), + 1 => Array( + 'from_line' => 41, + 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', + 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + 'campaign_id' => 263, + 'limit' => 2.0, + 'salutation' => '{{ user.first_name|default:"Hi" }},', + 'subject' => "Block Trump's attacks on immigrants", + 'subscribers' => Array ( + 0 => '6630478', + 1 => '6630479' + ), + 'url' => 'https://victorykit.local/c/no-wall-testing-2/', + 'variation_subject' => 1 + ) + ),vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock)); + } - // public function test_create_mailings_mailing_two_campaigns(): void - // { - // $mailings = new Mailings(); - // $wpdb = new wpdb(); - // $postObject = new stdClass(); + public function test_create_mailings_mailing_two_campaigns(): void + { + $mailings = new Mailings(); + $wpdb = new wpdb(); + $postObject = new stdClass(); - // $postObject->posts = array (); + $postObject->posts = array (); - // $campaigns = array( - // 263 => array( - // "conversions" => 0, - // "fields" => array( - // "subjects" => array ( - // 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ) - // ) - // ), - // "id" => 263, - // "losses" => 0, - // "sent" => 0, - // "subjects" => array (), - // "title" => "No wall testing", - // "valid" => true - // ), - // 267 => array( - // "conversions" => 0, - // "fields" => array( - // "subjects" => array ( - // 0 => array ( 'subject' => 'Tell Congress: Keep my boss out of my DNA!', 'enabled' => true, ) - // ) - // ), - // "id" => 267, - // "losses" => 0, - // "sent" => 0, - // "subjects" => array (), - // "title" => 'Genetic testing', - // "valid" => true - // ) - // ); - // $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); + $campaigns = array( + 263 => array( + "conversions" => 0, + "fields" => array( + "subjects" => array ( + 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ) + ) + ), + "id" => 263, + "losses" => 0, + "sent" => 0, + "subjects" => array (), + "title" => "No wall testing", + "valid" => true + ), + 267 => array( + "conversions" => 0, + "fields" => array( + "subjects" => array ( + 0 => array ( 'subject' => 'Tell Congress: Keep my boss out of my DNA!', 'enabled' => true, ) + ) + ), + "id" => 267, + "losses" => 0, + "sent" => 0, + "subjects" => array (), + "title" => 'Genetic testing', + "valid" => true + ) + ); + $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); - // $mailings_stats = array ( - // 0 => array ( - // 'campaign_id' => '263', - // 'variation_subject' => '0', - // 'conversions' => '500', - // 'losses' => '0', - // 'sent' => '500', ), - // 1 => array ( - // 'campaign_id' => '267', - // 'variation_subject' => '0', - // 'conversions' => '0', - // 'losses' => '500', - // 'sent' => '500', ) - // ); + $mailings_stats = array ( + 0 => array ( + 'campaign_id' => '263', + 'variation_subject' => '0', + 'conversions' => '500', + 'losses' => '0', + 'sent' => '500', ), + 1 => array ( + 'campaign_id' => '267', + 'variation_subject' => '0', + 'conversions' => '0', + 'losses' => '500', + 'sent' => '500', ) + ); - // $mhMock= $this->getMockBuilder(MailingsHelpers::class) - // ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) - // ->getMock(); + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign']) + ->getMock(); - // $mhMock->expects($this->once()) - // ->method('wp_query_posts') - // ->will($this->returnValue($postObject)); + $mhMock->expects($this->once()) + ->method('wp_query_posts') + ->will($this->returnValue($postObject)); - // $mhMock->expects($this->once()) - // ->method('setUpCampaigns') - // ->will($this->returnValue($campaigns)); + $mhMock->expects($this->once()) + ->method('setUpCampaigns') + ->will($this->returnValue($campaigns)); - // $mhMock->expects($this->once()) - // ->method('get_mailings_results_wpdb') - // ->will($this->returnValue($mailings_stats)); + $mhMock->expects($this->once()) + ->method('get_mailings_results_wpdb') + ->will($this->returnValue($mailings_stats)); - // $mhMock->expects($this->exactly(2)) - // ->method('get_fresh_subscribers_for_campaign') - // ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); + $mhMock->expects($this->exactly(2)) + ->method('get_fresh_subscribers_for_campaign') + ->will($this->returnValue(['6630475', '6630477', '6630478','6630479'])); - // $this->assertEquals(Array( - // 'campaigns' => Array( - // 0 => Array( - // 'conversions' => 500, - // 'fields' => Array( - // 'subjects' => Array( - // 0 => Array ( - // 'subject' => 'Tell Senate: No attacks on immigrants', - // 'enabled' => true - // ) - // ) - // ), - // 'id' => 263, - // 'losses' => 0, - // 'sent' => 500, - // 'subjects' => Array( - // 0 => Array( - // 'conversions' => 500, - // 'losses' => 0, - // 'sent' => 500, - // 'title' => 'Tell Senate: No attacks on immigrants', - // 'rate' => 0.6666666666666666, - // 'share' => 1.0 - // ) - // ), - // 'title' => 'No wall testing', - // 'valid' => true, - // 'rate' => 0.6666666666666666, - // 'share' => 1.0, - // 'limit' => 4.0 - // ), - // 1 => Array( - // 'conversions' => 0, - // 'fields' => Array ( - // 'subjects' => Array( - // 0 => Array( - // 'subject' => 'Tell Congress: Keep my boss out of my DNA!', - // 'enabled' => true - // ) - // ) - // ), - // 'id' => 267, - // 'losses' => 500, - // 'sent' => 500, - // 'subjects' => Array ( - // 0 => Array( - // 'conversions' => 0, - // 'losses' => 500, - // 'sent' => 500, - // 'title' => 'Tell Congress: Keep my boss out of my DNA!', - // 'rate' => 0, - // 'share' => 0 - // ) - // ), - // 'title' => 'Genetic testing', - // 'valid' => true, - // 'rate' => 0, - // 'share' => 0.0, - // 'limit' => 0.0 - // ), - // ), - // 'overall' => Array( - // 'conversions' => 500, - // 'losses' => 500, - // 'sent' => 1000, - // 'boost' => 500, - // 'rate' => 0.3333333333333333 - // ) - // ), $mailings->get_distributions($wpdb, $mhMock)); - // } - - - // ****function below plays with mailing stats**** - //next move to test with two campaigns - //each campaign below will only have one subject - //make one campaign not successful and another very successful - //make both campaigns equally sucessful + $this->assertEquals(Array( + 'campaigns' => Array( + 0 => Array( + 'conversions' => 500, + 'fields' => Array( + 'subjects' => Array( + 0 => Array ( + 'subject' => 'Tell Senate: No attacks on immigrants', + 'enabled' => true + ) + ) + ), + 'id' => 263, + 'losses' => 0, + 'sent' => 500, + 'subjects' => Array( + 0 => Array( + 'conversions' => 500, + 'losses' => 0, + 'sent' => 500, + 'title' => 'Tell Senate: No attacks on immigrants', + 'rate' => 0.6666666666666666, + 'share' => 1.0 + ) + ), + 'title' => 'No wall testing', + 'valid' => true, + 'rate' => 0.6666666666666666, + 'share' => 1.0, + 'limit' => 4.0 + ), + 1 => Array( + 'conversions' => 0, + 'fields' => Array ( + 'subjects' => Array( + 0 => Array( + 'subject' => 'Tell Congress: Keep my boss out of my DNA!', + 'enabled' => true + ) + ) + ), + 'id' => 267, + 'losses' => 500, + 'sent' => 500, + 'subjects' => Array ( + 0 => Array( + 'conversions' => 0, + 'losses' => 500, + 'sent' => 500, + 'title' => 'Tell Congress: Keep my boss out of my DNA!', + 'rate' => 0, + 'share' => 0 + ) + ), + 'title' => 'Genetic testing', + 'valid' => true, + 'rate' => 0, + 'share' => 0.0, + 'limit' => 0.0 + ), + ), + 'overall' => Array( + 'conversions' => 500, + 'losses' => 500, + 'sent' => 1000, + 'boost' => 500, + 'rate' => 0.3333333333333333 + ) + ), $mailings->get_distributions($wpdb, $mhMock)); + } public function test_two_campaigns_one_subject_one_campaign_not_successful(): void { @@ -1437,14 +1426,142 @@ public function test_two_campaigns_one_subject_one_campaign_not_successful(): vo ) ),vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock)); } - } - - - // ( 0 => array ( 'conversions' => 0, 'fields' => array ( 'subjects' => array ( 0 => array ( 'subject' => 'Tell Senate: No attacks on immigrants', 'enabled' => true, ), 1 => array ( 'subject' => 'Block Trump's attacks on immigrants', 'enabled' => true, ), ), 'salutation' => '{{ user.first_name|default:"Hi" }},', 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

Already he has banned Muslims and refugees, threatened sanctuary cities and criminalized millions of undocumented immigrants. His Immigration and Custom Enforcement (ICE) and Border Patrol agents are separating parents from their children, deporting innocent people and spreading fear in immigrant communities across the country. The budget he just released slashes funding for the social safety net in order to spend billions of dollars building his wall, increasing his deportation force and expediting deportations.

Senate Democrats have the power to block funding for Trump’s wall and his massive deportation force. Recent reports show they may be willing to fight to stop Trump’s hateful agenda. Can you help make sure they stand strong?

We cannot let any Senate Democrat cave to political pressure from the right-wing and give Republicans the 60 votes they need to fund Trump’s hate. That is where progressives come in. We have to show Democratic senators that we will have their backs if they act boldly and that we will hold them accountable if they fail to stand up for immigrants.

Tell Senate Democrats: Block funding for Trump’s attacks on immigrants. Click here to sign the petition.

Trump wants to harass, deport and reject people who have come or are dreaming of coming to the United States seeking refuge from conflict, lives free from persecution and better economic opportunities for their families. For as long as Trump is president and extremist right-wing Republicans are willing to enable his agenda, Senate Democrats will have a choice: enable Trump’s agenda of criminalization and deportations, or resist and obstruct his racist regime at every turn. It is up to make sure they do the right thing.

Tell Senate Democrats: Block funding for Trump’s attacks on immigrants.

', 'petition_text' => '

“Block funding for Trump’s attacks on immigrants, including his deportation force and his border wall. “

', 'disclaimer' => '

We do not share your email address without your permission. We may send you updates on this and other important campaigns by email. If at any time you would like to unsubscribe from our email list, you may do so.

', 'share_titles' => array ( 0 => array ( 'title' => 'Senate Democrats: Block Trump’s attacks on immigrants', 'enabled' => true, ), 1 => array ( 'title' => 'Block the border wall!', 'enabled' => true, ), ), 'share_descriptions' => array ( 0 => array ( 'description' => 'There's new legislation to block Trump's border wall -- tell the Senate to pass it.', 'enabled' => true, ), 1 => array ( 'description' => 'The Senate can block Trump's anti-immigrant agenda, including the border wall.', 'enabled' => true, ), ), 'share_images' => array ( 0 => array ( 'image' => array ( 'ID' => 203, 'id' => 203, 'title' => 'Logo-square', 'filename' => 'Logo-square.png', 'url' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', 'alt' => '', 'author' => '5', 'description' => '', 'caption' => '', 'name' => 'logo-square', 'date' => '2017-03-01 04:24:22', 'modified' => '2017-03-01 04:24:22', 'mime_type' => 'image/png', 'type' => 'image', 'icon' => 'https://victorykit.local/wp-includes/images/media/default.png', 'width' => 132, 'height' => 125, 'sizes' => array ( 'thumbnail' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', 'thumbnail-width' => 132, 'thumbnail-height' => 125, 'medium' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', 'medium-width' => 132, 'medium-height' => 125, 'medium_large' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', 'medium_large-width' => 132, 'medium_large-height' => 125, 'large' => 'https://victorykit.local/wp-content/uploads/2016/11/Logo-square.png', 'large-width' => 132, 'large-height' => 125, ), ), 'enabled' => true, ), ), 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', 'from_line' => '41', 'landing_page_body' => '', 'after_action_email_content' => '

Thank you for taking action. Now please share this important campaign with your friends, and ask them to join in it. This will make it much more likely we can win!

', ), 'id' => 263, 'losses' => 0, 'sent' => 0, 'subjects' => array ( 0 => array ( 'conversions' => 0, 'losses' => 0, 'sent' => 0, 'title' => 'Tell Senate: No attacks on immigrants', 'rate' => 1, 'share' => 0.5, ), 1 => array ( 'conversions' => 0, 'losses' => 0, 'sent' => 0, 'title' => 'Block Trump's attacks on immigrants', 'rate' => 1, 'share' => 0.5, ), ), 'title' => 'No wall testing', 'valid' => true, 'rate' => 1, 'share' => 1, 'limit' => 4.0, ), ) + public function test_two_campaigns_both_equally_successful(): void + { + $vk_mailings_mock = $this->createMock(Mailings::class); + $wpdb = new wpdb(); + + $mhMock= $this->getMockBuilder(MailingsHelpers::class) + ->setMethods(['wp_query_posts', 'setUpCampaigns', 'get_mailings_results_wpdb', 'get_fresh_subscribers_for_campaign', 'send', 'get_url']) + ->getMock(); + $mhMock->expects($this->exactly(2)) + ->method('get_fresh_subscribers_for_campaign') + ->will($this->returnValue(['6630475', '6630477', '6630478','6630479','6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479', '6630475', '6630477', '6630478','6630479'])); + + $mhMock->expects($this->exactly(2)) + ->method('send'); + $mhMock->expects($this->exactly(2)) + ->method('get_url') + ->willReturn('https://victorykit.local/c/no-wall-testing-2/'); + + $vk_mailings_mock->expects($this->once()) + ->method('get_distributions') + ->will($this->returnValue( + Array( + 'campaigns' => Array( + 0 => Array( + 'conversions' => 500, + 'fields' => Array( + 'from_line' => '41', + 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', + 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + 'salutation' => '{{ user.first_name|default:"Hi" }},', + 'subjects' => Array( + 0 => Array ( + 'subject' => 'Tell Senate: No attacks on immigrants', + 'enabled' => true + ) + ) + ), + 'id' => 263, + 'losses' => 0, + 'sent' => 500, + 'subjects' => Array( + 0 => Array( + 'conversions' => 500, + 'losses' => 0, + 'sent' => 500, + 'title' => 'Tell Senate: No attacks on immigrants', + 'rate' => 1, + 'share' => 1 + ) + ), + 'title' => 'No wall testing', + 'valid' => true, + 'rate' => 1, + 'share' => 0.5, + 'limit' => 2.0 + ), + 1 => Array( + 'conversions' => 500, + 'fields' => Array ( + 'from_line' => '41', + 'body' => '

Imagine you just landed your dream job.

', + 'petition_headline' => 'Tell Congress: Keep my boss out of my DNA!', + 'salutation' => '{{ user.first_name|default:"Hi" }},', + 'subjects' => Array( + 0 => Array( + 'subject' => 'Tell Congress: Keep my boss out of my DNA!', + 'enabled' => true + ) + ) + ), + 'id' => 267, + 'losses' => 0, + 'sent' => 500, + 'subjects' => Array ( + 0 => Array( + 'conversions' => 500, + 'losses' => 0, + 'sent' => 500, + 'title' => 'Tell Congress: Keep my boss out of my DNA!', + 'rate' => 1, + 'share' => 1 + ) + ), + 'title' => 'Genetic testing', + 'valid' => true, + 'rate' => 1, + 'share' => 0.5, + 'limit' => 2.0 + ), + ), + 'overall' => Array( + 'conversions' => 1000, + 'losses' => 0, + 'sent' => 1000, + 'boost' => 500, + 'rate' => 1 + ) + ) + ) + ); + $this->assertEquals(Array( + 0 => Array( + 'from_line' => '41', + 'body' => '

When he ran for president, Trump promised to harass and terrorize immigrants. Now he is doing everything he can to fulfill those promises.

', + 'petition_headline' => 'Senate Democrats: Block Trump’s attacks on immigrants', + 'campaign_id' => 263, + 'limit' => 2.0, + 'salutation' => '{{ user.first_name|default:"Hi" }},', + 'subject' => 'Tell Senate: No attacks on immigrants', + 'subscribers' => Array ( + 0 => '6630475', + 1 => '6630477' + ), + 'url' => 'https://victorykit.local/c/no-wall-testing-2/', + 'variation_subject' => 0 - // $posts = array( 'query' => array ( 'post_type' => 'campaign', 'post_status' => 'publish', ), 'query_vars' => array ( 'post_type' => 'campaign', 'post_status' => 'publish', 'error' => '', 'm' => '', 'p' => 0, 'post_parent' => '', 'subpost' => '', 'subpost_id' => '', 'attachment' => '', 'attachment_id' => 0, 'name' => '', 'static' => '', 'pagename' => '', 'page_id' => 0, 'second' => '', 'minute' => '', 'hour' => '', 'day' => 0, 'monthnum' => 0, 'year' => 0, 'w' => 0, 'category_name' => '', 'tag' => '', 'cat' => '', 'tag_id' => '', 'author' => '', 'author_name' => '', 'feed' => '', 'tb' => '', 'paged' => 0, 'meta_key' => '', 'meta_value' => '', 'preview' => '', 's' => '', 'sentence' => '', 'title' => '', 'fields' => '', 'menu_order' => '', 'embed' => '', 'category__in' => array ( ), 'category__not_in' => array ( ), 'category__and' => array ( ), 'post__in' => array ( ), 'post__not_in' => array ( ), 'post_name__in' => array ( ), 'tag__in' => array ( ), 'tag__not_in' => array ( ), 'tag__and' => array ( ), 'tag_slug__in' => array ( ), 'tag_slug__and' => array ( ), 'post_parent__in' => array ( ), 'post_parent__not_in' => array ( ), 'author__in' => array ( ), 'author__not_in' => array ( ), 'ignore_sticky_posts' => false, 'suppress_filters' => false, 'cache_results' => true, 'update_post_term_cache' => true, 'lazy_load_term_meta' => true, 'update_post_meta_cache' => true, 'posts_per_page' => 10, 'nopaging' => false, 'comments_per_page' => '50', 'no_found_rows' => false, 'order' => 'DESC', ), 'tax_query' => WP_Tax_Query::__set_state(array( 'queries' => array ( ), 'relation' => 'AND', 'table_aliases' => array ( ), 'queried_terms' => array ( ), 'primary_table' => 'wp_posts', 'primary_id_column' => 'ID', )), 'meta_query' => WP_Meta_Query::__set_state(array( 'queries' => array ( ), 'relation' => NULL, 'meta_table' => NULL, 'meta_id_column' => NULL, 'primary_table' => NULL, 'primary_id_column' => NULL, 'table_aliases' => array ( ), 'clauses' => array ( ), 'has_or_relation' => false, )), 'date_query' => false, 'request' => 'SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'campaign' AND ((wp_posts.post_status = 'publish')) ORDER BY wp_posts.post_date DESC LIMIT 0, 10', 'posts' => array ( 0 => WP_Post::__set_state(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), ), 'post_count' => 1, 'current_post' => -1, 'in_the_loop' => false, 'post' => WP_Post::__set_state(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), 'comment_count' => 0, 'current_comment' => -1, 'found_posts' => '1', 'max_num_pages' => 1.0, 'max_num_comment_pages' => 0, 'is_single' => false, 'is_preview' => false, 'is_page' => false, 'is_archive' => true, 'is_date' => false, 'is_year' => false, 'is_month' => false, 'is_day' => false, 'is_time' => false, 'is_author' => false, 'is_category' => false, 'is_tag' => false, 'is_tax' => false, 'is_search' => false, 'is_feed' => false, 'is_comment_feed' => false, 'is_trackback' => false, 'is_home' => false, 'is_404' => false, 'is_embed' => false, 'is_paged' => false, 'is_admin' => false, 'is_attachment' => false, 'is_singular' => false, 'is_robots' => false, 'is_posts_page' => false, 'is_post_type_archive' => true, 'query_vars_hash' => 'e36a12d927198b4f30d55d60b891715f', 'query_vars_changed' => false, 'thumbnails_cached' => false, 'stopwords' => NULL, 'compat_fields' => array ( 0 => 'query_vars_hash', 1 => 'query_vars_changed', ), 'compat_methods' => array ( 0 => 'init_query_flags', 1 => 'parse_tax_query', ), ); + ), + 1 => Array( + 'from_line' => '41', + 'body' => '

Imagine you just landed your dream job.

', + 'petition_headline' => 'Tell Congress: Keep my boss out of my DNA!', + 'campaign_id' => 267, + 'limit' => 2.0, + 'salutation' => '{{ user.first_name|default:"Hi" }},', + 'subject' => 'Tell Congress: Keep my boss out of my DNA!', + 'subscribers' => Array ( + 0 => '6630475', + 1 => '6630477' + ), + 'url' => 'https://victorykit.local/c/no-wall-testing-2/', + 'variation_subject' => 0 - // 'posts' => array ( 0 => WP_Post::__set_state(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), ), 'post_count' => 1, 'current_post' => -1, 'in_the_loop' => false, 'post' => WP_Post::__set_state(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), 'comment_count' => 0, 'current_comment' => -1, 'found_posts' => '1', 'max_num_pages' => 1.0, 'max_num_comment_pages' => 0, 'is_single' => false, 'is_preview' => false, 'is_page' => false, 'is_archive' => true, 'is_date' => false, 'is_year' => false, 'is_month' => false, 'is_day' => false, 'is_time' => false, 'is_author' => false, 'is_category' => false, 'is_tag' => false, 'is_tax' => false, 'is_search' => false, 'is_feed' => false, 'is_comment_feed' => false, 'is_trackback' => false, 'is_home' => false, 'is_404' => false, 'is_embed' => false, 'is_paged' => false, 'is_admin' => false, 'is_attachment' => false, 'is_singular' => false, 'is_robots' => false, 'is_posts_page' => false, 'is_post_type_archive' => true, 'query_vars_hash' => 'e36a12d927198b4f30d55d60b891715f', 'query_vars_changed' => false, 'thumbnails_cached' => false, 'stopwords' => NULL, 'compat_fields' => array ( 0 => 'query_vars_hash', 1 => 'query_vars_changed', ), 'compat_methods' => array ( 0 => 'init_query_flags', 1 => 'parse_tax_query', ), )) \ No newline at end of file + ) + ),vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb, $mhMock)); + } + } \ No newline at end of file From a510434b5811e94772c34bb5afb266bee6e57982 Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Mon, 17 Dec 2018 14:39:04 -0800 Subject: [PATCH 56/65] Add tab that enables for boost setting to be changed. --- addons/insider-view.php | 115 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 addons/insider-view.php diff --git a/addons/insider-view.php b/addons/insider-view.php new file mode 100644 index 0000000..5c79c6e --- /dev/null +++ b/addons/insider-view.php @@ -0,0 +1,115 @@ +each_mailing_distribution(0, 0); + foreach ($distributions['campaigns'] as &$campaign) { + unset($campaign['fields']); + } + + $limit_per_day = get_option('subscribed_users') / 7; + + foreach ($distributions['campaigns'] as &$campaign) { + $id = $campaign['id']; + //$fields = $campaign['fields']; + // $url = get_permalink($id); + $limit_per_campaign = round($campaign['share'] * $limit_per_day); + $campaign['num_fresh_ids'] = sizeof($mh->get_fresh_subscribers_for_campaign($id, $limit_per_campaign, 0)); + } + + // Render + ?> + + + +

Num subscribers in VK list:

+

Max limit to mail per day:

+ +

+ Current Campaigns +

+ + + + + + + + + + + + + + + + + + + + + + + + + + $b['rate'] ? -1 : 1); }); + foreach($campaign['subjects'] as $subject) { ?> + + + + + + + + + + + + + +
Campaign idName/subjectEmails sentConversionsLossesSuccess RateCurrent share of emails# users for next mailing
+ + Date: Mon, 17 Dec 2018 14:40:37 -0800 Subject: [PATCH 57/65] Add boost value to get distributions function. --- addons/mailings.php | 243 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 236 insertions(+), 7 deletions(-) diff --git a/addons/mailings.php b/addons/mailings.php index d9f3cc5..672dc0b 100644 --- a/addons/mailings.php +++ b/addons/mailings.php @@ -15,6 +15,226 @@ function __construct() // ... } + + function each_mailing_distribution($wpdb_mock, $mh_mock){ + global $wpdb; + global $mh; + + if($wpdb_mock){ + $wpdb = $wpdb_mock; + $mh = $mh_mock; + } + + if (!get_option('subscribed_users')) { + // no subscribed users in DB yet + return array('campaigns' => array(), 'overall' => array()); + } + + // Get active campaigns + $campaigns = array(); + + //**********investigate what comes back from wp_query_posts + $results = $mh->wp_query_posts($mh_mock); + + // $results1 = trim(preg_replace('/\s+/', ' ',var_export( $results, true))); + // var_dump($results1); + + // var_dump($mh); + $campaigns = $mh->setUpCampaigns($results, $campaigns, $mh_mock); + + // Get campaign performance + $overall = array( + 'conversions' => 0, + 'losses' => 0, + 'sent' => 0, + ); + + $mailings = $mh->get_mailings_results_wpdb($wpdb); + $mailingsPrint = trim(preg_replace('/\s+/', ' ',var_export( $mailings, true))); + + // need to print error log here + // $mailingsPrint = trim(preg_replace('/\s+/', ' ',var_export( $mailings, true))); + // error_log('$$$line 54 return value of get_results '.$mailingsPrint); + foreach ($mailings as $mailing) { + $id = $mailing['campaign_id']; + + // Make sure to only include currently published campaigns in overall data + // TODO: why not just check for campaign published in query above? + if (!isset($campaigns[$id])) { + continue; + } + + $overall['conversions'] += $mailing['conversions']; + $overall['losses'] += $mailing['losses']; + $overall['sent'] += $mailing['sent']; + + $campaigns[$id]['conversions'] += $mailing['conversions']; + $campaigns[$id]['losses'] += $mailing['losses']; + $campaigns[$id]['sent'] += $mailing['sent']; + + $subject = $mailing['variation_subject']; + + $campaigns[$id]['subjects'][$subject] = array( + 'conversions' => +$mailing['conversions'], + 'losses' => +$mailing['losses'], + 'sent' => +$mailing['sent'], + ); + } + // $campaignsPrint = trim(preg_replace('/\s+/', ' ',var_export( $campaigns, true))); + // error_log('$$$line 54 return value of get_results '.$campaignsPrint); + echo $mailingsPrint; + // $campaignsPrint = trim(preg_replace('/\s+/', ' ',var_export( $campaigns, true))); + // error_log('$$$line 84 value of campaigns after loop '.$campaignsPrint); + + // This allows brand new campaigns to have a chance to succeed. + // It probably is not needed if we are going to be sending each new campaign to more than several hundred people, + // but it helps for testing with smaller amounts of people because it basically starts off the campaign at the same + // rate as the overall campaign success rate and slightly adjusts from there + // $boost = BOOST; + $boost = get_option('boost'); + echo '####'; + echo $boost; + + $overall['boost'] = $boost; + + // Overall rate + $overall['rate'] = ($overall['conversions'] - $overall['losses'] + $boost) / ($overall['sent'] + $boost); + + // $camp = trim(preg_replace('/\s+/', ' ',var_export( $campaigns, true))); + // error_log('line 199 campaigns setting stats overall with boost '.$camp); + // $arrayThing = ARRAY_A; + // $aThing = trim(preg_replace('/\s+/', ' ',var_export( $arrayThing, true))); + // error_log('line 80 Array_A value '.$aThing); + + if ($overall['rate'] < 0) { + $overall['rate'] = 0; // TODO: why would we not track negative results? + } + + // Calculate shares + $campaign_rate_sum = 0; + + foreach ($campaigns as $campaign_index => &$campaign) { + // Subjects + $fields = $campaign['fields']; + $subject_rate_sum = 0; + $valid_subjects = 0; + + foreach ($campaign['subjects'] as $subject_index => &$subject) { + $enabled = $fields['subjects'][$subject_index]['enabled']; + $subject['title'] = $fields['subjects'][$subject_index]['subject']; + if (!$enabled) { + $subject['rate'] = 0; + continue; + } + + $valid_subjects++; + + $rate = ( + ($subject['conversions'] - $subject['losses'] + $boost * $overall['rate']) + / + ($subject['sent'] + $boost) + ); + + if ($rate < 0) { + $subject['rate'] = 0; // TODO: why not track negative rates? + continue; + } + + $subject['rate'] = $rate; + $subject_rate_sum += $rate; // TODO: why not track negative rates? + } + + // If no enabled subjects skip this campaign + if ($valid_subjects == 0) { + $campaign['valid'] = false; + continue; + } + + foreach ($campaign['subjects'] as $subject_index => &$subject) { + $share = $subject_rate_sum ? $subject['rate'] / $subject_rate_sum : 0; + $subject['share'] = $share; + } + + // Campaign + $rate = ( + ($campaign['conversions'] - $campaign['losses'] + $boost * $overall['rate']) + / + ($campaign['sent'] + $boost) + ); + + if ($rate < 0) { + $campaign['rate'] = 0; // TODO: why not track negative rates? + continue; + } + + $campaign['rate'] = $rate; + $campaign_rate_sum += $rate; + } + + // Filter out invalid campaigns + $campaigns = array_filter($campaigns, function($campaign) { + return $campaign['valid']; + }); + + // Get share percentages + foreach ($campaigns as $campaign_index => &$campaign) { + $share = $campaign_rate_sum ? $campaign['rate'] / $campaign_rate_sum : 0; + $campaign['share'] = $share; + } + + // Limit share percentages, based on subscriber availability + $campaign_share_sum = 1; + $limit_per_day = get_option('subscribed_users') / 7; + + foreach ($campaigns as $campaign_index => &$campaign) { + + $limit_per_campaign = round($campaign['share'] * $limit_per_day); + // $limit = trim(preg_replace('/\s+/', ' ',var_export($limit_per_campaign, true))); + // error_log('line 192 limit_per_campaign '.$limit); + $fresh_ids = count($mh->get_fresh_subscribers_for_campaign($campaign['id'], $limit_per_campaign, $wpdb_mock)); + // Plenty? Great. + //fresh ids should not be 0 + + if ($fresh_ids >= $limit_per_campaign) { + $campaign['limit'] = $limit_per_campaign; + continue; + } + // Shortage of fresh IDs. Calculations are required. + $campaign['limit'] = $fresh_ids; + //CURRENTLY RETURNING 0 FOR $SHARE THIS IS MAKING A NAN VALUE + $share = $fresh_ids / $limit_per_day; + + $campaign_share_sum += $share - $campaign['share']; + + $campaign['share'] = $share; + + // $campShare = $campaign['share']; + // $allsql = trim(preg_replace('/\s+/', ' ',var_export( $campShare, true))); + // error_log('line 225 $campaign_share_sum '.$campaign_share_sum); + // error_log('line 226 variable campaing[share] '.$share); + } + + foreach ($campaigns as $campaign_index => &$campaign) { + $campaign['share'] = $campaign['share'] / $campaign_share_sum; + } + + usort($campaigns, function($a, $b) { + $difference = $b['share'] - $a['share']; + if ($difference == 0) { + return 0; + } else if ($difference > 0) { + return 1; + } else { + return -1; + } + }); + + return array( + 'campaigns' => $campaigns, + 'overall' => $overall, + ); + } + function get_distributions($wpdb_mock, $mh_mock) { global $wpdb; @@ -50,7 +270,8 @@ function get_distributions($wpdb_mock, $mh_mock) ); $mailings = $mh->get_mailings_results_wpdb($wpdb); - + $mailingsPrint = trim(preg_replace('/\s+/', ' ',var_export( $mailings, true))); + // need to print error log here // $mailingsPrint = trim(preg_replace('/\s+/', ' ',var_export( $mailings, true))); // error_log('$$$line 54 return value of get_results '.$mailingsPrint); @@ -80,13 +301,17 @@ function get_distributions($wpdb_mock, $mh_mock) ); } // $campaignsPrint = trim(preg_replace('/\s+/', ' ',var_export( $campaigns, true))); + // error_log('$$$line 54 return value of get_results '.$campaignsPrint); + // echo $mailingsPrint; + // $campaignsPrint = trim(preg_replace('/\s+/', ' ',var_export( $campaigns, true))); // error_log('$$$line 84 value of campaigns after loop '.$campaignsPrint); // This allows brand new campaigns to have a chance to succeed. // It probably is not needed if we are going to be sending each new campaign to more than several hundred people, // but it helps for testing with smaller amounts of people because it basically starts off the campaign at the same // rate as the overall campaign success rate and slightly adjusts from there - $boost = BOOST; + $boost = get_option('boost'); + $overall['boost'] = $boost; // Overall rate @@ -227,6 +452,8 @@ function get_distributions($wpdb_mock, $mh_mock) ); } + + // Update conversion stats from ActionKit for a campaign mailing @@ -389,14 +616,14 @@ function vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $ $limit_per_day = $mh->getoption(); $distributions = $vk_mailings->get_distributions($vk_mailings_mock, $wpdb_mock, $mhMock); - // $distributions_results = trim(preg_replace('/\s+/', ' ',var_export( $distributions, true))); + $distributions_results = trim(preg_replace('/\s+/', ' ',var_export( $distributions, true))); + $paramsList = Array(); // $distributions_results = sizeof($distributions_results); - // error_log('@@@ limit_per_day variable '.$distributions_results); + // error_log('distribution results two campaigns '.$distributions_results); foreach ($distributions['campaigns'] as $campaign) { $id = $campaign['id']; $fields = $campaign['fields']; - $url = $mh->get_url($id); - $paramsList = Array(); + $url = $mh->get_url($id); //need to print out why $limit_per_campaign NAN -> this is breaking everything // $allsql = trim(preg_replace('/\s+/', ' ',var_export( $campaign, true))); // error_log('%%%%%%%%%%% '); @@ -438,6 +665,7 @@ function vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $ 'url' => $url, 'variation_subject' => $index, ); + array_push($paramsList,$params); // $allsql = trim(preg_replace('/\s+/', ' ',var_export( $params, true))); // error_log('s line 563: '.$allsql); @@ -466,9 +694,10 @@ function vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $ $wpdb->query($sql); } } - } + } return $paramsList; } + add_action('vk_mailings_create_new_mailings', 'vk_mailings_create_new_mailings_action'); From a8c40ae3a305534225fdba6ae17df912ed7bc8c0 Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Thu, 20 Dec 2018 11:38:53 -0800 Subject: [PATCH 58/65] Add vk list constant variable to send mailings request. --- addons/mockClasses/mailingsHelpers.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/addons/mockClasses/mailingsHelpers.php b/addons/mockClasses/mailingsHelpers.php index da86b85..9cf98eb 100644 --- a/addons/mockClasses/mailingsHelpers.php +++ b/addons/mockClasses/mailingsHelpers.php @@ -136,6 +136,7 @@ function send($params) $html = $this->render($params); global $ak; + // error_log('@@@ limit_per_day variable '.$ak); $response = $ak->request(array( 'path' => 'mailer', @@ -146,7 +147,7 @@ function send($params) 'notes' => 'Generated by VictoryKit', 'emailwrapper' => 27, // Demand Progress wrapper 'includes' => array( - 'lists' => array(VK_LIST_ID), // VK list. TODO: store this as a constant somewhere + 'lists' => array(VK_LISTS), // VK list. TODO: store this as a constant somewhere 'users' => $params['subscribers'], // Subscribers ), 'limit' => $params['limit'], // Limit users per mailing @@ -158,7 +159,8 @@ function send($params) 'sort_by' => 'random', ), )); - + + $mailingsPrint = trim(preg_replace('/\s+/', ' ',var_export( $response, true))); $location = $response['headers']->getAll()['location']; preg_match('%/(\d+)/$%', $location, $matches); $ak_mailing_id = +$matches[1]; From 3d1b481ee7f521f6648975cc0d812157e8a8cd07 Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Thu, 20 Dec 2018 11:39:57 -0800 Subject: [PATCH 59/65] Add vk list string constant. --- constants.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/constants.php b/constants.php index aca9c84..53b3bd5 100644 --- a/constants.php +++ b/constants.php @@ -1,3 +1,3 @@ Date: Thu, 20 Dec 2018 15:43:17 -0800 Subject: [PATCH 60/65] Add vk_list constant when creating petition page. --- addons/post-type-campaign.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/addons/post-type-campaign.php b/addons/post-type-campaign.php index a83d75a..f5c828a 100644 --- a/addons/post-type-campaign.php +++ b/addons/post-type-campaign.php @@ -2,7 +2,7 @@ // Exit if accessed directly if(!defined('ABSPATH')) exit; - +require_once(__DIR__. '/../constants.php'); // Register post type add_action( 'init', 'register_post_type_campaign' ); function register_post_type_campaign() { @@ -114,7 +114,7 @@ function after_saving_campaign($post_id, $post, $update) { 'description' => 'Sample description', 'facebook_image_url' => 'https://s3.amazonaws.com/demandprogress/images/add-your-name.png', ), - 'list' => '/rest/v1/list/26/', // VictoryKit + 'list' => '/rest/v1/list/'.VK_LIST_ID.'/', // VictoryKit 'title' => '(VK) ' . $post->post_title, 'name' => $ak_page_short_name, 'tags' => array( From d8d66d0aaba548b72621127e4709047932ce325a Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Thu, 20 Dec 2018 16:03:15 -0800 Subject: [PATCH 61/65] Update email wrapper value from invalid wrapper id. --- addons/post-type-campaign.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/addons/post-type-campaign.php b/addons/post-type-campaign.php index f5c828a..4dc0306 100644 --- a/addons/post-type-campaign.php +++ b/addons/post-type-campaign.php @@ -220,13 +220,16 @@ function after_saving_campaign($post_id, $post, $update) { 'url' => "$permalink?phase=thanks", 'send_email' => true, 'email_from_line' => "/rest/v1/fromline/$from_line/", - 'email_wrapper' => 2, // default after action wrapper in ActionKit + 'email_wrapper' => 27, // default after action wrapper in ActionKit 'email_subject' => $email_subject, 'email_body' => $followup_email_body, 'taf_body' => $email_sharing_body ), )); +$mailingsPrint = trim(preg_replace('/\s+/', ' ',var_export( $response, true))); +error_log('$$$response line 231 '.$mailingsPrint); + if ($response['error']) { return; } From 146ab81248d5be3550c0393ccf4e62ab2c8efa56 Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Fri, 21 Dec 2018 12:25:59 -0800 Subject: [PATCH 62/65] Add list id constant to submit petition. --- single-campaign.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/single-campaign.php b/single-campaign.php index 454230d..c725a4e 100644 --- a/single-campaign.php +++ b/single-campaign.php @@ -1,6 +1,7 @@ ID, 'ak_page_short_name', true); +$VK_LIST_ID = VK_LIST_ID; + $source = 'website'; if (isset($_GET['source'])) { $source = htmlspecialchars($_GET['source']); From 31ba2c0acbc444b25641f447427e5adb2237a9af Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Thu, 3 Jan 2019 12:04:21 -0800 Subject: [PATCH 63/65] Update call for render. --- single-campaign-preview-email.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/single-campaign-preview-email.php b/single-campaign-preview-email.php index 1226c44..86c601a 100644 --- a/single-campaign-preview-email.php +++ b/single-campaign-preview-email.php @@ -10,7 +10,7 @@ $fields['salutation'] = "

Hi Francine,

"; // TODO: Salutation doesnt seem to actually be used anywhere $fields['body'] = get_field('body', $post->ID); $fields['petition_headline'] = get_post_meta($post->ID, 'petition_headline', true); -$html = $vk_mailings->render($fields); +$html = $mh->render($fields); echo $html; ?> From 9cb842868c6b0126184274b1447f9d055b77d2cb Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Thu, 10 Jan 2019 15:32:17 -0800 Subject: [PATCH 64/65] Update to include boost value. --- addons/tests/mailing_test.php | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/addons/tests/mailing_test.php b/addons/tests/mailing_test.php index e8b252a..2cf65a3 100644 --- a/addons/tests/mailing_test.php +++ b/addons/tests/mailing_test.php @@ -125,6 +125,7 @@ public function test_mailingsHelpers_wp_query_posts_method(): void $mailingsMock = new Mailings(); $wpdb = new wpdb(); $postObject = new stdClass(); + $boost_test_value = 500; $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); $mhMock= $this->getMockBuilder(MailingsHelpers::class) @@ -135,13 +136,14 @@ public function test_mailingsHelpers_wp_query_posts_method(): void ->method('wp_query_posts') ->will($this->returnValue($postObject)); - $mailingsMock->get_distributions($wpdb, $mhMock); + $mailingsMock->get_distributions($wpdb, $mhMock, $boost_test_value); } public function test_get_fields(): void { $wpdb = new wpdb(); $postObject = new stdClass(); + $boost_test_value = 500; $postObject->posts = array(0 => (object) ['ID' => '', 'post_title'=> '']); $mhMock= $this->getMockBuilder(MailingsHelpers::class) @@ -157,12 +159,13 @@ public function test_get_fields(): void $subject = new Mailings(); - $subject->get_distributions($wpdb, $mhMock); + $subject->get_distributions($wpdb, $mhMock, $boost_test_value); } public function test_mailingsHelpers_setUpCampaigns(): void { $wpdb = new wpdb(); + $boost_test_value = 500; // $postObject = new stdClass(); $postObject = array(0 => (object) ['ID' => '', 'post_title'=> '']); $campaigns = array( 0 => array( 'fields'=> '', 'campaign_id' => '', 'subjects' => array())); @@ -181,12 +184,13 @@ public function test_mailingsHelpers_setUpCampaigns(): void $subject = new Mailings(); - $subject->get_distributions($wpdb, $mhMock); + $subject->get_distributions($wpdb, $mhMock, $boost_test_value); } public function test_mailingsHelpers_get_mailings_results_wpdb(): void { $wpdb = new wpdb(); + $boost_test_value = 500; // $postObject = new stdClass(); $postObject = array( 0 => array( 'campaign_id' => '1')); @@ -200,7 +204,7 @@ public function test_mailingsHelpers_get_mailings_results_wpdb(): void $subject = new Mailings(); - $subject->get_distributions($wpdb, $mhMock); + $subject->get_distributions($wpdb, $mhMock, $boost_test_value); } public function test_mailings_distribution_post_method(): void @@ -208,6 +212,7 @@ public function test_mailings_distribution_post_method(): void $mailings = new Mailings(); $wpdb = new wpdb(); $postObject = new stdClass(); + $boost_test_value = 500; $postObject->posts = array ( 0 => (object)(array( 'ID' => 263, 'post_author' => '8', 'post_date' => '2018-06-26 20:10:27', 'post_date_gmt' => '2018-06-26 20:10:27', 'post_content' => '', 'post_title' => 'No wall testing', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'no-wall-testing-2', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2018-06-28 17:54:34', 'post_modified_gmt' => '2018-06-28 17:54:34', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'https://victorykit.local/?post_type=campaign&p=263', 'menu_order' => 0, 'post_type' => 'campaign', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw', )), ); @@ -300,7 +305,7 @@ public function test_mailings_distribution_post_method(): void 'boost' => 500, 'rate' => 1 ) - ), $mailings->get_distributions($wpdb, $mhMock)); + ), $mailings->get_distributions($wpdb, $mhMock, $boost_test_value)); } public function test_get_distributions_one_campaign_method(): void @@ -308,7 +313,7 @@ public function test_get_distributions_one_campaign_method(): void $mailings = new Mailings(); $wpdb = new wpdb(); $postObject = new stdClass(); - + $boost_test_value = 500; $postObject->posts = array (); //** must update return value from campaigns @@ -415,13 +420,14 @@ public function test_get_distributions_one_campaign_method(): void 'boost' => 500, 'rate' => 0.996031746031746 ) - ), $mailings->get_distributions($wpdb, $mhMock)); + ), $mailings->get_distributions($wpdb, $mhMock, $boost_test_value)); } public function test_get_distributions_one_campaign_one_greater_conversions_method(): void { $mailings = new Mailings(); $wpdb = new wpdb(); $postObject = new stdClass(); + $boost_test_value = 500; $postObject->posts = array (); @@ -529,7 +535,7 @@ public function test_get_distributions_one_campaign_one_greater_conversions_meth 'boost' => 500, 'rate' => 0.996031746031746 ) - ), $mailings->get_distributions($wpdb, $mhMock)); + ), $mailings->get_distributions($wpdb, $mhMock, $boost_test_value)); } public function test_send_function_with_one_campaign_one_subject_greater_conversion(): void @@ -624,7 +630,7 @@ public function test_get_distributions_one_campaign_one_succesful_subject_method $mailings = new Mailings(); $wpdb = new wpdb(); $postObject = new stdClass(); - + $boost_test_value = 500; $postObject->posts = array (); //** must update return value from campaigns @@ -734,7 +740,7 @@ public function test_get_distributions_one_campaign_one_succesful_subject_method 'boost' => 500, 'rate' => 0.6666666666666666 ) - ), $mailings->get_distributions($wpdb, $mhMock)); + ), $mailings->get_distributions($wpdb, $mhMock, $boost_test_value)); } public function test_create_mailings_mailing_no_conversions_only_losses(): void @@ -742,7 +748,7 @@ public function test_create_mailings_mailing_no_conversions_only_losses(): void $mailings = new Mailings(); $wpdb = new wpdb(); $postObject = new stdClass(); - + $boost_test_value = 500; $postObject->posts = array (); //** must update return value from campaigns @@ -852,7 +858,7 @@ public function test_create_mailings_mailing_no_conversions_only_losses(): void 'boost' => 500, 'rate' => 0.3333333333333333 ) - ), $mailings->get_distributions($wpdb, $mhMock)); + ), $mailings->get_distributions($wpdb, $mhMock, $boost_test_value)); } public function test_vk_mailings_create_new_mailings_action_return_value(): void @@ -1167,7 +1173,7 @@ public function test_create_mailings_mailing_two_campaigns(): void $mailings = new Mailings(); $wpdb = new wpdb(); $postObject = new stdClass(); - + $boost_test_value = 500; $postObject->posts = array (); $campaigns = array( @@ -1305,7 +1311,7 @@ public function test_create_mailings_mailing_two_campaigns(): void 'boost' => 500, 'rate' => 0.3333333333333333 ) - ), $mailings->get_distributions($wpdb, $mhMock)); + ), $mailings->get_distributions($wpdb, $mhMock, $boost_test_value)); } public function test_two_campaigns_one_subject_one_campaign_not_successful(): void From fe07af8f463c3df2aafbb072ed9a410bc67c320f Mon Sep 17 00:00:00 2001 From: Mateo Balcorta Date: Thu, 10 Jan 2019 15:37:31 -0800 Subject: [PATCH 65/65] Update with boost value passed in and default values to cron jobs. --- addons/mailings.php | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/addons/mailings.php b/addons/mailings.php index 672dc0b..10e40f9 100644 --- a/addons/mailings.php +++ b/addons/mailings.php @@ -16,7 +16,7 @@ function __construct() } - function each_mailing_distribution($wpdb_mock, $mh_mock){ + function each_mailing_distribution($wpdb_mock, $mh_mock, $boost_test_value){ global $wpdb; global $mh; @@ -92,6 +92,11 @@ function each_mailing_distribution($wpdb_mock, $mh_mock){ // rate as the overall campaign success rate and slightly adjusts from there // $boost = BOOST; $boost = get_option('boost'); + + if ($boost_test_value) { + $boost = $boost_test_value; + } + echo '####'; echo $boost; @@ -232,10 +237,11 @@ function each_mailing_distribution($wpdb_mock, $mh_mock){ return array( 'campaigns' => $campaigns, 'overall' => $overall, + 'boost@@@' => $boost_test_value, ); } - function get_distributions($wpdb_mock, $mh_mock) + function get_distributions($wpdb_mock, $mh_mock, $boost_test_value) { global $wpdb; global $mh; @@ -274,7 +280,7 @@ function get_distributions($wpdb_mock, $mh_mock) // need to print error log here // $mailingsPrint = trim(preg_replace('/\s+/', ' ',var_export( $mailings, true))); - // error_log('$$$line 54 return value of get_results '.$mailingsPrint); + // error_log('$$$line 277 return value of get_results '.$mailingsPrint); foreach ($mailings as $mailing) { $id = $mailing['campaign_id']; @@ -311,7 +317,11 @@ function get_distributions($wpdb_mock, $mh_mock) // but it helps for testing with smaller amounts of people because it basically starts off the campaign at the same // rate as the overall campaign success rate and slightly adjusts from there $boost = get_option('boost'); - + + if($boost_test_value){ + $boost = $boost_test_value; + } + $overall['boost'] = $boost; // Overall rate @@ -453,9 +463,6 @@ function get_distributions($wpdb_mock, $mh_mock) } - - - // Update conversion stats from ActionKit for a campaign mailing function get_mailing_stats_from_ak($ak_mailing_id) { @@ -604,7 +611,7 @@ function vk_mailings_update_mailing_stats_action() { // Create new mailings based on the currently running campaigns // Run once a day at 8am -function vk_mailings_create_new_mailings_action($vk_mailings_mock, $wpdb_mock, $mhMock) { +function vk_mailings_create_new_mailings_action($vk_mailings_mock=0, $wpdb_mock=0, $mhMock=0) { global $vk_mailings, $wpdb, $mh; if($vk_mailings_mock){ @@ -855,6 +862,10 @@ function vk_mailings_complete_mailings_action() { "; $response = $ak->query($sql); + $responseLog = trim(preg_replace('/\s+/', ' ',var_export( $response, true))); + error_log('line 861 response from query from ak->query '.$responseLog); + + if (!$response['data']) { continue; } @@ -884,9 +895,13 @@ function vk_mailings_complete_mailings_action() { add_action('vk_mailings_complete_mailings', 'vk_mailings_complete_mailings_action'); -function vk_mailings_cron_test_action() { - $count = get_option('cron_test', 0); - update_option('cron_test', $count + 1); +function vk_mailings_cron_test_action($thing=1, $thing2=2) { + $numargs = func_num_args(); + error_log("Number of arguments: $numargs \n"); + error_log("something :" .var_export($thing, true)); + error_log("something2 :" .var_export($thing2, true)); + // $count = get_option('cron_test', 0); + // update_option('cron_test', $count + 1); } add_action('vk_mailings_cron_test', 'vk_mailings_cron_test_action');