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

Backport block template resolution algorithm unit tests #1920

Closed
wants to merge 19 commits into from
Closed

Backport block template resolution algorithm unit tests #1920

wants to merge 19 commits into from

Conversation

ockham
Copy link
Contributor

@ockham ockham commented Nov 19, 2021

It seems like we missed backporting these as part of #1796. Doing it now since they will give us better confidence when we need to modify the algorithm.

Prompted by this discussion.

I opted to integrated test fixture files into existing block theme test fixtures in wordpress-develop to keep the number of fixtures and files down. AFAICS, they've been used for testing theme.json related features so far, so I don't think there should be any collisions.

Trac ticket: https://core.trac.wordpress.org/ticket/54478


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@ockham
Copy link
Contributor Author

ockham commented Nov 19, 2021

Can't remove index.php from block-theme yet because of this check:

if ( ! file_exists( $this->theme_root . '/' . $this->stylesheet . '/index.php' ) ) {
$error_message = sprintf(
/* translators: 1: index.php, 2: Documentation URL, 3: style.css */
__( 'Template is missing. Standalone themes need to have a %1$s template file. <a href="%2$s">Child themes</a> need to have a Template header in the %3$s stylesheet.' ),
'<code>index.php</code>',
__( 'https://developer.wordpress.org/themes/advanced-topics/child-themes/' ),
'<code>style.css</code>'
);

Are we going to relax this to accept block-templates/index.html instead (and soon, templates/index.html)?

@ockham ockham changed the title Backport template resolution algorithm unit tests Backport block template resolution algorithm unit tests Nov 19, 2021
@ockham
Copy link
Contributor Author

ockham commented Nov 19, 2021

3 unit test failures to go -- 1 from the newly backported tests, 2 from what seems to be a side effect of the theme switching (?):

There were 3 failures:

1) Block_Template_Test::test_child_theme_php_template_takes_precedence_over_equally_specific_parent_theme_block_template
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'/var/www/tests/phpunit/includes/../data/themedir1/block-theme-child/page-home.php'
+'/var/www/src/wp-includes/template-canvas.php'

/var/www/tests/phpunit/tests/block-template.php:120

2) Tests_General_Template::test_has_custom_logo
Failed asserting that true is false.

/var/www/tests/phpunit/tests/general/template.php:270

3) Tests_General_Template::test_get_custom_logo
Failed asserting that a string is empty.

/var/www/tests/phpunit/tests/general/template.php:315

@youknowriad
Copy link
Contributor

For the remaining failure test_child_theme_php_template_takes_precedence_over_equally_specific_parent_theme_block_template, I followed the logic of the algorithm and it's behaving as expected, basically I don't see any logic that compares the specificity of the php template with the block template that comes from the parent theme so I don't really know why this test passes in Gutenberg.

@ockham
Copy link
Contributor Author

ockham commented Nov 22, 2021

For the remaining failure test_child_theme_php_template_takes_precedence_over_equally_specific_parent_theme_block_template, I followed the logic of the algorithm and it's behaving as expected, basically I don't see any logic that compares the specificity of the php template with the block template that comes from the parent theme so I don't really know why this test passes in Gutenberg.

That's a bit concerning though. The test has a note that specifically points at a regression fix: https://github.com/WordPress/wordpress-develop/pull/1920/files#diff-12e215aaa7e67637d3de9eae20536f56ac9248727dcdca0b6f87c09e3f256166R101-R108

The reasoning of that PR makes sense to me; and I think I thought to preserve that functionality when refactoring the template resolution algorithm, and added the test to cover against further regressions.

I'll look a bit more into this.

@ockham
Copy link
Contributor Author

ockham commented Nov 22, 2021

I think I've tracked it down: You're right that the algorithm is working as expected here.

In Gutenberg OTOH, something isn't quite working with test theme directory registration, and thus, we're not detecting that test-theme-child is a child theme, and test-theme is its parent. (This can be validated by echoing get_template() somewhere in the unit test file: It should return the parent theme -- i.e. test-theme -- rather than the child -- but in GB, it returns the child theme instead.)

This will require a number of different fixes:

  • In the unit test in GB, we need to make sure the child theme is detected as such, and its parent is found. This will cause test_child_theme_php_template_takes_precedence_over_equally_specific_parent_theme_block_template to fail in GB.
  • We need to fix the template resolution algorithm in both GB and WP Core to make test_child_theme_php_template_takes_precedence_over_equally_specific_parent_theme_block_template pass in both.

@youknowriad
Copy link
Contributor

@ockham I think to avoid delaying the switch to "templates" and "parts", we should comment this test for now while we work on it separately. In parallel, I can start the "folders" patch in Core that way it will unblock the Gutenberg PR.

@youknowriad
Copy link
Contributor

@ockham I've created the folders PR in #1932

@carolinan
Copy link
Contributor

Can't remove index.php from block-theme yet because of this check:

if ( ! file_exists( $this->theme_root . '/' . $this->stylesheet . '/index.php' ) ) {
$error_message = sprintf(
/* translators: 1: index.php, 2: Documentation URL, 3: style.css */
__( 'Template is missing. Standalone themes need to have a %1$s template file. <a href="%2$s">Child themes</a> need to have a Template header in the %3$s stylesheet.' ),
'<code>index.php</code>',
__( 'https://developer.wordpress.org/themes/advanced-topics/child-themes/' ),
'<code>style.css</code>'
);

Are we going to relax this to accept block-templates/index.html instead (and soon, templates/index.html)?

This was punted to 6.0:
https://core.trac.wordpress.org/ticket/54272

@ockham
Copy link
Contributor Author

ockham commented Nov 23, 2021

Are we going to relax this to accept block-templates/index.html instead (and soon, templates/index.html)?

This was punted to 6.0: https://core.trac.wordpress.org/ticket/54272

Thanks for clarifying @carolinan!

@ockham
Copy link
Contributor Author

ockham commented Nov 23, 2021

@ockham I think to avoid delaying the switch to "templates" and "parts", we should comment this test for now while we work on it separately. In parallel, I can start the "folders" patch in Core that way it will unblock the Gutenberg PR.

Okay, I'll do that now 👍

@ockham
Copy link
Contributor Author

ockham commented Nov 23, 2021

@ockham I think to avoid delaying the switch to "templates" and "parts", we should comment this test for now while we work on it separately. In parallel, I can start the "folders" patch in Core that way it will unblock the Gutenberg PR.

Okay, I'll do that now 👍

Done: 6d63d8f. Should pass now & be ready for review.

@ockham ockham marked this pull request as ready for review November 23, 2021 13:17
@ockham
Copy link
Contributor Author

ockham commented Nov 23, 2021

Multisite tests are failing. I'll try a rebase for starters.

@ockham
Copy link
Contributor Author

ockham commented Nov 23, 2021

Multisite tests are still failing. Specifically, tests affected are in the sidebars and widgets controller test suites (rest-sidebars-controller.php and rest-widgets-controller.php), and they're mostly getting the wrong REST API responses (e.g. some data where an empty response was expected, or extraneous fields).

I've double-checked that this is not affecting trunk, or other PRs. It's also not affecting any of the single-site tests. The only thing in this PR that I think could've caused this is the theme switching (to our block test theme(s), and back to whatever theme was active before); but apparently there's some cross-site leakage of data. Or so.

cc/ @adamziel @TimothyBJacobs @desrosj in case the above seems familiar to any of you, since AFAICS, y'all have worked on the sidebar and widget controllers.

@ockham
Copy link
Contributor Author

ockham commented Nov 24, 2021

Trying to revert the theme switch, and restoring the delete_option( 'site_logo' ).

@ockham
Copy link
Contributor Author

ockham commented Nov 24, 2021

No, multisite tests are still failing 😕 (It's the same ones as before, plus a bunch more in themeMods.php.

@ockham
Copy link
Contributor Author

ockham commented Nov 24, 2021

The next thing I'll try will be to copy the theme switching strategy from tests/phpunit/tests/theme/wpThemeJsonResolver.php, since that's also using block-theme and block-theme-child, and multisite tests obviously seem to be passing there:

public function set_up() {
parent::set_up();
$this->theme_root = realpath( DIR_TESTDATA . '/themedir1' );
$this->orig_theme_dir = $GLOBALS['wp_theme_directories'];
// /themes is necessary as theme.php functions assume /themes is the root if there is only one root.
$GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root );
add_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) );
add_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) );
add_filter( 'template_root', array( $this, 'filter_set_theme_root' ) );
// Clear caches.
wp_clean_themes_cache();
unset( $GLOBALS['wp_themes'] );
}
public function tear_down() {
$GLOBALS['wp_theme_directories'] = $this->orig_theme_dir;
wp_clean_themes_cache();
unset( $GLOBALS['wp_themes'] );
parent::tear_down();
}

@ockham
Copy link
Contributor Author

ockham commented Nov 24, 2021

Having another look at the test matrix, not all multisite tests are failing. The ones that are passing are:

  • PHPUnit Tests / 5.6 multisite on ubuntu-latest
  • PHPUnit Tests / 8.1 multisite on ubuntu-latest
  • PHPUnit Tests / 5.6 multisite slow tests on ubuntu-latest

@ockham
Copy link
Contributor Author

ockham commented Nov 24, 2021

Uh, but the 8.1 jobs have continue-on-error. (Closer inspections shows that unit tests are failing there.)

The 5.6 ones do seem to pass, tho.

@ockham
Copy link
Contributor Author

ockham commented Nov 24, 2021

The next thing I'll try will be to copy the theme switching strategy from tests/phpunit/tests/theme/wpThemeJsonResolver.php

This got a bit messy upon first attempt (more failures in other tests).

New/alternative hypothesis: Since we've been doing the theme switching in the static wpSetUpBeforeClass (and wpTearDownAfterClass) -- maybe that's happening too early (or late, respectively) -- i.e. before multisite setup is complete? I'll try moving them into the non-static set_up and tear_down.

@ockham
Copy link
Contributor Author

ockham commented Nov 24, 2021

Hmm, looking at the test failures, it seems like the data might be coming from tests/phpunit/tests/widgets.php. I'll look into that file...

@ockham
Copy link
Contributor Author

ockham commented Nov 24, 2021

This could be relevant: #1331

@ockham
Copy link
Contributor Author

ockham commented Nov 24, 2021

Seems like the sidebar/widgets data we're seeing if written by wp_install_defaults. In a multi-site context, the latter is called by wp_initialize_site.

@ockham
Copy link
Contributor Author

ockham commented Nov 24, 2021

@jorgefilipecosta gave me the decisive tip: 5087aeb 😄

@youknowriad
Copy link
Contributor

Great, going to commit this.

@youknowriad
Copy link
Contributor

committed in https://core.trac.wordpress.org/changeset/52246

@ockham ockham deleted the add/block-template-resolution-unit-tests branch November 25, 2021 09:28
ockham added a commit to WordPress/gutenberg that referenced this pull request Nov 25, 2021
These tests have now been backported to Core, see WordPress/wordpress-develop#1920, so they're no longer needed here.
@ockham
Copy link
Contributor Author

ockham commented Nov 25, 2021

I've filed an issue for the failing unit test that we had to disable in Core Trac: https://core.trac.wordpress.org/ticket/54515

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants