Skip to content

Commit

Permalink
test: Add specs for proposal_published_event
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentinchampenois committed Nov 7, 2024
1 parent 2fbbf92 commit 899f214
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 24 deletions.
91 changes: 67 additions & 24 deletions spec/events/decidim/proposals/proposal_published_event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,77 @@

require "spec_helper"

module Decidim::Proposals
describe "ProposalPublishedEvent", type: :job do
include MailerHelpers

let(:organization) { create(:organization) }
let(:participatory_process) { create(:participatory_process, organization: organization) }
let(:component) { create(:component, manifest_name: :proposals, participatory_space: participatory_process) }
let!(:proposal_state) { create(:proposal_state, component: component) }
let(:proposal) { create(:proposal, component: component, proposal_state: proposal_state, title: { en: "Test proposition notification" }) }
let(:user) { create(:user, :admin, organization: organization, notifications_sending_frequency: "daily") }

before do
clear_emails
end
module Decidim
module Proposals
describe ProposalPublishedEvent do
let(:resource) { create :extended_proposal }
let(:participatory_process) { create :participatory_process, organization: organization }
let(:proposal_component) { create(:extended_proposal_component, participatory_space: participatory_process) }
let(:resource_title) { decidim_sanitize_translated(resource.title) }
let(:event_name) { "decidim.events.proposals.proposal_published" }

include_context "when a simple event"

it_behaves_like "a simple event"

describe "resource_text" do
it "returns the proposal body" do
expect(subject.resource_text).to eq(resource.body)
end
end

describe "email_subject" do
context "when resource title contains apostrophes" do
it "is generated correctly" do
expect(subject.email_subject).to eq("New proposal \"#{resource_title}\" by @#{author.nickname}")
end
end

it "is generated correctly" do
expect(subject.email_subject).to eq("New proposal \"#{resource_title}\" by @#{author.nickname}")
end
end

context "when proposal is published" do
subject { ProposalPublishedEvent.new(resource: proposal, event_name: "decidim.proposals.proposal_published", user: proposal.creator_identity) }
it "sends a notification email with the correct content" do
describe "email_intro" do
it "is generated correctly" do
expect(subject.email_intro)
.to eq("#{author.name} @#{author.nickname}, who you are following, has published a new proposal called \"#{resource_title}\". Check it out and contribute:")
end
end

expect(last_email).not_to be_nil
expect(last_email_body).not_to include("translation missing")
expect(last_email_body).to include("Test proposition notification")
describe "email_outro" do
it "is generated correctly" do
expect(subject.email_outro)
.to eq("You have received this notification because you are following @#{author.nickname}. You can stop receiving notifications following the previous link.")
end
end

it "sends a notification with the correct content" do
notification = Decidim::Notification.last
expect(notification).not_to be_nil
describe "notification_title" do
it "is generated correctly" do
expect(subject.notification_title)
.to include("The <a href=\"#{resource_path}\">#{resource_title}</a> proposal was published by ")

expect(subject.notification_title)
.to include("<a href=\"/profiles/#{author.nickname}\">#{author.name} @#{author.nickname}</a>.")
end
end

describe "translated notifications" do
let(:en_body) { "A nice proposal" }
let(:body) { { en: en_body, machine_translations: { ca: "Une belle idee" } } }
let(:resource) do
create :extended_proposal,
component: proposal_component,
title: { en: "A nice proposal", machine_translations: { ca: "Une belle idee" } },
body: body
end

let(:en_version) { subject.resource_text["en"] }
let(:machine_translated) { subject.resource_text["machine_translations"]["ca"] }
let(:translatable) { true }

it_behaves_like "a translated event"
end
end
end
end
end
131 changes: 131 additions & 0 deletions spec/shared/translated_event_examples.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# frozen_string_literal: true

shared_examples_for "a translated event" do
context "when it is not machine machine translated" do
let(:organization) { create(:organization, enable_machine_translations: false, machine_translation_display_priority: "original") }

it "does not perform translation" do
expect(subject.perform_translation?).to eq(false)
end

it "does not have a missing translation" do
expect(subject.translation_missing?).to eq(false)
end

it "does have content available in multiple languages" do
expect(subject.content_in_same_language?).to eq(false)
end

it "does return the original language" do
expect(subject.safe_resource_text).to eq(en_version)
end

it "does not offer an alternate translation" do
expect(subject.safe_resource_translated_text).to eq(en_version)
end
end

context "when is machine machine translated" do
let(:user) { create :user, organization: organization, locale: "ca" }

around do |example|
I18n.with_locale(user.locale) { example.run }
end

context "when priority is original" do
let(:organization) { create(:organization, enable_machine_translations: true, machine_translation_display_priority: "original") }

it "does perform translation" do
expect(subject.perform_translation?).to eq(translatable)
end

it "does not have a missing translation" do
expect(subject.translation_missing?).to eq(false)
end

it "does have content available in multiple languages" do
expect(subject.content_in_same_language?).to eq(false)
end

it "does return the original language" do
expect(subject.safe_resource_text).to eq(en_version)
end

it "does not offer an alternate translation" do
expect(subject.safe_resource_translated_text).to eq(machine_translated)
end

context "when translation is not available" do
let(:body) { { en: en_body } }

it "does perform translation" do
expect(subject.perform_translation?).to eq(translatable)
end

it "does have a missing translation" do
expect(subject.translation_missing?).to eq(translatable)
end

it "does have content available in multiple languages" do
expect(subject.content_in_same_language?).to eq(false)
end

it "does return the original language" do
expect(subject.safe_resource_text).to eq(en_version)
end

it "does not offer an alternate translation" do
expect(subject.safe_resource_translated_text).to eq(en_version)
end
end
end

context "when priority is translation" do
let(:organization) { create(:organization, enable_machine_translations: true, machine_translation_display_priority: "translation") }

it "does perform translation" do
expect(subject.perform_translation?).to eq(translatable)
end

it "does not have a missing translation" do
expect(subject.translation_missing?).to eq(false)
end

it "does have content available in multiple languages" do
expect(subject.content_in_same_language?).to eq(false)
end

it "does return the original language" do
expect(subject.safe_resource_text).to eq(en_version)
end

it "does not offer an alternate translation" do
expect(subject.safe_resource_translated_text).to eq(machine_translated)
end

context "when translation is not available" do
let(:body) { { en: en_body } }

it "does perform translation" do
expect(subject.perform_translation?).to eq(translatable)
end

it "does have a missing translation" do
expect(subject.translation_missing?).to eq(translatable)
end

it "does have content available in multiple languages" do
expect(subject.content_in_same_language?).to eq(false)
end

it "does return the original language" do
expect(subject.safe_resource_text).to eq(en_version)
end

it "does not offer an alternate translation" do
expect(subject.safe_resource_translated_text).to eq(en_version)
end
end
end
end
end

0 comments on commit 899f214

Please sign in to comment.