From b739e6cc5f263edfb2f1234b5bb4dc041eb36adf Mon Sep 17 00:00:00 2001 From: Franco Springveldt Date: Thu, 7 Sep 2017 11:32:55 +1200 Subject: [PATCH] Namespace changes and some more linting fixes --- .upgrade.yml | 14 ++ _config/config.yml | 3 + _config/legacy.yml | 6 + codecov.yml | 1 + license.md | 2 +- src/{model => Models}/MultiForm.php | 94 ++++++----- src/{model => Models}/MultiFormSession.php | 24 ++- src/{model => Models}/MultiFormStep.php | 66 +++++--- src/extensions/MultiFormObjectDecorator.php | 20 ++- src/tasks/MultiFormPurgeTask.php | 11 +- templates/Includes/MultiFormProgressList.ss | 24 ++- .../Includes/MultiFormProgressPercent.ss | 2 +- tests/MultiFormObjectDecoratorTest.php | 32 ++-- tests/MultiFormObjectDecoratorTest.yml | 2 +- tests/MultiFormTest.php | 154 +++++------------- tests/MultiFormTest.yml | 2 +- .../MultiFormObjectDecoratorDataObject.php | 13 ++ .../MultiFormObjectDecorator_DataObject.php | 8 - tests/Stubs/MultiFormStepOne.php | 23 ++- tests/Stubs/MultiFormTestController.php | 25 +++ tests/Stubs/MultiFormTestForm.php | 16 +- tests/Stubs/MultiFormTestStepThree.php | 20 ++- tests/Stubs/MultiFormTestStepTwo.php | 23 +++ tests/Stubs/MultiFormTest_Controller.php | 8 - 24 files changed, 352 insertions(+), 241 deletions(-) create mode 100644 .upgrade.yml create mode 100644 _config/config.yml create mode 100644 _config/legacy.yml create mode 100644 codecov.yml rename src/{model => Models}/MultiForm.php (89%) rename src/{model => Models}/MultiFormSession.php (81%) rename src/{model => Models}/MultiFormStep.php (90%) create mode 100644 tests/Stubs/MultiFormObjectDecoratorDataObject.php delete mode 100644 tests/Stubs/MultiFormObjectDecorator_DataObject.php create mode 100644 tests/Stubs/MultiFormTestController.php delete mode 100644 tests/Stubs/MultiFormTest_Controller.php diff --git a/.upgrade.yml b/.upgrade.yml new file mode 100644 index 0000000..8104798 --- /dev/null +++ b/.upgrade.yml @@ -0,0 +1,14 @@ +mappings: + MultiFormObjectDecorator: SilverStripe\MultiForm\MultiFormObjectDecorator + MultiForm: SilverStripe\MultiForm\MultiForm + MultiFormSession: SilverStripe\MultiForm\MultiFormSession + MultiFormStep: SilverStripe\MultiForm\MultiFormStep + MultiFormPurgeTask: SilverStripe\MultiForm\MultiFormPurgeTask + MultiFormObjectDecoratorTest: SilverStripe\MultiForm\MultiFormObjectDecoratorTest + MultiFormObjectDecorator_DataObject: SilverStripe\MultiForm\MultiFormObjectDecorator_DataObject + MultiFormTest: SilverStripe\MultiForm\MultiFormTest + MultiFormTest_Controller: SilverStripe\MultiForm\MultiFormTest_Controller + MultiFormTest_Form: SilverStripe\MultiForm\MultiFormTest_Form + MultiFormTest_StepOne: SilverStripe\MultiForm\MultiFormTest_StepOne + MultiFormTest_StepTwo: SilverStripe\MultiForm\MultiFormTest_StepTwo + MultiFormTest_StepThree: SilverStripe\MultiForm\MultiFormTest_StepThree diff --git a/_config/config.yml b/_config/config.yml new file mode 100644 index 0000000..775b48a --- /dev/null +++ b/_config/config.yml @@ -0,0 +1,3 @@ +--- +Name: multisiteconfig +--- diff --git a/_config/legacy.yml b/_config/legacy.yml new file mode 100644 index 0000000..2eaf6f9 --- /dev/null +++ b/_config/legacy.yml @@ -0,0 +1,6 @@ +--- +Name: multisitelegacy +--- +SilverStripe\ORM\DatabaseAdmin: + classname_value_remapping: + MultiFormSession: SilverStripe\MultiForm\Model\MultiFormSession diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 0000000..69cb760 --- /dev/null +++ b/codecov.yml @@ -0,0 +1 @@ +comment: false diff --git a/license.md b/license.md index 9445c8e..8794670 100644 --- a/license.md +++ b/license.md @@ -1,4 +1,4 @@ -Copyright (c) 2016, SilverStripe Limited +Copyright (c) 2017, SilverStripe Limited All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/src/model/MultiForm.php b/src/Models/MultiForm.php similarity index 89% rename from src/model/MultiForm.php rename to src/Models/MultiForm.php index 547fdd7..1f42c90 100644 --- a/src/model/MultiForm.php +++ b/src/Models/MultiForm.php @@ -1,5 +1,21 @@ 'Int', 'TotalStepCount' => 'Int', 'CompletedPercent' => 'Float' - ); + ]; /** * @var string @@ -63,11 +78,11 @@ abstract class MultiForm extends Form * * @var array */ - public static $ignored_fields = array( + public static $ignored_fields = [ 'url', 'executeForm', 'SecurityID' - ); + ]; /** * Any of the actions defined in this variable are exempt from @@ -79,9 +94,9 @@ abstract class MultiForm extends Form * * @var array */ - public static $actions_exempt_from_validation = array( + public static $actions_exempt_from_validation = [ 'action_prev' - ); + ]; /** * @var string @@ -98,7 +113,7 @@ abstract class MultiForm extends Form /** * Start the MultiForm instance. * - * @param Controller instance $controller Controller this form is created on + * @param Controller $controller Controller instance this form is created on * @param string $name The form name, typically the same as the method name */ public function __construct($controller, $name) @@ -155,7 +170,7 @@ abstract class MultiForm extends Form $getVar = $this->config()->get_var; // Set a hidden field in our form with an encrypted hash to identify this session. - $this->fields->push(new HiddenField($getVar, false, $this->session->Hash)); + $this->fields->push(HiddenField::create($getVar, false, $this->session->Hash)); // If there is saved data for the current step, we load it into the form it here //(CAUTION: loadData() MUST unserialize first!) @@ -213,14 +228,14 @@ abstract class MultiForm extends Form // Determine whether we use the current step, or create one if it doesn't exist $currentStep = null; - $StepID = $this->controller->request->getVar('StepID'); + $StepID = $this->controller->getRequest()->getVar('StepID'); if (isset($StepID)) { $currentStep = DataObject::get_one( 'MultiFormStep', - array( + [ 'SessionID' => $this->session->ID, 'ID' => $StepID - ) + ] ); } elseif ($this->session->CurrentStepID) { $currentStep = $this->session->CurrentStep(); @@ -228,7 +243,7 @@ abstract class MultiForm extends Form // Always fall back to creating a new step (in case the session or request data is invalid) if (!$currentStep || !$currentStep->ID) { - $currentStep = Object::create($startStepClass); + $currentStep = Injector::inst()->create($startStepClass); $currentStep->SessionID = $this->session->ID; $currentStep->write(); $this->session->CurrentStepID = $currentStep->ID; @@ -314,17 +329,17 @@ abstract class MultiForm extends Form public function getCurrentSession() { if (!$this->currentSessionHash) { - $this->currentSessionHash = $this->controller->request->getVar($this->config()->get_var); + $this->currentSessionHash = $this->controller->getRequest()->getVar($this->config()->get_var); if (!$this->currentSessionHash) { return false; } } - $this->session = MultiFormSession::get()->filter(array( + $this->session = MultiFormSession::get()->filter([ "Hash" => $this->currentSessionHash, "IsComplete" => 0 - ))->first(); + ])->first(); return $this->session; } @@ -336,7 +351,7 @@ abstract class MultiForm extends Form * to the database, use {@link getAllStepsLinear()}. * * @param string $filter SQL WHERE statement - * @return DataObjectSet|boolean A set of MultiFormStep subclasses + * @return DataList|boolean A set of MultiFormStep subclasses */ public function getSavedSteps($filter = null) { @@ -351,7 +366,7 @@ abstract class MultiForm extends Form * in your chain with the same classname. * * @param string $className Classname of a {@link MultiFormStep} subclass - * @return MultiFormStep + * @return DataObject */ public function getSavedStepByClass($className) { @@ -381,30 +396,30 @@ abstract class MultiForm extends Form * then that set of actions is appended to the end of the actions FieldSet we * have created in this method. * - * @param $currentStep Subclass of MultiFormStep + * @param MultiFormStep $step Subclass of MultiFormStep * @return FieldList of FormAction objects */ public function actionsFor($step) { // Create default multi step actions (next, prev), and merge with extra actions, if any - $actions = (class_exists('FieldList')) ? new FieldList() : new FieldSet(); + $actions = FieldList::create(); // If the form is at final step, create a submit button to perform final actions // The last step doesn't have a next button, so add that action to any step that isn't the final one if ($step->isFinalStep()) { - $actions->push(new FormAction('finish', $step->getSubmitText())); + $actions->push(FormAction::create('finish', $step->getSubmitText())); } else { - $actions->push(new FormAction('next', $step->getNextText())); + $actions->push(FormAction::create('next', $step->getNextText())); } // If there is a previous step defined, add the back button if ($step->getPreviousStep() && $step->canGoBack()) { // If there is a next step, insert the action before the next action if ($step->getNextStep()) { - $actions->insertBefore($prev = new FormAction('prev', $step->getPrevText()), 'action_next'); + $actions->insertBefore($prev = FormAction::create('prev', $step->getPrevText()), 'action_next'); // Assume that this is the last step, insert the action before the finish action } else { - $actions->insertBefore($prev = new FormAction('prev', $step->getPrevText()), 'action_finish'); + $actions->insertBefore($prev = FormAction::create('prev', $step->getPrevText()), 'action_finish'); } //remove browser validation from prev action $prev->setAttribute("formnovalidate", "formnovalidate"); @@ -427,13 +442,13 @@ abstract class MultiForm extends Form */ public function forTemplate() { - $return = $this->renderWith(array( + $return = $this->renderWith([ $this->getCurrentStep()->class, 'MultiFormStep', $this->class, 'MultiForm', 'Form' - )); + ]); $this->clearMessage(); @@ -447,7 +462,8 @@ abstract class MultiForm extends Form * of all the data collected through each step of the form. * * @param array $data The request data returned from the form - * @param object $form The form that the action was called on + * @param Form $form The form that the action was called on + * @return bool */ public function finish($data, $form) { @@ -460,7 +476,7 @@ abstract class MultiForm extends Form } if (!$this->getCurrentStep()->validateStep($data, $form)) { - Session::set("FormInfo.{$form->FormName()}.data", $form->getData()); + Injector::inst()->get(Session::class)->set("FormInfo.{$form->FormName()}.data", $form->getData()); $this->controller->redirectBack(); return false; } @@ -475,7 +491,8 @@ abstract class MultiForm extends Form * then redirects to the newly set step. * * @param array $data The request data returned from the form - * @param object $form The form that the action was called on + * @param Form $form The form that the action was called on + * @return bool */ public function next($data, $form) { @@ -494,17 +511,17 @@ abstract class MultiForm extends Form // built-in functionality). The data needs to be manually saved on error // so the form is re-populated. if (!$this->getCurrentStep()->validateStep($data, $form)) { - Session::set("FormInfo.{$form->FormName()}.data", $form->getData()); + Injector::inst()->get(Session::class)->set("FormInfo.{$form->FormName()}.data", $form->getData()); $this->controller->redirectBack(); return false; } // validation succeeded so we reset it to remove errors and messages - $this->resetValidation(); + $this->clearFormState(); // Determine whether we can use a step already in the DB, or have to create a new one if (!$nextStep = DataObject::get_one($nextStepClass, "\"SessionID\" = {$this->session->ID}")) { - $nextStep = Object::create($nextStepClass); + $nextStep = Injector::inst()->create($nextStepClass); $nextStep->SessionID = $this->session->ID; $nextStep->write(); } @@ -524,7 +541,8 @@ abstract class MultiForm extends Form * Finally, it redirects to that step. * * @param array $data The request data returned from the form - * @param object $form The form that the action was called on + * @param Form $form The form that the action was called on + * @return bool|HTTPResponse */ public function prev($data, $form) { @@ -546,7 +564,7 @@ abstract class MultiForm extends Form $this->setCurrentStep($prevStep); // Redirect to the previous step - $this->controller->redirect($prevStep->Link()); + return $this->controller->redirect($prevStep->Link()); } /** @@ -620,11 +638,11 @@ abstract class MultiForm extends Form * first step. We run {@link getAllStepsRecursive} passing the steps found * by reference to get a listing of the steps. * - * @return DataObjectSet of MultiFormStep instances + * @return ArrayList of MultiFormStep instances */ public function getAllStepsLinear() { - $stepsFound = (class_exists('ArrayList')) ? new ArrayList() : new DataObjectSet(); + $stepsFound = ArrayList::create(); $firstStep = DataObject::get_one(static::$start_step, "\"SessionID\" = {$this->session->ID}"); $firstStep->LinkingMode = ($firstStep->ID == $this->getCurrentStep()->ID) ? 'current' : 'link'; @@ -651,9 +669,9 @@ abstract class MultiForm extends Form * session, its used - otherwise a singleton of this step is used. * Caution: Doesn't consider branching for steps which aren't in the database yet. * - * @param $step Subclass of MultiFormStep to find the next step of + * @param MultiFormStep $step Subclass of MultiFormStep to find the next step of * @param $stepsFound $stepsFound DataObjectSet reference, the steps found to call back on - * @return DataObjectSet of MultiFormStep instances + * @return DataList of MultiFormStep instances */ protected function getAllStepsRecursive($step, &$stepsFound) { diff --git a/src/model/MultiFormSession.php b/src/Models/MultiFormSession.php similarity index 81% rename from src/model/MultiFormSession.php rename to src/Models/MultiFormSession.php index dbd3cf7..431a693 100644 --- a/src/model/MultiFormSession.php +++ b/src/Models/MultiFormSession.php @@ -1,5 +1,10 @@ 'Varchar(40)', // cryptographic hash identification to this session 'IsComplete' => 'Boolean' // flag to determine if this session is marked completed - ); + ]; - private static $has_one = array( + private static $has_one = [ 'Submitter' => 'Member', 'CurrentStep' => 'MultiFormStep' - ); + ]; - private static $has_many = array( + private static $has_many = [ 'FormSteps' => 'MultiFormStep' - ); + ]; + + private static $table_name = 'MultiFormSession'; /** * Mark this session as completed. @@ -45,8 +51,8 @@ class MultiFormSession extends DataObject public function onBeforeWrite() { // save submitter if a Member is logged in - $currentMember = Member::currentUser(); - if (!$this->SubmitterID && $currentMember) { + $currentMember = Security::getCurrentUser(); + if (!$this->SubmitterID && $currentMember->ID) { $this->SubmitterID = $currentMember->ID; } diff --git a/src/model/MultiFormStep.php b/src/Models/MultiFormStep.php similarity index 90% rename from src/model/MultiFormStep.php rename to src/Models/MultiFormStep.php index f08adf7..1e03b21 100644 --- a/src/model/MultiFormStep.php +++ b/src/Models/MultiFormStep.php @@ -1,5 +1,13 @@ 'Text' // stores serialized maps with all session information - ); + ]; - private static $has_one = array( + private static $has_one = [ 'Session' => 'MultiFormSession' - ); + ]; + + private static $table_name = 'MultiFormStep'; /** * Centerpiece of the flow control for the form. @@ -78,14 +87,14 @@ class MultiFormStep extends DataObject * * @var array $extraClasses */ - protected $extraClasses = array(); + protected $extraClasses = []; /** * Temporary cache to increase the performance for repeated look ups. * * @var array $cache */ - protected $step_data_cache = array(); + protected $step_data_cache = []; /** * Form fields to be rendered with this step. @@ -112,14 +121,14 @@ class MultiFormStep extends DataObject */ public function getExtraActions() { - return (class_exists('FieldList')) ? new FieldList() : new FieldSet(); + return FieldList::create(); } /** * Get a validator specific to this form. * The form is automatically validated in {@link Form->httpSubmission()}. * - * @return Validator + * @return bool|Validator */ public function getValidator() { @@ -147,7 +156,10 @@ class MultiFormStep extends DataObject public function Link() { $form = $this->form; - return Controller::join_links($form->getDisplayLink(), "?{$form->config()->get_var}={$this->Session()->Hash}"); + return Controller::join_links( + $form->getDisplayLink(), + "?{$form->config()->get_var}={$this->getSession()->Hash}" + ); } /** @@ -164,7 +176,7 @@ class MultiFormStep extends DataObject */ public function loadData() { - return ($this->Data && is_string($this->Data)) ? unserialize($this->Data) : array(); + return ($this->Data && is_string($this->Data)) ? unserialize($this->Data) : []; } /** @@ -191,14 +203,15 @@ class MultiFormStep extends DataObject * as a simple value outside of the original FormField context. * * @param DataObject $obj + * @return DataObject */ public function saveInto($obj) { - $form = new Form( + $form = Form::create( Controller::curr(), 'Form', $this->getFields(), - ((class_exists('FieldList')) ? new FieldList() : new FieldSet()) + FieldList::create() ); $form->loadDataFrom($this->loadData()); $form->saveInto($obj); @@ -235,7 +248,11 @@ class MultiFormStep extends DataObject // Check if next_steps have been implemented properly if not the final step if (!$this->isFinalStep()) { if (!isset($nextSteps)) { - user_error('MultiFormStep->getNextStep(): Please define at least one $next_steps on ' . $this->class, E_USER_ERROR); + user_error( + 'MultiFormStep->getNextStep(): Please define at least one $next_steps on ' + . $this->class, + E_USER_ERROR + ); } } @@ -326,7 +343,7 @@ class MultiFormStep extends DataObject */ public function getPrevText() { - return _t('MultiForm.BACK', 'Back'); + return _t(__CLASS__ . '.BACK', 'Back'); } /** @@ -335,7 +352,7 @@ class MultiFormStep extends DataObject */ public function getNextText() { - return _t('MultiForm.NEXT', 'Next'); + return _t(__CLASS__ . '.NEXT', 'Next'); } /** @@ -344,13 +361,13 @@ class MultiFormStep extends DataObject */ public function getSubmitText() { - return _t('MultiForm.SUBMIT', 'Submit'); + return _t(__CLASS__ . '.SUBMIT', 'Submit'); } /** * Sets the form that this step is directly related to. * - * @param MultiForm subclass $form + * @param MultiForm $form subclass */ public function setForm($form) { @@ -401,7 +418,7 @@ class MultiFormStep extends DataObject */ public function isCurrentStep() { - return ($this->class == $this->Session()->CurrentStep()->class) ? true : false; + return ($this->class == $this->getSession()->CurrentStep()->class) ? true : false; } /** @@ -458,7 +475,7 @@ class MultiFormStep extends DataObject { // load the steps in the cache, if this one doesn't exist if (!array_key_exists('steps_' . $fromStep, $this->step_data_cache)) { - $steps = MultiFormStep::get()->filter('SessionID', $this->form->session->ID); + $steps = self::get()->filter('SessionID', $this->form->session->ID); if ($steps) { foreach ($steps as $step) { @@ -494,4 +511,13 @@ class MultiFormStep extends DataObject $fields->fieldByName($fieldNameTarget)->setValue($this->getValueFromOtherStep($formStep, $fieldName)); } + + /** + * Gets the linked MultiFormSession + * @return MultiFormSession + */ + public function getSession() + { + return $this->Session(); + } } diff --git a/src/extensions/MultiFormObjectDecorator.php b/src/extensions/MultiFormObjectDecorator.php index 3423a65..73beb63 100644 --- a/src/extensions/MultiFormObjectDecorator.php +++ b/src/extensions/MultiFormObjectDecorator.php @@ -1,5 +1,11 @@ 'Boolean', - ); + ]; - private static $has_one = array( + private static $has_one = [ 'MultiFormSession' => 'MultiFormSession', - ); + ]; /** * Augment any queries to MultiFormObjectDecorator and only * return anything that isn't considered temporary. + * @param SQLSelect $query + * @param DataQuery|null $dataQuery */ - public function augmentSQL(SQLQuery &$query) + public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null) { $where = $query->getWhere(); if (!$where && !$this->wantsTemporary($query)) { @@ -53,7 +61,7 @@ class MultiFormObjectDecorator extends DataExtension * to be exempt from the automatic filtering out * of temporary records. * - * @param SQLQuery $query + * @param SQLSelect $query * @return boolean */ protected function wantsTemporary($query) diff --git a/src/tasks/MultiFormPurgeTask.php b/src/tasks/MultiFormPurgeTask.php index c761e98..0aa56fe 100644 --- a/src/tasks/MultiFormPurgeTask.php +++ b/src/tasks/MultiFormPurgeTask.php @@ -1,5 +1,11 @@ " . self::$session_expiry_days ); } diff --git a/templates/Includes/MultiFormProgressList.ss b/templates/Includes/MultiFormProgressList.ss index 59a420d..af77977 100644 --- a/templates/Includes/MultiFormProgressList.ss +++ b/templates/Includes/MultiFormProgressList.ss @@ -1,9 +1,19 @@ diff --git a/templates/Includes/MultiFormProgressPercent.ss b/templates/Includes/MultiFormProgressPercent.ss index f2b064a..a1d919b 100644 --- a/templates/Includes/MultiFormProgressPercent.ss +++ b/templates/Includes/MultiFormProgressPercent.ss @@ -1 +1 @@ -

You've completed {$CompletedPercent.Nice}% ($CompletedStepCount/$TotalStepCount)

\ No newline at end of file +

<%t MULTIFORM.ProgressPercent "You've completed {percent}% ({completedSteps}/{totalSteps})" percent=$CompletedPercent.Nice completedSteps=$CompletedStepCount totalSteps$TotalStepCount %>

diff --git a/tests/MultiFormObjectDecoratorTest.php b/tests/MultiFormObjectDecoratorTest.php index 15f9b8a..f825b2a 100644 --- a/tests/MultiFormObjectDecoratorTest.php +++ b/tests/MultiFormObjectDecoratorTest.php @@ -1,20 +1,24 @@ array('MultiFormObjectDecorator') - ); + protected $requiredExtensions = [ + 'MultiFormObjectDecoratorDataObject' => ['MultiFormObjectDecorator'] + ]; - protected $extraDataObjects = array( - 'MultiFormObjectDecorator_DataObject' - ); + protected $extraDataObjects = [ + 'MultiFormObjectDecoratorDataObject' + ]; public function testTemporaryDataFilteredQuery() { - $records = MultiFormObjectDecorator_DataObject::get() + $records = MultiFormObjectDecoratorDataObject::get() ->map('Name') ->toArray(); @@ -25,8 +29,8 @@ class MultiFormObjectDecoratorTest extends SapphireTest public function testTemporaryDataQuery() { - $records = MultiFormObjectDecorator_DataObject::get() - ->filter(array('MultiFormIsTemporary' => 1)) + $records = MultiFormObjectDecoratorDataObject::get() + ->filter(['MultiFormIsTemporary' => 1]) ->map('Name') ->toArray(); @@ -35,11 +39,3 @@ class MultiFormObjectDecoratorTest extends SapphireTest $this->assertContains('Test 3', $records); } } - -class MultiFormObjectDecorator_DataObject extends DataObject -{ - - private static $db = array( - 'Name' => 'Varchar' - ); -} diff --git a/tests/MultiFormObjectDecoratorTest.yml b/tests/MultiFormObjectDecoratorTest.yml index 99bee26..1b4fca3 100644 --- a/tests/MultiFormObjectDecoratorTest.yml +++ b/tests/MultiFormObjectDecoratorTest.yml @@ -1,4 +1,4 @@ -MultiFormObjectDecorator_DataObject: +SilverStripe\MultiForm\Tests\MultiFormObjectDecoratorDataObject: test-data-1: Name: Test 1 MultiFormIsTemporary: 0 diff --git a/tests/MultiFormTest.php b/tests/MultiFormTest.php index 589a485..4430046 100644 --- a/tests/MultiFormTest.php +++ b/tests/MultiFormTest.php @@ -1,16 +1,22 @@ controller = new MultiFormTest_Controller(); + $this->controller = new MultiFormTestController(); $this->form = $this->controller->Form(); } @@ -36,22 +47,22 @@ class MultiFormTest extends FunctionalTest { $this->assertTrue(is_numeric($this->form->getCurrentStep()->ID) && ($this->form->getCurrentStep()->ID > 0)); $this->assertTrue(is_numeric($this->form->getSession()->ID) && ($this->form->getSession()->ID > 0)); - $this->assertEquals('MultiFormTest_StepOne', $this->form->getStartStep()); + $this->assertEquals(MultiFormTestStepOne::class, $this->form->getStartStep()); } public function testSessionGeneration() { - $this->assertTrue($this->form->session->ID > 0); + $this->assertTrue($this->form->getSession()->ID > 0); } public function testMemberLogging() { // Grab any user to fake being logged in as, and ensure that after a session is written it has // that user as the submitter. - $userId = Member::get_one("Member")->ID; - $this->session()->inst_set('loggedInAs', $userId); - $session = $this->form->session; + $userId = $this->logInWithPermission('ADMIN'); + + $session = $this->form->getSession(); $session->write(); $this->assertEquals($userId, $session->SubmitterID); @@ -59,7 +70,7 @@ class MultiFormTest extends FunctionalTest public function testSecondStep() { - $this->assertEquals('MultiFormTest_StepTwo', $this->form->getCurrentStep()->getNextStep()); + $this->assertEquals(MultiFormTestStepTwo::class, $this->form->getCurrentStep()->getNextStep()); } public function testParentForm() @@ -75,9 +86,9 @@ class MultiFormTest extends FunctionalTest public function testCompletedSession() { - $this->form->setCurrentSessionHash($this->form->session->Hash); + $this->form->setCurrentSessionHash($this->form->getSession()->Hash); $this->assertInstanceOf('MultiFormSession', $this->form->getCurrentSession()); - $this->form->session->markCompleted(); + $this->form->getSession()->markCompleted(); $this->assertNull($this->form->getCurrentSession()); } @@ -86,112 +97,27 @@ class MultiFormTest extends FunctionalTest $this->form->setCurrentSessionHash('sdfsdf3432325325sfsdfdf'); // made up! // A new session is generated, even though we made up the identifier - $this->assertInstanceOf('MultiFormSession', $this->form->session); + $this->assertInstanceOf('MultiFormSession', $this->form->getSession()); } - function testCustomGetVar() + public function testCustomGetVar() { Config::nest(); - Config::inst()->update('MultiForm', 'get_var', 'SuperSessionID'); + Config::modify()->set('MultiForm', 'get_var', 'SuperSessionID'); $form = $this->controller->Form(); $this->assertContains('SuperSessionID', $form::$ignored_fields, "GET var wasn't added to ignored fields"); - $this->assertContains('SuperSessionID', $form->FormAction(), "Form action doesn't contain correct session - ID parameter"); - $this->assertContains('SuperSessionID', $form->getCurrentStep()->Link(), "Form step doesn't contain correct - session ID parameter"); + $this->assertContains( + 'SuperSessionID', + $form->FormAction(), + "Form action doesn't contain correct session ID parameter" + ); + $this->assertContains( + 'SuperSessionID', + $form->getCurrentStep()->Link(), + "Form step doesn't contain correct session ID parameter" + ); Config::unnest(); } } - -/** - * @package multiform - * @subpackage tests - */ -class MultiFormTest_Controller extends Controller implements TestOnly -{ - - public function Link() - { - return 'MultiFormTest_Controller'; - } - - public function Form($request = null) - { - $form = new MultiFormTest_Form($this, 'Form'); - $form->setHTMLID('MultiFormTest_Form'); - return $form; - } -} - -/** - * @package multiform - * @subpackage tests - */ -class MultiFormTest_Form extends MultiForm implements TestOnly -{ - - public static $start_step = 'MultiFormTest_StepOne'; - - public function getStartStep() - { - return self::$start_step; - } -} - -/** - * @package multiform - * @subpackage tests - */ -class MultiFormTest_StepOne extends MultiFormStep implements TestOnly -{ - - public static $next_steps = 'MultiFormTest_StepTwo'; - - public function getFields() - { - $class = (class_exists('FieldList')) ? 'FieldList' : 'FieldSet'; - return new $class( - new TextField('FirstName', 'First name'), - new TextField('Surname', 'Surname'), - new EmailField('Email', 'Email address') - ); - } -} - -/** - * @package multiform - * @subpackage tests - */ -class MultiFormTest_StepTwo extends MultiFormStep implements TestOnly -{ - - public static $next_steps = 'MultiFormTest_StepThree'; - - public function getFields() - { - $class = (class_exists('FieldList')) ? 'FieldList' : 'FieldSet'; - return new $class( - new TextareaField('Comments', 'Tell us a bit about yourself...') - ); - } -} - -/** - * @package multiform - * @subpackage tests - */ -class MultiFormTest_StepThree extends MultiFormStep implements TestOnly -{ - - public static $is_final_step = true; - - public function getFields() - { - $class = (class_exists('FieldList')) ? 'FieldList' : 'FieldSet'; - return new $class( - new TextField('Test', 'Anything else you\'d like to tell us?') - ); - } -} diff --git a/tests/MultiFormTest.yml b/tests/MultiFormTest.yml index b11eb72..e4a5ae0 100644 --- a/tests/MultiFormTest.yml +++ b/tests/MultiFormTest.yml @@ -1,4 +1,4 @@ -Member: +SilverStripe\Security\Member: admin: FirstName: Admin Surname: Admin diff --git a/tests/Stubs/MultiFormObjectDecoratorDataObject.php b/tests/Stubs/MultiFormObjectDecoratorDataObject.php new file mode 100644 index 0000000..f2d3d0f --- /dev/null +++ b/tests/Stubs/MultiFormObjectDecoratorDataObject.php @@ -0,0 +1,13 @@ + 'Varchar' + ]; +} diff --git a/tests/Stubs/MultiFormObjectDecorator_DataObject.php b/tests/Stubs/MultiFormObjectDecorator_DataObject.php deleted file mode 100644 index 88e83ad..0000000 --- a/tests/Stubs/MultiFormObjectDecorator_DataObject.php +++ /dev/null @@ -1,8 +0,0 @@ -setHTMLID(self::class); + return $form; + } +} diff --git a/tests/Stubs/MultiFormTestForm.php b/tests/Stubs/MultiFormTestForm.php index 6ab1dee..aeb5ab5 100644 --- a/tests/Stubs/MultiFormTestForm.php +++ b/tests/Stubs/MultiFormTestForm.php @@ -1,8 +1,20 @@