Skip to content

Commit

Permalink
PHPUnit test updates. (#1)
Browse files Browse the repository at this point in the history
* PHPUnit test updates.

* Testing setting current screen in tests.

* Set user in enqueue scripts test.

* Revert helper method in scripts test.

* Checking for null.

* BE sure the plugin aware object is set.

* Tests

* Skipping.
  • Loading branch information
thefrosty authored Mar 6, 2019
1 parent c4e5f48 commit 1d08cdc
Showing 1 changed file with 40 additions and 11 deletions.
51 changes: 40 additions & 11 deletions tests/unit/UpgradeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace TheFrosty\WpUpgradeTaskRunner\PhpUnit;

use TheFrosty\WpUpgradeTaskRunner\Upgrade;
use TheFrosty\WpUtilities\Plugin\PluginInterface;

/**
* Class UpgradeTest
Expand All @@ -27,14 +28,23 @@ public function setUp()
{
parent::setUp();

$this->markTestSkipped();
$screens = [
'index.php?page=upgrade-task-runner' => ['base' => 'dashboard', 'id' => 'dashboard'],
];
$this->upgrade = new Upgrade($this->container);
$this->upgrade->setPlugin($this->plugin);
$GLOBALS['hook_suffix'] = $this->getSettingsPage($this->upgrade);
\set_current_screen($this->getSettingsPage($this->upgrade));

$this->admin_user_id = $this->factory()->user->create([
'role' => 'administrator',
]);
$this->author_user_id = $this->factory()->user->create([
'role' => 'author',
]);
$this->assertTrue($this->upgrade instanceof Upgrade, 'Upgrade object creation error');
$this->assertTrue($this->upgrade->getPlugin() instanceof PluginInterface, 'Upgrade getPlugin object error');
$this->assertTrue(is_int($this->admin_user_id), 'Admin user not created');
$this->assertTrue(is_int($this->author_user_id), 'Author user not created');
}
Expand All @@ -45,9 +55,10 @@ public function setUp()
public function tearDown()
{
parent::tearDown();
unset($this->upgrade);
unset($this->upgrade, $GLOBALS['hook_suffix']);
\wp_delete_user($this->admin_user_id);
\wp_delete_user($this->author_user_id);
\set_current_screen();
}

/**
Expand Down Expand Up @@ -89,11 +100,13 @@ public function testConstants()
public function testAddDashboardPage()
{
\wp_set_current_user($this->admin_user_id);
$this->upgrade->addHooks();
$this->assertTrue(\property_exists($this->upgrade, 'settings_page'));
$this->assertTrue(\method_exists($this->upgrade, 'addDashboardPage'));
$addDashboardPage = $this->getReflection($this->upgrade)->getMethod('addDashboardPage');
$addDashboardPage->setAccessible(true);
$addDashboardPage->invoke($this->upgrade);
\set_current_screen($this->getSettingsPage($this->upgrade));

$page_url = menu_page_url($this->upgrade::MENU_SLUG, false);
$this->assertNotEmpty($page_url, 'No dashboard page found');
Expand All @@ -105,6 +118,7 @@ public function testAddDashboardPage()
public function testAddDashboardPageWithAdmin()
{
\wp_set_current_user($this->admin_user_id);
$this->upgrade->addHooks();
$this->assertTrue(\property_exists($this->upgrade, 'settings_page'));
$this->assertTrue(\method_exists($this->upgrade, 'addDashboardPage'));
$addDashboardPage = $this->getReflection($this->upgrade)->getMethod('addDashboardPage');
Expand All @@ -125,11 +139,13 @@ public function testAddDashboardPageWithAdmin()
public function testAddDashboardPageWithNonAdmin()
{
\wp_set_current_user($this->author_user_id);
$this->upgrade->addHooks();
$this->assertTrue(\property_exists($this->upgrade, 'settings_page'));
$this->assertTrue(\method_exists($this->upgrade, 'addDashboardPage'));
$addDashboardPage = $this->getReflection($this->upgrade)->getMethod('addDashboardPage');
$addDashboardPage->setAccessible(true);
$addDashboardPage->invoke($this->upgrade);
\set_current_screen($this->getSettingsPage($this->upgrade));

$settingsPage = $this->getReflection($this->upgrade)->getProperty('settings_page');
$settingsPage->setAccessible(true);
Expand All @@ -144,32 +160,45 @@ public function testAddDashboardPageWithNonAdmin()
*/
public function testEnqueueScripts()
{
\wp_set_current_user($this->admin_user_id);
$this->upgrade->addHooks();
$this->assertTrue(\property_exists($this->upgrade, 'settings_page'));
$this->assertTrue(\method_exists($this->upgrade, 'addDashboardPage'));
$this->assertTrue(\method_exists($this->upgrade, 'enqueueScripts'));

$addDashboardPage = $this->getReflection($this->upgrade)->getMethod('addDashboardPage');
$addDashboardPage->setAccessible(true);
$addDashboardPage->invoke($this->upgrade);

$settingsPage = $this->getReflection($this->upgrade)->getProperty('settings_page');
$settingsPage->setAccessible(true);
\set_current_screen($this->getSettingsPage($this->upgrade));

$enqueueScripts = $this->getReflection($this->upgrade)->getMethod('enqueueScripts');
$enqueueScripts->setAccessible(true);
$enqueueScripts->invoke($this->upgrade, $settingsPage->getValue($this->upgrade));
$enqueueScripts->invoke($this->upgrade, $this->getSettingsPage($this->upgrade));

$this->assertTrue(
\wp_script_is('upgrade-task-runner-dialog'),
'Upgrade Task Runner Dialog JS not enqueued'
\wp_script_is('upgrade-task-runner-dialog', 'registered'),
'Upgrade Task Runner Dialog JS not registered'
);
$this->assertTrue(
\wp_script_is('upgrade-task-runner'),
'Upgrade Task Runner JS not enqueued'
\wp_script_is('upgrade-task-runner', 'registered'),
'Upgrade Task Runner JS not registered'
);
$this->assertTrue(
\wp_style_is('upgrade-task-runner'),
'Upgrade Task Runner CSS not enqueued'
\wp_style_is('upgrade-task-runner', 'registered'),
'Upgrade Task Runner CSS not registered'
);
}

private function getSettingsPage(Upgrade $upgrade): string
{
static $value;

if (empty($value)) {
$settings_page = $this->getReflection($upgrade)->getProperty('settings_page');
$settings_page->setAccessible(true);
$value = $settings_page->getValue($upgrade) ?? 'dashboard_page_upgrade-task-runner';
}

return \strval($value);
}
}

0 comments on commit 1d08cdc

Please sign in to comment.