From 041b751b8e190c61ee662dc9b773cff9e27fd7a9 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 6 Jun 2024 11:20:31 +0200 Subject: [PATCH] Add test coverage --- .../phpunit/data/blocks/notice/variations.php | 10 ++++++ tests/phpunit/tests/blocks/register.php | 33 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 tests/phpunit/data/blocks/notice/variations.php diff --git a/tests/phpunit/data/blocks/notice/variations.php b/tests/phpunit/data/blocks/notice/variations.php new file mode 100644 index 0000000000000..59a62c7244108 --- /dev/null +++ b/tests/phpunit/data/blocks/notice/variations.php @@ -0,0 +1,10 @@ + 'warning', + 'title' => 'warning', + 'description' => 'Shows warning.', + 'keywords' => array( 'warning' ) + ) +); diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 1dbc688bb16cf..9a7f811127a92 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -957,6 +957,39 @@ public function data_register_block_registers_with_args_override_returns_false_w ); } + /** + * Tests registering a block with variations from a PHP file. + * + * @ticket 61280 + * + * @covers ::register_block_type_from_metadata + */ + public function test_register_block_type_from_metadata_with_variations_php_file() { + $result = register_block_type_from_metadata( + DIR_TESTDATA . '/blocks/notice', + array( + 'api_version' => 2, + 'name' => 'tests/notice-with-variations-php', + 'title' => 'Notice with variations from a PHP file', + 'variations' => 'variations.php' + ) + ); + + $this->assertInstanceOf( 'WP_Block_Type', $result, 'The block was not registered' ); + $this->assertSame( 2, $result->api_version, 'The API version is incorrect' ); + $this->assertSame( 'tests/notice-with-variations-php', $result->name, 'The block name is incorrect' ); + $this->assertSame( 'Notice with variations from a PHP file', $result->title, 'The block title is incorrect' ); + $this->assertSame( 'variations.php', $result->variations, 'The block variations are incorrect' ); + $this->assertIsCallable( $result->variation_callback, 'The variation callback hasn\'t been set' ); + + $expected_variations = require DIR_TESTDATA . '/blocks/notice/variations.php'; + $this->assertSame( + $expected_variations, + call_user_func( $result->variation_callback ), + 'The variation callback hasn\'t been set correctly' + ); + } + /** * Tests that the function returns the registered block when the `block.json` * is found in the fixtures directory.