Skip to content

Commit

Permalink
fix: Change ProposalPublishedEvent to SimpleEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentinchampenois committed Nov 7, 2024
1 parent df4935d commit cb8772a
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 12 deletions.
14 changes: 2 additions & 12 deletions app/events/decidim/proposals/proposal_published_event.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
# app/events/decidim/proposals/proposal_published_event.rb
module Decidim
module Proposals
class ProposalPublishedEvent < Decidim::Events::BaseEvent
include Decidim::Events::NotificationEvent

def self.model_name
ActiveModel::Name.new(self, nil, I18n.t('decidim.proposals.notifications.proposal_published.subject'))
end

class ProposalPublishedEvent < Decidim::Events::SimpleEvent
def notification_title
I18n.t("decidim.proposals.notifications.proposal_published.title", proposal_title: resource_title)
end
Expand All @@ -23,11 +17,7 @@ def resource_title
end

def resource_path
Decidim::Proposals::Engine.routes.url_helpers.proposal_path(
resource,
component_id: resource.component.id,
initiative_slug: resource.component.participatory_space.slug
)
""
end
end
end
Expand Down
202 changes: 202 additions & 0 deletions spec/shared/simple_event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
# frozen_string_literal: true

require "spec_helper"

shared_context "when a simple event" do
include Decidim::SanitizeHelper
subject { event_instance }

let(:event_instance) do
described_class.new(
resource: resource,
event_name: event_name,
user: user,
user_role: user_role,
extra: extra
)
end

let(:organization) do
if resource.respond_to?(:organization)
resource.organization
else
create :organization
end
end
let(:user) { create :user, organization: organization }
let(:user_role) { :follower }
let(:extra) { {} }
let(:resource_path) { resource_locator(resource).path }
let(:resource_url) { resource_locator(resource).url }
let(:resource_title) { decidim_sanitize_translated(resource.title) }
# to be used when resource is a component resource, not a participatory space, in which case should be overriden
let(:participatory_space) { resource.participatory_space }
let(:participatory_space_title) { decidim_sanitize_translated(participatory_space.title) }
let(:participatory_space_path) { Decidim::ResourceLocatorPresenter.new(participatory_space).path }
let(:participatory_space_url) { Decidim::ResourceLocatorPresenter.new(participatory_space).url }
let(:author) do
if resource.respond_to?(:creator_author)
resource.creator_author
else
resource.author
end
end
let(:author_presenter) { Decidim::UserPresenter.new(author) }
let(:author_name) { decidim_html_escape author.name }
let(:author_path) { author_presenter&.profile_path.to_s }
let(:author_nickname) { author_presenter&.nickname.to_s }
let(:i18n_scope) { event_name }
end

shared_examples_for "a simple event" do |skip_space_checks|
describe "types" do
subject { described_class }

it "supports notifications" do
expect(subject.types).to include :notification
end

it "supports emails" do
expect(subject.types).to include :email
end
end

describe "email_subject" do
it "is generated correctly" do
expect(subject.email_subject).to be_kind_of(String)
expect(subject.email_subject).not_to include("translation missing")
expect(subject.email_subject).not_to include("script")
end
end

describe "email_intro" do
it "is generated correctly" do
expect(subject.email_intro).to be_kind_of(String)
expect(subject.email_intro).not_to include("translation missing")
end
end

describe "email_outro" do
it "is generated correctly" do
expect(subject.email_outro).to be_kind_of(String)
expect(subject.email_outro).not_to include("translation missing")
end
end

describe "email_greeting" do
it "is generated correctly" do
expect(subject.email_greeting).to be_kind_of(String)
expect(subject.email_greeting).not_to include("translation missing")
end
end

describe "safe_resource_text" do
it "is generated correctly" do
expect(subject.safe_resource_text).to be_kind_of(String)
expect(subject.safe_resource_text).to be_html_safe
end
end

describe "notification_title" do
it "is generated correctly" do
expect(subject.notification_title).to be_kind_of(String)
expect(subject.notification_title).not_to include("translation missing")
expect(subject.notification_title).not_to include("script")
end
end

describe "resource_path" do
it "is generated correctly" do
expect(subject.resource_path).to be_kind_of(String)
end
end

describe "resource_url" do
it "is generated correctly" do
expect(subject.resource_url).to be_kind_of(String)
expect(subject.resource_url).to start_with("http")
end
end

describe "resource_title" do
it "responds to the method" do
expect(subject).to respond_to(:resource_title)
end
end

unless skip_space_checks
describe "participatory_space_url" do
it "is generated correctly" do
expect(subject.participatory_space_url).to be_kind_of(String)
expect(subject.participatory_space_url).to start_with("http")
end
end

describe "participatory_space_title" do
it "is generated correctly" do
expect(translated(participatory_space.title)).to include("script")
end
end
end

describe "i18n_options" do
subject { super().i18n_options }

it { is_expected.to include(resource_path: satisfy(&:present?)) }
it { is_expected.to include(resource_title: satisfy(&:present?)) }
it { is_expected.to include(resource_url: start_with("http")) }

it "includes the i18n scope" do
if event_instance.event_has_roles?
expect(subject).to include(scope: "#{i18n_scope}.#{user_role}")
else
expect(subject).to include(scope: i18n_scope)
end
end

unless skip_space_checks
it { is_expected.to include(participatory_space_title: satisfy(&:present?)) }
it { is_expected.to include(participatory_space_url: start_with("http")) }
end
end
end

shared_examples_for "a simple event email" do
describe "email_subject" do
it "is generated correctly" do
expect(subject.email_subject).to eq(email_subject)
end

# it "is html safe" do
# # pending "Enable after #12547 is merged"
# expect(subject.email_subject).not_to include("script")
# end
end

describe "email_intro" do
it "is generated correctly" do
expect(subject.email_intro).to eq(email_intro)
end
end

describe "email_outro" do
it "is generated correctly" do
expect(subject.email_outro).to eq(email_outro)
end
end
end

shared_examples_for "a simple event notification" do
describe "notification_title" do
it "is generated correctly" do
expect(subject.notification_title)
.to eq(notification_title)
end
#
# it "is html safe" do
# pp subject.notification_title
# pending "Enable after #12547 is merged"
# expect(subject.notification_title).not_to include("script")
# end
end
end

0 comments on commit cb8772a

Please sign in to comment.