summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathaniel Catchpole2015-10-05 13:12:02 (GMT)
committerNathaniel Catchpole2015-10-05 13:12:02 (GMT)
commit72b0180f596e54643d8458d72d552ace0413cb22 (patch)
tree4c3dca2a90688aed8fed1b26dcb98cf42913b2a7
parent8d196a795038e76db34a58eba0a399ac2c6de7a3 (diff)
Issue #2454649 by Aki Tendo, dawehner: Cache Optimization and hardening -- [PP-1] Use assert() instead of exceptions in Cache::merge(Tags|Contexts)
-rw-r--r--core/lib/Drupal/Core/Cache/ApcuBackend.php2
-rw-r--r--core/lib/Drupal/Core/Cache/Cache.php8
-rw-r--r--core/lib/Drupal/Core/Cache/CacheCollector.php2
-rw-r--r--core/lib/Drupal/Core/Cache/CacheTagsInvalidator.php3
-rw-r--r--core/lib/Drupal/Core/Cache/Context/CacheContextsManager.php33
-rw-r--r--core/lib/Drupal/Core/Cache/DatabaseBackend.php2
-rw-r--r--core/lib/Drupal/Core/Cache/MemoryBackend.php2
-rw-r--r--core/lib/Drupal/Core/Cache/PhpBackend.php2
-rw-r--r--core/lib/Drupal/Core/Plugin/DefaultPluginManager.php2
-rw-r--r--core/modules/block/tests/src/Unit/Plugin/DisplayVariant/BlockPageVariantTest.php5
-rw-r--r--core/modules/content_translation/tests/src/Unit/Access/ContentTranslationManageAccessCheckTest.php1
-rw-r--r--core/modules/forum/tests/src/Unit/Breadcrumb/ForumBreadcrumbBuilderBaseTest.php3
-rw-r--r--core/modules/forum/tests/src/Unit/Breadcrumb/ForumListingBreadcrumbBuilderTest.php3
-rw-r--r--core/modules/forum/tests/src/Unit/Breadcrumb/ForumNodeBreadcrumbBuilderTest.php3
-rw-r--r--core/modules/language/tests/src/Unit/LanguageNegotiationUrlTest.php1
-rw-r--r--core/modules/quickedit/tests/src/Unit/Access/EditEntityFieldAccessCheckTest.php4
-rw-r--r--core/modules/simpletest/src/TestBase.php4
-rw-r--r--core/modules/system/src/Tests/Cache/GenericCacheBackendUnitTestBase.php19
-rw-r--r--core/modules/system/tests/src/Unit/Breadcrumbs/PathBasedBreadcrumbBuilderTest.php1
-rw-r--r--core/modules/system/tests/src/Unit/Menu/MenuLinkTreeTest.php1
-rw-r--r--core/modules/user/tests/src/Unit/PermissionAccessCheckTest.php4
-rw-r--r--core/modules/user/tests/src/Unit/UserAccessControlHandlerTest.php4
-rw-r--r--core/tests/Drupal/Tests/Component/Assertion/InspectorTest.php34
-rw-r--r--core/tests/Drupal/Tests/Core/Access/AccessResultTest.php1
-rw-r--r--core/tests/Drupal/Tests/Core/Breadcrumb/BreadcrumbManagerTest.php4
-rw-r--r--core/tests/Drupal/Tests/Core/Cache/CacheTagsInvalidatorTest.php3
-rw-r--r--core/tests/Drupal/Tests/Core/Cache/CacheTest.php2
-rw-r--r--core/tests/Drupal/Tests/Core/Cache/CacheableMetadataTest.php3
-rw-r--r--core/tests/Drupal/Tests/Core/Cache/Context/CacheContextsManagerTest.php3
-rw-r--r--core/tests/Drupal/Tests/Core/Entity/EntityCreateAccessCheckTest.php4
-rw-r--r--core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php2
-rw-r--r--core/tests/Drupal/Tests/Core/Menu/DefaultMenuLinkTreeManipulatorsTest.php5
-rw-r--r--core/tests/Drupal/Tests/Core/Menu/LocalTaskManagerTest.php4
-rw-r--r--core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php2
-rw-r--r--core/tests/Drupal/Tests/Core/Render/RendererTestBase.php1
-rw-r--r--core/tests/Drupal/Tests/Core/Route/RoleAccessCheckTest.php4
-rw-r--r--core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php1
37 files changed, 135 insertions, 47 deletions
diff --git a/core/lib/Drupal/Core/Cache/ApcuBackend.php b/core/lib/Drupal/Core/Cache/ApcuBackend.php
index 9bac79e..7f04f0e 100644
--- a/core/lib/Drupal/Core/Cache/ApcuBackend.php
+++ b/core/lib/Drupal/Core/Cache/ApcuBackend.php
@@ -166,7 +166,7 @@ class ApcuBackend implements CacheBackendInterface {
* {@inheritdoc}
*/
public function set($cid, $data, $expire = CacheBackendInterface::CACHE_PERMANENT, array $tags = array()) {
- Cache::validateTags($tags);
+ assert('\Drupal\Component\Assertion\Inspector::assertAllStrings($tags)', 'Cache tags must be strings.');
$tags = array_unique($tags);
$cache = new \stdClass();
$cache->cid = $cid;
diff --git a/core/lib/Drupal/Core/Cache/Cache.php b/core/lib/Drupal/Core/Cache/Cache.php
index 5ad9500..d75eaff 100644
--- a/core/lib/Drupal/Core/Cache/Cache.php
+++ b/core/lib/Drupal/Core/Cache/Cache.php
@@ -34,7 +34,7 @@ class Cache {
*/
public static function mergeContexts(array $a = [], array $b = []) {
$cache_contexts = array_unique(array_merge($a, $b));
- \Drupal::service('cache_contexts_manager')->validateTokens($cache_contexts);
+ assert('\Drupal::service(\'cache_contexts_manager\')->assertValidTokens($cache_contexts)');
sort($cache_contexts);
return $cache_contexts;
}
@@ -59,8 +59,9 @@ class Cache {
* The merged array of cache tags.
*/
public static function mergeTags(array $a = [], array $b = []) {
+ assert('\Drupal\Component\Assertion\Inspector::assertAllStrings($a) && \Drupal\Component\Assertion\Inspector::assertAllStrings($b)', 'Cache tags must be valid strings');
+
$cache_tags = array_unique(array_merge($a, $b));
- static::validateTags($cache_tags);
sort($cache_tags);
return $cache_tags;
}
@@ -99,6 +100,9 @@ class Cache {
* @param string[] $tags
* An array of cache tags.
*
+ * @deprecated
+ * Use assert('\Drupal\Component\Assertion\Inspector::assertAllStrings($tags)');
+ *
* @throws \LogicException
*/
public static function validateTags(array $tags) {
diff --git a/core/lib/Drupal/Core/Cache/CacheCollector.php b/core/lib/Drupal/Core/Cache/CacheCollector.php
index a6d8ab5..eb1ca57 100644
--- a/core/lib/Drupal/Core/Cache/CacheCollector.php
+++ b/core/lib/Drupal/Core/Cache/CacheCollector.php
@@ -115,7 +115,7 @@ abstract class CacheCollector implements CacheCollectorInterface, DestructableIn
* (optional) The tags to specify for the cache item.
*/
public function __construct($cid, CacheBackendInterface $cache, LockBackendInterface $lock, array $tags = array()) {
- Cache::validateTags($tags);
+ assert('\Drupal\Component\Assertion\Inspector::assertAllStrings($tags)', 'Cache tags must be strings.');
$this->cid = $cid;
$this->cache = $cache;
$this->tags = $tags;
diff --git a/core/lib/Drupal/Core/Cache/CacheTagsInvalidator.php b/core/lib/Drupal/Core/Cache/CacheTagsInvalidator.php
index 64a8eb0..69d97c0 100644
--- a/core/lib/Drupal/Core/Cache/CacheTagsInvalidator.php
+++ b/core/lib/Drupal/Core/Cache/CacheTagsInvalidator.php
@@ -27,8 +27,7 @@ class CacheTagsInvalidator implements CacheTagsInvalidatorInterface {
* {@inheritdoc}
*/
public function invalidateTags(array $tags) {
- // Validate the tags.
- Cache::validateTags($tags);
+ assert('Drupal\Component\Assertion\Inspector::assertAllStrings($tags)', 'Cache tags must be strings.');
// Notify all added cache tags invalidators.
foreach ($this->invalidators as $invalidator) {
diff --git a/core/lib/Drupal/Core/Cache/Context/CacheContextsManager.php b/core/lib/Drupal/Core/Cache/Context/CacheContextsManager.php
index b01dc9e..81d6735 100644
--- a/core/lib/Drupal/Core/Cache/Context/CacheContextsManager.php
+++ b/core/lib/Drupal/Core/Cache/Context/CacheContextsManager.php
@@ -103,11 +103,9 @@ class CacheContextsManager {
* The ContextCacheKeys object containing the converted cache keys and
* cacheability metadata.
*
- * @throws \LogicException
- * Thrown if any of the context tokens or parameters are not valid.
*/
public function convertTokensToKeys(array $context_tokens) {
- $this->validateTokens($context_tokens);
+ assert('$this->assertValidTokens($context_tokens)');
$cacheable_metadata = new CacheableMetadata();
$optimized_tokens = $this->optimizeTokens($context_tokens);
// Iterate over cache contexts that have been optimized away and get their
@@ -299,4 +297,33 @@ class CacheContextsManager {
}
}
+ /**
+ * Asserts the context tokens are valid
+ *
+ * Similar to ::validateTokens, this method returns boolean TRUE when the
+ * context tokens are valid, and FALSE when they are not instead of returning
+ * NULL when they are valid and throwing a \LogicException when they are not.
+ * This function should be used with the assert() statement.
+ *
+ * @param mixed $context_tokens
+ * Variable to be examined - should be array of context_tokens.
+ *
+ * @return bool
+ * TRUE if context_tokens is an array of valid tokens.
+ */
+ public function assertValidTokens($context_tokens) {
+ if (!is_array($context_tokens)) {
+ return FALSE;
+ }
+
+ try {
+ $this->validateTokens($context_tokens);
+ }
+ catch (\LogicException $e) {
+ return FALSE;
+ }
+
+ return TRUE;
+ }
+
}
diff --git a/core/lib/Drupal/Core/Cache/DatabaseBackend.php b/core/lib/Drupal/Core/Cache/DatabaseBackend.php
index 06c5dc2..7faa806 100644
--- a/core/lib/Drupal/Core/Cache/DatabaseBackend.php
+++ b/core/lib/Drupal/Core/Cache/DatabaseBackend.php
@@ -199,7 +199,7 @@ class DatabaseBackend implements CacheBackendInterface {
'tags' => array(),
);
- Cache::validateTags($item['tags']);
+ assert('\Drupal\Component\Assertion\Inspector::assertAllStrings($item[\'tags\'])', 'Cache Tags must be strings.');
$item['tags'] = array_unique($item['tags']);
// Sort the cache tags so that they are stored consistently in the DB.
sort($item['tags']);
diff --git a/core/lib/Drupal/Core/Cache/MemoryBackend.php b/core/lib/Drupal/Core/Cache/MemoryBackend.php
index 5c7f928..5c93e06 100644
--- a/core/lib/Drupal/Core/Cache/MemoryBackend.php
+++ b/core/lib/Drupal/Core/Cache/MemoryBackend.php
@@ -107,7 +107,7 @@ class MemoryBackend implements CacheBackendInterface, CacheTagsInvalidatorInterf
* Implements Drupal\Core\Cache\CacheBackendInterface::set().
*/
public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = array()) {
- Cache::validateTags($tags);
+ assert('\Drupal\Component\Assertion\Inspector::assertAllStrings($tags)', 'Cache Tags must be strings.');
$tags = array_unique($tags);
// Sort the cache tags so that they are stored consistently in the database.
sort($tags);
diff --git a/core/lib/Drupal/Core/Cache/PhpBackend.php b/core/lib/Drupal/Core/Cache/PhpBackend.php
index 761e394..80b8771 100644
--- a/core/lib/Drupal/Core/Cache/PhpBackend.php
+++ b/core/lib/Drupal/Core/Cache/PhpBackend.php
@@ -148,7 +148,7 @@ class PhpBackend implements CacheBackendInterface {
* {@inheritdoc}
*/
public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = array()) {
- Cache::validateTags($tags);
+ assert('\Drupal\Component\Assertion\Inspector::assertAllStrings($tags)', 'Cache Tags must be strings.');
$item = (object) array(
'cid' => $cid,
'data' => $data,
diff --git a/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php b/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php
index 7c8799a..5b7b86b 100644
--- a/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php
+++ b/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php
@@ -154,7 +154,7 @@ class DefaultPluginManager extends PluginManagerBase implements PluginManagerInt
* definitions should be cleared along with other, related cache entries.
*/
public function setCacheBackend(CacheBackendInterface $cache_backend, $cache_key, array $cache_tags = array()) {
- Cache::validateTags($cache_tags);
+ assert('\Drupal\Component\Assertion\Inspector::assertAllStrings($cache_tags)', 'Cache Tags must be strings.');
$this->cacheBackend = $cache_backend;
$this->cacheKey = $cache_key;
$this->cacheTags = $cache_tags;
diff --git a/core/modules/block/tests/src/Unit/Plugin/DisplayVariant/BlockPageVariantTest.php b/core/modules/block/tests/src/Unit/Plugin/DisplayVariant/BlockPageVariantTest.php
index 5652533..3c5b0ba 100644
--- a/core/modules/block/tests/src/Unit/Plugin/DisplayVariant/BlockPageVariantTest.php
+++ b/core/modules/block/tests/src/Unit/Plugin/DisplayVariant/BlockPageVariantTest.php
@@ -57,9 +57,8 @@ class BlockPageVariantTest extends UnitTestCase {
->getMock();
$container->set('cache_contexts_manager', $cache_context_manager);
$cache_context_manager->expects($this->any())
- ->method('validateTokens')
- ->with([])
- ->willReturn([]);
+ ->method('assertValidTokens')
+ ->willReturn(TRUE);
\Drupal::setContainer($container);
$this->blockRepository = $this->getMock('Drupal\block\BlockRepositoryInterface');
diff --git a/core/modules/content_translation/tests/src/Unit/Access/ContentTranslationManageAccessCheckTest.php b/core/modules/content_translation/tests/src/Unit/Access/ContentTranslationManageAccessCheckTest.php
index 0818be2..8cb5881 100644
--- a/core/modules/content_translation/tests/src/Unit/Access/ContentTranslationManageAccessCheckTest.php
+++ b/core/modules/content_translation/tests/src/Unit/Access/ContentTranslationManageAccessCheckTest.php
@@ -40,6 +40,7 @@ class ContentTranslationManageAccessCheckTest extends UnitTestCase {
$this->cacheContextsManager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
->disableOriginalConstructor()
->getMock();
+ $this->cacheContextsManager->method('assertValidTokens')->willReturn(TRUE);
$container = new ContainerBuilder();
$container->set('cache_contexts_manager', $this->cacheContextsManager);
diff --git a/core/modules/forum/tests/src/Unit/Breadcrumb/ForumBreadcrumbBuilderBaseTest.php b/core/modules/forum/tests/src/Unit/Breadcrumb/ForumBreadcrumbBuilderBaseTest.php
index f690e03..3cb442a 100644
--- a/core/modules/forum/tests/src/Unit/Breadcrumb/ForumBreadcrumbBuilderBaseTest.php
+++ b/core/modules/forum/tests/src/Unit/Breadcrumb/ForumBreadcrumbBuilderBaseTest.php
@@ -27,8 +27,7 @@ class ForumBreadcrumbBuilderBaseTest extends UnitTestCase {
$cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
->disableOriginalConstructor()
->getMock();
- $cache_contexts_manager->expects($this->any())
- ->method('validate_tokens');
+ $cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);
$container = new Container();
$container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
diff --git a/core/modules/forum/tests/src/Unit/Breadcrumb/ForumListingBreadcrumbBuilderTest.php b/core/modules/forum/tests/src/Unit/Breadcrumb/ForumListingBreadcrumbBuilderTest.php
index 0d201ed..b591394 100644
--- a/core/modules/forum/tests/src/Unit/Breadcrumb/ForumListingBreadcrumbBuilderTest.php
+++ b/core/modules/forum/tests/src/Unit/Breadcrumb/ForumListingBreadcrumbBuilderTest.php
@@ -28,8 +28,7 @@ class ForumListingBreadcrumbBuilderTest extends UnitTestCase {
$cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
->disableOriginalConstructor()
->getMock();
- $cache_contexts_manager->expects($this->any())
- ->method('validate_tokens');
+ $cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);
$container = new Container();
$container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
diff --git a/core/modules/forum/tests/src/Unit/Breadcrumb/ForumNodeBreadcrumbBuilderTest.php b/core/modules/forum/tests/src/Unit/Breadcrumb/ForumNodeBreadcrumbBuilderTest.php
index 76851fd..ba1c974 100644
--- a/core/modules/forum/tests/src/Unit/Breadcrumb/ForumNodeBreadcrumbBuilderTest.php
+++ b/core/modules/forum/tests/src/Unit/Breadcrumb/ForumNodeBreadcrumbBuilderTest.php
@@ -27,8 +27,7 @@ class ForumNodeBreadcrumbBuilderTest extends UnitTestCase {
$cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
->disableOriginalConstructor()
->getMock();
- $cache_contexts_manager->expects($this->any())
- ->method('validate_tokens');
+ $cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);
$container = new Container();
$container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
diff --git a/core/modules/language/tests/src/Unit/LanguageNegotiationUrlTest.php b/core/modules/language/tests/src/Unit/LanguageNegotiationUrlTest.php
index 34abb61..d0d946e 100644
--- a/core/modules/language/tests/src/Unit/LanguageNegotiationUrlTest.php
+++ b/core/modules/language/tests/src/Unit/LanguageNegotiationUrlTest.php
@@ -60,6 +60,7 @@ class LanguageNegotiationUrlTest extends UnitTestCase {
$cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
->disableOriginalConstructor()
->getMock();
+ $cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);
$container = new ContainerBuilder();
$container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
diff --git a/core/modules/quickedit/tests/src/Unit/Access/EditEntityFieldAccessCheckTest.php b/core/modules/quickedit/tests/src/Unit/Access/EditEntityFieldAccessCheckTest.php
index 26a1c86..5ac92aa 100644
--- a/core/modules/quickedit/tests/src/Unit/Access/EditEntityFieldAccessCheckTest.php
+++ b/core/modules/quickedit/tests/src/Unit/Access/EditEntityFieldAccessCheckTest.php
@@ -34,7 +34,9 @@ class EditEntityFieldAccessCheckTest extends UnitTestCase {
protected function setUp() {
$this->editAccessCheck = new EditEntityFieldAccessCheck();
- $cache_contexts_manager = $this->prophesize(CacheContextsManager::class)->reveal();
+ $cache_contexts_manager = $this->prophesize(CacheContextsManager::class);
+ $cache_contexts_manager->assertValidTokens()->willReturn(TRUE);
+ $cache_contexts_manager->reveal();
$container = new Container();
$container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
diff --git a/core/modules/simpletest/src/TestBase.php b/core/modules/simpletest/src/TestBase.php
index 9bf67aa..7abfa2f 100644
--- a/core/modules/simpletest/src/TestBase.php
+++ b/core/modules/simpletest/src/TestBase.php
@@ -969,6 +969,10 @@ abstract class TestBase {
$this->httpAuthCredentials = $username . ':' . $password;
}
+ // Force assertion failures to be thrown as AssertionError for PHP 5 & 7
+ // compatibility.
+ \Drupal\Component\Assertion\Handle::register();
+
set_error_handler(array($this, 'errorHandler'));
// Iterate through all the methods in this class, unless a specific list of
// methods to run was passed.
diff --git a/core/modules/system/src/Tests/Cache/GenericCacheBackendUnitTestBase.php b/core/modules/system/src/Tests/Cache/GenericCacheBackendUnitTestBase.php
index cc0d9df..742bd46 100644
--- a/core/modules/system/src/Tests/Cache/GenericCacheBackendUnitTestBase.php
+++ b/core/modules/system/src/Tests/Cache/GenericCacheBackendUnitTestBase.php
@@ -218,13 +218,13 @@ abstract class GenericCacheBackendUnitTestBase extends KernelTestBase {
$this->assertEqual('value', $backend->get('TEST8')->data);
$this->assertFalse($backend->get('test8'));
- // Calling ::set() with invalid cache tags.
+ // Calling ::set() with invalid cache tags. This should fail an assertion.
try {
- $backend->set('exception_test', 'value', Cache::PERMANENT, ['node' => [3, 5, 7]]);
- $this->fail('::set() was called with invalid cache tags, no exception was thrown.');
+ $backend->set('assertion_test', 'value', Cache::PERMANENT, ['node' => [3, 5, 7]]);
+ $this->fail('::set() was called with invalid cache tags, runtime assertion did not fail.');
}
- catch (\LogicException $e) {
- $this->pass('::set() was called with invalid cache tags, an exception was thrown.');
+ catch (\AssertionError $e) {
+ $this->pass('::set() was called with invalid cache tags, runtime assertion failed.');
}
}
@@ -412,7 +412,8 @@ abstract class GenericCacheBackendUnitTestBase extends KernelTestBase {
$this->assertEqual($cached['cid_5']->data, $items['cid_5']['data'], 'New cache item set correctly.');
- // Calling ::setMultiple() with invalid cache tags.
+ // Calling ::setMultiple() with invalid cache tags. This should fail an
+ // assertion.
try {
$items = [
'exception_test_1' => array('data' => 1, 'tags' => []),
@@ -420,10 +421,10 @@ abstract class GenericCacheBackendUnitTestBase extends KernelTestBase {
'exception_test_3' => array('data' => 3, 'tags' => ['node' => [3, 5, 7]]),
];
$backend->setMultiple($items);
- $this->fail('::setMultiple() was called with invalid cache tags, no exception was thrown.');
+ $this->fail('::setMultiple() was called with invalid cache tags, runtime assertion did not fail.');
}
- catch (\LogicException $e) {
- $this->pass('::setMultiple() was called with invalid cache tags, an exception was thrown.');
+ catch (\AssertionError $e) {
+ $this->pass('::setMultiple() was called with invalid cache tags, runtime assertion failed.');
}
}
diff --git a/core/modules/system/tests/src/Unit/Breadcrumbs/PathBasedBreadcrumbBuilderTest.php b/core/modules/system/tests/src/Unit/Breadcrumbs/PathBasedBreadcrumbBuilderTest.php
index bf106f6..64e61c7 100644
--- a/core/modules/system/tests/src/Unit/Breadcrumbs/PathBasedBreadcrumbBuilderTest.php
+++ b/core/modules/system/tests/src/Unit/Breadcrumbs/PathBasedBreadcrumbBuilderTest.php
@@ -124,6 +124,7 @@ class PathBasedBreadcrumbBuilderTest extends UnitTestCase {
$cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
->disableOriginalConstructor()
->getMock();
+ $cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);
$cache_contexts_manager->expects($this->any())
->method('validate_tokens');
$container = new Container();
diff --git a/core/modules/system/tests/src/Unit/Menu/MenuLinkTreeTest.php b/core/modules/system/tests/src/Unit/Menu/MenuLinkTreeTest.php
index 9faa03d..ac6af14 100644
--- a/core/modules/system/tests/src/Unit/Menu/MenuLinkTreeTest.php
+++ b/core/modules/system/tests/src/Unit/Menu/MenuLinkTreeTest.php
@@ -47,6 +47,7 @@ class MenuLinkTreeTest extends UnitTestCase {
$cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
->disableOriginalConstructor()
->getMock();
+ $cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);
$container = new ContainerBuilder();
$container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
diff --git a/core/modules/user/tests/src/Unit/PermissionAccessCheckTest.php b/core/modules/user/tests/src/Unit/PermissionAccessCheckTest.php
index 29b6247..7ad78ef 100644
--- a/core/modules/user/tests/src/Unit/PermissionAccessCheckTest.php
+++ b/core/modules/user/tests/src/Unit/PermissionAccessCheckTest.php
@@ -42,7 +42,9 @@ class PermissionAccessCheckTest extends UnitTestCase {
parent::setUp();
$this->container = new ContainerBuilder();
- $cache_contexts_manager = $this->prophesize(CacheContextsManager::class)->reveal();
+ $cache_contexts_manager = $this->prophesize(CacheContextsManager::class);
+ $cache_contexts_manager->assertValidTokens()->willReturn(TRUE);
+ $cache_contexts_manager->reveal();
$this->container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($this->container);
diff --git a/core/modules/user/tests/src/Unit/UserAccessControlHandlerTest.php b/core/modules/user/tests/src/Unit/UserAccessControlHandlerTest.php
index 849cde5..26bd3a9 100644
--- a/core/modules/user/tests/src/Unit/UserAccessControlHandlerTest.php
+++ b/core/modules/user/tests/src/Unit/UserAccessControlHandlerTest.php
@@ -64,7 +64,9 @@ class UserAccessControlHandlerTest extends UnitTestCase {
public function setUp() {
parent::setUp();
- $cache_contexts_manager = $this->prophesize(CacheContextsManager::class)->reveal();
+ $cache_contexts_manager = $this->prophesize(CacheContextsManager::class);
+ $cache_contexts_manager->assertValidTokens()->willReturn(TRUE);
+ $cache_contexts_manager->reveal();
$container = new Container();
$container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
diff --git a/core/tests/Drupal/Tests/Component/Assertion/InspectorTest.php b/core/tests/Drupal/Tests/Component/Assertion/InspectorTest.php
index 55b09d8..7c36bda 100644
--- a/core/tests/Drupal/Tests/Component/Assertion/InspectorTest.php
+++ b/core/tests/Drupal/Tests/Component/Assertion/InspectorTest.php
@@ -31,12 +31,36 @@ class InspectorTest extends PHPUnit_Framework_TestCase {
* Tests asserting all members are strings.
*
* @covers ::assertAllStrings
+ * @dataProvider providerTestAssertAllStrings
*/
- public function testAssertAllStrings() {
- $this->assertTrue(Inspector::assertAllStrings([]));
- $this->assertTrue(Inspector::assertAllStrings(['foo', 'bar']));
- $this->assertFalse(Inspector::assertAllStrings('foo'));
- $this->assertFalse(Inspector::assertAllStrings(['foo', new StringObject()]));
+ public function testAssertAllStrings($input, $expected) {
+ $this->assertSame($expected, Inspector::assertAllStrings($input));
+ }
+
+ public function providerTestAssertAllStrings() {
+ $data = [
+ 'empty-array' => [[], TRUE],
+ 'array-with-strings' => [['foo', 'bar'], TRUE],
+ 'string' => ['foo', FALSE],
+ 'array-with-strings-with-colon' => [['foo', 'bar', 'llama:2001988', 'baz', 'llama:14031991'], TRUE],
+
+ 'with-FALSE' => [[FALSE], FALSE],
+ 'with-TRUE' => [[TRUE], FALSE],
+ 'with-string-and-boolean' => [['foo', FALSE], FALSE],
+ 'with-NULL' => [[NULL], FALSE],
+ 'string-with-NULL' => [['foo', NULL], FALSE],
+ 'integer' => [[1337], FALSE],
+ 'string-and-integer' => [['foo', 1337], FALSE],
+ 'double' => [[3.14], FALSE],
+ 'string-and-double' => [['foo', 3.14], FALSE],
+ 'array' => [[[]], FALSE],
+ 'string-and-array' => [['foo', []], FALSE],
+ 'string-and-nested-array' => [['foo', ['bar']], FALSE],
+ 'object' => [[new \stdClass()], FALSE],
+ 'string-and-object' => [['foo', new StringObject()], FALSE],
+ ];
+
+ return $data;
}
/**
diff --git a/core/tests/Drupal/Tests/Core/Access/AccessResultTest.php b/core/tests/Drupal/Tests/Core/Access/AccessResultTest.php
index ae5dadc..2c46a0b 100644
--- a/core/tests/Drupal/Tests/Core/Access/AccessResultTest.php
+++ b/core/tests/Drupal/Tests/Core/Access/AccessResultTest.php
@@ -38,6 +38,7 @@ class AccessResultTest extends UnitTestCase {
->disableOriginalConstructor()
->getMock();
+ $this->cacheContextsManager->method('assertValidTokens')->willReturn(TRUE);
$container = new ContainerBuilder();
$container->set('cache_contexts_manager', $this->cacheContextsManager);
\Drupal::setContainer($container);
diff --git a/core/tests/Drupal/Tests/Core/Breadcrumb/BreadcrumbManagerTest.php b/core/tests/Drupal/Tests/Core/Breadcrumb/BreadcrumbManagerTest.php
index a9f301d..fd5be62 100644
--- a/core/tests/Drupal/Tests/Core/Breadcrumb/BreadcrumbManagerTest.php
+++ b/core/tests/Drupal/Tests/Core/Breadcrumb/BreadcrumbManagerTest.php
@@ -57,7 +57,9 @@ class BreadcrumbManagerTest extends UnitTestCase {
$this->breadcrumb = new Breadcrumb();
$this->container = new ContainerBuilder();
- $cache_contexts_manager = $this->prophesize(CacheContextsManager::class)->reveal();
+ $cache_contexts_manager = $this->prophesize(CacheContextsManager::class);
+ $cache_contexts_manager->assertValidTokens()->willReturn(TRUE);
+ $cache_contexts_manager->reveal();
$this->container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($this->container);
}
diff --git a/core/tests/Drupal/Tests/Core/Cache/CacheTagsInvalidatorTest.php b/core/tests/Drupal/Tests/Core/Cache/CacheTagsInvalidatorTest.php
index a2daba7..80f6756 100644
--- a/core/tests/Drupal/Tests/Core/Cache/CacheTagsInvalidatorTest.php
+++ b/core/tests/Drupal/Tests/Core/Cache/CacheTagsInvalidatorTest.php
@@ -20,8 +20,7 @@ class CacheTagsInvalidatorTest extends UnitTestCase {
/**
* @covers ::invalidateTags
*
- * @expectedException \LogicException
- * @expectedExceptionMessage Cache tags must be strings, array given.
+ * @expectedException \AssertionError
*/
public function testInvalidateTagsWithInvalidTags() {
$cache_tags_invalidator = new CacheTagsInvalidator();
diff --git a/core/tests/Drupal/Tests/Core/Cache/CacheTest.php b/core/tests/Drupal/Tests/Core/Cache/CacheTest.php
index c2e61a1..6c30996 100644
--- a/core/tests/Drupal/Tests/Core/Cache/CacheTest.php
+++ b/core/tests/Drupal/Tests/Core/Cache/CacheTest.php
@@ -7,6 +7,7 @@
namespace Drupal\Tests\Core\Cache;
+use Drupal\Component\Assertion\Inspector;
use Drupal\Core\Cache\Cache;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -59,6 +60,7 @@ class CacheTest extends UnitTestCase {
$this->assertNull(Cache::validateTags($tags));
}
+
/**
* Provides a list of pairs of cache tags arrays to be merged.
*
diff --git a/core/tests/Drupal/Tests/Core/Cache/CacheableMetadataTest.php b/core/tests/Drupal/Tests/Core/Cache/CacheableMetadataTest.php
index 05b51aa..b73bec2 100644
--- a/core/tests/Drupal/Tests/Core/Cache/CacheableMetadataTest.php
+++ b/core/tests/Drupal/Tests/Core/Cache/CacheableMetadataTest.php
@@ -35,6 +35,8 @@ class CacheableMetadataTest extends UnitTestCase {
$cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
->disableOriginalConstructor()
->getMock();
+ $cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);
+
$container = new ContainerBuilder();
$container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
@@ -57,6 +59,7 @@ class CacheableMetadataTest extends UnitTestCase {
$cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
->disableOriginalConstructor()
->getMock();
+ $cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);
$container = new ContainerBuilder();
$container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
diff --git a/core/tests/Drupal/Tests/Core/Cache/Context/CacheContextsManagerTest.php b/core/tests/Drupal/Tests/Core/Cache/Context/CacheContextsManagerTest.php
index db8d7b7..6b233aa 100644
--- a/core/tests/Drupal/Tests/Core/Cache/Context/CacheContextsManagerTest.php
+++ b/core/tests/Drupal/Tests/Core/Cache/Context/CacheContextsManagerTest.php
@@ -106,8 +106,7 @@ class CacheContextsManagerTest extends UnitTestCase {
/**
* @covers ::convertTokensToKeys
*
- * @expectedException \LogicException
- * @expectedExceptionMessage "non-cache-context" is not a valid cache context ID.
+ * @expectedException \AssertionError
*/
public function testInvalidContext() {
$container = $this->getMockContainer();
diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityCreateAccessCheckTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityCreateAccessCheckTest.php
index b0b4c8c..e062cc9 100644
--- a/core/tests/Drupal/Tests/Core/Entity/EntityCreateAccessCheckTest.php
+++ b/core/tests/Drupal/Tests/Core/Entity/EntityCreateAccessCheckTest.php
@@ -35,7 +35,9 @@ class EntityCreateAccessCheckTest extends UnitTestCase {
protected function setUp() {
parent::setUp();
- $cache_contexts_manager = $this->prophesize(CacheContextsManager::class)->reveal();
+ $cache_contexts_manager = $this->prophesize(CacheContextsManager::class);
+ $cache_contexts_manager->assertValidTokens()->willReturn(TRUE);
+ $cache_contexts_manager->reveal();
$container = new Container();
$container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php
index 929ff00..3a21211 100644
--- a/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php
+++ b/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php
@@ -518,6 +518,8 @@ class EntityUnitTest extends UnitTestCase {
$cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
->disableOriginalConstructor()
->getMock();
+ $cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);
+
$container = new ContainerBuilder();
$container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
diff --git a/core/tests/Drupal/Tests/Core/Menu/DefaultMenuLinkTreeManipulatorsTest.php b/core/tests/Drupal/Tests/Core/Menu/DefaultMenuLinkTreeManipulatorsTest.php
index a748c5b..4511ece 100644
--- a/core/tests/Drupal/Tests/Core/Menu/DefaultMenuLinkTreeManipulatorsTest.php
+++ b/core/tests/Drupal/Tests/Core/Menu/DefaultMenuLinkTreeManipulatorsTest.php
@@ -81,7 +81,10 @@ class DefaultMenuLinkTreeManipulatorsTest extends UnitTestCase {
$this->defaultMenuTreeManipulators = new DefaultMenuLinkTreeManipulators($this->accessManager, $this->currentUser, $this->queryFactory);
- $cache_contexts_manager = $this->prophesize(CacheContextsManager::class)->reveal();
+ $cache_contexts_manager = $this->prophesize(CacheContextsManager::class);
+ $cache_contexts_manager->assertValidTokens()->willReturn(TRUE);
+ $cache_contexts_manager->reveal();
+
$container = new Container();
$container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
diff --git a/core/tests/Drupal/Tests/Core/Menu/LocalTaskManagerTest.php b/core/tests/Drupal/Tests/Core/Menu/LocalTaskManagerTest.php
index 20c680c..c69dcf2 100644
--- a/core/tests/Drupal/Tests/Core/Menu/LocalTaskManagerTest.php
+++ b/core/tests/Drupal/Tests/Core/Menu/LocalTaskManagerTest.php
@@ -488,6 +488,10 @@ class LocalTaskManagerTest extends UnitTestCase {
$cache_context_manager = $this->prophesize(CacheContextsManager::class);
+ foreach ([NULL, ['user.permissions'], ['route'], ['route', 'context.example1'], ['context.example1', 'route'], ['context.example1', 'route', 'context.example2'], ['context.example1', 'context.example2', 'route'], ['context.example1', 'context.example2', 'route', 'user.permissions']] as $argument) {
+ $cache_context_manager->assertValidTokens($argument)->willReturn(TRUE);
+ }
+
$container->set('cache_contexts_manager', $cache_context_manager->reveal());
\Drupal::setContainer($container);
}
diff --git a/core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php b/core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php
index e695c79..72e5893 100644
--- a/core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php
+++ b/core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php
@@ -60,6 +60,7 @@ class BubbleableMetadataTest extends UnitTestCase {
$cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
->disableOriginalConstructor()
->getMock();
+ $cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);
$container = new ContainerBuilder();
$container->set('cache_contexts_manager', $cache_contexts_manager);
$container->set('renderer', $renderer);
@@ -664,6 +665,7 @@ class BubbleableMetadataTest extends UnitTestCase {
$cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
->disableOriginalConstructor()
->getMock();
+ $cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);
$container = new ContainerBuilder();
$container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
diff --git a/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php b/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php
index e903739..199d170 100644
--- a/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php
+++ b/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php
@@ -144,6 +144,7 @@ class RendererTestBase extends UnitTestCase {
$this->cacheContextsManager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
->disableOriginalConstructor()
->getMock();
+ $this->cacheContextsManager->method('assertValidTokens')->willReturn(TRUE);
$current_user_role = &$this->currentUserRole;
$this->cacheContextsManager->expects($this->any())
->method('convertTokensToKeys')
diff --git a/core/tests/Drupal/Tests/Core/Route/RoleAccessCheckTest.php b/core/tests/Drupal/Tests/Core/Route/RoleAccessCheckTest.php
index 9d0d148..6d04fb7 100644
--- a/core/tests/Drupal/Tests/Core/Route/RoleAccessCheckTest.php
+++ b/core/tests/Drupal/Tests/Core/Route/RoleAccessCheckTest.php
@@ -145,7 +145,9 @@ class RoleAccessCheckTest extends UnitTestCase {
* @dataProvider roleAccessProvider
*/
public function testRoleAccess($path, $grant_accounts, $deny_accounts) {
- $cache_contexts_manager = $this->prophesize(CacheContextsManager::class)->reveal();
+ $cache_contexts_manager = $this->prophesize(CacheContextsManager::class);
+ $cache_contexts_manager->assertValidTokens()->willReturn(TRUE);
+ $cache_contexts_manager->reveal();
$container = new Container();
$container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
diff --git a/core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php b/core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php
index 2e04b87..6607cb5 100644
--- a/core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php
+++ b/core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php
@@ -63,6 +63,7 @@ class UrlGeneratorTest extends UnitTestCase {
$cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
->disableOriginalConstructor()
->getMock();
+ $cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);
$container = new ContainerBuilder();
$container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);