Make WordPress Core

Changeset 51490


Ignore:
Timestamp:
07/26/2021 05:39:53 PM (3 years ago)
Author:
SergeyBiryukov
Message:

Tests: Move and fix incorrectly placed tests for block supported styles.

The Block_Supported_Styles_Test class is not a TestCase to be extended, but an actual concrete test class. In order to run as expected, it should be placed under phpunit/tests/blocks/ along with the other block tests.

Additionally:

  • Add missing visibility keywords to test methods.
  • Update the expected results to the currently used format for the tests to pass.
  • Remove two outdated tests. The functionality being tested there is no longer available in this manner, so these tests are redundant.

Follow-up to [49226], [49310].

Props jrf, aristath, youknowriad.
See #53363.

Location:
trunk/tests/phpunit
Files:
1 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/bootstrap.php

    r51333 r51490  
    204204require __DIR__ . '/testcase-xmlrpc.php';
    205205require __DIR__ . '/testcase-ajax.php';
    206 require __DIR__ . '/testcase-block-supports.php';
    207206require __DIR__ . '/testcase-canonical.php';
    208207require __DIR__ . '/testcase-xml.php';
  • trunk/tests/phpunit/tests/blocks/block-supported-styles.php

    r51489 r51490  
    11<?php
     2/**
     3 * Block Tests
     4 *
     5 * @package WordPress
     6 * @subpackage Blocks
     7 * @since 5.6.0
     8 */
     9
    210/**
    311 * Test block supported styles.
    412 *
    5  * @package    WordPress
    6  * @subpackage UnitTests
    7  * @since      5.6.0
     13 * @since 5.6.0
     14 *
     15 * @group blocks
    816 */
    917class 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 a
    34      *                                   complete WP_Block_Type instance. In case a WP_Block_Type
    35      *                                   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 the
    38      *     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 string
    64      * (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     }
    7418
    7519    /**
     
    8731
    8832    /**
     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    /**
    8997     * Returns the rendered output for the current block.
    9098     *
    9199     * @param array $block Block to render.
    92      *
    93100     * @return string Rendered output for the current block.
    94101     */
     
    108115     * Runs assertions that the rendered output has expected class/style attrs.
    109116     *
    110      * @param array  $block Block to render.
     117     * @param array  $block            Block to render.
    111118     * @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.
    113120     */
    114121    private function assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ) {
     
    117124        $style_list   = $this->get_attribute_from_block( 'style', $styled_block );
    118125
    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' );
    121128    }
    122129
     
    124131     * Runs assertions that the rendered output has expected content and class/style attrs.
    125132     *
    126      * @param array  $block Block to render.
     133     * @param array  $block            Block to render.
    127134     * @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.
    129136     */
    130137    private function assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles ) {
     
    138145        $style_list = $this->get_attribute_from_block( 'style', $styled_block );
    139146
    140         $this->assertSame( self::BLOCK_CONTENT, $content );
     147        $this->assertSame( self::BLOCK_CONTENT, $content, 'Block content does not match expected content' );
    141148        $this->assertSameSets(
    142149            explode( ' ', $expected_classes ),
    143             explode( ' ', $class_list )
     150            explode( ' ', $class_list ),
     151            'Class list does not match expected classes'
    144152        );
    145153        $this->assertSame(
    146154            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'
    148157        );
    149158    }
     
    152161     * Tests color support for named color support for named colors.
    153162     */
    154     function test_named_color_support() {
     163    public function test_named_color_support() {
    155164        $block_type_settings = array(
    156165            'attributes'      => array(),
     
    184193     * Tests color support for custom colors.
    185194     */
    186     function test_custom_color_support() {
     195    public function test_custom_color_support() {
    187196        $block_type_settings = array(
    188197            'attributes'      => array(),
     
    219228
    220229    /**
    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     /**
    283230     * Tests gradient color support for named gradients.
    284231     */
    285     function test_named_gradient_support() {
     232    public function test_named_gradient_support() {
    286233        $block_type_settings = array(
    287234            'attributes'      => array(),
     
    314261     * Tests gradient color support for custom gradients.
    315262     */
    316     function test_custom_gradient_support() {
     263    public function test_custom_gradient_support() {
    317264        $block_type_settings = array(
    318265            'attributes'      => array(),
     
    345292     * Tests that style attributes for colors are not applied without the support flag.
    346293     */
    347     function test_color_unsupported() {
     294    public function test_color_unsupported() {
    348295        $block_type_settings = array(
    349296            'attributes'      => array(),
     
    381328     * Tests support for named font sizes.
    382329     */
    383     function test_named_font_size() {
     330    public function test_named_font_size() {
    384331        $block_type_settings = array(
    385332            'attributes' => array(),
    386333            'supports'   => array(
    387                 'fontSize' => true,
     334                'typography' => array(
     335                    'fontSize' => true,
     336                ),
    388337            ),
    389338        );
     
    409358     * Tests support for custom font sizes.
    410359     */
    411     function test_custom_font_size() {
     360    public function test_custom_font_size() {
    412361        $block_type_settings = array(
    413362            'attributes' => array(),
    414363            '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' ) ),
    424375            ),
    425376            'innerBlock'   => array(),
     
    437388     * Tests that font size attributes are not applied without support flag.
    438389     */
    439     function test_font_size_unsupported() {
     390    public function test_font_size_unsupported() {
    440391        $block_type_settings = array(
    441392            'attributes' => array(),
     
    464415     * Tests line height support.
    465416     */
    466     function test_line_height() {
     417    public function test_line_height() {
    467418        $block_type_settings = array(
    468419            'attributes' => array(),
    469420            'supports'   => array(
    470                 'lineHeight' => true,
     421                'typography' => array(
     422                    'lineHeight' => true,
     423                ),
    471424            ),
    472425        );
     
    492445     * Tests line height not applied without support flag.
    493446     */
    494     function test_line_height_unsupported() {
     447    public function test_line_height_unsupported() {
    495448        $block_type_settings = array(
    496449            'attributes' => array(),
     
    518471     * Tests support for block alignment.
    519472     */
    520     function test_block_alignment() {
     473    public function test_block_alignment() {
    521474        $block_type_settings = array(
    522475            'attributes' => array(),
     
    546499     * Tests block alignment requires support to be added.
    547500     */
    548     function test_block_alignment_unsupported() {
     501    public function test_block_alignment_unsupported() {
    549502        $block_type_settings = array(
    550503            'attributes' => array(),
     
    572525     * Tests all support flags together to ensure they work together as expected.
    573526     */
    574     function test_all_supported() {
     527    public function test_all_supported() {
    575528        $block_type_settings = array(
    576529            'attributes' => array(),
     
    580533                    'link'      => true,
    581534                ),
    582                 'fontSize'   => true,
    583                 'lineHeight' => true,
     535                'typography' => array(
     536                    'fontSize'   => true,
     537                    'lineHeight' => true,
     538                ),
    584539                'align'      => true,
    585540            ),
     
    599554                    'typography' => array(
    600555                        'lineHeight' => '20',
    601                         'fontSize'   => '10',
     556                        'fontSize'   => '10px',
    602557                    ),
    603558                ),
     
    618573     * Verify one support enabled does not imply multiple supports enabled.
    619574     */
    620     function test_one_supported() {
     575    public function test_one_supported() {
    621576        $block_type_settings = array(
    622577            'attributes' => array(),
    623578            'supports'   => array(
    624                 'fontSize' => true,
     579                'typography' => array(
     580                    'fontSize' => true,
     581                ),
    625582            ),
    626583        );
     
    640597                    'typography' => array(
    641598                        'lineHeight' => '20',
    642                         'fontSize'   => '10',
     599                        'fontSize'   => '10px',
    643600                    ),
    644601                ),
     
    658615     * Tests custom classname server-side block support.
    659616     */
    660     function test_custom_classnames_support() {
     617    public function test_custom_classnames_support() {
    661618        $block_type_settings = array(
    662619            'attributes' => array(),
     
    684641     * Tests custom classname server-side block support opt-out.
    685642     */
    686     function test_custom_classnames_support_opt_out() {
     643    public function test_custom_classnames_support_opt_out() {
    687644        $block_type_settings = array(
    688645            'attributes' => array(),
     
    712669     * Tests generated classname server-side block support opt-out.
    713670     */
    714     function test_generatted_classnames_support_opt_out() {
     671    public function test_generated_classnames_support_opt_out() {
    715672        $block_type_settings = array(
    716673            'attributes' => array(),
Note: See TracChangeset for help on using the changeset viewer.