Make WordPress Core

Opened 21 months ago

Closed 21 months ago

Last modified 20 months ago

#57071 closed defect (bug) (invalid)

PHPUnit tests are broken

Reported by: emanuelx's profile emanuelx Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Build/Test Tools Keywords:
Focuses: Cc:

Description (last modified by SergeyBiryukov)

Hi, I'm using the test suite for testing the functionalities for my theme.

I've adapted your install.sh scripts in a single container, and it's working fine until version 6.0.

My tests configuration:
PHP version: 8.0.2

<?php
ENV WP_TESTS_DIR=/app/wordpress/tests
ENV WP_CORE_DIR=/usr/src/wordpress/

define( 'ABSPATH', getenv_docker('WP_CORE_DIR', '') );

Version <=6.0
The behaviour is:

  1. Install.php file - https://github.com/WordPress/wordpress-develop/blob/6.1/tests/phpunit/includes/install.php#L43
  • on this line the require_once will duplicate the forward slash.

code line: require_once ABSPATH . '/wp-includes/class-wpdb.php';
Translated line: require_once '/usr/src/wordpress//wp-includes/class-wpdb.php'

  1. Mock-mailer file - https://github.com/WordPress/wordpress-develop/blob/6.1/tests/phpunit/includes/mock-mailer.php#L2

on this line we have a missing slash.

code line: require_once ABSPATH . 'wp-includes/PHPMailer/PHPMailer.php';
Translated line: require_once '/usr/src/wordpress/wp-includes/PHPMailer/PHPMailer.php';

On the first case, we have a duplicated forward slash, but this is necessary because the includes of phpMailer inside mock-mailer.php file.

but this works fine, without any issue.

Version 6.1
The version 6.1 of tests, the issue with duplicated forward slash will break the code, I don't know why on version <=6.0 it's working fine, and on version 6.1 the error is fired.

I've analysed the code, and this is issue with duplicated slash is present on the code for several versions. So, something was introduced on this version 6.1 to trigger the error with require_once.

Error ABSPATH with forward slash: /usr/src/wordpress/

<?php
[11-Nov-2022 10:03:02 UTC] PHP Warning:  require_once(/usr/src/wordpress//wp-includes/class-wpdb.php): Failed to open stream: No such file or directory in /app/wordpress/tests/includes/install.php on line 43
[11-Nov-2022 10:03:02 UTC] PHP Fatal error:  Uncaught Error: Failed opening required '/usr/src/wordpress//wp-includes/class-wpdb.php' (include_path='.:/usr/local/lib/php') in /app/wordpress/tests/includes/install.php:43
Stack trace:
#0 {main}
  thrown in /app/wordpress/tests/includes/install.php on line 43

Error ABSPATH without forward slash: /usr/src/wordpress

<?php
[11-Nov-2022 10:05:45 UTC] PHP Warning:  require_once(/usr/src/wordpresswp-includes/PHPMailer/PHPMailer.php): Failed to open stream: No such file or directory in /app/wordpress/tests/includes/mock-mailer.php on line 2
PHPUnit 9.5.26 by Sebastian Bergmann and contributors.

Error in bootstrap script: Error:
Failed opening required '/usr/src/wordpresswp-includes/PHPMailer/PHPMailer.php' (include_path='.:/usr/local/lib/php')
#0 /app/wordpress/tests/includes/bootstrap.php(250): require_once()
#1 /theme/tests/bootstrap.php(66): require('/app/wordpress/...')
#2 /theme/vendor/phpunit/phpunit/src/Util/FileLoader.php(66): include_once('/theme/tests/bo...')
#3 /theme/vendor/phpunit/phpunit/src/Util/FileLoader.php(49): PHPUnit\Util\FileLoader::load('/theme/tests/bo...')
#4 /theme/vendor/phpunit/phpunit/src/TextUI/Command.php(565): PHPUnit\Util\FileLoader::checkAndLoad('./tests/bootstr...')
#5 /theme/vendor/phpunit/phpunit/src/TextUI/Command.php(345): PHPUnit\TextUI\Command->handleBootstrap('./tests/bootstr...')
#6 /theme/vendor/phpunit/phpunit/src/TextUI/Command.php(112): PHPUnit\TextUI\Command->handleArguments(Array)
#7 /theme/vendor/phpunit/phpunit/src/TextUI/Command.php(97): PHPUnit\TextUI\Command->run(Array, true)
#8 /theme/vendor/phpunit/phpunit/phpunit(98): PHPUnit\TextUI\Command::main()
#9 /theme/vendor/bin/phpunit(123): include('/theme/vendor/p...')
#10 {main}
Script phpunit -c phpunit-no-coverage.xml.dev handling the phpunit event returned with error code 1

Change History (7)

#1 @SergeyBiryukov
21 months ago

  • Component changed from General to Build/Test Tools
  • Description modified (diff)
  • Keywords has-dev-note removed

#2 in reply to: ↑ description @SergeyBiryukov
21 months ago

Hi there, welcome to WordPress Trac! Thanks for the report.

Replying to emanuelx:

I've analysed the code, and this is issue with duplicated slash is present on the code for several versions. So, something was introduced on this version 6.1 to trigger the error with require_once.

Error ABSPATH with forward slash: /usr/src/wordpress/
...
Error ABSPATH without forward slash: /usr/src/wordpress

Just to clarify, ABSPATH does need the trailing slash:

So I don't think defining it without the slash would work.

Looking at the error:

[11-Nov-2022 10:03:02 UTC] PHP Fatal error: Uncaught Error: Failed opening required '/usr/src/wordpress//wp-includes/class-wpdb.php' (include_path='.:/usr/local/lib/php') in /app/wordpress/tests/includes/install.php:43

I think the double forward slash is not the issue here, it rather has to do with the wpdb class file being renamed from wp-db.php to class-wpdb.php in 6.1, see #56268.

Could you confirm that the file exists in /usr/src/wordpress/? Is that directory also updated to WordPress 6.1?

Last edited 21 months ago by SergeyBiryukov (previous) (diff)

#3 follow-up: @emanuelx
21 months ago

Hey @SergeyBiryukov, I've found the issue. For some reason my docker image doesn't updated the wordpress version, I'm still on 6.0.3.

I've updated my wordpress installation to 6.1, and worked again, But let me ask you, Why wordpress have some paths with double forward slash?

#4 in reply to: ↑ 3 @SergeyBiryukov
21 months ago

Replying to emanuelx:

I've found the issue. For some reason my docker image doesn't updated the wordpress version, I'm still on 6.0.3.

I've updated my wordpress installation to 6.1, and worked again

Great, thanks for the follow-up!

But let me ask you, Why wordpress have some paths with double forward slash?

I think it's just an oversight, though it should not affect anything in practice.

We should be able to remove the extra slash from these lines in phpunit/includes/install.php:

require_once ABSPATH . '/wp-settings.php';

require_once ABSPATH . '/wp-admin/includes/upgrade.php';
require_once ABSPATH . '/wp-includes/class-wpdb.php';

Searching for ABSPATH . '/, it appears to also be present in some other core and test files:

src/wp-admin/includes/class-wp-site-health.php
src/wp-admin/includes/class-wp-site-health-auto-updates.php
src/wp-admin/includes/file.php
tests/phpunit/includes/bootstrap.php
tests/phpunit/includes/install.php
tests/phpunit/tests/customize/widgets.php
tests/phpunit/tests/feed/wpSimplePieFile.php
tests/phpunit/tests/pluggable/signatures.php
tests/phpunit/tests/pluggable/wpMail.php
tests/phpunit/tests/pomo/po.php
tests/phpunit/tests/post.php
tests/phpunit/tests/widgets.php

Would you be interested in opening a new ticket to correct that?

#5 @TobiasBg
21 months ago

  • Keywords needs-testing-info removed
  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed
  • Version 6.1 deleted

There's also a file with ABSPATH . "/.

I have created #57074 with a patch.

Nice find, @emanuelx! As there's nothing left to do in this ticket and as the reported behavior turned out to not be a problem in WP Core, I'll resolve this ticket as invalid which is just the term used here for such cases.

#6 @emanuelx
21 months ago

Thank you @TobiasBg, btw let me share with you my repository with my Dockerfile for tests, I did this because it's easier to run on a pipeline and it's ready for any theme/plugin.

https://github.com/emanuellopes/wordpress-tests

@SergeyBiryukov, Tobias already created them.

#7 @audrasjb
20 months ago

In 54872:

Coding Standards: Remove extra slashes when concatenating ABSPATH with a path.

Since ABSPATH is defined and documented to end with a forward slash /, this changeset removes the first / from strings appended to ABSPATH in various files, leading to // in the resulting path.

Props TobiasBg, audrasjb, SergeyBiryukov, emanuelx.
Fixes #57074.
See #57071.

Note: See TracTickets for help on using tickets.