From 426b7b43b62a4641d00893b6e6be706e41bd623b Mon Sep 17 00:00:00 2001 From: Ryan Norton Date: Thu, 27 Jun 2019 11:38:58 -0600 Subject: [PATCH] fix submitting comments on attempt 0 in a2 when submitted comments for attempt 0 (the case where we have no submissions) the attempt was reading as nil in the validation and failing to create the comment. Test Plan: * as a teacher, create an assignment * as a student in this assignment who has not yet submitted, navigate to the Comments tab * create a new comment on the assignment * it should successfully create the comment * comments for attempt 0 and attempt 1 should display together * on an assignment with one or more submissions, create a comment * it should successfully create the comment fixes COMMS-2174 Change-Id: If216a541aebf7db2ea7b6942a02d194113cd8e41 Reviewed-on: https://gerrit.instructure.com/199377 Reviewed-by: Steven Burnett QA-Review: Michelle Simmons Tested-by: Jenkins Product-Review: Ryan Norton --- app/models/submission_comment.rb | 2 +- spec/models/submission_comment_spec.rb | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/models/submission_comment.rb b/app/models/submission_comment.rb index 03c5c9cced6..c786a7b77e1 100644 --- a/app/models/submission_comment.rb +++ b/app/models/submission_comment.rb @@ -49,7 +49,7 @@ class SubmissionComment < ActiveRecord::Base validates_length_of :comment, :minimum => 1, :allow_nil => true, :allow_blank => true validates_each :attempt do |record, attr, value| next if value.nil? - if record.submission.attempt.nil? || value > record.submission.attempt + if value > (record.submission.attempt || 0) record.errors.add(attr, 'attempt must not be larger than number of submission attempts') end end diff --git a/spec/models/submission_comment_spec.rb b/spec/models/submission_comment_spec.rb index c8c24fccfdc..c982925c879 100644 --- a/spec/models/submission_comment_spec.rb +++ b/spec/models/submission_comment_spec.rb @@ -648,6 +648,20 @@ This text has a http://www.google.com link in it... @comment4 = @submission.submission_comments.create!(valid_attributes.merge(attempt: nil)) end + context 'when the submission attempt is nil' do + before(:once) do + @submission.update!(attempt: nil) + end + + it 'raises an error if the submission_comment attempt is greater than 0' do + expect { @submission.submission_comments.create!(valid_attributes.merge(attempt: 1)) }.to raise_error(ActiveRecord::RecordInvalid) + end + + it 'does not raise an error if the submission_comment attempt is equal to 0' do + expect { @submission.submission_comments.create!(valid_attributes.merge(attempt: 0)) }.not_to raise_error + end + end + it 'can limit comments to the specific attempt' do expect(@submission.submission_comments.where(attempt: 1)).to eq [@comment1] end @@ -660,14 +674,14 @@ This text has a http://www.google.com link in it... expect(@submission.submission_comments.where(attempt: nil)).to eq [@comment4] end - it 'cannot be present? if submisssion#attempt is nil' do + it 'cannot be present? if submission#attempt is nil' do @submission.update_column(:attempt, nil) # bypass infer_values callback @comment1.reload @comment1.attempt = 2 expect(@comment1).not_to be_valid end - it 'cannot be larger then submisssion#attempt' do + it 'cannot be larger then submission#attempt' do @comment1.attempt = @submission.attempt + 1 expect(@comment1).not_to be_valid end