Problem/Motivation

I thought I'd start on this: #2052109: [meta] Expand phpunit tests for \Drupal\Component\Plugin classes But as you can see, the isssue is for the Drupal\Component\Plugin namespace. Not the Core one.

So I made this test, and then realized my mistake.

But still, it's a good test.

Proposed resolution

Review and commit the test.

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Unfrozen changes Unfozen because it only changes automated tests for more complete code coverage, without disrupting much of anything.

Comments

mile23’s picture

Status: Active » Needs review
StatusFileSize
new6.19 KB

ContextDefinition scores as riskiest in the Drupal\Core\Plugin namespace by CRAP number.

This patch reduces its overall CRAP score from 420 to 138.

We test the two most complex methods: getDataDefinition() and getConstraint().

Includes @uses directives in order to appease the combined --strict --coverage-text gods. We've been having this conversation over in #2364555: Add @covers annotation, fix some --strict for PHPUnit

The PHPUnit coding standards don't talk about @uses, which is why so many tests are marked as 'risky' by PHPUnit when you generate a coverage report with --strict. #2057905: [policy, no patch] Discuss the standards for phpunit based tests

jhedstrom’s picture

Since these are testing, as you say, the two most complex methods, I'd like to see a few code comments just to clarify the flow of these tests. It wasn't immediately obvious to me exactly where or when the exception in testGetDataDefinitionInvalidType() was going to be thrown.

mile23’s picture

StatusFileSize
new6.95 KB
new3.07 KB

Well this is one of the problems with maintaining complex code in the first place, but yah, it could use some inline comments. How's this?

jhedstrom’s picture

Those comments look good to me.

Does it make sense to visit \Drupal\system\Tests\Plugin\ContextPluginTest and remove some (all?) of those tests as part of this?

mile23’s picture

I'd say the more tests the better, and the less we muck about with other tests the better.

Also, this test is more of a pure unit test, while \Drupal\system\Tests\Plugin\ContextPluginTest is functional.

So therefore: No. :-)

jhedstrom’s picture

I'd say the more tests the better, and the less we muck about with other tests the better.

I don't fully agree with that--some of the tests in the ContextPluginTest are identical (fake unit tests). At least those should be removed to avoid unnecessary effort in the future.

mile23’s picture

Well as soon as you get here:

    $manager = new MockBlockManager();

...it's not a fake unit test any more. And that's the 2nd line of the only test method. MockBlockManager has a ton of already-implemented dependencies.

The test in the patch mocks everything, and it's probably a good idea to have a functional test, as well.

If you have a specific idea about what to remove, I'd love to see it. https://api.drupal.org/api/drupal/core!modules!system!src!Tests!Plugin!C...

jhedstrom’s picture

While ContextPluginTest is using functional items for testing (users, nodes, and blocks), most of that test still reads to me like a unit test. For instance, isn't this

   // Try to get context that is missing its definition.
    try {
      $plugin->getContextDefinition('not_exists');
      $this->fail('The user context should not yet be set.');
    }
    catch (ContextException $e) {
      $this->assertEqual($e->getMessage(), 'The not_exists context is not a valid context.');
    }

or

  // Try to pass the wrong class type as a context value.
    $plugin->setContextValue('user', $node);
    $violations = $plugin->validateContexts();
    $this->assertTrue(!empty($violations), 'The provided context value does not pass validation.');

replaced by the bad context exception tests you wrote in this patch?

On the other hand, something like this looks highly functional:

 // Try to get a value of a valid context, while this value has not been set.
    try {
      $plugin->getContextValue('user');
    }
    catch(ContextException $e) {
      $this->assertIdentical("The entity:user context is required and not present.", $e->getMessage(), 'Requesting a non-set value of a required context should throw a context exception.');
    }
jhedstrom’s picture

Status: Needs review » Reviewed & tested by the community

Thinking about this more, since the SimpleTest cannot be removed completely, there's no real reason to mess with it. The unit tests provided here add needed coverage and look good to me.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 3: 2368019_3.patch, failed testing.

mile23’s picture

Status: Needs work » Reviewed & tested by the community

Patch in #3 applies and makes passing unit tests for me. Not sure why the test failed... Is the testbot unhappy?

tim.plunkett queued 3: 2368019_3.patch for re-testing.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs issue summary update
  1. +++ b/core/tests/Drupal/Tests/Core/Plugin/Context/ContextDefinitionTest.php
    @@ -0,0 +1,210 @@
    +use Drupal\Core\Plugin\Context\ContextDefinition;
    

    Not used

  2. +++ b/core/tests/Drupal/Tests/Core/Plugin/Context/ContextDefinitionTest.php
    @@ -0,0 +1,210 @@
    +      array(NULL, array(), 'nonexistant_constraint_name'),
    

    nonexistent

  3. This issue is a normal task so we need to outline how it fits within the allowable Drupal 8 beta criteria. Can someone add Drupal 8 beta phase evaluation template to the issue summary. This will allowed because it is improving test coverage but it nice when someone other than me works this out and adds the info to the issue.
mile23’s picture

Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new6.9 KB
new819 bytes

1. I like to declare a use of the class under test. It's a style thing, though. Removed.

2. Typo gone.

3. Added Beta evaluation block.

jhedstrom’s picture

Status: Needs review » Reviewed & tested by the community

I think this is back to RTBC.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: -Needs issue summary update

Committed 7acca5b and pushed to 8.0.x. Thanks!

Thanks for adding the beta evaluation for to the issue summary.

  • alexpott committed 616b2ac on
    Issue #2368019 by Mile23: Expand unit testing for Drupal\Core\Plugin\...

Status: Fixed » Closed (fixed)

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