Problem

  1. FieldUnitTestBase::createFieldWithInstance() has a first $suffix argument, which is used to dynamically create non-existing properties on the test class instance.
  2. 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

  1. 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).
  2. #1932382: Use DrupalUnitTestBase for Field API tests. converted the test class into DUTB/KTB later on, but seemingly ignored that issue.

Proposed solution

  1. 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());
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

sun’s picture

Status: Active » Needs review
FileSize
46.95 KB
sun’s picture

sun’s picture

Issue summary: View changes
sun’s picture

QuickEditTestBase::createFieldWithInstance() is a custom re-implementation and has the same problem, so applying the same fix there.

The last submitted patch, 1: drupal8.field-unit-test-base.1.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 4: drupal8.field-unit-test-base.4.patch, failed testing.

sun’s picture

Status: Needs work » Needs review
FileSize
59.06 KB

Status: Needs review » Needs work

The last submitted patch, 7: drupal8.field-unit-test-base.7.patch, failed testing.

sun’s picture

Status: Needs work » Needs review
FileSize
63.61 KB

Rebased against HEAD.

andypost’s picture

Overall it's great clean-up!

  1. +++ b/core/modules/field/src/Tests/ConfigFieldDefinitionTest.php
    @@ -24,6 +24,9 @@ class ConfigFieldDefinitionTest extends FieldUnitTestBase {
    +  private $entityType;
    +  private $bundle;
    
    +++ b/core/modules/field/src/Tests/CrudTest.php
    @@ -26,6 +26,10 @@ class CrudTest extends FieldUnitTestBase {
    +  private $field;
    +  private $another_field;
    +  private $instance_definition;
    
    +++ b/core/modules/field/src/Tests/FieldValidationTest.php
    @@ -16,6 +16,10 @@
    +  private $entityType;
    +  private $bundle;
    +  private $entity;
    

    No doc blocks?

  2. +++ b/core/modules/field/src/Tests/FieldUnitTestBase.php
    @@ -24,10 +24,29 @@
    +    $this->fields = new \ArrayObject(array(), \ArrayObject::ARRAY_AS_PROPS);
    

    would be great to add comment here

sun’s picture

Sure. But re: 2) The $fields bag is verbosely documented on the property already; no need to repeat that.

andypost’s picture

Status: Needs review » Reviewed & tested by the community

I think that's ready

swentel’s picture

The 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 ?

sun’s picture

For 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.

alexpott’s picture

Yep it's not an array of fields either - how about fieldTestData - which is exactly what it is.

swentel’s picture

I 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.

alexpott’s picture

FileSize
43.26 KB
69.53 KB

Let's just do it right first time - less noise in #2312093: Rename FieldInstanceConfig to FieldConfig which will be plenty big already.

swentel’s picture

Ok +1 if green.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 17: 2306539.16.patch, failed testing.

yched’s picture

Awesome 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.

swentel’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
69.44 KB

Ok, alex, go ahead :)

sun’s picture

Component: simpletest.module » field system

Any chance to move forward here? :-)

sun’s picture

yched’s picture

@sun: just curious, why did you use ArrayObject instead of just an array ?

sun’s picture

@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.

yched’s picture

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

Fully 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.

webchick’s picture

Assigned: sun » catch

Since alexpott contributed to this one, seeing if maybe catch could review.

webchick’s picture

Assigned: catch » sun
Status: Reviewed & tested by the community » Fixed

Actually, 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!

  • webchick committed 0b0cdc2 on 8.0.x
    Issue #2306539 by swentel, alexpott, sun: Fixed FieldUnitTestBase::...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.