View source
<?php
namespace Drupal\Tests\graphql\Kernel\Framework;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\graphql\GraphQL\Cache\CacheableValue;
use Drupal\Tests\graphql\Kernel\GraphQLTestBase;
class SecureFieldTest extends GraphQLTestBase {
protected function userPermissions() {
return [
'execute graphql requests',
];
}
protected function setUp() {
parent::setUp();
$this
->mockField('secure', [
'name' => 'secure',
'type' => 'Boolean',
'secure' => TRUE,
], new CacheableValue(TRUE, [
(new CacheableMetadata())
->addCacheContexts([
'user.permissions',
]),
]));
$this
->mockField('insecure', [
'name' => 'insecure',
'type' => 'Boolean',
'secure' => FALSE,
], new CacheableValue(TRUE, [
(new CacheableMetadata())
->addCacheContexts([
'user.permissions',
]),
]));
}
public function testSecureField() {
$this
->assertResults('{ secure }', [], [
'secure' => TRUE,
], $this
->defaultCacheMetaData());
}
public function testInsecureField() {
$metadata = $this
->defaultCacheMetaData();
$metadata
->setCacheMaxAge(0);
$this
->assertErrors('{ insecure }', [], [
'Unable to resolve insecure field \'insecure\'.',
], $metadata);
}
public function testByPassFieldSecurity() {
$this->accountProphecy
->hasPermission('bypass graphql field security')
->willReturn(TRUE);
$this
->assertResults('{ insecure }', [], [
'insecure' => TRUE,
], $this
->defaultCacheMetaData());
}
}