2

I am currently doing TDD with WordPress, in the PHPUnit I need to test if the meta box is actually registered on a method call, I could not find any function in WordPress to do that, so I was trying to verify it by calling global $wp_meta_boxes, but it returns null when invoked inside a PHPUnit test.

Is this variable is assigned on a hook?

Could any one provide the action/filter name?

0

Here is what i have did as a workaround, i realized we cant do that in TDD way.so its better to write the code first instead of writing the test when you face this problem.

So in my code i have added this

add_meta_box( 'meta-box-id', __( 'My Meta Box', 'textdomain' ), 'wpdocs_my_display_callback', 'post' );

and in test the $wp_meta_boxes is now displaying the array of registered meta boxes.

array(1) {
  ["post"]=>
  array(1) {
    ["advanced"]=>
    array(1) {
      ["default"]=>
      array(1) {
        ["meta-box-id"]=>
        array(4) {
          ["id"]=>
          string(11) "meta-box-id"
          ["title"]=>
          string(11) "My Meta Box"
          ["callback"]=>
          string(26) "wpdocs_my_display_callback"
          ["args"]=>
          NULL
        }
      }
    }
  }
}

This changes nothing, the code still remains the same, but the way of writing differs now due to this issue.

1
  • Could you post the code of the test that you are using? That would make it easier to understand the issue that you are facing.
    – Fränk
    Oct 13 '20 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.