Skip to content

Commit

Permalink
Merge branch '3.6' into 3.7
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Nov 18, 2021
2 parents 930fa51 + d391399 commit b9589ec
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Permission/PermissionCheckerAware.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace SilverStripe\GraphQL\Permission;

use SilverStripe\Core\Injector\Injector;

trait PermissionCheckerAware
{
/**
Expand All @@ -26,6 +28,10 @@ public function setPermissionChecker(QueryPermissionChecker $checker)
*/
public function getPermissionChecker()
{
return $this->permissionChecker;
$checker = $this->permissionChecker;
if (is_null($checker)) {
return Injector::inst()->get(QueryPermissionChecker::class . '.default');
}
return $checker;
}
}
51 changes: 51 additions & 0 deletions tests/Permission/PermissionCheckerAwareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace SilverStripe\GraphQL\Tests\Permission;

use SilverStripe\Dev\SapphireTest;
use SilverStripe\GraphQL\Permission\PermissionCheckerAware;
use SilverStripe\GraphQL\Permission\QueryPermissionChecker;
use SilverStripe\Security\Member;
use SilverStripe\ORM\Filterable;
use SilverStripe\Core\Injector\Injector;

class PermissionCheckerAwareTest extends SapphireTest
{
public function testCanViewPermissionCheckerAddedIfNull()
{
$scaffolder = new class() {
use PermissionCheckerAware;
};
$checker = $scaffolder->getPermissionChecker();
$defaultChecker = Injector::inst()->get(QueryPermissionChecker::class . '.default');
$this->assertNotNull($checker);
$this->assertSame(get_class($defaultChecker), get_class($checker));
}

public function testDefaultCanViewPermissionCheckerNotAddedIfSet()
{
$scaffolder = new class() {
use PermissionCheckerAware;
};
$nonChecker = new class () implements QueryPermissionChecker {
public function applyToList(Filterable $list, Member $member = null)
{
return $list;
}

public function checkItem($item, Member $member = null)
{
return true;
}

public function testMe()
{
return true;
}
};
$scaffolder->setPermissionChecker($nonChecker);
$checker = $scaffolder->getPermissionChecker();
$this->assertNotNull($checker);
$this->assertTrue(method_exists($checker, 'testMe'));
}
}

0 comments on commit b9589ec

Please sign in to comment.