fix: worklog filter period to mutation - #350
Merged
Merged
Conversation
jeppekroghitk
approved these changes
Sep 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Link to ticket
https://leantime.itkdev.dk/#/tickets/showTicket/8000
Description
On the invoice-entry worklog picker, the total hours could cover one day more than the
list shown beneath it whenever a "Periode til" was set.
WorklogRepository::createFilterDataQueryBuilder()did this:\DateTime::modify()mutates the receiver and returns it, so the assignment reads as ifit produced a new value but actually shifted the caller's filter object.
InvoiceEntryWorklogsFilterData::$periodTois a mutable\DateTime, andInvoiceEntryWorklogController::worklogs()builds the query twice against the samefilter instance:
The first call moves
periodToto D+1 and queriesstarted < D+1— correct, the whole ofday D. The second call then modifies the already shifted value to D+2 and queries
started < D+2, so the total silently picks up day D+1. The user sees a total thatincludes worklogs the list doesn't show, with no way to tell where the extra hours came
from.
The fix is one word — clone before modifying:
Tests
Both were written first and confirmed red against the old code:
testFindByFilterDataDoesNotMutatePeriodTo— the root cause. Deterministic, no fixturedependency: the repository must not mutate the filter it was handed.
testSumSelectableTimeSpentSecondsCoversTheSameDaysAsTheListedWorklogs— the symptom.Failed with
10800 is not identical to 3600, i.e. the total carried an extra hour-and-a-half worklog the list omitted.
The symptom test persists its own project and two worklogs on consecutive days, via a new
persistProjectWithWorklogs()helper. That's necessary rather than incidental:AppFixturesderives
startedfromk % 12 + 1/k % 28 + 1, which spaces each month's days fourapart, so no two fixture worklogs land on consecutive days and a shifted boundary has
nothing to pick up. The helper registers its rows for the existing
tearDown()cleanup,since nothing wraps these tests in a transaction.
Not in scope
createFilterDataQueryBuilder()has a second latent bug that this PR deliberately leavesalone. Both there (
:166-172) and insumSelectableTimeSpentSecondsByFilterData()(
:147), the "not billed" branch passes a bare string toandWhere():'worklog.isBilled = FALSE OR worklog.isBilled is NULL'DQL binds
ANDtighter thanOR, so with any other predicate present this parses as(project = :project AND … AND isBilled = FALSE) OR isBilled IS NULL— the right-hand sideis unqualified and matches null-billed worklogs from any project. It needs
$qb->expr()->orX(...). It can't be demonstrated against the current fixtures (everyfixture worklog has
is_billedset), so whether it bites depends on whether production hasnull rows:
SELECT COUNT(*) FROM worklog WHERE is_billed IS NULL;. Worth its own branch.Checklist