Problem/Motivation

ConfigEntityListTest fails currently with PostgreSQL as database backend.

This is related to #2235111: EntityQueryTest is fragile. EntityQuery does not do any sorting at the query-level so the results are arbitrary. It also limits to 50 records. EntityQuery should do a sort by default if we're going to rely on a specific sort in PHP as well. Config entities use weight. Or for change the implementation for Config entities only. Or change how ListBuilder uses EntityQuery.

Proposed resolution

Explicitly sort by entity id in EntityListBuilder::getEntityIds because that is what the Drupal application expects in tests and in practice. The list will be sorted by property at a later time by EntityListBuilder although this deserves a follow-up because a pager limiting entities returned might conflict with a sort by a different property later. A follow-up issue was created for discussing changing the SQL and PHP sort, #2447805: EntityListBuilder::getEntityIds sort by label.

- This passes EntityListBuilderTest(s) and ConfigEntityListTest on SQLite.
- The patch to use is #4: drupal-2443636-get-entity-ids-missing-sort-4.patch

Remaining tasks

None.

User interface changes

None.

API changes

This changes the behavior of getEntityIds method to return a sorted list where before the list was returned unsorted. On MySQL and MySQL-like databases systems "unsorted" actually meant "sorted by primary key" so that there is no difference on those platforms.

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Bug
Issue priority Critical: PostgreSQL Support
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

bzrudi71’s picture

Issue summary: View changes
mradcliffe’s picture

Issue summary: View changes
mradcliffe’s picture

Issue summary: View changes
Status: Active » Needs review

This is going with fixing getEntityIds to have a default sort. It makes sense to me that if I want entity ids via a pager, then the method should query by the default sort expected by the application and tests. The list builder classes will sort by an entity property later.

I think this deserves a follow-up DrupalWTF issue because as a developer expecting a truncated list to be sorted by weight doesn't make sense. I would want to sort either before the pager or return all entity ids and do the expected sort by weight.

This passes tests locally and may also solve other entity order by tests.

mradcliffe’s picture

Urk. I forgot to upload the patch. :(

bzrudi71’s picture

Ha... nice find! I spend a complete hour this morning to get this *$%"$" list builder doing what I expect from it - and yes, this may also help with other fails around.
Good work @mradcliffe!

Berdir’s picture

From a user perspective, wouldn't it be more useful to sort on the label instead of the ID?

daffie’s picture

Status: Needs review » Reviewed & tested by the community

It all looks good to me.
I can confirm that the test fails for postgreSQL and with the patch the test passes for postgreSQL.
So for me it is RTBC.

@Berdir: If you think that an other solution is better please say so.

amateescu’s picture

Status: Reviewed & tested by the community » Needs work

@daffie, that's exactly what he did?

I agree with #6, we should sort on label and fallback to ID if there's no 'label' entity key.

grom358’s picture

As per comment #6

grom358’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 9: drupal-2443635-entity-list-builder-missing-sort-9.patch, failed testing.

grom358’s picture

Sorting by label doesn't work because the sort is applied by Query line 79

      uasort($result, function($a, $b) use ($field, $direction) {
        return ($a[$field] <= $b[$field]) ? $direction : -$direction;
      });

This does not work as expected for text labels.

grom358’s picture

Status: Needs work » Reviewed & tested by the community

@mradcliffe patch resolves the issue in regards to resolving the critical. Core makes assumptions in numerous places that using entity id sort order. So I have moved that as followup issue to here #2447805: EntityListBuilder::getEntityIds sort by label and it goes beyond the scope of the this issue.

grom358’s picture

mradcliffe’s picture

Assigned: Unassigned » mradcliffe
Issue summary: View changes
Status: Reviewed & tested by the community » Needs review

Thanks for working on this and creating follow-up, @grom358. I fell ill again earlier this week, and have been out of it and not been able to respond or do much of anything at all.

I agree. The scope of this issue should be limited to fixing the critical since there may have been some decisions made with regard to sort order.

This also may help SQLite for which the specification says that "If a SELECT statement that returns more than one row does not have an ORDER BY clause, the order in which the rows are returned is undefined." I'll assign this to me to look run the list builder tests on SQLite in vagrant. If it passes, then I'll move back to RTBC.

mradcliffe’s picture

Assigned: mradcliffe » Unassigned
Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

Passes the same tests in SQLite.

mradcliffe’s picture

Priority: Major » Critical

Forgot to change priority. I think that's correct based on the meta issue.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed fd350fb and pushed to 8.0.x. Thanks!

  • alexpott committed fd350fb on 8.0.x
    Issue #2443635 by mradcliffe, grom358: PostgreSQL: Fix config\Tests\...
amateescu’s picture

Priority: Critical » Major

All the other child issues of the critical parent meta issue are major, this one should be the same.

Status: Fixed » Closed (fixed)

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