Skip to content

Feature: Add tests for wp_removable_query_args#7731

Closed
Debarghya-Banerjee wants to merge 1 commit intoWordPress:trunkfrom
Debarghya-Banerjee:feature/add-tests-for-wp_removable_query_args
Closed

Feature: Add tests for wp_removable_query_args#7731
Debarghya-Banerjee wants to merge 1 commit intoWordPress:trunkfrom
Debarghya-Banerjee:feature/add-tests-for-wp_removable_query_args

Conversation

@Debarghya-Banerjee
Copy link
Copy Markdown

Trac Ticket: Core-53651

Overview

  • This pull request introduces unit tests for the wp_removable_query_args function to ensure that it behaves as expected in different scenarios. Specifically, the tests verify the following key behaviors:

  • Return Type: Ensures the function returns an array.

  • Non-Empty Array: Confirms that the array is not empty.

  • Filter Functionality: Verifies that custom filters applied to the function modify the returned array as expected.

  • Reverting to Original State: Ensures that the array reverts to its original state after the custom filter is removed.

Added Test Cases:

  • test_should_return_array_when_called: Confirms that the function returns an array.

  • test_should_return_non_empty_array_when_called: Verifies that the returned array is not empty.

  • test_should_modify_array_when_filter_applied: Ensures that a custom filter correctly modifies the array of query arguments.

  • test_should_revert_to_original_array_when_filter_removed: Checks that the array reverts to its original state after the custom filter is removed.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Nov 5, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props debarghyabanerjee.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Nov 5, 2024

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@Debarghya-Banerjee
Copy link
Copy Markdown
Author

Hi @SergeyBiryukov , can you please take a look into this PR. Thanks.

Copy link
Copy Markdown

@apermo apermo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some WPCS fixes. And some comments just state the obvious and could imho be removed.

* @ticket 59938
*/
public function test_should_modify_array_when_filter_applied() {
// Add a custom query argument using a filter
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Add a custom query argument using a filter
// Add a custom query argument using a filter.

WPCS fix.

Or omit this comment, it is just stating the obvious.


$result = wp_removable_query_args();

// Assert that the custom argument is in the array
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Assert that the custom argument is in the array
// Assert that the custom argument is in the array.

WPCS fix.

Or omit this comment, it is just stating the obvious.

// Assert that the custom argument is in the array
$this->assertContains( 'custom_arg', $result, 'The filter did not modify the array as expected.' );

// Remove the filter after the test
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Remove the filter after the test
// Remove the filter after the test.

WPCS fix.

Or omit this comment, it is just stating the obvious.

return $args;
};

// Apply the custom filter
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Apply the custom filter
// Apply the custom filter.

WPCS fix.

Or omit this comment, it is just stating the obvious.

// Apply the custom filter
add_filter( 'removable_query_args', $custom_filter_callback );

// Get the array with the filter applied
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Get the array with the filter applied
// Get the array with the filter applied.

WPCS fix.

Or omit this comment, it is just stating the obvious.

$result_with_filter = wp_removable_query_args();
$this->assertContains( 'custom_arg', $result_with_filter, 'The filter did not add the custom argument.' );

// Now, remove the custom filter
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Now, remove the custom filter
// Now, remove the custom filter.

WPCS fix.

Or omit this comment, it is just stating the obvious.

// Now, remove the custom filter
remove_filter( 'removable_query_args', $custom_filter_callback );

// Get the array after the filter is removed
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Get the array after the filter is removed
// Get the array after the filter is removed.

WPCS fix.

// Get the array after the filter is removed
$result_after_filter_removed = wp_removable_query_args();

// The original array should not contain 'custom_arg'
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// The original array should not contain 'custom_arg'
// The original array should not contain 'custom_arg'.

WPCS fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants