Skip to content

Commit

Permalink
test(phpunit): add new test for checking registration menus correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
rezaelahidev committed Dec 21, 2024
1 parent f74c208 commit a6a0848
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions packages/blockera-admin/php/tests/AdminProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Blockera\Admin\Tests;

use Blockera\Setup\Blockera;
use Blockera\Admin\Providers\AdminProvider;

class AdminProviderTest extends \Blockera\Dev\PHPUnit\AppTestCase
{

protected static ?AdminProvider $provider = null;

public function set_up(): void
{
parent::set_up();

// Login as admin user
$user_id = self::factory()->user->create(['role' => 'administrator']);
wp_set_current_user($user_id);

// Mock WordPress functions.
require_once ABSPATH . '/wp-admin/includes/plugin.php'; // Ensure you have WordPress functions available.

self::$provider = new AdminProvider(new Blockera());
}

/**
* Test if the menu page is registered.
*
* @test
* @return void
*/
public function itShouldRegisterMenuPage(): void
{
global $menu, $submenu;
self::$provider->boot();

do_action('admin_menu');

$registeredMenus = array_column($menu, 2);
$baseMenuSlug = blockera_core_config('menu.menu_slug');

// Check if the base menu is registered.
$this->assertContains($baseMenuSlug, $registeredMenus);

$expectedSubmenus = array_column(blockera_core_config('menu.submenus'), 'menu_slug');

$submenu_found = false;

// Assert the submenu was registered under the correct parent slug.
if (isset($submenu[$baseMenuSlug])) {
foreach ($submenu[$baseMenuSlug] as $submenu_item) {
foreach ($expectedSubmenus as $expected_submenu) {
if ($submenu_item[2] === $expected_submenu) {
$submenu_found = true;
break;
}
}
}
}

$this->assertTrue($submenu_found, 'Submenu was not registered correctly.');
}
}

0 comments on commit a6a0848

Please sign in to comment.