Skip to content

Commit 89cad11

Browse files
committed
Adds tests for deleting fonts in the previous folder location
1 parent 34321a4 commit 89cad11

File tree

1 file changed

+160
-0
lines changed

1 file changed

+160
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?php
2+
// @core-merge: Do not include these tests, they are for Gutenberg only.
3+
4+
/**
5+
* Test deleting font files from the previous fonts folder (wp-content/fonts).
6+
*
7+
* @package WordPress
8+
* @subpackage Font Library
9+
*
10+
* @group fonts
11+
* @group font-library
12+
*
13+
* @covers ::gutenberg_before_delete_font_face
14+
*/
15+
class Tests_Font_Delete_Files_From_Wp_Content_Folder extends WP_UnitTestCase {
16+
protected static $super_admin_id;
17+
18+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
19+
self::$super_admin_id = $factory->user->create(
20+
array(
21+
'role' => 'administrator',
22+
)
23+
);
24+
grant_super_admin( self::$super_admin_id );
25+
}
26+
27+
public static function wpTearDownAfterClass() {
28+
self::delete_user( self::$super_admin_id );
29+
}
30+
31+
public function test_uploaded_font_files_are_deleted_from_wp_content_folder() {
32+
wp_set_current_user( self::$super_admin_id );
33+
$font_family_id = $this->create_font_family();
34+
35+
// Upload the font file to wp-content/fonts, the previous directory used for fonts.
36+
add_filter( 'font_dir', array( $this, 'filter_font_dir' ) );
37+
$response = $this->create_font_face( $font_family_id );
38+
remove_filter( 'font_dir', array( $this, 'filter_font_dir' ) );
39+
40+
$this->assertSame( 201, $response->get_status(), 'The font face should be created successfully.' );
41+
42+
$data = $response->get_data();
43+
$font_path = str_replace( content_url(), WP_CONTENT_DIR, $data['font_face_settings']['src'] );
44+
45+
// Ensure the file was uploaded to the correct location.
46+
$this->assertFalse( str_contains( $font_path, 'uploads' ), 'The font file should not be in the uploads folder.' );
47+
$this->assertTrue( file_exists( $font_path ), 'The font file should exist in the wp-content/fonts folder.' );
48+
49+
// Ensure the file is deleted when the font face post is deleted, even when the directory is not filtered.
50+
wp_delete_post( $font_family_id, true );
51+
$this->assertTrue( str_contains( wp_get_font_dir()['path'], 'uploads' ), 'The font directory should be in uploads.' );
52+
$this->assertFalse( file_exists( $font_path ), 'The font file should be deleted from the wp-content/fonts folder.' );
53+
}
54+
55+
/**
56+
* @group multisite
57+
* @group ms-required
58+
*/
59+
public function test_uploaded_font_files_are_deleted_from_wp_content_folder_multisite() {
60+
wp_set_current_user( self::$super_admin_id );
61+
$blog_id = self::factory()->blog->create();
62+
63+
switch_to_blog( $blog_id );
64+
$font_family_id = $this->create_font_family();
65+
66+
// Upload the font file to wp-content/fonts, the previous directory used for fonts.
67+
add_filter( 'font_dir', array( $this, 'filter_font_dir' ) );
68+
$response = $this->create_font_face( $font_family_id );
69+
remove_filter( 'font_dir', array( $this, 'filter_font_dir' ) );
70+
restore_current_blog();
71+
72+
$this->assertSame( 201, $response->get_status(), 'The font face should be created successfully.' );
73+
74+
$data = $response->get_data();
75+
$font_path = str_replace( content_url(), WP_CONTENT_DIR, $data['font_face_settings']['src'] );
76+
77+
// Ensure the file was uploaded to the correct location.
78+
$this->assertFalse( str_contains( $font_path, 'uploads' ), 'The font file should not be in the uploads folder.' );
79+
$this->assertTrue( file_exists( $font_path ), 'The font file should exist in the wp-content/fonts folder.' );
80+
81+
// Ensure the file is deleted when the font face post is deleted, even when the directory is not filtered.
82+
switch_to_blog( $blog_id );
83+
wp_delete_post( $font_family_id, true );
84+
restore_current_blog();
85+
86+
$this->assertTrue( str_contains( wp_get_font_dir()['path'], 'uploads' ), 'The font directory should be in uploads.' );
87+
$this->assertFalse( file_exists( $font_path ), 'The font file should be deleted from the wp-content/fonts folder.' );
88+
}
89+
90+
/**
91+
* Set the font directory to wp-content/fonts, the default previously used in Gutenberg.
92+
*
93+
* @param array $font_dir Font directory settings.
94+
* @return array Filtered font directory settings.
95+
*/
96+
public function filter_font_dir( $font_dir ) {
97+
$site_path = '';
98+
if ( is_multisite() && ! ( is_main_network() && is_main_site() ) ) {
99+
$site_path = '/sites/' . get_current_blog_id();
100+
}
101+
102+
$font_dir['path'] = path_join( WP_CONTENT_DIR, 'fonts' ) . $site_path;
103+
$font_dir['url'] = untrailingslashit( content_url( 'fonts' ) ) . $site_path;
104+
$font_dir['basedir'] = path_join( WP_CONTENT_DIR, 'fonts' ) . $site_path;
105+
$font_dir['baseurl'] = untrailingslashit( content_url( 'fonts' ) ) . $site_path;
106+
107+
return $font_dir;
108+
}
109+
110+
protected function create_font_family() {
111+
return self::factory()->post->create(
112+
wp_slash(
113+
array(
114+
'post_type' => 'wp_font_family',
115+
'post_status' => 'publish',
116+
'post_title' => 'Open Sans',
117+
'post_name' => 'open-sans',
118+
'post_content' => wp_json_encode(
119+
array(
120+
'fontFamily' => '"Open Sans"',
121+
)
122+
),
123+
)
124+
)
125+
);
126+
}
127+
128+
protected function create_font_face( $font_family_id ) {
129+
// Create a new font face with a font file upload.
130+
$font_file = GUTENBERG_DIR_TESTDATA . '/fonts/OpenSans-Regular.woff2';
131+
$font_path = wp_tempnam( 'OpenSans-Regular.woff2' );
132+
copy( $font_file, $font_path );
133+
134+
$files = array(
135+
'file-0' => array(
136+
'name' => 'OpenSans-Regular.woff2',
137+
'full_path' => 'OpenSans-Regular.woff2',
138+
'type' => 'font/woff2',
139+
'tmp_name' => $font_path,
140+
'error' => 0,
141+
'size' => filesize( $font_path ),
142+
),
143+
);
144+
145+
$request = new WP_REST_Request( 'POST', '/wp/v2/font-families/' . $font_family_id . '/font-faces' );
146+
$request->set_param(
147+
'font_face_settings',
148+
wp_json_encode(
149+
array(
150+
'fontFamily' => '"Open Sans"',
151+
'fontWeight' => '200',
152+
'fontStyle' => 'normal',
153+
'src' => 'file-0',
154+
)
155+
)
156+
);
157+
$request->set_file_params( $files );
158+
return rest_get_server()->dispatch( $request );
159+
}
160+
}

0 commit comments

Comments
 (0)