WIP: 新增基础数据一级目录 #162
@ -20,7 +20,34 @@ class ShowInAdmin
|
||||
*/
|
||||
public function handle(Event $event): void
|
||||
{
|
||||
$this->renderSections($event->menu, self::groupPayables(SidebarPermissions::navigationFor(user())));
|
||||
$this->renderSections($event->menu, self::groupBasicData(self::groupPayables(SidebarPermissions::navigationFor(user()))));
|
||||
}
|
||||
|
||||
public static function groupBasicData(array $navigation): array
|
||||
{
|
||||
$keys = ['order-data', 'refunds'];
|
||||
$children = array_values(array_filter($navigation, static fn (array $section): bool => in_array($section['slug'], $keys, true)));
|
||||
if ($children === []) {
|
||||
return $navigation;
|
||||
}
|
||||
|
||||
$group = ['slug' => 'basic-data', 'title' => '基础数据', 'icon' => 'folder', 'sections' => $children];
|
||||
$hasDirectory = in_array('basic-data', array_column($navigation, 'slug'), true);
|
||||
$result = [];
|
||||
foreach ($navigation as $section) {
|
||||
if ($section['slug'] === 'basic-data') {
|
||||
$result[] = array_replace($section, ['sections' => $children]);
|
||||
} elseif (in_array($section['slug'], $keys, true)) {
|
||||
if (! $hasDirectory) {
|
||||
$result[] = $group;
|
||||
$hasDirectory = true;
|
||||
}
|
||||
} else {
|
||||
$result[] = $section;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function groupPayables(array $navigation): array
|
||||
|
||||
@ -289,6 +289,12 @@ $navigation = [
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
'slug' => 'basic-data',
|
||||
'title' => '基础数据',
|
||||
'icon' => 'folder',
|
||||
'pages' => [],
|
||||
],
|
||||
[
|
||||
'slug' => 'product-inventory',
|
||||
'title' => '商品及库存管理',
|
||||
|
||||
46
tests/Unit/View/BasicDataNavigationTest.php
Normal file
46
tests/Unit/View/BasicDataNavigationTest.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\View;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use App\Listeners\Menu\ShowInAdmin;
|
||||
|
||||
require_once dirname(__DIR__, 3) . '/app/Listeners/Menu/ShowInAdmin.php';
|
||||
|
||||
class BasicDataNavigationTest extends TestCase
|
||||
{
|
||||
public function test_group_moves_only_order_data_and_refunds_without_changing_pages(): void
|
||||
{
|
||||
$navigation = require dirname(__DIR__, 3) . '/config/business_navigation.php';
|
||||
$grouped = ShowInAdmin::groupBasicData($navigation);
|
||||
$slugs = array_column($grouped, 'slug');
|
||||
$group = $grouped[array_search('basic-data', $slugs, true)];
|
||||
$this->assertSame(['order-data', 'refunds'], array_column($group['sections'], 'slug'));
|
||||
$this->assertNotContains('order-data', $slugs);
|
||||
$this->assertNotContains('refunds', $slugs);
|
||||
foreach ($group['sections'] as $section) {
|
||||
$this->assertSame($navigation[array_search($section['slug'], array_column($navigation, 'slug'), true)], $section);
|
||||
}
|
||||
}
|
||||
|
||||
public function test_group_contains_only_authorized_sections_even_without_directory_permission(): void
|
||||
{
|
||||
$allowed = [['slug' => 'refunds', 'title' => '退款', 'pages' => [['slug' => 'non-jingxi-refund-data']]]];
|
||||
$grouped = ShowInAdmin::groupBasicData($allowed);
|
||||
$this->assertSame($allowed, $grouped[0]['sections']);
|
||||
$this->assertSame([], ShowInAdmin::groupBasicData([]));
|
||||
}
|
||||
|
||||
public function test_basic_data_is_an_empty_top_level_directory_before_invoice_management(): void
|
||||
{
|
||||
$navigation = require dirname(__DIR__, 3) . '/config/business_navigation.php';
|
||||
$slugs = array_column($navigation, 'slug');
|
||||
$index = array_search('basic-data', $slugs, true);
|
||||
|
||||
$this->assertNotFalse($index);
|
||||
$this->assertSame('基础数据', $navigation[$index]['title']);
|
||||
$this->assertSame([], $navigation[$index]['pages']);
|
||||
$this->assertSame('invoice-management', $slugs[$index + 1]);
|
||||
$this->assertCount(1, array_keys($slugs, 'basic-data', true));
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user