From ae322199bc508b1ade26530ffe9ef59674f964bf Mon Sep 17 00:00:00 2001 From: Marko Saric <34340819+metmarkosaric@users.noreply.github.com> Date: Thu, 6 Jun 2024 14:54:59 +0200 Subject: [PATCH 01/14] Update readme.txt --- readme.txt | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/readme.txt b/readme.txt index f7c0195d..0fbb38f4 100644 --- a/readme.txt +++ b/readme.txt @@ -47,9 +47,13 @@ Plausible is lightweight analytics. Our script is 45 times smaller than Google A Plausible is privacy-friendly analytics. All the site measurement is carried out absolutely anonymously. Cookies are not used and no personal data is collected. There are no persistent identifiers. No cross-site or cross-device tracking either. Your site data is not used for any other purposes. All visitor data is exclusively processed with servers owned and operated by European companies and it never leaves the EU. -### Track goal conversions, revenue and campaigns +### Track events and marketing campaigns -Plausible is useful. Segment your audience by any metric you click on. Answer the important questions about your visitors, content and referral sources. Analyze paid campaigns using UTM parameters. Track WooCommerce revenue, outbound link clicks, file downloads and 404 error pages. Create custom events with custom dimensions to track conversions and attribution. Increase conversions using funnel analysis. +Plausible is useful. Segment your audience by any metric you click on. Answer the important questions about your visitors, content and referral sources. Analyze paid campaigns using UTM parameters. Track site search terms, outbound link clicks, file downloads, 404 error pages, post authors, post categories and custom taxonomies without manually configuring anything or writing any code. + +### Built-in WooCommerce analytics + +Plausible provides an automatic WooCommerce analytics solution to track conversions, revenue and attribution. Activities tracked include adding to cart, removing from cart, entering checkout and completing a purchase. A purchase funnel looking at the user journey from viewing a product to making a purchase is enabled to help you see the drop-off rates between the different steps, understand your cart abandonment rate and increase your conversions. ### Invite team members and share your dashboard @@ -57,18 +61,18 @@ Plausible is shareable. Your stats are private by default but you can choose to ### Transparent and open source software -Plausible is open source analytics. Our source code is available and accessible on GitHub so anyone can read it, inspect it and review it to verify that our actions match with our words. We welcome feedback and have a public roadmap. If you're happy to manage your own infrastructure, you can self-host Plausible too. +Plausible is open source analytics. Our source code is available and accessible on GitHub so anyone can read it, inspect it and review it to verify that our actions match with our words. We welcome feedback and have a public roadmap. If you're happy to manage your own infrastructure, you can self-host Plausible too. ## Features * Our product is updated several times per week and with our WordPress plugin you always have access to all the latest features * Automatically includes tracking code in the header of your site * Simple plugin settings page with easy options and an onboarding guide -* Get more accurate stats by running the Plausible script as a first-party connection from your domain name +* Get more accurate stats and count those who use adblockers by running the Plausible script as a first-party connection from your domain name * View your Plausible stats directly in your WordPress dashboard (you can grant access to other user roles too) * Tracking of admin users is disabled by default (you can also disable tracking of other user roles) -* Enable ecommerce revenue, file downloads, external link clicks and 404 error pages tracking -* Enable automated tracking of post authors and post categories for better content analysis +* Enable WooCommerce revenue, file downloads, external link clicks, site search terms and 404 error pages tracking +* Enable automated tracking of post authors, post categories and custom taxonomies for better content analysis * Custom events and custom dimensions can be setup using CSS class names directly in the WordPress editor * Integrate with Google Search Console so you can see search queries people use to find your site in Google's search results * Import your historical Google Analytics stats From 3b77e51091114f4d67bd9c1c7e53a463061c15ca Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Thu, 6 Jun 2024 20:31:06 +0200 Subject: [PATCH 02/14] Ignore safety measures and untestable code. --- src/Admin/Provisioning.php | 2 +- src/Helpers.php | 2 +- src/Plugin.php | 2 +- src/Proxy.php | 6 +++++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Admin/Provisioning.php b/src/Admin/Provisioning.php index 2ad99435..88891d50 100644 --- a/src/Admin/Provisioning.php +++ b/src/Admin/Provisioning.php @@ -206,7 +206,7 @@ private function create_goals( $goals ) { */ public function maybe_create_woocommerce_goals( $old_settings, $settings ) { if ( ! Helpers::is_enhanced_measurement_enabled( 'revenue', $settings[ 'enhanced_measurements' ] ) || ! Integrations::is_wc_active() ) { - return; + return; // @codeCoverageIgnore } $goals = []; diff --git a/src/Helpers.php b/src/Helpers.php index 99f1ff27..0a091686 100644 --- a/src/Helpers.php +++ b/src/Helpers.php @@ -204,7 +204,7 @@ public static function is_enhanced_measurement_enabled( $name, $enhanced_measure } if ( ! is_array( $enhanced_measurements ) ) { - return false; + return false; // @codeCoverageIgnore } return in_array( $name, $enhanced_measurements ); diff --git a/src/Plugin.php b/src/Plugin.php index e86fdfb8..c0124c0d 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -41,7 +41,7 @@ public function register_services() { } if ( Helpers::is_enhanced_measurement_enabled( 'revenue' ) ) { - new Integrations(); + new Integrations(); // @codeCoverageIgnore } new Actions(); diff --git a/src/Proxy.php b/src/Proxy.php index 2a3a8e1a..dd8b8ca9 100644 --- a/src/Proxy.php +++ b/src/Proxy.php @@ -116,7 +116,7 @@ public function do_request( $name = 'pageview', $domain = '', $url = '', $props ]; if ( ! empty( $props ) ) { - $body[ 'p' ] = $props; + $body[ 'p' ] = $props; // @codeCoverageIgnore } $request->set_body( wp_json_encode( $body ) ); @@ -189,6 +189,8 @@ private function header_exists( $global ) { * Register the API route. * * @return void + * + * @codeCoverageIgnore Because we have no way of knowing if the API works in integration tests. */ public function register_route() { register_rest_route( @@ -214,6 +216,8 @@ public function register_route() { * @param WP_REST_Request $request * * @return WP_HTTP_Response + * + * @codeCoverageIgnore */ public function force_http_response_code( $response, $server, $request ) { if ( strpos( $request->get_route(), $this->namespace ) === false ) { From 25faee6f3a868edbf18719a2c3fa27bf9bbc7bfd Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Thu, 6 Jun 2024 20:34:39 +0200 Subject: [PATCH 03/14] This code won't be tested here. --- src/Compatibility.php | 3 +++ src/Integrations.php | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/Compatibility.php b/src/Compatibility.php index 1ace0275..0dba07db 100644 --- a/src/Compatibility.php +++ b/src/Compatibility.php @@ -11,6 +11,9 @@ use Exception; +/** + * @codeCoverageIgnore Because this is to be tested in a headless browser. + */ class Compatibility { /** * A list of filters and actions to prevent our script from being manipulated by other plugins, known to cause issues. diff --git a/src/Integrations.php b/src/Integrations.php index 77173600..feab82fb 100644 --- a/src/Integrations.php +++ b/src/Integrations.php @@ -10,6 +10,9 @@ namespace Plausible\Analytics\WP; +/** + * @codeCoverageIgnore Because the code is very straight-forward. + */ class Integrations { const SCRIPT_WRAPPER = ''; From 6441a3539b5d474c774f1738704c88a30e35acdf Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Fri, 7 Jun 2024 09:44:14 +0200 Subject: [PATCH 04/14] There's no reason to assume this would fail. --- src/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index c2c7a632..dadf3cef 100644 --- a/src/Client.php +++ b/src/Client.php @@ -64,7 +64,7 @@ public function validate_api_token() { * Don't cache invalid API tokens. */ if ( $is_valid ) { - set_transient( 'plausible_analytics_valid_token', [ $token => true ], 86400 ); + set_transient( 'plausible_analytics_valid_token', [ $token => true ], 86400 ); // @codeCoverageIgnore } return $is_valid; From 845b9ae6c039f07891b4394027889d2d1a875743 Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Fri, 7 Jun 2024 10:32:19 +0200 Subject: [PATCH 05/14] Added: plausible_analytics_integrations_* filters. --- src/Integrations.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Integrations.php b/src/Integrations.php index feab82fb..97d22bd3 100644 --- a/src/Integrations.php +++ b/src/Integrations.php @@ -46,7 +46,7 @@ private function init() { * @return bool */ public static function is_wc_active() { - return function_exists( 'WC' ); + return apply_filters( 'plausible_analytics_integrations_woocommerce', function_exists( 'WC' ) ); } /** @@ -55,6 +55,6 @@ public static function is_wc_active() { * @return bool */ public static function is_edd_active() { - return function_exists( 'EDD' ); + return apply_filters( 'plausible_analytics_integrations_edd', function_exists( 'EDD' ) ); } } From 0890bdd45d8942242f342fa112f170a686166faf Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Fri, 7 Jun 2024 10:36:20 +0200 Subject: [PATCH 06/14] Added: testGetPostSettings and expanded testGetFilename --- tests/integration/HelpersTest.php | 47 ++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/tests/integration/HelpersTest.php b/tests/integration/HelpersTest.php index a51ffeab..69d3dcba 100644 --- a/tests/integration/HelpersTest.php +++ b/tests/integration/HelpersTest.php @@ -5,6 +5,7 @@ namespace Plausible\Analytics\Tests\Integration; +use Exception; use Plausible\Analytics\Tests\TestCase; use Plausible\Analytics\WP\Helpers; @@ -49,6 +50,7 @@ public function enableSelfHostedDomain( $settings ) { /** * @see Helpers::get_filename() + * @throws Exception */ public function testGetFilename() { add_filter( 'plausible_analytics_settings', [ $this, 'addExcludedPages' ] ); @@ -74,6 +76,16 @@ public function testGetFilename() { remove_filter( 'plausible_analytics_settings', [ $this, 'enableOutboundLinks' ] ); $this->assertEquals( 'plausible.outbound-links', $filename ); + + add_filter( 'plausible_analytics_settings', [ $this, 'enableRevenue' ] ); + add_filter( 'plausible_analytics_integrations_woocommerce', '__return_true' ); + + $filename = Helpers::get_filename(); + + remove_filter( 'plausible_analytics_settings', [ $this, 'enableRevenue' ] ); + remove_filter( 'plausible_analytics_integrations_woocommerce', '__return_true' ); + + $this->assertEquals( 'plausible.revenue.tagged-events', $filename ); } /** @@ -102,10 +114,37 @@ public function enableOutboundLinks( $settings ) { return $settings; } + /** + * Enable Enhanced Measurements > Custom Events (Tagged Events) + * + * @param $settings + * + * @return mixed + */ + public function enableRevenue( $settings ) { + $settings[ 'enhanced_measurements' ] = [ 'revenue' ]; + + return $settings; + } + + /** + * @see Helpers::get_settings() + * + * @return void + */ + public function testGetPostSettings() { + $_POST[ 'action' ] = 'plausible_analytics_save_options'; + $_POST[ 'options' ] = wp_json_encode( [ [ 'name' => 'test', 'value' => 'test' ] ] ); + + $settings = Helpers::get_settings(); + + $this->assertArrayHasKey( 'test', $settings ); + } + /** * @see Helpers::get_proxy_resource() * @return void - * @throws \Exception + * @throws Exception */ public function testGetProxyResource() { $namespace = Helpers::get_proxy_resource( 'namespace' ); @@ -149,7 +188,7 @@ public function testUpdateSetting() { /** * @see Helpers::get_js_path() * @return void - * @throws \Exception + * @throws Exception */ public function testGetJsPath() { add_filter( 'plausible_analytics_settings', [ $this, 'enableProxy' ] ); @@ -166,7 +205,7 @@ public function testGetJsPath() { /** * @see Helpers::download_file() * @return void - * @throws \Exception + * @throws Exception */ public function testDownloadFile() { Helpers::download_file( 'https://plausible.io/js/plausible.js', wp_get_upload_dir()[ 'basedir' ] . '/test.js' ); @@ -221,7 +260,7 @@ public function testGetDataApiUrl() { /** * @see Helpers::get_rest_endpoint() * @return void - * @throws \Exception + * @throws Exception */ public function testGetRestEndpoint() { $endpoint = Helpers::get_rest_endpoint( false ); From 3b493b8223c774afbdf2cd4955f513d7cbd4f12f Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Fri, 7 Jun 2024 10:39:20 +0200 Subject: [PATCH 07/14] Prevent duplicate test values. --- tests/integration/HelpersTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration/HelpersTest.php b/tests/integration/HelpersTest.php index 69d3dcba..9e39c20a 100644 --- a/tests/integration/HelpersTest.php +++ b/tests/integration/HelpersTest.php @@ -134,11 +134,11 @@ public function enableRevenue( $settings ) { */ public function testGetPostSettings() { $_POST[ 'action' ] = 'plausible_analytics_save_options'; - $_POST[ 'options' ] = wp_json_encode( [ [ 'name' => 'test', 'value' => 'test' ] ] ); + $_POST[ 'options' ] = wp_json_encode( [ [ 'name' => 'post_test', 'value' => 'post_test' ] ] ); $settings = Helpers::get_settings(); - $this->assertArrayHasKey( 'test', $settings ); + $this->assertArrayHasKey( 'post_test', $settings ); } /** From 4bea53484d81ce0b55d801ec4a6c4948a1aa5c52 Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Fri, 7 Jun 2024 11:08:22 +0200 Subject: [PATCH 08/14] Added: testMaybeCreateWooCommerceGoals() --- tests/integration/Admin/ProvisioningTest.php | 68 +++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/tests/integration/Admin/ProvisioningTest.php b/tests/integration/Admin/ProvisioningTest.php index 02f226a2..2a70b078 100644 --- a/tests/integration/Admin/ProvisioningTest.php +++ b/tests/integration/Admin/ProvisioningTest.php @@ -12,6 +12,7 @@ use Plausible\Analytics\WP\Client\Model\Goal; use Plausible\Analytics\WP\Client\Model\GoalPageviewAllOfGoal; use Plausible\Analytics\WP\Helpers; +use function Brain\Monkey\Functions\when; class ProvisioningTest extends TestCase { /** @@ -49,7 +50,6 @@ public function testCreateSharedLink() { * @throws ApiException */ public function testCreateGoals() { - $settings = []; $settings[ 'enhanced_measurements' ] = [ '404', 'outbound-links', @@ -92,5 +92,71 @@ public function testCreateGoals() { $this->assertArrayHasKey( 111, $goal_ids ); $this->assertArrayHasKey( 222, $goal_ids ); $this->assertArrayHasKey( 333, $goal_ids ); + + delete_option( 'plausible_analytics_enhanced_measurements_goal_ids' ); + } + + /** + * @see Provisioning::maybe_create_woocommerce_goals() + * @return void + * @throws ApiException + */ + public function testCreateWooCommerceGoals() { + $settings = [ + 'enhanced_measurements' => [ + 'revenue', + ], + ]; + $mock = $this->getMockBuilder( Client::class )->onlyMethods( [ 'create_goals' ] )->getMock(); + $goals_array = [ + new Goal( + [ + 'goal' => new GoalPageviewAllOfGoal( [ 'display_name' => 'Add Item To Cart', 'id' => 112, 'path' => null ] ), + 'goal_type' => 'Goal.CustomEvent', + ] + ), + new Goal( + [ + 'goal' => new GoalPageviewAllOfGoal( [ 'display_name' => 'Remove Cart Item', 'id' => 223, 'path' => null ] ), + 'goal_type' => 'Goal.CustomEvent', + ] + ), + new Goal( + [ + 'goal' => new GoalPageviewAllOfGoal( [ 'display_name' => 'Entered Checkout', 'id' => 334, 'path' => null ] ), + 'goal_type' => 'Goal.CustomEvent', + ] + ), + new Goal( + [ + 'goal' => new GoalPageviewAllOfGoal( [ 'display_name' => 'Purchase', 'id' => 445, 'path' => null ] ), + 'goal_type' => 'Goal.Revenue', + ] + ), + ]; + $goals = new Client\Model\GoalListResponse(); + + $goals->setGoals( $goals_array ); + $goals->setMeta( new Client\Model\GoalListResponseMeta() ); + $mock->method( 'create_goals' )->willReturn( $goals ); + + $class = new Provisioning( $mock ); + + add_filter( 'plausible_analytics_integrations_woocommerce', '__return_true' ); + when( 'get_woocommerce_currency' )->justReturn( 'EUR' ); + + $class->maybe_create_woocommerce_goals( [], $settings ); + + remove_filter( 'plausible_analytics_integrations_woocommerce', '__return_true' ); + + $goal_ids = get_option( 'plausible_analytics_enhanced_measurements_goal_ids' ); + + $this->assertCount( 4, $goal_ids ); + $this->assertArrayHasKey( 112, $goal_ids ); + $this->assertArrayHasKey( 223, $goal_ids ); + $this->assertArrayHasKey( 334, $goal_ids ); + $this->assertArrayHasKey( 445, $goal_ids ); + + delete_option( 'plausible_analytics_enhanced_measurements_goal_ids' ); } } From e998df2a68d43c79f3a6a1239bb838cddf556e8f Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Fri, 7 Jun 2024 12:25:35 +0200 Subject: [PATCH 09/14] Added: WooComerceTest --- src/Integrations/WooCommerce.php | 26 +++++- .../Integrations/WooCommerceTest.php | 81 +++++++++++++++++++ 2 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 tests/integration/Integrations/WooCommerceTest.php diff --git a/src/Integrations/WooCommerce.php b/src/Integrations/WooCommerce.php index fab3f7cb..d8dc0e1d 100644 --- a/src/Integrations/WooCommerce.php +++ b/src/Integrations/WooCommerce.php @@ -42,6 +42,8 @@ class WooCommerce { /** * Build class. + * + * @codeCoverageIgnore */ public function __construct( $init = true ) { $this->event_goals = [ @@ -58,6 +60,8 @@ public function __construct( $init = true ) { * Filter and action hooks. * * @return void + * + * @codeCoverageIgnore */ private function init( $init ) { if ( ! $init ) { @@ -89,7 +93,7 @@ private function init( $init ) { public function add_js() { // Causes errors in checkout and isn't needed either way. if ( is_checkout() ) { - return; + return; // @codeCoverageIgnore } wp_enqueue_script( @@ -108,6 +112,8 @@ public function add_js() { * @param $request * * @return mixed + * + * @codeCoverageIgnore */ public function add_http_referer( $add_to_cart_data, $request ) { $http_referer = $request->get_param( '_wp_http_referer' ); @@ -123,8 +129,9 @@ public function add_http_referer( $add_to_cart_data, $request ) { * A hacky approach (with lack of a proper solution) to make sure Add To Cart events are tracked on simple product pages. Unfortunately, cart * information isn't available this way. * - * * @return void + * + * @codeCoverageIgnore Because we're not testing JS here. */ public function track_add_to_cart_on_product_page() { $product = wc_get_product(); @@ -153,6 +160,8 @@ public function track_add_to_cart_on_product_page() { * @param string|int $product_id ID of the product added to the cart. * * @return void + * + * @codeCoverageIgnore Because this function returns nothing. */ public function track_ajax_add_to_cart( $product_id ) { $product = wc_get_product( $product_id ); @@ -171,6 +180,8 @@ public function track_ajax_add_to_cart( $product_id ) { * @param array $add_to_cart_data Cart data for the product added to the cart, e.g. quantity, variation ID, etc. * * @return void + * + * @codeCoverageIgnore Because this function returns nothing. */ public function track_add_to_cart( $product, $add_to_cart_data ) { $product_data = $this->clean_data( $product->get_data() ); @@ -199,6 +210,8 @@ public function track_add_to_cart( $product, $add_to_cart_data ) { * @param array $product Product Data. * * @return mixed + * + * @codeCoverageIgnore Because it can't be tested. */ private function clean_data( $product ) { foreach ( $product as $key => $value ) { @@ -217,6 +230,8 @@ private function clean_data( $product ) { * @param WC_Cart $cart Instance of the current cart. * * @return void + * + * @codeCoverageIgnore because we can't test XHR requests here. */ public function track_remove_cart_item( $cart_item_key, $cart ) { $cart_contents = $cart->get_cart_contents(); @@ -240,12 +255,15 @@ public function track_remove_cart_item( $cart_item_key, $cart ) { /** * @return void */ - public function track_entered_checkout() { + public function track_entered_checkout( $cart = null ) { if ( ! is_checkout() ) { return; } - $cart = WC()->cart; + if ( ! $cart ) { + $cart = WC()->cart; + } + $props = apply_filters( 'plausible_analytics_woocommerce_entered_checkout_custom_properties', [ diff --git a/tests/integration/Integrations/WooCommerceTest.php b/tests/integration/Integrations/WooCommerceTest.php new file mode 100644 index 00000000..ca72b456 --- /dev/null +++ b/tests/integration/Integrations/WooCommerceTest.php @@ -0,0 +1,81 @@ + WooCommerce + */ + +namespace Plausible\Analytics\Tests\Integration; + +use Plausible\Analytics\Tests\TestCase; +use Plausible\Analytics\WP\Integrations\WooCommerce; +use function Brain\Monkey\Functions\when; + +class WooCommerceTest extends TestCase { + /** + * @see WooCommerce::add_js() + */ + public function testAddJs() { + when( 'is_checkout' )->justReturn( false ); + + $class = new WooCommerce( false ); + + $class->add_js(); + + $wp_scripts = wp_scripts(); + + $this->assertTrue( in_array( 'plausible-woocommerce-compatibility', $wp_scripts->queue ) ); + } + + /** + * @see WooCommerce::track_entered_checkout() + * @return void + */ + public function testTrackEnteredCheckout() { + when( 'is_checkout' )->justReturn( true ); + + $mock = $this->getMockBuilder( 'WC_Cart' )->setMethods( + [ + 'get_subtotal', + 'get_shipping_total', + 'get_total_tax', + 'get_total', + ] + )->getMock(); + + $mock->method( 'get_subtotal' )->willReturn( 10 ); + $mock->method( 'get_shipping_total' )->willReturn( 5 ); + $mock->method( 'get_total_tax' )->willReturn( 1 ); + $mock->method( 'get_total' )->willReturn( "16.00" ); + + $class = new WooCommerce( false ); + + $this->expectOutputContains( '{"props":{"subtotal":10,"shipping":5,"tax":1,"total":"16.00"}}' ); + + $class->track_entered_checkout( $mock ); + } + + /** + * @see WooCommerce::track_purchase() + * @return void + */ + public function testTrackPurchase() { + $class = new WooCommerce( false ); + $mock = $this->getMockBuilder( 'WC_Order' )->setMethods( + [ + 'get_meta', + 'get_total', + 'get_currency', + 'add_meta_data', + 'save', + ] + )->getMock(); + $mock->method( 'get_meta' )->willReturn( false ); + $mock->method( 'get_total' )->willReturn( 10 ); + $mock->method( 'get_currency' )->willReturn( 'EUR' ); + + when( 'wc_get_order' )->justReturn( $mock ); + + $this->expectOutputContains( '{"revenue":{"amount":"10.00","currency":"EUR"}}' ); + + $class->track_purchase( 1 ); + } +} From bf9a776b94c2b1d1562ece18a4adf17316b03a9d Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Fri, 7 Jun 2024 12:27:56 +0200 Subject: [PATCH 10/14] PHPDoc. --- src/Integrations/WooCommerce.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Integrations/WooCommerce.php b/src/Integrations/WooCommerce.php index d8dc0e1d..9c0fddd0 100644 --- a/src/Integrations/WooCommerce.php +++ b/src/Integrations/WooCommerce.php @@ -161,7 +161,7 @@ public function track_add_to_cart_on_product_page() { * * @return void * - * @codeCoverageIgnore Because this function returns nothing. + * @codeCoverageIgnore Because we can't test XHR requests here. */ public function track_ajax_add_to_cart( $product_id ) { $product = wc_get_product( $product_id ); @@ -181,7 +181,7 @@ public function track_ajax_add_to_cart( $product_id ) { * * @return void * - * @codeCoverageIgnore Because this function returns nothing. + * @codeCoverageIgnore Because we can't test XHR requests here. */ public function track_add_to_cart( $product, $add_to_cart_data ) { $product_data = $this->clean_data( $product->get_data() ); From 913ffa83c7659de6ba7d6a21d382c65a7c5454cf Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Fri, 7 Jun 2024 12:28:19 +0200 Subject: [PATCH 11/14] PHPDoc. --- src/Integrations/WooCommerce.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Integrations/WooCommerce.php b/src/Integrations/WooCommerce.php index 9c0fddd0..309a1bd0 100644 --- a/src/Integrations/WooCommerce.php +++ b/src/Integrations/WooCommerce.php @@ -113,7 +113,7 @@ public function add_js() { * * @return mixed * - * @codeCoverageIgnore + * @codeCoverageIgnore Because there's nothing to test here. */ public function add_http_referer( $add_to_cart_data, $request ) { $http_referer = $request->get_param( '_wp_http_referer' ); From 0197831ad5b231552e7e2c2cf16953f0fd12c284 Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Fri, 7 Jun 2024 12:31:13 +0200 Subject: [PATCH 12/14] Ignore AddJS --- src/Integrations/WooCommerce.php | 2 ++ .../integration/Integrations/WooCommerceTest.php | 15 --------------- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/src/Integrations/WooCommerce.php b/src/Integrations/WooCommerce.php index 309a1bd0..4146d3fd 100644 --- a/src/Integrations/WooCommerce.php +++ b/src/Integrations/WooCommerce.php @@ -89,6 +89,8 @@ private function init( $init ) { * Enqueue required JS in frontend. * * @return void + * + * @codeCoverageIgnore Because there's nothing to test here. */ public function add_js() { // Causes errors in checkout and isn't needed either way. diff --git a/tests/integration/Integrations/WooCommerceTest.php b/tests/integration/Integrations/WooCommerceTest.php index ca72b456..5666ec70 100644 --- a/tests/integration/Integrations/WooCommerceTest.php +++ b/tests/integration/Integrations/WooCommerceTest.php @@ -10,21 +10,6 @@ use function Brain\Monkey\Functions\when; class WooCommerceTest extends TestCase { - /** - * @see WooCommerce::add_js() - */ - public function testAddJs() { - when( 'is_checkout' )->justReturn( false ); - - $class = new WooCommerce( false ); - - $class->add_js(); - - $wp_scripts = wp_scripts(); - - $this->assertTrue( in_array( 'plausible-woocommerce-compatibility', $wp_scripts->queue ) ); - } - /** * @see WooCommerce::track_entered_checkout() * @return void From 698b5927126855934a424b726857d0868fb97b0c Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Fri, 7 Jun 2024 12:35:32 +0200 Subject: [PATCH 13/14] No need to modify source code. --- src/Integrations/WooCommerce.php | 6 ++---- tests/integration/Integrations/WooCommerceTest.php | 6 +++++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Integrations/WooCommerce.php b/src/Integrations/WooCommerce.php index 4146d3fd..d0b5c6b9 100644 --- a/src/Integrations/WooCommerce.php +++ b/src/Integrations/WooCommerce.php @@ -257,14 +257,12 @@ public function track_remove_cart_item( $cart_item_key, $cart ) { /** * @return void */ - public function track_entered_checkout( $cart = null ) { + public function track_entered_checkout() { if ( ! is_checkout() ) { return; } - if ( ! $cart ) { - $cart = WC()->cart; - } + $cart = WC()->cart; $props = apply_filters( 'plausible_analytics_woocommerce_entered_checkout_custom_properties', diff --git a/tests/integration/Integrations/WooCommerceTest.php b/tests/integration/Integrations/WooCommerceTest.php index 5666ec70..ce59fe0b 100644 --- a/tests/integration/Integrations/WooCommerceTest.php +++ b/tests/integration/Integrations/WooCommerceTest.php @@ -31,11 +31,15 @@ public function testTrackEnteredCheckout() { $mock->method( 'get_total_tax' )->willReturn( 1 ); $mock->method( 'get_total' )->willReturn( "16.00" ); + $woo_mock = $this->getMockBuilder( 'WooCommerce' )->getMock(); + $woo_mock->cart = $mock; + when( 'WC' )->justReturn( $woo_mock ); + $class = new WooCommerce( false ); $this->expectOutputContains( '{"props":{"subtotal":10,"shipping":5,"tax":1,"total":"16.00"}}' ); - $class->track_entered_checkout( $mock ); + $class->track_entered_checkout(); } /** From 98f360f5e9242af9f49e529ef33e64ae4937c28d Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Fri, 7 Jun 2024 12:35:58 +0200 Subject: [PATCH 14/14] Minor re-factor for readability. --- tests/integration/Integrations/WooCommerceTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/integration/Integrations/WooCommerceTest.php b/tests/integration/Integrations/WooCommerceTest.php index ce59fe0b..37e97985 100644 --- a/tests/integration/Integrations/WooCommerceTest.php +++ b/tests/integration/Integrations/WooCommerceTest.php @@ -17,7 +17,7 @@ class WooCommerceTest extends TestCase { public function testTrackEnteredCheckout() { when( 'is_checkout' )->justReturn( true ); - $mock = $this->getMockBuilder( 'WC_Cart' )->setMethods( + $cart_mock = $this->getMockBuilder( 'WC_Cart' )->setMethods( [ 'get_subtotal', 'get_shipping_total', @@ -26,13 +26,13 @@ public function testTrackEnteredCheckout() { ] )->getMock(); - $mock->method( 'get_subtotal' )->willReturn( 10 ); - $mock->method( 'get_shipping_total' )->willReturn( 5 ); - $mock->method( 'get_total_tax' )->willReturn( 1 ); - $mock->method( 'get_total' )->willReturn( "16.00" ); + $cart_mock->method( 'get_subtotal' )->willReturn( 10 ); + $cart_mock->method( 'get_shipping_total' )->willReturn( 5 ); + $cart_mock->method( 'get_total_tax' )->willReturn( 1 ); + $cart_mock->method( 'get_total' )->willReturn( "16.00" ); $woo_mock = $this->getMockBuilder( 'WooCommerce' )->getMock(); - $woo_mock->cart = $mock; + $woo_mock->cart = $cart_mock; when( 'WC' )->justReturn( $woo_mock ); $class = new WooCommerce( false );