@@ -102,4 +102,104 @@ public function test_export() {
102102 unlink ( $ target );
103103 rmdir ( $ target_dir );
104104 }
105+
106+ public function test_import () {
107+ $ max_size = 32 ;
108+ $ ttl = 60 ;
109+ $ cache_dir = Utils \get_temp_dir () . uniqid ( 'wp-cli-test-file-cache ' , true );
110+ $ cache = new FileCache ( $ cache_dir , $ ttl , $ max_size );
111+
112+ $ tmp_dir = Utils \get_temp_dir () . uniqid ( 'wp-cli-test-file-cache-import ' , true );
113+ mkdir ( $ tmp_dir );
114+
115+ // "$group/$slug-$version.$ext";
116+ $ key = 'plugin/my-fixture-plugin-1.0.0.zip ' ;
117+ $ fixture_filepath = $ tmp_dir . '/my-downloaded-fixture-plugin-1.0.0.zip ' ;
118+
119+ $ zip = new ZipArchive ();
120+ $ zip ->open ( $ fixture_filepath , ZIPARCHIVE ::CREATE );
121+ $ zip ->addFile ( __FILE__ );
122+ $ zip ->close ();
123+
124+ $ result = $ cache ->import ( $ key , $ fixture_filepath );
125+
126+ // Assert file is imported.
127+ $ this ->assertTrue ( $ result );
128+ $ this ->assertFileExists ( "{$ cache_dir }/ {$ key }" );
129+
130+ // Clean up.
131+ $ cache ->clear ();
132+ unlink ( $ fixture_filepath );
133+ rmdir ( $ tmp_dir );
134+ }
135+
136+ /**
137+ * @see https://github.com/wp-cli/wp-cli/pull/5947
138+ */
139+ public function test_import_do_not_use_cache_file_cannot_be_read () {
140+ $ max_size = 32 ;
141+ $ ttl = 60 ;
142+ $ cache_dir = Utils \get_temp_dir () . uniqid ( 'wp-cli-test-file-cache ' , true );
143+ $ cache = new FileCache ( $ cache_dir , $ ttl , $ max_size );
144+
145+ $ tmp_dir = Utils \get_temp_dir () . uniqid ( 'wp-cli-test-file-cache-import ' , true );
146+ mkdir ( $ tmp_dir );
147+
148+ $ key = 'plugin/my-fixture-plugin-1.0.0.zip ' ;
149+ $ fixture_filepath = $ tmp_dir . '/my-bad-permissions-fixture-plugin-1.0.0.zip ' ;
150+
151+ $ zip = new ZipArchive ();
152+ $ zip ->open ( $ fixture_filepath , ZIPARCHIVE ::CREATE );
153+ $ zip ->addFile ( __FILE__ );
154+ $ zip ->close ();
155+
156+ chmod ( $ fixture_filepath , 0000 );
157+
158+ // "Warning: copy(-.): Failed to open stream: Permission denied".
159+ $ error = null ;
160+ set_error_handler (
161+ function ( $ errno , $ errstr ) use ( &$ error ) {
162+ $ error = $ errstr ;
163+ }
164+ );
165+
166+ $ result = $ cache ->import ( $ key , $ fixture_filepath );
167+
168+ restore_error_handler ();
169+
170+ $ this ->assertNull ( $ error );
171+ $ this ->assertFalse ( $ result );
172+
173+ // Clean up.
174+ $ cache ->clear ();
175+ unlink ( $ fixture_filepath );
176+ rmdir ( $ tmp_dir );
177+ }
178+
179+ /**
180+ * Windows filenames cannot end in periods.
181+ *
182+ * @covers \WP_CLI\FileCache::validate_key
183+ *
184+ * @see https://github.com/wp-cli/wp-cli/pull/5947
185+ * @see https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions
186+ */
187+ public function test_validate_key_ending_in_period () {
188+ $ max_size = 32 ;
189+ $ ttl = 60 ;
190+ $ cache_dir = Utils \get_temp_dir () . uniqid ( 'wp-cli-test-file-cache ' , true );
191+ $ cache = new FileCache ( $ cache_dir , $ ttl , $ max_size );
192+
193+ $ key = 'plugin/advanced-sidebar-menu-pro-9.5.7. ' ;
194+
195+ $ reflection = new ReflectionClass ( $ cache );
196+
197+ $ method = $ reflection ->getMethod ( 'validate_key ' );
198+ $ method ->setAccessible ( true );
199+
200+ $ result = $ method ->invoke ( $ cache , $ key );
201+
202+ $ this ->assertStringEndsNotWith ( '. ' , $ result );
203+ $ this ->assertSame ( 'plugin/advanced-sidebar-menu-pro-9.5.7 ' , $ result );
204+ }
105205}
0 commit comments