Skip to content

Commit

Permalink
test: harmonize options and plugin initialization
Browse files Browse the repository at this point in the history
All initializations with custom options are now triggered through the
same trait method to ensure complete options in every test case.
  • Loading branch information
stklcode committed Jun 16, 2020
1 parent 935365c commit 00d8797
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 43 deletions.
6 changes: 3 additions & 3 deletions tests/test-ajax-tracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function test_track_ajax() {
}

// Initialize Statify with default configuration: no JS tracking, no logged-in users.
$this->init_statify();
$this->init_statify_tracking();

try {
$this->_handleAjax( 'nopriv_statify_track' );
Expand All @@ -56,7 +56,7 @@ public function test_track_ajax() {
$this->assertNull( $stats, 'Stats should be empty, i.e. visit should not have been tracked' );

// Now enable JS tracking.
$this->init_statify( true );
$this->init_statify_tracking( true );

try {
$_POST['_wpnonce'] = wp_create_nonce( 'statify_track' );
Expand Down Expand Up @@ -111,7 +111,7 @@ public function test_track_ajax() {
unset( $e );

// Now we allow tracking for logged-in users.
$this->init_statify( true, true );
$this->init_statify_tracking( true, true );

try {
$_POST['_wpnonce'] = wp_create_nonce( 'statify_track' );
Expand Down
5 changes: 1 addition & 4 deletions tests/test-cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ public function setUp() {
*/
public function test_cronjob() {
// Initialize normal cycle, configure storage period of 3 days.
$options = get_option( 'statify' );
$options['days'] = 3;
update_option( 'statify', $options );
Statify::init();
$this->init_statify_widget( 3 );
$this->assertFalse(
has_action( 'statify_cleanup', array( 'Statify_Cron', 'cleanup_data' ) ),
'Statify cleanup cron job should not be registered in normal cycle'
Expand Down
6 changes: 3 additions & 3 deletions tests/test-dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function test_get_stats() {

// Initialize with default configuration, all limits greater data dimension.
Statify::init();
$this->init_statify_2( 14, 14, 3, false, false );
$this->init_statify_widget( 14, 14, 3, false, false );
$stats = $this->get_stats();

$this->assertEquals( 3, count( $stats['visits'] ), 'Unexpected number of days with visits' );
Expand Down Expand Up @@ -191,7 +191,7 @@ public function test_get_stats() {
$this->assertArrayNotHasKey( 'visit_totals', $stats, 'Totals should not be provided, if not configured' );

// Top lists only for today.
$this->init_statify_2( 14, 14, 3, true, false );
$this->init_statify_widget( 14, 14, 3, true, false );
$stats2 = $this->get_stats();

$this->assertEquals( $stats['visits'], $stats2['visits'], 'Visit counts should not be affected by "today" switch' );
Expand All @@ -215,7 +215,7 @@ public function test_get_stats() {
$this->assertArrayNotHasKey( 'visit_totals', $stats2, 'Totals should not be provided, if not configured' );

// Limited display range of 2 days with total numbers.
$this->init_statify_2( 14, 2, 3, false, true );
$this->init_statify_widget( 14, 2, 3, false, true );
$stats3 = $this->get_stats();

$this->assertEquals(
Expand Down
10 changes: 3 additions & 7 deletions tests/test-frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@
* Tests for frontend integration.
*/
class Test_Frontend extends WP_UnitTestCase {
use Statify_Test_Support;

/**
* Test wp_footer() generation.
*/
public function test_wp_footer() {
// Disable JS tracking.
$options = get_option( 'statify' );
$options['snippet'] = 0;
update_option( 'statify', $options );
Statify::init();
$this->init_statify_tracking( false );
$this->assertNotFalse(
has_action( 'wp_footer', array( 'Statify_Frontend', 'wp_footer' ) ),
'Statify footer action not registered'
Expand All @@ -32,9 +30,7 @@ public function test_wp_footer() {
);

// Enable JS tracking.
$options['snippet'] = 1;
update_option( 'statify', $options );
Statify::init();
$this->init_statify_tracking( true );

Statify_Frontend::wp_footer();
$this->assertTrue(
Expand Down
20 changes: 10 additions & 10 deletions tests/test-tracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function test_default_tracking() {
global $_SERVER;

// Initialize Statify with default configuration: no JS tracking, no logged-in users.
$this->init_statify();
$this->init_statify_tracking();

// Check if actions are registered.
$this->assertNotFalse(
Expand Down Expand Up @@ -109,7 +109,7 @@ public function test_default_tracking() {

// If JavaScript tracking is enabled, the regular request should not be tracked.
$_SERVER['REQUEST_URI'] = '/';
$this->init_statify( true, false );
$this->init_statify_tracking( true, false );
Statify_Frontend::track_visit();
$stats = $this->get_stats();
$this->assertEquals( 3, $stats['visits'][0]['count'], 'Unexpected visit count' );
Expand All @@ -123,7 +123,7 @@ public function test_skip_tracking() {
global $wp_query;

// Initialize Statify with default configuration: no JS tracking, no logged-in users.
$this->init_statify();
$this->init_statify_tracking();

// Basically a valid request.
$_SERVER['REQUEST_URI'] = '/';
Expand Down Expand Up @@ -177,7 +177,7 @@ public function test_bot_tracking() {
global $_SERVER;

// Initialize Statify with default configuration: no JS tracking, no logged-in users.
$this->init_statify();
$this->init_statify_tracking();

$bot_uas = array(
// Google Bots.
Expand Down Expand Up @@ -245,7 +245,7 @@ public function test_referer_blacklist() {
"example.com\nstatify.pluginkollektiv.org\nexample.net"
);

$this->init_statify( false, false, true );
$this->init_statify_tracking( false, false, true );

// Basically a valid request.
$_SERVER['REQUEST_URI'] = '/';
Expand All @@ -256,7 +256,7 @@ public function test_referer_blacklist() {
$stats = $this->get_stats();
$this->assertNull( $stats, 'Tracking for blacklisted referrer succeeded' );

$this->init_statify();
$this->init_statify_tracking();
Statify_Frontend::track_visit();
$stats = $this->get_stats();
$this->assertNotNull( $stats, 'Blacklist evaluated when not enabled' );
Expand All @@ -269,7 +269,7 @@ public function test_skip_tracking_hook() {
global $_SERVER;
global $wp_query;

$this->init_statify();
$this->init_statify_tracking();

// A valid request that should be tracked.
$_SERVER['REQUEST_URI'] = '/';
Expand Down Expand Up @@ -322,7 +322,7 @@ function ( $previous_result ) use ( &$capture, &$filter_result ) {
public function test_visit_saved_hook() {
global $_SERVER;

$this->init_statify();
$this->init_statify_tracking();

// A valid request that should be tracked.
$_SERVER['REQUEST_URI'] = '/page/';
Expand Down Expand Up @@ -363,7 +363,7 @@ public function test_track_users() {
wp_set_current_user( 1 );

// Initialize Statify with default configuration: no JS tracking, no logged-in users.
$this->init_statify();
$this->init_statify_tracking();

// Basically a valid request.
$_SERVER['REQUEST_URI'] = '/private-page/';
Expand All @@ -375,7 +375,7 @@ public function test_track_users() {
$this->assertNull( $stats, 'Logged-in user should not be tracked' );

// Re-initialize Statify, enabling logged-in user tracking.
$this->init_statify( false, true );
$this->init_statify_tracking( false, true );

Statify_Frontend::track_visit();
$stats = $this->get_stats();
Expand Down
45 changes: 29 additions & 16 deletions tests/trait-statify-test-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ trait Statify_Test_Support {
* @param bool $track_logged_in Configure tracking for logged-in users (default: false).
* @param bool $blacklist Configure blacklist usage (default: false).
*/
protected function init_statify( $use_snippet = false, $track_logged_in = false, $blacklist = false ) {
$options = get_option( 'statify' );

$options['snippet'] = $use_snippet ? 1 : 0;
$options['skip']['logged_in'] = $track_logged_in ? 0 : 1;
$options['blacklist'] = $blacklist ? 1 : 0;

update_option( 'statify', $options );

Statify::init();
protected function init_statify_tracking( $use_snippet = false, $track_logged_in = false, $blacklist = false ) {
$this->init_statify(
array(
'snippet' => $use_snippet ? 1 : 0,
'skip' => array(
'logged_in' => $track_logged_in ? 0 : 1,
),
'blacklist' => $blacklist ? 1 : 0,
)
);
}

/**
Expand All @@ -39,18 +39,31 @@ protected function init_statify( $use_snippet = false, $track_logged_in = false,
* @param boolean $today Show top list only for today.
* @param boolean $totals Show totals.
*/
protected function init_statify_2( $days_store, $days_show, $top_limit, $today, $totals ) {
protected function init_statify_widget( $days_store = 14, $days_show = 14, $top_limit = 3, $today = false, $totals = false ) {
$this->init_statify(
array(
'days' => $days_store,
'days_show' => $days_show,
'limit' => $top_limit,
'today' => $today ? 1 : 0,
'show_totals' => $totals ? 1 : 0,
)
);
}

/**
* Initialize Statify with custom options.
*
* @param array $args Custom parameters (key => value).
*/
protected function init_statify( $args = array() ) {
$options = get_option( 'statify' );

if ( false === $options && isset( Statify::$_options ) ) {
$options = Statify::$_options;
}

$options['days'] = $days_store;
$options['days_show'] = $days_show;
$options['limit'] = $top_limit;
$options['today'] = $today ? 1 : 0;
$options['show_totals'] = $totals ? 1 : 0;
$options = wp_parse_args( $args, $options );

update_option( 'statify', $options );

Expand Down

0 comments on commit 00d8797

Please sign in to comment.