FIX: Ensure that topic timers belonging to trashed topics are destroyed.

FIX: Ensure that topic timers belonging to trashed topics are destroyed.

diff --git a/app/models/topic_timer.rb b/app/models/topic_timer.rb
index 20e3bb6..27d4a94 100644
--- a/app/models/topic_timer.rb
+++ b/app/models/topic_timer.rb
@@ -126,8 +126,7 @@ class TopicTimer < ActiveRecord::Base
   end
 
   def schedule_auto_open_job(time)
-    return unless topic
-    topic.update_status('closed', true, user) if !topic.closed
+    topic.update_status('closed', true, user) if !topic&.closed
 
     Jobs.enqueue_at(time, :toggle_topic_closed,
       topic_timer_id: id,
@@ -136,8 +135,7 @@ class TopicTimer < ActiveRecord::Base
   end
 
   def schedule_auto_close_job(time)
-    return unless topic
-    topic.update_status('closed', false, user) if topic.closed
+    topic.update_status('closed', false, user) if topic&.closed
 
     Jobs.enqueue_at(time, :toggle_topic_closed,
       topic_timer_id: id,
diff --git a/spec/models/topic_timer_spec.rb b/spec/models/topic_timer_spec.rb
index 6c19bd6..40da0ec 100644
--- a/spec/models/topic_timer_spec.rb
+++ b/spec/models/topic_timer_spec.rb
@@ -232,27 +232,34 @@ RSpec.describe TopicTimer, type: :model do
 
       Fabricate(:topic_timer, execute_at: Time.zone.now + 1.hour)
 
-      Fabricate(:topic_timer,
+      trashed_topic_timer = Fabricate(:topic_timer,
         execute_at: Time.zone.now - 1.hour,
         created_at: Time.zone.now - 2.hour
-      ).topic.trash!
+      )
+
+      trashed_topic_timer.topic.trash!
 
       # creating topic timers already enqueues jobs
       # let's delete them to test ensure_consistency!
       Sidekiq::Worker.clear_all
 
       expect { described_class.ensure_consistency! }
-        .to change { Jobs::ToggleTopicClosed.jobs.count }.by(2)
-
-      job_args = Jobs::ToggleTopicClosed.jobs.first["args"].first
-
-      expect(job_args["topic_timer_id"]).to eq(close_topic_timer.id)
-      expect(job_args["state"]).to eq(true)
-
-      job_args = Jobs::ToggleTopicClosed.jobs.last["args"].first
-
-      expect(job_args["topic_timer_id"]).to eq(open_topic_timer.id)
-      expect(job_args["state"]).to eq(false)
+        .to change { Jobs::ToggleTopicClosed.jobs.count }.by(3)
+
+      expect(job_enqueued?(job: :toggle_topic_closed, args: {
+        topic_timer_id: close_topic_timer.id,
+        state: true
+      })).to eq(true)
+
+      expect(job_enqueued?(job: :toggle_topic_closed, args: {
+        topic_timer_id: open_topic_timer.id,
+        state: false
+      })).to eq(true)
+
+      expect(job_enqueued?(job: :toggle_topic_closed, args: {
+        topic_timer_id: trashed_topic_timer.id,
+        state: true
+      })).to eq(true)
     end
 
     it "should enqueue remind me jobs that have been missed" do

GitHub sha: 5ff6c103