Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
PHP Fatal error: Call to a member function getCanonicalRepresentation() on a non-object in core/lib/Drupal/Core/Plugin/Context/Context.php on line 50
The typed data object is null here because it has not been intialized yet.
Proposed resolution
Always use the getTypedDataManager() method provided by the TypedDataTrait to make sure the manager is initialized on demand.
Remaining tasks
Review patch.
User interface changes
none.
API changes
none.
Comment | File | Size | Author |
---|---|---|---|
#11 | 2467411.patch | 2.96 KB | klausi |
#9 | 2467411.patch | 2.53 KB | klausi |
#6 | 2467411.patch | 2.46 KB | klausi |
#2 | 2467411-testonly.patch | 1.51 KB | klausi |
#1 | 2467411.patch | 2.03 KB | klausi |
Comments
Comment #1
klausiklausi opened a new pull request for this issue.
Comment #2
klausiTest only patch to demonstrate the fatal error.
Comment #3
klausiThis affects Rules for D8.
Comment #4
fagoouch, yes - totally.
I do not get this reasoning - why shouldn't it be possible to the test this with phpunit?
Also, the test case is named a bit weird. integrating with what? Maybe, it should be called ContextTypedDataTest to ensure context works properly with typed data in general?
Comment #6
klausiklausi pushed some commits to the pull request.
For an interdiff please see the list of recent commits.
Comment #7
klausiYou are right, we can actually test this with phpunit. I just never populated \Drupal in a unit test before, but I found other tests doing that, so copied from there.
Comment #8
fagoThanks, code looks good now except this:
That's cheating. It should be able to return the right value from setting it. Also, the canonical representation for strings is the string.
I think best, you'd set a callback as getCanonicalRepresentation() on the TDM mock which just returns the string. Then, the test should check that the returned context value really is a plain string - it's a test for getContextValue() after all.
Comment #9
klausiklausi pushed some commits to the pull request.
For an interdiff please see the list of recent commits.
Comment #10
klausiWe don't need a callback, the mocked typed data manager can just return the string. Implemented that and added an assertion on the returned value.
Comment #11
klausiklausi pushed some commits to the pull request.
For an interdiff please see the list of recent commits.
Comment #12
klausiTalked with fago about this at DevDays and we agreed that implementing this with a return callback on the mocked typed data manager makes more sense to actually invoke the typed data object that is used by the Context class.
Comment #13
fagoThanks, the test makes sense to me like that now.
Added tag as this causes failures in Rules.
Comment #14
webchickNice catch. Sounds major to me.
Committed and pushed to 8.0.x. Thanks!