-
Notifications
You must be signed in to change notification settings - Fork 47
Tests: Add block e2e experiment. #133
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' ); | ||
|
|
||
| const PLUGIN_SLUG = 'secure-custom-fields'; | ||
| const TEST_PLUGIN_SLUG = 'scf-test-plugin-get-field-movie-title-block'; | ||
| const FIELD_GROUP_LABEL = 'Movie Details'; | ||
| const FIELD_LABEL = 'Movie Title'; | ||
|
|
||
| test.describe( 'Field Type > Text', () => { | ||
| test.beforeAll( async ( { requestUtils } ) => { | ||
| await requestUtils.activatePlugin( PLUGIN_SLUG ); | ||
| await requestUtils.activatePlugin( TEST_PLUGIN_SLUG ); | ||
| } ); | ||
|
|
||
| test.afterAll( async ( { requestUtils } ) => { | ||
| await requestUtils.deactivatePlugin( PLUGIN_SLUG ); | ||
| await requestUtils.deactivatePlugin( TEST_PLUGIN_SLUG ); | ||
| await requestUtils.deleteAllPosts(); | ||
| } ); | ||
|
|
||
| test.beforeEach( async ( { page, admin } ) => { | ||
| await deleteFieldGroups( page, admin ); | ||
| } ); | ||
|
|
||
| test( 'should create a text field for movies, add content to it, and verify it displays on the frontend as a block', async ( { | ||
| page, | ||
| admin, | ||
| editor, | ||
| requestUtils, | ||
| } ) => { | ||
| // Navigate to Field Groups and create new. | ||
| await admin.visitAdminPage( 'edit.php', 'post_type=acf-field-group' ); | ||
| const addNewButton = page.locator( 'a.acf-btn:has-text("Add New")' ); | ||
| await addNewButton.click(); | ||
|
|
||
| // Fill field group title. | ||
| await page.waitForSelector( '#title' ); | ||
| await page.fill( '#title', FIELD_GROUP_LABEL ); | ||
|
|
||
| // Add text field. | ||
| const fieldLabel = page.locator( | ||
| 'input[id^="acf_fields-field_"][id$="-label"]' | ||
| ); | ||
| await fieldLabel.fill( FIELD_LABEL ); | ||
| // The field name is generated automatically. | ||
|
|
||
| // Select field type as text (it's default, but let's be explicit). | ||
| const fieldType = page.locator( | ||
| 'select[id^="acf_fields-field_"][id$="-type"]' | ||
| ); | ||
| await fieldType.selectOption( 'text' ); | ||
|
|
||
| // Set block in location rules. | ||
| await page.selectOption( | ||
| 'select[id^="acf_field_group-location-group_0-rule_0-param"]', | ||
| 'block' | ||
| ); | ||
| await page.selectOption( | ||
| 'select[id^="acf_field_group-location-group_0-rule_0-operator"]', | ||
| '==' | ||
| ); | ||
| await page.selectOption( | ||
| 'select[id^="acf_field_group-location-group_0-rule_0-value"]', | ||
| 'scf/movie-title-block' | ||
| ); | ||
|
|
||
| // Submit form. | ||
| const publishButton = page.locator( | ||
| 'button.acf-btn.acf-publish[type="submit"]' | ||
| ); | ||
| await publishButton.click(); | ||
|
|
||
| // Verify success message. | ||
| const successNotice = page.locator( '.updated.notice' ); | ||
| await expect( successNotice ).toBeVisible(); | ||
| await expect( successNotice ).toContainText( 'Field group published' ); | ||
|
|
||
| // Verify field group appears in the list. | ||
| await admin.visitAdminPage( 'edit.php', 'post_type=acf-field-group' ); | ||
| const fieldGroupRow = page.locator( | ||
| `tr:has-text("${ FIELD_GROUP_LABEL }")` | ||
| ); | ||
| await expect( fieldGroupRow ).toBeVisible(); | ||
|
|
||
| const post = await requestUtils.createPost( { | ||
| title: 'Movie 1', | ||
| status: 'publish', | ||
| showWelcomeGuide: false, | ||
| } ); | ||
|
|
||
| await admin.editPost( post.id ); | ||
|
|
||
| await editor.insertBlock( { | ||
| name: 'scf/movie-title-block', | ||
| } ); | ||
|
|
||
| await page.waitForSelector( | ||
| '.acf-field[data-name="movie_title"] input' | ||
| ); | ||
| await page.fill( | ||
| '.acf-field[data-name="movie_title"] input', | ||
| 'Awesome movie' | ||
| ); | ||
| // Add a blur event to trigger the field's onchange handlers. | ||
| await page.click('body', { position: { x: 0, y: 0 } }); | ||
|
|
||
| // Let's also make sure we give the editor a moment to save the field data. | ||
| await page.waitForTimeout(200); | ||
|
|
||
| const previewPage = await editor.openPreviewPage(); | ||
|
|
||
| // Verify the custom field value appears in the preview. | ||
| await previewPage.waitForSelector( '#scf-test-block-movie-title' ); | ||
| await expect( | ||
| previewPage.locator( '#scf-test-block-movie-title' ) | ||
| ).toContainText( 'Movie title: Awesome movie' ); | ||
| } ); | ||
| } ); | ||
|
|
||
| /** | ||
| * Helper function to delete the field group | ||
| */ | ||
| async function deleteFieldGroups( page, admin ) { | ||
| await admin.visitAdminPage( 'edit.php', 'post_type=acf-field-group' ); | ||
|
|
||
| // Find and select the field group row | ||
| const allFieldGroupsCheckbox = page.locator( 'input#cb-select-all-1' ); | ||
|
|
||
| if ( await allFieldGroupsCheckbox.isVisible() ) { | ||
| await allFieldGroupsCheckbox.check(); | ||
| // Use bulk actions to trash the field group. | ||
| await page.selectOption( '#bulk-action-selector-bottom', 'trash' ); | ||
| await page.click( '#doaction2' ); | ||
|
|
||
| // Verify deletion success message. | ||
| const deleteMessage = page.locator( '.updated.notice' ); | ||
| await expect( deleteMessage ).toBeVisible( { timeout: 5000 } ); | ||
| await expect( deleteMessage ).toContainText( 'moved to the Trash' ); | ||
|
|
||
| await emptyTrash( page, admin ); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Helper function to empty trash | ||
| */ | ||
| async function emptyTrash( page, admin ) { | ||
| await admin.visitAdminPage( | ||
| 'edit.php', | ||
| 'post_status=trash&post_type=acf-field-group' | ||
| ); | ||
| const emptyTrashButton = page.locator( | ||
| '.tablenav.bottom input[name="delete_all"][value="Empty Trash"]' | ||
| ); | ||
| await emptyTrashButton.waitFor( { state: 'visible' } ); | ||
| await emptyTrashButton.click(); | ||
|
|
||
| // Verify success notice. | ||
| const successNotice = page.locator( '.notice.updated p' ); | ||
| await expect( successNotice ).toBeVisible(); | ||
| await expect( successNotice ).toHaveText( /permanently deleted/ ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| { | ||
| "name": "scf/movie-title-block", | ||
| "title": "My Test Plugin Movie Title Block", | ||
| "description": "A custom block that uses SCF fields.", | ||
| "category": "layout", | ||
| "icon": "admin-comments", | ||
| "keywords": [ "custom", "scf", "block" ], | ||
| "acf": { | ||
| "mode": "edit", | ||
| "renderTemplate": "template.php" | ||
| }, | ||
| "supports": { | ||
| "align": true | ||
| }, | ||
| "textdomain": "secure-custom-fields" | ||
| } |
15 changes: 15 additions & 0 deletions
15
tests/e2e/plugins/blocks/scf-movie-title-block/template.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <?php | ||
| /** | ||
| * Template for the SCF Movie Title Block. | ||
| * | ||
| * @package scf-test-plugins | ||
| */ | ||
|
|
||
| // Exit if accessed directly. | ||
| if ( ! defined( 'ABSPATH' ) ) { | ||
| exit; | ||
| } | ||
| $movie_title = get_field( 'movie_title' ); | ||
|
|
||
| ?> | ||
| <p id="scf-test-block-movie-title">Movie title: <?php echo esc_html( $movie_title ); ?></p> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <?php | ||
| /** | ||
| * Plugin Name: SCF Test Plugin, Get Field Movie Title Block | ||
| * Plugin URI: https://github.com/WordPress/secure-custom-fields | ||
| * Author: SCF Team | ||
| * | ||
| * @package scf-test-plugins | ||
| */ | ||
|
|
||
| /** | ||
| * Registers a custom block type for testing. | ||
| * | ||
| * @return void | ||
| */ | ||
| function scf_test_register_acf_blocks() { | ||
| register_block_type( __DIR__ . '/blocks/scf-movie-title-block' ); | ||
| } | ||
| add_action( 'init', 'scf_test_register_acf_blocks' ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.