core/lib/Drupal/Core/Cache/CacheableMetadata.php | 7 +- .../Tests/Core/Cache/CacheableMetadataTest.php | 74 ++++++++++++++++++++++ .../Tests/Core/Render/BubbleableMetadataTest.php | 35 ---------- 3 files changed, 77 insertions(+), 39 deletions(-) diff --git a/core/lib/Drupal/Core/Cache/CacheableMetadata.php b/core/lib/Drupal/Core/Cache/CacheableMetadata.php index c00ff13..d21bd65 100644 --- a/core/lib/Drupal/Core/Cache/CacheableMetadata.php +++ b/core/lib/Drupal/Core/Cache/CacheableMetadata.php @@ -132,13 +132,11 @@ public function setCacheMaxAge($max_age) { /** * Merges the values of another CacheableMetadata object with this one. * - * @param \Drupal\Core\Render\CacheableMetadata $other + * @param \Drupal\Core\Cache\CacheableMetadata $other * The other CacheableMetadata object. + * * @return static * A new CacheableMetadata object, with the merged data. - * - * @todo Add unit test for this in - * \Drupal\Tests\Core\Render\CacheableMetadataTest when */ public function merge(CacheableMetadata $other) { $result = new static(); @@ -199,4 +197,5 @@ public static function createFromObject($object) { $meta->maxAge = 0; return $meta; } + } diff --git a/core/tests/Drupal/Tests/Core/Cache/CacheableMetadataTest.php b/core/tests/Drupal/Tests/Core/Cache/CacheableMetadataTest.php index 7918d60..3a3856c 100644 --- a/core/tests/Drupal/Tests/Core/Cache/CacheableMetadataTest.php +++ b/core/tests/Drupal/Tests/Core/Cache/CacheableMetadataTest.php @@ -7,7 +7,9 @@ namespace Drupal\Tests\Core\Cache; +use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheableMetadata; +use Drupal\Tests\Core\Render\TestCacheableDependency; use Drupal\Tests\UnitTestCase; use Drupal\Core\Render\Element; @@ -69,4 +71,76 @@ public function providerSetCacheMaxAge() { [8.0, TRUE] ]; } + + /** + * @covers ::createFromRenderArray + * @dataProvider providerTestCreateFromRenderArray + */ + public function testCreateFromRenderArray(array $render_array, CacheableMetadata $expected) { + $this->assertEquals($expected, CacheableMetadata::createFromRenderArray($render_array)); + } + + /** + * Provides test data for createFromRenderArray(). + * + * @return array + */ + public function providerTestCreateFromRenderArray() { + $data = []; + + $empty_metadata = new CacheableMetadata(); + $nonempty_metadata = new CacheableMetadata(); + $nonempty_metadata->setCacheContexts(['qux']) + ->setCacheTags(['foo:bar']); + + $empty_render_array = []; + $nonempty_render_array = [ + '#cache' => [ + 'contexts' => ['qux'], + 'tags' => ['foo:bar'], + 'max-age' => Cache::PERMANENT, + ], + ]; + + $data[] = [$empty_render_array, $empty_metadata]; + $data[] = [$nonempty_render_array, $nonempty_metadata]; + + return $data; + } + + /** + * @covers ::createFromObject + * @dataProvider providerTestCreateFromObject + */ + public function testCreateFromObject($object, CacheableMetadata $expected) { + $this->assertEquals($expected, CacheableMetadata::createFromObject($object)); + } + + /** + * Provides test data for createFromObject(). + * + * @return array + */ + public function providerTestCreateFromObject() { + $data = []; + + $empty_metadata = new CacheableMetadata(); + $nonempty_metadata = new CacheableMetadata(); + $nonempty_metadata->setCacheContexts(['qux']) + ->setCacheTags(['foo:bar']) + ->setCacheMaxAge(600); + $uncacheable_metadata = new CacheableMetadata(); + $uncacheable_metadata->setCacheMaxAge(0); + + $empty_cacheable_object = new TestCacheableDependency([], [], Cache::PERMANENT); + $nonempty_cacheable_object = new TestCacheableDependency(['qux'], ['foo:bar'], 600); + $uncacheable_object = new \stdClass(); + + $data[] = [$empty_cacheable_object, $empty_metadata]; + $data[] = [$nonempty_cacheable_object, $nonempty_metadata]; + $data[] = [$uncacheable_object, $uncacheable_metadata]; + + return $data; + } + } diff --git a/core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php b/core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php index 721d992..4c25860 100644 --- a/core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php +++ b/core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php @@ -132,39 +132,4 @@ public function providerTestCreateFromRenderArray() { return $data; } - /** - * @covers ::createFromObject - * @dataProvider providerTestCreateFromObject - */ - public function testCreateFromObject($object, BubbleableMetadata $expected) { - $this->assertEquals($expected, BubbleableMetadata::createFromObject($object)); - } - - /** - * Provides test data for createFromObject(). - * - * @return array - */ - public function providerTestCreateFromObject() { - $data = []; - - $empty_metadata = new BubbleableMetadata(); - $nonempty_metadata = new BubbleableMetadata(); - $nonempty_metadata->setCacheContexts(['qux']) - ->setCacheTags(['foo:bar']) - ->setCacheMaxAge(600); - $uncacheable_metadata = new BubbleableMetadata(); - $uncacheable_metadata->setCacheMaxAge(0); - - $empty_cacheable_object = new TestCacheableDependency([], [], Cache::PERMANENT); - $nonempty_cacheable_object = new TestCacheableDependency(['qux'], ['foo:bar'], 600); - $uncacheable_object = new \stdClass(); - - $data[] = [$empty_cacheable_object, $empty_metadata]; - $data[] = [$nonempty_cacheable_object, $nonempty_metadata]; - $data[] = [$uncacheable_object, $uncacheable_metadata]; - - return $data; - } - }