diff --git a/inc/Engine/Saas/Admin/AdminBar.php b/inc/Engine/Saas/Admin/AdminBar.php index 7690fa8611..cc513e67e2 100644 --- a/inc/Engine/Saas/Admin/AdminBar.php +++ b/inc/Engine/Saas/Admin/AdminBar.php @@ -106,23 +106,13 @@ public function add_clean_saas_menu_item( $wp_admin_bar ) { * @return void */ public function add_clean_url_menu_item( WP_Admin_Bar $wp_admin_bar ) { - global $pagenow, $post; + global $post; if ( 'local' === wp_get_environment_type() ) { return; } - if ( - is_admin() - && - ( - ! $post - || - 'post.php' !== $pagenow - || - ! isset( $_GET['action'], $_GET['post'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended - ) - ) { + if ( is_admin() ) { return; } @@ -176,7 +166,7 @@ public function add_clean_url_menu_item( WP_Admin_Bar $wp_admin_bar ) { $wp_admin_bar->add_menu( [ 'parent' => 'wp-rocket', - 'id' => 'clear-saaas-url', + 'id' => 'clear-saas-url', 'title' => $title, 'href' => wp_nonce_url( admin_url( 'admin-post.php?action=' . $action . $referer ), $action ), ] 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/Fixtures/inc/Engine/Saas/Admin/AdminBar/addCleanUrlMenuItem.php b/tests/Fixtures/inc/Engine/Saas/Admin/AdminBar/addCleanUrlMenuItem.php new file mode 100644 index 0000000000..985a06b1a3 --- /dev/null +++ b/tests/Fixtures/inc/Engine/Saas/Admin/AdminBar/addCleanUrlMenuItem.php @@ -0,0 +1,94 @@ + [ + 'config' => [ + 'environment' => 'local', + 'is_admin' => false, + 'post' => (object) [ + 'post_type' => 'post', + 'post_status' => 'publish', + ], + 'can_display_options' => true, + 'atf_context' => true, + 'rucss_context' => true, + ], + 'expected' => null, + ], + 'testShouldReturnNullWhenAdmin' => [ + 'config' => [ + 'environment' => 'production', + 'is_admin' => true, + 'post' => (object) [ + 'post_type' => 'post', + 'post_status' => 'publish', + ], + 'can_display_options' => true, + 'atf_context' => true, + 'rucss_context' => true, + ], + 'expected' => null, + ], + 'testShouldReturnNullWhenOptionsNotDisplayed' => [ + 'config' => [ + 'environment' => 'production', + 'is_admin' => false, + 'post' => (object) [ + 'post_type' => 'post', + 'post_status' => 'draft', + ], + 'can_display_options' => false, + 'atf_context' => true, + 'rucss_context' => true, + ], + 'expected' => null, + ], + 'testShouldReturnNullWhenNotAllowed' => [ + 'config' => [ + 'environment' => 'production', + 'is_admin' => false, + 'post' => (object) [ + 'post_type' => 'post', + 'post_status' => 'publish', + ], + 'can_display_options' => true, + 'atf_context' => false, + 'rucss_context' => false, + ], + 'expected' => null, + ], + 'testShouldAddItemWithDefaultTitle' => [ + 'config' => [ + 'environment' => 'production', + 'is_admin' => false, + 'post' => (object) [ + 'post_type' => 'post', + 'post_status' => 'publish', + ], + 'can_display_options' => true, + 'atf_context' => true, + 'rucss_context' => false, + ], + 'expected' => [ + 'id' => 'clear-saas-url', + 'title' => 'Clear Critical Images of this URL', + ], + ], + 'testShouldAddItemWithRUCSSTitle' => [ + 'config' => [ + 'environment' => 'production', + 'is_admin' => false, + 'post' => (object) [ + 'post_type' => 'post', + 'post_status' => 'publish', + ], + 'can_display_options' => true, + 'atf_context' => true, + 'rucss_context' => true, + ], + 'expected' => [ + 'id' => 'clear-saas-url', + 'title' => 'Clear Used CSS of this URL', + ], + ], +]; 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 + ); + } +} diff --git a/tests/Unit/inc/Engine/Saas/Admin/AdminBar/addCleanUrlMenuItem.php b/tests/Unit/inc/Engine/Saas/Admin/AdminBar/addCleanUrlMenuItem.php new file mode 100644 index 0000000000..557e1f39dc --- /dev/null +++ b/tests/Unit/inc/Engine/Saas/Admin/AdminBar/addCleanUrlMenuItem.php @@ -0,0 +1,99 @@ +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(); + } + + protected function tearDown(): void { + unset( $GLOBALS['post'] ); + + parent::tearDown(); + } + + /** + * @dataProvider configTestData + */ + public function testShouldDoExpected( $config, $expected ) { + Functions\when( 'wp_get_environment_type' ) + ->justReturn( $config['environment'] ); + Functions\when( 'is_admin' ) + ->justReturn( $config['is_admin'] ); + + $GLOBALS['post'] = $config['post']; + + Functions\when( 'rocket_can_display_options' ) + ->justReturn( $config['can_display_options'] ); + + $this->atf_context->shouldReceive( 'is_allowed' ) + ->andReturn( $config['atf_context'] ); + + $this->rucss_url_context->shouldReceive( 'is_allowed' ) + ->andReturn( $config['rucss_context'] ); + + 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_url_menu_item( $this->wp_admin_bar ); + + $node = $this->wp_admin_bar->get_node( 'clear-saas-url' ); + + if ( null === $expected ) { + $this->assertNull( $node ); + return; + } + + $this->assertSame( + $expected['id'], + $node->id + ); + + $this->assertSame( + $expected['title'], + $node->title + ); + } +}