BlocacheMetadataUnitTest.php in Blocache (Block Cache Control) 8        
                          
                  
                        
  
  
  
  
  
File
  tests/src/Unit/BlocacheMetadataUnitTest.php
  
    View source  
  <?php
namespace Drupal\Tests\blocache\Unit;
use Drupal\blocache\BlocacheMetadata;
use Drupal\block\BlockInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\Tests\block\Traits\BlockCreationTrait;
class BlocacheMetadataUnitTest extends UnitTestCase {
  use BlockCreationTrait;
  
  protected $blocacheMetadata;
  
  protected $block;
  
  protected $metadata;
  
  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',
      ],
    ];
  }
  
  public function testSetBlock() {
    $this
      ->assertInstanceOf(BlockInterface::class, $this->blocacheMetadata
      ->getBlock());
  }
  
  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());
  }
  
  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());
  }
  
  public function tearDown() {
    unset($this->blocacheMetadata);
    unset($this->block);
  }
}