View source
<?php
namespace Drupal\Tests\akamai\Unit\Plugin\Purge\Purger;
use Drupal\Tests\UnitTestCase;
use Drupal\Tests\akamai\Kernel\EventSubscriber\MockSubscriber;
use Drupal\akamai\Event\AkamaiPurgeEvents;
use Drupal\akamai\Helper\CacheTagFormatter;
use Drupal\akamai\Plugin\Purge\Purger\AkamaiTagPurger;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\EventDispatcher\EventDispatcher;
class AkamaiTagPurgerTest extends UnitTestCase {
public function testPurgeCreationEvent() {
$purger = $this
->getMockBuilder('Drupal\\akamai\\Plugin\\Purge\\Purger\\AkamaiTagPurger')
->disableOriginalConstructor()
->setMethods(NULL)
->getMock();
$formatter = new CacheTagFormatter();
$container = new ContainerBuilder();
$container
->set('akamai.helper.cachetagformatter', $formatter);
\Drupal::setContainer($container);
$client = $this
->getMockBuilder('Drupal\\akamai\\Plugin\\Client\\AkamaiClientV3')
->disableOriginalConstructor()
->setMethods([
'setType',
'purgeTags',
])
->getMock();
$reflection = new \ReflectionClass($purger);
$reflection_property = $reflection
->getProperty('client');
$reflection_property
->setAccessible(TRUE);
$reflection_property
->setValue($purger, $client);
$subscriber = new MockSubscriber();
$event_dispatcher = new EventDispatcher();
$event_dispatcher
->addListener(AkamaiPurgeEvents::PURGE_CREATION, [
$subscriber,
'onPurgeCreation',
]);
$reflection_property = $reflection
->getProperty('eventDispatcher');
$reflection_property
->setAccessible(TRUE);
$reflection_property
->setValue($purger, $event_dispatcher);
$invalidation_1 = $this
->getMockBuilder('Drupal\\purge\\Plugin\\Purge\\Invalidation\\TagInvalidation')
->disableOriginalConstructor()
->getMock();
$invalidation_1
->method('getExpression')
->willReturn('foo');
$invalidation_2 = $this
->getMockBuilder('Drupal\\purge\\Plugin\\Purge\\Invalidation\\TagInvalidation')
->disableOriginalConstructor()
->getMock();
$invalidation_2
->method('getExpression')
->willReturn('foo');
$invalidation_3 = $this
->getMockBuilder('Drupal\\purge\\Plugin\\Purge\\Invalidation\\TagInvalidation')
->disableOriginalConstructor()
->getMock();
$invalidation_3
->method('getExpression')
->willReturn('bar');
$invalidation_4 = $this
->getMockBuilder('Drupal\\purge\\Plugin\\Purge\\Invalidation\\TagInvalidation')
->disableOriginalConstructor()
->getMock();
$invalidation_4
->method('getExpression')
->willReturn('123');
$invalidation_5 = $this
->getMockBuilder('Drupal\\purge\\Plugin\\Purge\\Invalidation\\TagInvalidation')
->disableOriginalConstructor()
->getMock();
$invalidation_5
->method('getExpression')
->willReturn('234');
$invalidation_6 = $this
->getMockBuilder('Drupal\\purge\\Plugin\\Purge\\Invalidation\\TagInvalidation')
->disableOriginalConstructor()
->getMock();
$invalidation_6
->method('getExpression')
->willReturn(123);
$invalidation_7 = $this
->getMockBuilder('Drupal\\purge\\Plugin\\Purge\\Invalidation\\TagInvalidation')
->disableOriginalConstructor()
->getMock();
$invalidation_7
->method('getExpression')
->willReturn(456);
$invalidation_8 = $this
->getMockBuilder('Drupal\\purge\\Plugin\\Purge\\Invalidation\\TagInvalidation')
->disableOriginalConstructor()
->getMock();
$invalidation_8
->method('getExpression')
->willReturn(1.01);
$invalidation_9 = $this
->getMockBuilder('Drupal\\purge\\Plugin\\Purge\\Invalidation\\TagInvalidation')
->disableOriginalConstructor()
->getMock();
$invalidation_9
->method('getExpression')
->willReturn(TRUE);
$purger
->invalidate([
$invalidation_1,
$invalidation_2,
$invalidation_3,
$invalidation_4,
$invalidation_5,
$invalidation_6,
$invalidation_7,
$invalidation_8,
$invalidation_9,
]);
$this
->assertSame([
'foo',
'bar',
'123',
'234',
'456',
'1.01',
'1',
'on_purge_creation',
], $subscriber->event->data);
}
public function testGetTimeHintReturnsCorrectValues() {
$akamai_client_factory = $this
->getMockBuilder('Drupal\\akamai\\AkamaiClientFactory')
->disableOriginalConstructor()
->getMock();
$akamai_client_factory
->method('get')
->willReturn(NULL);
$event_dispatcher = $this
->getMockBuilder('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface')
->getMock();
$result_map = [
'-5' => 0,
'0' => 0,
'7' => 7,
'9.5' => 9.5,
'10' => 10,
'10.5' => 10,
'11' => 10,
];
foreach ($result_map as $config_value => $returned_value) {
$config = $this
->getMockBuilder('Drupal\\Core\\Config\\ImmutableConfig')
->disableOriginalConstructor()
->getMock();
$config
->method('get')
->willReturn($config_value);
$config_factory = $this
->getMockBuilder('Drupal\\Core\\Config\\ConfigFactoryInterface')
->getMock();
$config_factory
->method('get')
->willReturn($config);
$logger = $this
->getMockBuilder('\\Psr\\Log\\LoggerInterface')
->getMock();
$purger = new AkamaiTagPurger([
'id' => 'my_id',
], 'my_id', 'my_definition', $config_factory, $event_dispatcher, $akamai_client_factory, $logger);
$this
->assertEquals($purger
->getTimeHint(), $returned_value);
}
}
public function testInvalidateTagLength() {
$long_tag = str_repeat('a', 129);
$logger = $this
->getMockBuilder('\\Drupal\\Core\\Logger\\LoggerChannel')
->disableOriginalConstructor()
->setMethods([
'warning',
])
->getMock();
$logger
->expects($this
->once())
->method('warning')
->with('Cache Tag %tag has exceeded the Akamai 128 character tag maximum length.', [
'%tag' => $long_tag,
]);
$purger = $this
->getMockBuilder('Drupal\\akamai\\Plugin\\Purge\\Purger\\AkamaiTagPurger')
->disableOriginalConstructor()
->setMethods(NULL)
->getMock();
$reflection = new \ReflectionClass($purger);
$reflection_property = $reflection
->getProperty('logger');
$reflection_property
->setAccessible(TRUE);
$reflection_property
->setValue($purger, $logger);
$formatter = new CacheTagFormatter();
$container = new ContainerBuilder();
$container
->set('akamai.helper.cachetagformatter', $formatter);
\Drupal::setContainer($container);
$client = $this
->getMockBuilder('Drupal\\akamai\\Plugin\\Client\\AkamaiClientV3')
->disableOriginalConstructor()
->setMethods([
'setType',
'purgeTags',
])
->getMock();
$reflection = new \ReflectionClass($purger);
$reflection_property = $reflection
->getProperty('client');
$reflection_property
->setAccessible(TRUE);
$reflection_property
->setValue($purger, $client);
$subscriber = new MockSubscriber();
$event_dispatcher = new EventDispatcher();
$event_dispatcher
->addListener(AkamaiPurgeEvents::PURGE_CREATION, [
$subscriber,
'onPurgeCreation',
]);
$reflection_property = $reflection
->getProperty('eventDispatcher');
$reflection_property
->setAccessible(TRUE);
$reflection_property
->setValue($purger, $event_dispatcher);
$invalidation = $this
->getMockBuilder('Drupal\\purge\\Plugin\\Purge\\Invalidation\\TagInvalidation')
->disableOriginalConstructor()
->getMock();
$invalidation
->method('setState')
->willReturn('foo');
$invalidation
->method('getExpression')
->willReturn($long_tag);
$purger
->invalidate([
$invalidation,
]);
}
}