diff --git a/code/extensions/JSONTextExtension.php b/code/extensions/JSONTextExtension.php index 2452aba..fe078b4 100644 --- a/code/extensions/JSONTextExtension.php +++ b/code/extensions/JSONTextExtension.php @@ -47,6 +47,7 @@ use phptek\JSONText\Fields\JSONText; use phptek\JSONText\Exceptions\JSONTextException; use SilverStripe\ORM\DataExtension; +use SilverStripe\Control\Controller; class JSONTextExtension extends DataExtension { @@ -61,20 +62,22 @@ public function onBeforeWrite() parent::onBeforeWrite(); $owner = $this->getOwner(); - $controller = \Controller::curr(); + $controller = Controller::curr(); $postVars = $controller->getRequest()->postVars(); $fieldMap = $owner->config()->get('json_field_map'); $doUpdate = ( count($postVars) && - in_array(get_class($controller), ['CMSPageEditController', 'FakeController']) && - !empty($fieldMap) + in_array(get_class($controller), [ + 'SilverStripe\CMS\Controllers\CMSPageEditController', + 'SilverStripe\Control\Tests\FakeController'] + ) && !empty($fieldMap) ); if (!$doUpdate) { return null; } - foreach ($owner->db() as $field => $type) { + foreach ($owner->config()->get('db') as $field => $type) { if ($type === 'JSONText') { $this->updateJSON($postVars, $owner); } @@ -124,7 +127,7 @@ public function updateJSON(array $postVars, $owner) public function updateCMSFields(\SilverStripe\Forms\FieldList $fields) { $owner = $this->getOwner(); - $jsonFieldMap = $owner->config()->json_field_map; + $jsonFieldMap = $owner->config()->get('json_field_map'); foreach ($jsonFieldMap as $jsonField => $mappedFields) { if (!$owner->getField($jsonField)) { diff --git a/tests/JSONTextExtensionTest.php b/tests/JSONTextExtensionTest.php index 3e3dbc8..7481f7f 100644 --- a/tests/JSONTextExtensionTest.php +++ b/tests/JSONTextExtensionTest.php @@ -8,6 +8,7 @@ use SilverStripe\Dev\FunctionalTest; use SilverStripe\Dev\TestOnly; +use SilverStripe\Core\Config\Config; class JSONTextExtensionTest extends FunctionalTest { @@ -16,41 +17,31 @@ class JSONTextExtensionTest extends FunctionalTest */ protected static $fixture_file = 'jsontext/tests/fixtures/yml/JSONTextExtension.yml'; - /** - * Ensure our TestOnly DO's are usable as fixtures in the test DB - * - * @var array - */ - protected $extraDataObjects = [ - 'JSONTextTestPage', - ]; - /** * Is an exception thrown when no POSTed vars are available for * non DB-backed fields declared on a SiteTree class? */ public function testExceptionThrownOnBeforeWrite() { - $member = $this->objFromFixture('Member', 'admin'); - $fixture = $this->objFromFixture('JSONTextTestPage', 'jsontext-text'); - $this->session()->inst_set('loggedInAs', $member->ID); + $member = $this->objFromFixture('SilverStripe\\Security\\Member', 'admin'); - $this->setExpectedException('JSONTextException', "FooField doesn't exist in POST data."); - $this->post('admin/pages/edit/EditForm', [ - 'Title' => 'Dummy', - 'action_save' => 1, + $fixture = Page::create([ 'ID' => 44, + 'Title' => 'Dummy', + 'ParentID' => 0 + ]); + + $member->logIn(); + $fixture->config()->update('db', ['MyJSON' => 'JSONText']); + $fixture->config()->update('json_field_map', ['MyJSON' => ['FooField']]); + $fixture->write(); + + // Submit a CMS POST request _without_ JSON data + $this->setExpectedException('\phptek\JSONText\Exceptions\JSONTextException', "FooField doesn't exist in POST data."); + $this->post('admin/pages/edit/EditForm/44/', [ + 'ParentID' => '0', + 'action_save' => 'Saved', + 'ID' => '44', ]); } } - -class JSONTextTestPage extends Page implements TestOnly -{ - private static $db = [ - 'MyJSON' => '\phptek\JSONText\Fields\JSONText' - ]; - - private static $json_field_map = [ - 'MyJSON' => ['FooField'] - ]; -} diff --git a/tests/JSONTextIntegrationTest.php b/tests/JSONTextIntegrationTest.php index 9d4c0ee..259933f 100755 --- a/tests/JSONTextIntegrationTest.php +++ b/tests/JSONTextIntegrationTest.php @@ -24,6 +24,14 @@ public function __construct() { self::$fixture_file = MODULE_DIR . '/tests/fixtures/yml/MyAwesomeJSONModel.yml'; } + + /** + * Allows us the ability to scaffold DB records for {@link TestOnly} implementations. + * @var array + */ + protected static $extra_dataobjects = [ + 'MyAwesomeJSONModel' + ]; /** * Tests JSONText::setValue() by means of a simple JSONPath expression operating on a nested JSON array diff --git a/tests/fixtures/yml/JSONTextExtension.yml b/tests/fixtures/yml/JSONTextExtension.yml index c8abb3d..f416dd7 100644 --- a/tests/fixtures/yml/JSONTextExtension.yml +++ b/tests/fixtures/yml/JSONTextExtension.yml @@ -1,20 +1,14 @@ -Permission: - admin: - Code: ADMIN - -Group: +'SilverStripe\Security\Group': admins: Title: Administrators - Permissions: =>Permission.admin -Member: +'SilverStripe\Security\Permission': + admin: + Code: ADMIN + GroupID: '=>SilverStripe\Security\Group.admins' + +'SilverStripe\Security\Member': admin: FirstName: Admin Email: admin@user.com - Groups: =>Group.admins - -JSONTextTestPage: - jsontext-text: - Title: Dummy - ParentID: 0 - ID: 44 \ No newline at end of file + Groups: '=>SilverStripe\Security\Group.admins' \ No newline at end of file