Skip to content

Commit 0c5b9f1

Browse files
committed
Merge branch 'trunk' into trac-64406
* trunk: Filesystem API: Pass correct `$file` value to `pre_unzip_file` and `unzip_file` filters. Tests: Use `assertSame()` in some newly introduced tests.
2 parents df2b276 + a8630eb commit 0c5b9f1

File tree

5 files changed

+52
-50
lines changed

5 files changed

+52
-50
lines changed

src/wp-admin/includes/file.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,14 +1896,14 @@ function _unzip_file_pclzip( $file, $to, $needed_dirs = array() ) {
18961896
$uncompressed_size = 0;
18971897

18981898
// Determine any children directories needed (From within the archive).
1899-
foreach ( $archive_files as $file ) {
1900-
if ( str_starts_with( $file['filename'], '__MACOSX/' ) ) { // Skip the OS X-created __MACOSX directory.
1899+
foreach ( $archive_files as $archive_file ) {
1900+
if ( str_starts_with( $archive_file['filename'], '__MACOSX/' ) ) { // Skip the OS X-created __MACOSX directory.
19011901
continue;
19021902
}
19031903

1904-
$uncompressed_size += $file['size'];
1904+
$uncompressed_size += $archive_file['size'];
19051905

1906-
$needed_dirs[] = $to . untrailingslashit( $file['folder'] ? $file['filename'] : dirname( $file['filename'] ) );
1906+
$needed_dirs[] = $to . untrailingslashit( $archive_file['folder'] ? $archive_file['filename'] : dirname( $archive_file['filename'] ) );
19071907
}
19081908

19091909
// Enough space to unzip the file and copy its contents, with a 10% buffer.
@@ -1967,22 +1967,22 @@ function _unzip_file_pclzip( $file, $to, $needed_dirs = array() ) {
19671967
}
19681968

19691969
// Extract the files from the zip.
1970-
foreach ( $archive_files as $file ) {
1971-
if ( $file['folder'] ) {
1970+
foreach ( $archive_files as $archive_file ) {
1971+
if ( $archive_file['folder'] ) {
19721972
continue;
19731973
}
1974-
1975-
if ( str_starts_with( $file['filename'], '__MACOSX/' ) ) { // Don't extract the OS X-created __MACOSX directory files.
1974+
1975+
if ( str_starts_with( $archive_file['filename'], '__MACOSX/' ) ) { // Don't extract the OS X-created __MACOSX directory files.
19761976
continue;
19771977
}
19781978

19791979
// Don't extract invalid files:
1980-
if ( 0 !== validate_file( $file['filename'] ) ) {
1980+
if ( 0 !== validate_file( $archive_file['filename'] ) ) {
19811981
continue;
19821982
}
19831983

1984-
if ( ! $wp_filesystem->put_contents( $to . $file['filename'], $file['content'], FS_CHMOD_FILE ) ) {
1985-
return new WP_Error( 'copy_failed_pclzip', __( 'Could not copy file.' ), $file['filename'] );
1984+
if ( ! $wp_filesystem->put_contents( $to . $archive_file['filename'], $archive_file['content'], FS_CHMOD_FILE ) ) {
1985+
return new WP_Error( 'copy_failed_pclzip', __( 'Could not copy file.' ), $archive_file['filename'] );
19861986
}
19871987
}
19881988

tests/phpunit/tests/filesystem/unzipFilePclzip.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static function set_up_before_class() {
3737
*/
3838
public function test_should_apply_pre_unzip_file_filters() {
3939
$filter = new MockAction();
40-
add_filter( 'pre_unzip_file', array( $filter, 'filter' ) );
40+
add_filter( 'pre_unzip_file', array( $filter, 'filter' ), 10, 2 );
4141

4242
// Prepare test environment.
4343
$unzip_destination = self::$test_data_dir . 'archive/';
@@ -53,7 +53,8 @@ public function test_should_apply_pre_unzip_file_filters() {
5353
$this->rmdir( $unzip_destination );
5454
$this->delete_folders( $unzip_destination );
5555

56-
$this->assertSame( 1, $filter->get_call_count() );
56+
$this->assertSame( 1, $filter->get_call_count(), 'The filter should be called once.' );
57+
$this->assertSame( self::$test_data_dir . 'archive.zip', $filter->get_args()[0][1], 'The $file parameter should be correct.' );
5758
}
5859

5960
/**
@@ -63,7 +64,7 @@ public function test_should_apply_pre_unzip_file_filters() {
6364
*/
6465
public function test_should_apply_unzip_file_filters() {
6566
$filter = new MockAction();
66-
add_filter( 'unzip_file', array( $filter, 'filter' ) );
67+
add_filter( 'unzip_file', array( $filter, 'filter' ), 10, 2 );
6768

6869
// Prepare test environment.
6970
$unzip_destination = self::$test_data_dir . 'archive/';
@@ -79,6 +80,7 @@ public function test_should_apply_unzip_file_filters() {
7980
$this->rmdir( $unzip_destination );
8081
$this->delete_folders( $unzip_destination );
8182

82-
$this->assertSame( 1, $filter->get_call_count() );
83+
$this->assertSame( 1, $filter->get_call_count(), 'The filter should be called once.' );
84+
$this->assertSame( self::$test_data_dir . 'archive.zip', $filter->get_args()[0][1], 'The $file parameter should be correct.' );
8385
}
8486
}

tests/phpunit/tests/rest-api/wpRestAbilitiesV1CategoriesController.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@ public function test_get_item(): void {
171171
$this->assertEquals( 200, $response->get_status() );
172172

173173
$data = $response->get_data();
174-
$this->assertEquals( 'test-data-retrieval', $data['slug'] );
175-
$this->assertEquals( 'Data Retrieval', $data['label'] );
176-
$this->assertEquals( 'Abilities that retrieve and return data from the WordPress site.', $data['description'] );
174+
$this->assertSame( 'test-data-retrieval', $data['slug'] );
175+
$this->assertSame( 'Data Retrieval', $data['label'] );
176+
$this->assertSame( 'Abilities that retrieve and return data from the WordPress site.', $data['description'] );
177177
$this->assertArrayHasKey( 'meta', $data );
178178
}
179179

@@ -189,10 +189,10 @@ public function test_get_item_with_meta(): void {
189189
$this->assertEquals( 200, $response->get_status() );
190190

191191
$data = $response->get_data();
192-
$this->assertEquals( 'test-communication', $data['slug'] );
192+
$this->assertSame( 'test-communication', $data['slug'] );
193193
$this->assertArrayHasKey( 'meta', $data );
194194
$this->assertIsArray( $data['meta'] );
195-
$this->assertEquals( 'high', $data['meta']['priority'] );
195+
$this->assertSame( 'high', $data['meta']['priority'] );
196196
}
197197

198198
/**
@@ -212,8 +212,8 @@ public function test_get_item_with_selected_fields(): void {
212212

213213
$data = $response->get_data();
214214
$this->assertCount( 2, $data, 'Response should only contain the requested fields.' );
215-
$this->assertEquals( 'test-data-retrieval', $data['slug'] );
216-
$this->assertEquals( 'Data Retrieval', $data['label'] );
215+
$this->assertSame( 'test-data-retrieval', $data['slug'] );
216+
$this->assertSame( 'Data Retrieval', $data['label'] );
217217
}
218218

219219
/**
@@ -230,7 +230,7 @@ public function test_get_item_not_found(): void {
230230
$this->assertEquals( 404, $response->get_status() );
231231

232232
$data = $response->get_data();
233-
$this->assertEquals( 'rest_ability_category_not_found', $data['code'] );
233+
$this->assertSame( 'rest_ability_category_not_found', $data['code'] );
234234
}
235235

236236
/**
@@ -424,8 +424,8 @@ public function test_get_schema(): void {
424424
$this->assertArrayHasKey( 'schema', $data );
425425
$schema = $data['schema'];
426426

427-
$this->assertEquals( 'ability-category', $schema['title'] );
428-
$this->assertEquals( 'object', $schema['type'] );
427+
$this->assertSame( 'ability-category', $schema['title'] );
428+
$this->assertSame( 'object', $schema['type'] );
429429
$this->assertArrayHasKey( 'properties', $schema );
430430

431431
$properties = $schema['properties'];
@@ -438,7 +438,7 @@ public function test_get_schema(): void {
438438
$this->assertArrayHasKey( 'meta', $properties );
439439

440440
$slug_property = $properties['slug'];
441-
$this->assertEquals( 'string', $slug_property['type'] );
441+
$this->assertSame( 'string', $slug_property['type'] );
442442
$this->assertTrue( $slug_property['readonly'] );
443443
}
444444

tests/phpunit/tests/rest-api/wpRestAbilitiesV1ListController.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,10 @@ public function test_get_item(): void {
307307

308308
$data = $response->get_data();
309309
$this->assertCount( 7, $data, 'Response should contain all fields.' );
310-
$this->assertEquals( 'test/calculator', $data['name'] );
311-
$this->assertEquals( 'Calculator', $data['label'] );
312-
$this->assertEquals( 'Performs basic calculations', $data['description'] );
313-
$this->assertEquals( 'math', $data['category'] );
310+
$this->assertSame( 'test/calculator', $data['name'] );
311+
$this->assertSame( 'Calculator', $data['label'] );
312+
$this->assertSame( 'Performs basic calculations', $data['description'] );
313+
$this->assertSame( 'math', $data['category'] );
314314
$this->assertArrayHasKey( 'input_schema', $data );
315315
$this->assertArrayHasKey( 'output_schema', $data );
316316
$this->assertArrayHasKey( 'meta', $data );
@@ -334,8 +334,8 @@ public function test_get_item_with_selected_fields(): void {
334334

335335
$data = $response->get_data();
336336
$this->assertCount( 2, $data, 'Response should only contain the requested fields.' );
337-
$this->assertEquals( 'test/calculator', $data['name'] );
338-
$this->assertEquals( 'Calculator', $data['label'] );
337+
$this->assertSame( 'test/calculator', $data['name'] );
338+
$this->assertSame( 'Calculator', $data['label'] );
339339
}
340340

341341
/**
@@ -355,9 +355,9 @@ public function test_get_item_with_embed_context(): void {
355355

356356
$data = $response->get_data();
357357
$this->assertCount( 3, $data, 'Response should only contain the fields for embed context.' );
358-
$this->assertEquals( 'test/calculator', $data['name'] );
359-
$this->assertEquals( 'Calculator', $data['label'] );
360-
$this->assertEquals( 'math', $data['category'] );
358+
$this->assertSame( 'test/calculator', $data['name'] );
359+
$this->assertSame( 'Calculator', $data['label'] );
360+
$this->assertSame( 'math', $data['category'] );
361361
}
362362

363363
/**
@@ -374,7 +374,7 @@ public function test_get_item_not_found(): void {
374374
$this->assertEquals( 404, $response->get_status() );
375375

376376
$data = $response->get_data();
377-
$this->assertEquals( 'rest_ability_not_found', $data['code'] );
377+
$this->assertSame( 'rest_ability_not_found', $data['code'] );
378378
}
379379

380380
/**
@@ -389,7 +389,7 @@ public function test_get_item_not_show_in_rest(): void {
389389
$this->assertEquals( 404, $response->get_status() );
390390

391391
$data = $response->get_data();
392-
$this->assertEquals( 'rest_ability_not_found', $data['code'] );
392+
$this->assertSame( 'rest_ability_not_found', $data['code'] );
393393
}
394394

395395
/**
@@ -581,8 +581,8 @@ public function test_get_schema(): void {
581581
$this->assertArrayHasKey( 'schema', $data );
582582
$schema = $data['schema'];
583583

584-
$this->assertEquals( 'ability', $schema['title'] );
585-
$this->assertEquals( 'object', $schema['type'] );
584+
$this->assertSame( 'ability', $schema['title'] );
585+
$this->assertSame( 'object', $schema['type'] );
586586
$this->assertArrayHasKey( 'properties', $schema );
587587

588588
$properties = $schema['properties'];
@@ -745,7 +745,7 @@ public function test_filter_by_category(): void {
745745

746746
// Should only have math category abilities
747747
foreach ( $data as $ability ) {
748-
$this->assertEquals( 'math', $ability['category'], 'All abilities should be in math category' );
748+
$this->assertSame( 'math', $ability['category'], 'All abilities should be in math category' );
749749
}
750750

751751
// Should at least contain the calculator

tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ public function test_execute_destructive_ability_delete(): void {
472472
$response = $this->server->dispatch( $request );
473473

474474
$this->assertEquals( 200, $response->get_status() );
475-
$this->assertEquals( 'User successfully deleted!', $response->get_data() );
475+
$this->assertSame( 'User successfully deleted!', $response->get_data() );
476476
}
477477

478478
/**
@@ -630,7 +630,7 @@ public function test_contextual_permission_check(): void {
630630

631631
$response = $this->server->dispatch( $request );
632632
$this->assertEquals( 200, $response->get_status() );
633-
$this->assertEquals( 'Success: test data', $response->get_data() );
633+
$this->assertSame( 'Success: test data', $response->get_data() );
634634
}
635635

636636
/**
@@ -646,8 +646,8 @@ public function test_do_not_show_in_rest(): void {
646646

647647
$this->assertEquals( 404, $response->get_status() );
648648
$data = $response->get_data();
649-
$this->assertEquals( 'rest_ability_not_found', $data['code'] );
650-
$this->assertEquals( 'Ability not found.', $data['message'] );
649+
$this->assertSame( 'rest_ability_not_found', $data['code'] );
650+
$this->assertSame( 'Ability not found.', $data['message'] );
651651
}
652652

653653
/**
@@ -679,8 +679,8 @@ public function test_wp_error_return_handling(): void {
679679

680680
$this->assertEquals( 500, $response->get_status() );
681681
$data = $response->get_data();
682-
$this->assertEquals( 'test_error', $data['code'] );
683-
$this->assertEquals( 'This is a test error', $data['message'] );
682+
$this->assertSame( 'test_error', $data['code'] );
683+
$this->assertSame( 'This is a test error', $data['message'] );
684684
}
685685

686686
/**
@@ -698,7 +698,7 @@ public function test_execute_non_existent_ability(): void {
698698

699699
$this->assertEquals( 404, $response->get_status() );
700700
$data = $response->get_data();
701-
$this->assertEquals( 'rest_ability_not_found', $data['code'] );
701+
$this->assertSame( 'rest_ability_not_found', $data['code'] );
702702
}
703703

704704
/**
@@ -714,8 +714,8 @@ public function test_run_endpoint_schema(): void {
714714
$this->assertArrayHasKey( 'schema', $data );
715715
$schema = $data['schema'];
716716

717-
$this->assertEquals( 'ability-execution', $schema['title'] );
718-
$this->assertEquals( 'object', $schema['type'] );
717+
$this->assertSame( 'ability-execution', $schema['title'] );
718+
$this->assertSame( 'object', $schema['type'] );
719719
$this->assertArrayHasKey( 'properties', $schema );
720720
$this->assertArrayHasKey( 'result', $schema['properties'] );
721721
}
@@ -761,7 +761,7 @@ public function test_get_request_with_nested_input_array(): void {
761761
$this->assertEquals( 200, $response->get_status() );
762762

763763
$data = $response->get_data();
764-
$this->assertEquals( 'nested', $data['level1']['level2']['value'] );
764+
$this->assertSame( 'nested', $data['level1']['level2']['value'] );
765765
$this->assertEquals( array( 1, 2, 3 ), $data['array'] );
766766
}
767767

0 commit comments

Comments
 (0)