2017-06-17 15:49:07 +03:00
|
|
|
<?php
|
2018-09-02 14:08:49 +03:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2017-06-17 15:49:07 +03:00
|
|
|
namespace GraphQL\Tests\Validator;
|
|
|
|
|
|
|
|
use GraphQL\Error\FormattedError;
|
|
|
|
use GraphQL\Language\SourceLocation;
|
|
|
|
use GraphQL\Validator\Rules\DisableIntrospection;
|
|
|
|
|
2018-07-29 18:43:10 +03:00
|
|
|
class DisableIntrospectionTest extends ValidatorTestCase
|
2017-06-17 15:49:07 +03:00
|
|
|
{
|
|
|
|
// Validate: Disable Introspection
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('fails if the query contains __schema')
|
2017-06-17 15:49:07 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testQueryContainsSchema() : void
|
2017-06-17 15:49:07 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectFailsRule(
|
|
|
|
new DisableIntrospection(DisableIntrospection::ENABLED),
|
|
|
|
'
|
2017-06-17 15:49:07 +03:00
|
|
|
query {
|
|
|
|
__schema {
|
|
|
|
queryType {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
2018-09-02 14:08:49 +03:00
|
|
|
[$this->error(3, 9)]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function error($line, $column)
|
|
|
|
{
|
|
|
|
return FormattedError::create(
|
|
|
|
DisableIntrospection::introspectionDisabledMessage(),
|
|
|
|
[new SourceLocation($line, $column)]
|
2017-06-17 15:49:07 +03:00
|
|
|
);
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
|
2017-06-17 15:49:07 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('fails if the query contains __type')
|
2017-06-17 15:49:07 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testQueryContainsType() : void
|
2017-06-17 15:49:07 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectFailsRule(
|
|
|
|
new DisableIntrospection(DisableIntrospection::ENABLED),
|
|
|
|
'
|
2017-06-17 15:49:07 +03:00
|
|
|
query {
|
|
|
|
__type(
|
|
|
|
name: "Query"
|
|
|
|
){
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
[$this->error(3, 9)]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('does not fail on a query that does not contain __type')
|
2017-06-17 15:49:07 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testValidQuery() : void
|
2017-06-17 15:49:07 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectPassesRule(
|
|
|
|
new DisableIntrospection(DisableIntrospection::ENABLED),
|
|
|
|
'
|
2017-06-17 15:49:07 +03:00
|
|
|
query {
|
|
|
|
user {
|
|
|
|
name
|
|
|
|
email
|
|
|
|
friends {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
'
|
|
|
|
);
|
2017-06-17 15:49:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('does not fail when not enabled')
|
2017-06-17 15:49:07 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testQueryWhenDisabled() : void
|
2017-06-17 15:49:07 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectPassesRule(
|
|
|
|
new DisableIntrospection(DisableIntrospection::DISABLED),
|
|
|
|
'
|
2017-06-17 15:49:07 +03:00
|
|
|
query {
|
|
|
|
__type(
|
|
|
|
name: "Query"
|
|
|
|
){
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
'
|
|
|
|
);
|
2017-06-17 15:49:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('has a public interface for enabeling the rule')
|
2017-06-17 15:49:07 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testPublicEnableInterface() : void
|
2017-06-17 15:49:07 +03:00
|
|
|
{
|
|
|
|
$disableIntrospection = new DisableIntrospection(DisableIntrospection::DISABLED);
|
|
|
|
$disableIntrospection->setEnabled(DisableIntrospection::ENABLED);
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectFailsRule(
|
|
|
|
$disableIntrospection,
|
|
|
|
'
|
2017-06-17 15:49:07 +03:00
|
|
|
query {
|
|
|
|
__type(
|
|
|
|
name: "Query"
|
|
|
|
){
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
[$this->error(3, 9)]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('has a public interface for disableing the rule')
|
2017-06-17 15:49:07 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testPublicDisableInterface() : void
|
2017-06-17 15:49:07 +03:00
|
|
|
{
|
|
|
|
$disableIntrospection = new DisableIntrospection(DisableIntrospection::ENABLED);
|
|
|
|
$disableIntrospection->setEnabled(DisableIntrospection::DISABLED);
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectPassesRule(
|
|
|
|
$disableIntrospection,
|
|
|
|
'
|
2017-06-17 15:49:07 +03:00
|
|
|
query {
|
|
|
|
__type(
|
|
|
|
name: "Query"
|
|
|
|
){
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
'
|
2017-06-17 15:49:07 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|