|
| 1 | +/** |
| 2 | + * WordPress dependencies |
| 3 | + */ |
| 4 | +const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' ); |
| 5 | + |
| 6 | +test.describe( 'Allowed Blocks Setting on InnerBlocks', () => { |
| 7 | + test.beforeAll( async ( { requestUtils } ) => { |
| 8 | + await requestUtils.activatePlugin( |
| 9 | + 'gutenberg-test-inner-blocks-allowed-blocks' |
| 10 | + ); |
| 11 | + } ); |
| 12 | + |
| 13 | + test.afterAll( async ( { requestUtils } ) => { |
| 14 | + await requestUtils.deactivatePlugin( |
| 15 | + 'gutenberg-test-inner-blocks-allowed-blocks' |
| 16 | + ); |
| 17 | + } ); |
| 18 | + |
| 19 | + test.beforeEach( async ( { admin } ) => { |
| 20 | + await admin.createNewPost(); |
| 21 | + } ); |
| 22 | + |
| 23 | + test( 'allows all blocks if the allowed blocks setting was not set', async ( { |
| 24 | + editor, |
| 25 | + page, |
| 26 | + } ) => { |
| 27 | + await editor.insertBlock( { |
| 28 | + name: 'core/group', |
| 29 | + attributes: { |
| 30 | + layout: { type: 'constrained' }, |
| 31 | + }, |
| 32 | + innerBlocks: [ |
| 33 | + { |
| 34 | + name: 'core/paragraph', |
| 35 | + attributes: { placeholder: 'Add a description' }, |
| 36 | + }, |
| 37 | + ], |
| 38 | + } ); |
| 39 | + |
| 40 | + await editor.canvas |
| 41 | + .getByRole( 'document', { |
| 42 | + name: 'Empty block', |
| 43 | + } ) |
| 44 | + .click(); |
| 45 | + |
| 46 | + const blockInserter = page |
| 47 | + .getByRole( 'toolbar', { name: 'Document tools' } ) |
| 48 | + .getByRole( 'button', { name: 'Toggle block inserter' } ); |
| 49 | + const blockLibrary = page.getByRole( 'region', { |
| 50 | + name: 'Block Library', |
| 51 | + } ); |
| 52 | + |
| 53 | + await blockInserter.click(); |
| 54 | + await expect( blockLibrary ).toBeVisible(); |
| 55 | + expect( |
| 56 | + await blockLibrary.getByRole( 'option' ).count() |
| 57 | + ).toBeGreaterThan( 10 ); |
| 58 | + } ); |
| 59 | + |
| 60 | + test( 'limits the blocks if the allowed blocks setting was set', async ( { |
| 61 | + editor, |
| 62 | + page, |
| 63 | + } ) => { |
| 64 | + await editor.insertBlock( { |
| 65 | + name: 'core/group', |
| 66 | + attributes: { |
| 67 | + layout: { type: 'constrained' }, |
| 68 | + allowedBlocks: [ |
| 69 | + 'core/paragraph', |
| 70 | + 'core/heading', |
| 71 | + 'core/image', |
| 72 | + ], |
| 73 | + }, |
| 74 | + innerBlocks: [ |
| 75 | + { |
| 76 | + name: 'core/paragraph', |
| 77 | + attributes: { placeholder: 'Add a description' }, |
| 78 | + }, |
| 79 | + ], |
| 80 | + } ); |
| 81 | + |
| 82 | + // Select inner block. |
| 83 | + await editor.canvas |
| 84 | + .getByRole( 'document', { |
| 85 | + name: 'Empty block', |
| 86 | + } ) |
| 87 | + .click(); |
| 88 | + |
| 89 | + const blockInserter = page |
| 90 | + .getByRole( 'toolbar', { name: 'Document tools' } ) |
| 91 | + .getByRole( 'button', { name: 'Toggle block inserter' } ); |
| 92 | + const blockLibrary = page.getByRole( 'region', { |
| 93 | + name: 'Block Library', |
| 94 | + } ); |
| 95 | + |
| 96 | + await blockInserter.click(); |
| 97 | + await expect( blockLibrary ).toBeVisible(); |
| 98 | + await expect( blockLibrary.getByRole( 'option' ) ).toHaveText( [ |
| 99 | + 'Paragraph', |
| 100 | + 'Heading', |
| 101 | + 'Image', |
| 102 | + ] ); |
| 103 | + } ); |
| 104 | + |
| 105 | + // Note: This behavior isn't fully supported. See https://github.com/WordPress/gutenberg/issues/14515. |
| 106 | + test( 'correctly applies dynamic allowed blocks restrictions', async ( { |
| 107 | + editor, |
| 108 | + page, |
| 109 | + } ) => { |
| 110 | + await editor.canvas.click( 'role=button[name="Add default block"i]' ); |
| 111 | + await page.keyboard.type( '/Allowed Blocks Dynamic' ); |
| 112 | + await page.keyboard.press( 'Enter' ); |
| 113 | + |
| 114 | + const blockAppender = editor.canvas.getByRole( 'button', { |
| 115 | + name: 'Add block', |
| 116 | + } ); |
| 117 | + await expect( blockAppender ).toBeVisible(); |
| 118 | + await blockAppender.click(); |
| 119 | + |
| 120 | + const blockListBox = page.getByRole( 'listbox', { name: 'Blocks' } ); |
| 121 | + await expect( blockListBox ).toBeVisible(); |
| 122 | + await expect( blockListBox.getByRole( 'option' ) ).toHaveText( [ |
| 123 | + 'Image', |
| 124 | + 'List', |
| 125 | + ] ); |
| 126 | + |
| 127 | + // Insert list block. |
| 128 | + await blockListBox.getByRole( 'option', { name: 'List' } ).click(); |
| 129 | + // Select the list wrapper and then parent block. |
| 130 | + await page.keyboard.press( 'ArrowUp' ); |
| 131 | + await editor.clickBlockToolbarButton( 'Select Allowed Blocks Dynamic' ); |
| 132 | + |
| 133 | + // Insert the image. |
| 134 | + await blockAppender.click(); |
| 135 | + await blockListBox.getByRole( 'option', { name: 'Image' } ).click(); |
| 136 | + |
| 137 | + await editor.clickBlockToolbarButton( 'Select Allowed Blocks Dynamic' ); |
| 138 | + await blockAppender.click(); |
| 139 | + |
| 140 | + // It should display a different allowed block list. |
| 141 | + await expect( blockListBox.getByRole( 'option' ) ).toHaveText( [ |
| 142 | + 'Gallery', |
| 143 | + 'Video', |
| 144 | + ] ); |
| 145 | + } ); |
| 146 | +} ); |
0 commit comments