Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do not return NULL, if database is empty #273

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
65 changes: 50 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,39 @@ 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,
$message
);
}
}