Commit Graph

58 Commits

Author SHA1 Message Date
Boone Gorges c4c80d3dd4 Tests: Reset post-related globals after each test.
Globals like `$pages` were leaking between tests, resulting in various
bits of weirdness.

Globals will kill WordPress. Globals are killing WordPress.

See #38196.

git-svn-id: https://develop.svn.wordpress.org/trunk@38678 602fd350-edb4-49c9-b593-d223f7449a82
2016-09-30 03:15:36 +00:00
Gary Pendergast 61abf68e6d Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.

Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.

And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.

Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.



git-svn-id: https://develop.svn.wordpress.org/trunk@38571 602fd350-edb4-49c9-b593-d223f7449a82
2016-09-08 03:54:13 +00:00
John Blackbourn 417de45c08 Build/Test Tools: Add support for `is_front_page()` to `assertQueryTrue()` and assert that all the tested conditions are supported by the method.
Fixes #37849


git-svn-id: https://develop.svn.wordpress.org/trunk@38405 602fd350-edb4-49c9-b593-d223f7449a82
2016-08-27 15:27:35 +00:00
Scott Taylor 02319efa71 Unit Tests:
* Automatically delete objects that we were created during `wpSetUpBeforeClass` - posts, comments, terms (except 1), and user (except 1)
* The amount of leftover data between tests was breathtaking - use the new function: `_delete_all_data()`
* Commit database transactions for all `TestCase`s, not just those that implement `wpSetUpBeforeClass` and `wpTearDownAfterClass`
* The tests run 10-20 seconds faster now

See #37699.


git-svn-id: https://develop.svn.wordpress.org/trunk@38398 602fd350-edb4-49c9-b593-d223f7449a82
2016-08-27 08:35:16 +00:00
Boone Gorges c97f234e6f Allow action and filter hooks to be deprecated.
When a filter or action hook is deprecated, the corresponding `apply_filters()`
or `do_action()` calls should be switched out with `apply_filters_deprecated()`
or `do_action_deprecated()`. The latter functions will throw a deprecation
before invoking the original hook.

Props solarissmoke, SergeyBiryukov, DrewAPicture.
Fixes #10441.

git-svn-id: https://develop.svn.wordpress.org/trunk@37861 602fd350-edb4-49c9-b593-d223f7449a82
2016-06-25 19:56:19 +00:00
Eric Andrew Lewis 8692cdbecd Build/Test Tools: Document `WP_UnitTestCase->go_to()`
Fixes #36679.


git-svn-id: https://develop.svn.wordpress.org/trunk@37319 602fd350-edb4-49c9-b593-d223f7449a82
2016-04-27 18:24:34 +00:00
Boone Gorges 8e317daa37 Tests: Ensure that the default `wp_die()` handler can handle a `WP_Error` object.
Props dd32, utkarshpatel.
Fixes #36166.

git-svn-id: https://develop.svn.wordpress.org/trunk@37071 602fd350-edb4-49c9-b593-d223f7449a82
2016-03-23 19:00:38 +00:00
ericlewis 59c8ba7871 Build/Test Tools: Document PHP unit test classes.
Props boonebgorges.
Fixes #35494.


git-svn-id: https://develop.svn.wordpress.org/trunk@36854 602fd350-edb4-49c9-b593-d223f7449a82
2016-03-05 19:47:41 +00:00
John Blackbourn 1ebe4658f1 Remove (or at least reduce) the need to reset common `$_SERVER` variables before assertions or between tests, by introducing a method which automatically resets them during test setup.
See #35954


git-svn-id: https://develop.svn.wordpress.org/trunk@36721 602fd350-edb4-49c9-b593-d223f7449a82
2016-02-26 02:08:47 +00:00
John Blackbourn ad338fb3d0 Comments: Commit tests missed in [35848].
See #28617


git-svn-id: https://develop.svn.wordpress.org/trunk@35849 602fd350-edb4-49c9-b593-d223f7449a82
2015-12-10 03:15:44 +00:00
John Blackbourn 82cb793355 Initialise `$_SERVER['SERVER_NAME']` during the test bootstrap to avoid individual tests having to do it.
Fixes #34394


git-svn-id: https://develop.svn.wordpress.org/trunk@35350 602fd350-edb4-49c9-b593-d223f7449a82
2015-10-21 23:51:45 +00:00
Scott Taylor f5921a0c98 Unit Tests: consolidate the many separate implementations of `_make_attachment()` into a helper method on `WP_UnitTestCase`.
Fixes #34075.


git-svn-id: https://develop.svn.wordpress.org/trunk@35309 602fd350-edb4-49c9-b593-d223f7449a82
2015-10-21 01:58:52 +00:00
John Blackbourn a314c6c234 Preserve the public and private query var properties on the global `WP` instance when using `WP_UnitTestCase::go_to()`. These properties apply to the application state, not the current request.
See #34346


git-svn-id: https://develop.svn.wordpress.org/trunk@35258 602fd350-edb4-49c9-b593-d223f7449a82
2015-10-18 05:00:40 +00:00
John Blackbourn 9fbff7f63a Add `is_embed()` to `WP_UnitTestCase::assertQueryTrue()` and update the relevant embed tests.
See #32522


git-svn-id: https://develop.svn.wordpress.org/trunk@35257 602fd350-edb4-49c9-b593-d223f7449a82
2015-10-18 04:50:15 +00:00
Scott Taylor b757b37551 Unit Tests: after [35225], make `factory` a method/getter on `WP_UnitTestCase` and add magic methods for BC for every plugin that is extending `WP_UnitTestCase` and accessing the `$factory` instance prop.
Props nerrad, wonderboymusic.
See #30017, #33968.



git-svn-id: https://develop.svn.wordpress.org/trunk@35242 602fd350-edb4-49c9-b593-d223f7449a82
2015-10-17 18:02:16 +00:00
Scott Taylor e70ebea219 Unit Tests: one `$factory` to rule them all, and it shall be `static`.
Using more than one instance of `WP_UnitTest_Factory` causes all kinds of craziness, due to out-of-sync internal generator sequences. Since we want to use `setUpBeforeClass`, we were creating ad hoc instances. To avoid that, we were injecting one `static` instance via Dependency Injection in `wpSetUpBeforeClass`. All tests should really use the `static` instance, so we will remove the instance prop `$factory`.

Replace `$this->factory` with `self::$factory` over 2000 times.
Rewrite all of the tests that were hard-coding dynamic values. 

#YOLOFriday



git-svn-id: https://develop.svn.wordpress.org/trunk@35225 602fd350-edb4-49c9-b593-d223f7449a82
2015-10-16 21:04:12 +00:00
Scott Taylor 16d98ebf73 Unit Tests: implement `setUpBeforeClass()` and `tearDownAfterClass()` on `WP_UnitTestCase`. Use late static binding (plus a gross fallback for PHP 5.2) to check if `wpSetUpBeforeClass()` or `wpTearDownAfterClass()` exist on the called class, and then call it and pass a static `WP_UnitTest_Factory` instance via Dependency Injection, if it exists.
This makes it way easier to add fixtures, and tear them down, without needing to instantiate `WP_UnitTest_Factory` in every class - removes the need to call `commit_transaction()` in each individual class.

See #30017, #33968.


git-svn-id: https://develop.svn.wordpress.org/trunk@35186 602fd350-edb4-49c9-b593-d223f7449a82
2015-10-15 04:43:37 +00:00
Boone Gorges e3f8b888ad In `WP_UnitTestCase`, only flush rewrite rules when they're set.
See [34810]. See #33968.

git-svn-id: https://develop.svn.wordpress.org/trunk@35136 602fd350-edb4-49c9-b593-d223f7449a82
2015-10-13 15:21:20 +00:00
Boone Gorges 578134d4ff Use `wp_installing()` instead of `WP_INSTALLING` constant.
The `WP_INSTALLING` constant is a flag that WordPress sets in a number of
places, telling the system that options should be fetched directly from the
database instead of from the cache, that WP should not ping wordpress.org for
updates, that the normal "not installed" checks should be bypassed, and so on.

A constant is generally necessary for this purpose, because the flag is
typically set before the WP bootstrap, meaning that WP functions are not yet
available.  However, it is possible - notably, during `wpmu_create_blog()` -
for the "installing" flag to be set after WP has already loaded. In these
cases, `WP_INSTALLING` would be set for the remainder of the process, since
there's no way to change a constant once it's defined. This, in turn, polluted
later function calls that ought to have been outside the scope of site
creation, particularly the non-caching of option data. The problem was
particularly evident in the case of the automated tests, where `WP_INSTALLING`
was set the first time a site was created, and remained set for the rest of the
suite.

The new `wp_installing()` function allows developers to fetch the current
installation status (when called without any arguments) or to set the
installation status (when called with a boolean `true` or `false`). Use of
the `WP_INSTALLING` constant is still supported; `wp_installing()` will default
to `true` if the constant is defined during the bootstrap.

Props boonebgorges, jeremyfelt.
See #31130.

git-svn-id: https://develop.svn.wordpress.org/trunk@34828 602fd350-edb4-49c9-b593-d223f7449a82
2015-10-05 15:05:26 +00:00
Drew Jaynes 0ea2acb720 Tests: Permalink Structures Phase II: DRY up logic for setting permalink structures in test methods.
Renames `reset_permalinks()` to `set_permalink_structure()` (mimicking `$wp_rewrite->set_permalink_structure()`) and allows it to accept an optional permalink structure. In this way, we can double dip using it to both set and reset the permalink structure from anywhere.

Removes alot of duplicated code from tests.

See #33968.


git-svn-id: https://develop.svn.wordpress.org/trunk@34810 602fd350-edb4-49c9-b593-d223f7449a82
2015-10-03 20:54:11 +00:00
Drew Jaynes 01f25fa247 Tests: Last try: It's redundant to reset permalinks on `tearDown()` if we're already doing it on every `setUp()`.
Removes the restoration logic, which leveraged a static property initialized with `get_option()`.

See #33968.


git-svn-id: https://develop.svn.wordpress.org/trunk@34807 602fd350-edb4-49c9-b593-d223f7449a82
2015-10-03 19:26:24 +00:00
Drew Jaynes 9cdffaaed8 Tests: Move the global permalinks reset routine to only fire when running core tests.
Should fix intermittent mysqli response errors in the AJAX tests.

See #33968.


git-svn-id: https://develop.svn.wordpress.org/trunk@34803 602fd350-edb4-49c9-b593-d223f7449a82
2015-10-03 18:35:58 +00:00
Drew Jaynes adffdab161 Tests: Introduce `WP_UnitTestCase::reset_permalinks()`, an attempt to DRY up logic for resetting and restoring default permalinks on `setUp()` and `tearDown()`.
See #33968.


git-svn-id: https://develop.svn.wordpress.org/trunk@34802 602fd350-edb4-49c9-b593-d223f7449a82
2015-10-03 17:14:12 +00:00
Boone Gorges 9dd6f369cf Fix db_version juggling during non-multisite tests.
Continuing with the "code is poetry" theme after [34719], we need to continue
to update the option in the database on non-multisite in this
`wp_insert_term()` test.

See #31130.

git-svn-id: https://develop.svn.wordpress.org/trunk@34720 602fd350-edb4-49c9-b593-d223f7449a82
2015-09-30 13:27:09 +00:00
Boone Gorges 13e28a6d34 Prevent Multisite term tests from hitting database for 'db_version'.
[34718] introduced a 'db_version' check to term meta functions, to ensure that
they don't run when the term meta schema is not yet in place. This call to
`get_option()` causes a database hit during Multisite tests, due to the
presence of the `WP_INSTALLING` constant. See #31130. The extra database
queries are causing cache tests to fail.

In similar cases, we have `markTestSkipped()` when `is_multisite()`. Because
the term meta API is so extensive - term meta caches can be primed anywhere a
`WP_Query` loop is fired up - we implement a more generous workaround in this
case. To prevent `get_option( 'db_version' )` from hitting the database during
multisite unit tests, we use a 'pre_option_' filter.

Heaven help us.

See #34091.

git-svn-id: https://develop.svn.wordpress.org/trunk@34719 602fd350-edb4-49c9-b593-d223f7449a82
2015-09-30 13:08:49 +00:00
John Blackbourn 5ff16465f2 Introduce `WP_UnitTestCase::assertNotWPError()` for when a test needs to assert that something is, well, not a `WP_Error`.
See #33978


git-svn-id: https://develop.svn.wordpress.org/trunk@34638 602fd350-edb4-49c9-b593-d223f7449a82
2015-09-27 21:28:26 +00:00
Boone Gorges da5c716009 Introduce `WP_UnitTestCase::delete_user()`.
This static method provides a multisite-agnostic way to delete users during
automated testing.

See #32796.

git-svn-id: https://develop.svn.wordpress.org/trunk@32953 602fd350-edb4-49c9-b593-d223f7449a82
2015-06-26 12:58:29 +00:00
Boone Gorges 9597066674 Make sure `$_SERVER['SERVER_NAME']` is set whenever `wp_mail()` is called in PHPUnit tests.
This eliminates PHP notices when `wp_mail()` needs to determine its own From header.

See [25381] for a previous fix, which focused only on the mail-specific tests.

Fixes #32702.

git-svn-id: https://develop.svn.wordpress.org/trunk@32839 602fd350-edb4-49c9-b593-d223f7449a82
2015-06-18 13:52:29 +00:00
Boone Gorges 129e5f5278 Ensure that a request URL is always set in `WP_UnitTestCase::go_to()`.
Failure to set this variable meant that passing the home URL to `go_to()`
(without a trailing slash) resulted in a PHP notice, and failed to reset the
globals properly.

Props joostdevalk.
Fixes #31417.

git-svn-id: https://develop.svn.wordpress.org/trunk@31515 602fd350-edb4-49c9-b593-d223f7449a82
2015-02-23 01:07:18 +00:00
Boone Gorges 086e03e8e2 Improved handling of expectedDeprecated and expectedIncorrectUsage annotations in unit tests.
* Do the `expectedDeprecated()` check in `assertPostConditions()` instead of `tearDown()`. Previously, `fail`ing inside of `tearDown()` was causing the rest of the teardown process to be aborted, resulting in inter-test leakage.
* Collect all `expectedDeprecated` and `expectedIncorrectUsage` annotations in an entire method and display them all when `fail`ing, instead of showing only the first one.

Props jdgrimes.
Fixes #31362.

git-svn-id: https://develop.svn.wordpress.org/trunk@31469 602fd350-edb4-49c9-b593-d223f7449a82
2015-02-17 16:11:09 +00:00
Boone Gorges 2fb8562a97 Introduce `setExpectedDeprecated()` and `setExpectedIncorrectUsage()` methods to `WP_UnitTestCase.
These methods provide a procedural alternative to the `@expectedDeprecated`
and `@expectedIncorrectUsage` test annotations, and parallel PHPUnit's native
`setExpectedException()`.

Props prasoon2211, jdgrimes.
Fixes #28486.

git-svn-id: https://develop.svn.wordpress.org/trunk@31306 602fd350-edb4-49c9-b593-d223f7449a82
2015-01-30 16:47:44 +00:00
Boone Gorges 04eef529b0 Remove non-built-in post statuses during each unit test teardown.
See #29167.

git-svn-id: https://develop.svn.wordpress.org/trunk@31046 602fd350-edb4-49c9-b593-d223f7449a82
2015-01-04 20:05:53 +00:00
Gary Pendergast 897b1645bb Unit Tests: The `assertEqualSets()` helper was returning true for some sets that were not equal. `assertEqualSets()` now behaves correctly, and the new `assertEqualSetsWithIndex()` helper also checks that the array indexes are the same.
Fixes #30522.


git-svn-id: https://develop.svn.wordpress.org/trunk@30687 602fd350-edb4-49c9-b593-d223f7449a82
2014-12-01 05:01:29 +00:00
Scott Taylor 653485e87f `wp_upload_dir()` has a (little-known?) side effect: if you call it, it will attempt to create an uploads directory for the current month. As such, `tearDown()` and cleanup routines have to be in sync with this behavior when deleting bogus directories used in unit tests.
Examples: if you clean up directories in a test, or a test fails before the directories are cleaned, or a test fails before the `'upload_path'` option is reset, the next call to `wp_upload_dir()` will recreate the directories you just tried to delete.

These changes ensure that `src/foo` and `/tmp/wp-unit-test` directories are deleted immediately after `wp_upload_dir()` is fired in the tests. 
 
Fixes #30513.


git-svn-id: https://develop.svn.wordpress.org/trunk@30658 602fd350-edb4-49c9-b593-d223f7449a82
2014-11-30 19:05:52 +00:00
Boone Gorges 96f9d84dba Stop checking Trac to skip tests against open tickets.
In general, skipped tests should live only in patches, which are committed at
the same time that the corresponding bug is fixed. In cases where it's
necessary to skip a test, use `markTestSkipped()` to declare this fact
explicitly.

We continue to check Trac when using `WP_UnitTestCase` to run non-core tests.

See #30284.

git-svn-id: https://develop.svn.wordpress.org/trunk@30526 602fd350-edb4-49c9-b593-d223f7449a82
2014-11-22 21:10:12 +00:00
Aaron Jorbin b94dd6a7ec When asserting microtime output as a number, make it a number
microtime is by default a string. Doing a greater then or less than check of that string is a bad idea since it uses the first part (the micro part of microtime) rather then the actual time. This adds a helper to convert microtime output into a float which we can then use to properly compare the output of microtime.

This fixes an intermittent test failure.

fixes #30336



git-svn-id: https://develop.svn.wordpress.org/trunk@30337 602fd350-edb4-49c9-b593-d223f7449a82
2014-11-13 22:03:15 +00:00
Boone Gorges fe726039bd Share fixtures across a number of query-related test classes.
This shaves 10-20 seconds off the running time for the suite.

See #30017.

git-svn-id: https://develop.svn.wordpress.org/trunk@30276 602fd350-edb4-49c9-b593-d223f7449a82
2014-11-08 15:29:31 +00:00
Boone Gorges f12b0f9aad Correct variable name in `WP_UnitTestCase::temp_filename()`.
git-svn-id: https://develop.svn.wordpress.org/trunk@30268 602fd350-edb4-49c9-b593-d223f7449a82
2014-11-07 01:36:05 +00:00
Boone Gorges 1fc0287164 Set current user to 0 after each unit test.
This prevents $current_user, $userdata, and other user-related globals from
being polluted if a previous test does not properly reset the current user.

Props nacin, mnelson4.
Fixes #29712.

git-svn-id: https://develop.svn.wordpress.org/trunk@30001 602fd350-edb4-49c9-b593-d223f7449a82
2014-10-24 00:58:35 +00:00
Boone Gorges f7dbf962d7 Only reset taxonomies and post types between tests when running core tests.
A growing number of plugins and other WP-based projects use the core test
tools, such as WP_UnitTestCase, as the basis of their own tests and continuous
integration setups. At the same time, many of these third-party plugins use
custom post types and taxonomies, which are generally registered a single time
during a run of the tests: at 'init', before the testcases have run. Wiping out
these globals between tests will mess with these third-party builds.

Best practice for plugin developers is probably to clean up their own post types
and taxonomies and then reinitialize before each test. But, in the interest of
not breaking everyone's builds, the core test suite will not enforce this.

Fixes #29827.

git-svn-id: https://develop.svn.wordpress.org/trunk@29869 602fd350-edb4-49c9-b593-d223f7449a82
2014-10-10 20:25:39 +00:00
Boone Gorges 5e7be10860 Reset post types and taxonomies before each unit test.
Registering a post type or taxonomy during a unit test causes modifications to
global variables. If the test fails to clean up these globals - either by
neglecting to call _unregister_post_type()/_unregister_taxonomy() at all or by
failing before getting a chance to do so - tests that run later in the suite
can fail, leading to much gnashing of teeth. Wiping all taxonomies and
restoring to the defaults before each test ensures that we always start with a
clean slate.

Fixes #29827.

git-svn-id: https://develop.svn.wordpress.org/trunk@29860 602fd350-edb4-49c9-b593-d223f7449a82
2014-10-09 00:57:26 +00:00
Sergey Biryukov 262929004d Avoid undefined index notices in WP_UnitTestCase::_restore_hooks().
props kevinlangleyjr.
fixes #29123.

git-svn-id: https://develop.svn.wordpress.org/trunk@29503 602fd350-edb4-49c9-b593-d223f7449a82
2014-08-15 15:40:48 +00:00
Helen Hou-Sandi c72d29e63e Revert [29267], as it causes unit test breakage and appears unnecessary. see #28986.
git-svn-id: https://develop.svn.wordpress.org/trunk@29274 602fd350-edb4-49c9-b593-d223f7449a82
2014-07-23 02:22:57 +00:00
Scott Taylor 7f9edd3e69 After [29251], set the array keys in `WP_UnitTestCase::$hooks_saved` to prevent notices in 3rd party use cases.
Props danielbachhuber.
Fixes #28986.



git-svn-id: https://develop.svn.wordpress.org/trunk@29267 602fd350-edb4-49c9-b593-d223f7449a82
2014-07-22 17:21:40 +00:00
Scott Taylor 36e455d018 Backup filter globals ( `$merged_filters, $wp_actions, $wp_current_filter, $wp_filter` ) statically when running unit tests, restore on `tearDown()`. This ensures that all tests initially use the same filters/actions.
Props mnelson4, wonderboymusic.
Fixes #28535.


git-svn-id: https://develop.svn.wordpress.org/trunk@29251 602fd350-edb4-49c9-b593-d223f7449a82
2014-07-19 23:58:07 +00:00
Scott Taylor d97b10eecf Wouldn't it be incredible if you could run Unit Tests without all of your uploads being indiscriminately blown away and your upload folder permissions being destroyed?
The Future Is Now.

Fixes #28847.


git-svn-id: https://develop.svn.wordpress.org/trunk@29120 602fd350-edb4-49c9-b593-d223f7449a82
2014-07-12 07:08:15 +00:00
Scott Taylor 9ab7f4b144 Avoid polluted `$table_prefix` and `$_wp_switched_stack` in multisite unit tests.
Add `WP_TESTS_TABLE_PREFIX` in `bootstrap.php` to ensure the original value for `$table_prefix` is always available. 

Props jeremyfelt.
Fixes #28692.


git-svn-id: https://develop.svn.wordpress.org/trunk@28943 602fd350-edb4-49c9-b593-d223f7449a82
2014-07-01 18:51:46 +00:00
Scott Taylor 17e4da2bd8 Remove `html5` theme support on `tearDown()` so theme settings don't leak into other tests.
git-svn-id: https://develop.svn.wordpress.org/trunk@27795 602fd350-edb4-49c9-b593-d223f7449a82
2014-03-27 20:54:08 +00:00
Scott Taylor a31a167359 Reset `$wp_query` and `$post` during `WP_UnitTestCase::tearDown()` to avoid spillage of globals between tests.
See #26321.



git-svn-id: https://develop.svn.wordpress.org/trunk@27183 602fd350-edb4-49c9-b593-d223f7449a82
2014-02-17 21:03:43 +00:00
Andrew Nacin e07424147c Test suite: Trim queries before deciding whether to create temporary tables.
props jdgrimes.
fixes #24800.


git-svn-id: https://develop.svn.wordpress.org/trunk@27086 602fd350-edb4-49c9-b593-d223f7449a82
2014-02-03 22:39:47 +00:00