diff --git a/app/presenters/flow_registration_presenter.rb b/app/presenters/flow_registration_presenter.rb index d7a3a959af1..667cba2cd32 100644 --- a/app/presenters/flow_registration_presenter.rb +++ b/app/presenters/flow_registration_presenter.rb @@ -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)+/, " ") ) diff --git a/test/fixtures/smart_answer_flows/flow-sample.rb b/test/fixtures/smart_answer_flows/flow-sample.rb index 2d3de922391..f82410b9aae 100644 --- a/test/fixtures/smart_answer_flows/flow-sample.rb +++ b/test/fixtures/smart_answer_flows/flow-sample.rb @@ -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 diff --git a/test/fixtures/smart_answer_flows/locales/en/flow-sample-interpolation.yml b/test/fixtures/smart_answer_flows/locales/en/flow-sample-interpolation.yml new file mode 100644 index 00000000000..1596506c681 --- /dev/null +++ b/test/fixtures/smart_answer_flows/locales/en/flow-sample-interpolation.yml @@ -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 diff --git a/test/fixtures/smart_answer_flows/locales/en/flow-sample.yml b/test/fixtures/smart_answer_flows/locales/en/flow-sample.yml new file mode 100644 index 00000000000..735829dbb07 --- /dev/null +++ b/test/fixtures/smart_answer_flows/locales/en/flow-sample.yml @@ -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> → + hint: QUESTION_2_HINT diff --git a/test/unit/flow_registration_presenter_test.rb b/test/unit/flow_registration_presenter_test.rb index c3c62593d84..9f5f7e410b7 100644 --- a/test/unit/flow_registration_presenter_test.rb +++ b/test/unit/flow_registration_presenter_test.rb @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/test/unit/flow_registration_presenter_with_erb_renderer_test.rb b/test/unit/flow_registration_presenter_with_erb_renderer_test.rb new file mode 100644 index 00000000000..a759a8f6fc8 --- /dev/null +++ b/test/unit/flow_registration_presenter_with_erb_renderer_test.rb @@ -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{→}, @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