Problem/Motivation

I've configured my site to not use a field prefix. EntityInheritRemoveSystemFields::filterFields() has field_ hardcoded so this module fails to sync field values.

Steps to reproduce

Change the default field prefix (#1393094: Make the field prefix in Field ui configurable), create a field on a content type, and then edit and save a node which should inherit.

Proposed resolution

Switch to using the contents of 'field_storage_config' for comparison which is the list of fields displayed on /admin/reports/fields

Remaining tasks

None

User interface changes

None

API changes

None

Data model changes

None

Comments

ericras created an issue. See original summary.

ericras’s picture

alberto56’s picture

Status: Active » Needs work

Setting to needs work based on the following issues.

If you have Docker installed on your development machine and you are using Mac or Linux, you can do the following:

(Please make sure to apply your patch to the latest dev version of the code, I made a change tonight)

First issue: dependency injection

./scripts/php-static-analysis.sh 

  Line   entity_inherit/src/Plugin/EntityInheritPlugin/EntityInheritRemoveSystemFields.php  
 ------ ----------------------------------------------------------------------------------- 
  25     \Drupal calls should be avoided in classes, use dependency injection               
         instead                                                                            

So the fix to this should be rather simple: instead of using

\Drupal::entityTypeManager()

You can use

$app->getEntityTypeManager()

In this case $app is an argument to the application singleton, which has entityTypeManager injected into it as a service.

Second issue: automated unit tests

Again, if you can docker, you can run:

./scripts/php-unit.sh

...
Error: Class "Drupal" not found
...

So this has to do with the fact that \Drupal is hard-coded and not dependency-injected.

We use a version of PHPUnit which has no knowledge of Drupal, so we need to mock everything which is related to Drupal.

The actual unit test for EntityInheritRemoveSystemFields::filterFields() is in tests/src/Unit/Plugin/EntityInheritPlugin/EntityInheritRemoveSystemFieldsTest.php. What I would do is wrap your check for whether a field is valid or not within another method, say $this->fieldExists() or something, then mock that in the unit test using the technique described in https://blog.nona.digital/mocking-in-phpunit/.

Finally, all tests will need to pass when you have Docker running and you run:

./scripts/ci.sh

before we commit the patch. Thanks!