سؤال

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?

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى wordpress.stackexchange
scroll top