diff --git a/app/presenters/supergroups/news_and_communications.rb b/app/presenters/supergroups/news_and_communications.rb index 6a3b35295..cb517e1ae 100644 --- a/app/presenters/supergroups/news_and_communications.rb +++ b/app/presenters/supergroups/news_and_communications.rb @@ -2,59 +2,47 @@ module Supergroups class NewsAndCommunications < Supergroup attr_reader :content + PLACEHOLDER_IMAGE = "#{Plek.current.asset_root}/government/assets/placeholder.jpg".freeze + def initialize(current_path, taxon_ids, filters) super(current_path, taxon_ids, filters, MostRecentContent) end def tagged_content - return unless @content.any? - { - documents: documents, - promoted_content: promoted_content, - } + format_document_data(@content) end private - def documents - items = @content.drop(promoted_content_count) - format_document_data(items) - end - - def promoted_content - items = @content.take(promoted_content_count) - content = format_document_data(items, data_category: "ImageCardClicked") - - content.each do |document| - document_image = news_item_photo(document[:link][:path]) - document[:image] = { - url: document_image["url"], - alt: document_image["alt_text"], - context: document_image["context"] + def format_document_data(documents) + # Start with_index at 1 to help align analytics + documents.each.with_index(1).map do |document, index| + data = { + link: { + text: document["title"], + path: document["link"], + data_attributes: data_attributes(document["link"], document["title"], index, "ImageCardClicked") + }, + metadata: { + document_type: document_type(document), + public_updated_at: updated_date(document) + }, + image: { + url: image_url(document), + context: context(document) + } } - end - content + data + end end - def promoted_content_count - 3 + def image_url(document) + document["image_url"] || PLACEHOLDER_IMAGE end - def news_item_photo(base_path) - default_news_image = { - "url" => "https://assets.publishing.service.gov.uk/government/assets/placeholder.jpg", - "alt_text" => "" - } - - news_item = ::Services.content_store.content_item(base_path).to_h - - image = news_item["details"]["image"] || default_news_image - date = Date.parse(news_item["public_updated_at"]).strftime("%d %B %Y") - document_type = news_item["document_type"].humanize - image["context"] = "#{document_type} - #{date}" - - image + def context(document) + "#{document_type(document)} - #{updated_date(document).strftime('%e %B %Y')}" end end end diff --git a/app/presenters/supergroups/services.rb b/app/presenters/supergroups/services.rb index 3f6b5d670..1eed005c2 100644 --- a/app/presenters/supergroups/services.rb +++ b/app/presenters/supergroups/services.rb @@ -7,48 +7,7 @@ def initialize(current_path, taxon_ids, filters) end def tagged_content - return unless @content.any? - { - documents: documents, - promoted_content: promoted_content, - } - end - - private - - def documents - items = @content.drop(promoted_content_count) - format_document_data(items) - end - - def promoted_content - items = @content.take(promoted_content_count) - format_document_data(items, "HighlightBoxClicked") - end - - def promoted_content_count - 3 - end - - def format_document_data(documents, data_category = "DocumentListClicked") - documents.each.with_index(1)&.map do |document, index| - data = { - link: { - text: document["title"], - path: document["link"], - data_attributes: { - track_category: "Services" + data_category, - track_action: index, - track_label: document["link"], - track_options: { - dimension29: document["title"], - } - } - } - } - - data - end + format_document_data(@content, data_category: "HighlightBoxClicked") end end end diff --git a/app/presenters/supergroups/supergroup.rb b/app/presenters/supergroups/supergroup.rb index 14e88678e..0f783fa95 100644 --- a/app/presenters/supergroups/supergroup.rb +++ b/app/presenters/supergroups/supergroup.rb @@ -14,6 +14,7 @@ def tagged_content private def format_document_data(documents, data_category: nil, include_timestamp: true) + # Start with_index at 1 to help align analytics documents.each.with_index(1).map do |document, index| data = { link: { @@ -22,16 +23,12 @@ def format_document_data(documents, data_category: nil, include_timestamp: true) data_attributes: data_attributes(document["link"], document["title"], index, data_category) }, metadata: { - document_type: document["content_store_document_type"].humanize + document_type: document_type(document) } } if include_timestamp && document["public_timestamp"] - data[:metadata][:public_updated_at] = Date.parse(document["public_timestamp"]) - end - - if data_category.present? - data[:link][:data_attributes][:track_category] = data_module_label + data_category + data[:metadata][:public_updated_at] = updated_date(document) end data @@ -49,6 +46,14 @@ def data_attributes(base_path, link_text, index, data_category) } end + def document_type(document) + document["content_store_document_type"].humanize + end + + def updated_date(document) + Date.parse(document["public_timestamp"]) + end + def data_module_label self.class.name.demodulize.camelize(:lower) end diff --git a/app/services/most_recent_content.rb b/app/services/most_recent_content.rb index 1363b67a3..a79c0fac5 100644 --- a/app/services/most_recent_content.rb +++ b/app/services/most_recent_content.rb @@ -20,17 +20,12 @@ def fetch def search_response params = { - start: 0, - count: number_of_links, - fields: %w(title - link - description - content_store_document_type - public_timestamp - organisations), - filter_part_of_taxonomy_tree: @content_ids, - order: '-public_timestamp', - reject_link: current_path, + start: 0, + count: number_of_links, + fields: RummagerFields::TAXON_SEARCH_FIELDS, + filter_part_of_taxonomy_tree: @content_ids, + order: '-public_timestamp', + reject_link: current_path, } params[:filter_content_purpose_supergroup] = @filters[:filter_content_purpose_supergroup] if @filters[:filter_content_purpose_supergroup].present? params[:filter_content_purpose_subgroup] = @filters[:filter_content_purpose_subgroup] if @filters[:filter_content_purpose_subgroup].present? diff --git a/app/services/rummager_fields.rb b/app/services/rummager_fields.rb index 97fa8651d..f8969821c 100644 --- a/app/services/rummager_fields.rb +++ b/app/services/rummager_fields.rb @@ -1,10 +1,11 @@ module RummagerFields - TAXON_SEARCH_FIELDS = %w(title - link + TAXON_SEARCH_FIELDS = %w(content_store_document_type description - content_store_document_type + image_url + link + organisations public_timestamp - organisations).freeze + title).freeze FEED_SEARCH_FIELDS = %w(title link diff --git a/app/views/shared/_taxonomy_navigation.html.erb b/app/views/shared/_taxonomy_navigation.html.erb index 78f071e94..1117e6967 100644 --- a/app/views/shared/_taxonomy_navigation.html.erb +++ b/app/views/shared/_taxonomy_navigation.html.erb @@ -3,7 +3,7 @@
<%= render "govuk_publishing_components/components/heading", - text: 'Topics', + text: t('supergroups.topics'), heading_level: 2, font_size: 19 %>
<% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 36739e5d6..74f91695d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -50,6 +50,8 @@ en: policy_and_engagement: "Policy and engagement" transparency: "Transparency" see_more_supergroups: "See more %{supergroup}" + topics: "Topics" + collections: "Collections" language_names: ar: Arabic de: German diff --git a/test/integration/content_pages_navigation_test.rb b/test/integration/content_pages_navigation_test.rb index 89da452f6..ddfda7361 100644 --- a/test/integration/content_pages_navigation_test.rb +++ b/test/integration/content_pages_navigation_test.rb @@ -93,7 +93,7 @@ def setup assert page.has_css?('h3', text: "Services") assert page.has_css?('.gem-c-highlight-boxes__title', text: 'Free school meals form') - assert page.has_css?('.gem-c-highlight-boxes__title[data-track-category="ServicesHighlightBoxClicked"]', text: 'Free school meals form') + assert page.has_css?('.gem-c-highlight-boxes__title[data-track-category="servicesHighlightBoxClicked"]', text: 'Free school meals form') assert page.has_css?('.gem-c-highlight-boxes__title[data-track-action="1"]', text: 'Free school meals form') assert page.has_css?('.gem-c-highlight-boxes__title[data-track-label="/government/publications/meals"]', text: 'Free school meals form') end @@ -203,11 +203,6 @@ def setup setup_and_visit_content_item_with_taxons('guide', taxons) - assert page.has_css?('h3', text: "News and communications") - assert page.has_css?('.gem-c-image-card__title', text: 'Free school meals form') - assert page.has_css?('.gem-c-image-card__title-link[data-track-category="newsAndCommunicationsImageCardClicked"]', text: 'Free school meals form') - assert page.has_css?('.gem-c-image-card__title-link[data-track-action="1"]', text: 'Free school meals form') - assert page.has_css?('.gem-c-image-card__title-link[data-track-label="/government/publications/meals"]', text: 'Free school meals form') assert_has_news_and_communications_section end diff --git a/test/presenters/supergroups/news_and_communications_test.rb b/test/presenters/supergroups/news_and_communications_test.rb index 060d49839..7193af1c2 100644 --- a/test/presenters/supergroups/news_and_communications_test.rb +++ b/test/presenters/supergroups/news_and_communications_test.rb @@ -5,7 +5,7 @@ class NewsAndCommunicationsTest < ActiveSupport::TestCase test "finds no results if taxon ids is a blank array" do news_and_comms = Supergroups::NewsAndCommunications.new("/a-random-path", [], {}) - assert_nil news_and_comms.tagged_content + assert_equal [], news_and_comms.tagged_content end test "finds no results if there are taxon ids but no results" do @@ -13,40 +13,24 @@ class NewsAndCommunicationsTest < ActiveSupport::TestCase stub_most_recent_content("/a-random-path", taxon_content_ids, 0, "news_and_communications") news_and_comms = Supergroups::NewsAndCommunications.new("/a-random-path", taxon_content_ids, {}) - assert_nil news_and_comms.tagged_content + assert_equal [], news_and_comms.tagged_content end - test "finds 2 featured items and 0 normal items with 2 results" do + test "Presents 2 items with 2 results" do taxon_content_ids = ['any-old-taxon', 'some-other-taxon-id'] stub_most_recent_content("/a-random-path", taxon_content_ids, 2, "news_and_communications") - stub_content_store_items(2) news_and_comms = Supergroups::NewsAndCommunications.new("/a-random-path", taxon_content_ids, {}) - assert_equal 0, news_and_comms.tagged_content[:documents].count - assert_equal 2, news_and_comms.tagged_content[:promoted_content].count - end - - test "finds 3 promoted items and 2 normal items if there are enough results" do - taxon_content_ids = ['any-old-taxon', 'some-other-taxon-id'] - stub_most_recent_content("/a-random-path", taxon_content_ids, 5, "news_and_communications") - stub_content_store_items(3) - - news_and_comms = Supergroups::NewsAndCommunications.new("/a-random-path", taxon_content_ids, {}) - - assert_equal 2, news_and_comms.tagged_content[:documents].count - assert_equal 3, news_and_comms.tagged_content[:promoted_content].count + assert_equal 2, news_and_comms.tagged_content.count end test "promoted content includes placeholder images if the content doesn't have one" do - placeholder_image = "https://assets.publishing.service.gov.uk/government/assets/placeholder.jpg" - taxon_content_ids = ['any-old-taxon'] - stub_most_recent_content("/a-random-path", taxon_content_ids, 1, "news_and_communications") - stub_content_store_items(1) - - news_and_comms = Supergroups::NewsAndCommunications.new("/a-random-path", taxon_content_ids, {}) + placeholder_image = "http://static.test.gov.uk/government/assets/placeholder.jpg" + stub_rummager_document_without_image_url - news_item = news_and_comms.tagged_content[:promoted_content].first + news_and_comms = Supergroups::NewsAndCommunications.new("/a-random-path", ['any-old-taxon'], {}) + news_item = news_and_comms.tagged_content.first assert_equal news_item[:image][:url], placeholder_image end @@ -55,26 +39,9 @@ class NewsAndCommunicationsTest < ActiveSupport::TestCase taxon_content_ids = ['any-old-taxon'] stub_most_recent_content("/a-random-path", taxon_content_ids, 1, "news_and_communications") - content = content_item_for_base_path("/content-item-0").merge( - "details": { - "image": { - "url": "an/image/path", - "alt_text": "some alt text" - } - } - ) - content_store_has_item("/content-item-0", content) - news_and_comms = Supergroups::NewsAndCommunications.new("/a-random-path", taxon_content_ids, {}) + news_item = news_and_comms.tagged_content.first - news_item = news_and_comms.tagged_content[:promoted_content].first - - assert_equal news_item[:image][:url], "an/image/path" - end - - def stub_content_store_items(count) - count.times.map do |i| - content_store_has_item("/content-item-#{i}") - end + assert_equal news_item[:image][:url], "https://assets.testing.gov.uk/awesome-pic.jpg" end end diff --git a/test/presenters/supergroups/services_test.rb b/test/presenters/supergroups/services_test.rb index 897a708cc..ffe48b048 100644 --- a/test/presenters/supergroups/services_test.rb +++ b/test/presenters/supergroups/services_test.rb @@ -5,7 +5,7 @@ class ServicesTest < ActiveSupport::TestCase test "services returns no results if taxon ids is a blank array" do services = Supergroups::Services.new("/a-random-path", [], {}) - assert_nil services.tagged_content + assert_equal [], services.tagged_content end test "services returns no results if there are taxon ids but no results" do @@ -13,7 +13,7 @@ class ServicesTest < ActiveSupport::TestCase stub_most_popular_content("/a-random-path", taxon_content_ids, 0, "services") services = Supergroups::Services.new("/a-random-path", taxon_content_ids, {}) - assert_nil services.tagged_content + assert_equal [], services.tagged_content end test "tagged_content returns hash with with 2 featured items and 0 normal items with 2 results" do @@ -23,18 +23,16 @@ class ServicesTest < ActiveSupport::TestCase services = Supergroups::Services.new("/a-random-path", taxon_content_ids, {}) - assert_equal 0, services.tagged_content[:documents].count - assert_equal 2, services.tagged_content[:promoted_content].count + assert_equal 2, services.tagged_content.count end test "tagged_content returns hash with with 3 featured items and 2 normal items if there are enough results" do taxon_content_ids = ['any-old-taxon', 'some-other-taxon-id'] - stub_most_popular_content("/a-random-path", taxon_content_ids, 5, "services") + stub_most_popular_content("/a-random-path", taxon_content_ids, 3, "services") services = Supergroups::Services.new("/a-random-path", taxon_content_ids, {}) - assert_equal 2, services.tagged_content[:documents].count - assert_equal 3, services.tagged_content[:promoted_content].count + assert_equal 3, services.tagged_content.count end end diff --git a/test/support/content_pages_nav_test_helper.rb b/test/support/content_pages_nav_test_helper.rb index 47e306903..6c357d783 100644 --- a/test/support/content_pages_nav_test_helper.rb +++ b/test/support/content_pages_nav_test_helper.rb @@ -7,6 +7,8 @@ def stub_rummager "content_store_document_type": "form", "link": "/government/publications/meals", "title": "Free school meals form", + "public_timestamp": "2018-01-02T03:04:05Z", + "image_url": "https://assets.gov.uk/pertinent-image.gif", ) end @@ -117,7 +119,7 @@ def supergroups def assert_has_services_section assert page.has_css?('h3', text: "Services") assert page.has_css?('.gem-c-highlight-boxes__title', text: 'Free school meals form') - assert page.has_css?('.gem-c-highlight-boxes__title[data-track-category="ServicesHighlightBoxClicked"]', text: 'Free school meals form') + assert page.has_css?('.gem-c-highlight-boxes__title[data-track-category="servicesHighlightBoxClicked"]', text: 'Free school meals form') assert page.has_css?('.gem-c-highlight-boxes__title[data-track-action="1"]', text: 'Free school meals form') assert page.has_css?('.gem-c-highlight-boxes__title[data-track-label="/government/publications/meals"]', text: 'Free school meals form') end @@ -152,9 +154,5 @@ def assert_has_news_and_communications_section assert page.has_css?('.gem-c-image-card__title-link[data-track-category="newsAndCommunicationsImageCardClicked"]', text: 'Free school meals form') assert page.has_css?('.gem-c-image-card__title-link[data-track-action="1"]', text: 'Free school meals form') assert page.has_css?('.gem-c-image-card__title-link[data-track-label="/government/publications/meals"]', text: 'Free school meals form') - - assert page.has_css?('.gem-c-document-list__item a[data-track-category="newsAndCommunicationsDocumentListClicked"]', text: 'Free school meals form') - assert page.has_css?('.gem-c-document-list__item a[data-track-action="1"]', text: 'Free school meals form') - assert page.has_css?('.gem-c-document-list__item a[data-track-label="/government/publications/meals"]', text: 'Free school meals form') end end diff --git a/test/support/rummager_helpers.rb b/test/support/rummager_helpers.rb index 5239c91fa..773057d7a 100644 --- a/test/support/rummager_helpers.rb +++ b/test/support/rummager_helpers.rb @@ -9,13 +9,26 @@ def stub_most_popular_content(reject_link, taxon_content_ids, content_link_count stub_for_taxon_and_supergroup(reject_link, taxon_content_ids, results, supergroup, "-popularity") end + def stub_rummager_document_without_image_url + result = rummager_document_for_supergroup_section("doc-with-no-url", "news_story", false) + Services.rummager.stubs(:search) + .returns( + "results" => [result], + "start" => 0, + "total" => 1 + ) + end + def stub_for_taxon_and_supergroup(reject_link, content_ids, results, filter_content_purpose_supergroup, order_by) - fields = %w(title - link - description - content_store_document_type - public_timestamp - organisations) + fields = %w( + content_store_document_type + description + image_url + link + organisations + public_timestamp + title + ) params = { start: 0, @@ -42,15 +55,17 @@ def generate_search_results(count, supergroup) end end - def rummager_document_for_supergroup_section(slug, content_store_document_type) - { - 'title' => slug.titleize.humanize.to_s, - 'link' => "/#{slug}", - 'description' => 'A discription about tagged content', - 'content_store_document_type' => content_store_document_type, - 'public_timestamp' => 1.hour.ago.iso8601, - 'organisations' => [{ 'title' => "#{content_store_document_type.humanize} Organisation Title" }] + def rummager_document_for_supergroup_section(slug, content_store_document_type, with_image_url = true) + document = { + 'title' => slug.titleize.humanize.to_s, + 'link' => "/#{slug}", + 'description' => 'A description about tagged content', + 'content_store_document_type' => content_store_document_type, + 'public_timestamp' => 1.hour.ago.iso8601, + 'organisations' => [{ 'title' => "#{content_store_document_type.humanize} Organisation Title" }] } + document['image_url'] = 'https://assets.testing.gov.uk/awesome-pic.jpg' if with_image_url + document end def assert_includes_params(expected_params)