From c4400ccfbc33f6ee2ca1d401f1ef8eacdf468b6d Mon Sep 17 00:00:00 2001 From: Darin Kotter Date: Wed, 31 Jan 2024 17:08:28 -0700 Subject: [PATCH] Add tests --- ...excerpt-generation-openapi-chatgpt.test.js | 4 +- .../moderation-openai-moderation.test.js | 130 ++++++++++++++++++ tests/cypress/support/commands.js | 17 +++ tests/test-plugin/e2e-test-plugin.php | 2 + tests/test-plugin/moderation.json | 35 +++++ 5 files changed, 187 insertions(+), 1 deletion(-) create mode 100644 tests/cypress/integration/language-processing/moderation-openai-moderation.test.js create mode 100644 tests/test-plugin/moderation.json diff --git a/tests/cypress/integration/language-processing/excerpt-generation-openapi-chatgpt.test.js b/tests/cypress/integration/language-processing/excerpt-generation-openapi-chatgpt.test.js index 0e02fb5c0..02903ef77 100644 --- a/tests/cypress/integration/language-processing/excerpt-generation-openapi-chatgpt.test.js +++ b/tests/cypress/integration/language-processing/excerpt-generation-openapi-chatgpt.test.js @@ -7,7 +7,9 @@ describe( '[Language processing] Excerpt Generation Tests', () => { '/wp-admin/tools.php?page=classifai&tab=language_processing&feature=feature_excerpt_generation' ); cy.get( '#status' ).check(); - cy.get( '#classifai_feature_excerpt_generation_post_types_post' ).check(); + cy.get( + '#classifai_feature_excerpt_generation_post_types_post' + ).check(); cy.get( '#submit' ).click(); cy.optInAllFeatures(); cy.disableClassicEditor(); diff --git a/tests/cypress/integration/language-processing/moderation-openai-moderation.test.js b/tests/cypress/integration/language-processing/moderation-openai-moderation.test.js new file mode 100644 index 000000000..743d6fe12 --- /dev/null +++ b/tests/cypress/integration/language-processing/moderation-openai-moderation.test.js @@ -0,0 +1,130 @@ +describe( '[Language processing] Moderation Tests', () => { + before( () => { + cy.login(); + cy.visit( + '/wp-admin/tools.php?page=classifai&tab=language_processing&feature=feature_moderation' + ); + cy.get( '#submit' ).click(); + cy.optInAllFeatures(); + cy.disableClassicEditor(); + } ); + + beforeEach( () => { + cy.login(); + } ); + + it( 'Can save OpenAI Moderation "Language Processing" settings', () => { + cy.visit( + '/wp-admin/tools.php?page=classifai&tab=language_processing&feature=feature_moderation' + ); + + cy.get( '#api_key' ).clear().type( 'password' ); + cy.get( '#status' ).check(); + cy.get( + '#classifai_feature_moderation_content_types_comments' + ).check(); + cy.get( '#role_based_access' ).check(); + cy.get( '#classifai_feature_moderation_roles_administrator' ).check(); + cy.get( '#submit' ).click(); + } ); + + it( 'Can run moderation on a comment', () => { + cy.visit( '/wp-admin/edit-comments.php' ); + + cy.get( '#cb-select-1' ).check(); + cy.get( '#bulk-action-selector-top' ).select( 'feature_moderation' ); + cy.get( '#doaction' ).click(); + + cy.get( '#comment-1 .column-moderation_flagged div' ).contains( 'Yes' ); + cy.get( '#comment-1 .column-moderation_flags div' ).contains( + 'harassment/threatening, violence' + ); + } ); + + it( 'Can enable/disable moderation feature', () => { + // Disable features. + cy.visit( + '/wp-admin/tools.php?page=classifai&tab=language_processing&feature=feature_moderation' + ); + cy.get( '#status' ).uncheck(); + cy.get( '#submit' ).click(); + + // Verify that the feature is not available. + cy.verifyModerationEnabled( false ); + + // Enable feature. + cy.visit( + '/wp-admin/tools.php?page=classifai&tab=language_processing&feature=feature_moderation' + ); + cy.get( '#status' ).check(); + cy.get( '#submit' ).click(); + + // Verify that the feature is available. + cy.verifyModerationEnabled( true ); + } ); + + it( 'Can enable/disable moderation feature by role', () => { + cy.visit( + '/wp-admin/tools.php?page=classifai&tab=language_processing&feature=feature_moderation' + ); + cy.get( '#status' ).check(); + cy.get( '#submit' ).click(); + + // Disable admin role. + cy.disableFeatureForRoles( 'feature_moderation', [ 'administrator' ] ); + + // Verify that the feature is not available. + cy.verifyModerationEnabled( false ); + + // enable admin role. + cy.enableFeatureForRoles( 'feature_moderation', [ 'administrator' ] ); + + // Verify that the feature is available. + cy.verifyModerationEnabled( true ); + } ); + + it( 'Can enable/disable moderation feature by user', () => { + cy.visit( + '/wp-admin/tools.php?page=classifai&tab=language_processing&feature=feature_moderation' + ); + cy.get( '#status' ).check(); + cy.get( '#submit' ).click(); + + // Disable admin role. + cy.disableFeatureForRoles( 'feature_moderation', [ 'administrator' ] ); + + cy.enableFeatureForUsers( 'feature_moderation', [] ); + + // Verify that the feature is not available. + cy.verifyModerationEnabled( false ); + + // Enable feature for admin user. + cy.enableFeatureForUsers( 'feature_moderation', [ 'admin' ] ); + + // Verify that the feature is available. + cy.verifyModerationEnabled( true ); + } ); + + it( 'User can opt-out of moderation feature', () => { + cy.visit( + '/wp-admin/tools.php?page=classifai&tab=language_processing&feature=feature_moderation' + ); + cy.get( '#status' ).check(); + cy.get( '#submit' ).click(); + + // Enable user based opt-out. + cy.enableFeatureOptOut( 'feature_moderation' ); + + // opt-out + cy.optOutFeature( 'feature_moderation' ); + + // Verify that the feature is not available. + cy.verifyModerationEnabled( false ); + + // opt-in + cy.optInFeature( 'feature_moderation' ); + + // Verify that the feature is available. + cy.verifyModerationEnabled( true ); + } ); +} ); diff --git a/tests/cypress/support/commands.js b/tests/cypress/support/commands.js index 9c1513100..7d91e2ba9 100644 --- a/tests/cypress/support/commands.js +++ b/tests/cypress/support/commands.js @@ -240,6 +240,23 @@ Cypress.Commands.add( 'verifyClassifyContentEnabled', ( enabled = true ) => { ).should( shouldExist ); } ); +/** + * Verify that the excerpt generation feature is enabled or disabled. + * + * @param {boolean} enabled Whether the feature should be enabled or disabled. + */ +Cypress.Commands.add( 'verifyModerationEnabled', ( enabled = true ) => { + const shouldExist = enabled ? 'exist' : 'not.exist'; + + cy.visit( '/wp-admin/edit-comments.php' ); + + cy.get( '#bulk-action-selector-top option:contains(Moderate)' ).should( + shouldExist + ); + cy.get( '#moderation_flagged' ).should( shouldExist ); + cy.get( '#moderation_flags' ).should( shouldExist ); +} ); + /** * Verify that the excerpt generation feature is enabled or disabled. * diff --git a/tests/test-plugin/e2e-test-plugin.php b/tests/test-plugin/e2e-test-plugin.php index aeb47bb52..874b0fc10 100644 --- a/tests/test-plugin/e2e-test-plugin.php +++ b/tests/test-plugin/e2e-test-plugin.php @@ -42,6 +42,8 @@ function classifai_test_mock_http_requests( $preempt, $parsed_args, $url ) { $response = file_get_contents( __DIR__ . '/resize-content-custom-prompt.json' ); } } + } elseif ( strpos( $url, 'https://api.openai.com/v1/moderations' ) !== false ) { + $response = file_get_contents( __DIR__ . '/moderation.json' ); } elseif ( strpos( $url, 'https://api.openai.com/v1/audio/transcriptions' ) !== false ) { $response = file_get_contents( __DIR__ . '/whisper.json' ); } elseif ( strpos( $url, 'https://api.openai.com/v1/images/generations' ) !== false ) { diff --git a/tests/test-plugin/moderation.json b/tests/test-plugin/moderation.json new file mode 100644 index 000000000..7aa617fd6 --- /dev/null +++ b/tests/test-plugin/moderation.json @@ -0,0 +1,35 @@ +{ + "id": "modr-XXXXX", + "model": "text-moderation-005", + "results": [ + { + "flagged": true, + "categories": { + "sexual": false, + "hate": false, + "harassment": false, + "self-harm": false, + "sexual/minors": false, + "hate/threatening": false, + "violence/graphic": false, + "self-harm/intent": false, + "self-harm/instructions": false, + "harassment/threatening": true, + "violence": true + }, + "category_scores": { + "sexual": 1.2282071e-06, + "hate": 0.010696256, + "harassment": 0.29842457, + "self-harm": 1.5236925e-08, + "sexual/minors": 5.7246268e-08, + "hate/threatening": 0.0060676364, + "violence/graphic": 4.435014e-06, + "self-harm/intent": 8.098441e-10, + "self-harm/instructions": 2.8498655e-11, + "harassment/threatening": 0.63055265, + "violence": 0.99011886 + } + } + ] +}