Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FlowRegistrationPresenter to work with both i18n & ERB #2138

Merged
merged 1 commit into from
Nov 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/presenters/flow_registration_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ def indexable_content
text = @flow.questions.inject([start_node.body]) { |acc, node|
pres = QuestionPresenter.new(@i18n_prefix, node, nil, helpers: [MethodMissingHelper])
acc.concat(NODE_PRESENTER_METHODS.map { |method|
pres.send(method)
begin
pres.send(method)
rescue I18n::MissingInterpolationArgument
# We can't do much about this, so we ignore these text nodes
nil
end
})
}.compact.join(" ").gsub(/(?:<[^>]+>|\s)+/, " ")
)
Expand Down
2 changes: 0 additions & 2 deletions test/fixtures/smart_answer_flows/flow-sample.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ def define
satisfies_need 4242
content_id "f26e566e-2557-4921-b944-9373c32255f1"

use_erb_templates_for_questions

multiple_choice :hotter_or_colder? do
option :hotter
option :colder
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
en-GB:
flow:
flow-sample:
hotter_or_colder?:
title: QUESTION_1_TITLE
body: QUESTION_1_BODY %{i_dont_exist}
hint: QUESTION_1_HINT
frozen?:
title: QUESTION_2_TITLE
body: QUESTION_2_BODY
hint: QUESTION_2_HINT
11 changes: 11 additions & 0 deletions test/fixtures/smart_answer_flows/locales/en/flow-sample.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
en-GB:
flow:
flow-sample:
hotter_or_colder?:
title: QUESTION_1_TITLE
body: QUESTION_1_BODY
hint: QUESTION_1_HINT
frozen?:
title: QUESTION_2_TITLE
body: QUESTION_2_BODY <a href="/">LINK TEXT</a> &rarr;
hint: QUESTION_2_HINT
26 changes: 17 additions & 9 deletions test/unit/flow_registration_presenter_test.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
require_relative "../test_helper"
require_relative "../helpers/fixture_flows_helper"
require_relative "../helpers/i18n_test_helper"

require File.expand_path('../../fixtures/smart_answer_flows/flow-sample', __FILE__)

class FlowRegistrationPresenterTest < ActiveSupport::TestCase
include FixtureFlowsHelper
include I18nTestHelper

def setup
setup_fixture_flows
example_translation_file =
File.expand_path('../../fixtures/smart_answer_flows/locales/en/flow-sample.yml', __FILE__)
use_additional_translation_file(example_translation_file)

load_path = fixture_file('smart_answer_flows')
SmartAnswer::FlowRegistry.instance.stubs(:load_path).returns(load_path)

@flow = SmartAnswer::FlowSampleFlow.build
@presenter = FlowRegistrationPresenter.new(@flow)
end

def teardown
teardown_fixture_flows
reset_translation_files
end

context "slug" do
Expand All @@ -29,7 +35,7 @@ def teardown
end

context "title" do
should "should use the title from the start node template" do
should "should use the title translation" do
assert_equal "FLOW_TITLE", @presenter.title
end
end
Expand All @@ -53,7 +59,7 @@ def teardown
end

context "description" do
should "use the meta_description from the start node template" do
should "use the meta.description translation" do
assert_equal "FLOW_DESCRIPTION", @presenter.description
end
end
Expand Down Expand Up @@ -105,12 +111,14 @@ def teardown
end

should "ignore any interpolation errors" do
@flow.multiple_choice(:question_with_interpolation)
interpolation_example_translation_file =
File.expand_path('../../fixtures/smart_answer_flows/locales/en/flow-sample-interpolation.yml', __FILE__)
reset_translation_files
use_additional_translation_file(interpolation_example_translation_file)
@content = @presenter.indexable_content
assert_match %r{FLOW_BODY}, @content
assert_match %r{QUESTION_1_BODY}, @content
assert_no_match %r{QUESTION_1_BODY}, @content
assert_match %r{QUESTION_2_BODY}, @content
assert_match %r{QUESTION_WITH_INTERPOLATION_BODY}, @content
end
end

Expand Down
124 changes: 124 additions & 0 deletions test/unit/flow_registration_presenter_with_erb_renderer_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
require_relative "../test_helper"
require_relative "../helpers/fixture_flows_helper"

require File.expand_path('../../fixtures/smart_answer_flows/flow-sample', __FILE__)

class FlowRegistrationPresenterWithErbRendererTest < ActiveSupport::TestCase
include FixtureFlowsHelper

def setup
setup_fixture_flows
@flow = SmartAnswer::FlowSampleFlow.new
@flow.use_erb_templates_for_questions
@flow.define
@presenter = FlowRegistrationPresenter.new(@flow)
end

def teardown
teardown_fixture_flows
end

context "slug" do
should "use the flow name" do
assert_equal "flow-sample", @presenter.slug
end
end

context "content_id" do
should "use the flow content_id" do
assert_equal "f26e566e-2557-4921-b944-9373c32255f1", @presenter.content_id
end
end

context "title" do
should "should use the title from the start node template" do
assert_equal "FLOW_TITLE", @presenter.title
end
end

context "need_id" do
should "use the flow's need_id" do
assert_equal 4242, @presenter.need_id
end
end

context "paths" do
should "generate and /flow.name.json" do
assert_equal ["/flow-sample.json"], @presenter.paths
end
end

context "prefixes" do
should "generate /flow.name" do
assert_equal ["/flow-sample"], @presenter.prefixes
end
end

context "description" do
should "use the meta_description from the start node template" do
assert_equal "FLOW_DESCRIPTION", @presenter.description
end
end

context "indexable_content" do
should "include all question node titles" do
@content = @presenter.indexable_content
assert_match %r{QUESTION_1_TITLE}, @content
assert_match %r{QUESTION_2_TITLE}, @content
end

should "not include any outcome node titles" do
@content = @presenter.indexable_content
assert_no_match %r{OUTCOME_1_TITLE}, @content
assert_no_match %r{OUTCOME_2_TITLE}, @content
assert_no_match %r{OUTCOME_3_TITLE}, @content
end

should "include the flow body and question node bodies" do
@content = @presenter.indexable_content
assert_match %r{FLOW_BODY}, @content
assert_match %r{QUESTION_1_BODY}, @content
assert_match %r{QUESTION_2_BODY}, @content
end

should "not include outcome node bodies" do
@content = @presenter.indexable_content
assert_no_match %r{OUTCOME_1_BODY}, @content
assert_no_match %r{OUTCOME_2_BODY}, @content
assert_no_match %r{OUTCOME_3_BODY}, @content
end

should "include all question hints" do
@content = @presenter.indexable_content
assert_match %r{QUESTION_1_HINT}, @content
assert_match %r{QUESTION_2_HINT}, @content
end

should "omit HTML" do
@content = @presenter.indexable_content
assert_no_match %r{<}, @content
assert_match %r{LINK TEXT}, @content
end

should "decode HTML entities" do
@content = @presenter.indexable_content
assert_no_match %r{&rarr;}, @content
assert_match %r{→}, @content
end

should "ignore any interpolation errors" do
@flow.multiple_choice(:question_with_interpolation)
@content = @presenter.indexable_content
assert_match %r{FLOW_BODY}, @content
assert_match %r{QUESTION_1_BODY}, @content
assert_match %r{QUESTION_2_BODY}, @content
assert_match %r{QUESTION_WITH_INTERPOLATION_BODY}, @content
end
end

context "state" do
should "always return live, because the FlowRegistry decides what to register" do
assert_equal 'live', @presenter.state
end
end
end