block-context.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * WP_Block_Context Tests
  4. *
  5. * @package WordPress
  6. * @subpackage Blocks
  7. * @since 5.5.0
  8. */
  9. /**
  10. * Tests for WP_Block_Context
  11. *
  12. * @since 5.5.0
  13. *
  14. * @group blocks
  15. */
  16. class WP_Block_Context_Test extends WP_UnitTestCase {
  17. /**
  18. * Registered block names.
  19. *
  20. * @var string[]
  21. */
  22. private $registered_block_names = array();
  23. /**
  24. * Sets up each test method.
  25. */
  26. public function setUp() {
  27. global $post;
  28. parent::setUp();
  29. $args = array(
  30. 'post_content' => 'example',
  31. 'post_excerpt' => '',
  32. );
  33. $post = $this->factory()->post->create_and_get( $args );
  34. setup_postdata( $post );
  35. }
  36. /**
  37. * Tear down each test method.
  38. */
  39. public function tearDown() {
  40. while ( ! empty( $this->registered_block_names ) ) {
  41. $block_name = array_pop( $this->registered_block_names );
  42. unregister_block_type( $block_name );
  43. }
  44. parent::tearDown();
  45. }
  46. /**
  47. * Registers a block type.
  48. *
  49. * @param string|WP_Block_Type $name Block type name including namespace, or alternatively a
  50. * complete WP_Block_Type instance. In case a WP_Block_Type
  51. * is provided, the $args parameter will be ignored.
  52. * @param array $args {
  53. * Optional. Array of block type arguments. Any arguments may be defined, however the
  54. * ones described below are supported by default. Default empty array.
  55. *
  56. * @type callable $render_callback Callback used to render blocks of this block type.
  57. * }
  58. */
  59. protected function register_block_type( $name, $args ) {
  60. register_block_type( $name, $args );
  61. $this->registered_block_names[] = $name;
  62. }
  63. /**
  64. * Tests that a block which provides context makes that context available to
  65. * its inner blocks.
  66. *
  67. * @ticket 49927
  68. */
  69. function test_provides_block_context() {
  70. $provided_context = array();
  71. $this->register_block_type(
  72. 'gutenberg/test-context-provider',
  73. array(
  74. 'attributes' => array(
  75. 'contextWithAssigned' => array(
  76. 'type' => 'number',
  77. ),
  78. 'contextWithDefault' => array(
  79. 'type' => 'number',
  80. 'default' => 0,
  81. ),
  82. 'contextWithoutDefault' => array(
  83. 'type' => 'number',
  84. ),
  85. 'contextNotRequested' => array(
  86. 'type' => 'number',
  87. ),
  88. ),
  89. 'provides_context' => array(
  90. 'gutenberg/contextWithAssigned' => 'contextWithAssigned',
  91. 'gutenberg/contextWithDefault' => 'contextWithDefault',
  92. 'gutenberg/contextWithoutDefault' => 'contextWithoutDefault',
  93. 'gutenberg/contextNotRequested' => 'contextNotRequested',
  94. ),
  95. )
  96. );
  97. $this->register_block_type(
  98. 'gutenberg/test-context-consumer',
  99. array(
  100. 'uses_context' => array(
  101. 'gutenberg/contextWithDefault',
  102. 'gutenberg/contextWithAssigned',
  103. 'gutenberg/contextWithoutDefault',
  104. ),
  105. 'render_callback' => function( $attributes, $content, $block ) use ( &$provided_context ) {
  106. $provided_context[] = $block->context;
  107. return '';
  108. },
  109. )
  110. );
  111. $parsed_blocks = parse_blocks(
  112. '<!-- wp:gutenberg/test-context-provider {"contextWithAssigned":10} -->' .
  113. '<!-- wp:gutenberg/test-context-consumer /-->' .
  114. '<!-- /wp:gutenberg/test-context-provider -->'
  115. );
  116. render_block( $parsed_blocks[0] );
  117. $this->assertSame(
  118. array(
  119. 'gutenberg/contextWithDefault' => 0,
  120. 'gutenberg/contextWithAssigned' => 10,
  121. ),
  122. $provided_context[0]
  123. );
  124. }
  125. /**
  126. * Tests that a block can receive default-provided context through
  127. * render_block.
  128. *
  129. * @ticket 49927
  130. */
  131. function test_provides_default_context() {
  132. global $post;
  133. $provided_context = array();
  134. $this->register_block_type(
  135. 'gutenberg/test-context-consumer',
  136. array(
  137. 'uses_context' => array( 'postId', 'postType' ),
  138. 'render_callback' => function( $attributes, $content, $block ) use ( &$provided_context ) {
  139. $provided_context[] = $block->context;
  140. return '';
  141. },
  142. )
  143. );
  144. $parsed_blocks = parse_blocks( '<!-- wp:gutenberg/test-context-consumer /-->' );
  145. render_block( $parsed_blocks[0] );
  146. $this->assertSame(
  147. array(
  148. 'postId' => $post->ID,
  149. 'postType' => $post->post_type,
  150. ),
  151. $provided_context[0]
  152. );
  153. }
  154. /**
  155. * Tests that default block context can be filtered.
  156. *
  157. * @ticket 49927
  158. */
  159. function test_default_context_is_filterable() {
  160. $provided_context = array();
  161. $this->register_block_type(
  162. 'gutenberg/test-context-consumer',
  163. array(
  164. 'uses_context' => array( 'example' ),
  165. 'render_callback' => function( $attributes, $content, $block ) use ( &$provided_context ) {
  166. $provided_context[] = $block->context;
  167. return '';
  168. },
  169. )
  170. );
  171. $filter_block_context = function( $context ) {
  172. $context['example'] = 'ok';
  173. return $context;
  174. };
  175. $parsed_blocks = parse_blocks( '<!-- wp:gutenberg/test-context-consumer /-->' );
  176. add_filter( 'render_block_context', $filter_block_context );
  177. render_block( $parsed_blocks[0] );
  178. remove_filter( 'render_block_context', $filter_block_context );
  179. $this->assertSame( array( 'example' => 'ok' ), $provided_context[0] );
  180. }
  181. }