Skip to content

Commit

Permalink
Refactor setup/form_processing to static methods
Browse files Browse the repository at this point in the history
Signed-off-by: Maurício Meneghini Fauth <mauriciofauth@gmail.com>
  • Loading branch information
MauricioFauth committed Sep 14, 2017
1 parent 614b8a6 commit b68b570
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 90 deletions.
8 changes: 2 additions & 6 deletions setup/frames/form.inc.php
Expand Up @@ -8,16 +8,12 @@

use PhpMyAdmin\Config\Forms\Setup\SetupFormList;
use PhpMyAdmin\Core;
use PhpMyAdmin\Setup\FormProcessing;

if (!defined('PHPMYADMIN')) {
exit;
}

/**
* Core libraries.
*/
require_once './setup/lib/form_processing.lib.php';

$formset_id = Core::isValid($_GET['formset'], 'scalar') ? $_GET['formset'] : null;
$mode = isset($_GET['mode']) ? $_GET['mode'] : null;
$form_class = SetupFormList::get($formset_id);
Expand All @@ -26,4 +22,4 @@
}
echo '<h2>' , $form_class::getName() , '</h2>';
$form_display = new $form_class($GLOBALS['ConfigFile']);
PMA_Process_formset($form_display);
FormProcessing::process($form_display);
8 changes: 2 additions & 6 deletions setup/frames/servers.inc.php
Expand Up @@ -9,17 +9,13 @@
use PhpMyAdmin\Config\ConfigFile;
use PhpMyAdmin\Config\Forms\Setup\ServersForm;
use PhpMyAdmin\Core;
use PhpMyAdmin\Setup\FormProcessing;
use PhpMyAdmin\Url;

if (!defined('PHPMYADMIN')) {
exit;
}

/**
* Core libraries.
*/
require_once './setup/lib/form_processing.lib.php';

$mode = isset($_GET['mode']) ? $_GET['mode'] : null;
$id = Core::isValid($_GET['id'], 'numeric') ? intval($_GET['id']) : null;

Expand All @@ -45,4 +41,4 @@
echo '<h2>' , $page_title . '</h2>';
}
$form_display = new ServersForm($cf, $id);
PMA_Process_formset($form_display);
FormProcessing::process($form_display);
77 changes: 77 additions & 0 deletions setup/lib/FormProcessing.php
@@ -0,0 +1,77 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Formset processing library
*
* @package PhpMyAdmin-Setup
*/
namespace PhpMyAdmin\Setup;

use PhpMyAdmin\Config\FormDisplay;
use PhpMyAdmin\Core;
use PhpMyAdmin\Url;
use PhpMyAdmin\Response;

/**
* PhpMyAdmin\Setup\FormProcessing class
*
* @package PhpMyAdmin-Setup
*/
class FormProcessing
{
/**
* Processes forms registered in $form_display, handles error correction
*
* @param FormDisplay $form_display Form to display
*
* @return void
*/
public static function process(FormDisplay $form_display)
{
if (isset($_GET['mode']) && $_GET['mode'] == 'revert') {
// revert erroneous fields to their default values
$form_display->fixErrors();
$response = Response::getInstance();
$response->generateHeader303('index.php' . Url::getCommonRaw());
}

if (!$form_display->process(false)) {
// handle form view and failed POST
echo $form_display->getDisplay(true, true);
return;
}

// check for form errors
if (!$form_display->hasErrors()) {
$response = Response::getInstance();
$response->generateHeader303('index.php' . Url::getCommonRaw());
return;
}

// form has errors, show warning
$page = isset($_GET['page']) ? $_GET['page'] : '';
$formset = isset($_GET['formset']) ? $_GET['formset'] : '';
$formId = Core::isValid($_GET['id'], 'numeric') ? $_GET['id'] : '';
if ($formId === null && $page == 'servers') {
// we've just added a new server, get its id
$formId = $form_display->getConfigFile()->getServerCount();
}
?>
<div class="error">
<h4><?php echo __('Warning') ?></h4>
<?php echo __('Submitted form contains errors') ?><br />
<a href="<?php echo Url::getCommon(array('page' => $page, 'formset' => $formset, 'id' => $formId, 'mode' => 'revert')) ?>">
<?php echo __('Try to revert erroneous fields to their default values') ?>
</a>
</div>
<?php echo $form_display->displayErrors() ?>
<a class="btn" href="index.php<?php echo Url::getCommon() ?>">
<?php echo __('Ignore errors') ?>
</a>
&nbsp;
<a class="btn" href="<?php echo Url::getCommon(array('page' => $page, 'formset' => $formset, 'id' => $formId, 'mode' => 'edit')) ?>">
<?php echo __('Show form') ?>
</a>
<?php
}
}
67 changes: 0 additions & 67 deletions setup/lib/form_processing.lib.php

This file was deleted.

Expand Up @@ -5,19 +5,16 @@
*
* @package PhpMyAdmin-test
*/
namespace PhpMyAdmin\Tests\Setup;

/*
* Include to test
*/
require_once 'setup/lib/form_processing.lib.php';

use PhpMyAdmin\Setup\FormProcessing;

/**
* tests for methods under Formset processing library
*
* @package PhpMyAdmin-test
*/
class PMA_Form_Processing_Test extends PMATestCase
class FormProcessingTest extends \PMATestCase
{
/**
* Prepares environment for the test.
Expand Down Expand Up @@ -60,7 +57,7 @@ public function testProcessFormSet()
->method('getDisplay')
->with(true, true);

PMA_Process_formset($formDisplay);
FormProcessing::process($formDisplay);

// case 2
$formDisplay = $this->getMockBuilder('PhpMyAdmin\Config\FormDisplay')
Expand All @@ -79,7 +76,7 @@ public function testProcessFormSet()
->will($this->returnValue(true));

ob_start();
PMA_Process_formset($formDisplay);
FormProcessing::process($formDisplay);
$result = ob_get_clean();

$this->assertContains(
Expand Down Expand Up @@ -118,8 +115,6 @@ public function testProcessFormSet()
->with()
->will($this->returnValue(false));

PMA_Process_formset($formDisplay);

FormProcessing::process($formDisplay);
}

}

0 comments on commit b68b570

Please sign in to comment.