Skip to content

Commit

Permalink
Add unit test for add_clean_saas_menu_item()
Browse files Browse the repository at this point in the history
  • Loading branch information
remyperona committed Apr 19, 2024
1 parent 3b8f3c0 commit e246da6
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

return [
'testShouldReturnNullWhenLocalEnvironment' => [
'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',
],
],
];
92 changes: 92 additions & 0 deletions tests/Unit/inc/Engine/Saas/Admin/AdminBar/addCleanSaasMenuItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace WP_Rocket\Tests\Unit\inc\Engine\Saas\Admin\AdminBar;

use Mockery;
use Brain\Monkey\Functions;
use WP_Admin_Bar;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\Common\Context\ContextInterface;
use WP_Rocket\Engine\Saas\Admin\AdminBar;
use WP_Rocket\Tests\Unit\TestCase;

/**
* @covers \WP_Rocket\Engine\Saas\Admin\AdminBar::add_clean_saas_menu_item
* @group Saas
*/
class Test_AddCleanSaasMenuItem extends TestCase {
private $admin_bar;
private $options;
private $atf_context;
private $rucss_url_context;
private $wp_admin_bar;

public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();

require_once WP_ROCKET_TESTS_FIXTURES_DIR . '/WP_Admin_Bar.php';
}

protected function setUp(): void {
parent::setUp();

$this->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( '&', '&amp;', "{$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
);
}
}

0 comments on commit e246da6

Please sign in to comment.