' . sprintf( esc_html__( 'For Matomo to track you will need to %1$sadd a Matomo Tag to the container%2$s. It otherwise won\'t track automatically.', 'matomo' ), '', '' ) . '
'; + $container_select .= '%3$s
', $title, esc_attr( $id ), $description ); + public function get_description( $id, $description, $hide_description = false ) { + return sprintf( '%2$s
', esc_attr( $id ), $description ); } /** @@ -62,8 +60,23 @@ public function get_description( $id, $description, $hide_description = true ) { * @param boolean $hide_description $hideDescription set to false to show description initially (default: true) * @param string $on_change javascript for onchange event (default: empty) */ - public function show_checkbox( $id, $name, $description, $is_hidden = false, $group_name = '', $hide_description = true, $on_change = '' ) { - printf( '' . $description[ $key ] . '
'; + } + + $button_list[] = sprintf( + '' + . '' + . $desc, + ( $tooltip ? ' title="' . esc_attr( $tooltip ) . '"' : '' ), + $radio_id, + esc_attr( TrackingSettings::FORM_NAME ) . '[' . esc_attr( $id ) . ']', + esc_attr( $key ), + // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison + ( $key == $default ? 'checked="checked"' : '' ), + ( $disabled ? 'disabled="disabled"' : '' ), + esc_attr( $on_change ), + $radio_id, + esc_html( $label ) + ); + } + } + $script_change = ''; + if ( $on_change ) { + // we make sure it will select the right settings by default + $script_change .= ''; + } + printf( + '+ +
+ ++ +
+ ++ [matomo_opt_out]' ); ?> + ', '' ); ?> +
+ ++ +
++ ', '', '', '' ); ?> +
+ is_tracking_enabled() ) { ?> -:
- -
- Matomo will always cost you nothing to use, but that doesn't mean it costs us nothing to make.
- Matomo needs your continued support to grow and thrive.
',
- '',
+ esc_html__( 'Matomo will always cost you nothing to use, but if you\'d like to support Matomo in a more meaningful way, take a look at our %1$spremium plugins%2$s.', 'matomo' ),
'',
''
);
?>
- Every penny will help.
', - '', - '', - '' - ); + require_once 'info_matomo_desc.php'; ?>
- ', '' ); ?> + ', '' ); ?>
- '; ?> - - '; ?> + '; ?> +foobarbaz
' ); - $this->assertStringStartsWith( 'foobarbaz
ID . '&action_name=hello-world&urlref=http%3A%2F%2Fexample.org', $url ); $this->assertStringEndsWith( '" style="border:0;width:0;height:0" width="0" height="0" alt="" />', $url ); } diff --git a/tests/phpunit/wpmatomo/trackingcode/test-generatoroptions.php b/tests/phpunit/wpmatomo/trackingcode/test-generatoroptions.php new file mode 100644 index 000000000..aa1a57ca8 --- /dev/null +++ b/tests/phpunit/wpmatomo/trackingcode/test-generatoroptions.php @@ -0,0 +1,230 @@ +assertEquals( $settings->default_global_settings['track_api_endpoint'], $options->get_track_api_endpoint() ); + $this->assertEquals( $settings->default_global_settings['force_protocol'], $options->get_force_protocol() ); + $this->assertEquals( $settings->default_global_settings['track_js_endpoint'], $options->get_track_js_endpoint() ); + $this->assertEquals( $settings->default_global_settings['set_download_extensions'], $options->get_set_download_extensions() ); + $this->assertEquals( $settings->default_global_settings['add_download_extensions'], $options->get_add_download_extensions() ); + $this->assertEquals( $settings->default_global_settings['set_download_classes'], $options->get_set_download_classes() ); + $this->assertEquals( $settings->default_global_settings['set_link_classes'], $options->get_set_link_classes() ); + $this->assertEquals( $settings->default_global_settings['disable_cookies'], $options->get_disable_cookies() ); + $this->assertEquals( $settings->default_global_settings['track_crossdomain_linking'], $options->get_track_crossdomain_linking() ); + $this->assertEquals( $settings->default_global_settings['track_jserrors'], $options->get_track_jserrors() ); + $this->assertEquals( $settings->default_global_settings['track_across'], $options->get_track_across() ); + $this->assertEquals( $settings->default_global_settings['track_across_alias'], $options->get_track_across_alias() ); + $this->assertEquals( $settings->default_global_settings['force_post'], $options->get_force_post() ); + $this->assertEquals( $settings->default_global_settings['cookie_consent'], $options->get_cookie_consent() ); + $this->assertEquals( $settings->default_global_settings['limit_cookies'], $options->get_limit_cookies() ); + $this->assertEquals( $settings->default_global_settings['limit_cookies_visitor'], $options->get_limit_cookies_visitor() ); + $this->assertEquals( $settings->default_global_settings['limit_cookies_referral'], $options->get_limit_cookies_referral() ); + $this->assertEquals( $settings->default_global_settings['limit_cookies_session'], $options->get_limit_cookies_session() ); + $this->assertEquals( $settings->default_global_settings['track_datacfasync'], $options->get_track_datacfasync() ); + $this->assertEquals( $settings->default_global_settings['track_heartbeat'], $options->get_track_heartbeat() ); + $this->assertEquals( $settings->default_global_settings['track_content'], $options->get_track_content() ); + } + + public function test_construct_with_settings_missing_values() { + $settings = new \WpMatomo\Settings(); + $settings->set_global_option( 'track_api_endpoint', 'test value 1' ); + $settings->set_global_option( 'force_protocol', 'test value 2' ); + $settings->set_global_option( 'track_js_endpoint', 'test value 3' ); + $settings->set_global_option( 'set_download_extensions', 'test value 4' ); + $settings->set_global_option( 'add_download_extensions', 'test value 5' ); + $settings->set_global_option( 'set_download_classes', 'test value 6' ); + $settings->set_global_option( 'set_link_classes', 'test value 7' ); + $settings->set_global_option( 'track_crossdomain_linking', 'test value 9' ); + $settings->set_global_option( 'track_jserrors', 'test value 10' ); + $settings->set_global_option( 'track_across', 'test value 11' ); + $settings->set_global_option( 'force_post', 'test value 13' ); + $settings->set_global_option( 'cookie_consent', 'test value 14' ); + $settings->set_global_option( 'limit_cookies', 'test value 15' ); + $settings->set_global_option( 'limit_cookies_visitor', 16 ); + $settings->set_global_option( 'limit_cookies_referral', 17 ); + $settings->set_global_option( 'limit_cookies_session', 18 ); + $settings->set_global_option( 'track_heartbeat', 20 ); + $settings->set_global_option( 'track_content', 'test value 21' ); + + $options = new GeneratorOptions( $settings ); + + $this->assertEquals( 'test value 1', $options->get_track_api_endpoint() ); + $this->assertEquals( 'test value 2', $options->get_force_protocol() ); + $this->assertEquals( 'test value 3', $options->get_track_js_endpoint() ); + $this->assertEquals( 'test value 4', $options->get_set_download_extensions() ); + $this->assertEquals( 'test value 5', $options->get_add_download_extensions() ); + $this->assertEquals( 'test value 6', $options->get_set_download_classes() ); + $this->assertEquals( 'test value 7', $options->get_set_link_classes() ); + $this->assertEquals( null, $options->get_disable_cookies() ); + $this->assertEquals( 'test value 9', $options->get_track_crossdomain_linking() ); + $this->assertEquals( 'test value 10', $options->get_track_jserrors() ); + $this->assertEquals( 'test value 11', $options->get_track_across() ); + $this->assertEquals( null, $options->get_track_across_alias() ); + $this->assertEquals( 'test value 13', $options->get_force_post() ); + $this->assertEquals( 'test value 14', $options->get_cookie_consent() ); + $this->assertEquals( 'test value 15', $options->get_limit_cookies() ); + $this->assertEquals( 16, $options->get_limit_cookies_visitor() ); + $this->assertEquals( 17, $options->get_limit_cookies_referral() ); + $this->assertEquals( 18, $options->get_limit_cookies_session() ); + $this->assertEquals( null, $options->get_track_datacfasync() ); + $this->assertEquals( 20, $options->get_track_heartbeat() ); + $this->assertEquals( 'test value 21', $options->get_track_content() ); + } + + public function test_construct_with_settings() { + $settings = new \WpMatomo\Settings(); + $settings->set_global_option( 'track_api_endpoint', 'test value 1' ); + $settings->set_global_option( 'force_protocol', 'test value 2' ); + $settings->set_global_option( 'track_js_endpoint', 'test value 3' ); + $settings->set_global_option( 'set_download_extensions', 'test value 4' ); + $settings->set_global_option( 'add_download_extensions', 'test value 5' ); + $settings->set_global_option( 'set_download_classes', 'test value 6' ); + $settings->set_global_option( 'set_link_classes', 'test value 7' ); + $settings->set_global_option( 'disable_cookies', 'test value 8' ); + $settings->set_global_option( 'track_crossdomain_linking', 'test value 9' ); + $settings->set_global_option( 'track_jserrors', 'test value 10' ); + $settings->set_global_option( 'track_across', 'test value 11' ); + $settings->set_global_option( 'track_across_alias', 'test value 12' ); + $settings->set_global_option( 'force_post', 'test value 13' ); + $settings->set_global_option( 'cookie_consent', 'test value 14' ); + $settings->set_global_option( 'limit_cookies', 'test value 15' ); + $settings->set_global_option( 'limit_cookies_visitor', 16 ); + $settings->set_global_option( 'limit_cookies_referral', 17 ); + $settings->set_global_option( 'limit_cookies_session', 18 ); + $settings->set_global_option( 'track_datacfasync', 'test value 19' ); + $settings->set_global_option( 'track_heartbeat', 20 ); + $settings->set_global_option( 'track_content', 'test value 21' ); + + $options = new GeneratorOptions( $settings ); + + $this->assertEquals( 'test value 1', $options->get_track_api_endpoint() ); + $this->assertEquals( 'test value 2', $options->get_force_protocol() ); + $this->assertEquals( 'test value 3', $options->get_track_js_endpoint() ); + $this->assertEquals( 'test value 4', $options->get_set_download_extensions() ); + $this->assertEquals( 'test value 5', $options->get_add_download_extensions() ); + $this->assertEquals( 'test value 6', $options->get_set_download_classes() ); + $this->assertEquals( 'test value 7', $options->get_set_link_classes() ); + $this->assertEquals( 'test value 8', $options->get_disable_cookies() ); + $this->assertEquals( 'test value 9', $options->get_track_crossdomain_linking() ); + $this->assertEquals( 'test value 10', $options->get_track_jserrors() ); + $this->assertEquals( 'test value 11', $options->get_track_across() ); + $this->assertEquals( 'test value 12', $options->get_track_across_alias() ); + $this->assertEquals( 'test value 13', $options->get_force_post() ); + $this->assertEquals( 'test value 14', $options->get_cookie_consent() ); + $this->assertEquals( 'test value 15', $options->get_limit_cookies() ); + $this->assertEquals( 16, $options->get_limit_cookies_visitor() ); + $this->assertEquals( 17, $options->get_limit_cookies_referral() ); + $this->assertEquals( 18, $options->get_limit_cookies_session() ); + $this->assertEquals( 'test value 19', $options->get_track_datacfasync() ); + $this->assertEquals( 20, $options->get_track_heartbeat() ); + $this->assertEquals( 'test value 21', $options->get_track_content() ); + } + + public function test_construct_with_request_missing_values() { + $settings = new \WpMatomo\Settings(); + $request = [ + 'track_api_endpoint' => 'test value 1', + 'force_protocol' => 'test value 2', + 'track_js_endpoint' => 'test value 3', + 'set_download_extensions' => 'test value 4', + 'add_download_extensions' => 'test value 5', + 'set_download_classes' => 'test value 6', + 'set_link_classes' => 'test value 7', + 'track_crossdomain_linking' => 'test value 9', + 'track_jserrors' => 'test value 10', + 'track_across' => 'test value 11', + 'track_across_alias' => 'test value 12', + 'force_post' => 'test value 13', + 'cookie_consent' => 'test value 14', + 'limit_cookies' => 'test value 15', + 'limit_cookies_referral' => 'test value 17', + 'limit_cookies_session' => 'test value 18', + 'track_heartbeat' => 'test value 19', + 'track_content' => 'test value 21', + ]; + + $options = new GeneratorOptions( $settings, $request ); + + $this->assertEquals( 'test value 1', $options->get_track_api_endpoint() ); + $this->assertEquals( 'test value 2', $options->get_force_protocol() ); + $this->assertEquals( 'test value 3', $options->get_track_js_endpoint() ); + $this->assertEquals( 'test value 4', $options->get_set_download_extensions() ); + $this->assertEquals( 'test value 5', $options->get_add_download_extensions() ); + $this->assertEquals( 'test value 6', $options->get_set_download_classes() ); + $this->assertEquals( 'test value 7', $options->get_set_link_classes() ); + $this->assertEquals( null, $options->get_disable_cookies() ); + $this->assertEquals( 'test value 9', $options->get_track_crossdomain_linking() ); + $this->assertEquals( 'test value 10', $options->get_track_jserrors() ); + $this->assertEquals( 'test value 11', $options->get_track_across() ); + $this->assertEquals( 'test value 12', $options->get_track_across_alias() ); + $this->assertEquals( 'test value 13', $options->get_force_post() ); + $this->assertEquals( 'test value 14', $options->get_cookie_consent() ); + $this->assertEquals( 'test value 15', $options->get_limit_cookies() ); + $this->assertEquals( 34186669, $options->get_limit_cookies_visitor() ); + $this->assertEquals( 'test value 17', $options->get_limit_cookies_referral() ); + $this->assertEquals( 'test value 18', $options->get_limit_cookies_session() ); + $this->assertEquals( 'test value 19', $options->get_track_heartbeat() ); + $this->assertEquals( null, $options->get_track_datacfasync() ); + $this->assertEquals( 'test value 21', $options->get_track_content() ); + } + + public function test_construct_with_full_request() { + $settings = new \WpMatomo\Settings(); + $request = [ + 'track_api_endpoint' => 'test value 1', + 'force_protocol' => 'test value 2', + 'track_js_endpoint' => 'test value 3', + 'set_download_extensions' => 'test value 4', + 'add_download_extensions' => 'test value 5', + 'set_download_classes' => 'test value 6', + 'set_link_classes' => 'test value 7', + 'disable_cookies' => 'test value 8', + 'track_crossdomain_linking' => 'test value 9', + 'track_jserrors' => 'test value 10', + 'track_across' => 'test value 11', + 'track_across_alias' => 'test value 12', + 'force_post' => 'test value 13', + 'cookie_consent' => 'test value 14', + 'limit_cookies' => 'test value 15', + 'limit_cookies_visitor' => 'test value 16', + 'limit_cookies_referral' => 'test value 17', + 'limit_cookies_session' => 'test value 18', + 'track_heartbeat' => 'test value 19', + 'track_datacfasync' => 'test value 20', + 'track_content' => 'test value 21', + ]; + + $options = new GeneratorOptions( $settings, $request ); + + $this->assertEquals( 'test value 1', $options->get_track_api_endpoint() ); + $this->assertEquals( 'test value 2', $options->get_force_protocol() ); + $this->assertEquals( 'test value 3', $options->get_track_js_endpoint() ); + $this->assertEquals( 'test value 4', $options->get_set_download_extensions() ); + $this->assertEquals( 'test value 5', $options->get_add_download_extensions() ); + $this->assertEquals( 'test value 6', $options->get_set_download_classes() ); + $this->assertEquals( 'test value 7', $options->get_set_link_classes() ); + $this->assertEquals( 'test value 8', $options->get_disable_cookies() ); + $this->assertEquals( 'test value 9', $options->get_track_crossdomain_linking() ); + $this->assertEquals( 'test value 10', $options->get_track_jserrors() ); + $this->assertEquals( 'test value 11', $options->get_track_across() ); + $this->assertEquals( 'test value 12', $options->get_track_across_alias() ); + $this->assertEquals( 'test value 13', $options->get_force_post() ); + $this->assertEquals( 'test value 14', $options->get_cookie_consent() ); + $this->assertEquals( 'test value 15', $options->get_limit_cookies() ); + $this->assertEquals( 'test value 16', $options->get_limit_cookies_visitor() ); + $this->assertEquals( 'test value 17', $options->get_limit_cookies_referral() ); + $this->assertEquals( 'test value 18', $options->get_limit_cookies_session() ); + $this->assertEquals( 'test value 19', $options->get_track_heartbeat() ); + $this->assertEquals( 'test value 20', $options->get_track_datacfasync() ); + $this->assertEquals( 'test value 21', $options->get_track_content() ); + } +} diff --git a/tests/phpunit/wpmatomo/trackingcode/test-trackingcodegenerator.php b/tests/phpunit/wpmatomo/trackingcode/test-trackingcodegenerator.php index 580bb4d85..505ca90e8 100644 --- a/tests/phpunit/wpmatomo/trackingcode/test-trackingcodegenerator.php +++ b/tests/phpunit/wpmatomo/trackingcode/test-trackingcodegenerator.php @@ -3,12 +3,10 @@ * @package matomo */ -use WpMatomo\Access; use WpMatomo\Admin\TrackingSettings; use WpMatomo\Admin\CookieConsent; -use WpMatomo\Capabilities; -use WpMatomo\Roles; use WpMatomo\Settings; +use WpMatomo\TrackingCode\GeneratorOptions; use WpMatomo\TrackingCode\TrackingCodeGenerator; class TrackingCodeGeneratorTest extends MatomoUnit_TestCase { @@ -31,7 +29,7 @@ public function setUp(): void { } private function make_tracking_code() { - $this->tracking_code = new TrackingCodeGenerator( $this->settings ); + $this->tracking_code = new TrackingCodeGenerator( $this->settings, new GeneratorOptions( $this->settings ) ); } private function get_tracking_code() { @@ -280,4 +278,44 @@ public function test_cookie_consent_tracking() { ); $this->assertStringContainsString( "_paq.push(['requireConsent']);", $this->get_tracking_code() ); } + + public function test_get_tracking_cookie_domain_no_cookie_domain() { + $this->make_tracking_code(); + $this->assertSame( '', $this->tracking_code->get_tracking_cookie_domain() ); + } + + public function test_get_tracking_cookie_domain_returns_cookie_domain() { + $this->make_tracking_code(); + $this->settings->set_global_option( 'track_across', true ); + $this->assertSame( '*.example.org', $this->tracking_code->get_tracking_cookie_domain() ); + } + + public function test_prepare_tracking_code_when_using_options_from_request() { + $cdata_start = "/* */\n"; + if ( getenv( 'WORDPRESS_VERSION' ) && ( getenv( 'WORDPRESS_VERSION' ) !== 'latest' && version_compare( getenv( 'WORDPRESS_VERSION' ), '6.4', '<' ) ) ) { + $cdata_start = ''; + $cdata_end = ''; + } + + $request = [ + 'track_mode' => TrackingSettings::TRACK_MODE_DEFAULT, + 'force_post' => true, + 'track_heartbeat' => 72, + ]; + + $settings = new Settings(); + $generator = new TrackingCodeGenerator( $settings, new GeneratorOptions( $settings, $request ) ); + + $this->assertSame( + [ + 'script' => '\n", + 'noscript' => '', + ], + $generator->prepare_tracking_code( 1 ) + ); + } } diff --git a/tests/phpunit/wpmatomo/wpstatistics/test-import.php b/tests/phpunit/wpmatomo/wpstatistics/test-import.php index fd8802120..32d0827bb 100644 --- a/tests/phpunit/wpmatomo/wpstatistics/test-import.php +++ b/tests/phpunit/wpmatomo/wpstatistics/test-import.php @@ -38,7 +38,13 @@ public function manually_load_plugin() { $file = $this->plugin_file(); if ( file_exists( $file ) ) { require_once $file; - $GLOBALS['WP_Statistics']->includes(); + + $wp_statistics = $GLOBALS['WP_Statistics']; + if ( method_exists( $wp_statistics, 'plugin_setup' ) ) { + $wp_statistics->plugin_setup(); + } else { + $wp_statistics->includes(); + } } } @@ -83,11 +89,23 @@ public function setUp(): void { * Download the geoip database for the GeoIP2 client * * @return void - * @throws Exception In case there is an error while downloading the geoip database. + * @throws \Exception In case there is an error while downloading the geoip database. */ private function download_geoip() { + // wpstatistics fails to download geoip during tests, so we do it ourselves and link it into the wpstatistics directory + $wp_statistics_geoip_url = 'https://cdn.jsdelivr.net/npm/geolite2-city/GeoLite2-City.mmdb.gz'; + $schedule_task = new ScheduledTasks( new Settings() ); - $schedule_task->update_geo_ip2_db(); + $schedule_task->update_geo_ip2_db( $wp_statistics_geoip_url ); + + $expected_path = ABSPATH . '/wp-content/uploads/matomo/GeoIP2-City.mmdb'; + if ( ! is_file( $expected_path ) ) { + throw new \Exception( 'failed to download geoip database. contents of upload directory: ' . var_export( scandir( dirname( $expected_path ) ), true ) ); + } + + $wpstats_database_path = ABSPATH . '/wp-content/uploads/wp-statistics/GeoLite2-City.mmdb'; + mkdir( dirname( $wpstats_database_path ), 0777, true ); + symlink( $expected_path, $wpstats_database_path ); } public function test_countries_found() { @@ -197,10 +215,8 @@ public function test_pages_found() { return; } - $possible_values = [ 156, 81, 77, 90, 91 ]; - $report = $this->fetch_report( 'Actions', 'getPageUrls' ); - $this->assertContains( $report['reportData']->getRowsCount(), $possible_values ); + $this->assertGreaterThan( 75, $report['reportData']->getRowsCount() ); } protected function fetch_report( $report_name, $method ) {