In user_filter_form_submit(), we validate that the user selected a valid option. I believe this is unnecessary, as the Form API already does this validation for us.

function user_filter_form_submit($form, &$form_state) {
  // ...
      // Apply every filter that has a choice selected other than 'any'.
      foreach ($filters as $filter => $options) {
        if (isset($form_state['values'][$filter]) && $form_state['values'][$filter] != '[any]') {
          // Merge an array of arrays into one if necessary.
          $options = ($filter == 'permission') ? form_options_flatten($filters[$filter]['options']) : $filters[$filter]['options'];
          // Only accept valid selections offered on the dropdown, block bad input.
          if (isset($options[$form_state['values'][$filter]])) {
            $_SESSION['user_overview_filter'][] = array($filter, $form_state['values'][$filter]);
          }
        }
      }

The validation code dates to the original introduction of the user filtering functionality in #34505: admin/user filter.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

bfroehle’s picture

Status: Active » Needs review
FileSize
1.4 KB

Patch attached.

bfroehle’s picture

FileSize
60.18 KB

For the curious, I've attached the result of submitting an invalid item after applying this patch --- clearly the Form API is catching the invalid selection.

(The error, incidentally is the same without the patch applied, showing that the relevant sections of code never get called).

xjm’s picture

Looks sane to me. Is there already a test for this behavior?

xjm’s picture

Status: Needs review » Needs work
Issue tags: +Novice

Tagging as novice for the task of rerolling the Drupal 8.x patch on account of #22336: Move all core Drupal files under a /core folder to improve usability and upgrades.

If you need help rerolling this patch, you can come to core office hours or ask in #drupal-gitsupport on IRC.

bfroehle’s picture

Status: Needs work » Needs review
Issue tags: -Novice
FileSize
1.43 KB
bfroehle’s picture

Title: Remove unnecessary validation in user_filter_form_submit() » Remove unnecessary validation in user/node list filtering
FileSize
2.48 KB

node.module has a similar section of code which is also unnecessary! Adding that hunk into the patch as well.

xjm’s picture

Issue tags: +Needs tests

Nice! Two thoughts:

  1. There doesn't seem to be test coverage for this particular form selection, so let's add one.
  2. In the process of thinking about what the test should be, I realized the "illegal choice" error message is probably not correct. Edit: Never mind. It is actually the correct error here. It's just not the most informative help text, but that's a separate issue. :)
bfroehle’s picture

1. There's already test coverage for the page. See testUserAdmin. The removed sections of code cannot be tested for as an illegal choice is already detected in http://api.drupal.org/api/drupal/includes--form.inc/function/_form_valid...

xjm’s picture

What I'm saying is there are options that don't get selected in the test. The test just filters by status and then refines by type, so the particular path that the redundant code is for is never tested. There's no test that would fail if the redundant code weren't redundant. Edit: I'm referring to NodeAdminTestCase::testContentAdminPages(); I didn't look at the user form test yet.

bfroehle’s picture

And generically form.test already checks against input forgery (which I guess is what this code is trying to prevent).

xjm’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests

Hm, alright. :)

catch’s picture

Status: Reviewed & tested by the community » Fixed

I had a quick look in git to see where this was added but didn't come up with much for these specific hunks, however it looked like pre-FAPI to me. Amazing how much pre-FAPI code is still in core. Committed/pushed to 8.x.

bfroehle’s picture

I had a quick look in git to see where this was added but didn't come up with much for these specific hunks, however it looked like pre-FAPI to me.

As far as I can tell the code originated in #34505: admin/user filter and #29465: New drupal forms api. -- see 30a322e and 7e1527, for example.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.