Make WordPress Core


Ignore:
Timestamp:
02/21/2024 07:24:14 PM (12 months ago)
Author:
swissspidy
Message:

Editor: Ensure font collection metadata can be properly localized.

Updates wp_register_font_collection() and WP_Font_Collection so that only font families can be loaded from a file or URL.
All metadata, such as name, description, and list of font categories, needs to be passed directly in PHP so that it can be properly localized.

Props swissspidy, mmaattiiaass, grantmkin, youknowriad.
Fixes #60509.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/fonts/font-library/wpFontCollection/getData.php

    r57657 r57686  
    4141        file_put_contents( $mock_file, wp_json_encode( $config ) );
    4242
    43         $collection = new WP_Font_Collection( $slug, $mock_file );
     43        $collection = new WP_Font_Collection(
     44            $slug,
     45            array_merge(
     46                $config,
     47                array( 'font_families' => $mock_file )
     48            )
     49        );
    4450        $data       = $collection->get_data();
    4551
    4652        $this->assertSame( $slug, $collection->slug, 'The slug should match.' );
    47         $this->assertSame( $expected_data, $data, 'The collection data should match.' );
     53        $this->assertEqualSetsWithIndex( $expected_data, $data, 'The collection data should match.' );
    4854    }
    4955
     
    5965
    6066        self::$mock_collection_data = $config;
    61         $collection                 = new WP_Font_Collection( $slug, 'https://example.com/fonts/mock-font-collection.json' );
     67        $collection                 = new WP_Font_Collection(
     68            $slug,
     69            array_merge(
     70                $config,
     71                array(
     72                    'font_families' => 'https://example.com/fonts/mock-font-collection.json',
     73                )
     74            )
     75        );
    6276        $data                       = $collection->get_data();
    6377
     
    6579
    6680        $this->assertSame( $slug, $collection->slug, 'The slug should match.' );
    67         $this->assertSame( $expected_data, $data, 'The collection data should match.' );
     81        $this->assertEqualSetsWithIndex( $expected_data, $data, 'The collection data should match.' );
    6882    }
    6983
     
    7589    public function data_create_font_collection() {
    7690        return array(
    77 
    7891            'font collection with required data' => array(
    7992                'slug'          => 'my-collection',
     
    186199                ),
    187200            ),
    188 
    189201        );
    190202    }
     
    203215        $this->assertWPError( $data, 'Error is not returned when property is missing or invalid.' );
    204216        $this->assertSame(
    205             $data->get_error_code(),
    206217            'font_collection_missing_property',
     218            $data->get_error_code(),
    207219            'Incorrect error code when property is missing or invalid.'
    208220        );
     
    244256        $this->setExpectedIncorrectUsage( 'WP_Font_Collection::load_from_json' );
    245257
    246         $collection = new WP_Font_Collection( 'my-collection', 'non-existing.json' );
     258        $collection = new WP_Font_Collection(
     259            'my-collection',
     260            array(
     261                'name'          => 'My collection',
     262                'font_families' => 'non-existing.json',
     263            )
     264        );
    247265        $data       = $collection->get_data();
    248266
    249267        $this->assertWPError( $data, 'Error is not returned when invalid file path is provided.' );
    250268        $this->assertSame(
    251             $data->get_error_code(),
    252269            'font_collection_json_missing',
     270            $data->get_error_code(),
    253271            'Incorrect error code when invalid file path is provided.'
    254272        );
     
    259277        file_put_contents( $mock_file, 'invalid-json' );
    260278
    261         $collection = new WP_Font_Collection( 'my-collection', $mock_file );
     279        $collection = new WP_Font_Collection(
     280            'my-collection',
     281            array(
     282                'name'          => 'Invalid collection',
     283                'font_families' => $mock_file,
     284            )
     285        );
    262286
    263287        // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- Testing error response returned by `load_from_json`, not the underlying error from `wp_json_file_decode`.
     
    266290        $this->assertWPError( $data, 'Error is not returned with invalid json file contents.' );
    267291        $this->assertSame(
    268             $data->get_error_code(),
    269292            'font_collection_decode_error',
     293            $data->get_error_code(),
    270294            'Incorrect error code with invalid json file contents.'
    271295        );
     
    275299        $this->setExpectedIncorrectUsage( 'WP_Font_Collection::load_from_json' );
    276300
    277         $collection = new WP_Font_Collection( 'my-collection', 'not-a-url' );
     301        $collection = new WP_Font_Collection(
     302            'my-collection',
     303            array(
     304                'name'          => 'Invalid collection',
     305                'font_families' => 'not-a-url',
     306            )
     307        );
    278308        $data       = $collection->get_data();
    279309
    280310        $this->assertWPError( $data, 'Error is not returned when invalid url is provided.' );
    281311        $this->assertSame(
    282             $data->get_error_code(),
    283312            'font_collection_json_missing',
     313            $data->get_error_code(),
    284314            'Incorrect error code when invalid url is provided.'
    285315        );
     
    289319        add_filter( 'pre_http_request', array( $this, 'mock_request_unsuccessful_response' ), 10, 3 );
    290320
    291         $collection = new WP_Font_Collection( 'my-collection', 'https://example.com/fonts/missing-collection.json' );
     321        $collection = new WP_Font_Collection(
     322            'my-collection',
     323            array(
     324                'name'          => 'Missing collection',
     325                'font_families' => 'https://example.com/fonts/missing-collection.json',
     326            )
     327        );
    292328        $data       = $collection->get_data();
    293329
     
    296332        $this->assertWPError( $data, 'Error is not returned when response is unsuccessful.' );
    297333        $this->assertSame(
    298             $data->get_error_code(),
    299334            'font_collection_request_error',
    300             'Incorrect error code when response is unsuccussful.'
     335            $data->get_error_code(),
     336            'Incorrect error code when response is unsuccessful.'
    301337        );
    302338    }
     
    305341        add_filter( 'pre_http_request', array( $this, 'mock_request_invalid_json' ), 10, 3 );
    306342
    307         $collection = new WP_Font_Collection( 'my-collection', 'https://example.com/fonts/invalid-collection.json' );
     343        $collection = new WP_Font_Collection(
     344            'my-collection',
     345            array(
     346                'name'          => 'Invalid collection',
     347                'font_families' => 'https://example.com/fonts/invalid-collection.json',
     348            )
     349        );
    308350        $data       = $collection->get_data();
    309351
     
    312354        $this->assertWPError( $data, 'Error is not returned when response is invalid json.' );
    313355        $this->assertSame(
    314             $data->get_error_code(),
    315356            'font_collection_decode_error',
     357            $data->get_error_code(),
    316358            'Incorrect error code when response is invalid json.'
    317359        );
Note: See TracChangeset for help on using the changeset viewer.