Changeset 51490
- Timestamp:
- 07/26/2021 05:39:53 PM (3 years ago)
- Location:
- trunk/tests/phpunit
- Files:
-
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/includes/bootstrap.php
r51333 r51490 204 204 require __DIR__ . '/testcase-xmlrpc.php'; 205 205 require __DIR__ . '/testcase-ajax.php'; 206 require __DIR__ . '/testcase-block-supports.php';207 206 require __DIR__ . '/testcase-canonical.php'; 208 207 require __DIR__ . '/testcase-xml.php'; -
trunk/tests/phpunit/tests/blocks/block-supported-styles.php
r51489 r51490 1 1 <?php 2 /** 3 * Block Tests 4 * 5 * @package WordPress 6 * @subpackage Blocks 7 * @since 5.6.0 8 */ 9 2 10 /** 3 11 * Test block supported styles. 4 12 * 5 * @ package WordPress6 * @subpackage UnitTests7 * @ since 5.6.013 * @since 5.6.0 14 * 15 * @group blocks 8 16 */ 9 17 class Block_Supported_Styles_Test extends WP_UnitTestCase { 10 11 /**12 * Registered block names.13 *14 * @var string[]15 */16 private $registered_block_names = array();17 18 /**19 * Tear down each test method.20 */21 public function tearDown() {22 while ( ! empty( $this->registered_block_names ) ) {23 $block_name = array_pop( $this->registered_block_names );24 unregister_block_type( $block_name );25 }26 27 parent::tearDown();28 }29 30 /**31 * Registers a block type.32 *33 * @param string|WP_Block_Type $name Block type name including namespace, or alternatively a34 * complete WP_Block_Type instance. In case a WP_Block_Type35 * is provided, the $args parameter will be ignored.36 * @param array $args {37 * Optional. Array of block type arguments. Any arguments may be defined, however the38 * ones described below are supported by default. Default empty array.39 *40 * @type callable $render_callback Callback used to render blocks of this block type.41 * }42 */43 protected function register_block_type( $name, $args ) {44 register_block_type( $name, $args );45 46 $this->registered_block_names[] = $name;47 }48 49 /**50 * Retrieves attribute such as 'class' or 'style' from the rendered block string.51 *52 * @param string $attribute Name of attribute to get.53 * @param string $block String of rendered block to check.54 */55 private function get_attribute_from_block( $attribute, $block ) {56 $start_index = strpos( $block, $attribute . '="' ) + strlen( $attribute ) + 2;57 $split_arr = substr( $block, $start_index );58 $end_index = strpos( $split_arr, '"' );59 return substr( $split_arr, 0, $end_index );60 }61 62 /**63 * Retrieves block content from the rendered block string64 * (i.e. what's wrapped by the block wrapper `<div />`).65 *66 * @param string $block String of rendered block to check.67 */68 private function get_content_from_block( $block ) {69 $start_index = strpos( $block, '>' ) + 1; // First occurrence of '>'.70 $split_arr = substr( $block, $start_index );71 $end_index = strrpos( $split_arr, '<' ); // Last occurrence of '<'.72 return substr( $split_arr, 0, $end_index ); // String between first '>' and last '<'.73 }74 18 75 19 /** … … 87 31 88 32 /** 33 * Registered block names. 34 * 35 * @var string[] 36 */ 37 private $registered_block_names = array(); 38 39 /** 40 * Tear down each test method. 41 */ 42 public function tearDown() { 43 while ( ! empty( $this->registered_block_names ) ) { 44 $block_name = array_pop( $this->registered_block_names ); 45 unregister_block_type( $block_name ); 46 } 47 48 parent::tearDown(); 49 } 50 51 /** 52 * Registers a block type. 53 * 54 * @param string|WP_Block_Type $name Block type name including namespace, or alternatively a 55 * complete WP_Block_Type instance. In case a WP_Block_Type 56 * is provided, the $args parameter will be ignored. 57 * @param array $args { 58 * Optional. Array of block type arguments. Any arguments may be defined, however the 59 * ones described below are supported by default. Default empty array. 60 * 61 * @type callable $render_callback Callback used to render blocks of this block type. 62 * } 63 */ 64 protected function register_block_type( $name, $args ) { 65 register_block_type( $name, $args ); 66 67 $this->registered_block_names[] = $name; 68 } 69 70 /** 71 * Retrieves attribute such as 'class' or 'style' from the rendered block string. 72 * 73 * @param string $attribute Name of attribute to get. 74 * @param string $block String of rendered block to check. 75 */ 76 private function get_attribute_from_block( $attribute, $block ) { 77 $start_index = strpos( $block, $attribute . '="' ) + strlen( $attribute ) + 2; 78 $split_arr = substr( $block, $start_index ); 79 $end_index = strpos( $split_arr, '"' ); 80 return substr( $split_arr, 0, $end_index ); 81 } 82 83 /** 84 * Retrieves block content from the rendered block string 85 * (i.e. what's wrapped by the block wrapper `<div />`). 86 * 87 * @param string $block String of rendered block to check. 88 */ 89 private function get_content_from_block( $block ) { 90 $start_index = strpos( $block, '>' ) + 1; // First occurrence of '>'. 91 $split_arr = substr( $block, $start_index ); 92 $end_index = strrpos( $split_arr, '<' ); // Last occurrence of '<'. 93 return substr( $split_arr, 0, $end_index ); // String between first '>' and last '<'. 94 } 95 96 /** 89 97 * Returns the rendered output for the current block. 90 98 * 91 99 * @param array $block Block to render. 92 *93 100 * @return string Rendered output for the current block. 94 101 */ … … 108 115 * Runs assertions that the rendered output has expected class/style attrs. 109 116 * 110 * @param array $block Block to render.117 * @param array $block Block to render. 111 118 * @param string $expected_classes Expected output class attr string. 112 * @param string $expected_styles Expected output styles attr string.119 * @param string $expected_styles Expected output styles attr string. 113 120 */ 114 121 private function assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ) { … … 117 124 $style_list = $this->get_attribute_from_block( 'style', $styled_block ); 118 125 119 $this->assertSame( $expected_classes, $class_list );120 $this->assertSame( $expected_styles, $style_list );126 $this->assertSame( $expected_classes, $class_list, 'Class list does not match expected classes' ); 127 $this->assertSame( $expected_styles, $style_list, 'Style list does not match expected styles' ); 121 128 } 122 129 … … 124 131 * Runs assertions that the rendered output has expected content and class/style attrs. 125 132 * 126 * @param array $block Block to render.133 * @param array $block Block to render. 127 134 * @param string $expected_classes Expected output class attr string. 128 * @param string $expected_styles Expected output styles attr string.135 * @param string $expected_styles Expected output styles attr string. 129 136 */ 130 137 private function assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles ) { … … 138 145 $style_list = $this->get_attribute_from_block( 'style', $styled_block ); 139 146 140 $this->assertSame( self::BLOCK_CONTENT, $content );147 $this->assertSame( self::BLOCK_CONTENT, $content, 'Block content does not match expected content' ); 141 148 $this->assertSameSets( 142 149 explode( ' ', $expected_classes ), 143 explode( ' ', $class_list ) 150 explode( ' ', $class_list ), 151 'Class list does not match expected classes' 144 152 ); 145 153 $this->assertSame( 146 154 array_map( 'trim', explode( ';', $expected_styles ) ), 147 array_map( 'trim', explode( ';', $style_list ) ) 155 array_map( 'trim', explode( ';', $style_list ) ), 156 'Style list does not match expected styles' 148 157 ); 149 158 } … … 152 161 * Tests color support for named color support for named colors. 153 162 */ 154 function test_named_color_support() {163 public function test_named_color_support() { 155 164 $block_type_settings = array( 156 165 'attributes' => array(), … … 184 193 * Tests color support for custom colors. 185 194 */ 186 function test_custom_color_support() {195 public function test_custom_color_support() { 187 196 $block_type_settings = array( 188 197 'attributes' => array(), … … 219 228 220 229 /** 221 * Tests link color support for named colors.222 */223 function test_named_link_color_support() {224 $block_type_settings = array(225 'attributes' => array(),226 'supports' => array(227 'color' => array(228 'link' => true,229 ),230 ),231 'render_callback' => true,232 );233 $this->register_block_type( 'core/example', $block_type_settings );234 235 $block = array(236 'blockName' => 'core/example',237 'attrs' => array(238 'style' => array( 'color' => array( 'link' => 'var:preset|color|red' ) ),239 ),240 'innerBlock' => array(),241 'innerContent' => array(),242 'innerHTML' => array(),243 );244 245 $expected_classes = 'foo-bar-class wp-block-example has-link-color';246 $expected_styles = 'test: style; --wp--style--color--link: var(--wp--preset--color--red);';247 248 $this->assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles );249 }250 251 /**252 * Tests link color support for custom colors.253 */254 function test_custom_link_color_support() {255 $block_type_settings = array(256 'attributes' => array(),257 'supports' => array(258 'color' => array(259 'link' => true,260 ),261 ),262 'render_callback' => true,263 );264 $this->register_block_type( 'core/example', $block_type_settings );265 266 $block = array(267 'blockName' => 'core/example',268 'attrs' => array(269 'style' => array( 'color' => array( 'link' => '#fff' ) ),270 ),271 'innerBlock' => array(),272 'innerContent' => array(),273 'innerHTML' => array(),274 );275 276 $expected_classes = 'foo-bar-class wp-block-example has-link-color';277 $expected_styles = 'test: style; --wp--style--color--link: #fff;';278 279 $this->assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles );280 }281 282 /**283 230 * Tests gradient color support for named gradients. 284 231 */ 285 function test_named_gradient_support() {232 public function test_named_gradient_support() { 286 233 $block_type_settings = array( 287 234 'attributes' => array(), … … 314 261 * Tests gradient color support for custom gradients. 315 262 */ 316 function test_custom_gradient_support() {263 public function test_custom_gradient_support() { 317 264 $block_type_settings = array( 318 265 'attributes' => array(), … … 345 292 * Tests that style attributes for colors are not applied without the support flag. 346 293 */ 347 function test_color_unsupported() {294 public function test_color_unsupported() { 348 295 $block_type_settings = array( 349 296 'attributes' => array(), … … 381 328 * Tests support for named font sizes. 382 329 */ 383 function test_named_font_size() {330 public function test_named_font_size() { 384 331 $block_type_settings = array( 385 332 'attributes' => array(), 386 333 'supports' => array( 387 'fontSize' => true, 334 'typography' => array( 335 'fontSize' => true, 336 ), 388 337 ), 389 338 ); … … 409 358 * Tests support for custom font sizes. 410 359 */ 411 function test_custom_font_size() {360 public function test_custom_font_size() { 412 361 $block_type_settings = array( 413 362 'attributes' => array(), 414 363 'supports' => array( 415 'fontSize' => true, 416 ), 417 ); 418 $this->register_block_type( 'core/example', $block_type_settings ); 419 420 $block = array( 421 'blockName' => 'core/example', 422 'attrs' => array( 423 'style' => array( 'typography' => array( 'fontSize' => '10' ) ), 364 'typography' => array( 365 'fontSize' => true, 366 ), 367 ), 368 ); 369 $this->register_block_type( 'core/example', $block_type_settings ); 370 371 $block = array( 372 'blockName' => 'core/example', 373 'attrs' => array( 374 'style' => array( 'typography' => array( 'fontSize' => '10px' ) ), 424 375 ), 425 376 'innerBlock' => array(), … … 437 388 * Tests that font size attributes are not applied without support flag. 438 389 */ 439 function test_font_size_unsupported() {390 public function test_font_size_unsupported() { 440 391 $block_type_settings = array( 441 392 'attributes' => array(), … … 464 415 * Tests line height support. 465 416 */ 466 function test_line_height() {417 public function test_line_height() { 467 418 $block_type_settings = array( 468 419 'attributes' => array(), 469 420 'supports' => array( 470 'lineHeight' => true, 421 'typography' => array( 422 'lineHeight' => true, 423 ), 471 424 ), 472 425 ); … … 492 445 * Tests line height not applied without support flag. 493 446 */ 494 function test_line_height_unsupported() {447 public function test_line_height_unsupported() { 495 448 $block_type_settings = array( 496 449 'attributes' => array(), … … 518 471 * Tests support for block alignment. 519 472 */ 520 function test_block_alignment() {473 public function test_block_alignment() { 521 474 $block_type_settings = array( 522 475 'attributes' => array(), … … 546 499 * Tests block alignment requires support to be added. 547 500 */ 548 function test_block_alignment_unsupported() {501 public function test_block_alignment_unsupported() { 549 502 $block_type_settings = array( 550 503 'attributes' => array(), … … 572 525 * Tests all support flags together to ensure they work together as expected. 573 526 */ 574 function test_all_supported() {527 public function test_all_supported() { 575 528 $block_type_settings = array( 576 529 'attributes' => array(), … … 580 533 'link' => true, 581 534 ), 582 'fontSize' => true, 583 'lineHeight' => true, 535 'typography' => array( 536 'fontSize' => true, 537 'lineHeight' => true, 538 ), 584 539 'align' => true, 585 540 ), … … 599 554 'typography' => array( 600 555 'lineHeight' => '20', 601 'fontSize' => '10 ',556 'fontSize' => '10px', 602 557 ), 603 558 ), … … 618 573 * Verify one support enabled does not imply multiple supports enabled. 619 574 */ 620 function test_one_supported() {575 public function test_one_supported() { 621 576 $block_type_settings = array( 622 577 'attributes' => array(), 623 578 'supports' => array( 624 'fontSize' => true, 579 'typography' => array( 580 'fontSize' => true, 581 ), 625 582 ), 626 583 ); … … 640 597 'typography' => array( 641 598 'lineHeight' => '20', 642 'fontSize' => '10 ',599 'fontSize' => '10px', 643 600 ), 644 601 ), … … 658 615 * Tests custom classname server-side block support. 659 616 */ 660 function test_custom_classnames_support() {617 public function test_custom_classnames_support() { 661 618 $block_type_settings = array( 662 619 'attributes' => array(), … … 684 641 * Tests custom classname server-side block support opt-out. 685 642 */ 686 function test_custom_classnames_support_opt_out() {643 public function test_custom_classnames_support_opt_out() { 687 644 $block_type_settings = array( 688 645 'attributes' => array(), … … 712 669 * Tests generated classname server-side block support opt-out. 713 670 */ 714 function test_generatted_classnames_support_opt_out() {671 public function test_generated_classnames_support_opt_out() { 715 672 $block_type_settings = array( 716 673 'attributes' => array(),
Note: See TracChangeset
for help on using the changeset viewer.