Skip to content

Commit

Permalink
Added: HelpersTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan0sz committed Mar 20, 2024
1 parent d8f3634 commit 13d9573
Show file tree
Hide file tree
Showing 3 changed files with 265 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public static function get_proxy_resource( $resource_name = '' ) {
/**
* Create the cache directory if it doesn't exist.
*/
if ( $resource_name === 'cache_dir' && ! is_dir( $resources[ $resource_name ] ) ) {
if ( ( $resource_name === 'cache_dir' || $resource_name === 'cache_url' ) && ! is_dir( $resources[ 'cache_dir' ] ) ) {
wp_mkdir_p( $resources[ $resource_name ] );
}

Expand Down Expand Up @@ -193,7 +193,7 @@ public static function update_setting( $option_name, $option_value ) {
}

/**
* A convenient way to retrieve the absolute path to the local JS file.
* A convenient way to retrieve the absolute path to the local JS file. Proxy should be enabled when this method is called!
* @return string
* @throws Exception
*/
Expand All @@ -202,7 +202,7 @@ public static function get_js_path() {
}

/**
* Downloads the plausible.js file to this server.
* Downloads a remote file to this server.
* @since 1.3.0
*
* @param string $local_file Absolute path to where to store the $remote_file.
Expand Down
261 changes: 261 additions & 0 deletions tests/integration/HelpersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
<?php
/**
* @package Plausible Analytics Integration Tests - Helpers
*/

namespace Plausible\Analytics\Tests\Integration;

use Plausible\Analytics\Tests\TestCase;
use Plausible\Analytics\WP\Helpers;

class HelpersTest extends TestCase {
/**
* @see Helpers::get_js_url()
*/
public function testGetJsUrl() {
$url = Helpers::get_js_url();

$this->assertEquals( 'https://plausible.io/js/plausible.js', $url );

add_filter( 'plausible_analytics_settings', [ $this, 'enableProxy' ] );

$url = Helpers::get_js_url( true );

remove_filter( 'plausible_analytics_settings', [ $this, 'enableProxy' ] );

$this->assertMatchesRegularExpression( '~http://example.org/wp-content/uploads/.*?/.*?.js~', $url );

add_filter( 'plausible_analytics_settings', [ $this, 'enableSelfHostedDomain' ] );

$url = Helpers::get_js_url();

remove_filter( 'plausible_analytics_settings', [ $this, 'enableSelfHostedDomain' ] );

$this->assertEquals( 'https://self-hosted-test.org/js/plausible.js', $url );
}

/**
* Enable the proxy.
*
* @param $settings
*
* @return mixed
*/
public function enableProxy( $settings ) {
$settings[ 'proxy_enabled' ] = 'on';

return $settings;
}

/**
* Enable Self Hosted domain.
*
* @param $settings
*
* @return mixed
*/
public function enableSelfHostedDomain( $settings ) {
$settings[ 'self_hosted_domain' ] = 'self-hosted-test.org';

return $settings;
}

/**
* @see Helpers::get_filename()
*/
public function testGetFilename() {
add_filter( 'plausible_analytics_settings', [ $this, 'addExcludedPages' ] );

$filename = Helpers::get_filename();

remove_filter( 'plausible_analytics_settings', [ $this, 'addExcludedPages' ] );

$this->assertEquals( 'plausible.exclusions', $filename );

add_filter( 'plausible_analytics_settings', [ $this, 'enableProxy' ] );

$filename = Helpers::get_filename( true );

remove_filter( 'plausible_analytics_settings', [ $this, 'enableProxy' ] );

$this->assertMatchesRegularExpression( '~[a-z0-9]{8}~', $filename );

add_filter( 'plausible_analytics_settings', [ $this, 'enableOutboundLinks' ] );

$filename = Helpers::get_filename();

remove_filter( 'plausible_analytics_settings', [ $this, 'enableOutboundLinks' ] );

$this->assertEquals( 'plausible.outbound-links', $filename );
}

/**
* Enable excluded pages option.
*
* @param $settings
*
* @return mixed
*/
public function addExcludedPages( $settings ) {
$settings[ 'excluded_pages' ] = 'test';

return $settings;
}

/**
* Enable Enhanced Measurements > Outbound Links.
*
* @param $settings
*
* @return mixed
*/
public function enableOutboundLinks( $settings ) {
$settings[ 'enhanced_measurements' ] = [ 'outbound-links' ];

return $settings;
}

/**
* @see Helpers::get_proxy_resource()
* @return void
* @throws \Exception
*/
public function testGetProxyResource() {
$namespace = Helpers::get_proxy_resource( 'namespace' );

$this->assertMatchesRegularExpression( '/[a-z0-9]{6}/', $namespace );

$base = Helpers::get_proxy_resource( 'base' );

$this->assertMatchesRegularExpression( '/[a-z0-9]{4}/', $base );

$endpoint = Helpers::get_proxy_resource( 'endpoint' );

$this->assertMatchesRegularExpression( '/[a-z0-9]{8}/', $endpoint );

$cache_dir = Helpers::get_proxy_resource( 'cache_dir' );
$upload_dir = wp_get_upload_dir()[ 'basedir' ];

$this->assertMatchesRegularExpression( "~$upload_dir/[a-z0-9]{10}/~", $cache_dir );
$this->assertTrue( is_dir( $cache_dir ) );

$cache_url = Helpers::get_proxy_resource( 'cache_url' );
$upload_url = wp_get_upload_dir()[ 'baseurl' ];

$this->assertMatchesRegularExpression( "~$upload_url/[a-z0-9]{10}/~", $cache_url );

$file_alias = Helpers::get_proxy_resource( 'file_alias' );

$this->assertMatchesRegularExpression( '/[a-z0-9]{8}/', $file_alias );
}

/**
* @see Helpers::update_setting()
* @return void
*/
public function testUpdateSetting() {
Helpers::update_setting( 'test', true );

$this->assertTrue( Helpers::get_settings()[ 'test' ] );
}

/**
* @see Helpers::get_js_path()
* @return void
* @throws \Exception
*/
public function testGetJsPath() {
add_filter( 'plausible_analytics_settings', [ $this, 'enableProxy' ] );

$path = Helpers::get_js_path();

remove_filter( 'plausible_analytics_settings', [ $this, 'enableProxy' ] );

$upload_dir = wp_get_upload_dir()[ 'basedir' ];

$this->assertMatchesRegularExpression( "~$upload_dir/[a-z0-9]{10}/[a-z0-9]{8}\.js~", $path );
}

/**
* @see Helpers::download_file()
* @return void
* @throws \Exception
*/
public function testDownloadFile() {
Helpers::download_file( 'https://plausible.io/js/plausible.js', wp_get_upload_dir()[ 'basedir' ] . '/test.js' );

$this->assertFileExists( wp_get_upload_dir()[ 'basedir' ] . '/test.js' );
}

/**
* @see Helpers::get_domain()
* @return void
*/
public function testGetDomain() {
$domain = Helpers::get_domain();

$this->assertEquals( 'example.org', $domain );

add_filter( 'plausible_analytics_settings', [ $this, 'setDomain' ] );

$domain = Helpers::get_domain();

remove_filter( 'plausible_analytics_settings', [ $this, 'setDomain' ] );

$this->assertEquals( 'test.dev', $domain );
}

/**
* Set domain_name option.
*
* @param $settings
*
* @return mixed
*/
public function setDomain( $settings ) {
$settings[ 'domain_name' ] = 'test.dev';

return $settings;
}

/**
* @see Helpers::get_data_api_url()
* @return void
*/
public function testGetDataApiUrl() {
$url = Helpers::get_data_api_url();

$this->assertEquals( 'https://plausible.io/api/event', $url );

add_filter( 'plausible_analytics_settings', [ $this, 'enableProxy' ] );

$url = Helpers::get_data_api_url();

remove_filter( 'plausible_analytics_settings', [ $this, 'enableProxy' ] );

$this->assertMatchesRegularExpression( '~http://example.org/index.php\?rest_route=/[0-9a-z]{6}/v1/[0-9a-z]{4}/[0-9a-z]{8}~', $url );

add_filter( 'plausible_analytics_settings', [ $this, 'enableSelfHostedDomain' ] );

$url = Helpers::get_data_api_url();

remove_filter( 'plausible_analytics_settings', [ $this, 'enableSelfHostedDomain' ] );

$this->assertEquals( 'https://self-hosted-test.org/api/event', $url );
}

/**
* @see Helpers::get_rest_endpoint()
* @return void
* @throws \Exception
*/
public function testGetRestEndpoint() {
$endpoint = Helpers::get_rest_endpoint( false );

$this->assertMatchesRegularExpression( '~/wp-json/[0-9a-z]{6}/v1/[0-9a-z]{4}/[0-9a-z]{8}~', $endpoint );

$endpoint = Helpers::get_rest_endpoint();

$this->assertMatchesRegularExpression( '~http://example.org/index.php\?rest_route=/[0-9a-z]{6}/v1/[0-9a-z]{4}/[0-9a-z]{8}~', $endpoint );
}
}
2 changes: 1 addition & 1 deletion tests/integration/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class PluginTest extends TestCase {
/**
*
* @see Plugin::register()
*/
public function testRegister() {
$class = new Plugin();
Expand Down

0 comments on commit 13d9573

Please sign in to comment.