Problem/Motivation

\Drupal\Tests\Core\Entity\EntityUrlTest is currently structured in a way that is hard to extend it and hard to provide coverage for link templates other than edit-form.

Note: All method references below refer to methods of \Drupal\Core\Entity\Entity.

This is problematic because there are a number of issues that want to expand the test coverage of entity URL handling:

Proposed resolution

This issue turns it into a proper unit test with 100% code coverage of ::toUrl(), ::urlRouteParmeters(), ::linkTemplates(), ::urlInfo(), ::url(), and ::hasLinkTemplate().

It does thorough "blackbox" testing of ::toUrl() (and by extension ::urlRouteParmeters() and ::linkTemplates()). To test ::urlInfo() and ::url() (and by extension ::hasLinkTemplate()), which are (deprecated) wrappers of ::toUrl(), it mocks ::toUrl() itself, and tests that the calls are properly wired up.

Remaining tasks

User interface changes

None.

API changes

None.

Data model changes

None.

Comments

tstoeckler created an issue. See original summary.

tstoeckler’s picture

Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new28.5 KB

Here we go.

tstoeckler’s picture

Issue summary: View changes
tstoeckler’s picture

Issue summary: View changes
tstoeckler’s picture

Issue summary: View changes
tstoeckler’s picture

Issue summary: View changes
tstoeckler’s picture

Issue summary: View changes
dawehner’s picture

It is amazing how much more readable your new tests are. On top of that it adds test coverage and provides the points you outlined in the issue summary.

Here are just a few small remarks

  1. +++ b/core/tests/Drupal/Tests/Core/Entity/EntityUrlTest.php
    @@ -2,377 +2,482 @@
    +   * @expectedException \Drupal\Core\Entity\EntityMalformedException
    +   * @expectedExceptionMessage The "test_entity" entity cannot have a URI as it does not have an ID
        */
    

    Note: Since https://thephp.cc/news/2016/02/questioning-phpunit-best-practices the annotation seems not longer be the right way to use here

  2. +++ b/core/tests/Drupal/Tests/Core/Entity/EntityUrlTest.php
    @@ -2,377 +2,482 @@
    +  public function testToUrlLinkTemplateRevision($is_default_revision, $link_template, $expected_route_name, array $expected_route_parameters) {
    ...
    +    $test_cases['default_revision'] = [TRUE, 'canonical', 'entity.test_entity.canonical', $route_parameters];
    ...
    +    $test_cases['non_default_revision'] = [FALSE, 'revision', 'entity.test_entity.revision', $route_parameters];
    

    One tricky I learned recently from someone: Not use TRUE/FALSE in providers but rather simply introduce named constants like static::DEFAULT_REVISION and static::NON_DEFAULT_REVISION to improve readability. What do you think about it?

  3. +++ b/core/tests/Drupal/Tests/Core/Entity/EntityUrlTest.php
    @@ -2,377 +2,482 @@
    +    $this->assertUrl('entity.test_entity.collection', [], $entity, $url);
    +    // The 'collection' link template because it does not get the 'language'
    +    // URL option.
    +    $this->assertNull($url->getOption('language'));
    

    Is there a reason you didn't included the language into the assertUrl method? It seems to just make sense to add it as an additional optional parameter at the end. Many of the great test methods you wrote use it now.

tstoeckler’s picture

Issue summary: View changes
tstoeckler’s picture

StatusFileSize
new6.26 KB
new28.94 KB

Thanks for the great review!

1. Done, yeah I hope @expectedException dies a slowfast! and horrible death.
2. I've never heard of that, but I love it. Done. Note that I used @var true and @var false instead of @var bool for the docs because that is consistent with our usage of @param \Some\Object|false elsewhere.
3. Great point! Done. This actually increased test coverage a little bit, because we now assert the language option in the URI callback case.

Status: Needs review » Needs work

The last submitted patch, 10: 2751395-10-entity-url-test.patch, failed testing.

tstoeckler’s picture

Status: Needs work » Needs review
StatusFileSize
new782 bytes
new28.97 KB

Sorry that was pure laziness/arrogance on my part of not running the test locally. :-/

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Sorry that was pure laziness/arrogance on my part of not running the test locally. :-/

Don't you feel bad about the global climate change caused by that ?

alexpott’s picture

Version: 8.1.x-dev » 8.2.x-dev
Status: Reviewed & tested by the community » Fixed

Committed c244dc4 and pushed to 8.1.x and 8.2.x. Thanks!

  • alexpott committed c009442 on 8.2.x
    Issue #2751395 by tstoeckler, dawehner: Rewrite EntityUrlTest
    

  • alexpott committed c244dc4 on 8.1.x
    Issue #2751395 by tstoeckler, dawehner: Rewrite EntityUrlTest
    
    (cherry...

Status: Fixed » Closed (fixed)

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