View source
<?php
namespace Drupal\Tests\purge_queuer_coretags\Unit;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface;
use Drupal\purge\Plugin\Purge\Invalidation\InvalidationsServiceInterface;
use Drupal\purge\Plugin\Purge\Queue\QueueServiceInterface;
use Drupal\purge\Plugin\Purge\Queuer\QueuerBase;
use Drupal\purge\Plugin\Purge\Queuers\QueuersServiceInterface;
use Drupal\purge_queuer_coretags\CacheTagsQueuer;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class CacheTagsQueuerTest extends UnitTestCase {
protected $cacheTagsQueuer;
protected $configFactory;
protected $container;
protected $purgeQueue;
protected $purgeQueuers;
protected $purgeInvalidationFactory;
protected function setUp() : void {
$this->purgeQueue = $this
->getMockBuilder(QueueServiceInterface::class)
->setMethods([])
->getMock();
$this->purgeQueuers = $this
->getMockBuilder(QueuersServiceInterface::class)
->setMethods([
'get',
])
->getMock();
$this->purgeInvalidationFactory = $this
->getMockForAbstractClass(InvalidationsServiceInterface::class);
$this->container = new ContainerBuilder();
$this->container
->set('purge.queue', $this->purgeQueue);
$this->container
->set('purge.queuers', $this->purgeQueuers);
$this->container
->set('purge.invalidation.factory', $this->purgeInvalidationFactory);
$this->cacheTagsQueuer = new CacheTagsQueuer();
$this->cacheTagsQueuer
->setContainer($this->container);
$this
->assertInstanceOf(ContainerAwareInterface::class, $this->cacheTagsQueuer);
$this
->assertInstanceOf(CacheTagsInvalidatorInterface::class, $this->cacheTagsQueuer);
}
public function testInitializeDoesntLoadWhenQueuerDisabled() : void {
$this->purgeInvalidationFactory
->expects($this
->never())
->method('get');
$this->purgeQueue
->expects($this
->never())
->method('add');
$this->purgeQueuers
->expects($this
->once())
->method('get')
->with('coretags')
->willReturn(FALSE);
$this
->assertNull($this->cacheTagsQueuer
->invalidateTags([
"1",
"2",
]));
}
public function testInvalidateTags($config, array $sets) : void {
$this->container
->set('config.factory', $this
->getConfigFactoryStub($config));
$this->purgeQueuers
->expects($this
->once())
->method('get')
->with('coretags')
->willReturn($this
->getMockBuilder(QueuerBase::class)
->disableOriginalConstructor()
->getMock());
$invs_added_total = array_sum(array_map(function ($set) {
return $set[1];
}, $sets));
$this->purgeInvalidationFactory
->expects($this
->exactly($invs_added_total))
->method('get')
->with('tag')
->willReturn($this
->createMock(InvalidationInterface::class));
$number_queue_add_calls = count(array_filter($sets, function ($set) {
return $set[1] !== 0;
}));
reset($sets);
$this->purgeQueue
->expects($this
->exactly($number_queue_add_calls))
->method('add')
->with($this
->callback(function ($queuer) {
return $queuer instanceof QueuerBase;
}), $this
->callback(function (array $invs) use (&$sets) {
$invs_added = current($sets)[1];
if ($invs_added === 0) {
next($sets);
$invs_added = current($sets)[1];
}
next($sets);
return is_array($invs) && count($invs) === $invs_added;
}));
foreach ($sets as $set) {
$this->cacheTagsQueuer
->invalidateTags($set[0]);
}
}
public function providerTestInvalidateTags() : array {
$blacklist = [
'purge_queuer_coretags.settings' => [
'blacklist' => [
'menu',
'node',
],
],
];
return [
[
$blacklist,
[
[
[
'block:1',
],
1,
],
[
[
'node:5',
],
0,
],
[
[
'extension:views',
'baz',
],
2,
],
],
],
[
$blacklist,
[
[
[
'menu:main',
],
0,
],
[
[
'NODE:5',
'foo',
'bar',
'bar',
],
3,
],
],
],
[
[
'purge_queuer_coretags.settings' => [
'blacklist' => [],
],
],
[
[
[
'node:5',
'foo:2',
'foo:3',
'bar:baz',
],
4,
],
],
],
[
[
'purge_queuer_coretags.settings' => [
'blacklist' => [],
],
],
[
[
[
'a',
'b',
],
2,
],
[
[
'c',
'd',
],
2,
],
[
[
'e',
'f',
],
2,
],
[
[
'g',
'h',
'i',
],
3,
],
[
[
'j',
],
1,
],
],
],
];
}
}