From 19cddc53f7f032219ed86a009838a4c9342c02ba Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Wed, 17 Jan 2024 15:22:06 +0100 Subject: [PATCH 1/2] Style loading reviewed --- includes/MslsPlugin.php | 6 +++-- tests/test-mslsplugin.php | 48 ++++++++++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/includes/MslsPlugin.php b/includes/MslsPlugin.php index 3c3f89d9..d8d41a0b 100644 --- a/includes/MslsPlugin.php +++ b/includes/MslsPlugin.php @@ -61,9 +61,11 @@ public static function init() { \lloc\Msls\ContentImport\Service::instance()->register(); - if ( is_admin() ) { + if ( is_admin() || is_admin_bar_showing() ) { add_action( 'admin_enqueue_scripts', [ $obj, 'custom_enqueue' ] ); + } + if ( is_admin() ) { add_action( 'admin_menu', [ MslsAdmin::class, 'init' ] ); add_action( 'load-post.php', [ MslsMetaBox::class, 'init' ] ); add_action( 'load-post-new.php', [ MslsMetaBox::class, 'init' ] ); @@ -261,7 +263,7 @@ public function admin_bar_init() { /** * Loads styles and some js if needed * - * The method returns true if JS is loaded or false if not + * The method returns true if the autocomplete-option is activated, false otherwise. * * @return boolean */ diff --git a/tests/test-mslsplugin.php b/tests/test-mslsplugin.php index d3acdc2b..7497398b 100644 --- a/tests/test-mslsplugin.php +++ b/tests/test-mslsplugin.php @@ -9,21 +9,28 @@ class WP_Test_MslsPlugin extends Msls_UnitTestCase { - function get_test() { + function test_admin_menu_without_autocomplete(): void { + Functions\expect( 'wp_enqueue_style' )->twice(); + Functions\expect( 'plugins_url' )->twice()->andReturn( 'https://lloc.de/wp-content/plugins' ); + $options = \Mockery::mock( MslsOptions::class ); - $options->shouldReceive( 'is_excluded' )->andReturn( false ); - return new MslsPlugin( $options ); + $test = new MslsPlugin( $options ); + + $this->assertFalse( $test->custom_enqueue() ); } - /** - * Verify the static init-method - */ - function test_admin_menu(): void { - Functions\when( 'wp_enqueue_style' )->returnArg(); - Functions\when( 'plugins_url' )->justReturn( 'https://lloc.de/wp-content/plugins' ); + function test_admin_menu_with_autocomplete(): void { + Functions\expect( 'wp_enqueue_style' )->twice(); + Functions\expect( 'plugins_url' )->times( 3 )->andReturn( 'https://lloc.de/wp-content/plugins' ); + Functions\expect( 'wp_enqueue_script' )->once(); - $this->assertIsBool( $this->get_test()->custom_enqueue() ); + $options = \Mockery::mock( MslsOptions::class ); + $options->activate_autocomplete = true; + + $test = new MslsPlugin( $options ); + + $this->assertTrue( $test->custom_enqueue() ); } /** @@ -32,7 +39,12 @@ function test_admin_menu(): void { function test_init_widget(): void { Functions\when( 'register_widget' )->justReturn( true ); - $this->assertIsBool( $this->get_test()->init_widget() ); + $options = \Mockery::mock( MslsOptions::class ); + $options->shouldReceive( 'is_excluded' )->andReturn( false ); + + $test = new MslsPlugin( $options ); + + $this->assertIsBool( $test->init_widget() ); } /** @@ -41,7 +53,12 @@ function test_init_widget(): void { function test_init_i18n_support(): void { Functions\when( 'load_plugin_textdomain' )->justReturn( true ); - $this->assertIsBool( $this->get_test()->init_i18n_support() ); + $options = \Mockery::mock( MslsOptions::class ); + $options->shouldReceive( 'is_excluded' )->andReturn( false ); + + $test = new MslsPlugin( $options ); + + $this->assertIsBool( $test->init_i18n_support() ); } /** @@ -59,7 +76,12 @@ function test_message_handler(): void { function test_uninstall(): void { Functions\when( 'delete_option' )->justReturn( false ); - $this->assertIsBool( $this->get_test()->uninstall() ); + $options = \Mockery::mock( MslsOptions::class ); + $options->shouldReceive( 'is_excluded' )->andReturn( false ); + + $test = new MslsPlugin( $options ); + + $this->assertIsBool( $test->uninstall() ); } /** From 7e69543278e2093736aee39a9a8a3fdee89c8c9a Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Wed, 17 Jan 2024 15:28:11 +0100 Subject: [PATCH 2/2] Tests enhanced --- tests/test-mslsplugin.php | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/tests/test-mslsplugin.php b/tests/test-mslsplugin.php index 7497398b..a3ceaec2 100644 --- a/tests/test-mslsplugin.php +++ b/tests/test-mslsplugin.php @@ -33,18 +33,24 @@ function test_admin_menu_with_autocomplete(): void { $this->assertTrue( $test->custom_enqueue() ); } - /** - * Verify the static init_widget-method - */ - function test_init_widget(): void { - Functions\when( 'register_widget' )->justReturn( true ); + function test_init_widget_not_excluded(): void { + Functions\expect( 'register_widget' )->once(); $options = \Mockery::mock( MslsOptions::class ); - $options->shouldReceive( 'is_excluded' )->andReturn( false ); + $options->shouldReceive( 'is_excluded' )->andReturnFalse(); + + $test = new MslsPlugin( $options ); + + $this->assertTrue( $test->init_widget() ); + } + + function test_init_widget_excluded(): void { + $options = \Mockery::mock( MslsOptions::class ); + $options->shouldReceive( 'is_excluded' )->andReturnTrue(); $test = new MslsPlugin( $options ); - $this->assertIsBool( $test->init_widget() ); + $this->assertFalse( $test->init_widget() ); } /**