silverstripe-framework/tests/php/Security/InheritedPermissionsFlusherTest.php

102 lines
4.0 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\Security\Tests;
use Psr\SimpleCache\CacheInterface;
use SilverStripe\Core\Cache\CacheFactory;
2017-12-11 04:46:21 +01:00
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;
2017-12-11 04:46:21 +01:00
use SilverStripe\Security\Group;
use SilverStripe\Security\InheritedPermissionFlusher;
use SilverStripe\Security\Member;
use SilverStripe\Security\Tests\InheritedPermissionsFlusherTest\TestCacheFlusher;
class InheritedPermissionsFlusherTest extends SapphireTest
{
protected static $fixture_file = 'InheritedPermissionsFlusherTest.yml';
2017-12-06 10:03:05 +01:00
protected function setUp()
{
parent::setUp();
// Set up a mock cache service
Injector::inst()->load([
CacheInterface::class . '.TestFlusherCache' => [
'factory' => CacheFactory::class,
'constructor' => ['namespace' => 'TestFlusherCache']
]
]);
}
public function testMemberFlushesPermissions()
{
$cache = Injector::inst()->create(CacheInterface::class . '.TestFlusherCache');
$flusher = new TestCacheFlusher($cache);
$extension = new InheritedPermissionFlusher();
$extension->setServices([$flusher]);
Injector::inst()->registerService($extension, InheritedPermissionFlusher::class);
$editor = $this->objFromFixture(Member::class, 'editor');
$admin = $this->objFromFixture(Member::class, 'admin');
$editorKey = $flusher->generateCacheKey(TestCacheFlusher::$categories[0], $editor->ID);
$adminKey = $flusher->generateCacheKey(TestCacheFlusher::$categories[0], $admin->ID);
$cache->set($editorKey, 'uncle');
$cache->set($adminKey, 'cheese');
$editor->flushCache();
$this->assertNull($cache->get($editorKey));
$this->assertEquals('cheese', $cache->get($adminKey));
$admin->flushCache();
$this->assertNull($cache->get($editorKey));
$this->assertNull($cache->get($adminKey));
}
public function testGroupFlushesPermissions()
{
$cache = Injector::inst()->create(CacheInterface::class . '.TestFlusherCache');
$flusher = new TestCacheFlusher($cache);
$extension = new InheritedPermissionFlusher();
$extension->setServices([$flusher]);
Injector::inst()->registerService($extension, InheritedPermissionFlusher::class);
2017-12-11 04:46:21 +01:00
/** @var Group $editors */
$editors = $this->objFromFixture(Group::class, 'editors');
2017-12-11 04:46:21 +01:00
/** @var Group $admins */
$admins = $this->objFromFixture(Group::class, 'admins');
// Populate the cache for all members in each group
foreach ($editors->Members() as $editor) {
$editorKey = $flusher->generateCacheKey(TestCacheFlusher::$categories[0], $editor->ID);
$cache->set($editorKey, 'uncle');
}
foreach ($admins->Members() as $admin) {
$adminKey = $flusher->generateCacheKey(TestCacheFlusher::$categories[0], $admin->ID);
$cache->set($adminKey, 'cheese');
}
// Clear the cache for all members in the editors group
$editors->flushCache();
foreach ($editors->Members() as $editor) {
$editorKey = $flusher->generateCacheKey(TestCacheFlusher::$categories[0], $editor->ID);
$this->assertNull($cache->get($editorKey));
}
// Admins group should be unaffected
foreach ($admins->Members() as $admin) {
$adminKey = $flusher->generateCacheKey(TestCacheFlusher::$categories[0], $admin->ID);
$this->assertEquals('cheese', $cache->get($adminKey));
}
$admins->flushCache();
// Admins now affected
foreach ($admins->Members() as $admin) {
$adminKey = $flusher->generateCacheKey(TestCacheFlusher::$categories[0], $admin->ID);
$this->assertNull($cache->get($adminKey));
}
foreach ($editors->Members() as $editor) {
$editorKey = $flusher->generateCacheKey(TestCacheFlusher::$categories[0], $editor->ID);
$this->assertNull($cache->get($editorKey));
}
}
2017-12-11 04:46:21 +01:00
}