Skip to content

Conversation

@prettyboymp
Copy link
Contributor

Changes proposed in this Pull Request:

Updates the PTKPatternsStore so that it uses the newer action_scheduler_ensure_recurring_actions action to keep the schedule of fetching updated PTK Patterns instead of running a query to see if the action already exists.

Closes WOOPLUG-5765 / #61722 .

How to test the changes in this Pull Request:

Using the WooCommerce Testing Instructions Guide, include your detailed testing instructions:

  1. Enable tracking via Admin > WooCommerce > Settings > Advanced > WooCommerce.com
  2. Go to Admin > WooCommerce > Status > Scheduled Actions
  3. Search for fetch_patterns.
  4. Ensure there is a pending scheduled action scheduled for daily.
  5. If you just enabled tracking, the an instance of the `fetch_patterns action should have just run, or past due.
  6. Disable tracking.
  7. Go back to the scheduled actions and make sure there are no longer pending scheduled fetch_patterns actions.

@github-actions github-actions bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Oct 31, 2025
@prettyboymp prettyboymp marked this pull request as ready for review October 31, 2025 20:00
@woocommercebot woocommercebot requested review from a team and joshuatf and removed request for a team October 31, 2025 20:01
@github-actions
Copy link
Contributor

github-actions bot commented Oct 31, 2025

Testing Guidelines

Hi @joshuatf ,

Apart from reviewing the code changes, please make sure to review the testing instructions (Guide) and verify that relevant tests (E2E, Unit, Integration, etc.) have been added or updated as needed.

Reminder: PR reviewers are required to document testing performed. This includes:

  • 🖼️ Screenshots or screen recordings.
  • 📝 List of functionality tested / steps followed.
  • 🌐 Site details (environment attributes such as hosting type, plugins, theme, store size, store age, and relevant settings).
  • 🔍 Any analysis performed, such as assessing potential impacts on environment attributes and other plugins, conducting performance profiling, or using LLM/AI-based analysis.

⚠️ Within the testing details you provide, please ensure that no sensitive information (such as API keys, passwords, user data, etc.) is included in this public issue.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 31, 2025

📝 Walkthrough

Walkthrough

Moves PTK Patterns fetching from a timestamp/option-based mechanism to Action Scheduler recurring actions, adds a DB updater to remove the legacy last_fetch_patterns_request option and unschedule legacy fetch_patterns actions, refactors PTKPatternsStore to use recurring scheduling/unscheduling, and adds tests for the new behavior.

Changes

Cohort / File(s) Summary
Changelog & Installer
plugins/woocommerce/changelog/WOOPLUG-5765-use-as-ensure-action-for-ptkpattern-fetching, plugins/woocommerce/includes/class-wc-install.php
Adds changelog entry and registers wc_update_1040_cleanup_legacy_ptk_patterns_fetching in the v10.4.0 DB updater callbacks (replaces a previously referenced function name).
DB Updater
plugins/woocommerce/includes/wc-update-functions.php
Adds wc_update_1040_cleanup_legacy_ptk_patterns_fetching() which deletes the last_fetch_patterns_request option and calls as_unschedule_all_actions('fetch_patterns', ..., 'woocommerce') to remove legacy scheduled actions.
PTK Patterns Store
plugins/woocommerce/src/Blocks/Patterns/PTKPatternsStore.php
Removes LAST_FETCH_PATTERNS_REQUEST_OPTION_NAME. Adds ensure_recurring_fetch_patterns_if_enabled() and hooks it to action_scheduler_ensure_recurring_actions. Replaces option/timestamp checks with Action Scheduler recurring checks (as_has_scheduled_action / as_schedule_recurring_action), uses as_unschedule_all_actions in flush_cached_patterns(), and stops calling flush_cached_patterns() at the start of fetch_patterns().
Unit Tests — Updater
plugins/woocommerce/tests/php/includes/wc-update-functions-test.php
Adds test_wc_update_1040_cleanup_legacy_ptk_patterns_fetching() asserting the legacy option is removed and legacy fetch_patterns action(s) unscheduled. (Duplicate method definition present in diff.)
Unit Tests — PTKPatternsStore
plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
Adds strict types, makes setUp() public, adds tearDown(), unschedules fetch_patterns actions in setup/teardown, updates assertions to include 'woocommerce' group for scheduled actions, and adds tests for recurring scheduling when tracking enabled, no scheduling when disabled, and that flush_cached_patterns() unschedules all actions.

Sequence Diagram

sequenceDiagram
    participant WP as WordPress
    participant AS as Action Scheduler
    participant Store as PTKPatternsStore
    participant Cache as Patterns Cache

    rect rgba(220,240,255,0.6)
    Note over WP,Cache: Legacy flow (timestamp/option based — cleaned up)
    WP->>Store: get_patterns()
    alt cache present & timestamp fresh (legacy)
        Store->>Cache: return cached
    else
        Store->>Store: fetch_patterns() (legacy would set option)
        Store->>Cache: update cache
    end
    end

    rect rgba(240,255,240,0.6)
    Note over WP,AS: New flow (recurring Action Scheduler)
    WP->>AS: action_scheduler_ensure_recurring_actions
    AS->>Store: ensure_recurring_fetch_patterns_if_enabled()
    alt tracking enabled & no recurring scheduled
        Store->>AS: as_schedule_recurring_action(fetch_patterns, interval=DAY_IN_SECONDS)
    end
    AS->>Store: fetch_patterns() [on schedule]
    Store->>Cache: update cache
    alt flush_cached_patterns() called
        Store->>AS: as_unschedule_all_actions(fetch_patterns, ... , 'woocommerce')
        Store->>Cache: clear cache
    end
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~30 minutes

Areas requiring extra attention:

  • Duplicate test method test_wc_update_1040_cleanup_legacy_ptk_patterns_fetching() in plugins/woocommerce/tests/php/includes/wc-update-functions-test.php.
  • Verify action_scheduler_ensure_recurring_actions hook is correctly registered and that as_has_scheduled_action()/as_schedule_recurring_action() use the 'woocommerce' group consistently to avoid duplicate scheduling.
  • Confirm removing the initial flush_cached_patterns() call from fetch_patterns() doesn't introduce stale-cache edge cases.
  • Ensure as_unschedule_all_actions('fetch_patterns', ..., 'woocommerce') targets only intended actions and contexts.
  • Check test setup/teardown reliably isolate Action Scheduler state and options between tests.

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title Check ✅ Passed The PR title "Use 'action_scheduler_ensure_recurring_actions' for scheduling PTK Pattern fetches" directly and accurately summarizes the primary change in the changeset. According to the raw summary, the core objective is to update PTKPatternsStore to use the action_scheduler_ensure_recurring_actions action for maintaining recurring pattern fetches instead of the previous query-based approach. The title is specific, concise, and clearly communicates this change without vague terminology, emojis, or unnecessary noise. It accurately reflects the main architectural improvement proposed in the PR.
Description Check ✅ Passed The PR description is clearly related to the changeset and provides meaningful information about the proposed changes. It explicitly states that PTKPatternsStore is being updated to use action_scheduler_ensure_recurring_actions instead of running queries to check if an action exists, which aligns with the modifications shown in the raw summary across PTKPatternsStore.php and related files. Additionally, the description includes actionable testing instructions and references the related issue WOOPLUG-5765, demonstrating a clear understanding of the changeset's purpose and intent.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch WOOPLUG-5765/use-as-ensure-action-for-ptkpattern-fetching

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Nitpick comments (3)
plugins/woocommerce/src/Blocks/Patterns/PTKPatternsStore.php (1)

96-102: Minor: early-return is fine; consider immediate fetch on enable

Current path only ensures the recurring job exists. If product wants an immediate refresh when tracking flips to “yes”, enqueue a single action too:

 public function ensure_recurring_fetch_patterns_if_enabled() {
   if ( ! $this->allowed_tracking_is_enabled() ) {
     return;
   }
   $this->schedule_action_if_not_pending( 'fetch_patterns' );
+  // Optionally kick an immediate run the first time.
+  if ( ! as_has_scheduled_action( 'fetch_patterns', array(), self::AS_GROUP ) ) {
+    as_schedule_single_action( time(), 'fetch_patterns', array(), self::AS_GROUP, true );
+  }
 }
plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php (2)

460-473: Strengthen “no duplicates” assertion

Comparing timestamps via as_next_scheduled_action() can miss duplicates scheduled at the same time. Assert count=1 instead:

- $first_action_id = as_next_scheduled_action( 'fetch_patterns' );
- $this->assertNotFalse( $first_action_id, 'First action should be scheduled' );
- // Call again - should not create duplicate.
- $this->pattern_store->ensure_recurring_fetch_patterns_if_enabled();
- $second_action_id = as_next_scheduled_action( 'fetch_patterns' );
- $this->assertEquals( $first_action_id, $second_action_id, 'Should not create duplicate action' );
+ $scheduled = as_get_scheduled_actions(
+   array( 'hook' => 'fetch_patterns', 'status' => \ActionScheduler_Store::STATUS_PENDING, 'per_page' => -1 )
+ );
+ $this->assertCount( 1, $scheduled, 'Exactly one action should be scheduled' );
+ // Call again - should not create duplicate.
+ $this->pattern_store->ensure_recurring_fetch_patterns_if_enabled();
+ $scheduled_after = as_get_scheduled_actions(
+   array( 'hook' => 'fetch_patterns', 'status' => \ActionScheduler_Store::STATUS_PENDING, 'per_page' => -1 )
+ );
+ $this->assertCount( 1, $scheduled_after, 'No duplicate should be created' );

478-490: Optional: verify flush uses group if one is added

If you adopt a group (e.g., woocommerce-ptk-patterns), update test assertions to pass the group to as_has_scheduled_action and as_unschedule_all_actions for precision.

📜 Review details

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cc1de6c and c070432.

📒 Files selected for processing (6)
  • plugins/woocommerce/changelog/WOOPLUG-5765-use-as-ensure-action-for-ptkpattern-fetching (1 hunks)
  • plugins/woocommerce/includes/class-wc-install.php (1 hunks)
  • plugins/woocommerce/includes/wc-update-functions.php (1 hunks)
  • plugins/woocommerce/src/Blocks/Patterns/PTKPatternsStore.php (6 hunks)
  • plugins/woocommerce/tests/php/includes/wc-update-functions-test.php (1 hunks)
  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php (3 hunks)
🧰 Additional context used
📓 Path-based instructions (4)
**/*.{php,js,jsx,ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/code-quality.mdc)

**/*.{php,js,jsx,ts,tsx}: Guard against unexpected inputs
Sanitize and validate any potentially dangerous inputs
Ensure code is backwards compatible
Write code that is readable and intuitive
Ensure code has unit or E2E tests where applicable

Files:

  • plugins/woocommerce/includes/wc-update-functions.php
  • plugins/woocommerce/includes/class-wc-install.php
  • plugins/woocommerce/src/Blocks/Patterns/PTKPatternsStore.php
  • plugins/woocommerce/tests/php/includes/wc-update-functions-test.php
  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
**/*.{php,js,jsx,tsx,ts}

📄 CodeRabbit inference engine (.cursor/rules/avoid-regex.mdc)

**/*.{php,js,jsx,tsx,ts}: Avoid using regular expressions unless absolutely necessary to favor readability and maintainability
Only consider regex when no built-in language alternative (string/array APIs) fits the need
Only use regex when performance is critical and faster than alternatives, with measurements to justify
Use regex for complex pattern matching only if the pattern is well-documented and thoroughly tested
Allow regex when maintaining legacy code where an existing, correct pattern is being modified
If regex is necessary, document the pattern extensively to explain what it matches
If regex is necessary, add comprehensive tests covering edge cases and potential security issues
Use named capture groups in regex to improve readability when supported
Validate input before applying regex to ensure it is safe
Assess and mitigate security risks when using regex, including ReDoS and injection vulnerabilities
Avoid regex patterns that can cause catastrophic backtracking (ReDoS)
Do not construct regex from untrusted input to prevent injection attacks
Ensure regex patterns do not overmatch and unintentionally capture unexpected inputs

Files:

  • plugins/woocommerce/includes/wc-update-functions.php
  • plugins/woocommerce/includes/class-wc-install.php
  • plugins/woocommerce/src/Blocks/Patterns/PTKPatternsStore.php
  • plugins/woocommerce/tests/php/includes/wc-update-functions-test.php
  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
**/*.{php,js,ts,jsx,tsx}

⚙️ CodeRabbit configuration file

**/*.{php,js,ts,jsx,tsx}: Don't trust that extension developers will follow the best practices, make sure the code:

  • Guards against unexpected inputs.
  • Sanitizes and validates any potentially dangerous inputs.
  • Is backwards compatible.
  • Is readable and intuitive.
  • Has unit or E2E tests where applicable.
    When making any changes to code that deletes or modifies orders/products/customer data, make sure that there are
    sufficient checks in place to prevent accidental data loss. As an example, if deleting a draft order, check that
    the order status is indeed draft or checkout-draft. Also think about whether race conditions could occur and
    delete orders that don't belong to the current customer. When in doubt, ask for clarification in the PR comments.

Files:

  • plugins/woocommerce/includes/wc-update-functions.php
  • plugins/woocommerce/includes/class-wc-install.php
  • plugins/woocommerce/src/Blocks/Patterns/PTKPatternsStore.php
  • plugins/woocommerce/tests/php/includes/wc-update-functions-test.php
  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
plugins/woocommerce/tests/**/*.php

📄 CodeRabbit inference engine (.cursor/rules/woo-phpunit.mdc)

plugins/woocommerce/tests/**/*.php: Ensure test classes define public setUp() and tearDown() methods (not protected)
All PHPUnit test methods must be public, not protected
Add declare(strict_types=1); at the top of each test file
Test classes should extend WC_Unit_Test_Case

Files:

  • plugins/woocommerce/tests/php/includes/wc-update-functions-test.php
  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
🧠 Learnings (13)
📓 Common learnings
Learnt from: opr
Repo: woocommerce/woocommerce PR: 60360
File: plugins/woocommerce/includes/class-woocommerce.php:1553-1563
Timestamp: 2025-08-15T13:00:31.543Z
Learning: The learning from PR #60360 about not needing to clean up legacy scheduled actions when hook names change is specific to that PR's context: PR #59325 was released in WC 10.1 with an incorrect hook name (action_scheduler_schedule_recurring_actions instead of action_scheduler_ensure_recurring_actions), so no actions were actually queued under the old hook name. This is not a general rule for all hook name changes.
Learnt from: opr
Repo: woocommerce/woocommerce PR: 60360
File: plugins/woocommerce/includes/class-woocommerce.php:1533-1544
Timestamp: 2025-08-14T07:34:00.169Z
Learning: When a WordPress action hook name is changed in a PR (like from action_scheduler_schedule_recurring_actions to action_scheduler_ensure_recurring_actions), any code that was previously hooked to the old hook name would not have been executed, so there's no need to clean up or unschedule any legacy scheduled actions that were supposedly created by the old hook.
Learnt from: senadir
Repo: woocommerce/woocommerce PR: 59325
File: plugins/woocommerce/includes/wc-formatting-functions.php:1211-1226
Timestamp: 2025-07-01T16:24:23.871Z
Learning: Action Scheduler is bundled into WooCommerce and always available, so there's no need to add fallback logic to WordPress cron functions when using Action Scheduler functions like as_schedule_single_action() and as_unschedule_all_actions() in WooCommerce code.
📚 Learning: 2025-08-14T07:34:00.169Z
Learnt from: opr
Repo: woocommerce/woocommerce PR: 60360
File: plugins/woocommerce/includes/class-woocommerce.php:1533-1544
Timestamp: 2025-08-14T07:34:00.169Z
Learning: When a WordPress action hook name is changed in a PR (like from action_scheduler_schedule_recurring_actions to action_scheduler_ensure_recurring_actions), any code that was previously hooked to the old hook name would not have been executed, so there's no need to clean up or unschedule any legacy scheduled actions that were supposedly created by the old hook.

Applied to files:

  • plugins/woocommerce/changelog/WOOPLUG-5765-use-as-ensure-action-for-ptkpattern-fetching
  • plugins/woocommerce/src/Blocks/Patterns/PTKPatternsStore.php
📚 Learning: 2025-08-15T13:00:31.543Z
Learnt from: opr
Repo: woocommerce/woocommerce PR: 60360
File: plugins/woocommerce/includes/class-woocommerce.php:1553-1563
Timestamp: 2025-08-15T13:00:31.543Z
Learning: The learning from PR #60360 about not needing to clean up legacy scheduled actions when hook names change is specific to that PR's context: PR #59325 was released in WC 10.1 with an incorrect hook name (action_scheduler_schedule_recurring_actions instead of action_scheduler_ensure_recurring_actions), so no actions were actually queued under the old hook name. This is not a general rule for all hook name changes.

Applied to files:

  • plugins/woocommerce/changelog/WOOPLUG-5765-use-as-ensure-action-for-ptkpattern-fetching
  • plugins/woocommerce/src/Blocks/Patterns/PTKPatternsStore.php
📚 Learning: 2025-06-17T07:07:53.443Z
Learnt from: samueljseay
Repo: woocommerce/woocommerce PR: 58716
File: plugins/woocommerce/client/blocks/assets/js/blocks/mini-cart/iapi-frontend.ts:83-101
Timestamp: 2025-06-17T07:07:53.443Z
Learning: In WooCommerce blocks, when porting existing code patterns that have known issues (like parseInt truncating decimal money values), maintain consistency with existing implementation rather than making isolated fixes. The preference is for systematic refactoring approaches (like broader Dinero adoption) over piecemeal changes.

Applied to files:

  • plugins/woocommerce/src/Blocks/Patterns/PTKPatternsStore.php
📚 Learning: 2025-07-01T16:24:23.871Z
Learnt from: senadir
Repo: woocommerce/woocommerce PR: 59325
File: plugins/woocommerce/includes/wc-formatting-functions.php:1211-1226
Timestamp: 2025-07-01T16:24:23.871Z
Learning: Action Scheduler is bundled into WooCommerce and always available, so there's no need to add fallback logic to WordPress cron functions when using Action Scheduler functions like as_schedule_single_action() and as_unschedule_all_actions() in WooCommerce code.

Applied to files:

  • plugins/woocommerce/src/Blocks/Patterns/PTKPatternsStore.php
📚 Learning: 2025-08-18T06:11:48.768Z
Learnt from: CR
Repo: woocommerce/woocommerce PR: 0
File: .cursor/rules/woo-phpunit.mdc:0-0
Timestamp: 2025-08-18T06:11:48.768Z
Learning: Applies to plugins/woocommerce/tests/**/*.php : Add declare(strict_types=1); at the top of each test file

Applied to files:

  • plugins/woocommerce/tests/php/includes/wc-update-functions-test.php
  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-08-18T06:11:48.768Z
Learnt from: CR
Repo: woocommerce/woocommerce PR: 0
File: .cursor/rules/woo-phpunit.mdc:0-0
Timestamp: 2025-08-18T06:11:48.768Z
Learning: Applies to plugins/woocommerce/tests/**/*.php : All PHPUnit test methods must be public, not protected

Applied to files:

  • plugins/woocommerce/tests/php/includes/wc-update-functions-test.php
  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-10-24T13:25:49.900Z
Learnt from: hannahtinkler
Repo: woocommerce/woocommerce PR: 61384
File: plugins/woocommerce/tests/php/src/Internal/PushNotifications/Entities/PushTokenTest.php:208-216
Timestamp: 2025-10-24T13:25:49.900Z
Learning: In plugins/woocommerce/tests/**/*.php files, omit setUp() and tearDown() methods when they are not needed, as the project's linting configuration makes it easier to omit these methods than to add them with phpcs:ignore comments.

Applied to files:

  • plugins/woocommerce/tests/php/includes/wc-update-functions-test.php
  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-08-18T06:11:48.768Z
Learnt from: CR
Repo: woocommerce/woocommerce PR: 0
File: .cursor/rules/woo-phpunit.mdc:0-0
Timestamp: 2025-08-18T06:11:48.768Z
Learning: Applies to plugins/woocommerce/tests/**/*.php : Test classes should extend WC_Unit_Test_Case

Applied to files:

  • plugins/woocommerce/tests/php/includes/wc-update-functions-test.php
📚 Learning: 2025-09-03T11:50:02.208Z
Learnt from: tpaksu
Repo: woocommerce/woocommerce PR: 60735
File: plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/Fulfillments/class-wc-rest-fulfillments-v4-controller-tests.php:10-10
Timestamp: 2025-09-03T11:50:02.208Z
Learning: For REST API controller tests in plugins/woocommerce/tests/**/*.php, test classes should extend WC_REST_Unit_Test_Case instead of WC_Unit_Test_Case because REST routes need to be registered before tests run.

Applied to files:

  • plugins/woocommerce/tests/php/includes/wc-update-functions-test.php
📚 Learning: 2025-08-14T08:20:36.357Z
Learnt from: Aljullu
Repo: woocommerce/woocommerce PR: 60191
File: plugins/woocommerce/client/blocks/tests/e2e/tests/single-product-template/single-product-template-compatibility-layer.spec.ts:4-4
Timestamp: 2025-08-14T08:20:36.357Z
Learning: In WooCommerce e2e tests, some test files intentionally use hard-coded "woocommerce/woocommerce//" template IDs instead of BLOCK_THEME_SLUG to test backwards compatibility with the legacy plugin-scoped template ID format. These should not be updated to use the dynamic slug constant as they serve a specific testing purpose.

Applied to files:

  • plugins/woocommerce/tests/php/includes/wc-update-functions-test.php
  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-08-14T08:22:34.886Z
Learnt from: Aljullu
Repo: woocommerce/woocommerce PR: 60191
File: plugins/woocommerce/client/blocks/tests/e2e/tests/product-filters/rating-filter-editor.block_theme.spec.ts:36-36
Timestamp: 2025-08-14T08:22:34.886Z
Learning: In WooCommerce e2e tests, some test files intentionally use hard-coded "woocommerce/woocommerce//" template slugs (not just template IDs) instead of BLOCK_THEME_SLUG to test backwards compatibility with the legacy plugin-scoped template slug format. These should not be updated to use the dynamic slug constant as they serve a specific backwards compatibility testing purpose.

Applied to files:

  • plugins/woocommerce/tests/php/includes/wc-update-functions-test.php
📚 Learning: 2025-06-25T15:39:25.166Z
Learnt from: vladolaru
Repo: woocommerce/woocommerce PR: 59160
File: plugins/woocommerce/tests/php/src/Internal/Admin/Settings/PaymentsProviders/WooPayments/WooPaymentsServiceTest.php:360-373
Timestamp: 2025-06-25T15:39:25.166Z
Learning: Test mocks should accurately simulate the behavior of the functions they replace, including return value semantics. For WordPress functions like `update_option`, the mock should properly return `false` when the value hasn't changed and `true` when it has, to avoid masking logic errors in tests.

Applied to files:

  • plugins/woocommerce/tests/php/includes/wc-update-functions-test.php
  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
🧬 Code graph analysis (2)
plugins/woocommerce/tests/php/includes/wc-update-functions-test.php (1)
plugins/woocommerce/includes/wc-update-functions.php (1)
  • wc_update_1040_remove_last_fetch_patterns_request_option (3136-3138)
plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php (1)
plugins/woocommerce/src/Blocks/Patterns/PTKPatternsStore.php (3)
  • PTKPatternsStore (13-295)
  • flush_cached_patterns (187-192)
  • ensure_recurring_fetch_patterns_if_enabled (96-102)
🔇 Additional comments (7)
plugins/woocommerce/changelog/WOOPLUG-5765-use-as-ensure-action-for-ptkpattern-fetching (1)

1-4: Changelog entry looks good

Accurately reflects the scheduling change. No action needed.

plugins/woocommerce/src/Blocks/Patterns/PTKPatternsStore.php (3)

49-49: Good move to action_scheduler_ensure_recurring_actions

Hook selection is correct for “ensure” semantics with Action Scheduler. LGTM.


126-129: Comment aligns with behavior

Scheduling on missing/invalid cache is fine; no fetch-in-constructor surprises. LGTM.


190-192: The suggested fix references a non-existent constant and misidentifies a non-issue

The as_unschedule_all_actions() call and as_schedule_recurring_action() call are already internally consistent—both omit the group parameter and default to an empty group. The referenced self::AS_GROUP constant does not exist in this class.

While using explicit group scoping is a defensive practice, the current code is not broken. If group scoping is desired, first define an AS_GROUP class constant, then use it in both the scheduling (line 111 in schedule_action_if_not_pending()) and unscheduling (line 191) calls. Search results show the 'fetch_patterns' hook is used only within this class, further reducing collision risk.

Likely an incorrect or invalid review comment.

plugins/woocommerce/includes/class-wc-install.php (1)

310-312: Updater wiring looks correct

wc_update_1040_remove_last_fetch_patterns_request_option is added to 10.4.0 callbacks. LGTM.

plugins/woocommerce/includes/wc-update-functions.php (1)

3129-3137: Updater implementation is minimal and correct

delete_option( 'last_fetch_patterns_request' ); is sufficient. No further changes needed.

plugins/woocommerce/tests/php/includes/wc-update-functions-test.php (1)

251-267: Test covers the updater well

Asserts option removal after running the updater. LGTM.

Please add strict types per repo test guidelines. Minimal patch:

 <?php
+declare(strict_types=1);

If this file already declares strict types elsewhere, ignore.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 31, 2025

Test using WordPress Playground

The changes in this pull request can be previewed and tested using a WordPress Playground instance.
WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Test this pull request with WordPress Playground.

Note that this URL is valid for 30 days from when this comment was last updated. You can update it by closing/reopening the PR or pushing a new commit.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
plugins/woocommerce/tests/php/includes/wc-update-functions-test.php (1)

1-8: Add strict types declaration

Per the coding guidelines for test files, a declare(strict_types=1); statement should be added at the top of the file.

Apply this diff:

 <?php
+declare(strict_types=1);
+
 /**
  * Update functions tests
  *
  * @package WooCommerce\Tests\Functions.
  */
🧹 Nitpick comments (1)
plugins/woocommerce/src/Blocks/Patterns/PTKPatternsStore.php (1)

110-116: Use a class constant for the Action Scheduler group and add uniqueness constraint

The hardcoded 'woocommerce' group string is repeated in three locations (lines 111, 115, and 191), which makes the code harder to maintain and prone to typos. Additionally, passing unique=true to as_schedule_recurring_action prevents race-condition duplicates when multiple processes might attempt to schedule the same action.

Apply this diff:

+ private const AS_GROUP = 'woocommerce-ptk-patterns';
+
  private function schedule_action_if_not_pending( $action ) {
-   if ( as_has_scheduled_action( $action, array(), 'woocommerce' ) ) {
+   if ( as_has_scheduled_action( $action, array(), self::AS_GROUP ) ) {
      return;
    }
-   as_schedule_recurring_action( time(), DAY_IN_SECONDS, $action, array(), 'woocommerce' );
+   as_schedule_recurring_action( time(), DAY_IN_SECONDS, $action, array(), self::AS_GROUP, true );
  }

Also update line 191 to use the same constant:

- as_unschedule_all_actions( 'fetch_patterns', array(), 'woocommerce' );
+ as_unschedule_all_actions( 'fetch_patterns', array(), self::AS_GROUP );
📜 Review details

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bee7667 and fa8ccc4.

📒 Files selected for processing (5)
  • plugins/woocommerce/includes/class-wc-install.php (1 hunks)
  • plugins/woocommerce/includes/wc-update-functions.php (1 hunks)
  • plugins/woocommerce/src/Blocks/Patterns/PTKPatternsStore.php (6 hunks)
  • plugins/woocommerce/tests/php/includes/wc-update-functions-test.php (1 hunks)
  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
  • plugins/woocommerce/includes/class-wc-install.php
🧰 Additional context used
📓 Path-based instructions (4)
**/*.{php,js,jsx,ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/code-quality.mdc)

**/*.{php,js,jsx,ts,tsx}: Guard against unexpected inputs
Sanitize and validate any potentially dangerous inputs
Ensure code is backwards compatible
Write code that is readable and intuitive
Ensure code has unit or E2E tests where applicable

Files:

  • plugins/woocommerce/tests/php/includes/wc-update-functions-test.php
  • plugins/woocommerce/includes/wc-update-functions.php
  • plugins/woocommerce/src/Blocks/Patterns/PTKPatternsStore.php
plugins/woocommerce/tests/**/*.php

📄 CodeRabbit inference engine (.cursor/rules/woo-phpunit.mdc)

plugins/woocommerce/tests/**/*.php: Ensure test classes define public setUp() and tearDown() methods (not protected)
All PHPUnit test methods must be public, not protected
Add declare(strict_types=1); at the top of each test file
Test classes should extend WC_Unit_Test_Case

Files:

  • plugins/woocommerce/tests/php/includes/wc-update-functions-test.php
**/*.{php,js,jsx,tsx,ts}

📄 CodeRabbit inference engine (.cursor/rules/avoid-regex.mdc)

**/*.{php,js,jsx,tsx,ts}: Avoid using regular expressions unless absolutely necessary to favor readability and maintainability
Only consider regex when no built-in language alternative (string/array APIs) fits the need
Only use regex when performance is critical and faster than alternatives, with measurements to justify
Use regex for complex pattern matching only if the pattern is well-documented and thoroughly tested
Allow regex when maintaining legacy code where an existing, correct pattern is being modified
If regex is necessary, document the pattern extensively to explain what it matches
If regex is necessary, add comprehensive tests covering edge cases and potential security issues
Use named capture groups in regex to improve readability when supported
Validate input before applying regex to ensure it is safe
Assess and mitigate security risks when using regex, including ReDoS and injection vulnerabilities
Avoid regex patterns that can cause catastrophic backtracking (ReDoS)
Do not construct regex from untrusted input to prevent injection attacks
Ensure regex patterns do not overmatch and unintentionally capture unexpected inputs

Files:

  • plugins/woocommerce/tests/php/includes/wc-update-functions-test.php
  • plugins/woocommerce/includes/wc-update-functions.php
  • plugins/woocommerce/src/Blocks/Patterns/PTKPatternsStore.php
**/*.{php,js,ts,jsx,tsx}

⚙️ CodeRabbit configuration file

**/*.{php,js,ts,jsx,tsx}: Don't trust that extension developers will follow the best practices, make sure the code:

  • Guards against unexpected inputs.
  • Sanitizes and validates any potentially dangerous inputs.
  • Is backwards compatible.
  • Is readable and intuitive.
  • Has unit or E2E tests where applicable.
    When making any changes to code that deletes or modifies orders/products/customer data, make sure that there are
    sufficient checks in place to prevent accidental data loss. As an example, if deleting a draft order, check that
    the order status is indeed draft or checkout-draft. Also think about whether race conditions could occur and
    delete orders that don't belong to the current customer. When in doubt, ask for clarification in the PR comments.

Files:

  • plugins/woocommerce/tests/php/includes/wc-update-functions-test.php
  • plugins/woocommerce/includes/wc-update-functions.php
  • plugins/woocommerce/src/Blocks/Patterns/PTKPatternsStore.php
🧠 Learnings (11)
📓 Common learnings
Learnt from: opr
Repo: woocommerce/woocommerce PR: 60360
File: plugins/woocommerce/includes/class-woocommerce.php:1553-1563
Timestamp: 2025-08-15T13:00:31.543Z
Learning: The learning from PR #60360 about not needing to clean up legacy scheduled actions when hook names change is specific to that PR's context: PR #59325 was released in WC 10.1 with an incorrect hook name (action_scheduler_schedule_recurring_actions instead of action_scheduler_ensure_recurring_actions), so no actions were actually queued under the old hook name. This is not a general rule for all hook name changes.
Learnt from: opr
Repo: woocommerce/woocommerce PR: 60360
File: plugins/woocommerce/includes/class-woocommerce.php:1533-1544
Timestamp: 2025-08-14T07:34:00.169Z
Learning: When a WordPress action hook name is changed in a PR (like from action_scheduler_schedule_recurring_actions to action_scheduler_ensure_recurring_actions), any code that was previously hooked to the old hook name would not have been executed, so there's no need to clean up or unschedule any legacy scheduled actions that were supposedly created by the old hook.
Learnt from: senadir
Repo: woocommerce/woocommerce PR: 59325
File: plugins/woocommerce/includes/wc-formatting-functions.php:1211-1226
Timestamp: 2025-07-01T16:24:23.871Z
Learning: Action Scheduler is bundled into WooCommerce and always available, so there's no need to add fallback logic to WordPress cron functions when using Action Scheduler functions like as_schedule_single_action() and as_unschedule_all_actions() in WooCommerce code.
Learnt from: prettyboymp
Repo: woocommerce/woocommerce PR: 60272
File: .github/workflows/release-open-issue-warning.yml:15-23
Timestamp: 2025-08-08T13:52:26.423Z
Learning: Repository: woocommerce/woocommerce
Context: .github/workflows/release-open-issue-warning.yml and similar workflows
Learning: Maintainers (per prettyboymp) are fine requiring third-party modules inside actions/github-script without adding setup-node/createRequire. Prefer to keep this pattern and defer structural changes unless it demonstrably causes issues; minor safeguards like version pinning are acceptable if needed.
📚 Learning: 2025-10-24T13:25:49.900Z
Learnt from: hannahtinkler
Repo: woocommerce/woocommerce PR: 61384
File: plugins/woocommerce/tests/php/src/Internal/PushNotifications/Entities/PushTokenTest.php:208-216
Timestamp: 2025-10-24T13:25:49.900Z
Learning: In plugins/woocommerce/tests/**/*.php files, omit setUp() and tearDown() methods when they are not needed, as the project's linting configuration makes it easier to omit these methods than to add them with phpcs:ignore comments.

Applied to files:

  • plugins/woocommerce/tests/php/includes/wc-update-functions-test.php
📚 Learning: 2025-08-18T06:11:48.768Z
Learnt from: CR
Repo: woocommerce/woocommerce PR: 0
File: .cursor/rules/woo-phpunit.mdc:0-0
Timestamp: 2025-08-18T06:11:48.768Z
Learning: Applies to plugins/woocommerce/tests/**/*.php : All PHPUnit test methods must be public, not protected

Applied to files:

  • plugins/woocommerce/tests/php/includes/wc-update-functions-test.php
📚 Learning: 2025-08-18T06:11:48.768Z
Learnt from: CR
Repo: woocommerce/woocommerce PR: 0
File: .cursor/rules/woo-phpunit.mdc:0-0
Timestamp: 2025-08-18T06:11:48.768Z
Learning: Applies to plugins/woocommerce/tests/**/*.php : Add declare(strict_types=1); at the top of each test file

Applied to files:

  • plugins/woocommerce/tests/php/includes/wc-update-functions-test.php
📚 Learning: 2025-08-14T08:20:36.357Z
Learnt from: Aljullu
Repo: woocommerce/woocommerce PR: 60191
File: plugins/woocommerce/client/blocks/tests/e2e/tests/single-product-template/single-product-template-compatibility-layer.spec.ts:4-4
Timestamp: 2025-08-14T08:20:36.357Z
Learning: In WooCommerce e2e tests, some test files intentionally use hard-coded "woocommerce/woocommerce//" template IDs instead of BLOCK_THEME_SLUG to test backwards compatibility with the legacy plugin-scoped template ID format. These should not be updated to use the dynamic slug constant as they serve a specific testing purpose.

Applied to files:

  • plugins/woocommerce/tests/php/includes/wc-update-functions-test.php
📚 Learning: 2025-08-18T06:11:48.768Z
Learnt from: CR
Repo: woocommerce/woocommerce PR: 0
File: .cursor/rules/woo-phpunit.mdc:0-0
Timestamp: 2025-08-18T06:11:48.768Z
Learning: Applies to plugins/woocommerce/tests/**/*.php : Test classes should extend WC_Unit_Test_Case

Applied to files:

  • plugins/woocommerce/tests/php/includes/wc-update-functions-test.php
📚 Learning: 2025-06-25T15:39:25.166Z
Learnt from: vladolaru
Repo: woocommerce/woocommerce PR: 59160
File: plugins/woocommerce/tests/php/src/Internal/Admin/Settings/PaymentsProviders/WooPayments/WooPaymentsServiceTest.php:360-373
Timestamp: 2025-06-25T15:39:25.166Z
Learning: Test mocks should accurately simulate the behavior of the functions they replace, including return value semantics. For WordPress functions like `update_option`, the mock should properly return `false` when the value hasn't changed and `true` when it has, to avoid masking logic errors in tests.

Applied to files:

  • plugins/woocommerce/tests/php/includes/wc-update-functions-test.php
📚 Learning: 2025-08-15T13:00:31.543Z
Learnt from: opr
Repo: woocommerce/woocommerce PR: 60360
File: plugins/woocommerce/includes/class-woocommerce.php:1553-1563
Timestamp: 2025-08-15T13:00:31.543Z
Learning: The learning from PR #60360 about not needing to clean up legacy scheduled actions when hook names change is specific to that PR's context: PR #59325 was released in WC 10.1 with an incorrect hook name (action_scheduler_schedule_recurring_actions instead of action_scheduler_ensure_recurring_actions), so no actions were actually queued under the old hook name. This is not a general rule for all hook name changes.

Applied to files:

  • plugins/woocommerce/includes/wc-update-functions.php
  • plugins/woocommerce/src/Blocks/Patterns/PTKPatternsStore.php
📚 Learning: 2025-08-14T07:34:00.169Z
Learnt from: opr
Repo: woocommerce/woocommerce PR: 60360
File: plugins/woocommerce/includes/class-woocommerce.php:1533-1544
Timestamp: 2025-08-14T07:34:00.169Z
Learning: When a WordPress action hook name is changed in a PR (like from action_scheduler_schedule_recurring_actions to action_scheduler_ensure_recurring_actions), any code that was previously hooked to the old hook name would not have been executed, so there's no need to clean up or unschedule any legacy scheduled actions that were supposedly created by the old hook.

Applied to files:

  • plugins/woocommerce/includes/wc-update-functions.php
  • plugins/woocommerce/src/Blocks/Patterns/PTKPatternsStore.php
📚 Learning: 2025-07-01T16:24:23.871Z
Learnt from: senadir
Repo: woocommerce/woocommerce PR: 59325
File: plugins/woocommerce/includes/wc-formatting-functions.php:1211-1226
Timestamp: 2025-07-01T16:24:23.871Z
Learning: Action Scheduler is bundled into WooCommerce and always available, so there's no need to add fallback logic to WordPress cron functions when using Action Scheduler functions like as_schedule_single_action() and as_unschedule_all_actions() in WooCommerce code.

Applied to files:

  • plugins/woocommerce/includes/wc-update-functions.php
  • plugins/woocommerce/src/Blocks/Patterns/PTKPatternsStore.php
📚 Learning: 2025-06-17T07:07:53.443Z
Learnt from: samueljseay
Repo: woocommerce/woocommerce PR: 58716
File: plugins/woocommerce/client/blocks/assets/js/blocks/mini-cart/iapi-frontend.ts:83-101
Timestamp: 2025-06-17T07:07:53.443Z
Learning: In WooCommerce blocks, when porting existing code patterns that have known issues (like parseInt truncating decimal money values), maintain consistency with existing implementation rather than making isolated fixes. The preference is for systematic refactoring approaches (like broader Dinero adoption) over piecemeal changes.

Applied to files:

  • plugins/woocommerce/src/Blocks/Patterns/PTKPatternsStore.php
🧬 Code graph analysis (1)
plugins/woocommerce/tests/php/includes/wc-update-functions-test.php (1)
plugins/woocommerce/includes/wc-update-functions.php (1)
  • wc_update_1040_cleanup_legacy_ptk_patterns_fetching (3136-3139)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (32)
  • GitHub Check: Blocks e2e tests 9/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests - Legacy MiniCart 1/3 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 8/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 10/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 7/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 6/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests - Legacy MiniCart - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 5/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 1/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 2/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Metrics - @woocommerce/plugin-woocommerce [performance]
  • GitHub Check: Core e2e tests 6/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests 3/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 4/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: PHP: 8.4 WP: latest (HPOS:off) [WP latest] 2/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: Blocks e2e tests 3/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests 5/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: PHP: 8.4 WP: latest (HPOS:off) [WP latest] 1/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: Core API tests - @woocommerce/plugin-woocommerce [api]
  • GitHub Check: PHP: 8.4 WP: latest [WP latest] 1/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: PHP: 7.4 WP: latest - 1 [WP 6.7.4] 2/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: Core e2e tests 2/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests 1/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: PHP: 8.4 WP: latest - Legacy MiniCart [WP latest] 1/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: PHP: 7.4 WP: pre-release [WP 6.9-beta2] 2/2 - @woocommerce/plugin-woocommerce [unit:php] (optional)
  • GitHub Check: PHP: 7.4 WP: pre-release [WP 6.9-beta2] 1/2 - @woocommerce/plugin-woocommerce [unit:php] (optional)
  • GitHub Check: PHP: 7.4 WP: latest - 1 [WP 6.7.4] 1/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: Core e2e tests 4/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: PHP: 8.4 WP: latest [WP latest] 2/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: PHP: 8.4 WP: latest - Legacy MiniCart [WP latest] 2/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: Lint - @woocommerce/plugin-woocommerce
  • GitHub Check: build
🔇 Additional comments (4)
plugins/woocommerce/src/Blocks/Patterns/PTKPatternsStore.php (2)

49-49: LGTM: Hook integration for recurring action scheduler

The integration with action_scheduler_ensure_recurring_actions correctly follows the new WooCommerce pattern for managing recurring scheduled actions.


96-102: LGTM: Guard condition properly prevents scheduling

The method correctly checks tracking status before scheduling and properly delegates to the existing scheduling logic.

plugins/woocommerce/includes/wc-update-functions.php (1)

3136-3139: LGTM: Cleanup correctly removes all legacy actions

The updater function properly:

  • Removes the obsolete last_fetch_patterns_request option
  • Unschedules all fetch_patterns actions (correctly omitting the group parameter to clean up both legacy ungrouped actions and any grouped actions)

This ensures a clean migration to the new recurring Action Scheduler approach implemented in PTKPatternsStore.

plugins/woocommerce/tests/php/includes/wc-update-functions-test.php (1)

256-274: LGTM: Test properly verifies cleanup behavior

The test correctly:

  • Sets up legacy state (option and ungrouped scheduled action)
  • Verifies the cleanup function removes both
  • Uses clear assertions with helpful failure messages

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fa8ccc4 and 9139915.

📒 Files selected for processing (1)
  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php (5 hunks)
🧰 Additional context used
📓 Path-based instructions (4)
**/*.{php,js,jsx,ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/code-quality.mdc)

**/*.{php,js,jsx,ts,tsx}: Guard against unexpected inputs
Sanitize and validate any potentially dangerous inputs
Ensure code is backwards compatible
Write code that is readable and intuitive
Ensure code has unit or E2E tests where applicable

Files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
plugins/woocommerce/tests/**/*.php

📄 CodeRabbit inference engine (.cursor/rules/woo-phpunit.mdc)

plugins/woocommerce/tests/**/*.php: Ensure test classes define public setUp() and tearDown() methods (not protected)
All PHPUnit test methods must be public, not protected
Add declare(strict_types=1); at the top of each test file
Test classes should extend WC_Unit_Test_Case

Files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
**/*.{php,js,jsx,tsx,ts}

📄 CodeRabbit inference engine (.cursor/rules/avoid-regex.mdc)

**/*.{php,js,jsx,tsx,ts}: Avoid using regular expressions unless absolutely necessary to favor readability and maintainability
Only consider regex when no built-in language alternative (string/array APIs) fits the need
Only use regex when performance is critical and faster than alternatives, with measurements to justify
Use regex for complex pattern matching only if the pattern is well-documented and thoroughly tested
Allow regex when maintaining legacy code where an existing, correct pattern is being modified
If regex is necessary, document the pattern extensively to explain what it matches
If regex is necessary, add comprehensive tests covering edge cases and potential security issues
Use named capture groups in regex to improve readability when supported
Validate input before applying regex to ensure it is safe
Assess and mitigate security risks when using regex, including ReDoS and injection vulnerabilities
Avoid regex patterns that can cause catastrophic backtracking (ReDoS)
Do not construct regex from untrusted input to prevent injection attacks
Ensure regex patterns do not overmatch and unintentionally capture unexpected inputs

Files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
**/*.{php,js,ts,jsx,tsx}

⚙️ CodeRabbit configuration file

**/*.{php,js,ts,jsx,tsx}: Don't trust that extension developers will follow the best practices, make sure the code:

  • Guards against unexpected inputs.
  • Sanitizes and validates any potentially dangerous inputs.
  • Is backwards compatible.
  • Is readable and intuitive.
  • Has unit or E2E tests where applicable.
    When making any changes to code that deletes or modifies orders/products/customer data, make sure that there are
    sufficient checks in place to prevent accidental data loss. As an example, if deleting a draft order, check that
    the order status is indeed draft or checkout-draft. Also think about whether race conditions could occur and
    delete orders that don't belong to the current customer. When in doubt, ask for clarification in the PR comments.

Files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
🧠 Learnings (20)
📓 Common learnings
Learnt from: opr
Repo: woocommerce/woocommerce PR: 60360
File: plugins/woocommerce/includes/class-woocommerce.php:1553-1563
Timestamp: 2025-08-15T13:00:31.543Z
Learning: The learning from PR #60360 about not needing to clean up legacy scheduled actions when hook names change is specific to that PR's context: PR #59325 was released in WC 10.1 with an incorrect hook name (action_scheduler_schedule_recurring_actions instead of action_scheduler_ensure_recurring_actions), so no actions were actually queued under the old hook name. This is not a general rule for all hook name changes.
Learnt from: opr
Repo: woocommerce/woocommerce PR: 60360
File: plugins/woocommerce/includes/class-woocommerce.php:1533-1544
Timestamp: 2025-08-14T07:34:00.169Z
Learning: When a WordPress action hook name is changed in a PR (like from action_scheduler_schedule_recurring_actions to action_scheduler_ensure_recurring_actions), any code that was previously hooked to the old hook name would not have been executed, so there's no need to clean up or unschedule any legacy scheduled actions that were supposedly created by the old hook.
Learnt from: senadir
Repo: woocommerce/woocommerce PR: 59325
File: plugins/woocommerce/includes/wc-formatting-functions.php:1211-1226
Timestamp: 2025-07-01T16:24:23.871Z
Learning: Action Scheduler is bundled into WooCommerce and always available, so there's no need to add fallback logic to WordPress cron functions when using Action Scheduler functions like as_schedule_single_action() and as_unschedule_all_actions() in WooCommerce code.
Learnt from: prettyboymp
Repo: woocommerce/woocommerce PR: 60272
File: .github/workflows/release-open-issue-warning.yml:15-23
Timestamp: 2025-08-08T13:52:26.423Z
Learning: Repository: woocommerce/woocommerce
Context: .github/workflows/release-open-issue-warning.yml and similar workflows
Learning: Maintainers (per prettyboymp) are fine requiring third-party modules inside actions/github-script without adding setup-node/createRequire. Prefer to keep this pattern and defer structural changes unless it demonstrably causes issues; minor safeguards like version pinning are acceptable if needed.
📚 Learning: 2025-08-18T06:11:48.768Z
Learnt from: CR
Repo: woocommerce/woocommerce PR: 0
File: .cursor/rules/woo-phpunit.mdc:0-0
Timestamp: 2025-08-18T06:11:48.768Z
Learning: Applies to plugins/woocommerce/tests/**/*.php : Add declare(strict_types=1); at the top of each test file

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-08-18T06:11:48.768Z
Learnt from: CR
Repo: woocommerce/woocommerce PR: 0
File: .cursor/rules/woo-phpunit.mdc:0-0
Timestamp: 2025-08-18T06:11:48.768Z
Learning: Applies to plugins/woocommerce/tests/**/*.php : All PHPUnit test methods must be public, not protected

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-10-24T13:25:49.900Z
Learnt from: hannahtinkler
Repo: woocommerce/woocommerce PR: 61384
File: plugins/woocommerce/tests/php/src/Internal/PushNotifications/Entities/PushTokenTest.php:208-216
Timestamp: 2025-10-24T13:25:49.900Z
Learning: In plugins/woocommerce/tests/**/*.php files, omit setUp() and tearDown() methods when they are not needed, as the project's linting configuration makes it easier to omit these methods than to add them with phpcs:ignore comments.

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-08-18T06:11:48.768Z
Learnt from: CR
Repo: woocommerce/woocommerce PR: 0
File: .cursor/rules/woo-phpunit.mdc:0-0
Timestamp: 2025-08-18T06:11:48.768Z
Learning: Applies to plugins/woocommerce/tests/**/*.php : Test classes should extend WC_Unit_Test_Case

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-08-14T08:20:36.357Z
Learnt from: Aljullu
Repo: woocommerce/woocommerce PR: 60191
File: plugins/woocommerce/client/blocks/tests/e2e/tests/single-product-template/single-product-template-compatibility-layer.spec.ts:4-4
Timestamp: 2025-08-14T08:20:36.357Z
Learning: In WooCommerce e2e tests, some test files intentionally use hard-coded "woocommerce/woocommerce//" template IDs instead of BLOCK_THEME_SLUG to test backwards compatibility with the legacy plugin-scoped template ID format. These should not be updated to use the dynamic slug constant as they serve a specific testing purpose.

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-09-03T11:50:02.208Z
Learnt from: tpaksu
Repo: woocommerce/woocommerce PR: 60735
File: plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/Fulfillments/class-wc-rest-fulfillments-v4-controller-tests.php:10-10
Timestamp: 2025-09-03T11:50:02.208Z
Learning: For REST API controller tests in plugins/woocommerce/tests/**/*.php, test classes should extend WC_REST_Unit_Test_Case instead of WC_Unit_Test_Case because REST routes need to be registered before tests run.

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-09-09T01:49:42.513Z
Learnt from: peterwilsoncc
Repo: woocommerce/woocommerce PR: 0
File: :0-0
Timestamp: 2025-09-09T01:49:42.513Z
Learning: The WooCommerce Blocks repository uses Playwright for e2e tests, not Jest unit tests (which were deprecated). Express Payment Methods accessibility tests should verify DOM structure changes (div vs ul/li) and screen reader compliance using Playwright's DOM querying capabilities.

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-08-11T18:01:34.499Z
Learnt from: nerrad
Repo: woocommerce/woocommerce PR: 60158
File: plugins/woocommerce/tests/php/includes/class-wc-brands-test.php:207-223
Timestamp: 2025-08-11T18:01:34.499Z
Learning: In the WooCommerce test suite, the team prefers test helper methods to fail immediately with errors (like undefined index) rather than adding defensive checks, as this makes test failures more visible and helps identify setup issues quickly. This applies to helper methods like `get_first_brand_term()` in test files.

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-06-25T06:51:41.381Z
Learnt from: samueljseay
Repo: woocommerce/woocommerce PR: 59142
File: plugins/woocommerce/src/Blocks/BlockTypes/MiniCart.php:594-602
Timestamp: 2025-06-25T06:51:41.381Z
Learning: WooCommerce block templates have a predictable structure where each block has one top-level div with wp-block-woocommerce- class containing arbitrary nested content that should be preserved.

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-08-14T08:22:34.886Z
Learnt from: Aljullu
Repo: woocommerce/woocommerce PR: 60191
File: plugins/woocommerce/client/blocks/tests/e2e/tests/product-filters/rating-filter-editor.block_theme.spec.ts:36-36
Timestamp: 2025-08-14T08:22:34.886Z
Learning: In WooCommerce e2e tests, some test files intentionally use hard-coded "woocommerce/woocommerce//" template slugs (not just template IDs) instead of BLOCK_THEME_SLUG to test backwards compatibility with the legacy plugin-scoped template slug format. These should not be updated to use the dynamic slug constant as they serve a specific backwards compatibility testing purpose.

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-06-17T07:07:53.443Z
Learnt from: samueljseay
Repo: woocommerce/woocommerce PR: 58716
File: plugins/woocommerce/client/blocks/assets/js/blocks/mini-cart/iapi-frontend.ts:83-101
Timestamp: 2025-06-17T07:07:53.443Z
Learning: In WooCommerce blocks, when porting existing code patterns that have known issues (like parseInt truncating decimal money values), maintain consistency with existing implementation rather than making isolated fixes. The preference is for systematic refactoring approaches (like broader Dinero adoption) over piecemeal changes.

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-08-22T10:24:05.067Z
Learnt from: yuliyan
Repo: woocommerce/woocommerce PR: 60554
File: plugins/woocommerce/client/blocks/bin/webpack-entries.js:62-62
Timestamp: 2025-08-22T10:24:05.067Z
Learning: WooCommerce blocks use their own block asset API system rather than standard WordPress block.json asset referencing. WC blocks extend AbstractBlock and use methods like get_block_type_editor_script() that leverage $this->asset_api->get_block_asset_build_path() to handle asset registration, instead of defining editorScript, viewScript, or style fields directly in block.json.

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-08-12T14:22:05.865Z
Learnt from: NeosinneR
Repo: woocommerce/woocommerce PR: 60325
File: plugins/woocommerce/tests/php/includes/settings/class-wc-settings-accounts-test.php:0-0
Timestamp: 2025-08-12T14:22:05.865Z
Learning: In WooCommerce settings tests, the woocommerce_enable_delayed_account_creation setting is conditionally present based on wp_is_block_theme() being true OR (wp_is_block_theme() being false AND CartCheckoutUtils::is_checkout_block_default() being true). In test environments with block themes, checking only wp_is_block_theme() is sufficient to determine if the setting should be expected in the settings array.

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-06-23T05:47:52.696Z
Learnt from: samueljseay
Repo: woocommerce/woocommerce PR: 59051
File: plugins/woocommerce/client/blocks/assets/js/blocks/mini-cart/mini-cart-contents/inner-blocks/mini-cart-footer-block/index.tsx:66-70
Timestamp: 2025-06-23T05:47:52.696Z
Learning: For WooCommerce mini-cart blocks in plugins/woocommerce/client/blocks/assets/js/blocks/mini-cart/, the standardized conditional pattern for experimental features should be `if ( isExperimentalMiniCartEnabled() ) { blockSettings.save = () => <InnerBlocks.Content />; }` - defaulting to the traditional Save component and only overriding when the experimental feature is enabled.

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-06-16T09:20:22.981Z
Learnt from: gigitux
Repo: woocommerce/woocommerce PR: 58846
File: plugins/woocommerce/client/blocks/tests/e2e/tests/all-products/all-products.block_theme.spec.ts:41-52
Timestamp: 2025-06-16T09:20:22.981Z
Learning: In WooCommerce E2E tests, the database is reset to the initial state for each test, so there's no need to manually restore global template changes (like clearing the header template) as the test infrastructure handles cleanup automatically.

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-08-18T06:11:48.768Z
Learnt from: CR
Repo: woocommerce/woocommerce PR: 0
File: .cursor/rules/woo-phpunit.mdc:0-0
Timestamp: 2025-08-18T06:11:48.768Z
Learning: Ensure all dependencies are initialized in setUp() before tests run

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-08-09T17:57:10.170Z
Learnt from: nerrad
Repo: woocommerce/woocommerce PR: 60158
File: plugins/woocommerce/tests/php/includes/class-wc-brands-test.php:15-23
Timestamp: 2025-08-09T17:57:10.170Z
Learning: WP_UnitTestCase_Base::tearDown() automatically resets the current screen context via set_current_screen(null), so tests extending WP_UnitTestCase (or WC_Unit_Test_Case which extends from it) don't need to manually reset the admin screen context if they call parent::tearDown().

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-06-25T15:39:25.166Z
Learnt from: vladolaru
Repo: woocommerce/woocommerce PR: 59160
File: plugins/woocommerce/tests/php/src/Internal/Admin/Settings/PaymentsProviders/WooPayments/WooPaymentsServiceTest.php:360-373
Timestamp: 2025-06-25T15:39:25.166Z
Learning: Test mocks should accurately simulate the behavior of the functions they replace, including return value semantics. For WordPress functions like `update_option`, the mock should properly return `false` when the value hasn't changed and `true` when it has, to avoid masking logic errors in tests.

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-07-01T16:24:23.871Z
Learnt from: senadir
Repo: woocommerce/woocommerce PR: 59325
File: plugins/woocommerce/includes/wc-formatting-functions.php:1211-1226
Timestamp: 2025-07-01T16:24:23.871Z
Learning: Action Scheduler is bundled into WooCommerce and always available, so there's no need to add fallback logic to WordPress cron functions when using Action Scheduler functions like as_schedule_single_action() and as_unschedule_all_actions() in WooCommerce code.

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
🧬 Code graph analysis (1)
plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php (1)
plugins/woocommerce/src/Blocks/Patterns/PTKPatternsStore.php (3)
  • PTKPatternsStore (13-295)
  • flush_cached_patterns (187-192)
  • ensure_recurring_fetch_patterns_if_enabled (96-102)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (32)
  • GitHub Check: Blocks e2e tests - Legacy MiniCart 1/3 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: PHP: 8.4 WP: latest (HPOS:off) [WP latest] 1/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: Blocks e2e tests 4/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 2/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 8/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core API tests - @woocommerce/plugin-woocommerce [api]
  • GitHub Check: Core e2e tests - Legacy MiniCart - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 5/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 10/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests 6/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 7/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 9/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 6/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 1/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: PHP: 8.4 WP: latest (HPOS:off) [WP latest] 2/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: Blocks e2e tests 3/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Metrics - @woocommerce/plugin-woocommerce [performance]
  • GitHub Check: Core e2e tests 5/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: PHP: 8.4 WP: latest [WP latest] 2/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: Core e2e tests 3/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests 4/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests 2/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: PHP: 7.4 WP: pre-release [WP 6.9-beta2] 2/2 - @woocommerce/plugin-woocommerce [unit:php] (optional)
  • GitHub Check: PHP: 7.4 WP: latest - 1 [WP 6.7.4] 1/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: Core e2e tests 1/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: PHP: 8.4 WP: latest - Legacy MiniCart [WP latest] 1/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: PHP: 7.4 WP: latest - 1 [WP 6.7.4] 2/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: PHP: 8.4 WP: latest - Legacy MiniCart [WP latest] 2/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: PHP: 7.4 WP: pre-release [WP 6.9-beta2] 1/2 - @woocommerce/plugin-woocommerce [unit:php] (optional)
  • GitHub Check: PHP: 8.4 WP: latest [WP latest] 1/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: Lint - @woocommerce/plugin-woocommerce
  • GitHub Check: build

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php (1)

13-13: Consider extending WC_Unit_Test_Case for WooCommerce-specific tests.

The test class extends \WP_UnitTestCase, but since it tests WooCommerce functionality and uses Action Scheduler (bundled with WooCommerce), extending WC_Unit_Test_Case would provide better initialization and cleanup for WooCommerce-specific test scenarios. This is a pre-existing pattern in this file, but worth considering for future refactoring.

Based on learnings.

📜 Review details

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9139915 and 09ae8d4.

📒 Files selected for processing (1)
  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php (5 hunks)
🧰 Additional context used
📓 Path-based instructions (4)
**/*.{php,js,jsx,ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/code-quality.mdc)

**/*.{php,js,jsx,ts,tsx}: Guard against unexpected inputs
Sanitize and validate any potentially dangerous inputs
Ensure code is backwards compatible
Write code that is readable and intuitive
Ensure code has unit or E2E tests where applicable

Files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
plugins/woocommerce/tests/**/*.php

📄 CodeRabbit inference engine (.cursor/rules/woo-phpunit.mdc)

plugins/woocommerce/tests/**/*.php: Ensure test classes define public setUp() and tearDown() methods (not protected)
All PHPUnit test methods must be public, not protected
Add declare(strict_types=1); at the top of each test file
Test classes should extend WC_Unit_Test_Case

Files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
**/*.{php,js,jsx,tsx,ts}

📄 CodeRabbit inference engine (.cursor/rules/avoid-regex.mdc)

**/*.{php,js,jsx,tsx,ts}: Avoid using regular expressions unless absolutely necessary to favor readability and maintainability
Only consider regex when no built-in language alternative (string/array APIs) fits the need
Only use regex when performance is critical and faster than alternatives, with measurements to justify
Use regex for complex pattern matching only if the pattern is well-documented and thoroughly tested
Allow regex when maintaining legacy code where an existing, correct pattern is being modified
If regex is necessary, document the pattern extensively to explain what it matches
If regex is necessary, add comprehensive tests covering edge cases and potential security issues
Use named capture groups in regex to improve readability when supported
Validate input before applying regex to ensure it is safe
Assess and mitigate security risks when using regex, including ReDoS and injection vulnerabilities
Avoid regex patterns that can cause catastrophic backtracking (ReDoS)
Do not construct regex from untrusted input to prevent injection attacks
Ensure regex patterns do not overmatch and unintentionally capture unexpected inputs

Files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
**/*.{php,js,ts,jsx,tsx}

⚙️ CodeRabbit configuration file

**/*.{php,js,ts,jsx,tsx}: Don't trust that extension developers will follow the best practices, make sure the code:

  • Guards against unexpected inputs.
  • Sanitizes and validates any potentially dangerous inputs.
  • Is backwards compatible.
  • Is readable and intuitive.
  • Has unit or E2E tests where applicable.
    When making any changes to code that deletes or modifies orders/products/customer data, make sure that there are
    sufficient checks in place to prevent accidental data loss. As an example, if deleting a draft order, check that
    the order status is indeed draft or checkout-draft. Also think about whether race conditions could occur and
    delete orders that don't belong to the current customer. When in doubt, ask for clarification in the PR comments.

Files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
🧠 Learnings (20)
📓 Common learnings
Learnt from: opr
Repo: woocommerce/woocommerce PR: 60360
File: plugins/woocommerce/includes/class-woocommerce.php:1553-1563
Timestamp: 2025-08-15T13:00:31.543Z
Learning: The learning from PR #60360 about not needing to clean up legacy scheduled actions when hook names change is specific to that PR's context: PR #59325 was released in WC 10.1 with an incorrect hook name (action_scheduler_schedule_recurring_actions instead of action_scheduler_ensure_recurring_actions), so no actions were actually queued under the old hook name. This is not a general rule for all hook name changes.
Learnt from: opr
Repo: woocommerce/woocommerce PR: 60360
File: plugins/woocommerce/includes/class-woocommerce.php:1533-1544
Timestamp: 2025-08-14T07:34:00.169Z
Learning: When a WordPress action hook name is changed in a PR (like from action_scheduler_schedule_recurring_actions to action_scheduler_ensure_recurring_actions), any code that was previously hooked to the old hook name would not have been executed, so there's no need to clean up or unschedule any legacy scheduled actions that were supposedly created by the old hook.
Learnt from: senadir
Repo: woocommerce/woocommerce PR: 59325
File: plugins/woocommerce/includes/wc-formatting-functions.php:1211-1226
Timestamp: 2025-07-01T16:24:23.871Z
Learning: Action Scheduler is bundled into WooCommerce and always available, so there's no need to add fallback logic to WordPress cron functions when using Action Scheduler functions like as_schedule_single_action() and as_unschedule_all_actions() in WooCommerce code.
📚 Learning: 2025-08-18T06:11:48.768Z
Learnt from: CR
Repo: woocommerce/woocommerce PR: 0
File: .cursor/rules/woo-phpunit.mdc:0-0
Timestamp: 2025-08-18T06:11:48.768Z
Learning: Applies to plugins/woocommerce/tests/**/*.php : Add declare(strict_types=1); at the top of each test file

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-08-18T06:11:48.768Z
Learnt from: CR
Repo: woocommerce/woocommerce PR: 0
File: .cursor/rules/woo-phpunit.mdc:0-0
Timestamp: 2025-08-18T06:11:48.768Z
Learning: Applies to plugins/woocommerce/tests/**/*.php : All PHPUnit test methods must be public, not protected

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-10-24T13:25:49.900Z
Learnt from: hannahtinkler
Repo: woocommerce/woocommerce PR: 61384
File: plugins/woocommerce/tests/php/src/Internal/PushNotifications/Entities/PushTokenTest.php:208-216
Timestamp: 2025-10-24T13:25:49.900Z
Learning: In plugins/woocommerce/tests/**/*.php files, omit setUp() and tearDown() methods when they are not needed, as the project's linting configuration makes it easier to omit these methods than to add them with phpcs:ignore comments.

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-08-18T06:11:48.768Z
Learnt from: CR
Repo: woocommerce/woocommerce PR: 0
File: .cursor/rules/woo-phpunit.mdc:0-0
Timestamp: 2025-08-18T06:11:48.768Z
Learning: Applies to plugins/woocommerce/tests/**/*.php : Test classes should extend WC_Unit_Test_Case

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-08-14T08:20:36.357Z
Learnt from: Aljullu
Repo: woocommerce/woocommerce PR: 60191
File: plugins/woocommerce/client/blocks/tests/e2e/tests/single-product-template/single-product-template-compatibility-layer.spec.ts:4-4
Timestamp: 2025-08-14T08:20:36.357Z
Learning: In WooCommerce e2e tests, some test files intentionally use hard-coded "woocommerce/woocommerce//" template IDs instead of BLOCK_THEME_SLUG to test backwards compatibility with the legacy plugin-scoped template ID format. These should not be updated to use the dynamic slug constant as they serve a specific testing purpose.

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-09-03T11:50:02.208Z
Learnt from: tpaksu
Repo: woocommerce/woocommerce PR: 60735
File: plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/Fulfillments/class-wc-rest-fulfillments-v4-controller-tests.php:10-10
Timestamp: 2025-09-03T11:50:02.208Z
Learning: For REST API controller tests in plugins/woocommerce/tests/**/*.php, test classes should extend WC_REST_Unit_Test_Case instead of WC_Unit_Test_Case because REST routes need to be registered before tests run.

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-09-09T01:49:42.513Z
Learnt from: peterwilsoncc
Repo: woocommerce/woocommerce PR: 0
File: :0-0
Timestamp: 2025-09-09T01:49:42.513Z
Learning: The WooCommerce Blocks repository uses Playwright for e2e tests, not Jest unit tests (which were deprecated). Express Payment Methods accessibility tests should verify DOM structure changes (div vs ul/li) and screen reader compliance using Playwright's DOM querying capabilities.

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-08-12T14:22:05.865Z
Learnt from: NeosinneR
Repo: woocommerce/woocommerce PR: 60325
File: plugins/woocommerce/tests/php/includes/settings/class-wc-settings-accounts-test.php:0-0
Timestamp: 2025-08-12T14:22:05.865Z
Learning: In WooCommerce settings tests, the woocommerce_enable_delayed_account_creation setting is conditionally present based on wp_is_block_theme() being true OR (wp_is_block_theme() being false AND CartCheckoutUtils::is_checkout_block_default() being true). In test environments with block themes, checking only wp_is_block_theme() is sufficient to determine if the setting should be expected in the settings array.

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-08-14T08:22:34.886Z
Learnt from: Aljullu
Repo: woocommerce/woocommerce PR: 60191
File: plugins/woocommerce/client/blocks/tests/e2e/tests/product-filters/rating-filter-editor.block_theme.spec.ts:36-36
Timestamp: 2025-08-14T08:22:34.886Z
Learning: In WooCommerce e2e tests, some test files intentionally use hard-coded "woocommerce/woocommerce//" template slugs (not just template IDs) instead of BLOCK_THEME_SLUG to test backwards compatibility with the legacy plugin-scoped template slug format. These should not be updated to use the dynamic slug constant as they serve a specific backwards compatibility testing purpose.

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-08-11T18:01:34.499Z
Learnt from: nerrad
Repo: woocommerce/woocommerce PR: 60158
File: plugins/woocommerce/tests/php/includes/class-wc-brands-test.php:207-223
Timestamp: 2025-08-11T18:01:34.499Z
Learning: In the WooCommerce test suite, the team prefers test helper methods to fail immediately with errors (like undefined index) rather than adding defensive checks, as this makes test failures more visible and helps identify setup issues quickly. This applies to helper methods like `get_first_brand_term()` in test files.

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-06-25T06:51:41.381Z
Learnt from: samueljseay
Repo: woocommerce/woocommerce PR: 59142
File: plugins/woocommerce/src/Blocks/BlockTypes/MiniCart.php:594-602
Timestamp: 2025-06-25T06:51:41.381Z
Learning: WooCommerce block templates have a predictable structure where each block has one top-level div with wp-block-woocommerce- class containing arbitrary nested content that should be preserved.

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-06-17T07:07:53.443Z
Learnt from: samueljseay
Repo: woocommerce/woocommerce PR: 58716
File: plugins/woocommerce/client/blocks/assets/js/blocks/mini-cart/iapi-frontend.ts:83-101
Timestamp: 2025-06-17T07:07:53.443Z
Learning: In WooCommerce blocks, when porting existing code patterns that have known issues (like parseInt truncating decimal money values), maintain consistency with existing implementation rather than making isolated fixes. The preference is for systematic refactoring approaches (like broader Dinero adoption) over piecemeal changes.

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-08-22T10:24:05.067Z
Learnt from: yuliyan
Repo: woocommerce/woocommerce PR: 60554
File: plugins/woocommerce/client/blocks/bin/webpack-entries.js:62-62
Timestamp: 2025-08-22T10:24:05.067Z
Learning: WooCommerce blocks use their own block asset API system rather than standard WordPress block.json asset referencing. WC blocks extend AbstractBlock and use methods like get_block_type_editor_script() that leverage $this->asset_api->get_block_asset_build_path() to handle asset registration, instead of defining editorScript, viewScript, or style fields directly in block.json.

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-06-23T05:47:52.696Z
Learnt from: samueljseay
Repo: woocommerce/woocommerce PR: 59051
File: plugins/woocommerce/client/blocks/assets/js/blocks/mini-cart/mini-cart-contents/inner-blocks/mini-cart-footer-block/index.tsx:66-70
Timestamp: 2025-06-23T05:47:52.696Z
Learning: For WooCommerce mini-cart blocks in plugins/woocommerce/client/blocks/assets/js/blocks/mini-cart/, the standardized conditional pattern for experimental features should be `if ( isExperimentalMiniCartEnabled() ) { blockSettings.save = () => <InnerBlocks.Content />; }` - defaulting to the traditional Save component and only overriding when the experimental feature is enabled.

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-06-16T09:20:22.981Z
Learnt from: gigitux
Repo: woocommerce/woocommerce PR: 58846
File: plugins/woocommerce/client/blocks/tests/e2e/tests/all-products/all-products.block_theme.spec.ts:41-52
Timestamp: 2025-06-16T09:20:22.981Z
Learning: In WooCommerce E2E tests, the database is reset to the initial state for each test, so there's no need to manually restore global template changes (like clearing the header template) as the test infrastructure handles cleanup automatically.

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-08-18T06:11:48.768Z
Learnt from: CR
Repo: woocommerce/woocommerce PR: 0
File: .cursor/rules/woo-phpunit.mdc:0-0
Timestamp: 2025-08-18T06:11:48.768Z
Learning: Ensure all dependencies are initialized in setUp() before tests run

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-08-09T17:57:10.170Z
Learnt from: nerrad
Repo: woocommerce/woocommerce PR: 60158
File: plugins/woocommerce/tests/php/includes/class-wc-brands-test.php:15-23
Timestamp: 2025-08-09T17:57:10.170Z
Learning: WP_UnitTestCase_Base::tearDown() automatically resets the current screen context via set_current_screen(null), so tests extending WP_UnitTestCase (or WC_Unit_Test_Case which extends from it) don't need to manually reset the admin screen context if they call parent::tearDown().

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-06-25T15:39:25.166Z
Learnt from: vladolaru
Repo: woocommerce/woocommerce PR: 59160
File: plugins/woocommerce/tests/php/src/Internal/Admin/Settings/PaymentsProviders/WooPayments/WooPaymentsServiceTest.php:360-373
Timestamp: 2025-06-25T15:39:25.166Z
Learning: Test mocks should accurately simulate the behavior of the functions they replace, including return value semantics. For WordPress functions like `update_option`, the mock should properly return `false` when the value hasn't changed and `true` when it has, to avoid masking logic errors in tests.

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
📚 Learning: 2025-07-01T16:24:23.871Z
Learnt from: senadir
Repo: woocommerce/woocommerce PR: 59325
File: plugins/woocommerce/includes/wc-formatting-functions.php:1211-1226
Timestamp: 2025-07-01T16:24:23.871Z
Learning: Action Scheduler is bundled into WooCommerce and always available, so there's no need to add fallback logic to WordPress cron functions when using Action Scheduler functions like as_schedule_single_action() and as_unschedule_all_actions() in WooCommerce code.

Applied to files:

  • plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php
🧬 Code graph analysis (1)
plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php (1)
plugins/woocommerce/src/Blocks/Patterns/PTKPatternsStore.php (3)
  • PTKPatternsStore (13-295)
  • flush_cached_patterns (187-192)
  • ensure_recurring_fetch_patterns_if_enabled (96-102)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (32)
  • GitHub Check: Blocks e2e tests 1/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 10/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests - Legacy MiniCart 1/3 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 7/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 2/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 3/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 9/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 5/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core API tests - @woocommerce/plugin-woocommerce [api]
  • GitHub Check: PHP: 7.4 WP: pre-release [WP 6.9-beta2] 1/2 - @woocommerce/plugin-woocommerce [unit:php] (optional)
  • GitHub Check: Blocks e2e tests 6/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: PHP: 7.4 WP: latest - 1 [WP 6.7.4] 2/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: PHP: 8.4 WP: latest - Legacy MiniCart [WP latest] 2/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: PHP: 8.4 WP: latest (HPOS:off) [WP latest] 1/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: Blocks e2e tests 8/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests 4/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Blocks e2e tests 4/10 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests - Legacy MiniCart - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Metrics - @woocommerce/plugin-woocommerce [performance]
  • GitHub Check: PHP: 7.4 WP: pre-release [WP 6.9-beta2] 2/2 - @woocommerce/plugin-woocommerce [unit:php] (optional)
  • GitHub Check: PHP: 8.4 WP: latest (HPOS:off) [WP latest] 2/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: Core e2e tests 6/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests 3/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: PHP: 8.4 WP: latest [WP latest] 2/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: Core e2e tests 5/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: PHP: 8.4 WP: latest - Legacy MiniCart [WP latest] 1/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: Core e2e tests 2/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests 1/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: PHP: 7.4 WP: latest - 1 [WP 6.7.4] 1/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: PHP: 8.4 WP: latest [WP latest] 1/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: Lint - @woocommerce/plugin-woocommerce
  • GitHub Check: build
🔇 Additional comments (2)
plugins/woocommerce/tests/php/src/Blocks/Patterns/PTKPatternsStoreTest.php (2)

33-57: Excellent test setup and cleanup.

The public visibility for setUp() and tearDown() is correct, and the cleanup logic (unscheduling actions, deleting options) ensures proper test isolation. This prevents test pollution and follows PHPUnit best practices.


135-143: LGTM! Comprehensive test coverage for the new recurring action functionality.

The new tests thoroughly cover the ensure_recurring_fetch_patterns_if_enabled() functionality:

  • Verifies actions are scheduled when tracking is enabled
  • Verifies actions are NOT scheduled when tracking is disabled
  • Confirms flush_cached_patterns() properly unschedules both single and recurring actions

The updated assertions now correctly include the 'woocommerce' group parameter, matching how actions are scheduled in the implementation. All tests follow best practices with clear names, descriptive assertion messages, and proper arrange-act-assert structure.

Also applies to: 200-200, 436-473

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

Labels

plugin: woocommerce Issues related to the WooCommerce Core plugin.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants