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..a3ceaec2 100644 --- a/tests/test-mslsplugin.php +++ b/tests/test-mslsplugin.php @@ -9,30 +9,48 @@ 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(); + + $options = \Mockery::mock( MslsOptions::class ); + $options->activate_autocomplete = true; - $this->assertIsBool( $this->get_test()->custom_enqueue() ); + $test = new MslsPlugin( $options ); + + $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' )->andReturnFalse(); + + $test = new MslsPlugin( $options ); - $this->assertIsBool( $this->get_test()->init_widget() ); + $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->assertFalse( $test->init_widget() ); } /** @@ -41,7 +59,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 +82,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() ); } /**