FIX: don't demote users to TL2 when default trust level is 3

FIX: don’t demote users to TL2 when default trust level is 3

Within 24 hours of signing up, new users were losing their default trust level of 3. With this fix, demotions from trust level 3 won’t happen when the “default trust level” setting is 3 or 4.

diff --git a/app/jobs/scheduled/tl3_promotions.rb b/app/jobs/scheduled/tl3_promotions.rb
index 848f409..7dcce77 100644
--- a/app/jobs/scheduled/tl3_promotions.rb
+++ b/app/jobs/scheduled/tl3_promotions.rb
@@ -6,22 +6,24 @@ module Jobs
     daily at: 4.hours
 
     def execute(args)
-      # Demotions
-      demoted_user_ids = []
-      User.real
-        .joins("LEFT JOIN (SELECT gu.user_id, MAX(g.grant_trust_level) AS group_granted_trust_level FROM groups g, group_users gu WHERE g.id = gu.group_id GROUP BY gu.user_id) tl ON users.id = tl.user_id")
-        .where(
-          trust_level: TrustLevel[3],
-          manual_locked_trust_level: nil
-        )
-        .where("group_granted_trust_level IS NULL OR group_granted_trust_level < ?", TrustLevel[3])
-        .find_each do |u|
-        # Don't demote too soon after being promoted
-        next if u.on_tl3_grace_period?
+      if SiteSetting.default_trust_level < 3
+        # Demotions
+        demoted_user_ids = []
+        User.real
+          .joins("LEFT JOIN (SELECT gu.user_id, MAX(g.grant_trust_level) AS group_granted_trust_level FROM groups g, group_users gu WHERE g.id = gu.group_id GROUP BY gu.user_id) tl ON users.id = tl.user_id")
+          .where(
+            trust_level: TrustLevel[3],
+            manual_locked_trust_level: nil
+          )
+          .where("group_granted_trust_level IS NULL OR group_granted_trust_level < ?", TrustLevel[3])
+          .find_each do |u|
+          # Don't demote too soon after being promoted
+          next if u.on_tl3_grace_period?
 
-        if Promotion.tl3_lost?(u)
-          demoted_user_ids << u.id
-          Promotion.new(u).change_trust_level!(TrustLevel[2])
+          if Promotion.tl3_lost?(u)
+            demoted_user_ids << u.id
+            Promotion.new(u).change_trust_level!(TrustLevel[2])
+          end
         end
       end
 
diff --git a/app/models/trust_level3_requirements.rb b/app/models/trust_level3_requirements.rb
index b01da6c..bf511a0 100644
--- a/app/models/trust_level3_requirements.rb
+++ b/app/models/trust_level3_requirements.rb
@@ -67,6 +67,7 @@ class TrustLevel3Requirements
 
   def requirements_lost?
     return false if trust_level_locked
+    return false if SiteSetting.default_trust_level > 2
 
     @user.suspended? ||
       @user.silenced? ||
diff --git a/spec/jobs/tl3_promotions_spec.rb b/spec/jobs/tl3_promotions_spec.rb
index abfa8eb..f76007d 100644
--- a/spec/jobs/tl3_promotions_spec.rb
+++ b/spec/jobs/tl3_promotions_spec.rb
@@ -135,5 +135,14 @@ describe Jobs::Tl3Promotions do
       run_job
       expect(user.reload.trust_level).to eq(TrustLevel[3])
     end
+
+    it "doesn't demote if default trust level for all users is 3" do
+      SiteSetting.default_trust_level = 3
+      user = Fabricate(:user, trust_level: TrustLevel[3], created_at: 1.year.ago)
+      expect(user).to_not be_on_tl3_grace_period
+      TrustLevel3Requirements.any_instance.stubs(:requirements_met?).returns(false)
+      run_job
+      expect(user.reload.trust_level).to eq(TrustLevel[3])
+    end
   end
 end
diff --git a/spec/models/trust_level3_requirements_spec.rb b/spec/models/trust_level3_requirements_spec.rb
index ef36c18..a28f54d 100644
--- a/spec/models/trust_level3_requirements_spec.rb
+++ b/spec/models/trust_level3_requirements_spec.rb
@@ -586,6 +586,14 @@ describe TrustLevel3Requirements do
       user.silenced_till = 4.weeks.from_now
       expect(tl3_requirements.requirements_lost?).to eq(true)
     end
+
+    [3, 4].each do |default_tl|
+      it "is not lost if default_trust_level is #{default_tl}" do
+        SiteSetting.default_trust_level = default_tl
+        tl3_requirements.stubs(:days_visited).returns(1)
+        expect(tl3_requirements.requirements_lost?).to eq(false)
+      end
+    end
   end
 
 end

GitHub sha: 074509fd