When a product is saved its validate_props method is invoked,
and this recalculates the stock_status property based on whether
the product manages stock or not, the stock quantity, and the
value of the woocommerce_notify_no_stock_amount option.
In the case of variable products, and when stock is managed, the stock
was set to "instock" when the current stock was enough, but only
if the "stock_quantity" property was in the list of changed properties
for the object (the method in the base product class doen't check
for changed properties). This is a problem because the
wc_update_product_stock function updates stock_quantity but via direct
database modification, and thus stock_quantity isn't considered
modified. Therefore stock modifications via wc_update_product_stock
don't update stock_status on the product (e.g. when going from 0 to 1
after a refund the stock status will remain as "outofstock").
The fix consists of removing the check for changed properties since
it's not done anyway in the other cases (when stock is below the
woocommerce_notify_no_stock_amount threshold) nor in the base class.
Also, validate_props is refactored for readabiliyy, and an useless
set_stock_status() call placed right before save()
in wc_update_product_stock is removed.
One of the problems with synchronous webhooks is that they are executed as soon as the related action is. Since we may call an action multiple times in the process of updating something, this causes only the first action to trigger the hook. This differs from asynchronous execution because in that case, the web hook will be executed after the entire request has completed.
We've hid the use of Axios behind a service so that we're able to easily mock it out in factories as well as handle the creation and configuration of the client. This will make it easier on consumers in that they won't have to worry about things like authentication when using the API.
The mapping of the "Automattic\WooCommerce\Testing\Tools\" namespace
to the "tests/tools" directory is moved from manual registration
inside the tests bootstrap constructor to a declaration inside the
autload-dev section in composer.json.
The code hacker as originally designed, as a mechanism that allowed
to enable hacks at the individual test level, is flawed because it
assumes that code files are loaded before each test, but actually
the PHP engine loads code files only once.
Therefore this commit redesigns it so that the two existing main hacks,
the functions mocker and the static methods hacker, are applied
to all the relevant functions and classes at bootstrap time, and
mocks for each individual function/method can be registered at the
beginning of each test. See README for the full details.
Right now, when a product having a parent (e.g. a variation having a
parent variable product) is saved, wc_deferred_product_sync is
executed so that product sync is performed at the end of the request.
This commit implements the same when the product is deleted.
The testing tools (only the code hacker at this time) have been moved
from 'src' to 'tests/Tools', since many opcode cache plugins
load the whole src folder in production.
Also, an extra autoloader is set in the tests bootstrap so that
the 'tests/Tools' directory corresponds, using PSR4, to the
'Automattic\WooCommerce\Testing\Tools' namespace.
- Add methods to temporarily disable and reenable the code hacker.
The code hacker is causing issues in some tests that perform
write operations to the local filesystem. Since this happens only
in a few cases, the easiest fix is to temporarily disable the
code hacker when that happens. This commit adds two new methods
for that in `WC_Unit_Test_Case`: `disable_code_hacker` and
`reenable_code_hacker`.
These methods use a disabling requests count so that the hacker
isn't enabled before it should. E.g. you call `disable`, then
a helper method that does `disable` and `enable`, then `enable` -
then only the last `enable` will have effect.
- `CodeHacker::add_hack` has now a boolean `persistent` parameter.
Persistent hacks won't be cleared by `clear_hacks`.
- `CodeHackerTestHook::executeAfterTest` will now disable the hacker
only if no persistent hacks are registered.
- The existing `file_copy` method is made static for consistency.
- `CodeHacker::restore` method renamed to `disable` for clarity.
The unit testing bootstrap loads and initializes WooCommerce, this
loads a bunch of code files that can't then be hacked in the test hooks.
A workaround is provided in this commit for the case of hacking
static methods. A new StaticWrapper class is created that allows
defining mock methods after the code file has been loaded.
This is applied to all classes from a fixed list in the bootstrap,
before WooCommerce is initialized. The list should be kept up to date
with the list of classes that require such workaround.
- Fix how CodeHackerTestHook::executeBeforeTest parses the test name,
to account for warnings and tests with data sets.
- CodeHackerTestHook now includes a executeAfterTest hook that
disables the code hacker (needed to prevent it from inadvertently
altering further tests). Also, clear_hacks is executed in
executeBeforeTest for the same reason.
- CodeHacker gets restore, clear_hacks and is_enabled methods
to support the changes in CodeHackerTestHook.
- FunctionsMockerHack fixed so that it doesn't modify strings
that are class method definitions.
- Added the WC_Unit_Test_Case::file_copy method, it must be used
instead of the PHP built-in "copy" in tests, otherwise tests
that run with the code hacker active will fail.
This is something to investigate.
Now @hack class and method annotations can be used to register
code hacks as an alternative to using before_ methods.
The syntax is /* @hack HackClassName param1 param2 */
where parameters will be passed to the class constructor.
If the class name ends with "Hack", then that suffix can be
omitted (e.g. "Foo" can be specified instead of "FooHack").
The "code hacker" is a class that hooks on filesystem events
(using stream_wrapper_unregister) in order to allow for dynamically
modifying the content of PHP code files while they are loaded.
The code hacker class allows registering hacks, which are
functions that take source code as input and return the modified code.
A hack can be a standalone function or a class with a "hack" method.
A few hacks are provided off the shelf. One allows mocking standalone
PHP functions (WP, WOO or not), another one allows mocking static
methods, and there's the one that removes the "final" qualifier
from a class definition. This helps unit testing stuff that would
otherwise be quite hard to test.
Since we were converting the field to lowercase we ended up inserting meta in all lowercase, regardless of what it was in the CSV file. We should only be using the normalized field name when looking at the default columns, and should instead rely on a case-insensitive regex for the special columns.
One thing to note is that we're still defaulting the $headers array to the normalized field, as we don't want to change what is being passed to the filter for unmapped columns.
Since those Notes were created because of WC Admin and the display is handled by WC Admin, it does not make sense to test them without WC Admin.
In addition, the data store that handles these Notices is not loaded without WC Admin.
All titles and questions in the new onboarding wizards only capitalise names and the first letter of the sentence. This seemed a tiny bit off. (Literally tiny.)
Changed "Build a Better WooCommerce" to "Build a better WooCommerce"
* Merge wc api authorization headers with given headers
* Add put method to WC_Helper_API
* Add unit test coverage around WC_Helper_API request methods
* Add tests for WC_Helper_API url method
Since the tokens are replaced in a first-discovered first-replaced order, we may accidentally create tokenized paths like '{{ABSPATH}}/test' instead of the desired '{{WP_CONTENT_DIR}}test'. By ordering them according to specificity however, we ensure that we tokenize as much of the path as possible.
Initially it was made more precise to avoid calculating estimates for incomplete addresses. Due to some bad working in template files though, as well as historical precedent, we need to change it back.
Instead of comparing all tables that exist with all tables that are registered with `WC_Install::get_tables()` (which was only introduced a few versions ago in #19436), rewrite the test to verify that all of the tables *registered* actually exist within the database.
This will prevent tests from failing when, for example, they're run against databases that may or may not have additional plugins installed/activated. This also prevents tests from failing when running WooCommerce 4.x (e.g. with WooCommerce Admin) on WordPress < 5.3, since the tables are created but the callback to register the custom tables is never hooked.
The WooCommerce unit tests started failing when running using WP nightly
with the following error:
```
Fatal error: require_once(): Failed opening required '/tmp/wordpress-tests-lib/src//wp-includes/class-phpmailer.php' (include_path='.:/home/travis/.phpenv/versions/7.4.2/share/pear') in /tmp/wordpress-tests-lib/includes/mock-mailer.php on line 2
```
https://travis-ci.org/woocommerce/woocommerce/jobs/646855363#L1955
This was happening because WP changed the syntax of the line where
ABSPATH is defined in the context of the unit tests
(https://core.trac.wordpress.org/changeset/47198/trunk/wp-tests-config-sample.php)
and we rely on this syntax to change ABSPATH when running
`tests/bin/install.sh`.
To fix this problem, this commit updates the sed command used to change
ABSPATH to work when both the old and new syntaxes are used. In the
future we might want to consider a more robust solution to the problem
of updating ABSPATH.
For some reason unknown to me, the following commit added logic to make
sure that negative fees never make the order total negative: 804feb9333
Probably by accident, commit 4326e39250 changed this logic when `$fees_total += $item->get_total()` was replaced by `$fees_total += $fee_total`:
4326e39250 (diff-9b4164165828b26c4b7aec01c7b17884R1594)
This commit adds a failing test that ensures that the old behavior is
tested. This test needs to be fixed before we merge the changes proposed
in PR #25504.
Note: The tests fail in this commit because of the defines. Ran individually there are no problems, but I'm going to make another PR to merge in a package to make testing constants possible, and then come back to fix this after it gets merged.
This commit fixes the unit test
WC_Tests_API_Functions::test_wc_rest_upload_image_from_url_should_return_error_when_invalid_image_is_passed() which was failing due to a change in a WordPress core message with the following error:
```
2) WC_Tests_API_Functions::test_wc_rest_upload_image_from_url_should_return_error_when_invalid_image_is_passed
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'Invalid image: File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini.'
+'Invalid image: File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini file or by post_max_size being defined as smaller than upload_max_filesize in php.ini.'
/home/travis/build/woocommerce/woocommerce/tests/unit-tests/util/api-functions.php:89
```
(https://travis-ci.org/woocommerce/woocommerce/jobs/637353161#L402)
This test only fails when using WordPress nightly build (the yet to be
released WP 5.4).
To fix this problem, this commit adds an if statement to check for different error
messages depending on the WordPress version that is being used to run
the tests. In the future, we might want to refactor this test to remove
its dependency to a WordPress error message to make it more robust and
stable.