render-reusable.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * Reusable block rendering tests.
  4. *
  5. * @package WordPress
  6. * @subpackage Blocks
  7. * @since 5.0.0
  8. */
  9. /**
  10. * Tests for reusable block rendering.
  11. *
  12. * @since 5.0.0
  13. *
  14. * @group blocks
  15. */
  16. class WP_Test_Render_Reusable_Blocks extends WP_UnitTestCase {
  17. /**
  18. * Fake user ID.
  19. *
  20. * @var int
  21. */
  22. protected static $user_id;
  23. /**
  24. * Fake block ID.
  25. *
  26. * @var int
  27. */
  28. protected static $block_id;
  29. /**
  30. * Fake post ID.
  31. *
  32. * @var int
  33. */
  34. protected static $post_id;
  35. /**
  36. * Create fake data before tests run.
  37. *
  38. * @since 5.0.0
  39. *
  40. * @param WP_UnitTest_Factory $factory Helper that creates fake data.
  41. */
  42. public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  43. self::$user_id = $factory->user->create(
  44. array(
  45. 'role' => 'editor',
  46. )
  47. );
  48. self::$post_id = $factory->post->create(
  49. array(
  50. 'post_author' => self::$user_id,
  51. 'post_type' => 'post',
  52. 'post_status' => 'publish',
  53. 'post_title' => 'Test Post',
  54. 'post_content' => '<p>Hello world!</p>',
  55. )
  56. );
  57. self::$block_id = $factory->post->create(
  58. array(
  59. 'post_author' => self::$user_id,
  60. 'post_type' => 'wp_block',
  61. 'post_status' => 'publish',
  62. 'post_title' => 'Test Block',
  63. 'post_content' => '<!-- wp:core/paragraph --><p>Hello world!</p><!-- /wp:core/paragraph -->',
  64. )
  65. );
  66. }
  67. /**
  68. * Delete fake data after tests run.
  69. *
  70. * @since 5.0.0
  71. */
  72. public static function wpTearDownAfterClass() {
  73. wp_delete_post( self::$block_id, true );
  74. wp_delete_post( self::$post_id, true );
  75. self::delete_user( self::$user_id );
  76. }
  77. public function test_render() {
  78. $block_type = WP_Block_Type_Registry::get_instance()->get_registered( 'core/block' );
  79. $output = $block_type->render( array( 'ref' => self::$block_id ) );
  80. $this->assertSame( '<p>Hello world!</p>', $output );
  81. }
  82. /**
  83. * Make sure that a reusable block can be rendered twice in a row.
  84. *
  85. * @ticket 52364
  86. */
  87. public function test_render_subsequent() {
  88. $block_type = WP_Block_Type_Registry::get_instance()->get_registered( 'core/block' );
  89. $output = $block_type->render( array( 'ref' => self::$block_id ) );
  90. $output .= $block_type->render( array( 'ref' => self::$block_id ) );
  91. $this->assertSame( '<p>Hello world!</p><p>Hello world!</p>', $output );
  92. }
  93. /**
  94. * Throw a warning if blocks are recursively nested.
  95. *
  96. * @ticket 52364
  97. */
  98. public function test_recursive_render_warning() {
  99. $recursive_reusable_block = array(
  100. 'ID' => self::$block_id,
  101. 'post_content' => '<!-- wp:block {"ref":' . self::$block_id . '} /-->',
  102. );
  103. wp_update_post( $recursive_reusable_block );
  104. $block_type = WP_Block_Type_Registry::get_instance()->get_registered( 'core/block' );
  105. // The block_render method for `core/block` triggers a user warning if it
  106. // encounters a recursively nested block.
  107. $this->expectException( 'PHPUnit_Framework_Error_Warning' );
  108. $block_type->render( array( 'ref' => self::$block_id ) );
  109. }
  110. public function test_ref_empty() {
  111. $block_type = WP_Block_Type_Registry::get_instance()->get_registered( 'core/block' );
  112. $output = $block_type->render( array() );
  113. $this->assertSame( '', $output );
  114. }
  115. public function test_ref_wrong_post_type() {
  116. $block_type = WP_Block_Type_Registry::get_instance()->get_registered( 'core/block' );
  117. $output = $block_type->render( array( 'ref' => self::$post_id ) );
  118. $this->assertSame( '', $output );
  119. }
  120. }