[Bugfix:Forum] Fix invalid date exception#12328
Conversation
williamjallen
left a comment
There was a problem hiding this comment.
How hard would it be to add a Cypress test for this case? Assuming it isn't overly difficult, it would be great to add one so this doesn't happen again.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #12328 +/- ##
============================================
- Coverage 21.66% 21.65% -0.01%
- Complexity 9639 9651 +12
============================================
Files 268 268
Lines 36226 36233 +7
Branches 487 487
============================================
- Hits 7847 7846 -1
- Misses 27896 27904 +8
Partials 483 483
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
|
@williamjallen I have updated the code to catch the specific I also added a regression test in |
|
@SohitBennett Please see failing CI. |
|
On the main branch, I was able to recreate the error by making a new discussion forum category, and modifying the date to be an invalid format. When that happens, the server fails to parse and returns the error message. On this branch, the error is caught, and correctly returned as JSON with status set to fail. However, in my testing, I discovered that a number of invalid dates are parsed and return a success, despite being impossible. These include: 2026-02-29 (leap day in a non leap year), 2026-02-31 (nonexistent day in month), 2026-02-0 (day 0 in month), 2026-0-1 (month 0 in year), 0-2-1 (year far too small), 1000000-2-1 (year far too big). This is due to the DateTime constructor in PHP automatically converting invalid dates to correct ones. Since this could cause confusion for the user, I recommend using the date dropdown for new categories everywhere a user can enter a date, to limit the possible issues this could cause. |
8cfeb58 to
aa40aec
Compare
aa40aec to
4127cb0
Compare
|
Hi @williamjallen, I've rebased on main and addressed all the feedback from earlier. The Cypress test is included and the CI lint issues are resolved. Could you re-review when you get a chance? Also, the workflows need maintainer approval to run. Thanks! |
|
Please update your description to use our PR description template: |
| cy.contains('Edit Categories').click(); | ||
|
|
||
| cy.get('.edit-category-date-button').first().click(); | ||
|
|
||
| cy.get('.edit-category-date-input').should('be.visible').first().as('dateInput'); | ||
|
|
||
| cy.get('@dateInput').clear({ force: true }); | ||
| cy.get('@dateInput').type('invalid-date', { force: true }); | ||
|
|
||
| cy.get('.save-date-button').first().click(); | ||
|
|
||
| cy.contains('Invalid date format provided.').should('be.visible'); |
There was a problem hiding this comment.
Instead of getting ids or classes, you should add data-testid attributed wherever needed, and get those.
| cy.get('@dateInput').clear({ force: true }); | ||
| cy.get('@dateInput').type('invalid-date', { force: true }); |
There was a problem hiding this comment.
Force should be used as a last resort, is there another way to do this without forcing it ?
|
|
||
| cy.get('.save-date-button').first().click(); | ||
|
|
||
| cy.contains('Invalid date format provided.').should('be.visible'); |
There was a problem hiding this comment.
Can you check for a specific element having that message, instead of just any element?
|
Hey, made the changes that were requested switched to data-testid selectors, removed the force: true, and scoped the error check to the popup-message element. Also updated the PR description. Let me know if anything else needs fixing! |
|
|
||
| it('Should handle invalid date format when editing categories', () => { | ||
| cy.get('[data-testid="more-dropdown"]').click(); | ||
| cy.contains('Edit Categories').click(); |
There was a problem hiding this comment.
Is there a testid for this too ?
| beforeEach(() => { | ||
| cy.login('instructor'); | ||
| cy.visit(['sample', 'forum']); | ||
| cy.get('#nav-sidebar-collapse-sidebar').click(); |
|
closed due to inactivity |
Why is this Change Important & Necessary?
Fixes #12327
Forum categories currently accept invalid date formats without proper
validation feedback. This adds server-side validation and a Cypress
end-to-end test to prevent regressions.
What is the New Behavior?
When an instructor edits a forum category date and enters an invalid
format, the server returns an error and a toast message
"Invalid date format provided." is displayed.
What steps should a reviewer take to reproduce or test the bug or new feature?
Automated Testing & Documentation
forums.spec.jscovering invalid date inputdata-testidattributes to forum category date editing elements inShowCategories.twigOther information