diff --git a/tests/cypress/integration/language-processing/key-takeaways-azure-openai.test.js b/tests/cypress/integration/language-processing/key-takeaways-azure-openai.test.js index 663eec193..f9662fc43 100644 --- a/tests/cypress/integration/language-processing/key-takeaways-azure-openai.test.js +++ b/tests/cypress/integration/language-processing/key-takeaways-azure-openai.test.js @@ -34,7 +34,7 @@ describe( '[Language processing] Key Takeaways Tests', () => { title: 'Test Key Takeaways post', content: 'Test GPT content', beforeSave: () => { - cy.insertBlock( 'classifai/key-takeaways' ); + cy.customInsertBlock( 'classifai/key-takeaways' ); }, } ).then( () => { cy.getBlockEditor() @@ -44,91 +44,4 @@ describe( '[Language processing] Key Takeaways Tests', () => { .should( 'contain.text', 'Request failed' ); } ); } ); - - it( 'Can disable feature', () => { - // Disable feature. - cy.visitFeatureSettings( 'language_processing/feature_key_takeaways' ); - cy.disableFeature(); - cy.saveFeatureSettings(); - - // Verify that the feature is not available. - cy.createPost( { - title: 'Test Key Takeaways post disabled', - content: 'Test GPT content', - beforeSave: () => { - cy.insertBlock( 'classifai/key-takeaways' ); - }, - } ).then( () => { - cy.getBlockEditor() - .find( '.wp-block-classifai-key-takeaways' ) - .should( 'not.exist' ); - } ); - } ); - - it( 'Can disable feature by role', () => { - cy.visitFeatureSettings( 'language_processing/feature_key_takeaways' ); - cy.enableFeature(); - cy.saveFeatureSettings(); - - // Disable admin role. - cy.disableFeatureForRoles( 'feature_key_takeaways', [ - 'administrator', - ] ); - - // Verify that the feature is not available. - cy.createPost( { - title: 'Test Key Takeaways post disabled user', - content: 'Test GPT content', - beforeSave: () => { - cy.insertBlock( 'classifai/key-takeaways' ); - }, - } ).then( () => { - cy.getBlockEditor() - .find( '.wp-block-classifai-key-takeaways' ) - .should( 'not.exist' ); - } ); - } ); - - it( 'Can disable feature by user', () => { - // Disable admin role. - cy.disableFeatureForRoles( 'feature_key_takeaways', [ - 'administrator', - ] ); - - cy.enableFeatureForUsers( 'feature_key_takeaways', [] ); - - // Verify that the feature is not available. - cy.createPost( { - title: 'Test Key Takeaways post disabled user', - content: 'Test GPT content', - beforeSave: () => { - cy.insertBlock( 'classifai/key-takeaways' ); - }, - } ).then( () => { - cy.getBlockEditor() - .find( '.wp-block-classifai-key-takeaways' ) - .should( 'not.exist' ); - } ); - } ); - - it( 'User can opt-out of feature', () => { - // Enable user based opt-out. - cy.enableFeatureOptOut( 'feature_key_takeaways', 'azure_openai' ); - - // opt-out - cy.optOutFeature( 'feature_key_takeaways' ); - - // Verify that the feature is not available. - cy.createPost( { - title: 'Test Key Takeaways post disabled', - content: 'Test GPT content', - beforeSave: () => { - cy.insertBlock( 'classifai/key-takeaways' ); - }, - } ).then( () => { - cy.getBlockEditor() - .find( '.wp-block-classifai-key-takeaways' ) - .should( 'not.exist' ); - } ); - } ); } ); diff --git a/tests/cypress/integration/language-processing/key-takeaways-ollama.test.js b/tests/cypress/integration/language-processing/key-takeaways-ollama.test.js index dbc6c035d..310d6f8de 100644 --- a/tests/cypress/integration/language-processing/key-takeaways-ollama.test.js +++ b/tests/cypress/integration/language-processing/key-takeaways-ollama.test.js @@ -32,7 +32,7 @@ describe( '[Language processing] Key Takeaways Tests', () => { title: 'Test Key Takeaways post', content: 'Test GPT content', beforeSave: () => { - cy.insertBlock( 'classifai/key-takeaways' ); + cy.customInsertBlock( 'classifai/key-takeaways' ); }, } ).then( () => { cy.getBlockEditor() diff --git a/tests/cypress/integration/language-processing/key-takeaways-openai-chatgpt.test.js b/tests/cypress/integration/language-processing/key-takeaways-openai-chatgpt.test.js index 72a20a643..2681483cf 100644 --- a/tests/cypress/integration/language-processing/key-takeaways-openai-chatgpt.test.js +++ b/tests/cypress/integration/language-processing/key-takeaways-openai-chatgpt.test.js @@ -30,7 +30,7 @@ describe( '[Language processing] Key Takeaways Tests', () => { title: 'Test Key Takeaways post', content: 'Test GPT content', beforeSave: () => { - cy.insertBlock( 'classifai/key-takeaways' ); + cy.customInsertBlock( 'classifai/key-takeaways' ); }, } ).then( () => { cy.getBlockEditor() @@ -102,7 +102,7 @@ describe( '[Language processing] Key Takeaways Tests', () => { title: 'Test Key Takeaways post disabled', content: 'Test GPT content', beforeSave: () => { - cy.insertBlock( 'classifai/key-takeaways' ); + cy.customInsertBlock( 'classifai/key-takeaways' ); }, } ).then( () => { cy.getBlockEditor() @@ -126,7 +126,7 @@ describe( '[Language processing] Key Takeaways Tests', () => { title: 'Test Key Takeaways post disabled user', content: 'Test GPT content', beforeSave: () => { - cy.insertBlock( 'classifai/key-takeaways' ); + cy.customInsertBlock( 'classifai/key-takeaways' ); }, } ).then( () => { cy.getBlockEditor() @@ -148,7 +148,7 @@ describe( '[Language processing] Key Takeaways Tests', () => { title: 'Test Key Takeaways post disabled user', content: 'Test GPT content', beforeSave: () => { - cy.insertBlock( 'classifai/key-takeaways' ); + cy.customInsertBlock( 'classifai/key-takeaways' ); }, } ).then( () => { cy.getBlockEditor() @@ -169,7 +169,7 @@ describe( '[Language processing] Key Takeaways Tests', () => { title: 'Test Key Takeaways post disabled', content: 'Test GPT content', beforeSave: () => { - cy.insertBlock( 'classifai/key-takeaways' ); + cy.customInsertBlock( 'classifai/key-takeaways' ); }, } ).then( () => { cy.getBlockEditor() diff --git a/tests/cypress/support/commands.js b/tests/cypress/support/commands.js index 40b187f84..2ec19d93b 100644 --- a/tests/cypress/support/commands.js +++ b/tests/cypress/support/commands.js @@ -24,6 +24,7 @@ // -- This will overwrite an existing command -- // Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... }) import { getNLUData } from '../plugins/functions'; +import { getIframe } from '@10up/cypress-wp-utils/lib/functions/get-iframe'; const imageProcessingFeatures = [ 'feature_descriptive_text_generator', @@ -669,3 +670,127 @@ Cypress.Commands.add( 'allowFeatureToAdmin', () => { cy.openUserPermissionsPanel(); cy.get( '.settings-allowed-roles input#administrator' ).check(); } ); + +/** + * Custom command to insert a block, until a new release of cypress-wp-utils. + * + * TODO: remove this command once cypress-wp-utils is updated. + */ +Cypress.Commands.add( 'customInsertBlock', ( type, name ) => { + const [ namespace = '', ...blockNameRest ] = type.split( '/' ); + let blockNames = [ + blockNameRest.join( '/' ).replace( '/', '-' ), + blockNameRest.join( '/' ).replace( '/', '\\/' ), + ]; + + blockNames = blockNames.filter( ( x, i, a ) => a.indexOf( x ) === i ); + + let inserterBtn; + let search = ''; + + if ( typeof name === 'string' && name.length ) { + search = name; + } else { + search = type; + } + + // Start of block inserter toggle button click logic. + cy.get( 'body' ).then( ( $body ) => { + const selectors = [ + 'button[aria-label="Add block"]', // 5.7 + 'button[aria-label="Toggle block inserter"]', // 6.4 + 'button[aria-label="Block Inserter"]', // 6.8 + ]; + + selectors.forEach( ( selector ) => { + if ( $body.find( selector ).length ) { + cy.get( selector ).then( ( $button ) => { + if ( $button.length ) { + inserterBtn = cy.wrap( $button ); + inserterBtn.first().click(); + } + } ); + } + } ); + } ); + // End of block inserter toggle button click logic. + + // Start of Block tab click logic. + cy.get( 'button[role="tab"]' ) + .contains( 'Blocks' ) + .then( ( $tab ) => { + if ( $tab.length ) { + cy.wrap( $tab ).click(); + } + } ); + // End of Block tab click logic. + + // Start of Block search logic. + cy.get( 'input[placeholder="Search"]' ).then( ( $input ) => { + if ( $input.length ) { + cy.wrap( $input ).type( search ); + } + } ); + // End of Block search logic. + + blockNames.forEach( ( blockName ) => { + const blockSelector = `.editor-block-list-item-${ + 'core' === namespace ? '' : namespace + '-' + }${ blockName }`; + + cy.get( 'body' ).then( ( $body ) => { + if ( $body.find( blockSelector ).length ) { + // Start of Block insertion by click logic. + cy.get( blockSelector ).then( ( $block ) => { + if ( $block.length ) { + cy.wrap( $block ).click(); + inserterBtn.click(); + + const [ ns, rest ] = type.split( '/' ); // namespace = ns, second namespace or block name = rest + + cy.get( 'body' ).then( ( $innerBody ) => { + if ( + $innerBody.find( + 'iframe[name="editor-canvas"]' + ).length + ) { + // Works with WP 6.4 + getIframe( + 'iframe[name="editor-canvas"]' + ).then( ( $iframe ) => { + const blockInIframe = $iframe.find( + `.wp-block[data-type="${ ns }/${ rest }"]` + ); + if ( blockInIframe.length > 0 ) { + cy.wrap( + blockInIframe.last().prop( 'id' ) + ); + } + } ); + } else if ( + $innerBody.find( + `.wp-block[data-type="${ ns }/${ rest }"]` + ).length + ) { + // Works with WP 5.7 + cy.get( + `.wp-block[data-type="${ ns }/${ rest }"]` + ).then( ( $blockInEditor ) => { + expect( $blockInEditor.length ).to.equal( + 1 + ); + cy.wrap( $blockInEditor.prop( 'id' ) ); + } ); + } else { + throw new Error( + `${ ns }/${ rest } not found.` + ); + } + } ); + } + } ); + // End of Block insertion by click logic. + } + } ); + } ); +} );