2020#[CoversClass(User_Interface::class)]
2121final class UserInterfaceTest extends MockeryTestCase
2222{
23+ /**
24+ * @var string[] Asset files created by a test to satisfy the file_exists() guard
25+ */
26+ private $ createdAssets = [];
27+
28+ /**
29+ * @var string[] Asset files moved aside by a test to force their absence
30+ */
31+ private $ movedAssets = [];
32+
2333 protected function setUp (): void
2434 {
2535 parent ::setUp ();
2636 setUp ();
2737 }
2838
39+ /**
40+ * The admin asset files the source guards with file_exists() (minified variant).
41+ *
42+ * @return string[] Absolute asset paths
43+ */
44+ private function assetTargets (): array
45+ {
46+ return [
47+ \EPI_EMBED_PRIVACY_BASE . 'assets/js/admin/image-upload.min.js ' ,
48+ \EPI_EMBED_PRIVACY_BASE . 'assets/style/embed-privacy-admin.min.css ' ,
49+ \EPI_EMBED_PRIVACY_BASE . 'assets/js/admin/clipboard.min.js ' ,
50+ \EPI_EMBED_PRIVACY_BASE . 'assets/style/settings.min.css ' ,
51+ ];
52+ }
53+
54+ /**
55+ * Ensure the guarded asset files exist on disk (creating only missing ones).
56+ */
57+ private function makeAssetsAvailable (): void
58+ {
59+ foreach ($ this ->assetTargets () as $ path ) {
60+ if (\file_exists ($ path )) {
61+ continue ;
62+ }
63+
64+ if (!\is_dir (\dirname ($ path ))) {
65+ \mkdir (\dirname ($ path ), 0755 , true );
66+ }
67+
68+ \file_put_contents ($ path , '' );
69+ $ this ->createdAssets [] = $ path ;
70+ }
71+ }
72+
73+ /**
74+ * Ensure the guarded asset files are absent (moving any existing ones aside).
75+ */
76+ private function makeAssetsUnavailable (): void
77+ {
78+ foreach ($ this ->assetTargets () as $ path ) {
79+ if (!\file_exists ($ path )) {
80+ continue ;
81+ }
82+
83+ \rename ($ path , $ path . '.epbak ' );
84+ $ this ->movedAssets [] = $ path ;
85+ }
86+ }
87+
2988 public function testInitRegistersHooks (): void
3089 {
3190 User_Interface::init ();
@@ -88,6 +147,8 @@ public function testEnqueueAssetsForPostEditScreen(): void
88147 stubs ([
89148 'get_current_screen ' => $ screen ,
90149 ]);
150+ // assets exist on disk, so they are enqueued (version is their filemtime)
151+ $ this ->makeAssetsAvailable ();
91152
92153 expect ('wp_enqueue_script ' )
93154 ->once ()
@@ -105,6 +166,23 @@ public function testEnqueueAssetsForPostEditScreen(): void
105166 User_Interface::enqueue_assets ('post.php ' );
106167 }
107168
169+ public function testEnqueueAssetsSkipsMissingAssetsOnPostEditScreen (): void
170+ {
171+ $ screen = Mockery::mock ('WP_Screen ' );
172+ $ screen ->id = 'post ' ;
173+
174+ stubs ([
175+ 'get_current_screen ' => $ screen ,
176+ ]);
177+ // assets are not available on disk, so the guard skips enqueue (and filemtime)
178+ $ this ->makeAssetsUnavailable ();
179+
180+ expect ('wp_enqueue_script ' )->never ();
181+ expect ('wp_enqueue_style ' )->never ();
182+
183+ User_Interface::enqueue_assets ('post.php ' );
184+ }
185+
108186 public function testEnqueueAssetsForEmbedScreen (): void
109187 {
110188 $ screen = Mockery::mock ('WP_Screen ' );
@@ -113,6 +191,7 @@ public function testEnqueueAssetsForEmbedScreen(): void
113191 stubs ([
114192 'get_current_screen ' => $ screen ,
115193 ]);
194+ $ this ->makeAssetsAvailable ();
116195
117196 // the embed post type screen also triggers the image upload assets
118197 expect ('wp_enqueue_script ' )->once ()->with (
@@ -136,6 +215,7 @@ public function testEnqueueAssetsForSettingsScreen(): void
136215 stubs ([
137216 'get_current_screen ' => $ screen ,
138217 ]);
218+ $ this ->makeAssetsAvailable ();
139219
140220 expect ('wp_enqueue_script ' )->once ()->with (
141221 'embed-privacy-admin-clipboard ' ,
@@ -155,8 +235,41 @@ public function testEnqueueAssetsForSettingsScreen(): void
155235 User_Interface::enqueue_assets ('settings_page_embed_privacy ' );
156236 }
157237
238+ public function testEnqueueAssetsSkipsMissingAssetsOnSettingsScreen (): void
239+ {
240+ $ screen = Mockery::mock ('WP_Screen ' );
241+ $ screen ->id = 'settings_page_embed_privacy ' ;
242+
243+ stubs ([
244+ 'get_current_screen ' => $ screen ,
245+ ]);
246+ // no built assets => no script/style/localize and no filemtime warning
247+ $ this ->makeAssetsUnavailable ();
248+
249+ expect ('wp_enqueue_script ' )->never ();
250+ expect ('wp_localize_script ' )->never ();
251+ expect ('wp_enqueue_style ' )->never ();
252+
253+ User_Interface::enqueue_assets ('settings_page_embed_privacy ' );
254+ }
255+
158256 protected function tearDown (): void
159257 {
258+ foreach ($ this ->createdAssets as $ path ) {
259+ if (\file_exists ($ path )) {
260+ \unlink ($ path );
261+ }
262+ }
263+
264+ foreach ($ this ->movedAssets as $ path ) {
265+ if (\file_exists ($ path . '.epbak ' )) {
266+ \rename ($ path . '.epbak ' , $ path );
267+ }
268+ }
269+
270+ $ this ->createdAssets = [];
271+ $ this ->movedAssets = [];
272+
160273 tearDown ();
161274 parent ::tearDown ();
162275 }
0 commit comments