testcase-block-supports.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. <?php
  2. /**
  3. * Test block supported styles.
  4. *
  5. * @package WordPress
  6. * @subpackage UnitTests
  7. * @since 5.6.0
  8. */
  9. class Block_Supported_Styles_Test extends WP_UnitTestCase {
  10. /**
  11. * Registered block names.
  12. *
  13. * @var string[]
  14. */
  15. private $registered_block_names = array();
  16. /**
  17. * Tear down each test method.
  18. */
  19. public function tearDown() {
  20. while ( ! empty( $this->registered_block_names ) ) {
  21. $block_name = array_pop( $this->registered_block_names );
  22. unregister_block_type( $block_name );
  23. }
  24. parent::tearDown();
  25. }
  26. /**
  27. * Registers a block type.
  28. *
  29. * @param string|WP_Block_Type $name Block type name including namespace, or alternatively a
  30. * complete WP_Block_Type instance. In case a WP_Block_Type
  31. * is provided, the $args parameter will be ignored.
  32. * @param array $args {
  33. * Optional. Array of block type arguments. Any arguments may be defined, however the
  34. * ones described below are supported by default. Default empty array.
  35. *
  36. * @type callable $render_callback Callback used to render blocks of this block type.
  37. * }
  38. */
  39. protected function register_block_type( $name, $args ) {
  40. register_block_type( $name, $args );
  41. $this->registered_block_names[] = $name;
  42. }
  43. /**
  44. * Retrieves attribute such as 'class' or 'style' from the rendered block string.
  45. *
  46. * @param string $attribute Name of attribute to get.
  47. * @param string $block String of rendered block to check.
  48. */
  49. private function get_attribute_from_block( $attribute, $block ) {
  50. $start_index = strpos( $block, $attribute . '="' ) + strlen( $attribute ) + 2;
  51. $split_arr = substr( $block, $start_index );
  52. $end_index = strpos( $split_arr, '"' );
  53. return substr( $split_arr, 0, $end_index );
  54. }
  55. /**
  56. * Retrieves block content from the rendered block string
  57. * (i.e. what's wrapped by the block wrapper `<div />`).
  58. *
  59. * @param string $block String of rendered block to check.
  60. */
  61. private function get_content_from_block( $block ) {
  62. $start_index = strpos( $block, '>' ) + 1; // First occurrence of '>'.
  63. $split_arr = substr( $block, $start_index );
  64. $end_index = strrpos( $split_arr, '<' ); // Last occurrence of '<'.
  65. return substr( $split_arr, 0, $end_index ); // String between first '>' and last '<'.
  66. }
  67. /**
  68. * Block content to test with (i.e. what's wrapped by the block wrapper `<div />`).
  69. *
  70. * @var string
  71. */
  72. const BLOCK_CONTENT = '
  73. <p data-image-description="&lt;p&gt;Test!&lt;/p&gt;">Test</p>
  74. <p>äöü</p>
  75. <p>ß</p>
  76. <p>系の家庭に</p>
  77. <p>Example &lt;p&gt;Test!&lt;/p&gt;</p>
  78. ';
  79. /**
  80. * Returns the rendered output for the current block.
  81. *
  82. * @param array $block Block to render.
  83. *
  84. * @return string Rendered output for the current block.
  85. */
  86. private function render_example_block( $block ) {
  87. WP_Block_Supports::init();
  88. WP_Block_Supports::$block_to_render = $block;
  89. $wrapper_attributes = get_block_wrapper_attributes(
  90. array(
  91. 'class' => 'foo-bar-class',
  92. 'style' => 'test: style;',
  93. )
  94. );
  95. return '<div ' . $wrapper_attributes . '>' . self::BLOCK_CONTENT . '</div>';
  96. }
  97. /**
  98. * Runs assertions that the rendered output has expected class/style attrs.
  99. *
  100. * @param array $block Block to render.
  101. * @param string $expected_classes Expected output class attr string.
  102. * @param string $expected_styles Expected output styles attr string.
  103. */
  104. private function assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ) {
  105. $styled_block = $this->render_example_block( $block );
  106. $class_list = $this->get_attribute_from_block( 'class', $styled_block );
  107. $style_list = $this->get_attribute_from_block( 'style', $styled_block );
  108. $this->assertSame( $expected_classes, $class_list );
  109. $this->assertSame( $expected_styles, $style_list );
  110. }
  111. /**
  112. * Runs assertions that the rendered output has expected content and class/style attrs.
  113. *
  114. * @param array $block Block to render.
  115. * @param string $expected_classes Expected output class attr string.
  116. * @param string $expected_styles Expected output styles attr string.
  117. */
  118. private function assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles ) {
  119. $styled_block = $this->render_example_block( $block );
  120. // Ensure blocks to not add extra whitespace.
  121. $this->assertSame( $styled_block, trim( $styled_block ) );
  122. $content = $this->get_content_from_block( $styled_block );
  123. $class_list = $this->get_attribute_from_block( 'class', $styled_block );
  124. $style_list = $this->get_attribute_from_block( 'style', $styled_block );
  125. $this->assertSame( self::BLOCK_CONTENT, $content );
  126. $this->assertSameSets(
  127. explode( ' ', $expected_classes ),
  128. explode( ' ', $class_list )
  129. );
  130. $this->assertSame(
  131. array_map( 'trim', explode( ';', $expected_styles ) ),
  132. array_map( 'trim', explode( ';', $style_list ) )
  133. );
  134. }
  135. /**
  136. * Tests color support for named color support for named colors.
  137. */
  138. function test_named_color_support() {
  139. $block_type_settings = array(
  140. 'attributes' => array(),
  141. 'supports' => array(
  142. 'color' => true,
  143. ),
  144. 'render_callback' => true,
  145. );
  146. $this->register_block_type( 'core/example', $block_type_settings );
  147. $block = array(
  148. 'blockName' => 'core/example',
  149. 'attrs' => array(
  150. 'textColor' => 'red',
  151. 'backgroundColor' => 'black',
  152. // The following should not be applied (subcatagories of color support).
  153. 'gradient' => 'some-gradient',
  154. ),
  155. 'innerBlock' => array(),
  156. 'innerContent' => array(),
  157. 'innerHTML' => array(),
  158. );
  159. $expected_classes = 'foo-bar-class wp-block-example has-text-color has-red-color has-background has-black-background-color';
  160. $expected_styles = 'test: style;';
  161. $this->assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles );
  162. }
  163. /**
  164. * Tests color support for custom colors.
  165. */
  166. function test_custom_color_support() {
  167. $block_type_settings = array(
  168. 'attributes' => array(),
  169. 'supports' => array(
  170. 'color' => true,
  171. ),
  172. 'render_callback' => true,
  173. );
  174. $this->register_block_type( 'core/example', $block_type_settings );
  175. $block = array(
  176. 'blockName' => 'core/example',
  177. 'attrs' => array(
  178. 'style' => array(
  179. 'color' => array(
  180. 'text' => '#000',
  181. 'background' => '#fff',
  182. // The following should not be applied (subcatagories of color support).
  183. 'gradient' => 'some-gradient',
  184. 'style' => array( 'color' => array( 'link' => '#fff' ) ),
  185. ),
  186. ),
  187. ),
  188. 'innerBlock' => array(),
  189. 'innerContent' => array(),
  190. 'innerHTML' => array(),
  191. );
  192. $expected_styles = 'test: style; color: #000; background-color: #fff;';
  193. $expected_classes = 'foo-bar-class wp-block-example has-text-color has-background';
  194. $this->assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles );
  195. }
  196. /**
  197. * Tests link color support for named colors.
  198. */
  199. function test_named_link_color_support() {
  200. $block_type_settings = array(
  201. 'attributes' => array(),
  202. 'supports' => array(
  203. 'color' => array(
  204. 'link' => true,
  205. ),
  206. ),
  207. 'render_callback' => true,
  208. );
  209. $this->register_block_type( 'core/example', $block_type_settings );
  210. $block = array(
  211. 'blockName' => 'core/example',
  212. 'attrs' => array(
  213. 'style' => array( 'color' => array( 'link' => 'var:preset|color|red' ) ),
  214. ),
  215. 'innerBlock' => array(),
  216. 'innerContent' => array(),
  217. 'innerHTML' => array(),
  218. );
  219. $expected_classes = 'foo-bar-class wp-block-example has-link-color';
  220. $expected_styles = 'test: style; --wp--style--color--link: var(--wp--preset--color--red);';
  221. $this->assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles );
  222. }
  223. /**
  224. * Tests link color support for custom colors.
  225. */
  226. function test_custom_link_color_support() {
  227. $block_type_settings = array(
  228. 'attributes' => array(),
  229. 'supports' => array(
  230. 'color' => array(
  231. 'link' => true,
  232. ),
  233. ),
  234. 'render_callback' => true,
  235. );
  236. $this->register_block_type( 'core/example', $block_type_settings );
  237. $block = array(
  238. 'blockName' => 'core/example',
  239. 'attrs' => array(
  240. 'style' => array( 'color' => array( 'link' => '#fff' ) ),
  241. ),
  242. 'innerBlock' => array(),
  243. 'innerContent' => array(),
  244. 'innerHTML' => array(),
  245. );
  246. $expected_classes = 'foo-bar-class wp-block-example has-link-color';
  247. $expected_styles = 'test: style; --wp--style--color--link: #fff;';
  248. $this->assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles );
  249. }
  250. /**
  251. * Tests gradient color support for named gradients.
  252. */
  253. function test_named_gradient_support() {
  254. $block_type_settings = array(
  255. 'attributes' => array(),
  256. 'supports' => array(
  257. 'color' => array(
  258. 'gradients' => true,
  259. ),
  260. ),
  261. 'render_callback' => true,
  262. );
  263. $this->register_block_type( 'core/example', $block_type_settings );
  264. $block = array(
  265. 'blockName' => 'core/example',
  266. 'attrs' => array(
  267. 'gradient' => 'red',
  268. ),
  269. 'innerBlock' => array(),
  270. 'innerContent' => array(),
  271. 'innerHTML' => array(),
  272. );
  273. $expected_classes = 'foo-bar-class wp-block-example has-background has-red-gradient-background';
  274. $expected_styles = 'test: style;';
  275. $this->assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles );
  276. }
  277. /**
  278. * Tests gradient color support for custom gradients.
  279. */
  280. function test_custom_gradient_support() {
  281. $block_type_settings = array(
  282. 'attributes' => array(),
  283. 'supports' => array(
  284. 'color' => array(
  285. 'gradients' => true,
  286. ),
  287. ),
  288. 'render_callback' => true,
  289. );
  290. $this->register_block_type( 'core/example', $block_type_settings );
  291. $block = array(
  292. 'blockName' => 'core/example',
  293. 'attrs' => array(
  294. 'style' => array( 'color' => array( 'gradient' => 'some-gradient-style' ) ),
  295. ),
  296. 'innerBlock' => array(),
  297. 'innerContent' => array(),
  298. 'innerHTML' => array(),
  299. );
  300. $expected_classes = 'foo-bar-class wp-block-example has-background';
  301. $expected_styles = 'test: style; background: some-gradient-style;';
  302. $this->assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles );
  303. }
  304. /**
  305. * Tests that style attributes for colors are not applied without the support flag.
  306. */
  307. function test_color_unsupported() {
  308. $block_type_settings = array(
  309. 'attributes' => array(),
  310. 'supports' => array(),
  311. 'render_callback' => true,
  312. );
  313. $this->register_block_type( 'core/example', $block_type_settings );
  314. $block = array(
  315. 'blockName' => 'core/example',
  316. 'attrs' => array(
  317. 'textColor' => 'red',
  318. 'backgroundColor' => 'black',
  319. 'style' => array(
  320. 'color' => array(
  321. 'text' => '#000',
  322. 'background' => '#fff',
  323. 'link' => '#ggg',
  324. 'gradient' => 'some-gradient',
  325. ),
  326. ),
  327. ),
  328. 'innerBlock' => array(),
  329. 'innerContent' => array(),
  330. 'innerHTML' => array(),
  331. );
  332. $expected_classes = 'foo-bar-class wp-block-example';
  333. $expected_styles = 'test: style;';
  334. $this->assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles );
  335. }
  336. /**
  337. * Tests support for named font sizes.
  338. */
  339. function test_named_font_size() {
  340. $block_type_settings = array(
  341. 'attributes' => array(),
  342. 'supports' => array(
  343. 'fontSize' => true,
  344. ),
  345. );
  346. $this->register_block_type( 'core/example', $block_type_settings );
  347. $block = array(
  348. 'blockName' => 'core/example',
  349. 'attrs' => array(
  350. 'fontSize' => 'large',
  351. ),
  352. 'innerBlock' => array(),
  353. 'innerContent' => array(),
  354. 'innerHTML' => array(),
  355. );
  356. $expected_classes = 'foo-bar-class wp-block-example has-large-font-size';
  357. $expected_styles = 'test: style;';
  358. $this->assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles );
  359. }
  360. /**
  361. * Tests support for custom font sizes.
  362. */
  363. function test_custom_font_size() {
  364. $block_type_settings = array(
  365. 'attributes' => array(),
  366. 'supports' => array(
  367. 'fontSize' => true,
  368. ),
  369. );
  370. $this->register_block_type( 'core/example', $block_type_settings );
  371. $block = array(
  372. 'blockName' => 'core/example',
  373. 'attrs' => array(
  374. 'style' => array( 'typography' => array( 'fontSize' => '10' ) ),
  375. ),
  376. 'innerBlock' => array(),
  377. 'innerContent' => array(),
  378. 'innerHTML' => array(),
  379. );
  380. $expected_classes = 'foo-bar-class wp-block-example';
  381. $expected_styles = 'test: style; font-size: 10px;';
  382. $this->assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles );
  383. }
  384. /**
  385. * Tests that font size attributes are not applied without support flag.
  386. */
  387. function test_font_size_unsupported() {
  388. $block_type_settings = array(
  389. 'attributes' => array(),
  390. 'supports' => array(),
  391. );
  392. $this->register_block_type( 'core/example', $block_type_settings );
  393. $block = array(
  394. 'blockName' => 'core/example',
  395. 'attrs' => array(
  396. 'fontSize' => 'large',
  397. 'style' => array( 'typography' => array( 'fontSize' => '10' ) ),
  398. ),
  399. 'innerBlock' => array(),
  400. 'innerContent' => array(),
  401. 'innerHTML' => array(),
  402. );
  403. $expected_classes = 'foo-bar-class wp-block-example';
  404. $expected_styles = 'test: style;';
  405. $this->assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles );
  406. }
  407. /**
  408. * Tests line height support.
  409. */
  410. function test_line_height() {
  411. $block_type_settings = array(
  412. 'attributes' => array(),
  413. 'supports' => array(
  414. 'lineHeight' => true,
  415. ),
  416. );
  417. $this->register_block_type( 'core/example', $block_type_settings );
  418. $block = array(
  419. 'blockName' => 'core/example',
  420. 'attrs' => array(
  421. 'style' => array( 'typography' => array( 'lineHeight' => '10' ) ),
  422. ),
  423. 'innerBlock' => array(),
  424. 'innerContent' => array(),
  425. 'innerHTML' => array(),
  426. );
  427. $expected_classes = 'foo-bar-class wp-block-example';
  428. $expected_styles = 'test: style; line-height: 10;';
  429. $this->assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles );
  430. }
  431. /**
  432. * Tests line height not applied without support flag.
  433. */
  434. function test_line_height_unsupported() {
  435. $block_type_settings = array(
  436. 'attributes' => array(),
  437. 'supports' => array(),
  438. );
  439. $this->register_block_type( 'core/example', $block_type_settings );
  440. $block = array(
  441. 'blockName' => 'core/example',
  442. 'attrs' => array(
  443. 'style' => array( 'typography' => array( 'lineHeight' => '10' ) ),
  444. ),
  445. 'innerBlock' => array(),
  446. 'innerContent' => array(),
  447. 'innerHTML' => array(),
  448. );
  449. $expected_classes = 'foo-bar-class wp-block-example';
  450. $expected_styles = 'test: style;';
  451. $this->assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles );
  452. }
  453. /**
  454. * Tests support for block alignment.
  455. */
  456. function test_block_alignment() {
  457. $block_type_settings = array(
  458. 'attributes' => array(),
  459. 'supports' => array(
  460. 'align' => true,
  461. ),
  462. );
  463. $this->register_block_type( 'core/example', $block_type_settings );
  464. $block = array(
  465. 'blockName' => 'core/example',
  466. 'attrs' => array(
  467. 'align' => 'wide',
  468. ),
  469. 'innerBlock' => array(),
  470. 'innerContent' => array(),
  471. 'innerHTML' => array(),
  472. );
  473. $expected_classes = 'foo-bar-class wp-block-example alignwide';
  474. $expected_styles = 'test: style;';
  475. $this->assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles );
  476. }
  477. /**
  478. * Tests block alignment requires support to be added.
  479. */
  480. function test_block_alignment_unsupported() {
  481. $block_type_settings = array(
  482. 'attributes' => array(),
  483. 'supports' => array(),
  484. );
  485. $this->register_block_type( 'core/example', $block_type_settings );
  486. $block = array(
  487. 'blockName' => 'core/example',
  488. 'attrs' => array(
  489. 'align' => 'wide',
  490. ),
  491. 'innerBlock' => array(),
  492. 'innerContent' => array(),
  493. 'innerHTML' => array(),
  494. );
  495. $expected_classes = 'foo-bar-class wp-block-example';
  496. $expected_styles = 'test: style;';
  497. $this->assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles );
  498. }
  499. /**
  500. * Tests all support flags together to ensure they work together as expected.
  501. */
  502. function test_all_supported() {
  503. $block_type_settings = array(
  504. 'attributes' => array(),
  505. 'supports' => array(
  506. 'color' => array(
  507. 'gradients' => true,
  508. 'link' => true,
  509. ),
  510. 'fontSize' => true,
  511. 'lineHeight' => true,
  512. 'align' => true,
  513. ),
  514. );
  515. $this->register_block_type( 'core/example', $block_type_settings );
  516. $block = array(
  517. 'blockName' => 'core/example',
  518. 'attrs' => array(
  519. 'align' => 'wide',
  520. 'style' => array(
  521. 'color' => array(
  522. 'text' => '#000',
  523. 'background' => '#fff',
  524. 'style' => array( 'color' => array( 'link' => '#fff' ) ),
  525. ),
  526. 'typography' => array(
  527. 'lineHeight' => '20',
  528. 'fontSize' => '10',
  529. ),
  530. ),
  531. ),
  532. 'innerBlock' => array(),
  533. 'innerContent' => array(),
  534. 'innerHTML' => array(),
  535. );
  536. $expected_classes = 'foo-bar-class wp-block-example has-text-color has-background alignwide';
  537. $expected_styles = 'test: style; color: #000; background-color: #fff; font-size: 10px; line-height: 20;';
  538. $this->assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles );
  539. }
  540. /**
  541. * Tests that only styles for the supported flag are added.
  542. * Verify one support enabled does not imply multiple supports enabled.
  543. */
  544. function test_one_supported() {
  545. $block_type_settings = array(
  546. 'attributes' => array(),
  547. 'supports' => array(
  548. 'fontSize' => true,
  549. ),
  550. );
  551. $this->register_block_type( 'core/example', $block_type_settings );
  552. $block = array(
  553. 'blockName' => 'core/example',
  554. 'attrs' => array(
  555. 'align' => 'wide',
  556. 'style' => array(
  557. 'color' => array(
  558. 'text' => '#000',
  559. 'background' => '#fff',
  560. 'gradient' => 'some-gradient',
  561. 'style' => array( 'color' => array( 'link' => '#fff' ) ),
  562. ),
  563. 'typography' => array(
  564. 'lineHeight' => '20',
  565. 'fontSize' => '10',
  566. ),
  567. ),
  568. ),
  569. 'innerBlock' => array(),
  570. 'innerContent' => array(),
  571. 'innerHTML' => array(),
  572. );
  573. $expected_classes = 'foo-bar-class wp-block-example';
  574. $expected_styles = 'test: style; font-size: 10px;';
  575. $this->assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles );
  576. }
  577. /**
  578. * Tests custom classname server-side block support.
  579. */
  580. function test_custom_classnames_support() {
  581. $block_type_settings = array(
  582. 'attributes' => array(),
  583. 'supports' => array(),
  584. );
  585. $this->register_block_type( 'core/example', $block_type_settings );
  586. $block = array(
  587. 'blockName' => 'core/example',
  588. 'attrs' => array(
  589. 'className' => 'my-custom-classname',
  590. ),
  591. 'innerBlock' => array(),
  592. 'innerContent' => array(),
  593. 'innerHTML' => array(),
  594. );
  595. $expected_styles = 'test: style;';
  596. $expected_classes = 'foo-bar-class wp-block-example my-custom-classname';
  597. $this->assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles );
  598. }
  599. /**
  600. * Tests custom classname server-side block support opt-out.
  601. */
  602. function test_custom_classnames_support_opt_out() {
  603. $block_type_settings = array(
  604. 'attributes' => array(),
  605. 'supports' => array(
  606. 'customClassName' => false,
  607. ),
  608. );
  609. $this->register_block_type( 'core/example', $block_type_settings );
  610. $block = array(
  611. 'blockName' => 'core/example',
  612. 'attrs' => array(
  613. 'className' => 'my-custom-classname',
  614. ),
  615. 'innerBlock' => array(),
  616. 'innerContent' => array(),
  617. 'innerHTML' => array(),
  618. );
  619. $expected_styles = 'test: style;';
  620. $expected_classes = 'foo-bar-class wp-block-example';
  621. $this->assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles );
  622. }
  623. /**
  624. * Tests generated classname server-side block support opt-out.
  625. */
  626. function test_generatted_classnames_support_opt_out() {
  627. $block_type_settings = array(
  628. 'attributes' => array(),
  629. 'supports' => array(
  630. 'className' => false,
  631. ),
  632. );
  633. $this->register_block_type( 'core/example', $block_type_settings );
  634. $block = array(
  635. 'blockName' => 'core/example',
  636. 'attrs' => array(),
  637. 'innerBlock' => array(),
  638. 'innerContent' => array(),
  639. 'innerHTML' => array(),
  640. );
  641. $expected_styles = 'test: style;';
  642. $expected_classes = 'foo-bar-class';
  643. $this->assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles );
  644. }
  645. /**
  646. * Ensures libxml_internal_errors is being used instead of @ warning suppression
  647. */
  648. public function test_render_block_suppresses_warnings_without_at_suppression() {
  649. $block_type_settings = array(
  650. 'attributes' => array(),
  651. 'supports' => array(),
  652. );
  653. $this->register_block_type( 'core/example', $block_type_settings );
  654. $block = array(
  655. 'blockName' => 'core/example',
  656. 'attrs' => array(),
  657. 'innerBlock' => array(),
  658. 'innerContent' => array(),
  659. 'innerHTML' => array(),
  660. );
  661. // Custom error handler's see Warnings even if they are suppressed by the @ symbol.
  662. $errors = array();
  663. set_error_handler(
  664. function ( $errno = 0, $errstr = '' ) use ( &$errors ) {
  665. $errors[] = $errstr;
  666. return false;
  667. }
  668. );
  669. // HTML5 elements like <time> are not supported by the DOMDocument parser used by the block supports feature.
  670. // This specific example is emitted by the "Display post date" setting in the latest-posts block.
  671. apply_filters( 'render_block', '<div><time datetime="2020-06-18T04:01:43+10:00" class="wp-block-latest-posts__post-date">June 18, 2020</time></div>', $block );
  672. restore_error_handler();
  673. $this->assertEmpty( $errors, 'Libxml errors should be dropped.' );
  674. }
  675. }