diff --git a/core/lib/Drupal/Core/Cache/ApcuBackend.php b/core/lib/Drupal/Core/Cache/ApcuBackend.php index 9bac79e2b70b2f17830219895220f80e88404458..7f04f0e900b330477f8cc1a067166fbb4f885413 100644 --- a/core/lib/Drupal/Core/Cache/ApcuBackend.php +++ b/core/lib/Drupal/Core/Cache/ApcuBackend.php @@ -166,7 +166,7 @@ protected function prepareItem($cache, $allow_invalid) { * {@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 5ad9500bb8fbc0896c0772fd1cfb394dc70d89ab..d75eaff47f26e87e162b827c2b1e82b60156382e 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 @@ public static function mergeContexts(array $a = [], array $b = []) { * 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 @@ public static function mergeMaxAges($a = Cache::PERMANENT, $b = Cache::PERMANENT * @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 a6d8ab52daf264d0480c44c7c44901526c5efe2c..eb1ca57a5fdf0ba42e8e9ace7208a6711dc593c3 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 64a8eb0765073c7cf98ac56a37c4c4ef7f798d5b..69d97c0617a9797adfca4d7fe7041050f6995bb1 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 b01dc9eeadc9a934a5d2d0d21637cf94f4d626a2..81d67350aa9bf3edbf303ea9c8bdde0b64e93799 100644 --- a/core/lib/Drupal/Core/Cache/Context/CacheContextsManager.php +++ b/core/lib/Drupal/Core/Cache/Context/CacheContextsManager.php @@ -103,11 +103,9 @@ public function getLabels($include_calculated_cache_contexts = FALSE) { * 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 @@ public function validateTokens(array $context_tokens = []) { } } + /** + * 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 06c5dc2087527db0f0fd2310e3beb5d1067f96fa..7faa806dece395b8974598f4a5b13fcff5688797 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseBackend.php +++ b/core/lib/Drupal/Core/Cache/DatabaseBackend.php @@ -199,7 +199,7 @@ protected function doSetMultiple(array $items) { '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 5c7f9288aa4a0e99b5c31951d8e2578eadde2f74..5c93e06f1e16cc60ea84a239d60f13905a98184f 100644 --- a/core/lib/Drupal/Core/Cache/MemoryBackend.php +++ b/core/lib/Drupal/Core/Cache/MemoryBackend.php @@ -107,7 +107,7 @@ protected function prepareItem($cache, $allow_invalid) { * 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 761e394b995b18f408c07bacd668d3ea66e8fa91..80b8771d0c2bb697b5213f67fc04667ba00d3323 100644 --- a/core/lib/Drupal/Core/Cache/PhpBackend.php +++ b/core/lib/Drupal/Core/Cache/PhpBackend.php @@ -148,7 +148,7 @@ protected function prepareItem($cache, $allow_invalid) { * {@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 7c8799aed27410806adc77bc9ca52baa7145b164..5b7b86ba2f844cb295dc455ceee135caf691e1c4 100644 --- a/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php +++ b/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php @@ -154,7 +154,7 @@ public function __construct($subdir, \Traversable $namespaces, ModuleHandlerInte * 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 565253351d44ed6275fab3b8e6f8c39acd4e01b5..3c5b0ba05bd6208f0deaf6e4f326c6cbf8456f4a 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 @@ public function setUpDisplayVariant($configuration = array(), $definition = arra ->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 0818be237d74a239e05de3ca5dfd57b1cdb3da4f..8cb588183ce468acc2004c609658fa879839d444 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 @@ protected function setUp() { $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 f690e03e27ef9613a85b0e020ec1d8912adeebca..3cb442a8e47d81ee4b3a4a2acb6d44a99ee95960 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 @@ public function setUp() { $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 0d201ed6a6add1a42e864869f9c5716146079664..b59139491718470f3c7e9cf5f9ee5e4339436bd5 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 @@ public function setUp() { $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 76851fd2d23a5ae55f58648f73095c9cebdeec5c..ba1c97472cf1b48c5e0ee0c4c6d36806379fdb2d 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 @@ public function setUp() { $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 34abb6105d87423d77ca1813d63531b97cf04139..d0d946e064a8a01a3473afcd3aed2419ceec7ddb 100644 --- a/core/modules/language/tests/src/Unit/LanguageNegotiationUrlTest.php +++ b/core/modules/language/tests/src/Unit/LanguageNegotiationUrlTest.php @@ -60,6 +60,7 @@ protected function setUp() { $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 26a1c86810f9dcf9d2a0dccf12dd93482f9ae979..5ac92aa5cebeff8341139f2315410fc06f13d008 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 9bf67aad3f7902f2083e142449ce45d323b6247b..7abfa2f19e2653f798c29d1a91365e5caa838bb8 100644 --- a/core/modules/simpletest/src/TestBase.php +++ b/core/modules/simpletest/src/TestBase.php @@ -969,6 +969,10 @@ public function run(array $methods = array()) { $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 cc0d9df814335598833471e335ba7996225b2251..742bd464d7602f46cb06c006fe32e3624ae88769 100644 --- a/core/modules/system/src/Tests/Cache/GenericCacheBackendUnitTestBase.php +++ b/core/modules/system/src/Tests/Cache/GenericCacheBackendUnitTestBase.php @@ -218,13 +218,13 @@ public function testSetGet() { $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 @@ public function testSetMultiple() { $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 @@ public function testSetMultiple() { '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 bf106f6afefeef3810e2407e0e9e11002ada4978..64e61c7d86fd2b8992f4abfb826793418d86cfb7 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 @@ protected function setUp() { $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 9faa03db68492becee6a834568e6b362898e918c..ac6af14914b7867cef568752cd979e2e0860077e 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 @@ protected function setUp() { $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 29b6247a2bc3ed976789fdd7482342beac56b316..7ad78efd0d941a6442f8e1f936304f0652a9ed7c 100644 --- a/core/modules/user/tests/src/Unit/PermissionAccessCheckTest.php +++ b/core/modules/user/tests/src/Unit/PermissionAccessCheckTest.php @@ -42,7 +42,9 @@ protected function setUp() { 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 849cde5e4f9cfc87540461babed36c4c9ca4f43f..26bd3a98c3a11492af9c15ac4cc3306f30d3a83c 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 55b09d8d57f2d0860d2cb152b6c1fc032856273b..7c36bda05df54388b3b7e9f581a6cabf5b73307d 100644 --- a/core/tests/Drupal/Tests/Component/Assertion/InspectorTest.php +++ b/core/tests/Drupal/Tests/Component/Assertion/InspectorTest.php @@ -31,12 +31,36 @@ public function testAssertTraversable() { * 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 ae5dadc590ecb34a6c985289ac35cfa1fbb564c9..2c46a0b1df1e39ad759674f7359660a4a38c97e6 100644 --- a/core/tests/Drupal/Tests/Core/Access/AccessResultTest.php +++ b/core/tests/Drupal/Tests/Core/Access/AccessResultTest.php @@ -38,6 +38,7 @@ protected function setUp() { ->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 a9f301d5b836952a3f17627b8e8e3fab2076e7c8..fd5be62bd956a79b579b1a2be495b8e883cb463e 100644 --- a/core/tests/Drupal/Tests/Core/Breadcrumb/BreadcrumbManagerTest.php +++ b/core/tests/Drupal/Tests/Core/Breadcrumb/BreadcrumbManagerTest.php @@ -57,7 +57,9 @@ protected function setUp() { $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 a2daba7be47ee520dd6fe0b02b3626451d95624b..80f6756210bf69c18e9855383ee3761ac12b2999 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 c2e61a1b2ff23783b6bd228679416a089b261496..6c30996cc544a4cb860ab825b7f99ba1503e6413 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 @@ public function testValidateTags(array $tags, $expected_exception_message) { $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 05b51aad5e62ef788b936cc84a86fd546e252c88..b73bec2b7a083d4ba24601862d4f0118980a6adb 100644 --- a/core/tests/Drupal/Tests/Core/Cache/CacheableMetadataTest.php +++ b/core/tests/Drupal/Tests/Core/Cache/CacheableMetadataTest.php @@ -35,6 +35,8 @@ public function testMerge(CacheableMetadata $a, CacheableMetadata $b, CacheableM $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 @@ public function testAddCacheableDependency(CacheableMetadata $a, CacheableMetada $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 db8d7b70ae95be582e0b06ce60948a46a865dfbd..6b233aa4997fa00c0cebe996b4aee1a1484b0dd7 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 @@ public function testConvertTokensToKeys() { /** * @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 b0b4c8cd6367929ab81fd735ea7134b073b160f9..e062cc9ed8a059eb9e3bee46b43a6fec5b6f8e05 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 929ff004c6523049255c15d5c1101ab4d37b4fcd..3a2121193df7d1524af53fc35ae6d71f7b36bef0 100644 --- a/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php @@ -518,6 +518,8 @@ public function testCacheContexts() { $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 a748c5b71b0d398710c683568a49619f90f66b04..4511ece85fd73f8ff563c82e1faec19960f2d72f 100644 --- a/core/tests/Drupal/Tests/Core/Menu/DefaultMenuLinkTreeManipulatorsTest.php +++ b/core/tests/Drupal/Tests/Core/Menu/DefaultMenuLinkTreeManipulatorsTest.php @@ -81,7 +81,10 @@ protected function setUp() { $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 20c680cbc68fe8029aefd96a4658f1dab34cb8e6..c69dcf2be3a03b3eb9f109e4364fa37c701a9970 100644 --- a/core/tests/Drupal/Tests/Core/Menu/LocalTaskManagerTest.php +++ b/core/tests/Drupal/Tests/Core/Menu/LocalTaskManagerTest.php @@ -488,6 +488,10 @@ protected function setupNullCacheabilityMetadataValidation() { $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 e695c79239fa4e3b44ae4e664e4c069e56e7a0b5..72e589375133cfe5002509a7cba7e6e666297e3e 100644 --- a/core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php +++ b/core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php @@ -60,6 +60,7 @@ public function testMerge(BubbleableMetadata $a, CacheableMetadata $b, Bubbleabl $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 @@ public function testAddCacheableDependency(BubbleableMetadata $a, $b, Bubbleable $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 e90373959adc5b5ee1f455ef968182cceba497f6..199d17018492835294d2534109653184887f5e9f 100644 --- a/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php +++ b/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php @@ -144,6 +144,7 @@ protected function setUp() { $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 9d0d14814a21417e5f70351ef925b1bd72848569..6d04fb7354e19b011e7a67842b4da73ca3e5706a 100644 --- a/core/tests/Drupal/Tests/Core/Route/RoleAccessCheckTest.php +++ b/core/tests/Drupal/Tests/Core/Route/RoleAccessCheckTest.php @@ -145,7 +145,9 @@ public function roleAccessProvider() { * @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 2e04b8736729c358ced76864ecc331ac4eac8608..6607cb5cc953b0a6eb958a6e9de795cc35b0e064 100644 --- a/core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php +++ b/core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php @@ -63,6 +63,7 @@ protected function setUp() { $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);