Skip to content

Commit

Permalink
FIX: Adapted existing tests to SS4 and all is green.
Browse files Browse the repository at this point in the history
  • Loading branch information
phptek committed May 3, 2017
1 parent 764d9c9 commit 4d7a5d2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 46 deletions.
13 changes: 8 additions & 5 deletions code/extensions/JSONTextExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}
Expand Down Expand Up @@ -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)) {
Expand Down
45 changes: 18 additions & 27 deletions tests/JSONTextExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use SilverStripe\Dev\FunctionalTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Core\Config\Config;

class JSONTextExtensionTest extends FunctionalTest
{
Expand All @@ -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']
];
}
8 changes: 8 additions & 0 deletions tests/JSONTextIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 8 additions & 14 deletions tests/fixtures/yml/JSONTextExtension.yml
Original file line number Diff line number Diff line change
@@ -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: [email protected]
Groups: =>Group.admins

JSONTextTestPage:
jsontext-text:
Title: Dummy
ParentID: 0
ID: 44
Groups: '=>SilverStripe\Security\Group.admins'

0 comments on commit 4d7a5d2

Please sign in to comment.