diff --git a/lib/Models/StatisticsClicks.php b/lib/Models/StatisticsClicks.php index 62dc5b66d2..cd10a2626a 100644 --- a/lib/Models/StatisticsClicks.php +++ b/lib/Models/StatisticsClicks.php @@ -57,18 +57,12 @@ class StatisticsClicks extends Model { // subquery to find latest click IDs for each newsletter $table = self::$_table; $latest_click_ids_per_newsletter_query = " - SELECT ( - SELECT id - FROM $table - WHERE newsletter_id = c.newsletter_id - ORDER BY updated_at DESC - LIMIT 1 - ) - FROM $table c - WHERE c.subscriber_id = :subscriber_id - AND c.updated_at > :from - AND c.updated_at < :to - GROUP BY c.newsletter_id + SELECT MAX(id) + FROM $table + WHERE subscriber_id = :subscriber_id + AND updated_at > :from + AND updated_at < :to + GROUP BY newsletter_id "; return static::tableAlias('clicks') diff --git a/tests/integration/Statistics/Track/WooCommercePurchasesTest.php b/tests/integration/Statistics/Track/WooCommercePurchasesTest.php index 438ea77ab9..26833d82aa 100644 --- a/tests/integration/Statistics/Track/WooCommercePurchasesTest.php +++ b/tests/integration/Statistics/Track/WooCommercePurchasesTest.php @@ -43,6 +43,21 @@ class WooCommercePurchasesTest extends \MailPoetTest { $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() { $click = $this->createClick($this->link, $this->subscriber); $order_mock = $this->createOrderMock($this->subscriber->email); @@ -102,14 +117,14 @@ class WooCommercePurchasesTest extends \MailPoetTest { } function testItTracksPaymentOnlyForLatestClick() { - $latest_click = $this->createClick($this->link, $this->subscriber, 1); $this->createClick($this->link, $this->subscriber, 3); $this->createClick($this->link, $this->subscriber, 5); + $latest_click = $this->createClick($this->link, $this->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(); + $purchase_stats = StatisticsWooCommercePurchases::orderByDesc('created_at')->findMany(); expect(count($purchase_stats))->equals(1); expect($purchase_stats[0]->click_id)->equals($latest_click->id); } @@ -147,6 +162,18 @@ class WooCommercePurchasesTest extends \MailPoetTest { 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() { $order_email = 'order.email@example.com'; $cookie_email = 'cookie.email@example.com';