-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit test for add_clean_saas_menu_item()
- Loading branch information
1 parent
3b8f3c0
commit e246da6
Showing
2 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
tests/Fixtures/inc/Engine/Saas/Admin/AdminBar/addCleanSaasMenuItem.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
92
tests/Unit/inc/Engine/Saas/Admin/AdminBar/addCleanSaasMenuItem.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( '&', '&', "{$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 | ||
); | ||
} | ||
} |