Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Update graphql to use new config #81

Merged
merged 1 commit into from
Feb 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 3 additions & 4 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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']) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions src/Scaffolding/Scaffolders/DataObjectScaffolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public function setFieldDescription($field, $description)
$this->dataObjectClass
));
}

$this->fields->replace($existing, ArrayData::create([
'Name' => $field,
'Description' => $description
Expand Down Expand Up @@ -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;
Expand Down
51 changes: 21 additions & 30 deletions tests/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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,
],
Expand All @@ -60,17 +64,12 @@ public function testGetGetManagerPopulatesFromConfig()

public function testIndexWithException()
{
Config::inst()->update('SilverStripe\\Control\\Director', 'environment_type', 'live');
Director::set_environment_type('live');

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did this need to be removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mock was incorrectly using a Schema in place of a Manager class.

$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')
Expand All @@ -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')
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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
]);

Expand Down