Problem/Motivation
EntityResourceTestBase is subclassed by every entity type in core, which is further subclassed nine times for various REST and HAL tests. It contains four test methods, one for each HTTP verb.
However, when testing config entities, three of these HTTP verbs simply pass immediately:
public function testPost() {
// @todo Remove this in https://www.drupal.org/node/2300677.
if ($this->entity instanceof ConfigEntityInterface) {
$this->assertTrue(TRUE, 'POSTing config entities is not yet supported.');
return;
}
...
public function testPatch() {
// @todo Remove this in https://www.drupal.org/node/2300677.
if ($this->entity instanceof ConfigEntityInterface) {
$this->assertTrue(TRUE, 'PATCHing config entities is not yet supported.');
return;
}
...
public function testDelete() {
// @todo Remove this in https://www.drupal.org/node/2300677.
if ($this->entity instanceof ConfigEntityInterface) {
$this->assertTrue(TRUE, 'DELETEing config entities is not yet supported.');
return;
}
The setup of these tests is still done, but no actual testing is performed. This means that for each config entity type we perform 3 x 9 = 27 installs via BrowserTestBase::setUp() that are immediately thrown away because the test is effectively skipped.
At present there are 31 config entity types (and only 13 content entity types) in core. This means that we are performing 31 x 27 = 837 unnecessary installs during every full test run across core.
Steps to reproduce
Proposed resolution
Add a new ConfigEntityResourceTestBase parent class to EntityResourceTestBase for config entities that only require testGet().
Keep EntityResourceTestBase intact for content entities and any downstream users in contrib or custom code.
Modify EntityResourceRestTestCoverageTest to ensure both config and content entities are tested correctly.
Revert this patch when #2300677: JSON:API POST/PATCH support for fully validatable config entities lands.
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
Issue fork drupal-3212346
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
Comment #3
longwaveAlso refactored EntityResourceRestTestCoverageTest from a functional to a kernel test.
Comment #4
longwaveThis patch appears to shave approx 4 minutes off the functional tests section of each full test run.
Sample run without this patch: https://dispatcher.drupalci.org/job/drupal_patches/83356/consoleFull
Latest run of this patch: https://dispatcher.drupalci.org/job/drupal_patches/83362/consoleFull
Comment #5
longwaveI am not sure the 837 is entirely accurate here, because XmlEntityNormalizationQuirksTrait is used 153 times and overrides two test methods to do nothing, which (if my calculations are correct) makes up 306 of the installs, so perhaps only 531 or so are fixed here? Maybe worth tackling those XML ones in a followup.
Comment #7
spokjeSince I've only merged the recent commits on
9.3.x-devinto the MR and didn't make any code changes, I _think_ I'm allowed to put this on RTBC?Comment #9
alexpottI think this will silently reduce test coverage of anything in contrib or custom that extends from
Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase. I'm not sure we can do that. If we want to do this I think we need to make a new class thatDrupal\Tests\rest\Functional\EntityResource\EntityResourceTestBaseextends from or deprecate it and build a new class.Comment #10
longwaveRefactored so
ConfigEntityResourceTestBaseis the base class, used by core config entities, and content entities and contrib/custom config entities extend EntityResourceTestBase as before.Comment #11
longwaveUpdate IS with new approach.
Comment #12
longwaveRefactored as per @alexpott's suggestion - did not realise we could use getName() in setUp() but that makes things much, much simpler.
Comment #13
alexpottAll the MR applies to 10.x we get...
Fatal error: Declaration of Drupal\Tests\rest\Functional\EntityResource\ConfigEntityResourceTestBase::setUp() must be compatible with Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::setUp(): void in /Volumes/dev/sites/drupal8alt.dev/core/modules/rest/tests/src/Functional/EntityResource/ConfigEntityResourceTestBase.php on line 22
Running one test locally...
Before
After
So this is definitely a win - we just need a 10.0.x patch with
setup(): voidin the new testbase class and I'll rtbc.Comment #14
longwaveAdded void return to the base class, as it's new code and all concrete classes have void return type already I think this is safe.
Comment #15
longwaveComment #16
alexpottThanks @longwave - this looks great.
Comment #18
catchCommitted/pushed to 10.0.x, thanks!
We can't do a straight backport to 9.4.x with the void return type hint due to PHP 7.3, so I cherry-picked, removed it, rebased with a fixup commit, and pushed that.
Nice find!
Comment #20
neclimdulNot sure how this didn't get caught but this broke tests.
Comment #21
neclimdulhm... that error is all sorts of weird. trying to figure out _why_ its weird I removed the rewritten classes in my sites/simpletest directory and magically things started working. I'm very confused but it seems to be working so moving back to fixed. Hopefully not a big deal.