You are here

class WebformBubbleableMetadataTest in Webform 8.5

Same name and namespace in other branches
  1. 6.x tests/src/Unit/Cache/WebformBubbleableMetadataTest.php \Drupal\Tests\webform\Unit\Cache\WebformBubbleableMetadataTest

Tests webform bubbleable metadata.

@coversDefaultClass \Drupal\webform\Cache\WebformBubbleableMetadata @group Cache

Hierarchy

Expanded class hierarchy of WebformBubbleableMetadataTest

File

tests/src/Unit/Cache/WebformBubbleableMetadataTest.php, line 16

Namespace

Drupal\Tests\webform\Unit\Cache
View source
class WebformBubbleableMetadataTest extends UnitTestCase {

  /**
   * Tests appendTo renderable array.
   *
   * @param \Drupal\webform\Cache\WebformBubbleableMetadata $bubbleable_metadata
   *   Bubbleable metadata.
   * @param array $build
   *   A render array.
   * @param array $expected
   *   The expected render array.
   * @covers ::appendTo
   *
   * @dataProvider providerTestAppendTo
   *
   * @see \Drupal\Tests\Core\Cache\CacheableMetadataTest
   */
  public function testAppendTo(WebformBubbleableMetadata $bubbleable_metadata, array $build, array $expected) {

    // Mock CacheContextsManager::assertValidTokens
    // @see \Drupal\Core\Cache\Cache::mergeContexts
    $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);

    /**************************************************************************/
    $bubbleable_metadata
      ->appendTo($build);
    $this
      ->assertEquals($expected, $build);
  }

  /**
   * Provides test data for testAppendTo().
   *
   * @return array
   */
  public function providerTestAppendTo() {
    return [
      [
        (new WebformBubbleableMetadata())
          ->setCacheContexts([
          'bar',
        ]),
        [],
        [
          '#cache' => [
            'contexts' => [
              'bar',
            ],
            'tags' => [],
            'max-age' => Cache::PERMANENT,
          ],
          '#attached' => [],
        ],
      ],
      [
        (new WebformBubbleableMetadata())
          ->setCacheContexts([
          'bar',
        ]),
        [
          '#cache' => [
            'contexts' => [
              'bar',
            ],
          ],
        ],
        [
          '#cache' => [
            'contexts' => [
              'bar',
            ],
            'tags' => [],
            'max-age' => Cache::PERMANENT,
          ],
          '#attached' => [],
        ],
      ],
      [
        (new WebformBubbleableMetadata())
          ->setCacheContexts([
          'bar',
          'foo',
        ]),
        [
          '#cache' => [
            'contexts' => [
              'bar',
            ],
          ],
        ],
        [
          '#cache' => [
            'contexts' => [
              'bar',
              'foo',
            ],
            'tags' => [],
            'max-age' => Cache::PERMANENT,
          ],
          '#attached' => [],
        ],
      ],
      [
        (new WebformBubbleableMetadata())
          ->setCacheMaxAge(99),
        [],
        [
          '#cache' => [
            'contexts' => [],
            'tags' => [],
            'max-age' => 99,
          ],
          '#attached' => [],
        ],
      ],
      [
        (new WebformBubbleableMetadata())
          ->setCacheContexts([
          'bar',
        ]),
        [
          '#cache' => [
            'max-age' => 99,
          ],
        ],
        [
          '#cache' => [
            'contexts' => [
              'bar',
            ],
            'tags' => [],
            'max-age' => 99,
          ],
          '#attached' => [],
        ],
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340
WebformBubbleableMetadataTest::providerTestAppendTo public function Provides test data for testAppendTo().
WebformBubbleableMetadataTest::testAppendTo public function Tests appendTo renderable array.