invalid drop rules input

fixes CNVS-9210

test plan:
 - with draft state on, in the assignments index page
 - attempt to input 'tree' or 'plant' or anything really, into
   both the drop lowest scores and drop highest scores input boxes
   in the edit assignment group dialog
 - click save
 - it should ask you to use a number and it should not close the
   dialog or save the rules

Change-Id: I72ecfb3181e685d6c619af4ba0741838018a87ed
Reviewed-on: https://gerrit.instructure.com/25975
Tested-by: Jenkins <jenkins@instructure.com>
Reviewed-by: Simon Williams <simon@instructure.com>
QA-Review: Caleb Guanzon <cguanzon@instructure.com>
Product-Review: David Scoville <davids@instructure.com>
This commit is contained in:
Cameron Sutter 2013-11-04 21:36:30 -07:00
parent b9a3185a41
commit 07f2868daa
4 changed files with 77 additions and 3 deletions

View File

@ -1,4 +1,5 @@
define [
'i18n!assignments'
'underscore'
'compiled/models/AssignmentGroup'
'compiled/collections/NeverDropCollection'
@ -6,7 +7,7 @@ define [
'compiled/views/DialogFormView'
'jst/assignments/CreateGroup'
'jst/EmptyDialogFormWrapper'
], ( _, AssignmentGroup, NeverDropCollection, NeverDropCollectionView, DialogFormView, template, wrapper) ->
], (I18n, _, AssignmentGroup, NeverDropCollection, NeverDropCollectionView, DialogFormView, template, wrapper) ->
class CreateGroupView extends DialogFormView
defaults:
@ -27,6 +28,11 @@ define [
@optionProperty 'assignmentGroup'
@optionProperty 'course'
messages:
non_number: I18n.t('non_number', 'You must use a number')
positive_number: I18n.t('positive_number', 'You must use a positive number')
max_number: I18n.t('higher_than_max', 'You cannot use a number greater than the number of assignments')
initialize: ->
super
#@assignmentGroup will be defined when editing
@ -50,6 +56,24 @@ define [
delete data.rules.never_drop if data.rules.never_drop?.length == 0
data
validateFormData: (data) ->
max = null
if @assignmentGroup
as = @assignmentGroup.get('assignments')
max = as.size() if as?
errors = {}
_.each data.rules, (value, name) =>
val = parseInt(value)
field = "rules[#{name}]"
if isNaN(val)
errors[field] = [{type: 'number', message: @messages.non_number}]
if val < 0
errors[field] = [{type: 'positive_number', message: @messages.positive_number}]
if max?
if val > max
errors[field] = [{type: 'maximum', message: @messages.max_number}]
errors
showWeight: ->
course = @course or @model.collection?.course
course?.get('apply_assignment_group_weights')

View File

@ -39,7 +39,7 @@
</label>
<div class="controls">
<input
type="text"
type="number"
id="ag_{{label_id}}_drop_lowest"
class="span1 field"
name="rules[drop_lowest]"
@ -53,7 +53,7 @@
</label>
<div class="controls">
<input
type="text"
type="number"
id="ag_{{label_id}}_drop_highest"
class="span1 field"
name="rules[drop_highest]"

View File

@ -0,0 +1,49 @@
define [
'underscore'
'Backbone'
'compiled/collections/AssignmentGroupCollection'
'compiled/models/AssignmentGroup'
'compiled/models/Assignment'
'compiled/views/assignments/CreateGroupView'
'jquery'
'helpers/jquery.simulate'
'helpers/fakeENV'
], (_, Backbone, AssignmentGroupCollection, AssignmentGroup, Assignment, CreateGroupView, $) ->
group = ->
new AssignmentGroup
assignments: [new Assignment, new Assignment]
assignmentGroups = ->
@groups = new AssignmentGroupCollection([group(), group()])
createView = ->
view = new CreateGroupView
assignmentGroups: assignmentGroups()
assignmentGroup: @groups.first()
module 'CreateGroupView'
test 'it should only allow positive numbers for drop rules', ->
view = createView()
data =
rules:
drop_lowest: "tree"
drop_highest: -1
errors = view.validateFormData(data)
ok errors
equal _.keys(errors).length, 2
test 'it should only allow less than the number of assignments for drop rules', ->
view = createView()
assignments = view.assignmentGroup.get('assignments')
data =
rules:
drop_highest: 5
errors = view.validateFormData(data)
ok errors
equal _.keys(errors).length, 1

View File

@ -148,6 +148,7 @@ describe "assignment groups" do
course_with_teacher_logged_in(:active_all => true)
@assignment_group = @course.assignment_groups.create!(:name => "Test Group")
@course.assignments.create(:name => "test", :assignment_group => @assignment_group)
get "/courses/#{@course.id}/assignments"
wait_for_ajaximations