Skip to content

Commit

Permalink
Add ElasticPress and install during our tests that need it. Fix unit …
Browse files Browse the repository at this point in the history
…tests
  • Loading branch information
dkotter committed Sep 12, 2024
1 parent 1fe1bec commit 28c5326
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .wp-env.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{

Check warning on line 1 in .wp-env.json

View workflow job for this annotation

GitHub Actions / eslint

File ignored by default.
"plugins": [".", "./tests/test-plugin", "https://downloads.wordpress.org/plugin/classic-editor.zip"],
"plugins": [".", "./tests/test-plugin", "https://downloads.wordpress.org/plugin/classic-editor.zip", "https://downloads.wordpress.org/plugin/elasticpress.zip"],
"env": {
"tests": {
"mappings": {
Expand Down
6 changes: 3 additions & 3 deletions includes/Classifai/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ function get_classification_mode(): string {
/**
* Get all parts from the current URL.
*
* For instance, if the URL is `https://example.com/this/is/a/test`,
* For instance, if the URL is `https://example.com/this/is/a/test/`,
* this function will return: `[ 'this', 'is', 'a', 'test' ]`.
*
* @return array
Expand All @@ -677,7 +677,7 @@ function get_url_slugs(): array {

$parts = explode( '/', $wp->request );

return $parts;
return array_filter( $parts );
}

/**
Expand All @@ -691,7 +691,7 @@ function get_url_slugs(): array {
function get_last_url_slug(): string {
$parts = get_url_slugs();

return end( $parts );
return trim( end( $parts ) );
}

/**
Expand Down
4 changes: 3 additions & 1 deletion tests/Classifai/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,9 @@ public function test_get_post_statuses() {
public function test_get_url_slugs() {
global $wp;

$wp->request = 'https://example.com/this/is/a/test/';
// If URL is https://www.example.com/this/is/a/test/
// $wp->request will be 'this/is/a/test'.
$wp->request = 'this/is/a/test';

$slugs = get_url_slugs();
$this->assertEquals( [ 'this', 'is', 'a', 'test' ], $slugs );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@ describe( '[Language processing] Smart 404 - Azure OpenAI Tests', () => {
cy.login();
} );

it( "See error message if ElasticPress isn't activate", () => {
cy.disableElasticPress();

cy.visit(
'/wp-admin/tools.php?page=classifai&tab=language_processing&feature=feature_smart_404'
);

cy.get( '.classifai-nlu-sections .notice-error' ).should( 'exist' );
} );

it( 'Can save Smart 404 settings', () => {
cy.enableElasticPress();

cy.visit(
'/wp-admin/tools.php?page=classifai&tab=language_processing&feature=feature_smart_404'
);
Expand Down Expand Up @@ -45,5 +57,7 @@ describe( '[Language processing] Smart 404 - Azure OpenAI Tests', () => {

// Save settings.
cy.get( '#submit' ).click();

cy.disableElasticPress();
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@ describe( '[Language processing] Smart 404 - OpenAI Tests', () => {
cy.login();
} );

it( "See error message if ElasticPress isn't activate", () => {
cy.disableElasticPress();

cy.visit(
'/wp-admin/tools.php?page=classifai&tab=language_processing&feature=feature_smart_404'
);

cy.get( '.classifai-nlu-sections .notice-error' ).should( 'exist' );
} );

it( 'Can save Smart 404 settings', () => {
cy.enableElasticPress();

cy.visit(
'/wp-admin/tools.php?page=classifai&tab=language_processing&feature=feature_smart_404'
);
Expand All @@ -31,5 +43,7 @@ describe( '[Language processing] Smart 404 - OpenAI Tests', () => {

// Save settings.
cy.get( '#submit' ).click();

cy.disableElasticPress();
} );
} );
24 changes: 24 additions & 0 deletions tests/cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,3 +574,27 @@ Cypress.Commands.add( 'enableClassicEditor', () => {
}
} );
} );

/**
* Activate the ElasticPress plugin.
*/
Cypress.Commands.add( 'enableElasticPress', () => {
cy.visit( '/wp-admin/plugins.php' );
cy.get( 'body' ).then( ( $body ) => {
if ( $body.find( '#activate-elasticpress' ).length > 0 ) {
cy.get( '#activate-elasticpress' ).click();
}
} );
} );

/**
* Deactivate the Classic Editor plugin.
*/
Cypress.Commands.add( 'disableElasticPress', () => {
cy.visit( '/wp-admin/plugins.php' );
cy.get( 'body' ).then( ( $body ) => {
if ( $body.find( '#deactivate-elasticpress' ).length > 0 ) {
cy.get( '#deactivate-elasticpress' ).click();
}
} );
} );

0 comments on commit 28c5326

Please sign in to comment.