Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limited external connection warning incorrect #1140

Merged
merged 9 commits into from
Oct 11, 2023
5 changes: 0 additions & 5 deletions assets/css/admin-external-connection.css
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@
}

.endpoint-result[data-endpoint-state="error"]::before,
.endpoint-result[data-endpoint-state="warning"]::before,
.endpoint-result[data-endpoint-state="valid"]::before {
content: " ";
display: inline-block;
Expand All @@ -129,10 +128,6 @@
background-color: #62ff00;
}

.endpoint-result[data-endpoint-state="warning"]::before {
background-color: #ffe000;
}

.endpoint-result .dashicons-yes {
color: #46b450;
}
Expand Down
4 changes: 0 additions & 4 deletions assets/css/admin-external-connections.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,3 @@
.connection-status.valid {
background-color: #62ff00;
}

.connection-status.warning {
background-color: #ffe000;
}
95 changes: 28 additions & 67 deletions assets/js/admin-external-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,12 @@ function checkConnections() {
.done( ( response ) => {
if ( ! response.success ) {
endpointResult.setAttribute( 'data-endpoint-state', 'error' );
} else if ( response.data.errors.no_external_connection ) {
} else if (
response.data.errors.no_external_connection ||
'no' === response.data.is_authenticated ||
response.data.errors.no_distributor ||
! response.data.can_post.length
) {
endpointResult.setAttribute( 'data-endpoint-state', 'error' );

if ( response.data.endpoint_suggestion ) {
Expand All @@ -337,88 +342,44 @@ function checkConnections() {
}`,
'polite'
);
} else {
} else if ( response.data.errors.no_distributor ) {
endpointResult.innerText = __(
'No connection found.',
'distributor'
);

speak(
__( 'No connection found.', 'distributor' ),
'polite'
);
}
} else if (
response.data.errors.no_distributor ||
! response.data.can_post.length
) {
endpointResult.setAttribute( 'data-endpoint-state', 'warning' );
endpointResult.innerText = __(
'Limited connection established.',
'distributor'
);

const warnings = [];

if ( response.data.errors.no_distributor ) {
endpointResult.innerText += ` ${ __(
'Distributor not installed on remote site.',
'distributor'
) }`;
);
speak(
`${ __(
'Limited connection established.',
'distributor'
) } ${ __(
__(
'Distributor not installed on remote site.',
'distributor'
) }`,
),
'polite'
);
} else {
} else if (
'no' === response.data.is_authenticated ||
response.data.errors.no_types
) {
endpointResult.innerText = __(
'Authentication failed due to insufficient or invalid credentials.',
'distributor'
);
speak(
`${ __(
'Limited connection established.',
__(
'Authentication failed due to insufficient or invalid credentials.',
'distributor'
) }`,
),
'polite'
);
}

if ( 'no' === response.data.is_authenticated ) {
warnings.push(
__(
'Authentication failed due to invalid credentials.',
'distributor'
)
} else {
theskinnyghost marked this conversation as resolved.
Show resolved Hide resolved
endpointResult.innerText = __(
'No connection found.',
'distributor'
);
}

if ( 'yes' === response.data.is_authenticated ) {
warnings.push(
__(
'Authentication succeeded but your account does not have permissions to create posts on the external site.',
'distributor'
)
speak(
__( 'No connection found.', 'distributor' ),
'polite'
);
}

warnings.push(
__( 'Push distribution unavailable.', 'distributor' )
);
warnings.push(
__(
'Pull distribution limited to basic content, i.e. title and content body.',
'distributor'
)
);

warnings.forEach( ( warning ) => {
const warningNode = document.createElement( 'li' );
warningNode.innerText = warning;

endpointErrors.append( warningNode );
} );
} else {
endpointResult.setAttribute( 'data-endpoint-state', 'valid' );
endpointResult.innerText = __(
Expand Down
2 changes: 1 addition & 1 deletion includes/debug-info.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ function get_external_connection_status( $external_connection_status ) {
}

if ( empty( $external_connection_status['can_post'] ) ) {
$status = __( 'warning', 'distributor' );
$status = __( 'error', 'distributor' );
}
}

Expand Down
2 changes: 1 addition & 1 deletion includes/external-connection-cpt.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function output_status_column( $column_name, $post_id ) {
}

if ( empty( $external_connection_status['can_post'] ) ) {
$status = 'warning';
$status = 'error';
}
}

Expand Down
7 changes: 5 additions & 2 deletions includes/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,8 +600,11 @@ function check_post_types_permissions() {
);

foreach ( $types as $type ) {
$caps = $type->cap;
$response['can_get'][] = $type->name;
$caps = $type->cap;

if ( current_user_can( $caps->edit_posts ) ) {
$response['can_get'][] = $type->name;
}

if ( current_user_can( $caps->edit_posts ) && current_user_can( $caps->create_posts ) && current_user_can( $caps->publish_posts ) ) {
$response['can_post'][] = $type->name;
Expand Down
27 changes: 3 additions & 24 deletions tests/cypress/e2e/external-connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,6 @@ describe( 'Admin can add a new external connection', () => {
cy.get( '#title' ).should( 'have.value', name );
} );

it( 'Should display warning status', () => {
cy.visit( '/wp-admin/admin.php?page=distributor' );
cy.get( '.page-title-action' ).contains( 'Add New' ).click();

const name = randomName();
cy.get( '#title' ).click().type( name );

cy.get( '.manual-setup-button' ).click();
cy.get( '#dt_external_connection_url' ).type(
'http://' + randomName()
);
cy.get( '#create-connection' ).click();

cy.visit( '/wp-admin/admin.php?page=distributor' );
cy.get( '.row-title' )
.contains( name )
.closest( '.hentry' )
.find( '.connection-status' )
.should( 'have.class', 'warning' );
} );

it( 'Should display error status', () => {
cy.visit( '/wp-admin/admin.php?page=distributor' );
cy.get( '.page-title-action' ).contains( 'Add New' ).click();
Expand All @@ -96,7 +75,7 @@ describe( 'Admin can add a new external connection', () => {
.should( 'have.class', 'error' );
} );

it( 'Should display limited connection warning', () => {
it( 'Should display error when credentials are invalid', () => {
cy.visit( '/wp-admin/admin.php?page=distributor' );
cy.get( '.page-title-action' ).contains( 'Add New' ).click();

Expand All @@ -109,9 +88,9 @@ describe( 'Admin can add a new external connection', () => {
cy.get( '#dt_external_connection_url' ).type(
'http://localhost/second/wp-json'
);
cy.get( '.description.endpoint-result' ).should(
cy.get( '.endpoint-result' ).should(
'contain.text',
'Limited connection established.'
'Authentication failed due to insufficient or invalid credentials.'
);
} );
} );