diff options
author | daniel <dkinzler@wikimedia.org> | 2020-10-01 17:30:58 +0200 |
---|---|---|
committer | Petr Pchelko <ppchelko@wikimedia.org> | 2020-10-01 14:53:00 -0600 |
commit | ff07253be5d4e82af4601917cd3c0927e1a997da (patch) | |
tree | 77047f1bf32c0304964a49c517c4bfa9f8fe4ad1 /tests/phpunit/includes/parser/ParserCacheTest.php | |
parent | e7ff3cbb6b4db6f4db55746422d64d4205720f79 (diff) | |
download | mediawikicore-ff07253be5d4e82af4601917cd3c0927e1a997da.tar.gz mediawikicore-ff07253be5d4e82af4601917cd3c0927e1a997da.zip |
ParserCache: be resilient to string values
This makes the parser cache resilient to encountering string values
where it is currently expecting to get a ParserOutput objerct from the
underlying cache.
This provides forward compatibility with a switch to JSON based caching:
If we have to switch back after writing JSON to the cache for a while,
ParserCache would simply ignore the respective entries, rather than
causing fatal errors.
Bug: T263579
Change-Id: Iaed582097ab2d05edb4b99a738ac39c530fd63c1
Diffstat (limited to 'tests/phpunit/includes/parser/ParserCacheTest.php')
-rw-r--r-- | tests/phpunit/includes/parser/ParserCacheTest.php | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/tests/phpunit/includes/parser/ParserCacheTest.php b/tests/phpunit/includes/parser/ParserCacheTest.php index 5c75d8b08464..d589fcd7301e 100644 --- a/tests/phpunit/includes/parser/ParserCacheTest.php +++ b/tests/phpunit/includes/parser/ParserCacheTest.php @@ -14,6 +14,7 @@ use ParserOptions; use ParserOutput; use Psr\Log\NullLogger; use Title; +use Wikimedia\TestingAccessWrapper; use WikiPage; /** @@ -385,4 +386,60 @@ class ParserCacheTest extends MediaWikiIntegrationTestCase { $cache = $this->createParserCache( null, $storage ); $this->assertSame( $storage, $cache->getCacheStorage() ); } + + /** + * Test that we handle corrupt data gracefully. + * This is important for forward-compatibility with JSON serialization. + * We want to be sure that we don't crash horribly if we have to roll + * back to a version of the code that doesn't know about JSON. + * + * @covers ParserCache::get + */ + public function testCorruptData() { + $bag = new HashBagOStuff(); + $cache = $this->createParserCache( null, $bag ); + $parserOutput = new ParserOutput( 'TEST_TEXT' ); + + $options1 = ParserOptions::newFromAnon(); + $cache->save( $parserOutput, $this->page, $options1, $this->cacheTime ); + + $outputKey = TestingAccessWrapper::newFromObject( $cache )->getParserOutputKey( + $this->page, + $options1->optionsHash( $parserOutput->getUsedOptions() ) + ); + + $bag->set( $outputKey, 'bad data' ); + + // just make sure we don't crash and burn + $result = $cache->get( $this->page, $options1 ); + $this->assertFalse( $result ); + } + + /** + * Test that we handle corrupt data gracefully. + * This is important for forward-compatibility with JSON serialization. + * We want to be sure that we don't crash horribly if we have to roll + * back to a version of the code that doesn't know about JSON. + * + * @covers ParserCache::getKey + */ + public function testCorruptMetadata() { + $bag = new HashBagOStuff(); + $cache = $this->createParserCache( null, $bag ); + $parserOutput = new ParserOutput( 'TEST_TEXT' ); + + $options1 = ParserOptions::newFromAnon(); + $cache->save( $parserOutput, $this->page, $options1, $this->cacheTime ); + + $optionsKey = TestingAccessWrapper::newFromObject( $cache )->getOptionsKey( + $this->page + ); + + $bag->set( $optionsKey, 'bad data' ); + + // just make sure we don't crash and burn + $result = $cache->getKey( $this->page, $options1, false ); + $this->assertFalse( $result ); + } + } |