From faf6af1c371296f0db41806388162128efcd3dae Mon Sep 17 00:00:00 2001 From: bancer Date: Wed, 8 Mar 2017 14:19:08 +0100 Subject: [PATCH 01/12] #more unit tests --- .../Component/WizardComponentTest.php | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/Test/Case/Controller/Component/WizardComponentTest.php b/Test/Case/Controller/Component/WizardComponentTest.php index 2bbd55a..3553095 100644 --- a/Test/Case/Controller/Component/WizardComponentTest.php +++ b/Test/Case/Controller/Component/WizardComponentTest.php @@ -23,6 +23,7 @@ class WizardUserMock extends Model { /** * AuthTestController class * + * @property WizardComponent $Wizard * @package Wizard.Test.Case.Controller.Component */ class WizardTestController extends Controller { @@ -71,6 +72,17 @@ public function processStep2() { return false; } + public function processGender() { + if (!empty($this->request->data)) { + if ($this->request->data['User']['gender'] == 'female') { + $this->Wizard->unbranch('male'); + $this->Wizard->branch('female'); + } + return true; + } + return false; + } + public function processStep3() { if (!empty($this->request->data)) { return true; @@ -344,6 +356,66 @@ public function testProcessStepOnePost() { $this->assertEquals($expectedSession, $resultSession); } + public function testProcessGenderPost() { + // Set session prerequisites. + $session = array( + 'config' => array( + 'steps' => array( + 'step1', + 'step2', + 'gender', + 'step3', + 'step4', + 'confirmation', + ), + 'action' => 'wizard', + 'expectedStep' => 'gender', + 'activeStep' => 'gender', + ), + 'WizardTest' => array( + 'step1' => array(), + 'step2' => array(), + ), + ); + $this->Wizard->Session->write('Wizard', $session); + + $this->Wizard->startup($this->Controller); + $postData = array( + 'User' => array( + 'gender' => 'female', + ), + ); + $this->Wizard->controller->request->data = $postData; + $CakeResponse = $this->Wizard->process('gender'); + + $this->assertInstanceOf('CakeResponse', $CakeResponse); + $headers = $CakeResponse->header(); + $this->assertContains('/wizard/step4', $headers['Location']); + + $expectedSession = array( + 'config' => array( + 'steps' => array( + 'step1', + 'step2', + 'gender', + 'step4', + 'step5', + 'confirmation', + ), + 'action' => 'wizard', + 'expectedStep' => 'step4', + 'activeStep' => 'step4', + ), + 'complete' => array( + 'step1' => array(), + 'step2' => array(), + 'gender' => $postData, + ), + ); + $resultSession = $this->Wizard->Session->read('Wizard'); + $this->assertEquals($expectedSession, $resultSession); + } + public function testProcessAutovalidatePost() { // Set session prerequisites. $session = array( From 9b7c204979edc0c3a17746d906440ee28fd3e4b1 Mon Sep 17 00:00:00 2001 From: bancer Date: Wed, 8 Mar 2017 14:41:10 +0100 Subject: [PATCH 02/12] adjusted unit tests --- .../Component/WizardComponentTest.php | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/Test/Case/Controller/Component/WizardComponentTest.php b/Test/Case/Controller/Component/WizardComponentTest.php index 3553095..a4aaf4c 100644 --- a/Test/Case/Controller/Component/WizardComponentTest.php +++ b/Test/Case/Controller/Component/WizardComponentTest.php @@ -74,9 +74,12 @@ public function processStep2() { public function processGender() { if (!empty($this->request->data)) { - if ($this->request->data['User']['gender'] == 'female') { + if ($this->request->data['WizardUserMock']['gender'] == 'female') { $this->Wizard->unbranch('male'); $this->Wizard->branch('female'); + } else { + $this->Wizard->unbranch('female'); + $this->Wizard->branch('male'); } return true; } @@ -357,6 +360,19 @@ public function testProcessStepOnePost() { } public function testProcessGenderPost() { + $this->Wizard->Session->delete('Wizard'); + unset($this->Controller, $this->Wizard); + $CakeRequest = new CakeRequest(null, false); + $CakeResponse = $this->getMock('CakeResponse', array('send')); + $this->Controller = new WizardTestController($CakeRequest, $CakeResponse); + $this->Controller->components['Wizard.Wizard']['autoAdvance'] = false; + $this->Controller->components['Wizard.Wizard']['defaultBranch'] = false; + $ComponentCollection = new ComponentCollection(); + $ComponentCollection->init($this->Controller); + $this->Controller->Components->init($this->Controller); + $this->Wizard = $this->Controller->Wizard; + $this->Wizard->initialize($this->Controller); + // Set session prerequisites. $session = array( 'config' => array( @@ -364,8 +380,6 @@ public function testProcessGenderPost() { 'step1', 'step2', 'gender', - 'step3', - 'step4', 'confirmation', ), 'action' => 'wizard', @@ -381,7 +395,7 @@ public function testProcessGenderPost() { $this->Wizard->startup($this->Controller); $postData = array( - 'User' => array( + 'WizardUserMock' => array( 'gender' => 'female', ), ); From 15ca3416408b692a504f5a576d6a970f48997ff5 Mon Sep 17 00:00:00 2001 From: bancer Date: Wed, 8 Mar 2017 14:45:13 +0100 Subject: [PATCH 03/12] better clear session in tests --- Test/Case/Controller/Component/WizardComponentTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Test/Case/Controller/Component/WizardComponentTest.php b/Test/Case/Controller/Component/WizardComponentTest.php index a4aaf4c..4bd9d27 100644 --- a/Test/Case/Controller/Component/WizardComponentTest.php +++ b/Test/Case/Controller/Component/WizardComponentTest.php @@ -151,7 +151,7 @@ public function setUp() { */ public function tearDown() { parent::tearDown(); - $this->Wizard->Session->delete('Wizard'); + CakeSession::destroy(); unset($this->Controller, $this->Wizard); } From e1966d74b3f7147701459293e08e6771a4f0e4b6 Mon Sep 17 00:00:00 2001 From: bancer Date: Wed, 8 Mar 2017 14:52:59 +0100 Subject: [PATCH 04/12] unit tests adjusted --- .../Component/WizardComponentTest.php | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/Test/Case/Controller/Component/WizardComponentTest.php b/Test/Case/Controller/Component/WizardComponentTest.php index 4bd9d27..4a54c06 100644 --- a/Test/Case/Controller/Component/WizardComponentTest.php +++ b/Test/Case/Controller/Component/WizardComponentTest.php @@ -74,12 +74,14 @@ public function processStep2() { public function processGender() { if (!empty($this->request->data)) { - if ($this->request->data['WizardUserMock']['gender'] == 'female') { - $this->Wizard->unbranch('male'); - $this->Wizard->branch('female'); - } else { - $this->Wizard->unbranch('female'); - $this->Wizard->branch('male'); + if ($this->components['Wizard.Wizard']['defaultBranch'] === false) { + if ($this->request->data['WizardUserMock']['gender'] == 'female') { + $this->Wizard->unbranch('male'); + $this->Wizard->branch('female'); + } else { + $this->Wizard->unbranch('female'); + $this->Wizard->branch('male'); + } } return true; } @@ -402,10 +404,6 @@ public function testProcessGenderPost() { $this->Wizard->controller->request->data = $postData; $CakeResponse = $this->Wizard->process('gender'); - $this->assertInstanceOf('CakeResponse', $CakeResponse); - $headers = $CakeResponse->header(); - $this->assertContains('/wizard/step4', $headers['Location']); - $expectedSession = array( 'config' => array( 'steps' => array( @@ -428,6 +426,10 @@ public function testProcessGenderPost() { ); $resultSession = $this->Wizard->Session->read('Wizard'); $this->assertEquals($expectedSession, $resultSession); + + $this->assertInstanceOf('CakeResponse', $CakeResponse); + $headers = $CakeResponse->header(); + $this->assertContains('/wizard/step4', $headers['Location']); } public function testProcessAutovalidatePost() { From c21d6798a77f0d15566009c06f5a89c2bbb50a37 Mon Sep 17 00:00:00 2001 From: bancer Date: Wed, 8 Mar 2017 15:01:19 +0100 Subject: [PATCH 05/12] unit tests --- Test/Case/Controller/Component/WizardComponentTest.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Test/Case/Controller/Component/WizardComponentTest.php b/Test/Case/Controller/Component/WizardComponentTest.php index 4a54c06..36c4196 100644 --- a/Test/Case/Controller/Component/WizardComponentTest.php +++ b/Test/Case/Controller/Component/WizardComponentTest.php @@ -74,7 +74,7 @@ public function processStep2() { public function processGender() { if (!empty($this->request->data)) { - if ($this->components['Wizard.Wizard']['defaultBranch'] === false) { + if ($this->Wizard->defaultBranch === false) { if ($this->request->data['WizardUserMock']['gender'] == 'female') { $this->Wizard->unbranch('male'); $this->Wizard->branch('female'); @@ -361,6 +361,9 @@ public function testProcessStepOnePost() { $this->assertEquals($expectedSession, $resultSession); } + /** + * Tests 'autoAdvance' and 'defaultBranch' settings set to false and manual call to `branch()`. + */ public function testProcessGenderPost() { $this->Wizard->Session->delete('Wizard'); unset($this->Controller, $this->Wizard); @@ -405,6 +408,7 @@ public function testProcessGenderPost() { $CakeResponse = $this->Wizard->process('gender'); $expectedSession = array( + 'branches' => array(), 'config' => array( 'steps' => array( 'step1', @@ -416,13 +420,14 @@ public function testProcessGenderPost() { ), 'action' => 'wizard', 'expectedStep' => 'step4', - 'activeStep' => 'step4', + 'activeStep' => 'gender', ), 'complete' => array( 'step1' => array(), 'step2' => array(), 'gender' => $postData, ), + 'WizardTest' => array(), ); $resultSession = $this->Wizard->Session->read('Wizard'); $this->assertEquals($expectedSession, $resultSession); From e65b739e06aaad5e15eed2b56b568244a4ed40ac Mon Sep 17 00:00:00 2001 From: bancer Date: Wed, 8 Mar 2017 15:05:22 +0100 Subject: [PATCH 06/12] unit tests improved --- .../Controller/Component/WizardComponentTest.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Test/Case/Controller/Component/WizardComponentTest.php b/Test/Case/Controller/Component/WizardComponentTest.php index 36c4196..774ef4c 100644 --- a/Test/Case/Controller/Component/WizardComponentTest.php +++ b/Test/Case/Controller/Component/WizardComponentTest.php @@ -361,9 +361,11 @@ public function testProcessStepOnePost() { $this->assertEquals($expectedSession, $resultSession); } - /** - * Tests 'autoAdvance' and 'defaultBranch' settings set to false and manual call to `branch()`. - */ +/** + * Tests 'autoAdvance' and 'defaultBranch' settings set to false and manual call to `branch()`. + * + * @return void + */ public function testProcessGenderPost() { $this->Wizard->Session->delete('Wizard'); unset($this->Controller, $this->Wizard); @@ -408,7 +410,9 @@ public function testProcessGenderPost() { $CakeResponse = $this->Wizard->process('gender'); $expectedSession = array( - 'branches' => array(), + 'branches' => array( + 'WizardTest' => array(), + ), 'config' => array( 'steps' => array( 'step1', @@ -422,12 +426,11 @@ public function testProcessGenderPost() { 'expectedStep' => 'step4', 'activeStep' => 'gender', ), - 'complete' => array( + 'WizardTest' => array( 'step1' => array(), 'step2' => array(), 'gender' => $postData, ), - 'WizardTest' => array(), ); $resultSession = $this->Wizard->Session->read('Wizard'); $this->assertEquals($expectedSession, $resultSession); From 9fdef2e368f61f30f4895332f319e56bdd9fd7ef Mon Sep 17 00:00:00 2001 From: bancer Date: Wed, 8 Mar 2017 15:17:18 +0100 Subject: [PATCH 07/12] Some refactoring in order to reduce the number of redirects --- Controller/Component/WizardComponent.php | 9 +++++++-- Test/Case/Controller/Component/WizardComponentTest.php | 4 +++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Controller/Component/WizardComponent.php b/Controller/Component/WizardComponent.php index fe1a3e5..be3f3d1 100644 --- a/Controller/Component/WizardComponent.php +++ b/Controller/Component/WizardComponent.php @@ -227,14 +227,18 @@ public function initialize(Controller $controller) { * @return void */ public function startup(Controller $controller) { - $this->steps = $this->_parseSteps($this->steps); $this->config('action', $this->action); - $this->config('steps', $this->steps); + $this->_configSteps(); if (!in_array('Wizard.Wizard', $this->controller->helpers) && !array_key_exists('Wizard.Wizard', $this->controller->helpers)) { $this->controller->helpers[] = 'Wizard.Wizard'; } } + protected function _configSteps() { + $this->steps = $this->_parseSteps($this->steps); + $this->config('steps', $this->steps); + } + /** * Parses the steps array by stripping off nested arrays not included in the branches * and returns a simple array with the correct steps. @@ -584,6 +588,7 @@ public function branch($name, $skip = false) { } $branches[$name] = $value; $this->controller->Session->write($this->_branchKey, $branches); + $this->_configSteps(); } /** diff --git a/Test/Case/Controller/Component/WizardComponentTest.php b/Test/Case/Controller/Component/WizardComponentTest.php index 774ef4c..5a8419d 100644 --- a/Test/Case/Controller/Component/WizardComponentTest.php +++ b/Test/Case/Controller/Component/WizardComponentTest.php @@ -411,7 +411,9 @@ public function testProcessGenderPost() { $expectedSession = array( 'branches' => array( - 'WizardTest' => array(), + 'WizardTest' => array( + 'female' => 'branch', + ), ), 'config' => array( 'steps' => array( From 0fb4e4e24f69359dd0aa9eb2300f00f1c0828740 Mon Sep 17 00:00:00 2001 From: bancer Date: Wed, 8 Mar 2017 15:32:33 +0100 Subject: [PATCH 08/12] more refactoring --- Controller/Component/WizardComponent.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Controller/Component/WizardComponent.php b/Controller/Component/WizardComponent.php index be3f3d1..541e47f 100644 --- a/Controller/Component/WizardComponent.php +++ b/Controller/Component/WizardComponent.php @@ -198,6 +198,8 @@ class WizardComponent extends Component { */ protected $_wizardUrl = array(); + protected $_stepsAndBranches = array(); + /** * Initializes WizardComponent for use in the controller * @@ -215,6 +217,7 @@ public function initialize(Controller $controller) { } $this->_configKey = 'Wizard.config'; $this->_branchKey = 'Wizard.branches.' . $controller->name; + $this->_stepsAndBranches = $this->steps; } /** @@ -228,14 +231,14 @@ public function initialize(Controller $controller) { */ public function startup(Controller $controller) { $this->config('action', $this->action); - $this->_configSteps(); + $this->_configSteps($this->steps); if (!in_array('Wizard.Wizard', $this->controller->helpers) && !array_key_exists('Wizard.Wizard', $this->controller->helpers)) { $this->controller->helpers[] = 'Wizard.Wizard'; } } - protected function _configSteps() { - $this->steps = $this->_parseSteps($this->steps); + protected function _configSteps($steps) { + $this->steps = $this->_parseSteps($steps); $this->config('steps', $this->steps); } @@ -588,7 +591,7 @@ public function branch($name, $skip = false) { } $branches[$name] = $value; $this->controller->Session->write($this->_branchKey, $branches); - $this->_configSteps(); + $this->_configSteps($this->_stepsAndBranches); } /** From cebb12658f0da168db026eeda4bd5f857436f3c4 Mon Sep 17 00:00:00 2001 From: bancer Date: Wed, 8 Mar 2017 15:40:54 +0100 Subject: [PATCH 09/12] more docs and reset the expected step in branch() --- Controller/Component/WizardComponent.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Controller/Component/WizardComponent.php b/Controller/Component/WizardComponent.php index 541e47f..da99eaa 100644 --- a/Controller/Component/WizardComponent.php +++ b/Controller/Component/WizardComponent.php @@ -198,6 +198,11 @@ class WizardComponent extends Component { */ protected $_wizardUrl = array(); +/** + * Holds the array with steps and branches from the initial Wizard configuration. + * + * @var array + */ protected $_stepsAndBranches = array(); /** @@ -237,6 +242,13 @@ public function startup(Controller $controller) { } } +/** + * Parses the steps array by stripping off nested arrays not included in the branches + * and writes a simple array with the correct steps to session. + * + * @param array $steps Array to be parsed for nested arrays. + * @return void + */ protected function _configSteps($steps) { $this->steps = $this->_parseSteps($steps); $this->config('steps', $this->steps); @@ -592,6 +604,7 @@ public function branch($name, $skip = false) { $branches[$name] = $value; $this->controller->Session->write($this->_branchKey, $branches); $this->_configSteps($this->_stepsAndBranches); + $this->_getExpectedStep(); } /** From 29e05ca06831700709c04607fc223afbdb6a3b1f Mon Sep 17 00:00:00 2001 From: bancer Date: Wed, 8 Mar 2017 15:57:14 +0100 Subject: [PATCH 10/12] updates expected step after save() and updates steps after unbranch() is called --- Controller/Component/WizardComponent.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Controller/Component/WizardComponent.php b/Controller/Component/WizardComponent.php index da99eaa..e2109f7 100644 --- a/Controller/Component/WizardComponent.php +++ b/Controller/Component/WizardComponent.php @@ -556,6 +556,7 @@ public function save($step = null, $data = null) { $data = $this->controller->request->data; } $this->controller->Session->write("$this->_sessionKey.$step", $data); + $this->_getExpectedStep(); } /** @@ -604,7 +605,6 @@ public function branch($name, $skip = false) { $branches[$name] = $value; $this->controller->Session->write($this->_branchKey, $branches); $this->_configSteps($this->_stepsAndBranches); - $this->_getExpectedStep(); } /** @@ -676,6 +676,7 @@ public function delete($key = null) { */ public function unbranch($branch) { $this->controller->Session->delete("$this->_branchKey.$branch"); + $this->_configSteps($this->_stepsAndBranches); } } From d36fea2650b1ee832b04ff903e704d90f668f9c5 Mon Sep 17 00:00:00 2001 From: bancer Date: Wed, 8 Mar 2017 16:25:10 +0100 Subject: [PATCH 11/12] ensure the current step is set properly --- Controller/Component/WizardComponent.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Controller/Component/WizardComponent.php b/Controller/Component/WizardComponent.php index e2109f7..f90a6dd 100644 --- a/Controller/Component/WizardComponent.php +++ b/Controller/Component/WizardComponent.php @@ -557,6 +557,7 @@ public function save($step = null, $data = null) { } $this->controller->Session->write("$this->_sessionKey.$step", $data); $this->_getExpectedStep(); + $this->_setCurrentStep($step); } /** From b348094aca5994ee6a745659c72041b3e994886a Mon Sep 17 00:00:00 2001 From: bancer Date: Thu, 9 Mar 2017 11:28:04 +0100 Subject: [PATCH 12/12] Fixed indefinite loop bug. --- Controller/Component/WizardComponent.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Controller/Component/WizardComponent.php b/Controller/Component/WizardComponent.php index ef40dcf..9ee8bb0 100644 --- a/Controller/Component/WizardComponent.php +++ b/Controller/Component/WizardComponent.php @@ -529,6 +529,9 @@ protected function _validStep($step) { * @return void */ protected function _setCurrentStep($step) { + if (!in_array($step, $this->steps)) { + return; + } $this->_currentStep = reset($this->steps); while (current($this->steps) != $step) { $this->_currentStep = next($this->steps);