Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unit tests for multisite #1327

Merged
merged 3 commits into from
Jul 8, 2024
Merged

Fix unit tests for multisite #1327

merged 3 commits into from
Jul 8, 2024

Conversation

westonruter
Copy link
Member

Summary

Fixes #1326

Relevant technical choices

@westonruter westonruter added the [Type] Bug An existing feature is broken label Jul 1, 2024
@westonruter westonruter added this to the performance-lab n.e.x.t milestone Jul 1, 2024
@westonruter
Copy link
Member Author

westonruter commented Jul 2, 2024

Interesting. There are PHPUnit test failures in most PHP versions related to Image Placeholders. I don't get this error locally. Instead I get a fatal error:

PHP Fatal error: Cannot declare class Perflab_Server_Timing_Metric, because the name is already in use in /var/www/html/wp-content/plugins/performance/plugins/performance-lab/includes/server-timing/class-perflab-server-timing-metric.php on line 14

@mukeshpanchal27
Copy link
Member

@westonruter The current trunk doesn't work for multisite unit tests; it shows No tests executed! for all plugins. Check https://github.com/WordPress/performance/actions/runs/9749428903/job/26906551079.

Additionally, I debugged the unit test failures for the current PR and found that tiff and bmp are not part of the supported mime types for multisite. However, support for these has been added in WP 6.6, as seen in changeset 58400. These changes are already in trunk, so https://github.com/WordPress/performance/actions/runs/9750444816/job/26909889640?pr=1327 has passed ✅.

Additionally, I occasionally encounter a PHP Fatal error: Cannot declare class Perflab_Server_Timing_Metric error when running the unit tests. However, this issue is intermittent and I cannot consistently reproduce it.

@mukeshpanchal27
Copy link
Member

If we want to solve the unit tests issue then we have to skip test_get_dominant_color_invalid test for multisite.

$mime_type = wp_check_filetype( $image_path )['type'];
// Old WP does not support ".tiff" and ".bmp" so return false.
if ( ! $mime_type ) {
	$this->markTestSkipped( "Mime type is not supported." );
}
if ( ! wp_image_editor_supports( array( 'mime_type' => $mime_type ) ) ) {
	$this->markTestSkipped( "Mime type $mime_type is not supported." );
}

@swissspidy
Copy link
Member

The current trunk doesn't work for multisite unit tests; it shows No tests executed! for all plugins

That's why this PR was opened, to fix this :-)

If we want to solve the unit tests issue then we have to skip test_get_dominant_color_invalid test for multisite.

Sounds reasonable 👍

@westonruter
Copy link
Member Author

If we want to solve the unit tests issue then we have to skip test_get_dominant_color_invalid test for multisite.

@mukeshpanchal27 Would you amend this PR with your suggestion?

Copy link

github-actions bot commented Jul 4, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: westonruter <westonruter@git.wordpress.org>
Co-authored-by: mukeshpanchal27 <mukesh27@git.wordpress.org>
Co-authored-by: thelovekesh <thelovekesh@git.wordpress.org>
Co-authored-by: swissspidy <swissspidy@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

composer.json Outdated Show resolved Hide resolved
bin/generate-multisite-phpunit.php Outdated Show resolved Hide resolved
.gitignore Outdated Show resolved Hide resolved
tools/phpcs/phpcs.ruleset.xml Outdated Show resolved Hide resolved
Comment on lines +178 to +185
$mime_type = wp_check_filetype( $image_path )['type'];
// Old WP does not support ".tiff" and ".bmp" so return false.
if ( ! $mime_type ) {
$this->markTestSkipped( 'Mime type is not supported.' );
}
if ( ! wp_image_editor_supports( array( 'mime_type' => $mime_type ) ) ) {
$this->markTestSkipped( "Mime type $mime_type is not supported." );
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was discovered previously as well when I added support for running PHPUnit tests using a custom setup.

As a fix, I didn't skip these tests rather I added tiff and bmp in supported mime types in multisite.

/**
 * Setup before class.
 */
public static function wpSetUpBeforeClass() {
	// Setup site options if it's a multisite network.
	if ( is_multisite() ) {
		$site_exts = explode( ' ', get_site_option( 'upload_filetypes', 'jpg jpeg png gif' ) );

		// Add `tiff` and `bmp` to the list of allowed file types.
		// These are removed by default in multisite.
		$site_exts[] = 'tiff';
		$site_exts[] = 'bmp';

		update_site_option( 'upload_filetypes', implode( ' ', $site_exts ) );
	}
}

See https://github.com/WordPress/performance/pull/1065/files#diff-670d6c64aed0f0b20a7a5971711c80e3d2b739a57e12b41044ee268010f01a13

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not in favor of adding support in multisite as it's a hacky solution. If WordPress itself doesn't support it, we should skip those tests to accurately reflect the lack of support in multisites.

As of WordPress 6.6, support for these features has been added in core, as seen in changeset 58400. These changes are already in trunk, so the tests are passing as seen in this run ✅.

From 6.6 onwards, the tests will work without needing to be skipped.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't call it a hacky solution, as WordPress allows this extensibility of adding more mime types. Also, since tests are running with these MIME types on a single site, testing them on a multisite setup looks good to me. However, it was just a suggestion, and I'm perfectly fine with either approach.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we can go with either approach.

I think the unit tests for dominant-color-images need to be revisited. The valid and invalid MIME type tests seem to be skipped somehow, as they are meant to test functionality based on the MIME type. We should ensure these tests are correctly executed to verify the implementation accurately.

Single site skipped tests

There were 24 skipped tests:

1) Test_Dominant_Color_Image_Editor_GD::test_get_dominant_color_invalid with data set "tiff" ('/var/www/html/wp-content/plug...e.tiff')
Mime type image/tiff is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:184

2) Test_Dominant_Color_Image_Editor_GD::test_get_dominant_color_invalid with data set "bmp" ('/var/www/html/wp-content/plug...ge.bmp')
Mime type image/bmp is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:184

3) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_valid with data set "animated_gif" ('/var/www/html/wp-content/plug...ed.gif', array('874e4e', '864e4e', 'df7f7f'), true)
Mime type image/gif is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:1[56](https://github.com/WordPress/performance/actions/runs/9812767245/job/27097470441?pr=1327#step:9:57)

4) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_valid with data set "red_jpg" ('/var/www/html/wp-content/plug...ed.jpg', array('ff0000', 'fe0000'), false)
Mime type image/jpeg is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:156

5) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_valid with data set "green_jpg" ('/var/www/html/wp-content/plug...en.jpg', array('00ff00', '00ff01', '02ff01'), false)
Mime type image/jpeg is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:156

6) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_valid with data set "white_jpg" ('/var/www/html/wp-content/plug...te.jpg', array('ffffff'), false)
Mime type image/jpeg is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:156

7) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_valid with data set "red_gif" ('/var/www/html/wp-content/plug...ed.gif', array('ff0000'), false)
Mime type image/gif is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:156

8) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_valid with data set "green_gif" ('/var/www/html/wp-content/plug...en.gif', array('00ff00'), false)
Mime type image/gif is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:156

9) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_valid with data set "white_gif" ('/var/www/html/wp-content/plug...te.gif', array('ffffff'), false)
Mime type image/gif is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:156

10) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_valid with data set "trans_gif" ('/var/www/html/wp-content/plug...ns.gif', array('5a5a5a', '020202'), true)
Mime type image/gif is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:156

11) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_valid with data set "red_png" ('/var/www/html/wp-content/plug...ed.png', array('ff0000'), false)
Mime type image/png is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:156

12) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_valid with data set "green_png" ('/var/www/html/wp-content/plug...en.png', array('00ff00'), false)
Mime type image/png is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:156

13) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_valid with data set "white_png" ('/var/www/html/wp-content/plug...te.png', array('ffffff'), false)
Mime type image/png is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:156

14) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_valid with data set "trans_png" ('/var/www/html/wp-content/plug...ns.png', array('000000'), true)
Mime type image/png is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:156

15) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_valid with data set "red_webp" ('/var/www/html/wp-content/plug...d.webp', array('ff0000'), false)
Mime type image/webp is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:156

16) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_valid with data set "green_webp" ('/var/www/html/wp-content/plug...n.webp', array('00ff00'), false)
Mime type image/webp is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:156

17) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_valid with data set "white_webp" ('/var/www/html/wp-content/plug...e.webp', array('ffffff'), false)
Mime type image/webp is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:156

18) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_valid with data set "trans_webp" ('/var/www/html/wp-content/plug...s.webp', array('000000'), true)
Mime type image/webp is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:156

19) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_valid with data set "balloons_webp" ('/var/www/html/wp-content/plug...s.webp', array('c1bbb9', 'c0bbb9', 'c0bab8', 'c3bdbd', 'bfbab8'), false)
Mime type image/webp is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:156

20) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_valid with data set "half_opaque" ('/var/www/html/wp-content/plug...ue.png', array('7e7e7e'), true)
Mime type image/png is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:156

21) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_invalid with data set "tiff" ('/var/www/html/wp-content/plug...e.tiff')
Mime type image/tiff is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:1[84](https://github.com/WordPress/performance/actions/runs/9812767245/job/27097470441?pr=1327#step:9:85)

22) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_invalid with data set "bmp" ('/var/www/html/wp-content/plug...ge.bmp')
Mime type image/bmp is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:184

23) Test_Dominant_Color::test_get_dominant_color_invalid with data set "tiff" ('/var/www/html/wp-content/plug...e.tiff')
Mime type image/tiff is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:184

24) Test_Dominant_Color::test_get_dominant_color_invalid with data set "bmp" ('/var/www/html/wp-content/plug...ge.bmp')
Mime type image/bmp is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:184
Multisite site skipped tests

There were 24 skipped tests:

1) Test_Dominant_Color_Image_Editor_GD::test_get_dominant_color_invalid with data set "tiff" ('/var/www/html/wp-content/plug...e.tiff')
Mime type is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:181

2) Test_Dominant_Color_Image_Editor_GD::test_get_dominant_color_invalid with data set "bmp" ('/var/www/html/wp-content/plug...ge.bmp')
Mime type is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:181

3) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_valid with data set "animated_gif" ('/var/www/html/wp-content/plug...ed.gif', array('874e4e', '864e4e', 'df7f7f'), true)
Mime type image/gif is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:1[56](https://github.com/WordPress/performance/actions/runs/9812767245/job/27097470441?pr=1327#step:10:57)

4) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_valid with data set "red_jpg" ('/var/www/html/wp-content/plug...ed.jpg', array('ff0000', 'fe0000'), false)
Mime type image/jpeg is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:156

5) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_valid with data set "green_jpg" ('/var/www/html/wp-content/plug...en.jpg', array('00ff00', '00ff01', '02ff01'), false)
Mime type image/jpeg is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:156

6) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_valid with data set "white_jpg" ('/var/www/html/wp-content/plug...te.jpg', array('ffffff'), false)
Mime type image/jpeg is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:156

7) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_valid with data set "red_gif" ('/var/www/html/wp-content/plug...ed.gif', array('ff0000'), false)
Mime type image/gif is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:156

8) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_valid with data set "green_gif" ('/var/www/html/wp-content/plug...en.gif', array('00ff00'), false)
Mime type image/gif is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:156

9) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_valid with data set "white_gif" ('/var/www/html/wp-content/plug...te.gif', array('ffffff'), false)
Mime type image/gif is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:156

10) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_valid with data set "trans_gif" ('/var/www/html/wp-content/plug...ns.gif', array('5a5a5a', '020202'), true)
Mime type image/gif is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:156

21) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_invalid with data set "tiff" ('/var/www/html/wp-content/plug...e.tiff')
Mime type is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:1[81](https://github.com/WordPress/performance/actions/runs/9812767245/job/27097470441?pr=1327#step:10:82)

22) Test_Dominant_Color_Image_Editor_Imagick::test_get_dominant_color_invalid with data set "bmp" ('/var/www/html/wp-content/plug...ge.bmp')
Mime type is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:181

23) Test_Dominant_Color::test_get_dominant_color_invalid with data set "tiff" ('/var/www/html/wp-content/plug...e.tiff')
Mime type is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:181

24) Test_Dominant_Color::test_get_dominant_color_invalid with data set "bmp" ('/var/www/html/wp-content/plug...ge.bmp')
Mime type is not supported.

/var/www/html/wp-content/plugins/performance/plugins/dominant-color-images/tests/data/class-testcase.php:181

Co-authored-by: Lovekesh Kumar <lovekesh.kumar@rtcamp.com>
@swissspidy swissspidy merged commit 351f577 into trunk Jul 8, 2024
19 checks passed
@swissspidy swissspidy deleted the fix/phpunit-multisite branch July 8, 2024 15:45
@westonruter
Copy link
Member Author

I just got this error again when running non-multisite tests locally:

> phpunit '--verbose' '--testsuite' 'performance-lab'
Installing...
Running as single site... To run multisite, use -c tests/phpunit/multisite.xml
[09-Jul-2024 01:45:59 UTC] PHP Fatal error:  Cannot declare class Perflab_Server_Timing_Metric, because the name is already in use in /var/www/html/wp-content/plugins/performance/plugins/performance-lab/includes/server-timing/class-perflab-server-timing-metric.php on line 14
Script phpunit handling the test event returned with error code 255
Script @test --verbose --testsuite performance-lab was called via test:performance-lab
Script @test:performance-lab was called via test:plugins

It's happening each time I do npm run test-php.

@swissspidy
Copy link
Member

Maybe it's a wp-env thing? PL is loaded in wp-env by default. And then the bootstrap.php loads the plugin again, so that could explain the double-inclusion of that class. Alternatively, maybe related to the object-cache drop-in? That one loadsclass-perflab-server-timing-metric.php too.

When I run the tests locally without wp-env, I don't get such an error.

@thelovekesh
Copy link
Member

Alternatively, maybe related to the object-cache drop-in? That one loadsclass-perflab-server-timing-metric.php too.

Yes, it is possible if the test cases don't delete the drop-in. @westonruter and I discussed this a while ago. It seems to be a side effect that occurs after running the test cases. If we halt the server timing test cases, we might be able to replicate this issue.

@westonruter westonruter changed the title Generate phpunit-multisite.xml on the fly Fix unit tests for multisite Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Bug An existing feature is broken
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Running multisite tests results in no tests being executed
4 participants