mirror of
https://github.com/woocommerce/woocommerce.git
synced 2025-08-18 10:21:16 +08:00
* Fix incorrect count for brands through the /wp-json/wc/v3/products/brands endpoint - Changed product_brand taxonomy to use _wc_term_recount instead of _update_post_term_count - Added product_brand to the valid taxonomies in wc_change_term_counts filter - This ensures brand counts properly account for WooCommerce product visibility logic * Fix brand count test to respect product visibility settings - Use go_to() to simulate frontend context instead of admin context - Use get_terms() instead of get_term() to trigger wc_change_term_counts filter - Test now properly validates that brand counts respect 'hide out of stock items' setting * Add comprehensive brand count tests for admin and frontend contexts - Add test_brand_count_respects_product_visibility() that uses go_to() and get_terms() to simulate frontend behavior - Add test_brand_count_ignores_product_visibility_in_admin_context() that uses get_term() to simulate admin behavior - Tests demonstrate different behaviors: frontend respects out-of-stock settings, admin ignores them - Both tests pass and provide complete coverage of the context-dependent behavior * Add REST API brands controller test file - Add comprehensive test suite for WC_REST_Product_Brands_Controller - Tests cover brand listing, count accuracy, product visibility respect - Includes test for brand counts respecting out-of-stock product settings - All 5 tests pass with 29 assertions - Provides end-to-end testing of brands API functionality * Remove admin vs frontend context documentation from woo-phpunit rule The context-dependent behavior documentation was removed as it was found to be inaccurate: - is_admin() returns false in test environment, not true - The real issue is function choice (get_terms vs get_term), not context - Simplified rule to focus on core testing practices * Remove duplicate product_brand filter from WC_Brands class * Add changefile(s) from automation for the following project(s): woocommerce * Delete plugins/woocommerce/changelog/fix-wooplug-5231-incorrect-count-for-brands-through-the-wp Remove superfluous changelog added by AI. * update brands rest controller test to remove unnecessary setup * Update doc block for method to include Brands * Add test for deleting product a brand is attached to. * Simplify the tests with helper for getting first brand term. * Add test for visibility affected count on specific brand REST request. * Move tests to category controller. The brands controller extends the category controller so this ensures tests live in the appropriate place. As a part of this refactor I also improved some of the test architecture and removed superfluous term_cache clears (WP scaffolding already clears caches on tearDown). --------- Co-authored-by: github-actions <github-actions@github.com>
86 lines
2.1 KiB
Text
86 lines
2.1 KiB
Text
---
|
|
description:
|
|
globs: plugins/woocommerce/tests/**/*.php
|
|
alwaysApply: false
|
|
---
|
|
|
|
# WooCommerce PHPUnit Tests
|
|
|
|
Rules for running WooCommerce PHPUnit tests.
|
|
|
|
## Run PHPUnit Tests
|
|
|
|
Run WooCommerce PHPUnit tests for specific files or directories.
|
|
|
|
The command to run is
|
|
|
|
```
|
|
pnpm run test:php:env {relative_path} --verbose
|
|
```
|
|
|
|
And the command must be run in the `plugins/woocommerce` directory.
|
|
|
|
## Test Execution Best Practices
|
|
|
|
### Command Structure
|
|
|
|
- **Basic test file**: `pnpm run test:php:env tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-product-brands-controller-test.php --verbose`
|
|
- **Filter by test method**: `pnpm run test:php:env -- --filter="test_get_brands_with_correct_count"`
|
|
- **Filter by test class**: `pnpm run test:php:env -- --filter="WC_REST_Product_Brands_Controller_Test"`
|
|
|
|
### Common Issues & Solutions
|
|
|
|
#### 1. Class Not Found Errors
|
|
|
|
If you get "Class could not be found" errors:
|
|
|
|
- Use the `--filter` approach instead of direct file paths
|
|
- Ensure test classes have `public` setUp() and tearDown() methods (not `protected`)
|
|
- Add `declare( strict_types = 1 );` at the top of test files
|
|
|
|
#### 2. Test Method Visibility
|
|
|
|
All test methods must be `public`, not `protected`:
|
|
|
|
```php
|
|
public function setUp(): void {
|
|
parent::setUp();
|
|
// test setup
|
|
}
|
|
|
|
public function tearDown(): void {
|
|
parent::tearDown();
|
|
// test cleanup
|
|
}
|
|
```
|
|
|
|
#### 3. Test File Structure
|
|
|
|
Ensure proper test file structure (the below is an example):
|
|
|
|
```php
|
|
<?php
|
|
declare( strict_types = 1 );
|
|
|
|
/**
|
|
* Test class description (this is an exmample)
|
|
*/
|
|
class WC_REST_Product_Brands_Controller_Test extends WC_Unit_Test_Case {
|
|
// test methods
|
|
}
|
|
```
|
|
|
|
### Debugging Tips
|
|
|
|
- Use `--verbose` flag for detailed output
|
|
- Run individual test methods to isolate issues
|
|
- Check for syntax errors in test files
|
|
- Ensure all dependencies are properly initialized in setUp()
|
|
|
|
### Environment Notes
|
|
|
|
- Tests run in a Docker container via wp-env
|
|
- Node version warnings can be ignored (tests still work)
|
|
- Memory usage is typically 150-200MB for test suites
|
|
|
|
And the command must be run in the `plugins/woocommerce` directory.
|