Aadd 'secondary_emails' field in users export

Aadd ‘secondary_emails’ field in users export

FIX: escape_comma wasn’t working in CSV exports FIX: group_names field wasn’t properly serialized

diff --git a/app/jobs/regular/export_csv_file.rb b/app/jobs/regular/export_csv_file.rb
index 4026dc7..f5e29ef 100644
--- a/app/jobs/regular/export_csv_file.rb
+++ b/app/jobs/regular/export_csv_file.rb
@@ -11,7 +11,7 @@ module Jobs
 
     HEADER_ATTRS_FOR ||= HashWithIndifferentAccess.new(
       user_archive: ['topic_title', 'category', 'sub_category', 'is_pm', 'post', 'like_count', 'reply_count', 'url', 'created_at'],
-      user_list: ['id', 'name', 'username', 'email', 'title', 'created_at', 'last_seen_at', 'last_posted_at', 'last_emailed_at', 'trust_level', 'approved', 'suspended_at', 'suspended_till', 'silenced_till', 'active', 'admin', 'moderator', 'ip_address', 'staged'],
+      user_list: ['id', 'name', 'username', 'email', 'title', 'created_at', 'last_seen_at', 'last_posted_at', 'last_emailed_at', 'trust_level', 'approved', 'suspended_at', 'suspended_till', 'silenced_till', 'active', 'admin', 'moderator', 'ip_address', 'staged', 'secondary_emails'],
       user_stats: ['topics_entered', 'posts_read_count', 'time_read', 'topic_count', 'post_count', 'likes_given', 'likes_received'],
       user_profile: ['location', 'website', 'views'],
       user_sso: ['external_id', 'external_email', 'external_username', 'external_name', 'external_avatar_url'],
@@ -114,7 +114,7 @@ module Jobs
 
       if SiteSetting.enable_sso
         # SSO enabled
-        User.where(condition).includes(:user_profile, :user_stat, :single_sign_on_record, :groups).find_each do |user|
+        User.where(condition).includes(:user_profile, :user_stat, :user_emails, :single_sign_on_record, :groups).find_each do |user|
           user_info_array = get_base_user_array(user)
           user_info_array = add_single_sign_on(user, user_info_array)
           user_info_array = add_custom_fields(user, user_info_array, user_field_ids)
@@ -123,7 +123,7 @@ module Jobs
         end
       else
         # SSO disabled
-        User.where(condition).includes(:user_profile, :user_stat, :groups).find_each do |user|
+        User.where(condition).includes(:user_profile, :user_stat, :user_emails, :groups).find_each do |user|
           user_info_array = get_base_user_array(user)
           user_info_array = add_custom_fields(user, user_info_array, user_field_ids)
           user_info_array = add_group_names(user, user_info_array)
@@ -212,16 +212,42 @@ module Jobs
     private
 
     def escape_comma(string)
-      if string && string =~ /,/
-        return "#{string}"
-      else
-        return string
-      end
+      string&.include?(",") ? %Q|"#{string}"| : string
     end
 
     def get_base_user_array(user)
-      user_array = []
-      user_array.push(user.id, escape_comma(user.name), user.username, user.email, escape_comma(user.title), user.created_at, user.last_seen_at, user.last_posted_at, user.last_emailed_at, user.trust_level, user.approved, user.suspended_at, user.suspended_till, user.silenced_till, user.active, user.admin, user.moderator, user.ip_address, user.staged, user.user_stat.topics_entered, user.user_stat.posts_read_count, user.user_stat.time_read, user.user_stat.topic_count, user.user_stat.post_count, user.user_stat.likes_given, user.user_stat.likes_received, escape_comma(user.user_profile.location), user.user_profile.website, user.user_profile.views)
+      [
+        user.id,
+        escape_comma(user.name),
+        user.username,
+        user.email,
+        escape_comma(user.title),
+        user.created_at,
+        user.last_seen_at,
+        user.last_posted_at,
+        user.last_emailed_at,
+        user.trust_level,
+        user.approved,
+        user.suspended_at,
+        user.suspended_till,
+        user.silenced_till,
+        user.active,
+        user.admin,
+        user.moderator,
+        user.ip_address,
+        user.staged,
+        user.secondary_emails.join(";"),
+        user.user_stat.topics_entered,
+        user.user_stat.posts_read_count,
+        user.user_stat.time_read,
+        user.user_stat.topic_count,
+        user.user_stat.post_count,
+        user.user_stat.likes_given,
+        user.user_stat.likes_received,
+        escape_comma(user.user_profile.location),
+        user.user_profile.website,
+        user.user_profile.views,
+      ]
     end
 
     def add_single_sign_on(user, user_info_array)
@@ -243,11 +269,8 @@ module Jobs
     end
 
     def add_group_names(user, user_info_array)
-      group_names = user.groups.each_with_object("") do |group, names|
-        names << "#{group.name};"
-      end
-      user_info_array << group_names[0..-2] unless group_names.blank?
-      group_names = nil
+      group_names = user.groups.map { |g| g.name }.join(";")
+      user_info_array << escape_comma(group_names) if group_names.present?
       user_info_array
     end
 
diff --git a/spec/jobs/export_csv_file_spec.rb b/spec/jobs/export_csv_file_spec.rb
index 1852556..53015aa 100644
--- a/spec/jobs/export_csv_file_spec.rb
+++ b/spec/jobs/export_csv_file_spec.rb
@@ -17,9 +17,10 @@ describe Jobs::ExportCsvFile do
           "system_messages.csv_export_succeeded.subject_template",
           export_title: "User Archive"
         ))
+
         expect(user.topics_allowed.last.first_post.raw).to include("user-archive-john_doe-")
       ensure
-        user.uploads.find_each { |upload| upload.destroy! }
+        user.uploads.each(&:destroy!)
       end
     end
   end
@@ -28,10 +29,10 @@ describe Jobs::ExportCsvFile do
     %w{
       id name username email title created_at last_seen_at last_posted_at
       last_emailed_at trust_level approved suspended_at suspended_till blocked
-      active admin moderator ip_address staged topics_entered posts_read_count
-      time_read topic_count post_count likes_given likes_received location
-      website views external_id external_email external_username external_name
-      external_avatar_url
+      active admin moderator ip_address staged secondary_emails topics_entered
+      posts_read_count time_read topic_count post_count likes_given
+      likes_received location website views external_id external_email
+      external_username external_name external_avatar_url
     }
   }
 
@@ -41,16 +42,27 @@ describe Jobs::ExportCsvFile do
     Hash[*user_list_header.zip(row).flatten]
   end
 
+  it "experts secondary emails" do
+    user = Fabricate(:user)
+    Fabricate(:secondary_email, user: user, primary: false)
+
+    secondary_emails = user.secondary_emails.join(";")
+
+    user = to_hash(user_list_export.find { |u| u[0].to_i == user.id })
+
+    expect(user["secondary_emails"]).to eq(secondary_emails)
+  end
+
   it 'exports sso data' do
     SiteSetting.sso_url = "https://www.example.com/sso"
     SiteSetting.enable_sso = true
     user = Fabricate(:user)
-    user.user_profile.update_column(:location, "La La Land")
+    user.user_profile.update_column(:location, "La,La Land")
     user.create_single_sign_on_record(external_id: "123", last_payload: "xxx", external_email: 'test@test.com')
 
     user = to_hash(user_list_export.find { |u| u[0].to_i == user.id })
 
-    expect(user["location"]).to eq("La La Land")
+    expect(user["location"]).to eq('"La,La Land"')
     expect(user["external_id"]).to eq("123")
     expect(user["external_email"]).to eq("test@test.com")
   end

GitHub sha: 326d892f

This commit has been mentioned on Discourse Meta. There might be relevant details there: