-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
class-extend-styles-test.php
185 lines (154 loc) · 4.04 KB
/
class-extend-styles-test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
* Test `gutenberg_extend_block_editor_styles`.
*
* @package Gutenberg
*/
class Extend_Styles_Test extends WP_UnitTestCase {
/**
* Path of the original `editor-styles.css` file.
*
* @var string|null
*/
protected $orignal_file = null;
/**
* Contents of the `editor-styles.css` file.
*
* @var string
*/
protected $style_contents = null;
public function wpTearDown() {
parent::wpTearDown();
$this->restore_editor_styles();
}
/**
* Restores the existence of `editor-styles.css` to its original state.
*/
protected function restore_editor_styles() {
$path = gutenberg_dir_path() . 'build/editor/editor-styles.css';
if ( $this->original_file ) {
if ( $this->original_file !== $path ) {
rename( $this->original_file, $path );
}
} elseif ( file_exists( $path ) ) {
unlink( $path );
}
$this->style_contents = null;
$this->original_file = null;
}
/**
* Guarantees that an `editor-styles.css` file exists, if and only if it
* should exist. Assigns `style_contents` according to the contents of the
* file if it should exist. Renames the existing file temporarily if it
* exists but should not.
*
* @param bool $should_exist Whether the editor styles file should exist.
*/
protected function ensure_editor_styles( $should_exist = true ) {
$path = gutenberg_dir_path() . 'build/editor/editor-styles.css';
if ( file_exists( $path ) ) {
if ( $should_exist ) {
$this->style_contents = file_get_contents( $path );
$this->original_file = $path;
} else {
rename( $path, $path . '.bak' );
$this->original_file = $path . '.bak';
}
} elseif ( $should_exist ) {
$this->style_contents = '';
file_put_contents( $path, $this->style_contents );
$this->original_file = null;
}
}
/**
* Tests a non-existent build `styles`.
*/
function test_without_built_styles() {
$this->ensure_editor_styles( false );
$settings = array(
'styles' => array(
array( 'css' => 'original' ),
array( 'css' => 'someother' ),
),
);
$result = gutenberg_extend_block_editor_styles( $settings );
$this->assertEquals( $settings, $result );
}
/**
* Tests an unset `styles` setting.
*/
function test_unset_editor_settings_style() {
$this->ensure_editor_styles();
$settings = array();
$settings = gutenberg_extend_block_editor_styles( $settings );
$this->assertEquals(
array( array( 'css' => $this->style_contents ) ),
$settings['styles']
);
}
/**
* Tests replacing the default styles.
*/
function test_replace_default_editor_styles() {
$this->ensure_editor_styles();
$default_styles = file_get_contents(
ABSPATH . WPINC . '/css/dist/editor/editor-styles.css'
);
$settings = array(
'styles' => array(
array( 'css' => $default_styles ),
array( 'css' => 'someother' ),
),
);
$settings = gutenberg_extend_block_editor_styles( $settings );
$this->assertEquals(
array(
array( 'css' => $this->style_contents ),
array( 'css' => 'someother' ),
),
$settings['styles']
);
}
/**
* Tests replacing the rearranged default styles.
*/
function test_replace_rearranged_default_editor_styles() {
$this->ensure_editor_styles();
$default_styles = file_get_contents(
ABSPATH . WPINC . '/css/dist/editor/editor-styles.css'
);
$settings = array(
'styles' => array(
array( 'css' => 'someother' ),
array( 'css' => $default_styles ),
),
);
$settings = gutenberg_extend_block_editor_styles( $settings );
$this->assertEquals(
array(
array( 'css' => 'someother' ),
array( 'css' => $this->style_contents ),
),
$settings['styles']
);
}
/**
* Tests when the default styles aren't in the styles setting.
*/
function test_without_default_editor_styles() {
$this->ensure_editor_styles();
$settings = array(
'styles' => array(
array( 'css' => 'someother' ),
),
);
$settings = gutenberg_extend_block_editor_styles( $settings );
$this->assertEquals(
array(
array( 'css' => $this->style_contents ),
array( 'css' => 'someother' ),
),
$settings['styles']
);
}
}