FEATURE: Check if selectable avatars exist before enabling them (#10032)

FEATURE: Check if selectable avatars exist before enabling them (#10032)

diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index eb5ae68..03d621d 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -2255,6 +2255,7 @@ en:
       max_username_length_exists: "You cannot set the maximum username length below the longest username (%{username})."
       max_username_length_range: "You cannot set the maximum below the minimum."
       invalid_hex_value: "Color values have to be 6-digit hexadecimal codes."
+      empty_selectable_avatars: "You must first upload at least two selectable avatars before enabling this setting."
       category_search_priority:
         very_low_weight_invalid: "You cannot set the weight to be greater than 'category_search_priority_low_weight'."
         low_weight_invalid: "You cannot set the weight to be greater or equal to 1 or smaller than 'category_search_priority_very_low_weight'."
diff --git a/config/site_settings.yml b/config/site_settings.yml
index 06549cd..e8cd6f7 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -1241,6 +1241,7 @@ files:
   selectable_avatars_enabled:
     default: false
     client: true
+    validator: "SelectableAvatarsEnabledValidator"
   selectable_avatars:
     default: ""
     type: uploaded_image_list
diff --git a/lib/validators/selectable_avatars_enabled_validator.rb b/lib/validators/selectable_avatars_enabled_validator.rb
new file mode 100644
index 0000000..065c5bc
--- /dev/null
+++ b/lib/validators/selectable_avatars_enabled_validator.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+class SelectableAvatarsEnabledValidator
+  def initialize(opts = {})
+    @opts = opts
+  end
+
+  def valid_value?(value)
+    value == "f" || SiteSetting.selectable_avatars.split("\n").size > 1
+  end
+
+  def error_message
+    I18n.t('site_settings.errors.empty_selectable_avatars')
+  end
+end
diff --git a/spec/components/validators/selectable_avatars_enabled_validator_spec.rb b/spec/components/validators/selectable_avatars_enabled_validator_spec.rb
new file mode 100644
index 0000000..2635e2e
--- /dev/null
+++ b/spec/components/validators/selectable_avatars_enabled_validator_spec.rb
@@ -0,0 +1,30 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+describe SelectableAvatarsEnabledValidator do
+  describe '#valid_value?' do
+    subject(:validator) { described_class.new }
+
+    it "returns true when disabling" do
+      SiteSetting.selectable_avatars = ""
+      expect(validator.valid_value?("f")).to eq(true)
+
+      SiteSetting.selectable_avatars = [Fabricate(:image_upload).url, Fabricate(:image_upload).url].join("\n")
+      expect(validator.valid_value?("f")).to eq(true)
+    end
+
+    it "returns true when there are at least two selectable avatars" do
+      SiteSetting.selectable_avatars = [Fabricate(:image_upload).url, Fabricate(:image_upload).url].join("\n")
+      expect(validator.valid_value?("t")).to eq(true)
+    end
+
+    it "returns false when selectable avatars is blank or has one avatar" do
+      SiteSetting.selectable_avatars = ""
+      expect(validator.valid_value?("t")).to eq(false)
+
+      SiteSetting.selectable_avatars = Fabricate(:image_upload).url
+      expect(validator.valid_value?("t")).to eq(false)
+    end
+  end
+end
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index cc11a35..791153b 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -2033,8 +2033,8 @@ describe User do
     it "sets a random avatar when selectable avatars is enabled" do
       avatar1 = Fabricate(:upload)
       avatar2 = Fabricate(:upload)
-      SiteSetting.selectable_avatars_enabled = true
       SiteSetting.selectable_avatars = [avatar1.url, avatar2.url].join("\n")
+      SiteSetting.selectable_avatars_enabled = true
 
       user = Fabricate(:user)
       expect(user.uploaded_avatar_id).not_to be(nil)
diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb
index add1d7a..b5a1342 100644
--- a/spec/requests/users_controller_spec.rb
+++ b/spec/requests/users_controller_spec.rb
@@ -2290,19 +2290,18 @@ describe UsersController do
 
       context 'selectable avatars is enabled' do
 
-        before { SiteSetting.selectable_avatars_enabled = true }
+        before do
+          SiteSetting.selectable_avatars = [avatar1.url, avatar2.url].join("\n")
+          SiteSetting.selectable_avatars_enabled = true
+        end
 
         it 'raises an error when selectable avatars is empty' do
+          SiteSetting.selectable_avatars = ""
           put "/u/#{user.username}/preferences/avatar/select.json", params: { url: url }
           expect(response.status).to eq(422)
         end
 
         context 'selectable avatars is properly setup' do
-
-          before do
-            SiteSetting.selectable_avatars = [avatar1.url, avatar2.url].join("\n")
-          end
-
           it 'raises an error when url is not in selectable avatars list' do
             put "/u/#{user.username}/preferences/avatar/select.json", params: { url: url }
             expect(response.status).to eq(422)

GitHub sha: 68f767a5

This commit appears in #10032 which was approved by ZogStriP. It was merged by nbianca.