Skip to content

Commit

Permalink
Fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
JakePT committed Sep 27, 2023
1 parent 808e0e5 commit 640280b
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 69 deletions.
14 changes: 7 additions & 7 deletions assets/js/sync-ui/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
* Window dependencies.
*/
const {
auto_start_index: autoIndex,
api_url: apiUrl,
index_meta: indexMeta = null,
autoIndex,
apiUrl,
indexMeta,
indexables,
is_epio: isEpio,
ep_last_sync_date: lastSyncDateTime = null,
ep_last_sync_failed: lastSyncFailed = false,
isEpio,
lastSyncDateTime,
lastSyncFailed,
nonce,
post_types: postTypes,
postTypes,
} = window.epDash;

export {
Expand Down
8 changes: 0 additions & 8 deletions assets/js/sync/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,16 +254,8 @@ export const SyncProvider = ({
* @returns {void}
*/
(indexMeta) => {
const isInitialSync = stateRef.current.lastSyncDateTime === null;

/**
* We should not appear to be deleting if this is the first sync.
*/
const isDeleting = isInitialSync ? false : indexMeta.put_mapping;

updateState({
isCli: indexMeta.method === 'cli',
isDeleting,
isSyncing: true,
itemsProcessed: getItemsProcessedFromIndexMeta(indexMeta),
itemsTotal: getItemsTotalFromIndexMeta(indexMeta),
Expand Down
33 changes: 19 additions & 14 deletions includes/classes/Screen/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,27 @@ public function admin_enqueue_scripts() {
Utils\get_asset_info( 'sync-script', 'version' )
);

$data = [];
$indexables = Indexables::factory()->get_all();

$ep_last_sync = IndexHelper::factory()->get_last_sync();
$indexables = Indexables::factory()->get_all();
$post_types = Indexables::factory()->get( 'post' )->get_indexable_post_types();
$post_types = array_values( $post_types );
$indices_comparison = Elasticsearch::factory()->get_indices_comparison();
$indices_missing = count( $indices_comparison['missing_indices'] ) > 0;

$data['api_url'] = rest_url( 'elasticpress/v1/sync' );
$data['auto_start_index'] = isset( $_GET['do_sync'] ) && ( ! defined( 'EP_DASHBOARD_SYNC' ) || EP_DASHBOARD_SYNC ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$data['ep_last_sync_date'] = ! empty( $ep_last_sync['end_date_time'] ) ? $ep_last_sync['end_date_time'] : false;
$data['ep_last_sync_failed'] = ! empty( $ep_last_sync['failed'] ) || ! empty( $ep_last_sync['errors'] ) ? true : false;
$data['index_meta'] = Utils\get_indexing_status();
$data['indexables'] = array_map( fn( $indexable) => [ $indexable->slug, $indexable->labels['plural'] ], $indexables );
$data['is_epio'] = Utils\is_epio();
$data['nonce'] = wp_create_nonce( 'wp_rest' );
$data['post_types'] = array_map( fn( $post_type ) => [ $post_type, get_post_type_object( $post_type )->labels->name ], $post_types );
$last_sync = ! $indices_missing ? IndexHelper::factory()->get_last_sync() : [];

$post_types = Indexables::factory()->get( 'post' )->get_indexable_post_types();
$post_types = array_values( $post_types );

$data = [
'apiUrl' => rest_url( 'elasticpress/v1/sync' ),
'autoIndex' => isset( $_GET['do_sync'] ) && ( ! defined( 'EP_DASHBOARD_SYNC' ) || EP_DASHBOARD_SYNC ), // phpcs:ignore WordPress.Security.NonceVerification.Recommended
'indexMeta' => Utils\get_indexing_status(),
'lastSyncDateTime' => ! empty( $last_sync['end_date_time'] ) ? $last_sync['end_date_time'] : null,
'lastSyncFailed' => ! empty( $last_sync['failed'] ) || ! empty( $last_sync['errors'] ) ? true : false,
'indexables' => array_map( fn( $indexable) => [ $indexable->slug, $indexable->labels['plural'] ], $indexables ),
'isEpio' => Utils\is_epio(),
'nonce' => wp_create_nonce( 'wp_rest' ),
'postTypes' => array_map( fn( $post_type ) => [ $post_type, get_post_type_object( $post_type )->labels->name ], $post_types ),
];

wp_localize_script( 'ep_sync_scripts', 'epDash', $data );
}
Expand Down
33 changes: 16 additions & 17 deletions tests/cypress/integration/dashboard-sync.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,27 @@ describe('Dashboard Sync', () => {
* fresh sync" checkbox should not appear.
*/
cy.visitAdminPage('admin.php?page=elasticpress-sync');
cy.get('.components-checkbox-control').contains('Delete all data').should('not.exist');
cy.contains('.components-checkbox-control', 'Delete all data').should('not.exist');

/**
* Perform an initial sync.
*/
cy.get('@syncPanel').find('.ep-sync-button').click();
cy.contains('.components-button', 'Start sync').click();

/**
* The sync log should indicate that the sync completed and that
* mapping was sent.
*/
cy.get('@syncPanel').find('.components-form-toggle').click();
cy.get('@syncPanel')
.find('.ep-sync-messages', { timeout: Cypress.config('elasticPressIndexTimeout') })
cy.contains('.components-button', 'Log').click();
cy.get('.ep-sync-messages', { timeout: Cypress.config('elasticPressIndexTimeout') })
.should('contain.text', 'Mapping sent')
.should('contain.text', 'Sync complete');

/**
* After the initial sync is complete the "Delete all data and start
* fresh sync" checkbox should appear.
*/
cy.get('.components-checkbox-control').contains('Delete all data').should('exist');
cy.contains('.components-checkbox-control', 'Delete all data').should('exist');
});

it('Can sync via Dashboard when activated in single site', () => {
Expand All @@ -78,7 +77,7 @@ describe('Dashboard Sync', () => {
);

cy.visitAdminPage('admin.php?page=elasticpress-sync');
cy.get('.components-button').contains('Start sync').click();
cy.contains('.components-button', 'Start sync').click();
cy.get('.ep-sync-progress strong', {
timeout: Cypress.config('elasticPressIndexTimeout'),
}).should('contain.text', 'Sync complete');
Expand Down Expand Up @@ -106,7 +105,7 @@ describe('Dashboard Sync', () => {
);

cy.visitAdminPage('network/admin.php?page=elasticpress-sync');
cy.get('.components-button').contains('Start sync').click();
cy.contains('.components-button', 'Start sync').click();
cy.get('.ep-sync-progress strong', {
timeout: Cypress.config('elasticPressIndexTimeout'),
}).should('contain.text', 'Sync complete');
Expand Down Expand Up @@ -140,9 +139,9 @@ describe('Dashboard Sync', () => {

// Start sync via dashboard and pause it
cy.intercept('POST', '/wp-json/elasticpress/v1/sync*').as('apiRequest');
cy.get('.components-button').contains('Resume sync').click();
cy.contains('.components-button', 'Start sync').click();
cy.wait('@apiRequest').its('response.statusCode').should('eq', 200);
cy.get('.components-button').contains('Pause sync').click();
cy.contains('.components-button', 'Pause sync').click();

// Can not activate a feature.
cy.visitAdminPage('admin.php?page=elasticpress');
Expand All @@ -155,7 +154,7 @@ describe('Dashboard Sync', () => {

// Check if it is paused
cy.visitAdminPage('admin.php?page=elasticpress-sync');
cy.get('.components-button').contains('Resume sync').should('be.visible');
cy.contains('.components-button', 'Resume sync').should('be.visible');
cy.get('.ep-sync-progress strong').should('contain.text', 'Sync paused');

resumeAndWait();
Expand All @@ -169,23 +168,23 @@ describe('Dashboard Sync', () => {
});

it('Should only display a single sync option if index is deleted', () => {
// Enable Terms
cy.wpCli('wp elasticpress activate-feature terms', true);
cy.wpCli('wp elasticpress delete-index --yes', true);

/**
* If an index is missing the "Delete all data and start fresh sync"
* checkbox should not appear.
*/
cy.visitAdminPage('admin.php?page=elasticpress-sync');
cy.get('.components-checkbox-control').contains('Delete all data').should('not.exist');
cy.contains('.components-checkbox-control', 'Delete all data').should('not.exist');

// Send mapping of the deleted index
cy.wpCli('wp elasticpress put-mapping --indexables=term');
// Send mapping
cy.wpCli('wp elasticpress put-mapping');

/**
* After the mapping is sent the "Delete all data and start fresh
* sync" checkbox should appear.
*/
cy.get('.components-checkbox-control').contains('Delete all data').should('exist');
cy.reload();
cy.contains('.components-checkbox-control', 'Delete all data').should('exist');
});
});
8 changes: 3 additions & 5 deletions tests/cypress/integration/features/comments.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,10 @@ describe('Comments Feature', { tags: '@slow' }, () => {
return true;
});

cy.get('.ep-sync-panel').last().as('syncPanel');
cy.get('@syncPanel').find('.components-form-toggle').click();
cy.get('@syncPanel')
.find('.ep-sync-messages', { timeout: Cypress.config('elasticPressIndexTimeout') })
.should('contain.text', 'Sync complete')
cy.contains('.components-button', 'Log').click();
cy.get('.ep-sync-messages', { timeout: Cypress.config('elasticPressIndexTimeout') })
.should('contain.text', 'Mapping sent')
.should('contain.text', 'Sync complete')
// check that the number of approved comments is the same as the default.
.should('contain.text', `Number of comments indexed: ${defaultApprovedComments}`);

Expand Down
6 changes: 2 additions & 4 deletions tests/cypress/integration/features/protected-content.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ describe('Protected Content Feature', () => {
return true;
});

cy.get('.ep-sync-panel').last().as('syncPanel');
cy.get('@syncPanel').find('.components-form-toggle').click();
cy.get('@syncPanel')
.find('.ep-sync-messages', { timeout: Cypress.config('elasticPressIndexTimeout') })
cy.contains('.components-button', 'Log').click();
cy.get('.ep-sync-messages', { timeout: Cypress.config('elasticPressIndexTimeout') })
.should('contain.text', 'Mapping sent')
.should('contain.text', 'Sync complete');

Expand Down
10 changes: 4 additions & 6 deletions tests/cypress/integration/features/terms.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ describe('Terms Feature', { tags: '@slow' }, () => {
return true;
});

cy.get('.ep-sync-panel').last().as('syncPanel');
cy.get('@syncPanel').find('.components-form-toggle').click();
cy.get('@syncPanel')
.find('.ep-sync-messages', { timeout: Cypress.config('elasticPressIndexTimeout') })
.should('contain.text', 'Sync complete')
.should('contain.text', 'Mapping sent');
cy.contains('.components-button', 'Log').click();
cy.get('.ep-sync-messages', { timeout: Cypress.config('elasticPressIndexTimeout') })
.should('contain.text', 'Mapping sent')
.should('contain.text', 'Sync complete');

cy.wpCli('wp elasticpress list-features').its('stdout').should('contain', 'terms');
});
Expand Down
12 changes: 4 additions & 8 deletions tests/cypress/integration/features/woocommerce.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@ describe('WooCommerce Feature', { tags: '@slow' }, () => {
return true;
});

cy.get('.ep-sync-panel').last().as('syncPanel');
cy.get('@syncPanel').find('.components-form-toggle').click();
cy.get('@syncPanel')
.find('.ep-sync-messages', { timeout: Cypress.config('elasticPressIndexTimeout') })
cy.contains('.components-button', 'Log').click();
cy.get('.ep-sync-messages', { timeout: Cypress.config('elasticPressIndexTimeout') })
.should('contain.text', 'Mapping sent')
.should('contain.text', 'Sync complete');

Expand Down Expand Up @@ -348,10 +346,8 @@ describe('WooCommerce Feature', { tags: '@slow' }, () => {
/**
* Syncing should complete.
*/
cy.get('.ep-sync-panel').last().as('syncPanel');
cy.get('@syncPanel').find('.components-form-toggle').click();
cy.get('@syncPanel')
.find('.ep-sync-messages', { timeout: Cypress.config('elasticPressIndexTimeout') })
cy.contains('.components-button', 'Log').click();
cy.get('.ep-sync-messages', { timeout: Cypress.config('elasticPressIndexTimeout') })
.should('contain.text', 'Mapping sent')
.should('contain.text', 'Sync complete');
});
Expand Down

0 comments on commit 640280b

Please sign in to comment.