This repository has been archived by the owner on Oct 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPatternDataHandlersTest.php
236 lines (209 loc) · 5.38 KB
/
PatternDataHandlersTest.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
/**
* Class PatternDataHandlersTest
*
* @package pattern-manager
*/
namespace PatternManager\PatternDataHandlers;
use WP_UnitTestCase;
require_once dirname( __DIR__ ) . '/pattern-data-handlers.php';
require_once __DIR__ . '/fixtures/WpFilesystemSpy.php';
/**
* Test the pattern functions.
*/
class PatternDataHandlersTest extends WP_UnitTestCase {
/**
* @inheritDoc
*/
public function setUp() : void {
parent::setUp();
add_filter( 'request_filesystem_credentials', '__return_true' );
add_filter( 'stylesheet_directory', [ $this, 'get_fixtures_directory' ] );
add_filter( 'stylesheet_directory_uri', [ $this, 'get_stylesheet_directory_uri' ] );
}
/**
* @inheritDoc
*/
public function tearDown() : void {
remove_filter( 'request_filesystem_credentials', '__return_true' );
remove_filter( 'stylesheet_directory', [ $this, 'get_fixtures_directory' ] );
remove_filter( 'stylesheet_directory_uri', [ $this, 'get_stylesheet_directory_uri' ] );
parent::tearDown();
}
/**
* Gets the fixtures directory.
*/
public function get_fixtures_directory() {
return __DIR__ . '/fixtures';
}
/**
* Gets the stylesheet directory URI.
*/
public function get_stylesheet_directory_uri() {
return 'https://example.com/wp-content/themes/foo';
}
/**
* Gets a stub for copy_dir.
*/
public function copy_dir_stub( string $source, string $destination ) {}
/**
* Normalizes in order to compare in tests.
*/
public function normalize( string $to_normalize ): string {
return preg_replace(
'/\s?[\t\n]/',
'',
preg_replace( '/\/\/ phpcs:disable.*[\t\n]/', '', $to_normalize )
);
}
/**
* Gets the expected pattern.
*/
public function get_expected_pattern() {
return [
'title' => 'My New Pattern',
'slug' => 'my-new-pattern',
'description' => 'Here is a description',
'viewportWidth' => 1280,
'categories' => [ 'contact', 'featured' ],
'keywords' => [ 'example', 'music' ],
'blockTypes' => [ 'core/gallery', 'core/media-text' ],
'postTypes' => [ 'wp_block', 'wp_template' ],
'inserter' => true,
'content' => '<!-- wp:paragraph --><p>Here is some content</p><!-- /wp:paragraph -->',
'name' => 'my-new-pattern',
];
}
/**
* Tests construct_pattern_php_file_contents.
*/
public function test_construct_pattern_php_file_contents_empty() {
$this->assertSame(
$this->normalize(
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
file_get_contents( $this->get_fixtures_directory() . '/expected/empty.php' )
),
$this->normalize(
construct_pattern_php_file_contents(
[
'name' => 'empty',
'title' => 'Empty',
]
)
)
);
}
/**
* Tests get_pattern_by_path.
*/
public function test_get_pattern_by_path() {
$actual_pattern = get_pattern_by_path( $this->get_fixtures_directory() . '/patterns/my-new-pattern.php' );
$this->assertSame(
$this->get_expected_pattern(),
array_merge(
$actual_pattern,
[
'content' => $this->normalize( $actual_pattern['content'] ),
]
)
);
}
/**
* Tests get_pattern_by_name.
*/
public function test_get_pattern_by_name_not_found() {
$this->assertFalse(
get_pattern_by_name( 'does-not-exist' )
);
}
/**
* Tests get_pattern_by_name.
*/
public function test_get_pattern_by_name() {
$actual_pattern = get_pattern_by_name( 'my-new-pattern' );
$this->assertSame(
$this->get_expected_pattern(),
array_merge(
$actual_pattern,
[
'content' => $this->normalize( $actual_pattern['content'] ),
]
)
);
}
/**
* Tests construct_pattern_php_file_contents.
*/
public function test_construct_pattern_php_file_contents_with_values() {
$pattern_path = $this->get_fixtures_directory() . '/patterns/my-new-pattern.php';
$this->assertSame(
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
file_get_contents( $pattern_path ),
construct_pattern_php_file_contents(
get_pattern_by_path( $pattern_path )
)
);
}
/**
* Tests get_theme_patterns.
*/
public function test_get_theme_patterns() {
$patterns = get_theme_patterns();
$this->assertCount( 2, array_values( $patterns ) );
$this->assertSame(
[
'my-new-pattern' => $this->get_expected_pattern(),
],
[
'my-new-pattern' => array_merge(
$patterns['my-new-pattern'],
[
'content' => $this->normalize( $patterns['my-new-pattern']['content'] ),
]
),
]
);
}
/**
* Tests get_theme_patterns_with_editor_links.
*/
public function test_get_theme_patterns_with_editor_links() {
$patterns = get_theme_patterns_with_editor_links();
$this->assertCount( 2, array_values( $patterns ) );
$this->assertTrue(
array_key_exists(
'editorLink',
$patterns['my-new-pattern']
)
);
}
/**
* Test get_pattern_names.
*/
public function test_get_pattern_names() {
$this->assertSame(
[
'my-new-pattern',
'with-image',
],
get_pattern_names()
);
}
/**
* Test tree_shake_theme_images.
*/
public function test_tree_shake_theme_images() {
$wp_filesystem = new WpFilesystemSpy();
tree_shake_theme_images(
$wp_filesystem,
[ $this, 'copy_dir_stub' ]
);
// Tree shaking should only keep (copy) the used image.
$this->assertSame(
[
$this->get_fixtures_directory() . '/patterns/images/used.jpg',
],
$wp_filesystem->get_copied()
);
}
}