Needs work
Project:
Drupal core
Version:
main
Component:
entity system
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
30 Sep 2016 at 16:30 UTC
Updated:
14 Oct 2024 at 15:10 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
blazey commentedAttaching patch. Let's see if it breaks anything.
Comment #3
berdirI don't thin this works anyway, isn't changed using REQUEST_TIME?
Comment #4
blazey commentedYep, that one is but EntityTestMulChanged uses a dedicated
changedfield typeDrupal\entity_test\Plugin\Field\FieldType\ChangedTestItem.Comment #6
blazey commentedCounter had been reinitialized each time an entity was created. Attached patch moves it to a static member.
Comment #7
blazey commentedComment #8
blazey commentedComment #9
berdirCould we use the new time service and actually fake the current time in ChangedItem instead of all those hacks?
https://www.drupal.org/node/2785211
Comment #10
blazey commentedAttached patch
ChangedTestItemfield type.ChangedItemfield type use REQUEST_TIME from time service.datetime.timeservice in entity_test module.EntityTestMulChanged::save()instead of waiting a second.How about that?
Comment #12
blazey commentedContentEntityChangedTestshould pass now.Comment #13
mpdonadioWe are doing this to avoid a BC if anything has CreatedItem (instead of injecting it)? There is also another explcit use of the static service locator, which I assume is for the same reason?
Do we explicitly add the interface when we extend something? Don't recall.
Overall I am very happy to see this being done. We added the time service to both abstract this (to allow different time sources), and to allow for predictable testing.
Right now, the class still relies on the initial request time, and then lets you bump it. I think it would be better to add this class to the testing system in general, and have everything be set explicitly to make it deterministic and also allow us to reuse this to set up different time scenarios (like force the time to be between DST fallback).
I'll play with this over the weekend.
Comment #14
berdirRe 1: There is a more obvious reason. typed data/field type plugins currently simply don't properly support dependency injection.
Comment #15
mpdonadioThis is what I was thinking about...
Comment #17
mpdonadioLet's regenerate that patch. Don't know why some of the new files are there, but not the new service.
Comment #20
blazey commented@mpdonadio would you be so kind to make the patch green again? ;)
Comment #27
amateescu commentedAdding credit to @BramDriesen and @valthebald for their work in #2903549: Replace REQUEST_TIME with time service in CreatedItem.
Comment #28
andypostit should use injection of $this->timeService
TimeTestHelper::class
The "else" is useless here
Comment #29
sam152 commentedUploading a patch from #3064727: Remove the REQUEST_TIME constant from ChangedItem and CreatedItem and delete ChangedTestItem in favour of a mock programmable time service, which was a duplicate of this issue. Quoting the IS there:
Personally I prefer controlling time in the test cases themselves, doing so means you could additionally test things that are happening at the same time. I also probably prefer installing the service in the test case as well, instead of assuming it should be installed for
entity_test. Also means other test cases can use the same service, and we're mocking less actual implementation when it's not required.Comment #30
andypostI looks weird to see \Drupal accessed for the same service twice, at least protected method makes sense, otoh it could add optional dependency on this service and make it required in 9.x
Same as above, as this class parent the service should be injected here
Comment #31
mpdonadioI thought FieldItems still don't support proper DI (see comments early in thread), or am I forgetting something?
Comment #32
andypostYes, that's why I'd better add protected getter, so child classes can reuse
Comment #33
sam152 commentedThanks for the review! How about something like this?
Comment #35
sam152 commentedComment #36
amateescu commentedHere and in the other test classes that use this pattern: we should register the testing time class by overriding
\Drupal\KernelTests\KernelTestBase::register(), and then set the local variable with$this->time = $this->container->get('datetime.time');.Maybe we should mention here that the time can be advanced manually for testing purposes?
This should reference
\Drupal\Component\Datetime\Time::getRequestTime()I think :)TestTimeService -> TestTime.
Shouldn't we also provide the same "manual advance" feature for
getRequestMicroTime()?Needs a "Defaults to 1 second." at the end.
Comment #37
drunken monkeyI can’t find any reference, one way or the other, at the moment, but I think the suggested first line is actually
Constructs a new class instance.This ensures that it will also be applicable for child classes.I disagree. I’m pretty sure it’s standard to omit this where the default is clear from the method header. (See here (last sentence).)
Comment #38
gogowitsch commentedThere is a contrib module datetime_testing. We use it for integration testing that needs arbitrary time jumps.
Comment #40
raman.b commentedRe-rolling for 9.1.x. Also addresses #36 and #37
Comment #41
hchonov@raman.b, please first upload a re-rolled patch and then a second one for addressing comments. We do not need a diff that shows the re-roll result, but the changes made when addressing comments. Both patches then could be uploaded in a single comment.
Comment #43
golddragon007 commented@hchonov here is the files, for me, it looks fine.
There's no need for rerolling for 9.2.x, it's applied fine.
I have just one note, that I can't agree to remove the parameters of the constructor `public function __construct(RequestStack $request_stack)` if somebody uses it as a parent class, they won't be able to populate properly the `$request_stack`. Don't use the Time class as a parent class (because technically you doesn't need anything from it, just the interface), or construct fully the object.
Comment #45
golddragon007 commented1. Fix failing test on 9.2.x.
2. Change parent object (Time) to the interface (TimeInterface).
Can somebody find a place where the `datetime.time` can be overwritten globally in the tests? Otherwise, it will be a pain to always override the registry... (well if it's needed actually)
Comment #46
golddragon007 commentedComment #48
kristen polPatch no longer applies cleanly to 9.3:
Comment #51
dpiRe-rolled against 9.3.x, resolving conflicts from #3131281: Replace assertEqual() with assertEquals(). and pushed as a MR.
MR created w dogit:
Comment #53
kristen polMR(patch) applies with offsets to versions 9.3, 9.4, and 10.
Comment #54
kristen polOne minor thing... not sure why some asserts are this order:
while others are reversed...
Since they are
assertEquals, it does matter, but it would be nice if things were consistent.Comment #57
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #58
kerasai commentedUpdated patch for 9.5.x, we'll see if the test bot can apply it to 10.x.
Comment #59
borisson_This change looks great, we need to update phpstan base level though.
Comment #60
pooja saraah commentedFixed failed commands on #58
Attached patch against Drupal 10.1.x
Comment #61
pooja saraah commentedComment #62
bramdriesenPatch does not apply. Needs a rebase I think.
Comment #63
mrinalini9 commentedRerolled patch #60, please review it.
Comment #64
borisson_Will just repeat #59
> This change looks great, we need to update phpstan base level though.
Comment #66
kerasai commentedNoting that the functional changes from the patches in this issue have been incorporated into what looks like all supported versions of core via #3112295: Replace REQUEST_TIME in rest of OO code (except for tests) and #3112283: Replace REQUEST_TIME in non-OO and non-module code. The remainder of the changes are pertinent only to running core's tests, otherwise patching likely is not necessary.