Skip to content

Commit

Permalink
Fix validation of form name and description when empty
Browse files Browse the repository at this point in the history
- Added "not_empty" validation rule to name
- Always check validation for name
- Added migration allowing null for description
- Add a test for setting form.name to empty
  • Loading branch information
ryanchristo authored and rjmackay committed Jul 27, 2017
1 parent 2fe2a73 commit 2ac537b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
7 changes: 4 additions & 3 deletions application/classes/Ushahidi/Validator/Form/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ class Ushahidi_Validator_Form_Create extends Ushahidi_Validator_Form_Update
protected function getRules()
{
return array_merge_recursive(parent::getRules(), [
'name' => [['not_empty'],
[[$this, 'checkPostTypeLimit'], [':validation']],
]]);
'name' => [
[[$this, 'checkPostTypeLimit'], [':validation']],
]
]);
}
}
10 changes: 10 additions & 0 deletions application/classes/Ushahidi/Validator/Form/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,18 @@ public function __construct(FormRepository $repo)

protected function getRules()
{
// Always check validation for name
$name = $this->validation_engine->getFullData('name');
if ($name) {
$data = $this->validation_engine->getData();
$data['name'] = $name;
$this->validation_engine->setData($data);
}
// End

return [
'name' => [
['not_empty'],
['min_length', [':value', 2]],
['regex', [':value', Validator::REGEX_STANDARD_TEXT]], // alpha, number, punctuation, space
],
Expand Down
26 changes: 26 additions & 0 deletions migrations/20170630084848_allow_null_in_description_forms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Phinx\Migration\AbstractMigration;

class AllowNullInDescriptionForms extends AbstractMigration
{
/**
* Migrate Up.
*/
public function up()
{
$this->table('forms')
->changeColumn('description', 'text', ['null' => true])
->save();
}

/**
* Migrate Down.
*/
public function down()
{
$this->table('forms')
->changeColumn('description', 'text', ['null' => false])
->save();
}
}
18 changes: 18 additions & 0 deletions tests/integration/forms.feature
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ Feature: Testing the Forms API
And the "everyone_can_create" property is false
Then the guzzle status code should be 200

Scenario: Updating a Form to clear name should fail
Given that I want to update a "Form"
And that the request "data" is:
"""
{
"name":"",
"type":"report",
"description":"This is a test form updated by BDD testing",
"disabled":true,
"require_approval":false,
"everyone_can_create":false
}
"""
And that its "id" is "1"
When I request "/forms"
Then the response is JSON
Then the guzzle status code should be 422

Scenario: Update a non-existent Form
Given that I want to update a "Form"
And that the request "data" is:
Expand Down

0 comments on commit 2ac537b

Please sign in to comment.