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

NEW: Validate the Title on Group is not empty #10113

Merged

Conversation

textagroup
Copy link
Contributor

You can currently create a new Group in the Security section of the CMS and leave the Title blank or fill it with only whitespace.

This pull requests adds some more validation on the Group DataObject as well as updating the GroupTest unit test.

@michalkleiner
Copy link
Contributor

Makes sense from my perspective, but I may not have all the history why it wasn't done in past.
@emteknetnz what's your view on this enhancement?

@emteknetnz
Copy link
Member

emteknetnz commented Oct 8, 2021

I don't know the history either. Seems sensible to me though. There's a semi-decent chance it'll break somewhere else if there's a $g = new Group(); $g->Code='my-code'; $g->write(); type of thing in a different module. Though given we just released 4.9.0 we've got plenty of time to fix any instances and they should show up in recipe-kitchen-sink unit testing fairly quickly.

My bigger concern though would be with projects, rather than modules, this they'd be might break on composer update

Possible change this from validation to onBeforeWrite instead? e.g. if (!trim($this->Title)) .. $this->Title = ucwords(str_replace('-', ' ', $this->Code));

@michalkleiner
Copy link
Contributor

Yeah, I think I view it the same, more concerned about existing project code that works with the Code field and doesn't bother about the Title.

I guess we could keep the validation and check if (!trim(Title) || !trim(Code)) .. raise the error, and also update the Title from the Code field as you suggested either in onBeforeWrite if it kept the validation on both fields functional, or in onAfterWrite using an extra write (which won't matter much since groups are not versioned).

@textagroup
Copy link
Contributor Author

textagroup commented Oct 8, 2021

I can make the changes in validation or onBeforeWrite, did come across another bug where you can duplicate the group codes by creating a Group with a previous title.

Might be an idea to consider addressing this in this pull request as well.

MariaDB [ss4vanilla]> SELECT ID, Title, Code FROM Group;
+----+-----------------+-----------------+
| ID | Title | Code |
+----+-----------------+-----------------+
| 1 | Content Authors | content-authors |
| 2 | Administrators | administrators |
| 3 | A Group | a-group |
| 4 | Content Authors | content-authors |
+----+-----------------+-----------------+
4 rows in set (0.001 sec)

@emteknetnz
Copy link
Member

Yes duplicate Title and/or Code are both bad, so catching those in validation would be helpful

@kinglozzer
Copy link
Member

If we’re worried about this breaking things like $g = new Group(); $g->Code='my-code'; $g->write();, why don’t we add this as form validation?

public function getCMSCompositeValidator(): CompositeValidator
{
    $validator = parent::getCMSCompositeValidator();

    $validator->addValidator(RequiredFields::create([
        'Title'
    ]));

    return $validator;
}

Someone could then still create a group with no title programatically if they like. Admittedly that won’t stop someone creating a group with a whitespace-only title, but if someone really wants to create a group with a name “ ” then why stop them? 😛

@michalkleiner
Copy link
Contributor

Nice one @kinglozzer!

If there's no field for code to manage via the CMS UI, should it act like URLSegment and automatically check if it's unique and append the -2, -3 to keep it unique?

@textagroup textagroup force-pushed the add-group-title-validation branch 4 times, most recently from 9c30c41 to bcaaf33 Compare October 11, 2021 20:41
NEW: Use trim to validate Group title

NEW: Stop duplicate and empty Group titles

NEW: Do not allow group codes to be duplicated
@textagroup
Copy link
Contributor Author

textagroup commented Oct 11, 2021

Changes made to validate Title (CMS) and handle duplicate group codes.
Had to amend SapphireTest->createMemberWithPermission by adding an extra check to see if a group exists before trying to create it.
This was not a issue before due to duplicate code/titles being allowed

Copy link
Contributor

@michalkleiner michalkleiner left a comment

Choose a reason for hiding this comment

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

Looks ok to me, thanks @textagroup.

@kinglozzer or @emteknetnz — can you give it another tick, too? Thanks!

@michalkleiner michalkleiner merged commit b8d37f9 into silverstripe:4 Nov 3, 2021
@kinglozzer
Copy link
Member

Looks like this has caused some build failures 😞 not sure why the PR build passed https://github.com/silverstripe/silverstripe-framework/runs/4087515550

@kinglozzer
Copy link
Member

kinglozzer commented Nov 4, 2021

I guess it’s because we now have to make the SapphireTest change in two places as a result of the PHPUnit 9 changes?

@kinglozzer kinglozzer mentioned this pull request Nov 4, 2021
@kinglozzer
Copy link
Member

Fix: #10136

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.

None yet

4 participants