diff --git a/_config/config.yml b/_config/config.yml index c982b98ec..64296812d 100644 --- a/_config/config.yml +++ b/_config/config.yml @@ -4,7 +4,7 @@ Name: graphqlconfig SilverStripe\GraphQL\Scaffolding\Scaffolders\DataObjectScaffolder: default_fields: ID: ID - + ## Map DB fields to GraphQL types SilverStripe\ORM\FieldType\DBField: # fallback @@ -26,7 +26,7 @@ SilverStripe\ORM\FieldType\DBForeignKey: graphql_type: 'ID' ## CORS default config -SilverStripe\GraphQL: +SilverStripe\GraphQL\Controller: cors: Enabled: false # Off by default Allow-Origin: # Deny all by default diff --git a/src/Controller.php b/src/Controller.php index 66ed9aabf..d1245fcd8 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -11,7 +11,6 @@ use SilverStripe\ORM\Versioning\Versioned; use Exception; use SilverStripe\Security\Permission; -use SilverStripe\Security\Security; /** * Top level controller for handling graphql requests. @@ -39,7 +38,7 @@ public function index(HTTPRequest $request) // Check for a possible CORS preflight request and handle if necessary // Refer issue 66: https://github.com/silverstripe/silverstripe-graphql/issues/66 - $corsConfig = Config::inst()->get('SilverStripe\GraphQL', 'cors'); + $corsConfig = Config::inst()->get(self::class, 'cors'); $corsEnabled = true; // Default to have CORS turned on. if ($corsConfig && isset($corsConfig['Enabled']) && !$corsConfig['Enabled']) { @@ -121,7 +120,7 @@ public function getManager() } // Get a service rather than an instance (to allow procedural configuration) - $config = Config::inst()->get('SilverStripe\GraphQL', 'schema'); + $config = Config::inst()->get(static::class, 'schema'); $manager = Manager::createFromConfig($config); return $manager; @@ -154,7 +153,7 @@ public function getAuthHandler() */ public function addCorsHeaders(HTTPRequest $request, HTTPResponse $response) { - $corsConfig = Config::inst()->get('SilverStripe\GraphQL', 'cors'); + $corsConfig = Config::inst()->get(static::class, 'cors'); if (empty($corsConfig['Enabled'])) { // If CORS is disabled don't add the extra headers. Simply return the response untouched. return $response; diff --git a/src/Scaffolding/Scaffolders/DataObjectScaffolder.php b/src/Scaffolding/Scaffolders/DataObjectScaffolder.php index d01d9126c..642a89f51 100644 --- a/src/Scaffolding/Scaffolders/DataObjectScaffolder.php +++ b/src/Scaffolding/Scaffolders/DataObjectScaffolder.php @@ -207,7 +207,7 @@ public function setFieldDescription($field, $description) $this->dataObjectClass )); } - + $this->fields->replace($existing, ArrayData::create([ 'Name' => $field, 'Description' => $description @@ -495,7 +495,10 @@ protected function allFieldsFromDataObject($includeHasOne = false) { $fields = []; $db = DataObject::config()->fixed_fields; - $db = array_merge($db, Config::inst()->get($this->dataObjectClass, 'db', Config::INHERITED)); + $extra = Config::inst()->get($this->dataObjectClass, 'db'); + if ($extra) { + $db = array_merge($db, $extra); + } foreach ($db as $fieldName => $type) { $fields[] = $fieldName; diff --git a/tests/ControllerTest.php b/tests/ControllerTest.php index c4ebb4835..d448b6ccb 100644 --- a/tests/ControllerTest.php +++ b/tests/ControllerTest.php @@ -2,6 +2,8 @@ namespace SilverStripe\GraphQL\Tests; +use PHPUnit_Framework_MockObject_MockBuilder; +use SilverStripe\Control\Director; use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPResponse; use SilverStripe\Dev\SapphireTest; @@ -11,23 +13,26 @@ use SilverStripe\GraphQL\Tests\Fake\TypeCreatorFake; use SilverStripe\GraphQL\Tests\Fake\QueryCreatorFake; use SilverStripe\Core\Config\Config; -use SilverStripe\Security\Member; -use GraphQL\Schema; -use GraphQL\Type\Definition\ObjectType; use ReflectionClass; use Exception; -use SilverStripe\Control\HTTPResponse_Exception; class ControllerTest extends SapphireTest { public function setUp() { + Director::set_environment_type('dev'); parent::setUp(); Handler::config()->remove('authenticators'); $this->logInWithPermission('CMS_ACCESS_CMSMain'); } + public function tearDown() + { + Director::set_environment_type('dev'); + parent::tearDown(); + } + public function testIndex() { $controller = new Controller(); @@ -41,8 +46,7 @@ public function testIndex() public function testGetGetManagerPopulatesFromConfig() { - Config::inst()->remove('SilverStripe\GraphQL', 'schema'); - Config::inst()->update('SilverStripe\GraphQL', 'schema', [ + Config::modify()->set(Controller::class, 'schema', [ 'types' => [ 'mytype' => TypeCreatorFake::class, ], @@ -60,17 +64,12 @@ public function testGetGetManagerPopulatesFromConfig() public function testIndexWithException() { - Config::inst()->update('SilverStripe\\Control\\Director', 'environment_type', 'live'); + Director::set_environment_type('live'); $controller = new Controller(); - $managerMock = $this->getMockBuilder(Schema::class) + /** @var Manager|PHPUnit_Framework_MockObject_MockBuilder $managerMock */ + $managerMock = $this->getMockBuilder(Manager::class) ->setMethods(['query']) - ->setConstructorArgs([ - ['query' => new ObjectType([ - 'name' => 'Query', - 'fields' => [] - ])] - ]) ->getMock(); $managerMock->method('query') @@ -88,17 +87,12 @@ public function testIndexWithException() public function testIndexWithExceptionIncludesTraceInDevMode() { - Config::inst()->update('SilverStripe\\Control\\Director', 'environment_type', 'dev'); + Director::set_environment_type('dev'); $controller = new Controller(); - $managerMock = $this->getMockBuilder(Schema::class) + /** @var Manager|PHPUnit_Framework_MockObject_MockBuilder $managerMock */ + $managerMock = $this->getMockBuilder(Manager::class) ->setMethods(['query']) - ->setConstructorArgs([ - ['query' => new ObjectType([ - 'name' => 'Query', - 'fields' => [] - ])] - ]) ->getMock(); $managerMock->method('query') @@ -150,12 +144,11 @@ public function testAuthenticationProtectionOnQueries($authenticator, $shouldFai } /** - * @expectedException SilverStripe\Control\HTTPResponse_Exception + * @expectedException \SilverStripe\Control\HTTPResponse_Exception */ public function testAddCorsHeadersOriginDisallowed() { - Config::inst()->remove('SilverStripe\GraphQL', 'cors'); - Config::inst()->update('SilverStripe\GraphQL', 'cors', [ + Config::modify()->set(Controller::class, 'cors', [ 'Enabled' => true, 'Allow-Origin' => null, 'Allow-Headers' => 'Authorization, Content-Type', @@ -175,8 +168,7 @@ public function testAddCorsHeadersOriginDisallowed() public function testAddCorsHeadersOriginAllowed() { - Config::inst()->remove('SilverStripe\GraphQL', 'cors'); - Config::inst()->update('SilverStripe\GraphQL', 'cors', [ + Config::modify()->set(Controller::class, 'cors', [ 'Enabled' => true, 'Allow-Origin' => 'localhost', 'Allow-Headers' => 'Authorization, Content-Type', @@ -201,12 +193,11 @@ public function testAddCorsHeadersOriginAllowed() } /** - * @expectedException SilverStripe\Control\HTTPResponse_Exception + * @expectedException \SilverStripe\Control\HTTPResponse_Exception */ public function testAddCorsHeadersResponseCORSDisabled() { - Config::inst()->remove('SilverStripe\GraphQL', 'cors'); - Config::inst()->update('SilverStripe\GraphQL', 'cors', [ + Config::modify()->set(Controller::class, 'cors', [ 'Enabled' => false ]);