Fix and simplify query for latest click per-newsleter-per-subscriber

[MAILPOET-2446]
This commit is contained in:
Jan Jakeš
2019-10-08 14:35:45 +02:00
committed by Jack Kitterhing
parent a4c1bac911
commit 0091755301
2 changed files with 35 additions and 14 deletions

View File

@@ -57,18 +57,12 @@ class StatisticsClicks extends Model {
// subquery to find latest click IDs for each newsletter // subquery to find latest click IDs for each newsletter
$table = self::$_table; $table = self::$_table;
$latest_click_ids_per_newsletter_query = " $latest_click_ids_per_newsletter_query = "
SELECT ( SELECT MAX(id)
SELECT id FROM $table
FROM $table WHERE subscriber_id = :subscriber_id
WHERE newsletter_id = c.newsletter_id AND updated_at > :from
ORDER BY updated_at DESC AND updated_at < :to
LIMIT 1 GROUP BY newsletter_id
)
FROM $table c
WHERE c.subscriber_id = :subscriber_id
AND c.updated_at > :from
AND c.updated_at < :to
GROUP BY c.newsletter_id
"; ";
return static::tableAlias('clicks') return static::tableAlias('clicks')

View File

@@ -43,6 +43,21 @@ class WooCommercePurchasesTest extends \MailPoetTest {
$this->cookies = new Cookies(); $this->cookies = new Cookies();
} }
function testItDoesNotTrackPaymentForWrongSubscriber() {
$click = $this->createClick($this->link, $this->subscriber, 3);
// create 'wrong_click' for different subscriber that is newer than the correct 'click'
$wrong_subscriber = $this->createSubscriber('wrong.subscriber@example.com');
$wrong_click = $this->createClick($this->link, $wrong_subscriber, 1);
$order_mock = $this->createOrderMock($this->subscriber->email);
$woocommerce_purchases = new WooCommercePurchases($this->createWooCommerceHelperMock($order_mock), $this->cookies);
$woocommerce_purchases->trackPurchase($order_mock->get_id());
$purchase_stats = StatisticsWooCommercePurchases::findMany();
expect(count($purchase_stats))->equals(1);
expect($purchase_stats[0]->click_id)->equals($click->id);
}
function testItTracksPayment() { function testItTracksPayment() {
$click = $this->createClick($this->link, $this->subscriber); $click = $this->createClick($this->link, $this->subscriber);
$order_mock = $this->createOrderMock($this->subscriber->email); $order_mock = $this->createOrderMock($this->subscriber->email);
@@ -102,14 +117,14 @@ class WooCommercePurchasesTest extends \MailPoetTest {
} }
function testItTracksPaymentOnlyForLatestClick() { function testItTracksPaymentOnlyForLatestClick() {
$latest_click = $this->createClick($this->link, $this->subscriber, 1);
$this->createClick($this->link, $this->subscriber, 3); $this->createClick($this->link, $this->subscriber, 3);
$this->createClick($this->link, $this->subscriber, 5); $this->createClick($this->link, $this->subscriber, 5);
$latest_click = $this->createClick($this->link, $this->subscriber, 1);
$order_mock = $this->createOrderMock($this->subscriber->email); $order_mock = $this->createOrderMock($this->subscriber->email);
$woocommerce_purchases = new WooCommercePurchases($this->createWooCommerceHelperMock($order_mock), $this->cookies); $woocommerce_purchases = new WooCommercePurchases($this->createWooCommerceHelperMock($order_mock), $this->cookies);
$woocommerce_purchases->trackPurchase($order_mock->get_id()); $woocommerce_purchases->trackPurchase($order_mock->get_id());
$purchase_stats = StatisticsWooCommercePurchases::findMany(); $purchase_stats = StatisticsWooCommercePurchases::orderByDesc('created_at')->findMany();
expect(count($purchase_stats))->equals(1); expect(count($purchase_stats))->equals(1);
expect($purchase_stats[0]->click_id)->equals($latest_click->id); expect($purchase_stats[0]->click_id)->equals($latest_click->id);
} }
@@ -147,6 +162,18 @@ class WooCommercePurchasesTest extends \MailPoetTest {
expect(count(StatisticsWooCommercePurchases::findMany()))->equals(0); expect(count(StatisticsWooCommercePurchases::findMany()))->equals(0);
} }
function testItTracksPaymentForCorrectClickWhenClickNewerThanOrderExists() {
$click = $this->createClick($this->link, $this->subscriber, 5);
$this->createClick($this->link, $this->subscriber, 0); // wrong click, should not be tracked
$order_mock = $this->createOrderMock($this->subscriber->email);
$woocommerce_purchases = new WooCommercePurchases($this->createWooCommerceHelperMock($order_mock), $this->cookies);
$woocommerce_purchases->trackPurchase($order_mock->get_id());
$purchase_stats = StatisticsWooCommercePurchases::findMany();
expect($purchase_stats)->count(1);
expect($purchase_stats[0]->click_id)->equals($click->id);
}
function testItTracksByCookie() { function testItTracksByCookie() {
$order_email = 'order.email@example.com'; $order_email = 'order.email@example.com';
$cookie_email = 'cookie.email@example.com'; $cookie_email = 'cookie.email@example.com';