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

FIX Sort without specifying a table name #10675

Merged

Conversation

GuySartorelli
Copy link
Member

Using a table name in sort() is not allowed in CMS 5. We could use orderBy() here but member is the table it will sort on by default anyway so there's no need.

Also added unit tests, which should have caught this ages ago.

Parent issue

* CMS permissions.
* @return Map Returns a map of all members in the groups given that
* have CMS permissions.
*/
public static function mapInCMSGroups($groups = null)
public static function mapInCMSGroups(SS_List|array|null $groups = null): Map
Copy link
Member Author

Choose a reason for hiding this comment

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

We may as well typehint this - the PHPDoc was wrong before anyway.
I checked and in kitchen sink there's no uses of this method passing in arguments anyway so nothing else for us to change.

if (!$groups) {
if ($groups === null) {
Copy link
Member Author

@GuySartorelli GuySartorelli Jan 31, 2023

Choose a reason for hiding this comment

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

Now that the param is typehinted we can be more specific, which will reduce potential errors and just makes it a little clearer what we intend.

if (count($groups ?? []) == 0) {
if (count($groups) === 0) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Leaving this behaviour alone - just tidying up a little ($groups is never not countable here, and === is more correct)

return $members->sort('"Member"."Surname", "Member"."FirstName"')->map();
return $members->sort('"Surname", "FirstName"')->map();
Copy link
Member Author

Choose a reason for hiding this comment

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

This is, ultimately, the fix this PR is for.

Copy link
Member

Choose a reason for hiding this comment

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

Is this a legit non raw sql sort string? It doesn't match any of the examples in https://github.com/silverstripe/silverstripe-framework/blob/5/src/ORM/DataList.php#L324. None of the examples use double quotes.

If it is legit, then add it to the list of examples in DataList. Otherwise changes this to match one of the other examples

Copy link
Member Author

Choose a reason for hiding this comment

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

It works, so I don't see how it wouldn't be "legit". If it shouldn't work it should explicitly not work.
The validateSortColumn method does explicitly ignore double quotes. I'll add it to the examples I guess but I don't think we need to provide an exhaustive list there.

I'm not for example going to duplicate EVERY SINGLE EXAMPLE there so it shows with and without double quotes. I'll just dupe the first one.

Copy link
Contributor

Choose a reason for hiding this comment

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

Were double quotes more a Postgres thing? If so, we could drop them here and not change the examples. But since this is already merged.. nvm.

Copy link
Member Author

@GuySartorelli GuySartorelli Feb 1, 2023

Choose a reason for hiding this comment

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

Yes, but the argument max made when we decided to drop official support for postgres was that we should still add double quotes so that a community-maintained postgres module would still work.

I don't think that matters for this sort method (I suspect the lower levels of the ORM add quotes to it where needed)... but if it works now it's easier to let it keep working than to willfully break it.

Comment on lines +61 to +62
FirstName: Staff
Surname: User
Copy link
Member Author

Choose a reason for hiding this comment

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

I've confirmed that this doesn't affect any other tests - but it is necessary for the new one, or the value in the map will just be whitespace.


public function testMapInCMSGroupsNoLeftAndMain()
{
if (class_exists(LeftAndMain::class)) {
Copy link
Member

Choose a reason for hiding this comment

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

LeftAndMain will always exist in practice, as silverstripe/admin is installed as part of silverstripe/installer in CI

I guess this is still a required test due to the logic in Member::mapInCMSGroups() .. though :/

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah it won't run the way our CI is currently set up - there are likely other tests like that too. I'd like to keep it and if we make upgrades to our CI in the future to let framework run without installer then it'll be run.

return $members->sort('"Member"."Surname", "Member"."FirstName"')->map();
return $members->sort('"Surname", "FirstName"')->map();
Copy link
Member

Choose a reason for hiding this comment

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

Is this a legit non raw sql sort string? It doesn't match any of the examples in https://github.com/silverstripe/silverstripe-framework/blob/5/src/ORM/DataList.php#L324. None of the examples use double quotes.

If it is legit, then add it to the list of examples in DataList. Otherwise changes this to match one of the other examples

$this->assertSame($expectedValue, $result->values());
}

public function mapInCMSGroupsProvider()
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
public function mapInCMSGroupsProvider()
public function provideMapInCMSGroups()

Current convention is to change the leading word 'test' to 'provide' so it's easy to match the test and the provider, they have the same casing

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

}

/**
* @dataProvider mapInCMSGroupsProvider
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* @dataProvider mapInCMSGroupsProvider
* @dataProvider provideMapInCMSGroups

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@GuySartorelli GuySartorelli force-pushed the pulls/5/fix-member-sort branch from 21d7c91 to 8df5141 Compare February 1, 2023 00:17
@GuySartorelli
Copy link
Member Author

The unit tests pass for me locally with kitchen sink installed.... it seems like there might be some extension which changes the Title value in kitchen sink. I'll test against the IDs of the members instead since all that matters is that they're the right members, not what the title of the member is.

Using a table name in sort() is not allowed in CMS 5. We could use
orderBy() here but member is the table it will sort on by default anyway
so there's no need.

Also added unit tests, which should have caught this ages ago.
@GuySartorelli GuySartorelli force-pushed the pulls/5/fix-member-sort branch from 8df5141 to e3f8fc2 Compare February 1, 2023 00:26
@emteknetnz emteknetnz merged commit 8260280 into silverstripe:5 Feb 1, 2023
@emteknetnz emteknetnz deleted the pulls/5/fix-member-sort branch February 1, 2023 00:52
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