Problem/Motivation
https://qa.drupal.org/pifr/test/1076308
According the PHPDocs in the CacheContextsManager the calling method is REQUIRED to send an array of strings. Currently it is not doing so, and the assert statement reveals this. Whether or not this is an underlying cause of a greater issue is unknown.
Steps to reproduce
NA
Proposed resolution
- Assert that cache context keys and tokens are strings in CacheContextManager::convertTokensToKeys and ContextCacheKeys::__construct()
- In QueryArgsCacheContext::getContext(), when the value is an array, return a query string with the query argument and all values separated by "&", by using the existing http_build_query().
Remaining tasks
Review MR
Commit
User interface changes
N/A
API changes
N/A
Data model changes
N/A
Release notes snippet
N/A
| Comment | File | Size | Author |
|---|
Issue fork drupal-2509436
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 #1
Aki Tendo commentedUploaded is a patch containing only the assert statement that is being failed, and the assert patch (so that the test can run)
Comment #3
dawehnerThe only thing I can imagine that its NULL sometimes ...
at least this piece of code will always return in a string
Comment #4
olli commentedThis makes QueryArgsCacheContext::getContext() always return a string. This would also fix a problem with query strings like '?value[0]=a&value[1]=b', see #2501905-7: \Drupal\views\Plugin\views\argument_default\QueryParameter should specify a more specific cache context.
Comment #5
Aki Tendo commentedVerified to work. The contents of the #4 interdiff can be committed to trunk alone if desired - that's the actual fix and it is not reliant on the rest of the patch which is just an assert statement test that discovered the problem.
Comment #6
xjmThanks @Aki Tendo and @olli!
The contents of this patch seem radically out of scope for the title of the issue, so let's create a patch that just fixes the bug. A simple unit test or such would be preferable for test coverage.
Also, the issue needs a beta evaluation.
Comment #7
xjmComment #8
olli commentedHere's a test.
Comment #10
dawehnerWhat about just casting that into a string.
<3
Comment #11
olli commentedThanks for the review @dawehner. Casting seems to work too.
Comment #12
olli commentedremoved $query_string variable.
No need to cast here.
Comment #13
dawehnerNice test coverage!
Just for future reference, you can now use ::class
Comment #14
Aki Tendo commentedWith the assert tools patch committed to core I can now append the original failing assertion to the patch.
Note: Due to an issue with the test bots the .htaccess file in the patch has assert active set to 1. This change is not to be included in the final patch.
Comment #15
Aki Tendo commentedK, it passed. Uploading patch with .htaccess file change reverted for final commit.
Comment #16
Aki Tendo commentedAs #15 but with assertions back on - the file size differences clued me in that the #14 patch was just the runtime assertion and the fix and didn't hold olli's new unit test code.
Comment #17
Aki Tendo commentedComment #18
Aki Tendo commentedOk, I'm going to mark this RTBC since the only code I've wrote in this have all been the test failure that olli later wrote code to pass. It is the patch posted at #15 that needs to go be committed to trunk.
Comment #19
olli commented@Aki Tendo: Did you mean to add an assert for $keys here? I think that's what failed in #1.
Comment #20
Aki Tendo commentedSorry it took so long to get back to this. In the code I originally added the assertion to there was no ContextCacheKeys object. So the original test is now outdated.
Submitted is a new test - the check of the validity of the keys now lies in the ContextCacheKeys constructor since it declares it must take an array of strings as an argument. The merge function of Cacheable Metadata asserts that it is returning a new instance of itself. The new QueryArgsCacheContextTest now has 16 failures - possibly cause it also has slipped out of sync with motion in the code base.
Comment #22
Aki Tendo commentedWell, good news - the only fails are coming out of olli's new test.
Comment #23
olli commentedFixed the test and reverted .htaccess changes.
Comment #24
Aki Tendo commentedLooks great to me, anyone else?
Comment #25
olli commentedThat is nice, thank you.
Reroll.
Comment #27
wim leersFound this via #2729439: QueryArgsCacheContext should return a special value for ?arg (without value).
I think this assertion is quite pointless, because
$result = clone $this;, so that's always true.These assertions OTOH are very valuable!
This is the bugfix the issue summary/title state.
I don't think this is a good idea.
\Drupal\Core\Cache\Context\CacheContextsManager::convertTokensToKeys()is already ensuring a clear key-value pair.Let's not change this.
Comment #28
olli commentedThanks for your review!
4. We need that (in addition to 3) to always get a string from QueryArgsCacheContext::getContext(). Without it, both '?value[min]=1' and '?value[min]=2' for 'url.query_args:value' evaluate to 'Array' with a notice 'Array to string conversion' from CacheContextsManager::convertTokensToKeys().
Comment #29
wim leersOh, interesting. Fine, let's do some conversion from array to string, but let's not repeat
$query_arg.Also, #2729439: QueryArgsCacheContext should return a special value for ?arg (without value) just landed, which already added
QueryArgsCacheContextTest. That means this patch's test will need to be refactored.Comment #33
andypostThe related issues fixed now it needs work and reroll
Comment #44
quietone commentedThis was a bugsmash daily target. I ran the test that is in the patch and it fails, so I assumed this still needed to be done. larowlan confirmed that in slack.
This also needs an issue summary update, see Write an issue summary for an existing issue for guidance.
Comment #45
quietone commentedTrying to improve the title
Comment #46
bhaveshithape commentedAdding Rerole for d10, As previous patch failed to apply on d10 and also most of the files already had the changes so there is minor change in the patch.
Comment #47
smustgrave commentedRegarding #46
How so? I'm checking D10.1 and I don't see the changes for a few of those files. Also this lost test coverage so adding that tag back
Was previously tagged for issue summary which still needs to happen as well.
Comment #51
dimitriskr commentedI've altered the existing test to support the new format brought by
$cache_context->getContext()Comment #52
smustgrave commentedIssue summary section for solution is missing.
Tagging for submaintainer to make sure they agree on the approach
Comment #53
dimitriskr commentedComment #54
berdirThose asserts are not doing anything, see MR comment.
Comment #55
dimitriskr commentedComment #56
kristiaanvandeneyndeI feel like #27.1 was never addressed. My assumption is also that if CacheableMetadata::merge() has
$result = clone $this;, then it seems kind of moot toassert($result instanceof CacheableMetadata);.Comment #28 also touched on the ampersand but not sure if we really need that.
From a cache submaintainer point of view, I see nothing wrong here. Assertions are harmless, casting casche context return values to strings is too because worst case scenario that leads to a few cache entries no longer being reachable, at which point they get recalculated and stored again. Only been subsystem maintainer for a few months now, so will ask @catch before removing tag.
Comment #57
kristiaanvandeneyndeThis looks fine conceptually, still NW per my latest comment, though.
Comment #58
bhaveshithape commented