Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Bindings: Refactor e2e tests #65526

Merged
merged 27 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
99cd7ec
WIP: Add script to block bindings tests
SantosGuillamot Sep 26, 2024
3986ff9
Add first version of block bindings test theme
SantosGuillamot Sep 26, 2024
3831c97
Register movie CPT
SantosGuillamot Sep 26, 2024
5941ef4
Create structure for tests
SantosGuillamot Sep 26, 2024
fcdcdc6
Remove current specs
SantosGuillamot Sep 26, 2024
9a67274
Share data between server and client
SantosGuillamot Sep 26, 2024
53b6ddd
Add `getValues` tests
SantosGuillamot Sep 26, 2024
b15c9bb
Move checks for paragraph locking
SantosGuillamot Sep 26, 2024
4f9ba94
Wrap tests that should lock editing
SantosGuillamot Sep 26, 2024
4d36f2a
Add setValues e2e tests
SantosGuillamot Sep 26, 2024
968d80e
Add tests for `getFieldsList`
SantosGuillamot Sep 26, 2024
d6105fd
Add tests for rich text workflows
SantosGuillamot Sep 26, 2024
fdab66f
Add remaining use cases
SantosGuillamot Sep 26, 2024
9e1c846
Use one complete source
SantosGuillamot Sep 26, 2024
041fe6c
Add tests for CPT template
SantosGuillamot Sep 26, 2024
7722cf9
Fix label test
SantosGuillamot Sep 26, 2024
969be9d
Add attributes panel tests
SantosGuillamot Sep 26, 2024
82dd071
Add dropdown post meta tests
SantosGuillamot Sep 26, 2024
b40ea0c
Add post meta tests for custom templates
SantosGuillamot Sep 26, 2024
74b3200
Add e2e tests in movie post
SantosGuillamot Sep 26, 2024
31d7b34
Add template for posts
SantosGuillamot Sep 26, 2024
34766ec
Rename specs
SantosGuillamot Sep 27, 2024
5b0895c
Fix post-meta tests
SantosGuillamot Sep 27, 2024
60ada8f
Check post meta in the frontend
SantosGuillamot Sep 27, 2024
c7457d0
Update theme definition
SantosGuillamot Sep 27, 2024
6ed6f43
Populate `setValues`
SantosGuillamot Sep 27, 2024
5b4f1e3
Check image title locking
SantosGuillamot Sep 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 112 additions & 21 deletions packages/e2e-tests/plugins/block-bindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,67 +8,158 @@
*/

/**
* Register custom fields and custom block bindings sources.
*/
* Code necessary for testing block bindings:
* - Enqueues a custom script to register sources in the client.
* - Registers sources in the server.
* - Registers a custom post type and custom fields.
*/
function gutenberg_test_block_bindings_registration() {
// Define fields list.
$upload_dir = wp_upload_dir();
$testing_url = $upload_dir['url'] . '/1024x768_e2e_test_image_size.jpeg';
$fields_list = array(
'text_field' => array(
'label' => 'Text Field Label',
'value' => 'Text Field Value',
),
'url_field' => array(
'label' => 'URL Field Label',
'value' => $testing_url,
),
'empty_field' => array(
'label' => 'Empty Field Label',
'value' => '',
),
);

// Enqueue a custom script for the plugin.
wp_enqueue_script(
'gutenberg-test-block-bindings',
plugins_url( 'block-bindings/index.js', __FILE__ ),
array(
'wp-blocks',
'wp-private-apis',
),
filemtime( plugin_dir_path( __FILE__ ) . 'block-bindings/index.js' ),
true
);

// Pass data to the script.
wp_localize_script(
'gutenberg-test-block-bindings',
'testingBindings',
array(
'fieldsList' => $fields_list,
)
);

// Register custom block bindings sources.
register_block_bindings_source(
'core/server-source',
'testing/complete-source',
array(
'label' => 'Complete Source',
'get_value_callback' => function ( $source_args ) use ( $fields_list ) {
if ( ! isset( $source_args['key'] ) || ! isset( $fields_list[ $source_args['key'] ] ) ) {
return null;
}
return $fields_list[ $source_args['key'] ]['value']; },
)
);
register_block_bindings_source(
'testing/server-only-source',
array(
'label' => 'Server Source',
'get_value_callback' => function () {},
)
);

// Register custom fields.
// Register "movie" custom post type.
register_post_type(
'movie',
array(
'label' => 'Movie',
'public' => true,
'supports' => array( 'title', 'editor', 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', 'page-attributes', 'thumbnail', 'custom-fields', 'post-formats' ),
'has_archive' => true,
'show_in_rest' => true,
)
);

// Register global custom fields.
register_meta(
'post',
'text_custom_field',
array(
'default' => 'Value of the text custom field',
'show_in_rest' => true,
'type' => 'string',
'single' => true,
'default' => 'Value of the text custom field',
'type' => 'string',
)
);
register_meta(
'post',
'url_custom_field',
array(
'default' => '#url-custom-field',
'show_in_rest' => true,
'type' => 'string',
'single' => true,
'default' => '#url-custom-field',
'type' => 'string',
)
);
// Register CPT custom fields.
register_meta(
'post',
'empty_field',
'movie_field',
array(
'show_in_rest' => true,
'type' => 'string',
'single' => true,
'default' => '',
'label' => 'Movie field label',
'default' => 'Movie field default value',
'object_subtype' => 'movie',
'show_in_rest' => true,
'single' => true,
'type' => 'string',
)
);
register_meta(
'post',
'field_with_only_label',
array(
'label' => 'Field with only label',
'object_subtype' => 'movie',
'show_in_rest' => true,
'single' => true,
'type' => 'string',
)
);
register_meta(
'post',
'field_without_label_or_default',
array(
'object_subtype' => 'movie',
'show_in_rest' => true,
'single' => true,
'type' => 'string',
)
);
register_meta(
'post',
'_protected_field',
array(
'type' => 'string',
'show_in_rest' => true,
'single' => true,
'default' => 'protected field value',
'default' => 'Protected field value',
'object_subtype' => 'movie',
'show_in_rest' => true,
'single' => true,
'type' => 'string',
)
);
register_meta(
'post',
'show_in_rest_false_field',
array(
'show_in_rest' => false,
'type' => 'string',
'single' => true,
'default' => 'show_in_rest false field value',
'default' => 'show_in_rest false field value',
'object_subtype' => 'movie',
'show_in_rest' => false,
'single' => true,
'type' => 'string',
)
);
}
Expand Down
55 changes: 55 additions & 0 deletions packages/e2e-tests/plugins/block-bindings/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const { unlock } =
wp.privateApis.__dangerousOptInToUnstableAPIsOnlyForCoreModules(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember to update this once they are public.

'I acknowledge private features are not for use in themes or plugins and doing so will break in the next version of WordPress.',
'@wordpress/blocks'
);

const { registerBlockBindingsSource } = unlock( wp.blocks.privateApis );
const { fieldsList } = window.testingBindings || {};

const getValues = ( { bindings } ) => {
const newValues = {};
for ( const [ attributeName, source ] of Object.entries( bindings ) ) {
newValues[ attributeName ] = fieldsList[ source.args.key ]?.value;
}
return newValues;
};
const setValues = ( { registry, bindings } ) => {
Object.values( bindings ).forEach( ( { args, newValue } ) => {
// Example of what could be done.
registry.dispatch( 'core' ).editEntityRecord( 'postType', 'post', 1, {
meta: { [ args?.key ]: newValue },
} );
} );
};

registerBlockBindingsSource( {
name: 'testing/complete-source',
label: 'Complete Source',
getValues,
setValues,
canUserEditValue: () => true,
getFieldsList: () => fieldsList,
} );

registerBlockBindingsSource( {
name: 'testing/can-user-edit-false',
label: 'Can User Edit: False',
getValues,
setValues,
canUserEditValue: () => false,
} );

registerBlockBindingsSource( {
name: 'testing/can-user-edit-undefined',
label: 'Can User Edit: Undefined',
getValues,
setValues,
} );

registerBlockBindingsSource( {
name: 'testing/set-values-undefined',
label: 'Set Values: Undefined',
getValues,
canUserEditValue: () => true,
} );
Loading
Loading