|
217 | 217 | end |
218 | 218 | end |
219 | 219 | end |
| 220 | + |
| 221 | + describe "on 'post_edited' with category change" do |
| 222 | + fab!(:category_b, :category) |
| 223 | + fab!(:group_b, :group) |
| 224 | + |
| 225 | + before do |
| 226 | + category_b.custom_fields[CategoryExperts::CATEGORY_EXPERT_GROUP_IDS] = "#{group_b.id}" |
| 227 | + category_b.save! |
| 228 | + end |
| 229 | + |
| 230 | + context "when topic is moved from category with experts to category without experts" do |
| 231 | + it "clears all expert custom fields from posts and topic" do |
| 232 | + expect( |
| 233 | + original_topic.custom_fields[CategoryExperts::TOPIC_EXPERT_POST_GROUP_NAMES], |
| 234 | + ).to eq(group.name) |
| 235 | + expect(original_topic.custom_fields[CategoryExperts::TOPIC_FIRST_EXPERT_POST_ID]).to eq( |
| 236 | + second_post.post_number, |
| 237 | + ) |
| 238 | + expect(second_post.custom_fields[CategoryExperts::POST_APPROVED_GROUP_NAME]).to eq( |
| 239 | + group.name, |
| 240 | + ) |
| 241 | + |
| 242 | + category_without_experts = Fabricate(:category) |
| 243 | + |
| 244 | + PostRevisor.new(original_topic.first_post).revise!( |
| 245 | + admin, |
| 246 | + category_id: category_without_experts.id, |
| 247 | + ) |
| 248 | + |
| 249 | + original_topic.reload |
| 250 | + second_post.reload |
| 251 | + |
| 252 | + expect( |
| 253 | + original_topic.custom_fields[CategoryExperts::TOPIC_EXPERT_POST_GROUP_NAMES], |
| 254 | + ).to be_blank |
| 255 | + expect( |
| 256 | + original_topic.custom_fields[CategoryExperts::TOPIC_FIRST_EXPERT_POST_ID], |
| 257 | + ).to be_blank |
| 258 | + expect(second_post.custom_fields[CategoryExperts::POST_APPROVED_GROUP_NAME]).to be_blank |
| 259 | + end |
| 260 | + end |
| 261 | + |
| 262 | + context "when topic is moved from one category with experts to another with different experts" do |
| 263 | + it "updates expert custom fields appropriately when user is not expert in new category" do |
| 264 | + expect( |
| 265 | + original_topic.custom_fields[CategoryExperts::TOPIC_EXPERT_POST_GROUP_NAMES], |
| 266 | + ).to eq(group.name) |
| 267 | + expect(second_post.custom_fields[CategoryExperts::POST_APPROVED_GROUP_NAME]).to eq( |
| 268 | + group.name, |
| 269 | + ) |
| 270 | + |
| 271 | + PostRevisor.new(original_topic.first_post).revise!(admin, category_id: category_b.id) |
| 272 | + |
| 273 | + original_topic.reload |
| 274 | + second_post.reload |
| 275 | + |
| 276 | + expect( |
| 277 | + original_topic.custom_fields[CategoryExperts::TOPIC_EXPERT_POST_GROUP_NAMES], |
| 278 | + ).to be_blank |
| 279 | + expect(second_post.custom_fields[CategoryExperts::POST_APPROVED_GROUP_NAME]).to be_blank |
| 280 | + end |
| 281 | + |
| 282 | + it "updates expert group name when user is expert in both categories" do |
| 283 | + group_b.add(expert) |
| 284 | + |
| 285 | + expect( |
| 286 | + original_topic.custom_fields[CategoryExperts::TOPIC_EXPERT_POST_GROUP_NAMES], |
| 287 | + ).to eq(group.name) |
| 288 | + expect(second_post.custom_fields[CategoryExperts::POST_APPROVED_GROUP_NAME]).to eq( |
| 289 | + group.name, |
| 290 | + ) |
| 291 | + |
| 292 | + PostRevisor.new(original_topic.first_post).revise!(admin, category_id: category_b.id) |
| 293 | + |
| 294 | + original_topic.reload |
| 295 | + second_post.reload |
| 296 | + |
| 297 | + expect( |
| 298 | + original_topic.custom_fields[CategoryExperts::TOPIC_EXPERT_POST_GROUP_NAMES], |
| 299 | + ).to eq(group_b.name) |
| 300 | + expect(second_post.custom_fields[CategoryExperts::POST_APPROVED_GROUP_NAME]).to eq( |
| 301 | + group_b.name, |
| 302 | + ) |
| 303 | + end |
| 304 | + |
| 305 | + context "with multiple expert posts from different users" do |
| 306 | + fab!(:expert_2) { Fabricate(:user, refresh_auto_groups: true) } |
| 307 | + fab!(:third_post) { Fabricate(:post, topic: original_topic, user: expert_2) } |
| 308 | + |
| 309 | + before do |
| 310 | + group.add(expert_2) |
| 311 | + CategoryExperts::PostHandler.new(post: third_post, user: expert_2).mark_post_as_approved |
| 312 | + end |
| 313 | + |
| 314 | + it "only keeps expert status for users who are experts in new category" do |
| 315 | + group_b.add(expert) |
| 316 | + |
| 317 | + expect( |
| 318 | + original_topic.custom_fields[CategoryExperts::TOPIC_EXPERT_POST_GROUP_NAMES], |
| 319 | + ).to eq(group.name) |
| 320 | + expect(second_post.custom_fields[CategoryExperts::POST_APPROVED_GROUP_NAME]).to eq( |
| 321 | + group.name, |
| 322 | + ) |
| 323 | + expect(third_post.custom_fields[CategoryExperts::POST_APPROVED_GROUP_NAME]).to eq( |
| 324 | + group.name, |
| 325 | + ) |
| 326 | + |
| 327 | + PostRevisor.new(original_topic.first_post).revise!(admin, category_id: category_b.id) |
| 328 | + |
| 329 | + original_topic.reload |
| 330 | + second_post.reload |
| 331 | + third_post.reload |
| 332 | + |
| 333 | + expect( |
| 334 | + original_topic.custom_fields[CategoryExperts::TOPIC_EXPERT_POST_GROUP_NAMES], |
| 335 | + ).to eq(group_b.name) |
| 336 | + expect(second_post.custom_fields[CategoryExperts::POST_APPROVED_GROUP_NAME]).to eq( |
| 337 | + group_b.name, |
| 338 | + ) |
| 339 | + expect(third_post.custom_fields[CategoryExperts::POST_APPROVED_GROUP_NAME]).to be_blank |
| 340 | + end |
| 341 | + end |
| 342 | + end |
| 343 | + |
| 344 | + context "when topic has auto-tag" do |
| 345 | + fab!(:auto_tag_b, :tag) |
| 346 | + |
| 347 | + before do |
| 348 | + SiteSetting.tagging_enabled = true |
| 349 | + category_b.custom_fields[CategoryExperts::CATEGORY_EXPERT_AUTO_TAG] = auto_tag_b.name |
| 350 | + category_b.save! |
| 351 | + end |
| 352 | + |
| 353 | + it "removes old auto-tag and adds new auto-tag when category changes" do |
| 354 | + expect(original_topic.tags.map(&:name)).to include(auto_tag.name) |
| 355 | + expect(original_topic.tags.map(&:name)).not_to include(auto_tag_b.name) |
| 356 | + |
| 357 | + PostRevisor.new(original_topic.first_post).revise!(admin, category_id: category_b.id) |
| 358 | + |
| 359 | + original_topic.reload |
| 360 | + |
| 361 | + expect(original_topic.tags.map(&:name)).not_to include(auto_tag.name) |
| 362 | + expect(original_topic.tags.map(&:name)).to include(auto_tag_b.name) |
| 363 | + end |
| 364 | + end |
| 365 | + end |
220 | 366 | end |
221 | 367 | end |
0 commit comments