Skip to content

Commit

Permalink
Merge pull request #198 from plausible/automated_testing
Browse files Browse the repository at this point in the history
Automated Testing
  • Loading branch information
Dan0sz authored Apr 7, 2024
2 parents 6d27d7d + 37bb52c commit 18bc986
Show file tree
Hide file tree
Showing 17 changed files with 271 additions and 66 deletions.
8 changes: 7 additions & 1 deletion phpunit-with-integration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
failOnWarning="true"
verbose="true">
<php>
<const name="DAAN_DOING_TESTS" value="true"/>
<const name="DOING_TESTS" value="true"/>
</php>
<testsuites>
<testsuite name="default">
Expand All @@ -27,7 +27,13 @@
</include>
<exclude>
<directory suffix=".php">src/Client</directory>
<directory suffix=".php">src/Admin/Settings</directory>
<file>src/Actions.php</file>
<file>src/Cron.php</file>
<file>src/Uninstall.php</file>
<file>src/Admin/Actions.php</file>
<file>src/Admin/Filters.php</file>
<file>src/Admin/Messages.php</file>
</exclude>
</coverage>
</phpunit>
22 changes: 13 additions & 9 deletions src/Admin/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ public function maybe_install_module( $old_settings, $settings ) {
*/
public function install() {
if ( ! current_user_can( 'install_plugins' ) ) {
return;
return; // @codeCoverageIgnore
}

if ( ! function_exists( 'WP_Filesystem' ) ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' ); // @codeCoverageIgnore
}

WP_Filesystem();
Expand All @@ -74,13 +74,13 @@ public function install() {
}

if ( ! is_dir( WPMU_PLUGIN_DIR ) ) {
$this->show_module_not_installed_error();
$this->show_module_not_installed_error(); // @codeCoverageIgnore
}

$results = copy_dir( PLAUSIBLE_ANALYTICS_PLUGIN_DIR . 'mu-plugin', WPMU_PLUGIN_DIR );

if ( is_wp_error( $results ) ) {
$this->show_module_not_installed_error();
$this->show_module_not_installed_error(); // @codeCoverageIgnore
}

add_option( 'plausible_analytics_proxy_speed_module_installed', true );
Expand All @@ -89,6 +89,8 @@ public function install() {
/**
* @since 1.3.0
* @return void
*
* @codeCoverageIgnore
*/
private function show_module_not_installed_error() {
$message = sprintf(
Expand All @@ -114,7 +116,7 @@ private function show_module_not_installed_error() {
*/
public function uninstall() {
if ( ! current_user_can( 'install_plugins' ) ) {
return;
return; // @codeCoverageIgnore
}

/**
Expand All @@ -137,7 +139,7 @@ public function uninstall() {
$js_file = Helpers::get_proxy_resource( 'file_alias' );

if ( file_exists( $cache_dir . $js_file . '.js' ) ) {
unlink( $cache_dir . $js_file . '.js' );
unlink( $cache_dir . $js_file . '.js' ); // @codeCoverageIgnore
}

if ( $this->dir_is_empty( $cache_dir ) ) {
Expand Down Expand Up @@ -205,6 +207,7 @@ public function maybe_enable_proxy( $settings, $old_settings ) {
$test_succeeded = $this->test_proxy( Helpers::proxy_enabled( $settings ) && wp_doing_ajax() );

if ( ! $test_succeeded ) {
// @codeCoverageIgnoreStart
Messages::set_error(
sprintf(
wp_kses(
Expand All @@ -221,6 +224,7 @@ public function maybe_enable_proxy( $settings, $old_settings ) {

// Disable the proxy.
return $old_settings;
// @codeCoverageIgnoreEnd
}

return $settings;
Expand All @@ -247,7 +251,7 @@ private function is_ssl() {
private function test_proxy( $run = true ) {
// Should we run the test?
if ( ! apply_filters( 'plausible_analytics_module_run_test_proxy', $run ) ) {
return false;
return false; // @codeCoverageIgnore
}

$namespace = Helpers::get_proxy_resource( 'namespace' );
Expand All @@ -267,12 +271,12 @@ private function test_proxy( $run = true ) {
/** @var \WP_REST_Response $result */
try {
$result = rest_do_request( $request );
} catch ( \Exception $e ) {
} catch ( \Exception $e ) { // @codeCoverageIgnore
/**
* There's no need to handle the error, because we don't want to display it anyway.
* We'll leave the parameter for backwards compatibility.
*/
return false;
return false; // @codeCoverageIgnore
}

return wp_remote_retrieve_response_code( $result->get_data() ) === 202;
Expand Down
37 changes: 27 additions & 10 deletions src/Admin/Provisioning.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ class Provisioning {

/**
* Build class.
*
* @param bool|Client $client Allows for mocking during CI.
*
* @throws ApiException
* @codeCoverageIgnore
*/
public function __construct() {
public function __construct( $client = null ) {
/**
* cURL or allow_url_fopen ini setting is required for GuzzleHttp to function properly.
*/
Expand All @@ -50,7 +55,11 @@ public function __construct() {
return;
}

$this->client = new Client();
$this->client = $client;

if ( ! $this->client ) {
$this->client = new Client();
}

$this->init();
}
Expand All @@ -60,10 +69,12 @@ public function __construct() {
*
* @return void
* @throws ApiException
*
* @codeCoverageIgnore
*/
private function init() {
if ( ! $this->client->validate_api_token() ) {
return;
return; // @codeCoverageIgnore
}

add_action( 'update_option_plausible_analytics_settings', [ $this, 'create_shared_link' ], 10, 2 );
Expand All @@ -76,6 +87,8 @@ private function init() {
* Show an error on the settings screen if cURL isn't enabled on this machine.
*
* @return void
*
* @codeCoverageIgnore
*/
public function add_curl_error() {
Messages::set_error(
Expand All @@ -94,7 +107,7 @@ public function add_curl_error() {
*/
public function create_shared_link( $old_settings, $settings ) {
if ( empty( $settings[ 'enable_analytics_dashboard' ] ) ) {
return;
return; // @codeCoverageIgnore
}

$this->client->create_shared_link();
Expand All @@ -110,7 +123,7 @@ public function create_goals( $old_settings, $settings ) {
$enhanced_measurements = array_filter( $settings[ 'enhanced_measurements' ] );

if ( empty( $enhanced_measurements ) ) {
return;
return; // @codeCoverageIgnore
}

$custom_event_keys = array_keys( $this->custom_event_goals );
Expand All @@ -119,7 +132,7 @@ public function create_goals( $old_settings, $settings ) {

foreach ( $enhanced_measurements as $measurement ) {
if ( ! in_array( $measurement, $custom_event_keys ) ) {
continue;
continue; // @codeCoverageIgnore
}

$goals[] = new Client\Model\GoalCreateRequestCustomEvent(
Expand All @@ -133,15 +146,15 @@ public function create_goals( $old_settings, $settings ) {
}

if ( empty( $goals ) ) {
return;
return; // @codeCoverageIgnore
}

$create_request->setGoals( $goals );
$response = $this->client->create_goals( $create_request );

if ( $response->valid() ) {
$goals = $response->getGoals();
$ids = get_option( 'plausible_analytics_enhanced_measurements_goal_ids' );
$ids = get_option( 'plausible_analytics_enhanced_measurements_goal_ids', [] );

foreach ( $goals as $goal ) {
$goal = $goal->getGoal();
Expand All @@ -159,6 +172,8 @@ public function create_goals( $old_settings, $settings ) {
*
* @param $old_settings
* @param $settings
*
* @codeCoverageIgnore Because we don't want to test if the API is working.
*/
public function maybe_delete_goals( $old_settings, $settings ) {
$enhanced_measurements_old = array_filter( $old_settings[ 'enhanced_measurements' ] );
Expand All @@ -175,7 +190,7 @@ public function maybe_delete_goals( $old_settings, $settings ) {
$key = array_search( $name, $this->custom_event_goals );

if ( ! in_array( $key, $disabled_settings ) ) {
continue;
continue; // @codeCoverageIgnore
}

$this->client->delete_goal( $id );
Expand All @@ -187,12 +202,14 @@ public function maybe_delete_goals( $old_settings, $settings ) {
* @param array $settings
*
* @return void
*
* @codeCoverageIgnore Because we don't want to test if the API is working.
*/
public function maybe_create_custom_properties( $old_settings, $settings ) {
$enhanced_measurements = $settings[ 'enhanced_measurements' ];

if ( ! in_array( 'pageview-props', $enhanced_measurements ) ) {
return;
return; // @codeCoverageIgnore
}

$create_request = new Client\Model\CustomPropEnableRequestBulkEnable();
Expand Down
3 changes: 3 additions & 0 deletions src/Admin/Settings/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

defined( 'ABSPATH' ) || exit;

/**
* @codeCoverageIgnore
*/
class API {
/**
* Admin Setting Fields.
Expand Down
3 changes: 3 additions & 0 deletions src/Admin/Settings/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

defined( 'ABSPATH' ) || exit;

/**
* @codeCoverageIgnore
*/
class Hooks extends API {
/**
* Build class properties.
Expand Down
4 changes: 3 additions & 1 deletion src/Admin/Upgrades.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ public function __construct() {
* @access public
* @return void
*
* @codeCoverageIgnore
* @throws Exception
*/
public function run() {
// @codeCoverageIgnoreStart
$plausible_analytics_version = get_option( 'plausible_analytics_version' );

// If version doesn't exist, then consider it `1.0.0`.
Expand Down Expand Up @@ -72,6 +73,7 @@ public function run() {
if ( version_compare( $plausible_analytics_version, '2.0.3', '<' ) ) {
$this->upgrade_to_203();
}
// @codeCoverageIgnoreEnd

// Add required upgrade routines for future versions here.
}
Expand Down
2 changes: 2 additions & 0 deletions src/Ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ private function clean( $var ) {
* @param $direction
*
* @return void
*
* @codeCoverageIgnore
*/
private function maybe_handle_redirect( $direction ) {
if ( ! empty( $direction ) ) {
Expand Down
Loading

0 comments on commit 18bc986

Please sign in to comment.