block.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?php
  2. /**
  3. * WP_Block Tests
  4. *
  5. * @package WordPress
  6. * @subpackage Blocks
  7. * @since 5.5.0
  8. */
  9. /**
  10. * Tests for WP_Block
  11. *
  12. * @since 5.5.0
  13. *
  14. * @group blocks
  15. */
  16. class WP_Block_Test extends WP_UnitTestCase {
  17. /**
  18. * Fake block type registry.
  19. *
  20. * @var WP_Block_Type_Registry
  21. */
  22. private $registry = null;
  23. /**
  24. * Set up each test method.
  25. */
  26. public function setUp() {
  27. parent::setUp();
  28. $this->registry = new WP_Block_Type_Registry();
  29. }
  30. /**
  31. * Tear down each test method.
  32. */
  33. public function tearDown() {
  34. $this->registry = null;
  35. parent::tearDown();
  36. }
  37. function filter_render_block( $content, $parsed_block ) {
  38. return 'Original: "' . $content . '", from block "' . $parsed_block['blockName'] . '"';
  39. }
  40. /**
  41. * @ticket 49927
  42. */
  43. function test_constructor_assigns_properties_from_parsed_block() {
  44. $this->registry->register( 'core/example', array() );
  45. $parsed_blocks = parse_blocks( '<!-- wp:example {"ok":true} -->a<!-- wp:example /-->b<!-- /wp:example -->' );
  46. $parsed_block = $parsed_blocks[0];
  47. $context = array();
  48. $block = new WP_Block( $parsed_block, $context, $this->registry );
  49. $this->assertSame( $parsed_block, $block->parsed_block );
  50. $this->assertSame( $parsed_block['blockName'], $block->name );
  51. $this->assertSame( $parsed_block['attrs'], $block->attributes );
  52. $this->assertSame( $parsed_block['innerContent'], $block->inner_content );
  53. $this->assertSame( $parsed_block['innerHTML'], $block->inner_html );
  54. }
  55. /**
  56. * @ticket 49927
  57. */
  58. function test_constructor_assigns_block_type_from_registry() {
  59. $block_type_settings = array(
  60. 'attributes' => array(
  61. 'defaulted' => array(
  62. 'type' => 'number',
  63. 'default' => 10,
  64. ),
  65. ),
  66. );
  67. $this->registry->register( 'core/example', $block_type_settings );
  68. $parsed_block = array( 'blockName' => 'core/example' );
  69. $context = array();
  70. $block = new WP_Block( $parsed_block, $context, $this->registry );
  71. $this->assertInstanceOf( WP_Block_Type::class, $block->block_type );
  72. $this->assertSame(
  73. $block_type_settings['attributes'],
  74. $block->block_type->attributes
  75. );
  76. }
  77. /**
  78. * @ticket 49927
  79. */
  80. function test_lazily_assigns_attributes_with_defaults() {
  81. $this->registry->register(
  82. 'core/example',
  83. array(
  84. 'attributes' => array(
  85. 'defaulted' => array(
  86. 'type' => 'number',
  87. 'default' => 10,
  88. ),
  89. ),
  90. )
  91. );
  92. $parsed_block = array(
  93. 'blockName' => 'core/example',
  94. 'attrs' => array(
  95. 'explicit' => 20,
  96. ),
  97. );
  98. $context = array();
  99. $block = new WP_Block( $parsed_block, $context, $this->registry );
  100. $this->assertSame(
  101. array(
  102. 'explicit' => 20,
  103. 'defaulted' => 10,
  104. ),
  105. $block->attributes
  106. );
  107. }
  108. /**
  109. * @ticket 49927
  110. */
  111. function test_lazily_assigns_attributes_with_only_defaults() {
  112. $this->registry->register(
  113. 'core/example',
  114. array(
  115. 'attributes' => array(
  116. 'defaulted' => array(
  117. 'type' => 'number',
  118. 'default' => 10,
  119. ),
  120. ),
  121. )
  122. );
  123. $parsed_block = array(
  124. 'blockName' => 'core/example',
  125. 'attrs' => array(),
  126. );
  127. $context = array();
  128. $block = new WP_Block( $parsed_block, $context, $this->registry );
  129. $this->assertSame( array( 'defaulted' => 10 ), $block->attributes );
  130. // Intentionally call a second time, to ensure property was assigned.
  131. $this->assertSame( array( 'defaulted' => 10 ), $block->attributes );
  132. }
  133. /**
  134. * @ticket 49927
  135. */
  136. function test_constructor_assigns_context_from_block_type() {
  137. $this->registry->register(
  138. 'core/example',
  139. array(
  140. 'uses_context' => array( 'requested' ),
  141. )
  142. );
  143. $parsed_block = array( 'blockName' => 'core/example' );
  144. $context = array(
  145. 'requested' => 'included',
  146. 'unrequested' => 'not included',
  147. );
  148. $block = new WP_Block( $parsed_block, $context, $this->registry );
  149. $this->assertSame( array( 'requested' => 'included' ), $block->context );
  150. }
  151. /**
  152. * @ticket 49927
  153. */
  154. function test_constructor_maps_inner_blocks() {
  155. $this->registry->register( 'core/example', array() );
  156. $parsed_blocks = parse_blocks( '<!-- wp:example {"ok":true} -->a<!-- wp:example /-->b<!-- /wp:example -->' );
  157. $parsed_block = $parsed_blocks[0];
  158. $context = array();
  159. $block = new WP_Block( $parsed_block, $context, $this->registry );
  160. $this->assertCount( 1, $block->inner_blocks );
  161. $this->assertInstanceOf( WP_Block::class, $block->inner_blocks[0] );
  162. $this->assertSame( 'core/example', $block->inner_blocks[0]->name );
  163. }
  164. /**
  165. * @ticket 49927
  166. */
  167. function test_constructor_prepares_context_for_inner_blocks() {
  168. $this->registry->register(
  169. 'core/outer',
  170. array(
  171. 'attributes' => array(
  172. 'recordId' => array(
  173. 'type' => 'number',
  174. ),
  175. ),
  176. 'provides_context' => array(
  177. 'core/recordId' => 'recordId',
  178. ),
  179. )
  180. );
  181. $this->registry->register(
  182. 'core/inner',
  183. array(
  184. 'uses_context' => array( 'core/recordId' ),
  185. )
  186. );
  187. $parsed_blocks = parse_blocks( '<!-- wp:outer {"recordId":10} --><!-- wp:inner /--><!-- /wp:outer -->' );
  188. $parsed_block = $parsed_blocks[0];
  189. $context = array( 'unrequested' => 'not included' );
  190. $block = new WP_Block( $parsed_block, $context, $this->registry );
  191. $this->assertCount( 0, $block->context );
  192. $this->assertSame(
  193. array( 'core/recordId' => 10 ),
  194. $block->inner_blocks[0]->context
  195. );
  196. }
  197. /**
  198. * @ticket 49927
  199. */
  200. function test_constructor_assigns_merged_context() {
  201. $this->registry->register(
  202. 'core/example',
  203. array(
  204. 'attributes' => array(
  205. 'value' => array(
  206. 'type' => array( 'string', 'null' ),
  207. ),
  208. ),
  209. 'provides_context' => array(
  210. 'core/value' => 'value',
  211. ),
  212. 'uses_context' => array( 'core/value' ),
  213. )
  214. );
  215. $parsed_blocks = parse_blocks(
  216. '<!-- wp:example {"value":"merged"} -->' .
  217. '<!-- wp:example {"value":null} -->' .
  218. '<!-- wp:example /-->' .
  219. '<!-- /wp:example -->' .
  220. '<!-- /wp:example -->'
  221. );
  222. $parsed_block = $parsed_blocks[0];
  223. $context = array( 'core/value' => 'original' );
  224. $block = new WP_Block( $parsed_block, $context, $this->registry );
  225. $this->assertSame(
  226. array( 'core/value' => 'original' ),
  227. $block->context
  228. );
  229. $this->assertSame(
  230. array( 'core/value' => 'merged' ),
  231. $block->inner_blocks[0]->context
  232. );
  233. $this->assertSame(
  234. array( 'core/value' => null ),
  235. $block->inner_blocks[0]->inner_blocks[0]->context
  236. );
  237. }
  238. /**
  239. * @ticket 49927
  240. */
  241. function test_render_static_block_type_returns_own_content() {
  242. $this->registry->register( 'core/static', array() );
  243. $this->registry->register(
  244. 'core/dynamic',
  245. array(
  246. 'render_callback' => function() {
  247. return 'b';
  248. },
  249. )
  250. );
  251. $parsed_blocks = parse_blocks( '<!-- wp:static -->a<!-- wp:dynamic /-->c<!-- /wp:static -->' );
  252. $parsed_block = $parsed_blocks[0];
  253. $context = array();
  254. $block = new WP_Block( $parsed_block, $context, $this->registry );
  255. $this->assertSame( 'abc', $block->render() );
  256. }
  257. /**
  258. * @ticket 49927
  259. */
  260. function test_render_passes_block_for_render_callback() {
  261. $this->registry->register(
  262. 'core/greeting',
  263. array(
  264. 'render_callback' => function( $attributes, $content, $block ) {
  265. return sprintf( 'Hello from %s', $block->name );
  266. },
  267. )
  268. );
  269. $parsed_blocks = parse_blocks( '<!-- wp:greeting /-->' );
  270. $parsed_block = $parsed_blocks[0];
  271. $context = array();
  272. $block = new WP_Block( $parsed_block, $context, $this->registry );
  273. $this->assertSame( 'Hello from core/greeting', $block->render() );
  274. }
  275. /**
  276. * @ticket 49927
  277. */
  278. function test_render_applies_render_block_filter() {
  279. $this->registry->register( 'core/example', array() );
  280. add_filter( 'render_block', array( $this, 'filter_render_block' ), 10, 2 );
  281. $parsed_blocks = parse_blocks( '<!-- wp:example -->Static<!-- wp:example -->Inner<!-- /wp:example --><!-- /wp:example -->' );
  282. $parsed_block = $parsed_blocks[0];
  283. $context = array();
  284. $block = new WP_Block( $parsed_block, $context, $this->registry );
  285. $rendered_content = $block->render();
  286. remove_filter( 'render_block', array( $this, 'filter_render_block' ) );
  287. $this->assertSame( 'Original: "StaticOriginal: "Inner", from block "core/example"", from block "core/example"', $rendered_content );
  288. }
  289. /**
  290. * @ticket 46187
  291. */
  292. function test_render_applies_dynamic_render_block_filter() {
  293. $this->registry->register( 'core/example', array() );
  294. add_filter( 'render_block_core/example', array( $this, 'filter_render_block' ), 10, 2 );
  295. $parsed_blocks = parse_blocks( '<!-- wp:example -->Static<!-- wp:example -->Inner<!-- /wp:example --><!-- /wp:example -->' );
  296. $parsed_block = $parsed_blocks[0];
  297. $context = array();
  298. $block = new WP_Block( $parsed_block, $context, $this->registry );
  299. $rendered_content = $block->render();
  300. remove_filter( 'render_block_core/example', array( $this, 'filter_render_block' ) );
  301. $this->assertSame( 'Original: "StaticOriginal: "Inner", from block "core/example"", from block "core/example"', $rendered_content );
  302. }
  303. /**
  304. * @ticket 49927
  305. */
  306. function test_passes_attributes_to_render_callback() {
  307. $this->registry->register(
  308. 'core/greeting',
  309. array(
  310. 'attributes' => array(
  311. 'toWhom' => array(
  312. 'type' => 'string',
  313. ),
  314. 'punctuation' => array(
  315. 'type' => 'string',
  316. 'default' => '!',
  317. ),
  318. ),
  319. 'render_callback' => function( $block_attributes ) {
  320. return sprintf(
  321. 'Hello %s%s',
  322. $block_attributes['toWhom'],
  323. $block_attributes['punctuation']
  324. );
  325. },
  326. )
  327. );
  328. $parsed_blocks = parse_blocks( '<!-- wp:greeting {"toWhom":"world"} /-->' );
  329. $parsed_block = $parsed_blocks[0];
  330. $context = array();
  331. $block = new WP_Block( $parsed_block, $context, $this->registry );
  332. $this->assertSame( 'Hello world!', $block->render() );
  333. }
  334. /**
  335. * @ticket 49927
  336. */
  337. function test_passes_content_to_render_callback() {
  338. $this->registry->register(
  339. 'core/outer',
  340. array(
  341. 'render_callback' => function( $block_attributes, $content ) {
  342. return $content;
  343. },
  344. )
  345. );
  346. $this->registry->register(
  347. 'core/inner',
  348. array(
  349. 'render_callback' => function() {
  350. return 'b';
  351. },
  352. )
  353. );
  354. $parsed_blocks = parse_blocks( '<!-- wp:outer -->a<!-- wp:inner /-->c<!-- /wp:outer -->' );
  355. $parsed_block = $parsed_blocks[0];
  356. $context = array();
  357. $block = new WP_Block( $parsed_block, $context, $this->registry );
  358. $this->assertSame( 'abc', $block->render() );
  359. }
  360. }