EntityViewsDataTest tests the behaviour of the EntityViewsData class.

However, because it's a unit test, the test is very close to the implementation. All the services the class expects have to be mocked very precisely.

This means that issues that need to fix problems in EntityViewsData which would require a change to either which services are injected, or what is called on those services, are stuck, because these changes to EntityViewsData have knock-on effects on the test. It's a very complicated class, and a very big test (~1200 lines!), with LOTS of mocking, and so it's a pretty horrendous task. Also, having to change both the SUT and the test at the same time is not a good thing -- because it pretty much invalidates the tests, as they can't tell you whether you've broken anything.

Some of the issues stuck by this include:

- #2930736: EntityViewsData assumes BaseFieldDefinitions where it should use FieldDefinitionInterface
- #2898635: bundleFieldDefinitions() are not added in EntityViewsData
- #2981047: Allow adding computed bundle fields in Views

I propose that we rewrite EntityViewsDataTest as a kernel test. This would mean we remove all the mocking. We define entity types, and let the entity system pass that to EntityViewsData, and test the output. We then have a test that isn't tied to the implementation details of the class.

Issue fork drupal-3116481

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

joachim created an issue. See original summary.

joachim’s picture

As this is going to be a lot of work, would be good to get a green light from a subsystem maintainer before starting work.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

dawehner’s picture

I just had a look at the class and the test. Much like the class has a bit of logic, I believe the complexity is really in the integration level.
For this a kernel test makes definitely way more sense.

In doubt this might require setting up additional test entity types, but I think the clarity and more important the resilience would be certainly improved, when the test is a kernel test

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

joachim’s picture

Assigned: Unassigned » joachim
joachim’s picture

I'm making good progress on this.

- much less mocking to do, so much less code!
- the unit test actually uses entity type definitions from the entity_test module, which I find a bit weird... it's actually pretty simple to put the definitions in the test
- found a bug in the test!

    $this->assertEquals([
      'left_field' => 'id',
      'field' => 'entity_id',
      'extra' => [[
          'field' => 'deleted',
          'value' => 0,
          'numeric' => TRUE,
        ],
      ],
      // This is wrong! On a live site with the same entity type and fields enabeld, it joins to entity_test_mul_property_data!
    ], $data['entity_test_mul__string']['table']['join']['entity_test_mul']);

joachim’s picture

Status: Active » Needs review

Done!

daffie made their first commit to this issue’s fork.

daffie’s picture

Status: Needs review » Needs work

The test is changed to a kernel test. Great!
Now it needs a bit of love to get it committed.

joachim’s picture

Status: Needs work » Needs review

Thanks for your help with making the tests pass!

> Why add this todo? Can we not just do it? It looks simple enough
> Can we change this to an assertTrue().
> Can we change this to an assertArrayNotHasKey().

Because this is a test conversion, I think that the new test class needs to be as similar as possible to the unit test class we're removing, to help reviewers understand the changes and see clearly that the new test is still covering all the same things.

Changing the type of assertions will add a lot of noise to the diff and possibly make git diff stop seeing it as as a file rename, which will then make reviewing even harder.

I agree with the suggested changes to the assertions, but I think they should be done in a follow-up.

daffie’s picture

Status: Needs review » Reviewed & tested by the community
Related issues: +#3225692: Cleanup the EntityViewsDataTest

All right lets do the cleanup in a followup. For that I have created #3225692: Cleanup the EntityViewsDataTest.
All code changes look good to me.
The patch fixes the problem from the IS.
For me it is RTBC.

joachim’s picture

Removing the tag, as that was addressed in #5.

owenbush’s picture

Issue summary: View changes

Adding #2981047: Allow adding computed bundle fields in Views as blocked in the issue description

alexpott’s picture

Version: 9.3.x-dev » 9.2.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and pushed 47c2f348cf to 9.3.x and eabee5c4c8 to 9.2.x. Thanks!

Backported to 9.2.x as this is only a test.

  • alexpott committed 47c2f34 on 9.3.x
    Issue #3116481 by joachim, daffie: Convert EntityViewsDataTest from a...

  • alexpott committed eabee5c on 9.2.x
    Issue #3116481 by joachim, daffie: Convert EntityViewsDataTest from a...

Status: Fixed » Closed (fixed)

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