From e5e3f3cd54b47e61ded3f9605e52eecbfbcf5816 Mon Sep 17 00:00:00 2001 From: Chris Reynolds Date: Wed, 27 Mar 2024 08:56:47 -0600 Subject: [PATCH] [CMSP-967] Remove `font_dir` filter (#35) * remove fonts * remove font tests --- inc/fonts.php | 38 --------- pantheon.php | 8 +- tests/phpunit/test-fonts.php | 158 ----------------------------------- 3 files changed, 2 insertions(+), 202 deletions(-) delete mode 100644 inc/fonts.php delete mode 100644 tests/phpunit/test-fonts.php diff --git a/inc/fonts.php b/inc/fonts.php deleted file mode 100644 index 68b4984..0000000 --- a/inc/fonts.php +++ /dev/null @@ -1,38 +0,0 @@ -original_pantheon_upload_dir = $_pantheon_upload_dir; // Backup original global if needed. - - // Manually set the global variable to a mocked value. - $_pantheon_upload_dir = [ - 'basedir' => WP_CONTENT_DIR . '/uploads', - 'baseurl' => 'http://example.org/wp-content/uploads', - ]; - } - - public function tearDown(): void { - // Restore original global state after each test if necessary. - global $_pantheon_upload_dir; - $_pantheon_upload_dir = $this->original_pantheon_upload_dir; - - parent::tearDown(); - } - - /** - * Test the font library modifications have been loaded. - */ - public function test_font_library_modifications() { - $this->assertTrue( function_exists( 'Pantheon\Fonts\bootstrap' ) ); - $this->assertEquals( has_action( 'init', 'Pantheon\Fonts\bootstrap' ), 10 ); - } - - /** - * Test the pantheon_font_dir function. - */ - public function test_pantheon_font_dir() { - $this->assertTrue( function_exists( 'Pantheon\Fonts\pantheon_font_dir' ) ); - - $this->maybe_get_font_library(); - if ( ! function_exists( 'wp_get_font_dir' ) ) { - // If the function still doesn't exist after trying to get the font library from gutenberg, mark the test skipped. - $this->markTestSkipped( 'The wp_get_font_dir function is not available. We\'re probably not using WP 6.5+' ); - } - - // Remove the filters we apply to `font_dir` so we're getting the default data. - remove_all_filters( 'font_dir' ); - $default_fonts = wp_get_font_dir(); - $font_dir = Fonts\pantheon_font_dir( $default_fonts ); - - $this->assertNotEquals( $default_fonts, $font_dir ); - $this->assertEquals( array_keys( $default_fonts ), array_keys( $font_dir ) ); - $this->assertArrayHasKey( 'path', $font_dir ); - $this->assertArrayHasKey( 'url', $font_dir ); - $this->assertArrayHasKey( 'basedir', $font_dir ); - $this->assertArrayHasKey( 'baseurl', $font_dir ); - $this->assertStringContainsString( 'uploads/fonts', $font_dir['path'] ); - $this->assertStringContainsString( 'uploads/fonts', $font_dir['url'] ); - $this->assertStringContainsString( 'uploads/fonts', $font_dir['basedir'] ); - $this->assertStringContainsString( 'uploads/fonts', $font_dir['baseurl'] ); - } - - /** - * Test that our filtered font directory is filtered properly. - */ - public function test_pantheon_font_dir_filter() { - $this->maybe_get_font_library(); - if ( ! function_exists( 'wp_get_font_dir' ) ) { - // If the function still doesn't exist after trying to get the font library from gutenberg, mark the test skipped. - $this->markTestSkipped( 'The wp_get_font_dir function is not available. We\'re probably not using WP 6.5+' ); - } - - add_filter( 'font_dir', '\\Pantheon\\Fonts\\pantheon_font_dir' ); - $font_dir = wp_get_font_dir(); - - $expected = [ - 'path' => WP_CONTENT_DIR . '/uploads/fonts', - 'url' => WP_CONTENT_URL . '/uploads/fonts', - 'subdir' => '', - 'basedir' => WP_CONTENT_DIR . '/uploads/fonts', - 'baseurl' => WP_CONTENT_URL . '/uploads/fonts', - 'error' => false, - ]; - - $this->assertEquals( $expected, $font_dir ); - } - - /** - * Test that using the font_dir filter at priority 10 overrides our modifications. - */ - public function test_pantheon_fonts_dir_filter() { - $this->maybe_get_font_library(); - if ( ! function_exists( 'wp_get_font_dir' ) ) { - // If the function still doesn't exist after trying to get the font library from gutenberg, mark the test skipped. - $this->markTestSkipped( 'The wp_get_font_dir function is not available. We\'re probably not using WP 6.5+' ); - } - - $custom_directory = [ - 'path' => WP_CONTENT_DIR . '/custom-fonts', - 'url' => WP_CONTENT_URL . '/custom-fonts', - 'basedir' => WP_CONTENT_DIR . '/custom-fonts', - 'baseurl' => WP_CONTENT_URL . '/custom-fonts', - ]; - - add_filter( 'font_dir', function ( $defaults ) use ( $custom_directory ) { - $defaults['path'] = $custom_directory['path']; - $defaults['url'] = $custom_directory['url']; - $defaults['basedir'] = $custom_directory['basedir']; - $defaults['baseurl'] = $custom_directory['baseurl']; - return $defaults; - } ); - - Fonts\bootstrap(); - $font_dir = wp_get_font_dir(); - - $expected = [ - 'path' => WP_CONTENT_DIR . '/custom-fonts', - 'url' => WP_CONTENT_URL . '/custom-fonts', - 'subdir' => '', - 'basedir' => WP_CONTENT_DIR . '/custom-fonts', - 'baseurl' => WP_CONTENT_URL . '/custom-fonts', - 'error' => false, - ]; - - $this->assertEquals( $expected, $font_dir ); - } - - /** - * Get the font library from Gutenberg if it's not available. - */ - private function maybe_get_font_library() { - $fonts_php = WP_PLUGIN_DIR . '/gutenberg/lib/compat/wordpress-6.5/fonts/fonts.php'; - if ( ! function_exists( 'wp_get_font_dir' ) && file_exists( $fonts_php ) ) { - require_once $fonts_php; - } - } -}