Make WordPress Core

Opened 9 months ago

Closed 8 months ago

Last modified 7 months ago

#59325 closed enhancement (fixed)

Block Themes: Test that `_build_block_template_result_from_file` injects `theme` attribute

Reported by: bernhard-reiter's profile Bernhard Reiter Owned by: bernhard-reiter's profile Bernhard Reiter
Milestone: 6.4 Priority: normal
Severity: normal Version:
Component: Themes Keywords: has-patch has-unit-tests commit
Focuses: Cc:

Description

While we have coverage for _inject_theme_attribute_in_block_template_content, those tests only verify that that function does what is supposed to do; there's however no guarantee that _build_block_template_result_from_file uses that function (or whatever other technique) to actually inject the theme attribute.

This will be needed for #59313.

Change History (11)

This ticket was mentioned in PR #5186 on WordPress/wordpress-develop by @Bernhard Reiter.


9 months ago
#1

  • Keywords has-patch has-unit-tests added

While we have coverage for _inject_theme_attribute_in_block_template_content, those tests only verify that that function does what is supposed to do; there's however no guarantee that _build_block_template_result_from_file uses that function (or whatever other technique) to actually inject the theme attribute (see).

### Testing Instructions

  • Apply the following patch first on trunk, and run unit tests (npm run test:php -- --group=block-templates). Verify that they pass (although they shouldn't) ✅ 👎
  • Then, apply the patch on top of this branch, and run unit tests again. Verify that this time, they fail. :x: 👍
  • Finally, remove the patch (or check CI for this branch): Unit tests should pass ✅ 👍
  • src/wp-includes/block-template-utils.php

    diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php
    index b8538ee3b43e..11d4141aa342 100644
    a b function _build_block_template_result_from_file( $template_file, $template_type 
    565565        $template                 = new WP_Block_Template();
    566566        $template->id             = $theme . '//' . $template_file['slug'];
    567567        $template->theme          = $theme;
    568         $template->content        = _inject_theme_attribute_in_block_template_content( $template_content );
     568        $template->content        = $template_content;
    569569        $template->slug           = $template_file['slug'];
    570570        $template->source         = 'theme';
    571571        $template->type           = $template_type;

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

@gziolo commented on PR #5186:


8 months ago
#2

As for the functionality, these new tests cover the part of the functionality that is going to be refactored as outlined in https://github.com/WordPress/wordpress-develop/pull/5158 to bring support for the new Block Hooks feature. I can confirm that the tests look as intended.

@Bernhard Reiter commented on PR #5186:


8 months ago
#3

@costdev Thank you for your feedback! I believe I've addressed it all 😊

Would you mind giving it another look when you have a moment? I'd love to land this soon as it's only the first step for Block Hooks to land in 6.4 😊

#4 @Bernhard Reiter
8 months ago

  • Component changed from General to Themes
  • Keywords commit added

#5 @Bernhard Reiter
8 months ago

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

In 56562:

Themes: Add test for theme atttibute in file-based block template.

While we already have unit test coverage for _inject_theme_attribute_in_block_template_content, those tests only verify that that function does what is supposed to do; there's however no guarantee that _build_block_template_result_from_file uses that function (or whatever other technique) to actually inject the theme attribute.

This patch adds test coverage to verify that the theme attribute is correctly injected by _build_block_template_result_from_file.

Props costdev, gziolo, mukesh27, spacedmonkey.
Fixes #59325. See #59313.

This ticket was mentioned in PR #5396 on WordPress/wordpress-develop by @gziolo.


8 months ago
#7

…ile`

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

Follow-up to the comment from @costdev in https://github.com/WordPress/wordpress-develop/pull/5186#discussion_r1324147112:

Here's a coverage report which includes existing tests and tests included in this PR.

https://i0.wp.com/github.com/WordPress/wordpress-develop/assets/79332690/1cb0faa5-5923-4089-aab4-274c84b2ec8a
https://i0.wp.com/github.com/WordPress/wordpress-develop/assets/79332690/476f3ac1-46a4-4124-902f-4b7b7837ebc6

Everything that this PR intends to cover appears to be covered. 🙂

In terms of the remaining coverage for this function:

  • Line 585 isn't hit by the tests that specifically cover this function.
  • Two branches aren't covered.
    • One branch can be covered by testing with a postTypes key in $template_file and a $template_type of wp_template.
    • One branch can be covered by ensuring both outcomes of line 572 are met by tests, then testing $template->title for the expected result.
  • There don't appear to be tests specifically for this function that provide a non-default slug in $template_file. This means lines 578-581, while _hit_ by the tests, don't appear to have coverage to ensure that $template->title is retained for wp_template types with a non-default slug in the $template_file.

As the uncovered parts don't seem to be part of this PR's goals, we can follow up to complete coverage for _build_block_template_result_from_file(). I'd also suggest that we start to separate the tests for each function into their own files, completing @ticket, @covers, and any other annotations that need to be added. This makes it easier not to get lost amongst the tests.

@gziolo commented on PR #5396:


8 months ago
#8

Thank you for the feedback. I’ll rename and move test cases as suggested and land tomorrow 👍

@gziolo commented on PR #5396:


7 months ago
#9

It would be good to cover these two conditions so there's a complete set of tests in this function's test class.

Do I understand correctly that you mean adding coverage in the same test suite? I'm sure it has coverage in other places, but I agree we can add another test here 👍🏻

I want to wait until #5455 as I will have to rebase this branch and probably remove on or two tests, maybe move some new tests. I can work on the feedback as part of that.

#10 @gziolo
7 months ago

In 56983:

Tests: Improve code coverage for _build_block_template_result_from_file

Props costdev, bernhard-reiter.
See #54335, #59325.
Follow-up for [56562].

@gziolo commented on PR #5396:


7 months ago
#11

Committed with https://core.trac.wordpress.org/changeset/56983.

It would be good to cover these two conditions so there's a complete set of tests in this function's test class.

I will work on more tests separately covering the filter hooked_block_types, and I will tackle it as part of that effort.

Note: See TracTickets for help on using tickets.