This is a follow-up from #2671964: ContextHandler cannot validate constraints

Problem/Motivation

If no user is logged in, \Drupal\user\ContextProvider\CurrentUserContext::getRuntimeContexts() will trigger an E_WARNING "Creating default object from empty value" which can cause automated testing to fail. This is due to the assumption that the current user will exist in the database.

Proposed resolution

Check for a value before operating on the returned user.

Remaining tasks

N/A

User interface changes

N/A

API changes

N/A

Data model changes

N/A

CommentFileSizeAuthor
#2 2936642-context-2.patch1.15 KBtim.plunkett

Comments

tim.plunkett created an issue. See original summary.

tim.plunkett’s picture

Status: Active » Needs review
StatusFileSize
new1.15 KB
larowlan’s picture

Status: Needs review » Reviewed & tested by the community

assuming bot agrees

jonathan1055’s picture

Thanks for raising this. The Rules issue which shows examples of the test failures is #2936553: Rules kernel tests fail with "Creating default object from empty value"

I will test this core patch locally but it looks fine at first glance.

jonathan1055’s picture

I've tested #2 locally on 8.5.x with Rules dev and the failing tests now pass. So I am also happy with the RTBC.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

fago’s picture

Would be great to get this committed to 8.5 and 8.6.

jonathan1055’s picture

Yes, this is causing Rules to fail testing at 8.5 and 8.6. To show how this core patch fixes the Rules tests I have created a github branch which has a modfied .travis.yml file to conditionally apply the patch from #2 above when running at 8.5.x or above. I used the following:

  # TEMP apply patch from https://www.drupal.org/project/drupal/issues/2936642
  # for core 8.5 and above.
  - |
    if [[ "${DRUPAL_CORE}" > "8.4.x" ]]; then
      wget https://www.drupal.org/files/issues/2936642-context-2.patch;
      git apply -v -p1 < 2936642-context-2.patch;
    fi

The github branch is https://github.com/jonathan1055/rules/tree/2936642-context-warning
n.b. this branch also has the test fix from #2936679-3: Autocomplete test needs values for "Default Revision" flag

The Travis test job is https://travis-ci.org/jonathan1055/rules/builds/330574666

larowlan’s picture

Version: 8.6.x-dev » 8.5.x-dev
catch’s picture

Status: Reviewed & tested by the community » Needs work

It's good that this fixes the Rules tests, but is there a reason we cant add core test coverage for this?

jonathan1055’s picture

I'm sure a test could be added to Core to check the context handler for anon users, but I don't know how to do that. This ia a bug that was introduced in #2671964: ContextHandler cannot validate constraints and is blocking development on Rules because the tests do not pass. If the only reason for not committing this correction is a lack of Core test coverage then can we find someone who does know how to write the test?

tim.plunkett’s picture

Status: Needs work » Reviewed & tested by the community

This error can never be triggered on a real Drupal site, unless you manage to delete the anonymous user account.

The only way to run into this problem is by writing a unit test that makes incorrect assumptions about the user system.
No functional test (like any of those that test the condition system) could ever reproduce this bug.

I'd just as soon won't fix this and everyone should update their tests.
But I think it'd be nice of us to commit this simple fix.

tim.plunkett’s picture

Just to clarify my comment:

On a real site, $this->userStorage->load(0) still returns a valid user account, one with the anonymous role.

  • larowlan committed c3ebbc4 on 8.6.x
    Issue #2936642 by tim.plunkett: Getting runtime contexts will generate...
larowlan’s picture

Status: Reviewed & tested by the community » Fixed

Agree, this is a test-only edge case and we're blocking contrib here.

Committed as c3ebbc4 and pushed to 8.6.x. Thanks!

Cherry-picked as dd5bae6 and pushed to 8.5.x

  • larowlan committed dd5bae6 on 8.5.x
    Issue #2936642 by tim.plunkett: Getting runtime contexts will generate...

Status: Fixed » Closed (fixed)

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

jonathan1055’s picture

Just for the record, the six failing Rules tests were:

Kernel/Engine/MetadataAssertionTest.php:76  (testAssertingEntityBundle)
Kernel/Engine/MetadataAssertionTest.php:114 (testAssertingWithLogicalOperations)
Kernel/Engine/MetadataAssertionTest.php:153 (testAssertingOfNegatedConditions)
Kernel/Engine/AutocompleteTest.php:64 (testAutocomplete)
Kernel/Engine/AutocompleteTest.php:89 (testNodeAutocomplete)
Kernel/Engine/AutocompleteTest.php:352 (testListAutocomplete)
Kernel/RulesEngineTest.php:107 (testProvidedVariables)

In #12 tim.plunkett said

The only way to run into this problem is by writing a unit test that makes incorrect assumptions about the user system.

As this is now a closed issue discussion can continue on #2936553: Rules kernel tests fail with "Creating default object from empty value"