WordPress.org

Make WordPress Core

Opened 3 months ago

Closed 2 weeks ago

#48844 closed enhancement (fixed)

Unit test for wp()

Reported by: pbearne Owned by: SergeyBiryukov
Milestone: 5.4 Priority: normal
Severity: normal Version: trunk
Component: Bootstrap/Load Keywords: has-unit-tests dev-feedback
Focuses: Cc:

Description

Just a simple test for the wp() function in functions.php

It not posable to test fully as the WP object has already been created by the test framework

Attachments (2)

48844.patch (831 bytes) - added by pbearne 3 months ago.
Screen Shot 2020-01-25 at 12.38.17 AM.png (184.2 KB) - added by donmhico 4 weeks ago.
Full test suite failure

Download all attachments as: .zip

Change History (7)

@pbearne
3 months ago

#1 @SergeyBiryukov
3 months ago

  • Component changed from General to Bootstrap/Load
  • Milestone changed from Awaiting Review to 5.4
  • Owner set to SergeyBiryukov
  • Status changed from new to reviewing

@donmhico
4 weeks ago

Full test suite failure

#2 @donmhico
4 weeks ago

Thanks for the patch @pbearne. The test works fine when run separately phpunit --group=48844 after I added @ticket on it. But it fails on a full test suite.

https://core.trac.wordpress.org/raw-attachment/ticket/48844/Screen%20Shot%202020-01-25%20at%2012.38.17%20AM.png

I'm suspecting that somewhere in the test suite either or both $wp_query and $wp_the_query was changed. Need some more digging.

#3 @pbearne
4 weeks ago

I will have to dig as the tests should not be changing $wp_query and $wp_the_query we may have found a bug in the tests :-)

#4 @SergeyBiryukov
2 weeks ago

The last assertion fails because:

  1. wp() only sets $wp_the_query if it's not already set.
  2. It is actually already set earlier in wp-settings.php.
  3. It is then reset in WP_UnitTestCase_Base::go_to(), called by various canonical tests.
  4. After that, the variable no longer points to the same object, so assertSame() fails.

Some history on that code:

  • [4460] introduced setting $wp_the_query in wp-settings.php.
  • [6233] introduced setting $wp_the_query in wp(). However, the commit message on that changeset was wrong, see [6232] / #5121 for the correct commit message.

Note that WP::main() calls WP::query_posts(), which produces a fatal error if $wp_the_query is not set by then (Call to a member function query() on null), and did so in WP 2.3.3 too.

Given that, I'm not sure if [6232] actually fixed anything, setting $wp_the_query in wp() seems redundant. As far as I can tell, that line is never reached, but confirming that would require some more testing.

For now, let's get the unit test in, except for the last assertion, which apparently cannot be reliably tested.

#5 @SergeyBiryukov
2 weeks ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 47212:

Tests: Add a basic test for wp() function.

Props pbearne, donmhico.
Fixes #48844.

Note: See TracTickets for help on using tickets.