Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem
FieldUnitTestBase::createFieldWithInstance()
has a first$suffix
argument, which is used to dynamically create non-existing properties on the test class instance.- It's fine to provide easy DX/access to fields/instances/definitions that have been created for a test, but setting + relying on dynamic properties that do not exist is not.
History
- This code was invented in #1821906: Allow more Field API public API functions to act on a single field within an entity (and unfortunately backported to D7).
- #1932382: Use DrupalUnitTestBase for Field API tests. converted the test class into DUTB/KTB later on, but seemingly ignored that issue.
Proposed solution
-
Keep the DX idea (which is a fair one IMO), but move the dynamic properties into a single
$this->fields
bag.
API changes
Just in case we consider test base classes as "API"...
$this->createFieldWithInstance();
$this->createFieldWithInstance('_second');
...
-$this->assertTrue($this->field->id());
-$this->assertTrue($this->field_second->id());
+$this->assertTrue($this->fields->field->id());
+$this->assertTrue($this->fields->field_second->id());
Comment | File | Size | Author |
---|---|---|---|
#21 | 2306539.21.patch | 69.44 KB | swentel |
#17 | 2306539.16.patch | 69.53 KB | alexpott |
#17 | 11-16-interdiff.txt | 43.26 KB | alexpott |
#11 | drupal8.field-unit-test-base.11.patch | 69.65 KB | sun |
#11 | interdiff.txt | 7.61 KB | sun |
Comments
Comment #1
sunComment #2
sunSorry, blamed the wrong original issue; a more granular pickaxe revealed #1821906: Allow more Field API public API functions to act on a single field within an entity
Comment #3
sunComment #4
sunQuickEditTestBase::createFieldWithInstance()
is a custom re-implementation and has the same problem, so applying the same fix there.Comment #7
sunComment #9
sunRebased against HEAD.
Comment #10
andypostOverall it's great clean-up!
No doc blocks?
would be great to add comment here
Comment #11
sunSure. But re: 2) The $fields bag is verbosely documented on the property already; no need to repeat that.
Comment #12
andypostI think that's ready
Comment #13
swentel CreditAttribution: swentel commentedThe only confusing this for me is the name of the bag: $this->fields.
We've renamed field to field_storage in #2287727: Rename FieldConfig to FieldStorageConfig and hopefully soon will rename instance to field. So maybe just rename to $this->fields to $this->fieldBag or so ?
Comment #14
sunFor now, my immediate need (blocker) is to get rid of the arbitrary property overloading in those tests.
That's why I went with the most direct equivalent. The current thing in HEAD as well as in this patch is only marginally related to a field bag; it's a collection of arbitrary variables lumped together.
I'd totally +1 a follow-up issue to seek for a better and more appropriate redesign, but if possible, it would be great if we could do this as a first step.
Comment #15
alexpottYep it's not an array of fields either - how about fieldTestData - which is exactly what it is.
Comment #16
swentel CreditAttribution: swentel commentedI can live with fieldTestData as well. #2312093: Rename FieldInstanceConfig to FieldConfig is opened and I'll work on that next week, I'm fine with doing the rename in that issue, it's not that big.
Comment #17
alexpottLet's just do it right first time - less noise in #2312093: Rename FieldInstanceConfig to FieldConfig which will be plenty big already.
Comment #18
swentel CreditAttribution: swentel commentedOk +1 if green.
Comment #20
yched CreditAttribution: yched commentedAwesome cleanup. The ad-hoc dynamic properties in FieldUnitTestbase have long been an ugly hack.
(note: IIRC, this comes from far earlier than #1821906: Allow more Field API public API functions to act on a single field within an entity, was added in the very first days of D7 Field API...)
Curious to know why the patch uses an ArrayObject instead of just an array, but not a blocker.
RTBC +1.
Comment #21
swentel CreditAttribution: swentel commentedOk, alex, go ahead :)
Comment #22
sunAny chance to move forward here? :-)
Comment #23
sunClosely related: #2314123: Fix various tests
Comment #24
yched CreditAttribution: yched commented@sun: just curious, why did you use ArrayObject instead of just an array ?
Comment #25
sun@yched: No particular reason, really. The existing code accesses properties. Since this presents a critical blocker for my work, I tried hard to avoid any possible delays, and simply re-implemented the current DX/API/design, sans class property overloading.
FWIW, I do think that it (property access) looks more appealing. However, this entire construct is an Anti-API to begin with. As already mentioned in #14, I think we should rethink this test code/API from scratch in a separate follow-up issue.
Comment #26
yched CreditAttribution: yched commentedFully agreed, it always made me cringe a bit. Just never was enough of a priority :-/
I do understand the reasoning for this "good enough" cleanup step, thanks a lot for that @sun.
If no specific reason, I'd simply vote to go back to an array instead of an ArrayObject ? But no strong objection to getting this in as is.
Comment #27
webchickSince alexpott contributed to this one, seeing if maybe catch could review.
Comment #28
webchickActually, alexpott and I talked through this on IRC; this doesn't need to be reviewed by catch. It's already been looked at by several other folks, and the changes are confined to tests.
Therefore, committed and pushed to 8.x. Thanks!
Comment #30
sun