Skip to content

Commit

Permalink
Fix forms namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian Mooyman committed Jun 20, 2017
1 parent b65c212 commit 2a278e2
Show file tree
Hide file tree
Showing 41 changed files with 283 additions and 201 deletions.
37 changes: 21 additions & 16 deletions src/Control/Director.php
Expand Up @@ -131,18 +131,9 @@ public static function direct(HTTPRequest $request)
// Generate output
$result = static::handleRequest($request);

// Save session data. Note that inst_save() will start/resume the session if required.
// Save session data. Note that save() will start/resume the session if required.
$request->getSession()->save();

// Return code for a redirection request
// @todo: Refactor into CLIApplication
if ($result->isRedirect() && static::is_cli()) {
$url = Director::makeRelative($result->getHeader('Location'));
$request = clone $request;
$request->setUrl($url);
return static::direct($request);
}

// Post-request handling
$postRequest = RequestProcessor::singleton()->postRequest($request, $result);
if ($postRequest === false) {
Expand Down Expand Up @@ -178,7 +169,7 @@ public static function direct(HTTPRequest $request)
*/
public static function test(
$url,
$postVars = null,
$postVars = [],
$session = array(),
$httpMethod = null,
$body = null,
Expand Down Expand Up @@ -213,13 +204,24 @@ public static function test(
}

// Default httpMethod
$newVars['_SERVER']['REQUEST_METHOD'] = $httpMethod
?: (($postVars || is_array($postVars)) ? "POST" : "GET");
$newVars['_SERVER']['REQUEST_METHOD'] = $httpMethod ?: ($postVars ? "POST" : "GET");
$newVars['_POST'] = (array)$postVars;

// Setup session
$newVars['_SESSION'] = $session instanceof Session
? $session->getAll()
: ($session ?: []);
if ($session instanceof Session) {
// Note: If passing $session as object, ensure that changes are written back
// This is important for classes such as FunctionalTest which emulate cross-request persistence
$newVars['_SESSION'] = $session->getAll();
$finally[] = function () use ($session) {
if (isset($_SESSION)) {
foreach ($_SESSION as $key => $value) {
$session->set($key, $value);
}
}
};
} else {
$newVars['_SESSION'] = $session ?: [];
}

// Setup cookies
$cookieJar = $cookies instanceof Cookie_Backend
Expand Down Expand Up @@ -268,6 +270,9 @@ public static function test(
}
}

// Apply new vars to environment
static::varsToEnv($newVars);

try {
// Normal request handling
return static::direct($request);
Expand Down
11 changes: 5 additions & 6 deletions src/Dev/ExtensionTestState.php
Expand Up @@ -55,7 +55,7 @@ public function setUpOnce($class)
continue;
}
if (!isset($this->extensionsToReapply[$dataClass])) {
$this->extensionsToReapply[$dataClass] = array();
$this->extensionsToReapply[$dataClass] = [];
}
$this->extensionsToReapply[$dataClass][] = $extension;
$dataClass::remove_extension($extension);
Expand All @@ -70,14 +70,13 @@ public function setUpOnce($class)
}
$this->extensionsToRemove[$dataClass] = array();
foreach ($extensions as $extension) {
$dataClass = Extension::get_classname_without_arguments($extension);
if (!class_exists($dataClass)) {
$self = static::class;
throw new LogicException("Test {$self} requires extension {$extension} which doesn't exist");
$extension = Extension::get_classname_without_arguments($extension);
if (!class_exists($extension)) {
throw new LogicException("Test {$class} requires extension {$extension} which doesn't exist");
}
if (!$dataClass::has_extension($extension)) {
if (!isset($this->extensionsToRemove[$dataClass])) {
$this->extensionsToReapply[$dataClass] = array();
$this->extensionsToReapply[$dataClass] = [];
}
$this->extensionsToRemove[$dataClass][] = $extension;
$dataClass::add_extension($extension);
Expand Down
9 changes: 5 additions & 4 deletions src/Dev/FunctionalTest.php
Expand Up @@ -2,6 +2,7 @@

namespace SilverStripe\Dev;

use SilverStripe\Control\Controller;
use SilverStripe\Control\Session;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Core\Config\Config;
Expand Down Expand Up @@ -408,11 +409,11 @@ public function assertExactHTMLMatchBySelector($selector, $expectedMatches, $mes
public function useDraftSite($enabled = true)
{
if ($enabled) {
$this->session()->inst_set('readingMode', 'Stage.Stage');
$this->session()->inst_set('unsecuredDraftSite', true);
$this->session()->set('readingMode', 'Stage.Stage');
$this->session()->set('unsecuredDraftSite', true);
} else {
$this->session()->inst_set('readingMode', 'Stage.Live');
$this->session()->inst_set('unsecuredDraftSite', false);
$this->session()->set('readingMode', 'Stage.Live');
$this->session()->set('unsecuredDraftSite', false);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Dev/SapphireTest.php
Expand Up @@ -904,7 +904,7 @@ public static function start()
// (e.g. Member will now have various subclasses of DataObjects that implement TestOnly)
DataObject::reset();

// Set dummy controller
// Set dummy controller;
$controller = Controller::create();
$controller->setRequest($request);
$controller->pushCurrent();
Expand Down
1 change: 1 addition & 0 deletions src/Dev/TestSession.php
Expand Up @@ -61,6 +61,7 @@ public function __construct()
$this->controller = new Controller();
$this->controller->setRequest($request);
$this->controller->pushCurrent();
$this->controller->doInit();
}

public function __destruct()
Expand Down
6 changes: 6 additions & 0 deletions src/Forms/DefaultFormFactory.php
Expand Up @@ -26,6 +26,12 @@ public function __construct()
$this->constructExtensions();
}

/**
* @param RequestHandler $controller
* @param string $name
* @param array $context
* @return Form
*/
public function getForm(RequestHandler $controller = null, $name = FormFactory::DEFAULT_NAME, $context = [])
{
// Validate context
Expand Down
5 changes: 4 additions & 1 deletion src/Forms/Form.php
Expand Up @@ -2,6 +2,7 @@

namespace SilverStripe\Forms;

use SilverStripe\Control\Controller;
use SilverStripe\Control\HasRequestHandler;
use SilverStripe\Control\HTTP;
use SilverStripe\Control\RequestHandler;
Expand Down Expand Up @@ -347,7 +348,9 @@ public function clearFormState()
*/
protected function getSession()
{
return $this->getRequestHandler()->getRequest()->getSession();
// Note: Session may not be available if this form doesn't have a request handler
$controller = $this->getController() ?: Controller::curr();
return $controller->getRequest()->getSession();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/GridField/GridFieldDetailForm_ItemRequest.php
Expand Up @@ -4,9 +4,9 @@

use SilverStripe\Admin\LeftAndMain;
use SilverStripe\Control\Controller;
use SilverStripe\Control\RequestHandler;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\RequestHandler;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
Expand Down
4 changes: 1 addition & 3 deletions src/Forms/GridField/GridFieldExportButton.php
Expand Up @@ -134,9 +134,7 @@ protected function getExportColumnsForGridField(GridField $gridField)
}

/** @var GridFieldDataColumns $dataCols */
$dataCols = $gridField->getConfig()->getComponentByType(
'SilverStripe\\Forms\\GridField\\GridFieldDataColumns'
);
$dataCols = $gridField->getConfig()->getComponentByType(GridFieldDataColumns::class);
if ($dataCols) {
return $dataCols->getDisplayFields($gridField);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/HTMLEditor/TinyMCEConfig.php
Expand Up @@ -365,7 +365,7 @@ public function enablePlugins($plugin)

/**
* Enable one or several plugins. Will properly handle being passed a plugin that is already disabled
* @param string $plugin,... a string, or several strings, or a single array of strings - The plugins to enable
* @param string|array $plugin,... a string, or several strings, or a single array of strings - The plugins to enable
* @return $this
*/
public function disablePlugins($plugin)
Expand Down
8 changes: 8 additions & 0 deletions tests/php/Control/ControllerTest/TestController.php
Expand Up @@ -10,6 +10,14 @@
*/
class TestController extends Controller implements TestOnly
{
public function __construct()
{
parent::__construct();
if (Controller::has_curr()) {
$this->setRequest(Controller::curr()->getRequest());
}
}

private static $url_segment = 'TestController';

public $Content = "default content";
Expand Down
8 changes: 8 additions & 0 deletions tests/php/Control/DirectorTest/TestController.php
Expand Up @@ -7,6 +7,14 @@

class TestController extends Controller implements TestOnly
{
public function __construct()
{
parent::__construct();
if (Controller::has_curr()) {
$this->setRequest(Controller::curr()->getRequest());
}
}

private static $url_segment = 'TestController';

private static $allowed_actions = array(
Expand Down
12 changes: 8 additions & 4 deletions tests/php/Control/RequestHandlingTest/TestController.php
Expand Up @@ -3,6 +3,7 @@
namespace SilverStripe\Control\Tests\RequestHandlingTest;

use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\HTTPResponse_Exception;
use SilverStripe\Dev\TestOnly;
Expand Down Expand Up @@ -41,25 +42,28 @@ public function __construct()
{
$this->failover = new ControllerFailover();
parent::__construct();
if (Controller::has_curr()) {
$this->setRequest(Controller::curr()->getRequest());
}
}

public function index($request)
public function index(HTTPRequest $request)
{
return "This is the controller";
}

public function method($request)
public function method(HTTPRequest $request)
{
return "This is a method on the controller: " . $request->param('ID') . ', ' . $request->param('OtherID');
}

public function legacymethod($request)
public function legacymethod(HTTPRequest $request)
{
return "\$this->urlParams can be used, for backward compatibility: " . $this->urlParams['ID'] . ', '
. $this->urlParams['OtherID'];
}

public function virtualfile($request)
public function virtualfile(HTTPRequest $request)
{
return "This is the virtualfile method";
}
Expand Down
7 changes: 2 additions & 5 deletions tests/php/Forms/CheckboxSetFieldTest.php
Expand Up @@ -169,17 +169,14 @@ public function testSaveWithArrayValueSet()

public function testLoadDataFromObject()
{
$article = $this->objFromFixture(Article::class, 'articlewithouttags');
$articleWithTags = $this->objFromFixture(Article::class, 'articlewithtags');
$tag1 = $this->objFromFixture(Tag::class, 'tag1');
$tag2 = $this->objFromFixture(Tag::class, 'tag2');

$field = new CheckboxSetField("Tags", "Test field", DataObject::get(Tag::class)->map());
/**
* @skipUpgrade
*/
/** @skipUpgrade */
$form = new Form(
new Controller(),
Controller::curr(),
'Form',
new FieldList($field),
new FieldList()
Expand Down
23 changes: 8 additions & 15 deletions tests/php/Forms/ConfirmedPasswordFieldTest.php
Expand Up @@ -2,14 +2,13 @@

namespace SilverStripe\Forms\Tests;

use SilverStripe\Control\Tests\ControllerTest\TestController;
use SilverStripe\Security\Member;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Control\Controller;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\ConfirmedPasswordField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Security\Member;

class ConfirmedPasswordFieldTest extends SapphireTest
{
Expand Down Expand Up @@ -39,10 +38,8 @@ public function testHashHidden()
$member->Password = "valueB";
$member->write();

/**
* @skipUpgrade
*/
$form = new Form(new TestController(), 'Form', new FieldList($field), new FieldList());
/** @skipUpgrade */
$form = new Form(Controller::curr(), 'Form', new FieldList($field), new FieldList());
$form->loadDataFrom($member);

$this->assertEquals('', $field->Value());
Expand Down Expand Up @@ -92,10 +89,8 @@ public function testValidation()
)
);
$validator = new RequiredFields();
/**
* @skipUpgrade
*/
$form = new Form(new TestController(), 'Form', new FieldList($field), new FieldList(), $validator);
/** @skipUpgrade */
new Form(Controller::curr(), 'Form', new FieldList($field), new FieldList(), $validator);
$this->assertTrue(
$field->validate($validator),
"Validates when both passwords are the same"
Expand All @@ -120,11 +115,9 @@ public function testValidation()

public function testFormValidation()
{
/**
* @skipUpgrade
*/
/** @skipUpgrade */
$form = new Form(
new Controller(),
Controller::curr(),
'Form',
new FieldList($field = new ConfirmedPasswordField('Password')),
new FieldList()
Expand Down
12 changes: 5 additions & 7 deletions tests/php/Forms/DatetimeFieldTest.php
Expand Up @@ -2,16 +2,14 @@

namespace SilverStripe\Forms\Tests;

use SilverStripe\Dev\SapphireTest;
use SilverStripe\Control\Controller;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\DatetimeField;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\DateField;
use SilverStripe\Forms\Tests\DatetimeFieldTest\Model;
use SilverStripe\Forms\TimeField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\Tests\DatetimeFieldTest\Model;
use SilverStripe\i18n\i18n;
use SilverStripe\ORM\FieldType\DBDatetime;

Expand Down Expand Up @@ -452,7 +450,7 @@ protected function getMockForm()
{
/** @skipUpgrade */
return new Form(
new Controller(),
Controller::curr(),
'Form',
new FieldList(),
new FieldList(
Expand Down
2 changes: 1 addition & 1 deletion tests/php/Forms/DropdownFieldTest.php
Expand Up @@ -479,7 +479,7 @@ public function testValidation()
)
);
$validator = new RequiredFields();
$form = new Form(null, 'Form', new FieldList($field), new FieldList(), $validator);
new Form(null, 'Form', new FieldList($field), new FieldList(), $validator);
$field->setValue("One");
$this->assertTrue($field->validate($validator));
$field->setName("TestNew"); //try changing name of field
Expand Down

0 comments on commit 2a278e2

Please sign in to comment.