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

Ensure validation error sources property is always iterable #6846

Merged
merged 6 commits into from
Jan 21, 2022

Conversation

delawski
Copy link
Collaborator

Summary

Fixes #6827

In some specific cases, the sources property returned with a validation error may not be an array (it is null instead). However, in the Site Scan feature we're relying on a fact that sources is iterable. When it's not the case, an error is thrown in WP admin.

This PR fixes this issue in the following ways:

  1. 79c528b On server-side, we're ensuring the sources property is an array from the very beginning (as suggested in Error: s.sources is not iterable #6827 (comment) and the next comment).
  2. f3591a5 Moreover, on client-side, we're checking if sources is a non-empty array before attempting to iterate over it.

Checklist

  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

@delawski delawski added Bug Something isn't working Site Scanning labels Jan 19, 2022
@delawski delawski added this to the v2.2.1 milestone Jan 19, 2022
@delawski delawski self-assigned this Jan 19, 2022
@delawski
Copy link
Collaborator Author

@westonruter As noted in the original issue, I was not able to reproduce this error locally. So the backend part that you suggested is added as-is as I had no way to test it out.

When it comes to the JS part, I wrote some unit tests in order to make sure the issue is fixed.

@delawski delawski changed the title Fix/6827 ensure sources is iterable Ensure validation error sources property is always iterable Jan 19, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Jan 19, 2022

Plugin builds for 87a0424 are ready 🛎️!

Comment on lines 192 to 201
$item['validation_errors'] = array_map(
static function ( $validation_error ) {
if ( ! isset( $validation_error['sources'] ) || ! is_array( $validation_error['sources'] ) ) {
$validation_error['sources'] = [];
}

return $validation_error;
},
wp_list_pluck( $data, 'data' )
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change isn't needed due to the JS changes, right? Maybe we should leave the PHP changes as-is until we can identify the underlying cause. So only fix the symptoms until we can fix the underlying cause.

So that would mean reverting the PHP changes in this PR but leaving the JS changes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I'll revert the backend changes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be worthwhile to retain the PHP commit and its revert commit so that we can refer back to it when we return to identifying the underlying cause? I'm not bothered by such commits in the history 😄

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! Since the commits were reverted and then squashed, the history got messed. I've recreated the original commit along with the unit tests update (50e557b) and then reverted it (525b95c). It's now part of the git history.

@delawski delawski force-pushed the fix/6827-ensure-sources-is-iterable branch from 987ec15 to fda65f2 Compare January 20, 2022 11:05
@westonruter
Copy link
Member

Rebasing onto develop will fix the PHPUnit failures.

@delawski delawski force-pushed the fix/6827-ensure-sources-is-iterable branch from 4723f40 to 60d170a Compare January 21, 2022 11:41
Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this by manually forcing null sources via:

--- a/includes/validation/class-amp-validation-manager.php
+++ b/includes/validation/class-amp-validation-manager.php
@@ -790,7 +790,7 @@ public static function add_validation_error( array $error, array $data = [] ) {
 			$node = $data['node'];
 		}
 
-		if ( self::is_validate_request() ) {
+		if ( self::is_validate_request() && 0 ) {
 			if ( ! empty( $error['sources'] ) ) {
 				$sources = $error['sources'];
 			} elseif ( $node ) {

And:

--- a/includes/validation/class-amp-validation-manager.php
+++ b/includes/validation/class-amp-validation-manager.php
@@ -1591,7 +1591,7 @@ public static function decorate_filter_source( $value ) {
 		if ( isset( self::$current_hook_source_stack[ current_filter() ] ) ) {
 			$sources = self::$current_hook_source_stack[ current_filter() ];
 			array_pop( $sources ); // Remove self.
-			$source['sources'] = $sources;
+			$source['sources'] = null;
 		}
 		return implode(
 			'',

Before the changes in this PR I got an error on the Settings screen:

Screen Shot 2022-01-21 at 10 04 06

But after the changes in this PR, I no longer got any errors on:

  1. Settings screen
  2. Onboarding Wizard
  3. Plugin activation

@westonruter westonruter merged commit a32862f into develop Jan 21, 2022
@westonruter westonruter deleted the fix/6827-ensure-sources-is-iterable branch January 21, 2022 18:32
@westonruter westonruter added the Changelogged Whether the issue/PR has been added to release notes. label Feb 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working Changelogged Whether the issue/PR has been added to release notes. Site Scanning
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error: s.sources is not iterable
2 participants