From f40cda4fadcc50aa8ef18d18bcaaae8857cb109d Mon Sep 17 00:00:00 2001 From: ramonjd Date: Wed, 6 Apr 2022 09:49:33 +1000 Subject: [PATCH] Migrate layout PHP unit tests --- tests/phpunit/tests/block-supports/layout.php | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 tests/phpunit/tests/block-supports/layout.php diff --git a/tests/phpunit/tests/block-supports/layout.php b/tests/phpunit/tests/block-supports/layout.php new file mode 100644 index 0000000000000..e99b9c326fc61 --- /dev/null +++ b/tests/phpunit/tests/block-supports/layout.php @@ -0,0 +1,107 @@ +theme_root = realpath( DIR_TESTDATA . '/themedir1' ); + $this->orig_theme_dir = $GLOBALS['wp_theme_directories']; + + // /themes is necessary as theme.php functions assume /themes is the root if there is only one root. + $GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root ); + // Set up the new root. + add_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) ); + add_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) ); + add_filter( 'template_root', array( $this, 'filter_set_theme_root' ) ); + + $this->queries = array(); + // Clear caches. + wp_clean_themes_cache(); + unset( $GLOBALS['wp_themes'] ); + } + + function tearDown() { + $GLOBALS['wp_theme_directories'] = $this->orig_theme_dir; + + // Clear up the filters to modify the theme root. + remove_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) ); + remove_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) ); + remove_filter( 'template_root', array( $this, 'filter_set_theme_root' ) ); + + wp_clean_themes_cache(); + unset( $GLOBALS['wp_themes'] ); + parent::tearDown(); + } + + function filter_set_theme_root() { + return $this->theme_root; + } + + function test_outer_container_not_restored_for_non_aligned_image_block_with_non_themejson_theme() { + // The "default" theme doesn't have theme.json support. + switch_theme( 'default' ); + $block = array( + 'blockName' => 'core/image', + 'attrs' => array(), + ); + $block_content = '
'; + $expected = '
'; + + $this->assertSame( $expected, wp_restore_image_outer_container( $block_content, $block ) ); + } + + function test_outer_container_restored_for_aligned_image_block_with_non_themejson_theme() { + // The "default" theme doesn't have theme.json support. + switch_theme( 'default' ); + $block = array( + 'blockName' => 'core/image', + 'attrs' => array(), + ); + $block_content = '
'; + $expected = '
'; + + $this->assertSame( $expected, wp_restore_image_outer_container( $block_content, $block ) ); + } + + function test_additional_styles_moved_to_restored_outer_container_for_aligned_image_block_with_non_themejson_theme() { + // The "default" theme doesn't have theme.json support. + switch_theme( 'default' ); + $block = array( + 'blockName' => 'core/image', + 'attrs' => array( + 'className' => 'is-style-round my-custom-classname', + ), + ); + + $block_classes_end_placement = '
'; + $block_classes_start_placement = '
'; + $block_classes_middle_placement = '
'; + $block_classes_random_placement = '
'; + $expected = '
'; + + $this->assertSame( $expected, wp_restore_image_outer_container( $block_classes_end_placement, $block ) ); + $this->assertSame( $expected, wp_restore_image_outer_container( $block_classes_start_placement, $block ) ); + $this->assertSame( $expected, wp_restore_image_outer_container( $block_classes_middle_placement, $block ) ); + $this->assertSame( $expected, wp_restore_image_outer_container( $block_classes_random_placement, $block ) ); + + $block_classes_other_attributes = '
'; + $expected_other_attributes = '
'; + + $this->assertSame( $expected_other_attributes, wp_restore_image_outer_container( $block_classes_other_attributes, $block ) ); + } + + function test_outer_container_not_restored_for_aligned_image_block_with_themejson_theme() { + switch_theme( 'block-theme' ); + $block = array( + 'blockName' => 'core/image', + 'attrs' => array( + 'className' => 'is-style-round my-custom-classname', + ), + ); + $block_content = '
'; + $expected = '
'; + + $this->assertSame( $expected, wp_restore_image_outer_container( $block_content, $block ) ); + } +}