From ac99947979620f496633ba79c129a7c071c502f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Thu, 6 Mar 2014 12:44:52 +0100 Subject: [PATCH] BUGFIX: topic specs were dependent on Time.now which would fail when DST was around --- app/models/topic.rb | 6 +++--- spec/models/topic_spec.rb | 15 ++++++++------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/app/models/topic.rb b/app/models/topic.rb index 70d9f154712..d3bfe1136ce 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -658,11 +658,11 @@ class Topic < ActiveRecord::Base # * A timestamp with timezone in JSON format. (e.g., "2013-11-26T21:00:00.000Z") # * nil, to prevent the topic from automatically closing. def set_auto_close(arg, by_user=nil) - if arg.is_a?(String) and matches = /^([\d]{1,2}):([\d]{1,2})$/.match(arg.strip) + if arg.is_a?(String) && matches = /^([\d]{1,2}):([\d]{1,2})$/.match(arg.strip) now = Time.zone.now self.auto_close_at = Time.zone.local(now.year, now.month, now.day, matches[1].to_i, matches[2].to_i) self.auto_close_at += 1.day if self.auto_close_at < now - elsif arg.is_a?(String) and arg.include?('-') and timestamp = Time.zone.parse(arg) + elsif arg.is_a?(String) && arg.include?('-') && timestamp = Time.zone.parse(arg) self.auto_close_at = timestamp self.errors.add(:auto_close_at, :invalid) if timestamp < Time.zone.now else @@ -672,7 +672,7 @@ class Topic < ActiveRecord::Base unless self.auto_close_at.nil? self.auto_close_started_at ||= Time.zone.now - if by_user and by_user.staff? + if by_user && by_user.staff? self.auto_close_user = by_user else self.auto_close_user ||= (self.user.staff? ? self.user : Discourse.system_user) diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index 01637dc49bd..1595b993b99 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -1089,46 +1089,47 @@ describe Topic do let(:topic) { Fabricate.build(:topic) } let(:closing_topic) { Fabricate.build(:topic, auto_close_hours: 5) } let(:admin) { Fabricate.build(:user, id: 123) } + let(:now) { Time.zone.local(2013,11,20,8,0) } before { Discourse.stubs(:system_user).returns(admin) } it 'can take a number of hours as an integer' do - Timecop.freeze(Time.zone.now) do + Timecop.freeze(now) do topic.set_auto_close(72, admin) expect(topic.auto_close_at).to eq(3.days.from_now) end end it 'can take a number of hours as a string' do - Timecop.freeze(Time.zone.now) do + Timecop.freeze(now) do topic.set_auto_close('18', admin) expect(topic.auto_close_at).to eq(18.hours.from_now) end end it "can take a time later in the day" do - Timecop.freeze(Time.zone.local(2013,11,20,8,0)) do + Timecop.freeze(now) do topic.set_auto_close('13:00', admin) topic.auto_close_at.should == Time.zone.local(2013,11,20,13,0) end end it "can take a time for the next day" do - Timecop.freeze(Time.zone.local(2013,11,20,8,0)) do + Timecop.freeze(now) do topic.set_auto_close('5:00', admin) topic.auto_close_at.should == Time.zone.local(2013,11,21,5,0) end end it "can take a timestamp for a future time" do - Timecop.freeze(Time.zone.local(2013,11,20,8,0)) do + Timecop.freeze(now) do topic.set_auto_close('2013-11-22 5:00', admin) topic.auto_close_at.should == Time.zone.local(2013,11,22,5,0) end end it "sets a validation error when given a timestamp in the past" do - Timecop.freeze(Time.zone.local(2013,11,20,8,0)) do + Timecop.freeze(now) do topic.set_auto_close('2013-11-19 5:00', admin) topic.auto_close_at.should == Time.zone.local(2013,11,19,5,0) topic.errors[:auto_close_at].should be_present @@ -1136,7 +1137,7 @@ describe Topic do end it "can take a timestamp with timezone" do - Timecop.freeze(Time.utc(2013,11,20,12,0)) do + Timecop.freeze(now) do topic.set_auto_close('2013-11-25T01:35:00-08:00', admin) topic.auto_close_at.should == Time.utc(2013,11,25,9,35) end