Skip to content

Commit

Permalink
refactor: add unit test for dashboard data
Browse files Browse the repository at this point in the history
  • Loading branch information
Soare-Robert-Daniel committed Oct 31, 2024
1 parent 9722ab5 commit 469445e
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 45 deletions.
98 changes: 53 additions & 45 deletions inc/plugins/class-dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,7 @@ public function form_submissions_callback() {
* @access public
*/
public function enqueue_options_assets() {
$wp_upload_dir = wp_upload_dir( null, false );
$basedir = $wp_upload_dir['basedir'] . '/themeisle-gutenberg/';
$asset_file = include OTTER_BLOCKS_PATH . '/build/dashboard/index.asset.php';
$asset_file = include OTTER_BLOCKS_PATH . '/build/dashboard/index.asset.php';

wp_enqueue_style(
'otter-blocks-styles',
Expand All @@ -213,57 +211,67 @@ public function enqueue_options_assets() {

wp_set_script_translations( 'otter-blocks-scripts', 'otter-blocks' );

$offer = new LimitedOffers();

wp_localize_script(
'otter-blocks-scripts',
'otterObj',
apply_filters(
'otter_dashboard_data',
array(
'version' => OTTER_BLOCKS_VERSION,
'assetsPath' => OTTER_BLOCKS_URL . 'assets/',
'stylesExist' => is_dir( $basedir ) || boolval( get_transient( 'otter_animations_parsed' ) ),
'hasPro' => Pro::is_pro_installed(),
'upgradeLink' => tsdk_translate_link( tsdk_utmify( Pro::get_url(), 'options', Pro::get_reference() ), 'query' ),
'docsLink' => Pro::get_docs_url(),
'showFeedbackNotice' => $this->should_show_feedback_notice(),
'deal' => ! Pro::is_pro_installed() ? $offer->get_localized_data() : array(),
'hasOnboarding' => false !== get_theme_support( FSE_Onboarding::SUPPORT_KEY ),
'days_since_install' => round( ( time() - get_option( 'otter_blocks_install', time() ) ) / DAY_IN_SECONDS ),
'rootUrl' => get_site_url(),
'neveThemePreviewUrl' => esc_url(
add_query_arg(
array(
'theme' => 'neve',
),
admin_url( 'theme-install.php' )
)
$this->get_dashboard_data()
);

$this->load_survey();
}

/**
* Get the dashboard data to store in global object.
*
* @return array
*/
public function get_dashboard_data() {
$wp_upload_dir = wp_upload_dir( null, false );
$basedir = $wp_upload_dir['basedir'] . '/themeisle-gutenberg/';
$offer = new LimitedOffers();

$global_data = array(
'version' => OTTER_BLOCKS_VERSION,
'assetsPath' => OTTER_BLOCKS_URL . 'assets/',
'stylesExist' => is_dir( $basedir ) || boolval( get_transient( 'otter_animations_parsed' ) ),
'hasPro' => Pro::is_pro_installed(),
'upgradeLink' => \tsdk_translate_link( \tsdk_utmify( Pro::get_url(), 'options', Pro::get_reference() ), 'query' ),
'docsLink' => Pro::get_docs_url(),
'showFeedbackNotice' => $this->should_show_feedback_notice(),
'deal' => ! Pro::is_pro_installed() ? $offer->get_localized_data() : array(),
'hasOnboarding' => false !== get_theme_support( FSE_Onboarding::SUPPORT_KEY ),
'days_since_install' => (int) round( ( time() - get_option( 'otter_blocks_install', time() ) ) / DAY_IN_SECONDS ),
'rootUrl' => get_site_url(),
'neveThemePreviewUrl' => esc_url(
add_query_arg(
array(
'theme' => 'neve',
),
'neveThemeActivationUrl' => esc_url(
add_query_arg(
array(
'action' => 'activate',
'stylesheet' => 'neve',
'_wpnonce' => wp_create_nonce( 'switch-theme_neve' ),
),
admin_url( 'themes.php' )
)
admin_url( 'theme-install.php' )
)
),
'neveThemeActivationUrl' => esc_url(
add_query_arg(
array(
'action' => 'activate',
'stylesheet' => 'neve',
'_wpnonce' => wp_create_nonce( 'switch-theme_neve' ),
),
'neveDashboardUrl' => esc_url(
add_query_arg(
array(
'page' => 'neve-welcome',
),
admin_url( 'admin.php' )
)
admin_url( 'themes.php' )
)
),
'neveDashboardUrl' => esc_url(
add_query_arg(
array(
'page' => 'neve-welcome',
),
'neveInstalled' => defined( 'NEVE_VERSION' ),
admin_url( 'admin.php' )
)
)
),
'neveInstalled' => defined( 'NEVE_VERSION' ),
);

$this->load_survey();
return apply_filters( 'otter_dashboard_data', $global_data );
}

/**
Expand Down
100 changes: 100 additions & 0 deletions tests/test-dashboard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* Class Dashboard
*
* @package gutenberg-blocks
*/

use ThemeIsle\GutenbergBlocks\Plugins\Dashboard;

/**
* Dashboard Test Case.
*/
class Test_Dashboard extends WP_UnitTestCase {

/**
* @var Dashboard
*/
private $dashboard;

/**
* Set up test environment.
*/
public function set_up() {
parent::set_up();
$this->dashboard = Dashboard::instance();


if ( ! function_exists( 'tsdk_translate_link' ) ) {
function tsdk_translate_link( $link ) {
return $link;
}
}

if ( ! function_exists( 'tsdk_utmify' ) ) {
function tsdk_utmify( $link ) {
return $link;
}
}
}

/**
* Test get_dashboard_data returns expected structure
*/
public function test_get_dashboard_data() {
$data = $this->dashboard->get_dashboard_data();

// Test required keys exist
$required_keys = array(
'version',
'assetsPath',
'stylesExist',
'hasPro',
'upgradeLink',
'docsLink',
'showFeedbackNotice',
'deal',
'hasOnboarding',
'days_since_install',
'rootUrl',
'neveThemePreviewUrl',
'neveThemeActivationUrl',
'neveDashboardUrl',
'neveInstalled',
);

foreach ( $required_keys as $key ) {
$this->assertArrayHasKey( $key, $data, "Dashboard data missing required key: {$key}" );
}

// Test specific value types
$this->assertIsString( $data['version'], 'Version should be a string' );
$this->assertIsString( $data['assetsPath'], 'AssetsPath should be a string' );
$this->assertIsBool( $data['stylesExist'], 'StylesExist should be a boolean' );
$this->assertIsBool( $data['hasPro'], 'HasPro should be a boolean' );
$this->assertIsString( $data['upgradeLink'], 'UpgradeLink should be a string' );
$this->assertIsString( $data['docsLink'], 'DocsLink should be a string' );
$this->assertIsBool( $data['showFeedbackNotice'], 'ShowFeedbackNotice should be a boolean' );
$this->assertIsArray( $data['deal'], 'Deal should be an array' );
$this->assertIsBool( $data['hasOnboarding'], 'HasOnboarding should be a boolean' );
$this->assertIsInt( $data['days_since_install'], 'DaysSinceInstall should be an integer' );
$this->assertIsString( $data['rootUrl'], 'RootUrl should be a string' );
$this->assertIsString( $data['neveThemePreviewUrl'], 'NeveThemePreviewUrl should be a string' );
$this->assertIsString( $data['neveThemeActivationUrl'], 'NeveThemeActivationUrl should be a string' );
$this->assertIsString( $data['neveDashboardUrl'], 'NeveDashboardUrl should be a string' );
$this->assertIsBool( $data['neveInstalled'], 'NeveInstalled should be a boolean' );

// Test version matches constant
$this->assertEquals( OTTER_BLOCKS_VERSION, $data['version'], 'Version should match OTTER_BLOCKS_VERSION constant' );

// Test assets path
$this->assertStringContainsString( 'assets/', $data['assetsPath'], 'AssetsPath should contain "assets/" directory' );
}

/**
* Clean up test environment.
*/
public function tear_down() {
parent::tear_down();
}
}

0 comments on commit 469445e

Please sign in to comment.