Skip to content

disable RI by default before all tests#298

Open
Mai-Saad wants to merge 5 commits intodevelopfrom
fix/280-disable-rocket-insights-before-all-tests
Open

disable RI by default before all tests#298
Mai-Saad wants to merge 5 commits intodevelopfrom
fix/280-disable-rocket-insights-before-all-tests

Conversation

@Mai-Saad
Copy link
Copy Markdown
Contributor

@Mai-Saad Mai-Saad commented Nov 14, 2025

Description

This PR disable RI by default before running e2e tests

Fixes #280
Explain how this code impacts users.

Type of change

  • New feature (non-breaking change which adds functionality).
  • Bug fix (non-breaking change which fixes an issue).
  • Enhancement (non-breaking change which improves an existing functionality).
  • Breaking change (fix or feature that would cause existing functionality to not work as before).
  • Sub-task of #(issue number)
  • Chore
  • Release

Detailed scenario

What was tested

Running smoke test, it passed and RI wasnot displayed in UI

How to test

Run all e2e => should pass
Screenshot from 2025-11-16 11-02-32

Technical description

Documentation

  • Before all tests , we add disable filter for rocket insights in 2020 functions.php
  • Note: If any test change theme, this filter won't be applied now

New dependencies

N/A

Risks

N/A

Mandatory Checklist

Code validation

  • I validated all the Acceptance Criteria. If possible, provide screenshots or videos.
  • I triggered all changed lines of code at least once without new errors/warnings/notices.
  • I implemented built-in tests to cover the new/changed code.

Code style

  • I wrote a self-explanatory code about what it does.
  • I protected entry points against unexpected inputs.
  • I did not introduce unnecessary complexity.
  • Output messages (errors, notices, logs) are explicit enough for users to understand the issue and are actionnable.

Unticked items justification

N/A

Additional Checks

  • In the case of complex code, I wrote comments to explain it.
  • When possible, I prepared ways to observe the implemented system (logs, data, etc.).
  • I added error handling logic when using functions that could throw errors (HTTP/API request, filesystem, etc.)

@wordpressfan
Copy link
Copy Markdown
Collaborator

In this PR we are adding a filter inside the active theme functions.php which I don't like for many reasons.
I believe the best way to do this is to add a checkbox or select inside e2e-helper plugin to enable or disable Rocket Insights and tell e2e to go to this page and disable it from UI without the need to do anything technical and this will make the test itself cleaner.
What do u think @wp-media/engineering-wpr-plugin-team ?

Copy link
Copy Markdown
Contributor

@Miraeld Miraeld left a comment

Choose a reason for hiding this comment

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

Overall Assessment

This PR successfully addresses issue #280 by disabling Rocket Insights before all E2E tests run. The implementation is functional but has some concerns that should be addressed.

Strengths ✅

  1. Clear Problem Solution: Directly addresses the issue of Rocket Insights interfering with E2E tests
  2. Idempotent Operations: Both addFilterToTheme and removeFilterFromTheme check for existence before adding/removing
  3. Good Error Handling: Proper error checking and informative console messages
  4. Proper Cleanup: Filter is removed in AfterAll hook

Critical Issues 🚨

1. Theme Dependency Fragility

The solution hardcodes the twentytwenty theme. As noted in the PR description:

"If any test change theme, this filter won't be applied now"

This creates a maintenance burden and potential failure point. Consider:

  • Using a more robust approach (e.g., a custom plugin or mu-plugin)
  • At minimum, add a check to verify the active theme or document this limitation clearly

2. Unused Dependency

ssh2-sftp-client was added to package.json but is not used anywhere in the codebase. This should be removed unless there's a planned use.

3. .gitignore Change

The Codacy instructions file is being ignored, which suggests it shouldn't be tracked. If it's generated, this is fine, but if it contains important project rules, it should be committed.

Code Quality Issues 📝

In utils/commands.ts:

  1. Inconsistent Whitespace (lines 61-62 in hooks.ts):
         await addFilterToTheme('rocket_rocket_insights_enabled', '__return_false', 'twentytwenty');
        
         browser = await chromium.launch({ headless: false });

Extra leading spaces before both lines - should be consistent with surrounding code.

  1. Shell Command Complexity: The heredoc approach with temporary files works but is complex. Consider:

    • Using a simpler approach with escaped quotes
    • Or using a proper file manipulation library
  2. Missing Input Validation: The functions don't validate that:

    • The theme exists
    • The functions.php file is writable
    • The filter name/callback are valid

Suggestions for Improvement 💡

High Priority:

  1. Remove unused dependency: Delete ssh2-sftp-client from package.json
  2. Fix whitespace issues in hooks.ts
  3. Consider alternative approach: As suggested in the timeline comments, using a UI checkbox in the e2e-helper plugin would be cleaner and more maintainable

Medium Priority:

  1. Add input validation to the filter functions
  2. Make theme name configurable via environment variable or config
  3. Add error handling for cases where the theme doesn't exist

Low Priority:

  1. Consider extracting the shell command logic into a separate utility
  2. Add unit tests for the new functions

Testing Verification ✓

The PR shows that smoke tests passed with RI disabled, which is good. However:

  • Should verify behavior when theme is switched during tests
  • Should test the cleanup in AfterAll actually works

Recommendation

Conditional Approval - The core functionality works, but I recommend addressing at least:

  1. Remove the unused ssh2-sftp-client dependency
  2. Fix the whitespace issues
  3. Add a clear comment/documentation about the theme dependency limitation

The team should also consider the alternative approach suggested by @wordpressfan using the e2e-helper plugin, which would be more robust long-term.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds functionality to disable Rocket Insights (RI) by default before running e2e tests by adding filters to the WordPress theme's functions.php file. The implementation adds utility functions to manage theme filters and integrates them into the test lifecycle hooks.

  • Adds three new utility functions to manipulate WordPress theme files via SSH
  • Modifies test hooks to add/remove the RI disable filter before/after all tests
  • Updates dependencies (ssh2, nan, cpu-features)

Reviewed Changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 4 comments.

File Description
utils/commands.ts Adds checkFunctionsPhpAccess, addFilterToTheme, and removeFilterFromTheme functions to manage WordPress theme filters via SSH commands
src/support/hooks.ts Integrates filter management into BeforeAll/AfterAll hooks, adds isPluginInstalled check before activation, and imports new utility functions
package-lock.json Updates dependency versions for ssh2 (1.14.0 → 1.17.0), nan (2.17.0 → 2.23.1), and cpu-features (0.0.8 → 0.0.10)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


/**
* After all tests, closes the Chromium browser.
* After all tests, closes the Chromium browser and remove filter added
Copy link

Copilot AI Nov 18, 2025

Choose a reason for hiding this comment

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

The comment says "wasnot" but should be "was not" (two separate words). This is a minor spelling error in the PR description context.

Suggested change
* After all tests, closes the Chromium browser and remove filter added
* After all tests, closes the Chromium browser and removes filter added

Copilot uses AI. Check for mistakes.
Comment on lines +645 to +662
const filterLine = `add_filter( '${filter}', '${callback}' );`;

// Use a multi-step approach: create pattern file, check if exists, add if not found
const patternFile = `${wpDir}wp-content/themes/${themeName}/.filter_pattern.tmp`;

// First check if the filter already exists, only add if not found
const command = wrapSSHPrefix(`cat > ${patternFile} << 'PHPEOF'
${filterLine}
PHPEOF
if grep -q -F -f ${patternFile} ${functionsPath}; then
echo "Filter already exists, skipping addition"
else
cat >> ${functionsPath} << 'PHPEOF'
${filterLine}
PHPEOF
echo "Filter added"
fi
rm ${patternFile}`);
Copy link

Copilot AI Nov 18, 2025

Choose a reason for hiding this comment

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

Potential security issue: The shell commands constructed using template literals with user-provided values (filter, callback, themeName) are vulnerable to command injection. Although these values may be controlled in the test context, it's best practice to properly escape or sanitize them before using in shell commands.

Copilot uses AI. Check for mistakes.
Comment on lines +702 to +717
const filterLine = `add_filter( '${filter}', '${callback}' );`;

// Use a multi-step approach: create pattern file, use grep, then cleanup
const patternFile = `${wpDir}wp-content/themes/${themeName}/.filter_pattern.tmp`;

// First check if the filter exists, then remove it if found
const command = wrapSSHPrefix(`cat > ${patternFile} << 'PATTERNEOF'
${filterLine}
PATTERNEOF
if grep -q -F -f ${patternFile} ${functionsPath}; then
grep -v -F -f ${patternFile} ${functionsPath} > ${functionsPath}.tmp && mv ${functionsPath}.tmp ${functionsPath}
echo "Filter removed"
else
echo "Filter not found, skipping removal"
fi
rm ${patternFile}`);
Copy link

Copilot AI Nov 18, 2025

Choose a reason for hiding this comment

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

Potential security issue: Similar to addFilterToTheme, this function is vulnerable to command injection through the filter, callback, and themeName parameters. The values should be properly escaped or sanitized before being used in shell commands.

Copilot uses AI. Check for mistakes.
@Mai-Saad
Copy link
Copy Markdown
Contributor Author

Will open another PR for this #298 (comment), which may result in closing the current PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Disable Rocket insights by default on e2e

4 participants