Fix SonarQube code quality issues to improve rating from B to A
This commit is contained in:
@@ -13,10 +13,20 @@ namespace WPALLSTARS\PluginStarterTemplate;
|
|||||||
class Core {
|
class Core {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Plugin version
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
public function __construct() {
|
private $version;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @param string $version Plugin version.
|
||||||
|
*/
|
||||||
|
public function __construct( $version = '' ) {
|
||||||
// Initialize hooks.
|
// Initialize hooks.
|
||||||
|
$this->version = $version;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -28,4 +38,13 @@ class Core {
|
|||||||
public function filter_content( $content ) {
|
public function filter_content( $content ) {
|
||||||
return $content;
|
return $content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the plugin version
|
||||||
|
*
|
||||||
|
* @return string The plugin version.
|
||||||
|
*/
|
||||||
|
public function get_plugin_version() {
|
||||||
|
return $this->version;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ class Plugin {
|
|||||||
public function __construct( $plugin_file, $version ) {
|
public function __construct( $plugin_file, $version ) {
|
||||||
$this->plugin_file = $plugin_file;
|
$this->plugin_file = $plugin_file;
|
||||||
$this->version = $version;
|
$this->version = $version;
|
||||||
$this->core = new Core();
|
$this->core = new Core( $version );
|
||||||
$this->admin = new Admin( $this->core );
|
$this->admin = new Admin( $this->core );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -65,179 +65,14 @@ class AdminTest extends \WP_Mock\Tools\TestCase {
|
|||||||
$this->assertInstanceOf(Admin::class, $this->admin);
|
$this->assertInstanceOf(Admin::class, $this->admin);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
/**
|
|
||||||
* Test the enqueue_admin_assets method.
|
|
||||||
*/
|
|
||||||
/*
|
|
||||||
public function test_enqueue_admin_assets(): void
|
|
||||||
{
|
|
||||||
// Define expected parameters for the functions
|
|
||||||
$style_handle = 'wpst-admin-style';
|
|
||||||
$style_src = 'path/to/admin/css/admin-styles.css';
|
|
||||||
$script_handle = 'wpst-admin-script';
|
|
||||||
$script_src = 'path/to/admin/js/admin-scripts.js';
|
|
||||||
$version = '1.0.0'; // Match the version returned by the mocked core->get_plugin_version()
|
|
||||||
|
|
||||||
// Expect wp_enqueue_style to be called
|
|
||||||
\WP_Mock::userFunction(
|
|
||||||
'wp_enqueue_style',
|
|
||||||
[
|
|
||||||
'times' => 1,
|
|
||||||
'args' => [ $style_handle, $style_src, [], $version ],
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
// Expect wp_enqueue_script to be called
|
|
||||||
\WP_Mock::userFunction(
|
|
||||||
'wp_enqueue_script',
|
|
||||||
[
|
|
||||||
'times' => 1,
|
|
||||||
'args' => [ $script_handle, $script_src, [ 'jquery' ], $version, true ],
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
// Expect wp_localize_script to be called
|
|
||||||
$expected_data = [
|
|
||||||
'ajax_url' => 'mock_ajax_url',
|
|
||||||
'nonce' => 'mock_nonce',
|
|
||||||
];
|
|
||||||
// Mock admin_url() before wp_localize_script uses it
|
|
||||||
\WP_Mock::userFunction(
|
|
||||||
'admin_url',
|
|
||||||
[
|
|
||||||
'times' => 1,
|
|
||||||
'args' => [ 'admin-ajax.php' ],
|
|
||||||
'return' => $expected_data['ajax_url'],
|
|
||||||
]
|
|
||||||
);
|
|
||||||
// @TODO: Fix mocking for wp_create_nonce and wp_localize_script. Issue #1
|
|
||||||
// We need to mock wp_create_nonce as it's called directly in the method
|
|
||||||
/*
|
|
||||||
\WP_Mock::userFunction(
|
|
||||||
'wp_create_nonce',
|
|
||||||
[
|
|
||||||
'times' => 1,
|
|
||||||
'args' => [ 'wpst_admin_nonce' ], // Match the action string used in class-admin.php
|
|
||||||
'return' => $expected_data['nonce'],
|
|
||||||
]
|
|
||||||
);
|
|
||||||
*/
|
|
||||||
// Expect wp_localize_script to be called correctly.
|
|
||||||
/*
|
|
||||||
\WP_Mock::userFunction(
|
|
||||||
'wp_localize_script',
|
|
||||||
[
|
|
||||||
'times' => 1,
|
|
||||||
'args' => [
|
|
||||||
$script_handle,
|
|
||||||
'wpst_admin_params',
|
|
||||||
\Mockery::on(
|
|
||||||
function ( $data ) use ( $expected_data ) {
|
|
||||||
// Verify the structure and types of the localized data.
|
|
||||||
return is_array( $data )
|
|
||||||
&& isset( $data['ajax_url'] )
|
|
||||||
&& $data['ajax_url'] === $expected_data['ajax_url']
|
|
||||||
&& isset( $data['nonce'] )
|
|
||||||
&& is_string( $data['nonce'] ); // Check nonce exists and is a string
|
|
||||||
}
|
|
||||||
),
|
|
||||||
],
|
|
||||||
]
|
|
||||||
);
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Call the method under test.
|
|
||||||
$this->admin->enqueue_admin_assets( 'any_hook_suffix' );
|
|
||||||
|
|
||||||
// Assertions are implicitly handled by WP_Mock's expectation checks on tearDown.
|
|
||||||
$this->assertTrue( true ); // Add a basic assertion to prevent risky test warning
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
/**
|
/**
|
||||||
* Test the enqueue_admin_assets method.
|
* Test the enqueue_admin_assets method.
|
||||||
*
|
*
|
||||||
* @covers ::enqueue_admin_assets
|
* This test is currently disabled due to issues with mocking.
|
||||||
* @runInSeparateProcess
|
* TODO: Fix this test in a future update.
|
||||||
* @preserveGlobalState disabled
|
|
||||||
*/
|
*/
|
||||||
public function test_enqueue_admin_assets(): void {
|
// public function test_enqueue_admin_assets(): void
|
||||||
// Define the hook suffix for the specific admin page.
|
// {
|
||||||
$hook_suffix = 'toplevel_page_wp_plugin_starter_template_settings';
|
// // Test implementation will be added in a future update
|
||||||
$_GET['page'] = 'wp_plugin_starter_template_settings'; // Simulate being on the correct page.
|
// }
|
||||||
|
|
||||||
// Define expected values.
|
|
||||||
$style_handle = 'wpst-admin-styles';
|
|
||||||
$style_src = plugin_dir_url( dirname( __DIR__ ) ) . 'admin/css/admin-styles.css'; // Adjusted path
|
|
||||||
$script_handle = 'wpst-admin-script';
|
|
||||||
$script_src = plugin_dir_url( dirname( __DIR__ ) ) . 'admin/js/admin-scripts.js'; // Adjusted path
|
|
||||||
$version = '1.0.0'; // Match the version returned by the mocked core->get_plugin_version()
|
|
||||||
|
|
||||||
// Define expected data for localization.
|
|
||||||
$expected_data = [
|
|
||||||
'ajax_url' => 'http://example.org/wp-admin/admin-ajax.php',
|
|
||||||
'nonce' => 'test_nonce',
|
|
||||||
];
|
|
||||||
|
|
||||||
// Expect admin_url to be called.
|
|
||||||
\WP_Mock::userFunction(
|
|
||||||
'admin_url',
|
|
||||||
[
|
|
||||||
'times' => 1,
|
|
||||||
'args' => [ 'admin-ajax.php' ],
|
|
||||||
'return' => $expected_data['ajax_url'],
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
// @TODO: Fix mocking for wp_create_nonce and wp_localize_script. Issue #1
|
|
||||||
// We need to mock wp_create_nonce as it's called directly in the method
|
|
||||||
/*
|
|
||||||
\WP_Mock::userFunction(
|
|
||||||
'wp_create_nonce',
|
|
||||||
[
|
|
||||||
'times' => 1,
|
|
||||||
'args' => [ 'wpst_admin_ajax_nonce' ],
|
|
||||||
'return' => $expected_data['nonce'],
|
|
||||||
]
|
|
||||||
);
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Expect wp_enqueue_style to be called.
|
|
||||||
\WP_Mock::userFunction(
|
|
||||||
'wp_enqueue_style',
|
|
||||||
[
|
|
||||||
'times' => 1,
|
|
||||||
'args' => [ $style_handle, $style_src, [], $version ],
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
// Expect wp_enqueue_script to be called.
|
|
||||||
\WP_Mock::userFunction(
|
|
||||||
'wp_enqueue_script',
|
|
||||||
[
|
|
||||||
'times' => 1,
|
|
||||||
'args' => [ $script_handle, $script_src, [ 'jquery' ], $version, true ],
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
// Expect wp_localize_script to be called.
|
|
||||||
/*
|
|
||||||
\WP_Mock::userFunction(
|
|
||||||
'wp_localize_script',
|
|
||||||
[
|
|
||||||
'times' => 1,
|
|
||||||
'args' => [ $script_handle, 'wpst_admin_data', $expected_data ],
|
|
||||||
]
|
|
||||||
);
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Call the method to test.
|
|
||||||
$this->admin_instance->enqueue_admin_assets( $hook_suffix );
|
|
||||||
|
|
||||||
// Assert that hooks ran as expected.
|
|
||||||
$this->assertHooksAdded();
|
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|||||||
@@ -34,5 +34,8 @@ if ( ! defined( 'WPINC' ) ) {
|
|||||||
// Load the main plugin class.
|
// Load the main plugin class.
|
||||||
require_once plugin_dir_path( __FILE__ ) . 'includes/class-plugin.php';
|
require_once plugin_dir_path( __FILE__ ) . 'includes/class-plugin.php';
|
||||||
|
|
||||||
|
// Initialize the plugin and store the instance in a global variable.
|
||||||
|
$wpst_plugin = new WPALLSTARS\PluginStarterTemplate\Plugin( __FILE__, '0.1.10' );
|
||||||
|
|
||||||
// Initialize the plugin.
|
// Initialize the plugin.
|
||||||
new WPALLSTARS\PluginStarterTemplate\Plugin( __FILE__, '0.1.10' );
|
$wpst_plugin->init();
|
||||||
|
|||||||
Reference in New Issue
Block a user