You are here

class BlocacheMetadataUnitTest in Blocache (Block Cache Control) 8

Tests for BlocacheMetadata class.

@group blocache

Hierarchy

Expanded class hierarchy of BlocacheMetadataUnitTest

File

tests/src/Unit/BlocacheMetadataUnitTest.php, line 15

Namespace

Drupal\Tests\blocache\Unit
View source
class BlocacheMetadataUnitTest extends UnitTestCase {
  use BlockCreationTrait;

  /**
   * The blocache.metadata service.
   *
   * @var \Drupal\blocache\BlocacheMetadata
   */
  protected $blocacheMetadata;

  /**
   * The block entity.
   *
   * @var \Drupal\block\BlockInterface
   */
  protected $block;

  /**
   * Cache metadata for the tests.
   *
   * @var array
   */
  protected $metadata;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $this->blocacheMetadata = new BlocacheMetadata();
    $plugin = $this
      ->getMockBuilder('Drupal\\Core\\Block\\BlockBase')
      ->disableOriginalConstructor()
      ->getMock();
    $plugin
      ->expects($this
      ->any())
      ->method('getMachineNameSuggestion')
      ->will($this
      ->returnValue($this
      ->randomMachineName(8)));
    $block = $this
      ->getMockBuilder('Drupal\\block\\Entity\\Block')
      ->disableOriginalConstructor()
      ->getMock();
    $block
      ->expects($this
      ->any())
      ->method('getPlugin')
      ->will($this
      ->returnValue($plugin));
    $this->block = $block;
    $this->blocacheMetadata
      ->setBlock($this->block);
    $this->metadata = [
      BlocacheMetadata::METADATA_MAX_AGE => 600,
      BlocacheMetadata::METADATA_CONTEXTS => [
        'user',
        'language',
      ],
      BlocacheMetadata::METADATA_TAGS => [
        'user:1',
        'language:en',
      ],
    ];
  }

  /**
   * @covers Drupal\blocache\BlocacheMetadata::setBlock
   */
  public function testSetBlock() {
    $this
      ->assertInstanceOf(BlockInterface::class, $this->blocacheMetadata
      ->getBlock());
  }

  /**
   * @covers Drupal\blocache\BlocacheMetadata::setOverrides
   */
  public function testSetOverrides() {
    $max_age = $this->metadata[BlocacheMetadata::METADATA_MAX_AGE];
    $contexts = $this->metadata[BlocacheMetadata::METADATA_CONTEXTS];
    $tags = $this->metadata[BlocacheMetadata::METADATA_TAGS];
    $this
      ->assertEquals(TRUE, $this->blocacheMetadata
      ->setOverrides($max_age, $contexts, $tags));
    $this
      ->assertEquals(TRUE, $this->blocacheMetadata
      ->isOverridden());
    $this
      ->assertEquals($this->metadata, $this->blocacheMetadata
      ->getOverrides());
  }

  /**
   * @covers Drupal\blocache\BlocacheMetadata::unsetOverrides
   */
  public function testUnsetOverrides() {
    $max_age = $this->metadata[BlocacheMetadata::METADATA_MAX_AGE];
    $contexts = $this->metadata[BlocacheMetadata::METADATA_CONTEXTS];
    $tags = $this->metadata[BlocacheMetadata::METADATA_TAGS];
    $this
      ->assertEquals(TRUE, $this->blocacheMetadata
      ->setOverrides($max_age, $contexts, $tags));
    $this
      ->assertEquals(TRUE, $this->blocacheMetadata
      ->unsetOverrides());
    $this
      ->assertEquals([], $this->blocacheMetadata
      ->getOverrides());
  }

  /**
   * {@inheritdoc}
   */
  public function tearDown() {
    unset($this->blocacheMetadata);
    unset($this->block);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlocacheMetadataUnitTest::$blocacheMetadata protected property The blocache.metadata service.
BlocacheMetadataUnitTest::$block protected property The block entity.
BlocacheMetadataUnitTest::$metadata protected property Cache metadata for the tests.
BlocacheMetadataUnitTest::setUp public function Overrides UnitTestCase::setUp
BlocacheMetadataUnitTest::tearDown public function
BlocacheMetadataUnitTest::testSetBlock public function @covers Drupal\blocache\BlocacheMetadata::setBlock
BlocacheMetadataUnitTest::testSetOverrides public function @covers Drupal\blocache\BlocacheMetadata::setOverrides
BlocacheMetadataUnitTest::testUnsetOverrides public function @covers Drupal\blocache\BlocacheMetadata::unsetOverrides
BlockCreationTrait::placeBlock protected function Creates a block instance based on default settings.
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.