diff --git a/app/presenters/content_item/linkable.rb b/app/presenters/content_item/linkable.rb index 6766b63e6..945d1e829 100644 --- a/app/presenters/content_item/linkable.rb +++ b/app/presenters/content_item/linkable.rb @@ -3,7 +3,7 @@ module Linkable include ActionView::Helpers::UrlHelper def from - organisations_ordered_by_importance + links_group(%w{worldwide_organisations ministers speaker}) + organisations_ordered_by_importance + links_group(%w{worldwide_organisations people speaker}) end def part_of @@ -21,9 +21,9 @@ def part_of private def links(type) - expanded_links_from_content_item(type).map do |link| - link_for_type(type, link) - end + expanded_links_from_content_item(type) + .select { |link| link["base_path"] || type == "world_locations" } + .map { |link| link_for_type(type, link) } end def expanded_links_from_content_item(type) diff --git a/test/presenters/content_item/linkable_test.rb b/test/presenters/content_item/linkable_test.rb new file mode 100644 index 000000000..156166ff1 --- /dev/null +++ b/test/presenters/content_item/linkable_test.rb @@ -0,0 +1,62 @@ +require 'test_helper' + +class ContentItemLinkableTest < ActiveSupport::TestCase + class DummyContentItem + include ContentItem::Linkable + attr_accessor :content_item, :title + + def initialize + @content_item = { + "base_path" => "/a/base/path", + "links" => {}, + } + @title = "A Title" + end + end + + test "when people links have base_paths they are linked to" do + item = DummyContentItem.new + item.content_item["links"]["people"] = [ + { + "title" => "Winston Churchill", + "base_path" => "/government/people/winston-churchill", + } + ] + + expected_from_links = [ + %{Winston Churchill} + ] + assert_equal expected_from_links, item.from + end + + test "when people links don't have base_paths they are skipped" do + item = DummyContentItem.new + item.content_item["links"]["people"] = [ + { + "title" => "Winston Churchill", + "base_path" => nil, + } + ] + + expected_from_links = [] + assert_equal expected_from_links, item.from + end + + # World locations don't have links in the Publishing API payload + # This weird situation is explained here: + # - https://github.com/alphagov/government-frontend/pull/386 + test "when a world location is linked to" do + item = DummyContentItem.new + item.content_item["links"]["world_locations"] = [ + { + "title" => "Germany", + "base_path" => nil, + } + ] + + expected_from_links = [ + %{Germany} + ] + assert_equal expected_from_links, item.part_of + end +end diff --git a/test/presenters/content_item/shareable_test.rb b/test/presenters/content_item/shareable_test.rb index 596e76a20..f653fc2af 100644 --- a/test/presenters/content_item/shareable_test.rb +++ b/test/presenters/content_item/shareable_test.rb @@ -1,18 +1,18 @@ require 'test_helper' include ERB::Util -class DummyContentItem - include ContentItem::Shareable - attr_accessor :content_item, :title +class ContentItemShareableTest < ActiveSupport::TestCase + class DummyContentItem + include ContentItem::Shareable + attr_accessor :content_item, :title - def initialize - @content_item = {} - @content_item["base_path"] = "/a/base/path" - @title = "A Title" + def initialize + @content_item = {} + @content_item["base_path"] = "/a/base/path" + @title = "A Title" + end end -end -class ContentItemShareableTest < ActiveSupport::TestCase def expected_path url_encode(Plek.current.website_root + "/a/base/path") end diff --git a/test/presenters/publication_presenter_test.rb b/test/presenters/publication_presenter_test.rb index f59349c50..df24aee5e 100644 --- a/test/presenters/publication_presenter_test.rb +++ b/test/presenters/publication_presenter_test.rb @@ -35,8 +35,8 @@ def schema_name assert presented.historically_political? end - test '#from presents ministers' do - minister = schema_item["links"]["ministers"][0] + test '#from presents people' do + minister = schema_item["links"]["people"][0] assert presented_item.from.include?("#{minister['title']}") end