From 5c25763914b68e5e20455f269c53a507371b52ac Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Wed, 17 Jul 2024 16:44:20 +0400 Subject: [PATCH] Fix unit tests --- .../navigation/test/use-navigation-menu.js | 69 +++++++++++++++---- .../src/hooks/use-resource-permissions.ts | 10 ++- 2 files changed, 62 insertions(+), 17 deletions(-) diff --git a/packages/block-library/src/navigation/test/use-navigation-menu.js b/packages/block-library/src/navigation/test/use-navigation-menu.js index eb7e90aff22d1a..557fd30ac24a59 100644 --- a/packages/block-library/src/navigation/test/use-navigation-menu.js +++ b/packages/block-library/src/navigation/test/use-navigation-menu.js @@ -63,37 +63,76 @@ function resolveRecords( registry, menus ) { function resolveReadPermission( registry, allowed ) { const dispatch = registry.dispatch( coreStore ); - dispatch.receiveUserPermission( 'create/navigation', allowed ); - dispatch.startResolution( 'canUser', [ 'read', 'navigation' ] ); - dispatch.finishResolution( 'canUser', [ 'read', 'navigation' ] ); + dispatch.receiveUserPermission( 'read/postType/wp_navigation', allowed ); + dispatch.startResolution( 'canUser', [ + 'read', + { kind: 'postType', name: 'wp_navigation', id: undefined }, + ] ); + dispatch.finishResolution( 'canUser', [ + 'read', + { kind: 'postType', name: 'wp_navigation', id: undefined }, + ] ); } function resolveReadRecordPermission( registry, ref, allowed ) { const dispatch = registry.dispatch( coreStore ); - dispatch.receiveUserPermission( 'create/navigation', allowed ); - dispatch.startResolution( 'canUser', [ 'read', 'navigation', ref ] ); - dispatch.finishResolution( 'canUser', [ 'read', 'navigation', ref ] ); + dispatch.receiveUserPermission( + `read/postType/wp_navigation/${ ref }`, + allowed + ); + dispatch.startResolution( 'canUser', [ + 'read', + { kind: 'postType', name: 'wp_navigation', id: ref }, + ] ); + dispatch.finishResolution( 'canUser', [ + 'read', + { kind: 'postType', name: 'wp_navigation', id: ref }, + ] ); } function resolveCreatePermission( registry, allowed ) { const dispatch = registry.dispatch( coreStore ); - dispatch.receiveUserPermission( 'create/navigation', allowed ); - dispatch.startResolution( 'canUser', [ 'create', 'navigation' ] ); - dispatch.finishResolution( 'canUser', [ 'create', 'navigation' ] ); + dispatch.receiveUserPermission( 'create/postType/wp_navigation', allowed ); + dispatch.startResolution( 'canUser', [ + 'create', + { kind: 'postType', name: 'wp_navigation' }, + ] ); + dispatch.finishResolution( 'canUser', [ + 'create', + { kind: 'postType', name: 'wp_navigation' }, + ] ); } function resolveUpdatePermission( registry, ref, allowed ) { const dispatch = registry.dispatch( coreStore ); - dispatch.receiveUserPermission( `update/navigation/${ ref }`, allowed ); - dispatch.startResolution( 'canUser', [ 'update', 'navigation', ref ] ); - dispatch.finishResolution( 'canUser', [ 'update', 'navigation', ref ] ); + dispatch.receiveUserPermission( + `update/postType/wp_navigation/${ ref }`, + allowed + ); + dispatch.startResolution( 'canUser', [ + 'update', + { kind: 'postType', name: 'wp_navigation', id: ref }, + ] ); + dispatch.finishResolution( 'canUser', [ + 'update', + { kind: 'postType', name: 'wp_navigation', id: ref }, + ] ); } function resolveDeletePermission( registry, ref, allowed ) { const dispatch = registry.dispatch( coreStore ); - dispatch.receiveUserPermission( `delete/navigation/${ ref }`, allowed ); - dispatch.startResolution( 'canUser', [ 'delete', 'navigation', ref ] ); - dispatch.finishResolution( 'canUser', [ 'delete', 'navigation', ref ] ); + dispatch.receiveUserPermission( + `delete/postType/wp_navigation/${ ref }`, + allowed + ); + dispatch.startResolution( 'canUser', [ + 'delete', + { kind: 'postType', name: 'wp_navigation', id: ref }, + ] ); + dispatch.finishResolution( 'canUser', [ + 'delete', + { kind: 'postType', name: 'wp_navigation', id: ref }, + ] ); } describe( 'useNavigationMenus', () => { diff --git a/packages/core-data/src/hooks/use-resource-permissions.ts b/packages/core-data/src/hooks/use-resource-permissions.ts index 1f97ed5b45aa5e..2e93583eee8f85 100644 --- a/packages/core-data/src/hooks/use-resource-permissions.ts +++ b/packages/core-data/src/hooks/use-resource-permissions.ts @@ -126,10 +126,16 @@ export default function useResourcePermissions< IdType = void >( return useQuerySelect( ( resolve ) => { + const isEntity = typeof resource === 'object'; + const hasId = isEntity ? !! resource.id : !! id; const { canUser } = resolve( coreStore ); - const create = canUser( 'create', resource ); + const create = canUser( + 'create', + isEntity + ? { kind: resource.kind, name: resource.name } + : resource + ); - const hasId = typeof resource === 'object' ? !! resource.id : !! id; if ( ! hasId ) { const read = canUser( 'read', resource );