Skip to content

Commit c33efed

Browse files
authored
Merge branch 'trunk' into add/google-sheets-fields-selection
2 parents 00bf209 + 4dae381 commit c33efed

File tree

11 files changed

+158
-25
lines changed

11 files changed

+158
-25
lines changed

inc/Editor/DataBinding/BlockBindings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public static function get_remote_value( array $block_context, array $source_arg
236236
return sprintf( '<span class="rdb-block-label">%s</span> %s', $source_args['label'], $value );
237237
}
238238

239-
return $value;
239+
return strval( $value );
240240
}
241241

242242
public static function loop_block_render_callback( array $attributes, string $content, WP_Block $block ): string {

inc/Validation/Types.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static function button_text(): array {
6161
}
6262

6363
public static function button_url(): array {
64-
return self::generate_primitive_type( 'image_url' );
64+
return self::generate_primitive_type( 'button_url' );
6565
}
6666

6767
/**

src/blocks/remote-data-container/components/InnerBlocks.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import { LoopTemplate } from '@/blocks/remote-data-container/components/loop-tem
55

66
interface InnerBlocksProps {
77
blockConfig: BlockConfig;
8-
getInnerBlocks: (
9-
result: Record< string, string >
10-
) => BlockInstance< RemoteDataInnerBlockAttributes >[];
8+
getInnerBlocks: ( result: RemoteDataResult ) => BlockInstance< RemoteDataInnerBlockAttributes >[];
119
remoteData: RemoteData;
1210
}
1311

src/blocks/remote-data-container/components/field-shortcode/FieldShortcodeSelection.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ export function FieldSelectionFromAvailableBindings( props: FieldSelectionWithFi
7474
...acc,
7575
[ fieldName ]: {
7676
name: binding.name,
77-
value: fieldValue,
77+
// eslint-disable-next-line @typescript-eslint/no-base-to-string
78+
value: fieldValue?.toString() ?? '',
7879
},
7980
};
8081
},

src/blocks/remote-data-container/components/item-list/ItemList.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface ItemListProps {
99
blockName: string;
1010
loading: boolean;
1111
onSelect: ( data: RemoteDataQueryInput ) => void;
12-
results?: RemoteData[ 'results' ];
12+
results?: RemoteDataResult[];
1313
searchTerms: string;
1414
setSearchTerms: ( newValue: string ) => void;
1515
}
@@ -31,7 +31,7 @@ export function ItemList( props: ItemListProps ) {
3131
? item[ Object.keys( item ).find( key => /(^|_)(id)$/i.test( key ) ) as string ]
3232
: instanceId,
3333
}
34-
) as RemoteData[ 'results' ];
34+
) as RemoteDataResult[];
3535
}, [ results ] );
3636

3737
// get fields from results data to use as columns
@@ -62,7 +62,7 @@ export function ItemList( props: ItemListProps ) {
6262
id: string;
6363
label: string;
6464
enableGlobalSearch: boolean;
65-
render?: ( { item }: { item: RemoteData[ 'results' ][ 0 ] } ) => JSX.Element;
65+
render?: ( { item }: { item: RemoteDataResult } ) => JSX.Element;
6666
enableSorting: boolean;
6767
}[] = getFields.map( field => {
6868
return {
@@ -71,7 +71,7 @@ export function ItemList( props: ItemListProps ) {
7171
enableGlobalSearch: true,
7272
render:
7373
field === media
74-
? ( { item }: { item: RemoteData[ 'results' ][ 0 ] } ) => {
74+
? ( { item }: { item: RemoteDataResult } ) => {
7575
return (
7676
<img
7777
// temporary until we pull in more data
@@ -134,7 +134,7 @@ export function ItemList( props: ItemListProps ) {
134134
icon: <>{ __( 'Choose' ) }</>,
135135
isPrimary: true,
136136
label: '',
137-
callback: ( items: RemoteData[ 'results' ] ) => {
137+
callback: ( items: RemoteDataResult[] ) => {
138138
items.map( item => onSelect( item ) );
139139
},
140140
},

src/blocks/remote-data-container/components/loop-template/LoopTemplate.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ import { LoopTemplateInnerBlocks } from '@/blocks/remote-data-container/componen
1313
import { LoopIndexContext } from '@/blocks/remote-data-container/context/LoopIndexContext';
1414

1515
interface LoopTemplateProps {
16-
getInnerBlocks: (
17-
result: Record< string, string >
18-
) => BlockInstance< RemoteDataInnerBlockAttributes >[];
16+
getInnerBlocks: ( result: RemoteDataResult ) => BlockInstance< RemoteDataInnerBlockAttributes >[];
1917
remoteData: RemoteData;
2018
}
2119

src/blocks/remote-data-container/hooks/usePatterns.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { getBlockConfig } from '@/utils/localized-block-data';
1818

1919
export function cloneBlockWithAttributes(
2020
block: BlockInstance,
21-
attributes: Record< string, string >,
21+
attributes: RemoteDataResult,
2222
remoteDataBlockName: string
2323
): BlockInstance {
2424
const mismatchedAttributes = getMismatchedAttributes(
@@ -53,13 +53,13 @@ export function usePatterns( remoteDataBlockName: string, rootClientId: string =
5353
const returnValue = {
5454
defaultPattern,
5555
getInnerBlocks: (
56-
result: Record< string, string >
56+
result: RemoteDataResult
5757
): BlockInstance< RemoteDataInnerBlockAttributes >[] => {
5858
return getBlocks< RemoteDataInnerBlockAttributes >( rootClientId ).map( block =>
5959
cloneBlockWithAttributes( block, result, remoteDataBlockName )
6060
);
6161
},
62-
getSupportedPatterns: ( result?: Record< string, string > ): BlockPattern[] => {
62+
getSupportedPatterns: ( result?: RemoteDataResult ): BlockPattern[] => {
6363
const supportedPatterns = __experimentalGetAllowedPatterns( rootClientId ).filter(
6464
pattern =>
6565
pattern?.blockTypes?.includes( remoteDataBlockName ) ||

src/utils/block-binding.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,21 @@ function getAttributeValue( attributes: unknown, key: string | undefined | null
1919
}
2020

2121
function getExpectedAttributeValue(
22-
result?: Record< string, string >,
22+
result?: Record< string, unknown >,
2323
args?: RemoteDataBlockBindingArgs
2424
): string | null {
2525
if ( ! args?.field || ! result?.[ args.field ] ) {
2626
return null;
2727
}
2828

29-
let expectedValue = result[ args.field ];
29+
// See comment on toString() in getAttributeValue.
30+
let expectedValue = result[ args.field ]?.toString() ?? '';
3031
if ( args.label ) {
3132
const labelClass = getClassName( 'block-label' );
3233
expectedValue = `<span class="${ labelClass }">${ args.label }</span> ${ expectedValue }`;
3334
}
3435

35-
return expectedValue ?? null;
36+
return expectedValue;
3637
}
3738

3839
export function getBoundAttributeEntries(
@@ -64,7 +65,7 @@ export function getBoundBlockClassName(
6465

6566
export function getMismatchedAttributes(
6667
attributes: RemoteDataInnerBlockAttributes,
67-
results: RemoteData[ 'results' ],
68+
results: RemoteDataResult[],
6869
remoteDataBlockName: string,
6970
index = 0
7071
): Partial< RemoteDataInnerBlockAttributes > {

tests/inc/Editor/DataBinding/BlockBindingsTest.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,4 +334,100 @@ public function execute( HttpQueryInterface $query, array $input_variables ): ar
334334
'test_input_field' => 'override_value transformed',
335335
] );
336336
}
337+
338+
/**
339+
* @runInSeparateProcess
340+
*/
341+
public function test_get_remote_value(): void {
342+
/**
343+
* Mock the QueryRunner to return a result.
344+
*/
345+
$mock_qr = new MockQueryRunner();
346+
$mock_qr->addResult( 'output_field', 'Test Output Value' );
347+
348+
$block_context = [
349+
'blockName' => self::MOCK_BLOCK_NAME,
350+
'queryInput' => [
351+
'test_input_field' => 'test_value',
352+
],
353+
];
354+
355+
$input_schema = [
356+
'test_input_field' => [
357+
'name' => 'Test Input Field',
358+
'type' => 'string',
359+
],
360+
];
361+
362+
$mock_block_config = [
363+
'queries' => [
364+
ConfigRegistry::DISPLAY_QUERY_KEY => MockQuery::from_array( [
365+
'input_schema' => $input_schema,
366+
'output_schema' => self::MOCK_OUTPUT_SCHEMA,
367+
'query_runner' => $mock_qr,
368+
] ),
369+
],
370+
];
371+
372+
$mock_config_store = Mockery::namedMock( ConfigStore::class );
373+
$mock_config_store->shouldReceive( 'get_block_configuration' )
374+
->once()
375+
->with( self::MOCK_BLOCK_NAME )
376+
->andReturn( $mock_block_config );
377+
378+
$source_args = [
379+
'field' => self::MOCK_OUTPUT_FIELD_NAME,
380+
];
381+
382+
$remote_value = BlockBindings::get_remote_value( $block_context, $source_args );
383+
$this->assertSame( $remote_value, self::MOCK_OUTPUT_FIELD_VALUE );
384+
}
385+
386+
/**
387+
* @runInSeparateProcess
388+
*/
389+
public function test_get_remote_value_with_non_string(): void {
390+
/**
391+
* Mock the QueryRunner to return a result.
392+
*/
393+
$mock_qr = new MockQueryRunner();
394+
$mock_qr->addResult( 'output_field', 123 );
395+
396+
$block_context = [
397+
'blockName' => self::MOCK_BLOCK_NAME,
398+
'queryInput' => [
399+
'test_input_field' => 'test_value',
400+
],
401+
];
402+
403+
$input_schema = [
404+
'test_input_field' => [
405+
'name' => 'Test Input Field',
406+
'type' => 'string',
407+
],
408+
];
409+
410+
$mock_block_config = [
411+
'queries' => [
412+
ConfigRegistry::DISPLAY_QUERY_KEY => MockQuery::from_array( [
413+
'input_schema' => $input_schema,
414+
'output_schema' => self::MOCK_OUTPUT_SCHEMA,
415+
'query_runner' => $mock_qr,
416+
] ),
417+
],
418+
];
419+
420+
$mock_config_store = Mockery::namedMock( ConfigStore::class );
421+
$mock_config_store->shouldReceive( 'get_block_configuration' )
422+
->once()
423+
->with( self::MOCK_BLOCK_NAME )
424+
->andReturn( $mock_block_config );
425+
426+
$source_args = [
427+
'field' => self::MOCK_OUTPUT_FIELD_NAME,
428+
];
429+
430+
$remote_value = BlockBindings::get_remote_value( $block_context, $source_args );
431+
$this->assertSame( $remote_value, '123' );
432+
}
337433
}

tests/src/utils/block-binding.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,5 +127,43 @@ describe( 'block-binding utils', () => {
127127
content: '<span class="rdb-block-label">Title</span> My Title',
128128
} );
129129
} );
130+
131+
it( 'should handle mismatched types', () => {
132+
const block = 'test/block';
133+
const attributes: RemoteDataInnerBlockAttributes = {
134+
content: '123',
135+
metadata: {
136+
bindings: {
137+
content: { source: BLOCK_BINDING_SOURCE, args: { block, field: 'a_field' } },
138+
},
139+
},
140+
};
141+
142+
const results: Record< string, unknown >[] = [ { a_field: 123 } ];
143+
144+
const result = getMismatchedAttributes( attributes, results, block );
145+
146+
expect( result ).toEqual( {} );
147+
} );
148+
149+
it( 'should handle convert mismatched attributes to string', () => {
150+
const block = 'test/block';
151+
const attributes: RemoteDataInnerBlockAttributes = {
152+
content: '123',
153+
metadata: {
154+
bindings: {
155+
content: { source: BLOCK_BINDING_SOURCE, args: { block, field: 'a_field' } },
156+
},
157+
},
158+
};
159+
160+
const results: Record< string, unknown >[] = [ { a_field: 1234 } ];
161+
162+
const result = getMismatchedAttributes( attributes, results, block );
163+
164+
expect( result ).toEqual( {
165+
content: '1234',
166+
} );
167+
} );
130168
} );
131169
} );

types/remote-data.d.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@ interface QueryInputOverride {
1414
sourceType: 'query_var';
1515
}
1616

17+
type RemoteDataResult = Record< string, unknown >;
18+
type RemoteDataQueryInput = Record< string, unknown >;
19+
1720
interface RemoteData {
1821
blockName: string;
1922
isCollection: boolean;
2023
metadata: Record< string, RemoteDataResultFields >;
21-
queryInput: Record< string, string >;
24+
queryInput: RemoteDataQueryInput;
2225
queryInputOverrides?: Record< string, QueryInputOverride >;
2326
resultId: string;
24-
results: Record< string, string >[];
27+
results: RemoteDataResult[];
2528
}
2629

2730
interface RemoteDataBlockAttributes {
@@ -62,8 +65,6 @@ interface RemoteDataInnerBlockAttributes {
6265
url?: string | RichTextData;
6366
}
6467

65-
type RemoteDataQueryInput = Record< string, string >;
66-
6768
interface RemoteDataApiRequest {
6869
block_name: string;
6970
query_key: string;

0 commit comments

Comments
 (0)