Skip to content

Commit

Permalink
Tests: Add tests for AspireUpdate\Branding.
Browse files Browse the repository at this point in the history
  • Loading branch information
costdev committed Nov 7, 2024
1 parent a0dda62 commit 7ec63d7
Show file tree
Hide file tree
Showing 4 changed files with 313 additions and 0 deletions.
104 changes: 104 additions & 0 deletions tests/Branding/Branding_AdminEnqueueScriptsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/**
* Class Branding_AdminEnqueueScriptsTest
*
* @package AspireUpdate
*/

/**
* Tests for Branding::admin_enqueue_scripts()
*
* @covers \AspireUpdate\Branding::admin_enqueue_scripts
*/
class Branding_AdminEnqueueScriptsTest extends WP_UnitTestCase {
/**
* Dequeue the stylesheet after each test runs.
*
* @return void
*/
public function tear_down() {
wp_dequeue_style( 'aspire_update_settings_css' );
}

/**
* Test that the stylesheet is enqueued on certain screens.
*
* @dataProvider data_hooks
*
* @param string $hook The current screen's hook.
*/
public function test_should_enqueue_style_on_certain_screens( $hook ) {
$branding = new AspireUpdate\Branding();
$branding->admin_enqueue_scripts( $hook );
$this->assertTrue( wp_style_is( 'aspire_update_settings_css' ) );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_hooks() {
return self::text_array_to_dataprovider(
[
'update-core.php',
'plugins.php',
'plugin-install.php',
'themes.php',
'theme-install.php',
]
);
}

/**
* Test that the stylesheet is not enqueued on adjacent screens.
*
* @dataProvider data_adjacent_screens
*
* @param string $hook The current screen's hook.
*/
public function test_should_not_enqueue_style_on_adjacent_screens( $hook ) {
$branding = new AspireUpdate\Branding();
$branding->admin_enqueue_scripts( $hook );
$this->assertFalse( wp_style_is( 'aspire_update_settings_css' ) );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_adjacent_screens() {
return self::text_array_to_dataprovider(
[
'index.php',
'nav-menus.php',
'plugin-editor.php',
]
);
}

/**
* Test that the stylesheet is not enqueued when there is no screen.
*/
public function test_should_not_enqueue_style_when_there_is_no_screen() {
$branding = new AspireUpdate\Branding();
$branding->admin_enqueue_scripts( '' );
$this->assertFalse( wp_style_is( 'aspire_update_settings_css' ) );
}

/**
* Test that the stylesheet is not enqueued when AP_REMOVE_UI is set to true.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_should_not_enqueue_style_when_ap_remove_ui_is_true() {
// Prevent the notice from being displayed.
define( 'AP_REMOVE_UI', true );

$branding = new AspireUpdate\Branding();
$branding->admin_enqueue_scripts( 'plugins.php' );
$this->assertFalse( wp_style_is( 'aspire_update_settings_css' ) );
}
}
67 changes: 67 additions & 0 deletions tests/Branding/Branding_ConstructTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Class Branding_ConstructTest
*
* @package AspireUpdate
*/

/**
* Tests for Branding::__construct()
*
* @covers \AspireUpdate\Branding::__construct
*/
class Branding_ConstructTest extends WP_UnitTestCase {
/**
* Test that hooks are added when API rewriting is enabled.
*
* @dataProvider data_hooks_and_methods
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*
* @string $hook The hook's name.
* @string $method The method to hook.
*/
public function test_should_add_hooks( $hook, $method ) {
define( 'AP_ENABLE', true );

$branding = new AspireUpdate\Branding();
$this->assertIsInt( has_action( $hook, [ $branding, $method ] ) );
}

/**
* Test that hooks are not added when API rewriting is disabled.
*
* @dataProvider data_hooks_and_methods
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*
* @string $hook The hook's name.
* @string $method The method to hook.
*/
public function test_should_not_add_hooks( $hook, $method ) {
define( 'AP_ENABLE', false );

$branding = new AspireUpdate\Branding();
$this->assertFalse( has_action( $hook, [ $branding, $method ] ) );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_hooks_and_methods() {
return [
'admin_notices -> output_admin_notice' => [
'hook' => 'admin_notices',
'method' => 'output_admin_notice',
],
'admin_enqueue_scripts -> admin_enqueue_scripts' => [
'hook' => 'admin_enqueue_scripts',
'method' => 'admin_enqueue_scripts',
],
];
}
}
23 changes: 23 additions & 0 deletions tests/Branding/Branding_GetInstanceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Class Branding_GetInstanceTest
*
* @package AspireUpdate
*/

/**
* Tests for Branding::get_instance()
*
* @covers \AspireUpdate\Branding::get_instance
*/
class Branding_GetInstanceTest extends WP_UnitTestCase {
/**
* Test that the same instance is retrieved.
*/
public function test_should_get_the_same_instance() {
$this->assertSame(
AspireUpdate\Branding::get_instance(),
AspireUpdate\Branding::get_instance()
);
}
}
119 changes: 119 additions & 0 deletions tests/Branding/Branding_OutputAdminNoticeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php
/**
* Class Branding_OutputAdminNoticeTest
*
* @package AspireUpdate
*/

/**
* Tests for Branding::output_admin_notice()
*
* @covers \AspireUpdate\Branding::output_admin_notice
*/
class Branding_OutputAdminNoticeTest extends WP_UnitTestCase {
/**
* Test that the expected admin notice is output.
*
* @dataProvider data_screen_specific_messages
*
* @param string $hook The current screen's hook.
* @param string $expected The expected substring to find.
*/
public function test_should_output_admin_notice( $hook, $expected ) {
set_current_screen( $hook );

$branding = new AspireUpdate\Branding();
$this->assertStringContainsString( $expected, get_echo( [ $branding, 'output_admin_notice' ] ) );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_screen_specific_messages() {
return [
'update-core.php' => [
'hook' => 'update-core.php',
'expected' => 'WordPress, plugin, theme and translation updates',
],
'plugins.php' => [
'hook' => 'plugins.php',
'expected' => 'plugin updates',
],
'plugin-install.php' => [
'hook' => 'plugin-install.php',
'expected' => 'plugin updates',
],
'themes.php' => [
'hook' => 'themes.php',
'expected' => 'theme updates',
],
'theme-install.php' => [
'hook' => 'theme-install.php',
'expected' => 'theme updates',
],
];
}

/**
* Test that no admin notice is output on adjacent screens.
*
* @dataProvider data_adjacent_screens
*
* @param string $hook The current screen's hook.
*/
public function test_should_not_output_notice_on_adjacent_screens( $hook ) {
set_current_screen( $hook );

$branding = new AspireUpdate\Branding();
$this->assertSame( '', get_echo( [ $branding, 'output_admin_notice' ] ) );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_adjacent_screens() {
return self::text_array_to_dataprovider(
[
'index.php',
'nav-menus.php',
'plugin-editor.php',
]
);
}

/**
* Test that no admin notice is output when there is no screen.
*/
public function test_should_not_output_notice_when_there_is_no_screen() {
global $current_screen;
$current_screen_backup = $current_screen;
unset( $current_screen );

$branding = new AspireUpdate\Branding();
$actual = get_echo( [ $branding, 'output_admin_notice' ] );
$current_screen = $current_screen_backup;

$this->assertSame( '', $actual );
}

/**
* Test that no admin notice is output when AP_REMOVE_UI is set to true.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_should_not_output_notice_when_ap_remove_ui_is_true() {
// Set to a screen that should display an admin notice.
set_current_screen( 'plugins.php' );

// Prevent the notice from being displayed.
define( 'AP_REMOVE_UI', true );

$branding = new AspireUpdate\Branding();
$this->assertSame( '', get_echo( [ $branding, 'output_admin_notice' ] ) );
}
}

0 comments on commit 7ec63d7

Please sign in to comment.