Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added check for case sensitive duplicated file names #711

Merged
merged 18 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions includes/Checker/Checks/Plugin_Repo/File_Type_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,49 @@
);
}
}

// Duplicated names in different folders.
$folders = array();
foreach ( $files as $file ) {
$folder = str_replace( basename( $file ), '', $file );
if ( empty( $folder ) ) {
continue;
}
$folders[] = $folder;

Check warning on line 313 in includes/Checker/Checks/Plugin_Repo/File_Type_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Repo/File_Type_Check.php#L313

Added line #L313 was not covered by tests
}
$folders = array_unique( $folders );
$folders_lowercase = array_map( 'strtolower', $folders );
$duplicated_folders = array_unique( array_diff_assoc( $folders_lowercase, array_unique( $folders_lowercase ) ) );

if ( ! empty( $duplicated_folders ) ) {
$this->add_result_error_for_file(
$result,
__( 'Duplicated folders names are not permitted.', 'plugin-check' ),
'duplicated_folders',
implode( ', ', $duplicated_folders ),
0,
0,
'',
8
);

Check warning on line 329 in includes/Checker/Checks/Plugin_Repo/File_Type_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Repo/File_Type_Check.php#L320-L329

Added lines #L320 - L329 were not covered by tests
}

// Duplicated names in same folder.
$files_lowercase = array_map( 'strtolower', $files );
$duplicated_files = array_unique( array_diff_assoc( $files_lowercase, array_unique( $files_lowercase ) ) );

if ( ! empty( $duplicated_files ) ) {
$this->add_result_error_for_file(
$result,
__( 'Duplicated file names are not permitted.', 'plugin-check' ),
'duplicated_files',
implode( ', ', $duplicated_files ),
0,
0,
'',
8
);

Check warning on line 346 in includes/Checker/Checks/Plugin_Repo/File_Type_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Repo/File_Type_Check.php#L337-L346

Added lines #L337 - L346 were not covered by tests
}
}

/**
Expand Down
42 changes: 42 additions & 0 deletions tests/phpunit/tests/Checker/Checks/File_Type_Check_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,48 @@ public function test_run_with_badly_named_errors() {
$this->assertCount( 1, wp_list_filter( $errors['badly|file%name!@#$%^&*()+=[]{};:"\'<>,?|`~.php'][0][0], array( 'code' => 'badly_named_files' ) ) );
}

public function test_run_with_duplicated_named_errors() {
// Initialize the Check_Context with a plugin path that mimics the directory structure.
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-file-type-badly-named-files-errors/load.php' );

// Create an empty Check_Result instance for this context.
$check_result = new Check_Result( $check_context );

// Initialize the File_Type_Check instance.
$check = new File_Type_Check();

// Use reflection to make protected method accessible.
$reflection = new ReflectionClass( $check );
$check_files_method = $reflection->getMethod( 'look_for_badly_named_files' );
$check_files_method->setAccessible( true );

// Define the custom file list with duplicate names as they would appear in a plugin directory.
$custom_files = array(
UNIT_TESTS_PLUGIN_DIR . 'test-plugin-file-type-badly-named-files-errors/custom-file.php',
UNIT_TESTS_PLUGIN_DIR . 'test-plugin-file-type-badly-named-files-errors/Custom-File.php',
UNIT_TESTS_PLUGIN_DIR . 'test-plugin-file-type-badly-named-files-errors/custom-FILE.php',
);

// Invoke method with the Check_Result instance and custom file list.
$result = $check_files_method->invoke( $check, $check_result, $custom_files );

$errors = $check_result->get_errors();

$this->assertCount( 1, wp_list_filter( $errors['custom-file.php'][0][0], array( 'code' => 'duplicated_files' ) ) );

// Define the custom file list with duplicate folder names as they would appear in a plugin directory.
$custom_files = array(
UNIT_TESTS_PLUGIN_DIR . 'test-plugin-file-type-badly-named-files-errors/sub directory/file-1.php',
UNIT_TESTS_PLUGIN_DIR . 'test-plugin-file-type-badly-named-files-errors/Sub Directory/file-2.php',
);

// Invoke method with the Check_Result instance and custom file list.
$result = $check_files_method->invoke( $check, $check_result, $custom_files );
$errors = $check_result->get_errors();

$this->assertCount( 1, wp_list_filter( $errors['sub directory/'][0][0], array( 'code' => 'duplicated_folders' ) ) );
}

public function test_run_with_library_core_errors() {
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-file-type-library-core-errors/load.php' );
$check_result = new Check_Result( $check_context );
Expand Down
Loading