Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dkotter committed Feb 1, 2024
1 parent a997d5a commit c4400cc
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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 );
} );
} );
17 changes: 17 additions & 0 deletions tests/cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
2 changes: 2 additions & 0 deletions tests/test-plugin/e2e-test-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
35 changes: 35 additions & 0 deletions tests/test-plugin/moderation.json
Original file line number Diff line number Diff line change
@@ -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
}
}
]
}

0 comments on commit c4400cc

Please sign in to comment.