Skip to content

Commit 66ebf13

Browse files
committed
update e2e test to support feature-first refactor
1 parent 1e22f51 commit 66ebf13

17 files changed

+515
-432
lines changed

.wp-env.json

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
2-
"core": "WordPress/WordPress#6.1",
32
"plugins": [".", "./tests/test-plugin", "https://downloads.wordpress.org/plugin/classic-editor.zip"],
43
"env": {
54
"tests": {

includes/Classifai/Features/Feature.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ abstract protected function setup_fields_sections();
8282
protected function get_default_settings() {
8383
return [
8484
'status' => '0',
85-
'role_based_access' => 'no',
85+
'role_based_access' => '1',
8686
'roles' => array_combine( array_keys( $this->roles ), array_keys( $this->roles ) ),
8787
'user_based_access' => 'no',
8888
'users' => [],

includes/Classifai/Features/ImageTextExtraction.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ImageTextExtraction extends Feature {
1414
*
1515
* @var string
1616
*/
17-
const ID = 'feature_image_to_text_generation';
17+
const ID = 'feature_image_to_text_generator';
1818

1919
/**
2020
* Constructor.

includes/Classifai/Plugin.php

+12
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ public function enable() {
3939
add_action( 'admin_init', [ $this, 'add_privacy_policy_content' ] );
4040
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_assets' ] );
4141
add_filter( 'plugin_action_links_' . CLASSIFAI_PLUGIN_BASENAME, array( $this, 'filter_plugin_action_links' ) );
42+
add_action(
43+
'admin_footer',
44+
static function () {
45+
printf(
46+
'<div id="js-classifai--delete-prompt-modal" style="display:none;"><p>%1$s</p></div>',
47+
esc_html__( 'Are you sure you want to delete the prompt?', 'classifai' ),
48+
);
49+
}
50+
);
4251
}
4352

4453
/**
@@ -178,6 +187,9 @@ public function enqueue_admin_assets( $hook_suffix ) {
178187
'all'
179188
);
180189

190+
wp_enqueue_script( 'jquery-ui-dialog' );
191+
wp_enqueue_style( 'wp-jquery-ui-dialog' );
192+
181193
wp_enqueue_script(
182194
'classifai-admin-script',
183195
CLASSIFAI_PLUGIN_URL . 'dist/admin.js',

includes/Classifai/Providers/Azure/ComputerVision.php

+31-2
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ public function register() {
353353
}
354354

355355
if ( ( new ImageTextExtraction() )->is_feature_enabled() ) {
356+
add_filter( 'wp_generate_attachment_metadata', [ $this, 'perform_ocr_processing' ], 8, 2 );
356357
add_filter( 'the_content', [ $this, 'add_ocr_aria_describedby' ] );
357358
add_filter( 'rest_api_init', [ $this, 'add_ocr_data_to_api_response' ] );
358359
}
@@ -745,7 +746,7 @@ public function maybe_rescan_image( $attachment_id ) {
745746
// Are we updating the OCR text?
746747
if ( clean_input( 'rescan-ocr' ) ) {
747748
$feature = new ImageTextExtraction();
748-
$this->ocr_processing( wp_get_attachment_metadata( $attachment_id ), $attachment_id, true );
749+
$feature->run( wp_get_attachment_metadata( $attachment_id ), $attachment_id, true );
749750
}
750751
}
751752

@@ -760,6 +761,10 @@ public function maybe_rescan_image( $attachment_id ) {
760761
* @return array Filtered attachment metadata.
761762
*/
762763
public function smart_crop_image( $metadata, $attachment_id ) {
764+
if ( ! wp_attachment_is_image( $attachment_id ) ) {
765+
return $metadata;
766+
}
767+
763768
$feature = new ImageCropping();
764769
$settings = $feature->get_settings( static::ID );
765770

@@ -809,6 +814,10 @@ public function smart_crop_image( $metadata, $attachment_id ) {
809814
* @return mixed
810815
*/
811816
public function generate_image_alt_tags( $metadata, $attachment_id ) {
817+
if ( ! wp_attachment_is_image( $attachment_id ) ) {
818+
return $metadata;
819+
}
820+
812821
$feature = new ImageTagsGenerator();
813822

814823
if ( $feature->is_feature_enabled() ) {
@@ -841,6 +850,22 @@ public function generate_image_alt_tags( $metadata, $attachment_id ) {
841850
return $metadata;
842851
}
843852

853+
/**
854+
* Performs OCR processing on an image.
855+
*
856+
* @param array $metadata The metadata for the image.
857+
* @param int $attachment_id Post ID for the attachment.
858+
*
859+
* @return void
860+
*/
861+
public function perform_ocr_processing( $metadata, $attachment_id ) {
862+
if ( ! wp_attachment_is_image( $attachment_id ) ) {
863+
return $metadata;
864+
}
865+
866+
return ( new ImageTextExtraction() )->run( $metadata, $attachment_id, true );
867+
}
868+
844869
/**
845870
* Runs text recognition on the attachment.
846871
*
@@ -854,6 +879,10 @@ public function generate_image_alt_tags( $metadata, $attachment_id ) {
854879
* @return array Filtered attachment metadata.
855880
*/
856881
public function ocr_processing( array $metadata = [], int $attachment_id = 0, bool $force = false ) {
882+
if ( ! wp_attachment_is_image( $attachment_id ) ) {
883+
return $metadata;
884+
}
885+
857886
$feature = new ImageTextExtraction();
858887
$settings = $feature->get_settings( static::ID );
859888

@@ -989,7 +1018,7 @@ public function generate_alt_tags( $attachment_id ) {
9891018
set_transient( 'classifai_azure_computer_vision_descriptive_text_latest_response', $details, DAY_IN_SECONDS * 30 );
9901019

9911020
// Don't save tags if feature is disabled or user don't have access to use it.
992-
if ( ! $this->is_feature_enabled( 'image_captions' ) ) {
1021+
if ( ! $feature->is_feature_enabled() ) {
9931022
return new WP_Error( 'invalid_settings', esc_html__( 'Image descriptive text feature is disabled.', 'classifai' ) );
9941023
}
9951024

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
describe('Common Feature Fields', () => {
2+
beforeEach( () => {
3+
cy.login();
4+
} );
5+
6+
const features = {
7+
'feature_classification': 'Classification',
8+
'feature_title_generation': 'Title Generation',
9+
'feature_excerpt_generation': 'Excerpt Generation',
10+
'feature_content_resizing': 'Content Resizing',
11+
'feature_text_to_speech_generation': 'Text to Speech',
12+
'feature_audio_transcripts_generation': 'Audio Transcripts Generation',
13+
'feature_image_generation': 'Image Generation',
14+
'feature_descriptive_text_generator': 'Descriptive Text Generator',
15+
'feature_image_tags_generator': 'Image Tags Generator',
16+
'feature_image_cropping': 'Image Cropping',
17+
'feature_image_to_text_generator': 'Image Text Extraction',
18+
'feature_pdf_to_text_generation': 'PDF Text Extraction',
19+
};
20+
21+
const allowedRoles = [
22+
'administrator',
23+
'editor',
24+
'author',
25+
'contributor',
26+
'subscriber',
27+
];
28+
29+
Object.keys( features ).forEach( ( feature ) => {
30+
it( `"${ features[feature] }" feature common fields`, () => {
31+
cy.visit( `/wp-admin/tools.php?page=classifai&tab=language_processing&feature=${feature}` );
32+
33+
cy.get( '#status' ).should( 'have.attr', 'name', `classifai_${ feature }[status]` );
34+
cy.get( '#role_based_access' ).should( 'have.attr', 'name', `classifai_${ feature }[role_based_access]` );
35+
cy.get( '#user_based_access' ).should( 'have.attr', 'name', `classifai_${ feature }[user_based_access]` );
36+
cy.get( '#user_based_opt_out' ).should( 'have.attr', 'name', `classifai_${ feature }[user_based_opt_out]` );
37+
cy.get( '#provider' ).should( 'have.attr', 'name', `classifai_${ feature }[provider]` );
38+
cy.get( '#role_based_access' ).check();
39+
40+
for ( let role of allowedRoles ) {
41+
if ( 'feature_image_generation' === feature && ( 'contributor' === role || 'subscriber' === role ) ) {
42+
continue;
43+
}
44+
45+
const roleField = cy.get( `#classifai_${ feature }_roles_${ role }` );
46+
roleField.should( 'be.visible' );
47+
roleField.should( 'have.value', role );
48+
roleField.should( 'have.attr', 'name', `classifai_${ feature }[roles][${ role }]` );
49+
}
50+
51+
cy.get( '#role_based_access' ).uncheck();
52+
cy.get( '.allowed_roles_row' ).should( 'not.be.visible' );
53+
54+
cy.get( '#user_based_access' ).check();
55+
cy.get( '.allowed_users_row' ).should( 'be.visible' );
56+
57+
cy.get( '#user_based_access' ).uncheck();
58+
cy.get( '.allowed_users_row' ).should( 'not.be.visible' );
59+
} );
60+
} );
61+
});

tests/cypress/integration/image-processing/image-generation-openai-dalle.test.js

+27-31
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ describe( 'Image Generation (OpenAI DALL·E) Tests', () => {
22
before( () => {
33
cy.login();
44
cy.visit(
5-
'/wp-admin/tools.php?page=classifai&tab=image_processing&provider=openai_dalle'
5+
'/wp-admin/tools.php?page=classifai&tab=language_processing&feature=feature_image_generation'
66
);
7-
cy.get( '#enable_image_gen' ).check();
7+
cy.get( '#status' ).check();
88
cy.get( '#submit' ).click();
99
cy.optInAllFeatures();
1010
} );
@@ -15,15 +15,15 @@ describe( 'Image Generation (OpenAI DALL·E) Tests', () => {
1515

1616
it( 'Can save OpenAI "Image Processing" settings', () => {
1717
cy.visit(
18-
'/wp-admin/tools.php?page=classifai&tab=image_processing&provider=openai_dalle'
18+
'/wp-admin/tools.php?page=classifai&tab=language_processing&feature=feature_image_generation'
1919
);
2020

2121
cy.get( '#api_key' ).clear().type( 'password' );
2222

23-
cy.get( '#enable_image_gen' ).check();
24-
cy.get( '#openai_dalle_image_generation_roles_administrator' ).check();
25-
cy.get( '#number' ).select( '2' );
26-
cy.get( '#size' ).select( '512x512' );
23+
cy.get( '#status' ).check();
24+
cy.get( '#classifai_feature_image_generation_roles_administrator' ).check();
25+
cy.get( '#number_of_images' ).select( '2' );
26+
cy.get( '#image_size' ).select( '512x512' );
2727
cy.get( '#submit' ).click();
2828
} );
2929

@@ -80,19 +80,19 @@ describe( 'Image Generation (OpenAI DALL·E) Tests', () => {
8080
it( 'Can enable/disable image generation feature', () => {
8181
// Disable feature.
8282
cy.visit(
83-
'/wp-admin/tools.php?page=classifai&tab=image_processing&provider=openai_dalle'
83+
'/wp-admin/tools.php?page=classifai&tab=language_processing&feature=feature_image_generation'
8484
);
85-
cy.get( '#enable_image_gen' ).uncheck();
85+
cy.get( '#status' ).uncheck();
8686
cy.get( '#submit' ).click();
8787

8888
// Verify that the feature is not available.
8989
cy.verifyImageGenerationEnabled( false );
9090

9191
// Enable feature.
9292
cy.visit(
93-
'/wp-admin/tools.php?page=classifai&tab=image_processing&provider=openai_dalle'
93+
'/wp-admin/tools.php?page=classifai&tab=language_processing&feature=feature_image_generation'
9494
);
95-
cy.get( '#enable_image_gen' ).check();
95+
cy.get( '#status' ).check();
9696
cy.get( '#submit' ).click();
9797

9898
// Verify that the feature is available.
@@ -101,11 +101,11 @@ describe( 'Image Generation (OpenAI DALL·E) Tests', () => {
101101

102102
it( 'Can generate image directly in media library', () => {
103103
cy.visit(
104-
'/wp-admin/tools.php?page=classifai&tab=image_processing&provider=openai_dalle'
104+
'/wp-admin/tools.php?page=classifai&tab=language_processing&feature=feature_image_generation'
105105
);
106106

107-
cy.get( '#enable_image_gen' ).check();
108-
cy.get( '#openai_dalle_image_generation_roles_administrator' ).check();
107+
cy.get( '#status' ).check();
108+
cy.get( '#classifai_feature_image_generation_roles_administrator' ).check();
109109
cy.get( '#submit' ).click();
110110

111111
cy.visit( '/wp-admin/upload.php' );
@@ -128,26 +128,24 @@ describe( 'Image Generation (OpenAI DALL·E) Tests', () => {
128128
it( 'Can enable/disable image generation feature by role', () => {
129129
// Enable feature.
130130
cy.visit(
131-
'/wp-admin/tools.php?page=classifai&tab=image_processing&provider=openai_dalle'
131+
'/wp-admin/tools.php?page=classifai&tab=language_processing&feature=feature_image_generation'
132132
);
133-
cy.get( '#enable_image_gen' ).check();
133+
cy.get( '#status' ).check();
134134
cy.get( '#submit' ).click();
135135

136136
// Disable admin role.
137137
cy.disableFeatureForRoles(
138-
'image_generation',
139-
[ 'administrator' ],
140-
'openai_dalle'
138+
'feature_image_generation',
139+
[ 'administrator' ]
141140
);
142141

143142
// Verify that the feature is not available.
144143
cy.verifyImageGenerationEnabled( false );
145144

146145
// Enable admin role.
147146
cy.enableFeatureForRoles(
148-
'image_generation',
149-
[ 'administrator' ],
150-
'openai_dalle'
147+
'feature_image_generation',
148+
[ 'administrator' ]
151149
);
152150

153151
// Verify that the feature is available.
@@ -157,19 +155,17 @@ describe( 'Image Generation (OpenAI DALL·E) Tests', () => {
157155
it( 'Can enable/disable image generation feature by user', () => {
158156
// Disable admin role.
159157
cy.disableFeatureForRoles(
160-
'image_generation',
161-
[ 'administrator' ],
162-
'openai_dalle'
158+
'feature_image_generation',
159+
[ 'administrator' ]
163160
);
164161

165162
// Verify that the feature is not available.
166163
cy.verifyImageGenerationEnabled( false );
167164

168165
// Enable feature for admin user.
169166
cy.enableFeatureForUsers(
170-
'image_generation',
171-
[ 'admin' ],
172-
'openai_dalle'
167+
'feature_image_generation',
168+
[ 'admin' ]
173169
);
174170

175171
// Verify that the feature is available.
@@ -178,16 +174,16 @@ describe( 'Image Generation (OpenAI DALL·E) Tests', () => {
178174

179175
it( 'User can opt-out image generation feature', () => {
180176
// Enable user based opt-out.
181-
cy.enableFeatureOptOut( 'image_generation', 'openai_dalle' );
177+
cy.enableFeatureOptOut( 'feature_image_generation' );
182178

183179
// opt-out
184-
cy.optOutFeature( 'image_generation' );
180+
cy.optOutFeature( 'feature_image_generation' );
185181

186182
// Verify that the feature is not available.
187183
cy.verifyImageGenerationEnabled( false );
188184

189185
// opt-in
190-
cy.optInFeature( 'image_generation' );
186+
cy.optInFeature( 'feature_image_generation' );
191187

192188
// Verify that the feature is available.
193189
cy.verifyImageGenerationEnabled( true );

0 commit comments

Comments
 (0)