From e246da6c7d2e169e680e696767ce6a8f1398f731 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Fri, 19 Apr 2024 16:13:25 -0400 Subject: [PATCH] Add unit test for add_clean_saas_menu_item() --- .../Admin/AdminBar/addCleanSaasMenuItem.php | 50 ++++++++++ .../Admin/AdminBar/addCleanSaasMenuItem.php | 92 +++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 tests/Fixtures/inc/Engine/Saas/Admin/AdminBar/addCleanSaasMenuItem.php create mode 100644 tests/Unit/inc/Engine/Saas/Admin/AdminBar/addCleanSaasMenuItem.php diff --git a/tests/Fixtures/inc/Engine/Saas/Admin/AdminBar/addCleanSaasMenuItem.php b/tests/Fixtures/inc/Engine/Saas/Admin/AdminBar/addCleanSaasMenuItem.php new file mode 100644 index 0000000000..ae980ac87e --- /dev/null +++ b/tests/Fixtures/inc/Engine/Saas/Admin/AdminBar/addCleanSaasMenuItem.php @@ -0,0 +1,50 @@ + [ + 'config' => [ + 'environment' => 'local', + 'is_admin' => false, + 'atf_context' => false, + 'remove_unused_css' => 0, + 'current_user_can' => true, + ], + 'expected' => null, + ], + 'testShouldReturnNullWhenNotAdmin' => [ + 'config' => [ + 'environment' => 'production', + 'is_admin' => false, + 'atf_context' => false, + 'remove_unused_css' => 0, + 'current_user_can' => true, + ], + 'expected' => null, + ], + 'testShouldAddItemWithDefaultTitle' => [ + 'config' => [ + 'environment' => 'production', + 'is_admin' => true, + 'atf_context' => true, + 'remove_unused_css' => 0, + 'current_user_can' => true, + ], + 'expected' => [ + 'id' => 'clean-saas', + 'title' => 'Clear Critical Images', + ], + ], + 'testShouldAddItemWithRUCSSTitle' => [ + 'config' => [ + 'environment' => 'production', + 'is_admin' => true, + 'atf_context' => true, + 'remove_unused_css' => 1, + 'current_user_can' => true, + ], + 'expected' => [ + 'id' => 'clean-saas', + 'title' => 'Clear Used CSS', + ], + ], +]; diff --git a/tests/Unit/inc/Engine/Saas/Admin/AdminBar/addCleanSaasMenuItem.php b/tests/Unit/inc/Engine/Saas/Admin/AdminBar/addCleanSaasMenuItem.php new file mode 100644 index 0000000000..12012ccf19 --- /dev/null +++ b/tests/Unit/inc/Engine/Saas/Admin/AdminBar/addCleanSaasMenuItem.php @@ -0,0 +1,92 @@ +options = Mockery::mock( Options_Data::class ); + $this->atf_context = Mockery::mock( ContextInterface::class ); + $this->rucss_url_context = Mockery::mock( ContextInterface::class ); + $this->admin_bar = new AdminBar( $this->options, $this->atf_context, $this->rucss_url_context, '' ); + $this->wp_admin_bar = new WP_Admin_Bar(); + + $this->stubTranslationFunctions(); + } + + /** + * @dataProvider configTestData + */ + public function testShouldDoExpected( $config, $expected ) { + Functions\when( 'wp_get_environment_type' ) + ->justReturn( $config['environment'] ); + Functions\when( 'is_admin' ) + ->justReturn( $config['is_admin'] ); + + $this->atf_context->shouldReceive( 'is_allowed' ) + ->andReturn( $config['atf_context'] ); + + $this->options->shouldReceive( 'get' ) + ->with( 'remove_unused_css', 0 ) + ->andReturn( $config['remove_unused_css'] ); + + Functions\when( 'current_user_can' ) + ->justReturn( $config['current_user_can'] ); + + Functions\when( 'wp_nonce_url' )->alias( + function ( $url ) { + return str_replace( '&', '&', "{$url}&_wpnonce=123456" ); + } + ); + + Functions\when( 'admin_url' )->alias( + function ( $path ) { + return "http://example.org/wp-admin/{$path}"; + } + ); + + $this->admin_bar->add_clean_saas_menu_item( $this->wp_admin_bar ); + + $node = $this->wp_admin_bar->get_node( 'clean-saas' ); + + if ( null === $expected ) { + $this->assertNull( $node ); + return; + } + + $this->assertSame( + $expected['id'], + $node->id + ); + + $this->assertSame( + $expected['title'], + $node->title + ); + } +}