Skip to content

Commit

Permalink
do not return NULL, if database is empty
Browse files Browse the repository at this point in the history
Null causes trouble during further processing, i.e. PHP warnings in the
API processing and later on problems building the dashboard widget in
Javascript, because all methods expect the array to have all keys.

Move the caching (transient) directive into the conditional, so we still
do not cache empty results and so not return NULL anymore.
  • Loading branch information
stklcode committed Oct 16, 2023
1 parent 0ec5f69 commit 81c467e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 25 deletions.
16 changes: 7 additions & 9 deletions inc/class-statify-dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,16 +283,14 @@ public static function get_stats( $force_refresh = false ) {
// Prepare data.
if ( ! empty( $data['visits'] ) ) {
$data['visits'] = array_reverse( $data['visits'] );
} else {
$data = null;
}

// Make cache.
set_transient(
'statify_data',
$data,
MINUTE_IN_SECONDS * 15
);
// Make cache.
set_transient(
'statify_data',
$data,
MINUTE_IN_SECONDS * 15
);
}

return $data;
}
Expand Down
10 changes: 9 additions & 1 deletion tests/test-dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,15 @@ function ( $original ) use ( &$original_capture, &$override ) {
*/
public function test_get_stats() {
// Initially the database is empty.
$this->assertNull( $this->get_stats(), 'Expected NULL stats for empty database' );
$this->assertEquals(
array(
'referrer' => array(),
'target' => array(),
'visits' => array(),
),
$this->get_stats(),
'Expected empty dataset for empty database'
);

// Now insert data for the last 3 days.
$date1 = new DateTime();
Expand Down
66 changes: 51 additions & 15 deletions tests/test-tracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function test_default_tracking() {
Statify_Frontend::track_visit();
$stats = $this->get_stats();

$this->assertNotNull( $stats, 'Stats should be filled after tracking' );
$this->assertNotEmptyStats( $stats, 'Stats should be filled after tracking' );

$this->assertEquals( 1, count( $stats['visits'] ), 'Unexpected number of days with visits' );
$this->assertEquals( ( new DateTime() )->format( 'Y-m-d' ), $stats['visits'][0]['date'], 'Unexpected date of tracking' );
Expand All @@ -71,7 +71,7 @@ public function test_default_tracking() {
Statify_Frontend::track_visit();

$stats = $this->get_stats();
$this->assertNotNull( $stats, 'Stats should be filled after tracking' );
$this->assertNotEmptyStats( $stats, 'Stats should be filled after tracking' );

$this->assertEquals( 1, count( $stats['visits'] ), 'Unexpected number of days with visits' );
$this->assertEquals( ( new DateTime() )->format( 'Y-m-d' ), $stats['visits'][0]['date'], 'Unexpected date of tracking' );
Expand Down Expand Up @@ -134,39 +134,39 @@ public function test_skip_tracking() {
$wp_query->is_robots = true;
Statify_Frontend::track_visit();
$stats = $this->get_stats();
$this->assertNull( $stats, 'Robots should not be tracked' );
$this->assertEmptyStats( $stats, 'Robots should not be tracked' );

$wp_query->is_robots = false;
$wp_query->is_trackback = true;
Statify_Frontend::track_visit();
$stats = $this->get_stats();
$this->assertNull( $stats, 'Trackbacks should not be tracked.' );
$this->assertEmptyStats( $stats, 'Trackbacks should not be tracked.' );

$wp_query->is_trackback = false;
$wp_query->is_preview = true;
Statify_Frontend::track_visit();
$stats = $this->get_stats();
$this->assertNull( $stats, 'Previews should not be tracked.' );
$this->assertEmptyStats( $stats, 'Previews should not be tracked.' );

$wp_query->is_preview = false;
$wp_query->is_404 = true;
Statify_Frontend::track_visit();
$stats = $this->get_stats();
$this->assertNull( $stats, '404 should not be tracked.' );
$this->assertEmptyStats( $stats, '404 should not be tracked.' );

$wp_query->is_404 = false;
$wp_query->is_feed = true;
Statify_Frontend::track_visit();
$stats = $this->get_stats();
$this->assertNull( $stats, 'Feeds should not be tracked.' );
$this->assertEmptyStats( $stats, 'Feeds should not be tracked.' );

// Favicon is available for WP 5.4 and above only.
$wp_query->is_feed = false;
if ( function_exists( 'is_favicon' ) ) {
$wp_query->is_favicon = true;
Statify_Frontend::track_visit();
$stats = $this->get_stats();
$this->assertNull( $stats, 'Favicons should not be tracked.' );
$this->assertEmptyStats( $stats, 'Favicons should not be tracked.' );
$wp_query->is_favicon = false;
}

Expand All @@ -175,12 +175,12 @@ public function test_skip_tracking() {
set_query_var( 'sitemap', 'index' );
Statify_Frontend::track_visit();
$stats = $this->get_stats();
$this->assertNull( $stats, 'Sitemap XML should not be tracked.' );
$this->assertEmptyStats( $stats, 'Sitemap XML should not be tracked.' );
set_query_var( 'sitemap', null );
set_query_var( 'sitemap-stylesheet', 'sitemap' );
Statify_Frontend::track_visit();
$stats = $this->get_stats();
$this->assertNull( $stats, 'Sitemap XSL should not be tracked.' );
$this->assertEmptyStats( $stats, 'Sitemap XSL should not be tracked.' );
}
}

Expand Down Expand Up @@ -243,7 +243,7 @@ public function test_bot_tracking() {

Statify_Frontend::track_visit();
$stats = $this->get_stats();
$this->assertNull( $stats, 'Bot exclusion failed for user agent: ' . $bot_ua );
$this->assertEmptyStats( $stats, 'Bot exclusion failed for user agent: ' . $bot_ua );
}
}

Expand All @@ -269,12 +269,12 @@ public function test_disallowed_referer() {

Statify_Frontend::track_visit();
$stats = $this->get_stats();
$this->assertNull( $stats, 'Tracking for blacklisted referrer succeeded' );
$this->assertEmptyStats( $stats, 'Tracking for blacklisted referrer succeeded' );

$this->init_statify_tracking();
Statify_Frontend::track_visit();
$stats = $this->get_stats();
$this->assertNotNull( $stats, 'Blacklist evaluated when not enabled' );
$this->assertNotEmptyStats( $stats, 'Blacklist evaluated when not enabled' );
}

/**
Expand Down Expand Up @@ -387,14 +387,14 @@ public function test_track_users() {

Statify_Frontend::track_visit();
$stats = $this->get_stats();
$this->assertNull( $stats, 'Logged-in user should not be tracked' );
$this->assertEmptyStats( $stats, 'Logged-in user should not be tracked' );

// Re-initialize Statify, enabling logged-in user tracking.
$this->init_statify_tracking( Statify_Frontend::TRACKING_METHOD_DEFAULT, Statify::SKIP_USERS_NONE );

Statify_Frontend::track_visit();
$stats = $this->get_stats();
$this->assertNotNull( $stats, 'Logged-in user should be tracked' );
$this->assertNotEmptyStats( $stats, 'Logged-in user should be tracked' );
$this->assertEquals( 1, $stats['visits'][0]['count'], 'Logged-in user should be tracked' );

// Exclude administrators.
Expand All @@ -410,4 +410,40 @@ public function test_track_users() {
$stats = $this->get_stats();
$this->assertEquals( 2, $stats['visits'][0]['count'], 'Regular user should be tracked' );
}

/**
* Assert that given value is an empty stats result.
*
* @param array $actual Actual value to test.
* @param string $message Reason (optional).
*
* @return void
*/
private function assertEmptyStats( $actual, $message = '' ) {
$this->assertEquals(
array(
'referrer' => array(),
'target' => array(),
'visits' => array(),
),
$actual,
$message
);
}

/**
* Assert that given value is not an empty stats result.
*
* @param array $actual Actual value to test.
* @param string $message Reason (optional).
*
* @return void
*/
private function assertNotEmptyStats( $actual, $message = '' ) {
$this->assertTrue(
! empty( $actual ) && isset( $actual['visits'] ) && count( $actual['visits'] ) > 0,
$actual,
$message
);
}
}

0 comments on commit 81c467e

Please sign in to comment.