|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe Jobs::TranslateTranslatable do |
| 4 | + fab!(:post) { Fabricate(:post) } |
| 5 | + fab!(:topic) { Fabricate(:topic) } |
| 6 | + |
| 7 | + before do |
| 8 | + SiteSetting.translator_enabled = true |
| 9 | + SiteSetting.translator = "Google" |
| 10 | + SiteSetting.automatic_translation_target_languages = "es|fr" |
| 11 | + end |
| 12 | + |
| 13 | + describe "#execute" do |
| 14 | + it "does nothing when translator is disabled" do |
| 15 | + SiteSetting.translator_enabled = false |
| 16 | + expect(DiscourseTranslator::Google).not_to receive(:translate) |
| 17 | + |
| 18 | + subject.execute(type: "Post", translatable_id: post.id) |
| 19 | + end |
| 20 | + |
| 21 | + it "does nothing when target languages are empty" do |
| 22 | + SiteSetting.automatic_translation_target_languages = "" |
| 23 | + expect(DiscourseTranslator::Google).not_to receive(:translate) |
| 24 | + |
| 25 | + subject.execute(type: "Post", translatable_id: post.id) |
| 26 | + end |
| 27 | + |
| 28 | + it "translates posts to configured target languages" do |
| 29 | + expect(DiscourseTranslator::Google).to receive(:translate).with(post, :es) |
| 30 | + expect(DiscourseTranslator::Google).to receive(:translate).with(post, :fr) |
| 31 | + expect(MessageBus).to receive(:publish).with( |
| 32 | + "/topic/#{post.topic_id}", |
| 33 | + type: :revised, |
| 34 | + id: post.id, |
| 35 | + ) |
| 36 | + |
| 37 | + subject.execute(type: "Post", translatable_id: post.id) |
| 38 | + end |
| 39 | + |
| 40 | + it "translates topics to configured target languages" do |
| 41 | + expect(DiscourseTranslator::Google).to receive(:translate).with(topic, :es) |
| 42 | + expect(DiscourseTranslator::Google).to receive(:translate).with(topic, :fr) |
| 43 | + expect(MessageBus).to receive(:publish).with("/topic/#{topic.id}", type: :revised, id: 1) |
| 44 | + |
| 45 | + subject.execute(type: "Topic", translatable_id: topic.id) |
| 46 | + end |
| 47 | + |
| 48 | + it "does nothing when translatable is not found" do |
| 49 | + expect(DiscourseTranslator::Google).not_to receive(:translate) |
| 50 | + expect(MessageBus).not_to receive(:publish) |
| 51 | + |
| 52 | + subject.execute(type: "Post", translatable_id: -1) |
| 53 | + end |
| 54 | + end |
| 55 | +end |
0 commit comments