slimmer 3.0.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ # 3.1.0
2
+
3
+ * 'Breadcrumb' trail is now populated from the artefact data. It adds the section.
4
+ subsection, and title.
5
+
1
6
  # 3.0.0
2
7
 
3
8
  Backwards-incompatible changes:
@@ -8,12 +8,24 @@ class Slimmer::Artefact
8
8
  end
9
9
 
10
10
  def tags_of_type(type)
11
- return [] unless @data.has_key?("tags")
12
- @data["tags"].select do |t|
11
+ return [] unless self.tags
12
+ self.tags.select do |t|
13
13
  t["details"]["type"] == type
14
14
  end
15
15
  end
16
16
 
17
+ def primary_section
18
+ tags_of_type("section").first
19
+ end
20
+
21
+ def primary_root_section
22
+ section = primary_section
23
+ while section and section["parent"]
24
+ section = section["parent"]
25
+ end
26
+ section
27
+ end
28
+
17
29
  def legacy_sources
18
30
  tags_of_type('legacy_source').map do |t|
19
31
  id_to_slug(t["id"])
@@ -21,14 +33,16 @@ class Slimmer::Artefact
21
33
  end
22
34
 
23
35
  def related_artefacts
24
- return [] unless @data.has_key?("related")
25
- @data["related"].map do |r|
36
+ return [] unless self.related
37
+ self.related.map do |r|
26
38
  self.class.new(r)
27
39
  end
28
40
  end
29
41
 
30
42
  def method_missing(name, *args)
31
- @data[name.to_s] || @data["details"][name.to_s]
43
+ value = @data[name.to_s]
44
+ value ||= @data["details"][name.to_s] if @data["details"]
45
+ value
32
46
  end
33
47
 
34
48
  private
@@ -31,5 +31,19 @@ module Slimmer
31
31
  artefact = artefact_input.dup
32
32
  headers[ARTEFACT_HEADER] = artefact.to_json
33
33
  end
34
+
35
+ def set_slimmer_dummy_artefact(details = {})
36
+ artefact = {}
37
+ artefact["title"] = details[:title] if details[:title]
38
+ if details[:section_name] and details[:section_link]
39
+ tag = {
40
+ "title" => details[:section_name],
41
+ "details" => {"type" => "section"},
42
+ "content_with_tag" => {"web_url" => details[:section_link]},
43
+ }
44
+ artefact["tags"] = [tag]
45
+ end
46
+ headers[ARTEFACT_HEADER] = artefact.to_json
47
+ end
34
48
  end
35
49
  end
@@ -8,23 +8,13 @@ module Slimmer
8
8
  end
9
9
 
10
10
  def filter(source, dest)
11
- return unless @artefact and @artefact["tags"]
12
- classes_to_use = LOGO_CLASSES & legacy_sources
11
+ return unless @artefact
12
+ classes_to_use = LOGO_CLASSES & @artefact.legacy_sources
13
13
  wrapper = dest.css('#wrapper')
14
14
  classes_to_use.each do |klass|
15
15
  wrapper.add_class(klass)
16
16
  end
17
17
  end
18
-
19
- def legacy_sources
20
- legacy_source_tags = @artefact["tags"].select do |tag|
21
- tag["details"]["type"] == "legacy_source"
22
- end
23
- legacy_sources = legacy_source_tags.map do |tag|
24
- tag["id"].split("/").last.chomp(".json")
25
- end
26
- legacy_sources
27
- end
28
18
  end
29
19
  end
30
20
  end
@@ -12,29 +12,11 @@ class Slimmer::Processors::RelatedItemsInserter
12
12
  end
13
13
  end
14
14
 
15
+ private
16
+
15
17
  def related_item_block
16
18
  artefact = @artefact
17
- root_primary_section = root_primary_section(artefact)
18
- related_block_template = @skin.template('related.raw')
19
- html = ERB.new(related_block_template).result(binding)
19
+ html = ERB.new(@skin.template('related.raw')).result(binding)
20
20
  Nokogiri::HTML.fragment(html)
21
21
  end
22
-
23
- # Duplicated in Frontend
24
- def root_primary_section(artefact)
25
- primary_section = artefact["tags"].detect do |tag|
26
- tag["details"]["type"] == "section"
27
- end
28
-
29
- if primary_section.nil?
30
- nil
31
- else
32
- if primary_section["parent"]
33
- primary_section["parent"]
34
- else
35
- primary_section
36
- end
37
- end
38
- end
39
-
40
22
  end
@@ -1,20 +1,37 @@
1
1
  module Slimmer::Processors
2
2
  class SectionInserter
3
+ def initialize(artefact)
4
+ @artefact = artefact
5
+ end
6
+
3
7
  def filter(src,dest)
4
- meta_name = dest.at_css('meta[name="x-section-name"]')
5
- meta_link = dest.at_css('meta[name="x-section-link"]')
6
- list = dest.at_css('nav[role=navigation] ol')
8
+ if @artefact and (list = dest.at_css('.header-context nav[role=navigation] ol'))
9
+ if (section = @artefact.primary_section)
10
+ append_tag(list, section["parent"]) if section["parent"]
11
+ append_tag(list, section)
12
+ end
13
+ append_text(list, @artefact.title) if @artefact.title and @artefact.title !~ /\A\s*\z/
14
+ end
15
+ end
7
16
 
8
- if meta_name && meta_link && list
9
- link_node = Nokogiri::XML::Node.new('a', dest)
10
- link_node['href'] = meta_link['content']
11
- link_node.content = meta_name['content']
17
+ private
12
18
 
13
- list_item = Nokogiri::XML::Node.new('li', dest)
14
- list_item.add_child(link_node)
19
+ def append_tag(list, tag)
20
+ link_node = Nokogiri::XML::Node.new('a', list)
21
+ link_node['href'] = tag["content_with_tag"]["web_url"]
22
+ link_node.content = tag["title"]
15
23
 
16
- list.add_child(list_item)
17
- end
24
+ list_item = Nokogiri::XML::Node.new('li', list)
25
+ list_item.add_child(link_node)
26
+
27
+ list.add_child(list_item)
28
+ end
29
+
30
+ def append_text(list, text)
31
+ list_item = Nokogiri::XML::Node.new('li', list)
32
+ list_item.content = text
33
+
34
+ list.add_child(list_item)
18
35
  end
19
36
  end
20
37
  end
data/lib/slimmer/skin.rb CHANGED
@@ -128,7 +128,7 @@ module Slimmer
128
128
  Processors::BodyInserter.new(options[:wrapper_id] || 'wrapper'),
129
129
  Processors::BodyClassCopier.new,
130
130
  Processors::HeaderContextInserter.new(),
131
- Processors::SectionInserter.new(),
131
+ Processors::SectionInserter.new(artefact),
132
132
  Processors::GoogleAnalyticsConfigurator.new(response),
133
133
  Processors::SearchPathSetter.new(response),
134
134
  Processors::RelatedItemsInserter.new(self, artefact),
@@ -149,10 +149,13 @@ module Slimmer
149
149
 
150
150
  def artefact_from_header(response)
151
151
  if response.headers.include?(Headers::ARTEFACT_HEADER)
152
- JSON.parse(response.headers[Headers::ARTEFACT_HEADER])
152
+ Artefact.new JSON.parse(response.headers[Headers::ARTEFACT_HEADER])
153
153
  else
154
154
  nil
155
155
  end
156
+ rescue JSON::ParserError => e
157
+ logger.error "Slimmer: Failed while parsing artefact header: #{[ e.message, e.backtrace ].flatten.join("\n")}"
158
+ nil
156
159
  end
157
160
  end
158
161
  end
@@ -19,12 +19,13 @@ module Slimmer::TestTemplate
19
19
  <script src="http://static.preview.alphagov.co.uk/static/jquery.tabs.js" defer></script>
20
20
  </head>
21
21
  <body>
22
- <nav role="navigation" class="header-context">
23
- <ol class="group">
24
- <li><a href="/">Home</a></li>
25
- <li><a href="/browse">All sections</a></li>
26
- </ol>
27
- </nav>
22
+ <div class="header-context">
23
+ <nav role="navigation">
24
+ <ol class="group">
25
+ <li><a href="/">Home</a></li>
26
+ </ol>
27
+ </nav>
28
+ </div>
28
29
 
29
30
  <div id="wrapper"></div>
30
31
  </body>
@@ -1,3 +1,3 @@
1
1
  module Slimmer
2
- VERSION = '3.0.0'
2
+ VERSION = '3.1.0'
3
3
  end
@@ -9,6 +9,66 @@ describe Slimmer::Artefact do
9
9
  assert_equal 'vat-rates', a.slug
10
10
  end
11
11
 
12
+ describe "Primary section" do
13
+ before do
14
+ @data = artefact_for_slug('something')
15
+ @tag1 = tag_for_slug("fooey", "section")
16
+ @tag2 = tag_for_slug("gooey", "section")
17
+ @data["tags"] << @tag1 << @tag2
18
+ end
19
+
20
+ it "should return the first section tag" do
21
+ assert_equal @tag1, Slimmer::Artefact.new(@data).primary_section
22
+ end
23
+
24
+ it "should ignore other tag types" do
25
+ @data["tags"].unshift(tag_for_slug("businesslink", "legacy_source"))
26
+ assert_equal @tag1, Slimmer::Artefact.new(@data).primary_section
27
+ end
28
+
29
+ it "should return nil if there are no sections" do
30
+ @data["tags"] = [tag_for_slug("businesslink", "legacy_source")]
31
+ assert_equal nil, Slimmer::Artefact.new(@data).primary_section
32
+ end
33
+
34
+ it "should return nil if there is no tags element" do
35
+ @data.delete("tags")
36
+ assert_equal nil, Slimmer::Artefact.new(@data).primary_section
37
+ end
38
+ end
39
+
40
+ describe "Primary root section" do
41
+ before do
42
+ @artefact = Slimmer::Artefact.new(artefact_for_slug('something'))
43
+ @tag1 = tag_for_slug("fooey", "section")
44
+ @tag2 = tag_for_slug("gooey", "section")
45
+ @tag3 = tag_for_slug("kablooie", "section")
46
+ end
47
+
48
+ it "should return the primary section if it has no parent" do
49
+ @artefact.stubs(:primary_section).returns(@tag1)
50
+ assert_equal @tag1, @artefact.primary_root_section
51
+ end
52
+
53
+ it "should return the primary section's parent" do
54
+ @tag1["parent"] = @tag2
55
+ @artefact.stubs(:primary_section).returns(@tag1)
56
+ assert_equal @tag2, @artefact.primary_root_section
57
+ end
58
+
59
+ it "should support arbitrarily deep heirarchies" do
60
+ @tag1["parent"] = @tag3
61
+ @tag3["parent"] = @tag2
62
+ @artefact.stubs(:primary_section).returns(@tag1)
63
+ assert_equal @tag2, @artefact.primary_root_section
64
+ end
65
+
66
+ it "should return nil if there is no primary section" do
67
+ @artefact.stubs(:primary_section).returns(nil)
68
+ assert_equal nil, @artefact.primary_root_section
69
+ end
70
+ end
71
+
12
72
  describe "Related artefacts" do
13
73
  before do
14
74
  @data = artefact_for_slug('something')
@@ -74,5 +134,10 @@ describe Slimmer::Artefact do
74
134
  it "should return nil if the field doesn't exist" do
75
135
  assert_equal nil, @a.non_existent
76
136
  end
137
+
138
+ it "should not blow up if the details attribute doesn't exist" do
139
+ @data.delete("details")
140
+ assert_equal nil, @a.non_existent
141
+ end
77
142
  end
78
143
  end
@@ -10,21 +10,18 @@
10
10
  <h2>More like this:</h2>
11
11
  <nav role="navigation">
12
12
  <ul>
13
- <!-- Content API style Artefact -->
14
- <% if artefact["related"] %>
15
- <% if artefact["related"].any? %>
16
- <% artefact["related"].each do |item| %>
17
-
18
- <li>
19
- <a href="<%= h item["web_url"] %>"><%= h item["title"] %></a>
20
- </li>
21
- <% end %>
13
+ <%# Content API style Artefact %>
14
+ <% if artefact.respond_to?(:related_artefacts) %>
15
+ <% artefact.related_artefacts.each do |item| %>
16
+ <li>
17
+ <a href="<%= h item.web_url %>"><%= h item.title %></a>
18
+ </li>
22
19
  <% end %>
23
- <% if root_primary_section %>
24
- <li class="related-topic"><a href="<%= root_primary_section["content_with_tag"]["web_url"] %>"
25
- >More from the <span><%= h root_primary_section["title"] %></span> category</a></li>
20
+ <% if root_section = artefact.root_primary_section %>
21
+ <li class="related-topic"><a href="<%= h root_section["content_with_tag"]["web_url"] %>"
22
+ >More from the <span><%= h root_section["title"] %></span> category</a></li>
26
23
  <% end %>
27
- <% else %> <!-- Panopticon API style Artefact (deprecated) -->
24
+ <% else %> <%# Panopticon API style Artefact (deprecated) %>
28
25
  <% if artefact["related_items"].any? %>
29
26
  <% artefact["related_items"].each do |item| %>
30
27
  <% if item["artefact"] %>
data/test/headers_test.rb CHANGED
@@ -84,4 +84,24 @@ describe Slimmer::Headers do
84
84
  assert_equal artefact_copy, artefact
85
85
  end
86
86
  end
87
+
88
+ describe "setting a dummy artefact in the artefact header" do
89
+ it "should setup an artefact title" do
90
+ self.set_slimmer_dummy_artefact(:title => "Foo")
91
+
92
+ artefact = JSON.parse(headers[Slimmer::Headers::ARTEFACT_HEADER])
93
+
94
+ assert_equal "Foo", artefact["title"]
95
+ end
96
+
97
+ it "should setup a section tag for the given name and link" do
98
+ self.set_slimmer_dummy_artefact(:section_name => "Foo", :section_link => "/something/foo")
99
+
100
+ artefact = JSON.parse(headers[Slimmer::Headers::ARTEFACT_HEADER])
101
+
102
+ assert_equal "Foo", artefact["tags"][0]["title"]
103
+ assert_equal "section", artefact["tags"][0]["details"]["type"]
104
+ assert_equal "/something/foo", artefact["tags"][0]["content_with_tag"]["web_url"]
105
+ end
106
+ end
87
107
  end
@@ -9,7 +9,7 @@ describe Slimmer::Processors::LogoClassInserter do
9
9
  legacy_sources.each do |legacy_source|
10
10
  artefact["tags"] << tag_for_slug(legacy_source, "legacy_source")
11
11
  end
12
- artefact
12
+ Slimmer::Artefact.new(artefact)
13
13
  end
14
14
 
15
15
  def business_link_artefact
@@ -68,7 +68,7 @@ describe Slimmer::Processors::LogoClassInserter do
68
68
 
69
69
  it "should do nothing if the artefact has no tag_ids" do
70
70
  template = example_template
71
- artefact = artefact_for_slug("vat")
71
+ artefact = Slimmer::Artefact.new(artefact_for_slug("vat"))
72
72
  process(artefact, template)
73
73
  assert_in template, "div#wrapper"
74
74
  end
@@ -8,7 +8,7 @@ class RelatedItemsInserterTest < MiniTest::Unit::TestCase
8
8
  super
9
9
  @related_template = File.read( File.dirname(__FILE__) + "/../fixtures/related.raw.html.erb" )
10
10
  @skin = stub("Skin", :template => @related_template)
11
- @artefact = artefact_for_slug_with_related_artefacts("vat", ["vat-rates", "starting-to-import"])
11
+ @artefact = Slimmer::Artefact.new artefact_for_slug_with_related_artefacts("vat", ["vat-rates", "starting-to-import"])
12
12
  end
13
13
 
14
14
  def test_should_add_related_items
@@ -1,105 +1,190 @@
1
1
  require_relative "../test_helper"
2
+ require 'gds_api/test_helpers/content_api'
2
3
 
3
4
  class SectionInserterTest < MiniTest::Unit::TestCase
5
+ include GdsApi::TestHelpers::ContentApi
4
6
 
5
- # Note: the SectionInserter processor runs after the TagMover processor, so the meta
6
- # tags have already been moved into the destination template
7
+ def create_artefact(slug, attributes = {})
8
+ if section_slug = attributes.delete("section_slug")
9
+ if subsection_slug = attributes.delete("subsection_slug")
10
+ a = artefact_for_slug_in_a_subsection(slug, "#{section_slug}/#{subsection_slug}")
11
+ else
12
+ a = artefact_for_slug_in_a_section(slug, section_slug)
13
+ end
14
+ else
15
+ a = artefact_for_slug(slug)
16
+ end
17
+ Slimmer::Artefact.new(a.merge(attributes))
18
+ end
19
+
20
+ def test_should_add_section_link_and_title_to_breadcrumb
21
+ artefact = create_artefact("something",
22
+ "section_slug" => "business",
23
+ "title" => "Something gooey")
7
24
 
8
- def test_should_add_section_link_to_breadcrumb
9
25
  template = as_nokogiri %{
10
26
  <html>
11
- <head>
12
- <meta content="Business" name="x-section-name">
13
- <meta content="/browse/business" name="x-section-link">
14
- </head>
15
27
  <body>
16
- <nav role="navigation">
17
- <ol><li><a href="/">Home</a></li></ol>
18
- </nav>
28
+ <div class="header-context">
29
+ <nav role="navigation">
30
+ <ol class="group">
31
+ <li><a href="/">Home</a></li>
32
+ </ol>
33
+ </nav>
34
+ </div>
19
35
  </body>
20
36
  </html>
21
37
  }
22
38
 
23
- Slimmer::Processors::SectionInserter.new.filter(:any_source, template)
24
- assert_in template, "nav[role=navigation] ol li:nth-child(1)", %{<a href="/">Home</a>}
25
- assert_in template, "nav[role=navigation] ol li:nth-child(2)", %{<a href="/browse/business">Business</a>}
39
+ Slimmer::Processors::SectionInserter.new(artefact).filter(:any_source, template)
40
+ list = template.at_css(".header-context nav[role=navigation] ol")
41
+ assert_in list, "li:nth-child(1)", %{<a href="/">Home</a>}
42
+ assert_in list, "li:nth-child(2)", %{<a href="https://www.test.gov.uk/browse/business">Business</a>}
43
+ assert_in list, "li:nth-child(3)", %{Something gooey}
26
44
  end
27
45
 
28
- def test_should_add_section_link_after_last_item_in_breadcrumb
46
+ def test_should_add_section_link_subsection_link_and_title_to_breadcrumb
47
+ artefact = create_artefact("something",
48
+ "section_slug" => "business",
49
+ "subsection_slug" => "employing-people",
50
+ "title" => "Something gooey")
51
+
29
52
  template = as_nokogiri %{
30
53
  <html>
31
- <head>
32
- <meta content="Business" name="x-section-name">
33
- <meta content="/browse/business" name="x-section-link">
34
- </head>
35
54
  <body>
36
- <nav role="navigation">
37
- <ol>
38
- <li><a href="/">Home</a></li>
39
- <li><a href="/browse">All Sections</a></li>
40
- </ol>
41
- </nav>
55
+ <div class="header-context">
56
+ <nav role="navigation">
57
+ <ol class="group"><li><a href="/">Home</a></li></ol>
58
+ </nav>
59
+ </div>
42
60
  </body>
43
61
  </html>
44
62
  }
45
63
 
46
- Slimmer::Processors::SectionInserter.new.filter(:any_source, template)
47
- assert_in template, "nav[role=navigation] ol li:nth-child(1)", %{<a href="/">Home</a>}
48
- assert_in template, "nav[role=navigation] ol li:nth-child(2)", %{<a href="/browse">All Sections</a>}
49
- assert_in template, "nav[role=navigation] ol li:nth-child(3)", %{<a href="/browse/business">Business</a>}
64
+ Slimmer::Processors::SectionInserter.new(artefact).filter(:any_source, template)
65
+ list = template.at_css(".header-context nav[role=navigation] ol")
66
+ assert_in list, "li:nth-child(1)", %{<a href="/">Home</a>}
67
+ assert_in list, "li:nth-child(2)", %{<a href="https://www.test.gov.uk/browse/business">Business</a>}
68
+ assert_in list, "li:nth-child(3)", %{<a href="https://www.test.gov.uk/browse/business/employing-people">Employing people</a>}
69
+ assert_in list, "li:nth-child(4)", %{Something gooey}
50
70
  end
51
71
 
52
- def test_should_not_add_section_link_if_no_section_name_tag
72
+ def test_should_not_append_title_if_its_blank
73
+ artefact = create_artefact("something",
74
+ "section_slug" => "business",
75
+ "subsection_slug" => "employing-people",
76
+ "title" => nil)
77
+
53
78
  template = as_nokogiri %{
54
79
  <html>
55
- <head>
56
- <meta content="/browse/business" name="x-section-link">
57
- </head>
58
80
  <body>
59
- <nav role="navigation">
60
- <ol><li><a href="/">Home</a></li></ol>
61
- </nav>
81
+ <div class="header-context">
82
+ <nav role="navigation">
83
+ <ol class="group"><li><a href="/">Home</a></li></ol>
84
+ </nav>
85
+ </div>
62
86
  </body>
63
87
  </html>
64
88
  }
65
89
 
66
- Slimmer::Processors::SectionInserter.new.filter(:any_source, template)
67
- assert_in template, "nav[role=navigation] ol li:nth-child(1)", %{<a href="/">Home</a>}
68
- assert_not_in template, "nav[role=navigation] ol li:nth-child(2)"
90
+ t = template.dup
91
+ Slimmer::Processors::SectionInserter.new(artefact).filter(:any_source, t)
92
+ list = t.at_css(".header-context nav[role=navigation] ol")
93
+ assert_not_in list, "li:nth-child(4)"
94
+
95
+ artefact = create_artefact("something",
96
+ "section_slug" => "business",
97
+ "subsection_slug" => "employing-people",
98
+ "title" => "")
99
+
100
+ Slimmer::Processors::SectionInserter.new(artefact).filter(:any_source, template)
101
+ list = template.at_css(".header-context nav[role=navigation] ol")
102
+ assert_not_in list, "li:nth-child(4)"
69
103
  end
70
104
 
71
- def test_should_not_add_section_link_if_no_section_link_tag
105
+ def test_should_add_links_after_last_item_in_breadcrumb
106
+ artefact = create_artefact("something",
107
+ "section_slug" => "business",
108
+ "subsection_slug" => "employing-people",
109
+ "title" => "Something gooey")
110
+
72
111
  template = as_nokogiri %{
73
112
  <html>
74
- <head>
75
- <meta content="Business" name="x-section-name">
76
- </head>
77
113
  <body>
78
- <nav role="navigation">
79
- <ol><li><a href="/">Home</a></li></ol>
80
- </nav>
114
+ <div class="header-context">
115
+ <nav role="navigation">
116
+ <ol class="group">
117
+ <li><a href="/">Home</a></li>
118
+ <li><a href="/browse">All Sections</a></li>
119
+ </ol>
120
+ </nav>
121
+ </div>
81
122
  </body>
82
123
  </html>
83
124
  }
84
125
 
85
- Slimmer::Processors::SectionInserter.new.filter(:any_source, template)
86
- assert_in template, "nav[role=navigation] ol li:nth-child(1)", %{<a href="/">Home</a>}
87
- assert_not_in template, "nav[role=navigation] ol li:nth-child(2)"
126
+ Slimmer::Processors::SectionInserter.new(artefact).filter(:any_source, template)
127
+ list = template.at_css(".header-context nav[role=navigation] ol")
128
+ assert_in list, "li:nth-child(1)", %{<a href="/">Home</a>}
129
+ assert_in list, "li:nth-child(2)", %{<a href="/browse">All Sections</a>}
130
+ assert_in list, "li:nth-child(3)", %{<a href="https://www.test.gov.uk/browse/business">Business</a>}
131
+ assert_in list, "li:nth-child(4)", %{<a href="https://www.test.gov.uk/browse/business/employing-people">Employing people</a>}
132
+ assert_in list, "li:nth-child(5)", %{Something gooey}
88
133
  end
89
134
 
90
135
  def test_should_do_nothing_if_navigation_not_in_template
136
+ artefact = create_artefact("something")
91
137
  template = as_nokogiri %{
92
138
  <html>
93
- <head>
94
- <meta content="Business" name="x-section-name">
95
- <meta content="/browse/business" name="x-section-link">
96
- </head>
97
139
  <body>
98
140
  </body>
99
141
  </html>
100
142
  }
101
143
 
102
- Slimmer::Processors::SectionInserter.new.filter(:any_source, template)
144
+ Slimmer::Processors::SectionInserter.new(artefact).filter(:any_source, template)
103
145
  assert_not_in template, "nav[role=navigation]"
104
146
  end
147
+
148
+ def test_should_do_nothing_with_no_artefact
149
+ template = as_nokogiri %{
150
+ <html>
151
+ <body>
152
+ <div class="header-context">
153
+ <nav role="navigation">
154
+ <ol class="group"><li><a href="/">Home</a></li></ol>
155
+ </nav>
156
+ </div>
157
+ </body>
158
+ </html>
159
+ }
160
+
161
+ Slimmer::Processors::SectionInserter.new(nil).filter(:any_source, template)
162
+ list = template.at_css(".header-context nav[role=navigation] ol")
163
+ assert_in list, "li:nth-child(1)", %{<a href="/">Home</a>}
164
+ assert_not_in list, "li:nth-child(2)"
165
+ end
166
+
167
+ def test_should_not_fail_with_no_sections
168
+ artefact = create_artefact("something",
169
+ "title" => "Something gooey")
170
+
171
+ template = as_nokogiri %{
172
+ <html>
173
+ <body>
174
+ <div class="header-context">
175
+ <nav role="navigation">
176
+ <ol class="group">
177
+ <li><a href="/">Home</a></li>
178
+ </ol>
179
+ </nav>
180
+ </div>
181
+ </body>
182
+ </html>
183
+ }
184
+
185
+ Slimmer::Processors::SectionInserter.new(artefact).filter(:any_source, template)
186
+ list = template.at_css(".header-context nav[role=navigation] ol")
187
+ assert_in list, "li:nth-child(1)", %{<a href="/">Home</a>}
188
+ assert_in list, "li:nth-child(2)", %{Something gooey}
189
+ end
105
190
  end
data/test/skin_test.rb CHANGED
@@ -1,4 +1,4 @@
1
- require "test_helper"
1
+ require_relative "test_helper"
2
2
 
3
3
  class SkinTest < MiniTest::Unit::TestCase
4
4
  def test_template_can_be_loaded
@@ -54,3 +54,30 @@ class SkinTest < MiniTest::Unit::TestCase
54
54
  end
55
55
  end
56
56
  end
57
+
58
+ describe Slimmer::Skin do
59
+
60
+ describe "parsing artefact from header" do
61
+ before do
62
+ @skin = Slimmer::Skin.new
63
+ @headers = {}
64
+ @response = stub("Response", :headers => @headers)
65
+ end
66
+
67
+ it "should construct and return an artefact with the parsed json" do
68
+ data = {"foo" => "bar", "baz" => 1}
69
+ @headers[Slimmer::Headers::ARTEFACT_HEADER] = data.to_json
70
+ Slimmer::Artefact.expects(:new).with(data).returns(:an_artefact)
71
+ assert_equal :an_artefact, @skin.artefact_from_header(@response)
72
+ end
73
+
74
+ it "should return nil if there is no artefact header" do
75
+ assert_equal nil, @skin.artefact_from_header(@response)
76
+ end
77
+
78
+ it "should return nil if there is invalid JSON in the artefact header" do
79
+ @headers[Slimmer::Headers::ARTEFACT_HEADER] = "fooey"
80
+ assert_equal nil, @skin.artefact_from_header(@response)
81
+ end
82
+ end
83
+ end
@@ -1,4 +1,4 @@
1
- require "test_helper"
1
+ require_relative "test_helper"
2
2
 
3
3
  module TypicalUsage
4
4
 
@@ -52,8 +52,6 @@ module TypicalUsage
52
52
  <html>
53
53
  <head><title>The title of the page</title>
54
54
  <meta name="something" content="yes">
55
- <meta name="x-section-name" content="This section">
56
- <meta name="x-section-link" content="/this_section">
57
55
  <script src="blah.js"></script>
58
56
  <link href="app.css" rel="stylesheet" type="text/css">
59
57
  </head>
@@ -72,14 +70,12 @@ module TypicalUsage
72
70
  class NormalResponseTest < SlimmerIntegrationTest
73
71
  def setup
74
72
  super
75
- @artefact = artefact_for_slug("some-slug")
73
+ @artefact = artefact_for_slug_in_a_section("some-article", 'this-section')
76
74
  @artefact["tags"] << tag_for_slug("directgov", "legacy_source")
77
75
  given_response 200, %{
78
76
  <html>
79
77
  <head><title>The title of the page</title>
80
78
  <meta name="something" content="yes">
81
- <meta name="x-section-name" content="This section">
82
- <meta name="x-section-link" content="/this_section">
83
79
  <script src="blah.js"></script>
84
80
  <link href="app.css" rel="stylesheet" type="text/css">
85
81
  </head>
@@ -114,8 +110,9 @@ module TypicalUsage
114
110
  assert_rendered_in_template "body.body_class"
115
111
  end
116
112
 
117
- def test_should_insert_meta_navigation_links_into_the_navigation
118
- assert_rendered_in_template "nav[role=navigation] li a[href='/this_section']", "This section"
113
+ def test_should_insert_section_links_into_the_navigation
114
+ assert_rendered_in_template "nav[role=navigation] li a[href='https://www.test.gov.uk/browse/this-section']", "This section"
115
+ assert_rendered_in_template "nav[role=navigation] li:last-child", "Some article"
119
116
  end
120
117
 
121
118
  def test_should_add_logo_classes_to_wrapper
@@ -173,8 +170,8 @@ module TypicalUsage
173
170
  end
174
171
 
175
172
  def test_should_insert_related_items_block
176
- assert_rendered_in_template "div.related nav li a", "How to test computer software automatically"
177
- assert_rendered_in_template "div.related nav li", %r{href="https://www.test.gov.uk/how-to-test-computer-software-automatically"}
173
+ assert_rendered_in_template "div.related nav li a[href='https://www.test.gov.uk/how-to-test-computer-software-automatically']",
174
+ "How to test computer software automatically"
178
175
  end
179
176
  end
180
177
 
@@ -200,8 +197,6 @@ module TypicalUsage
200
197
  <html>
201
198
  <head><title>The title of the page</title>
202
199
  <meta name="something" content="yes">
203
- <meta name="x-section-name" content="This section">
204
- <meta name="x-section-link" content="/this_section">
205
200
  <script src="blah.js"></script>
206
201
  <link href="app.css" rel="stylesheet" type="text/css">
207
202
  </head>
@@ -242,8 +237,6 @@ module TypicalUsage
242
237
  <html>
243
238
  <head><title>500 Error</title>
244
239
  <meta name="something" content="yes">
245
- <meta name="x-section-name" content="This section">
246
- <meta name="x-section-link" content="/this_section">
247
240
  <script src="blah.js"></script>
248
241
  <link href="app.css" rel="stylesheet" type="text/css">
249
242
  </head>
@@ -273,8 +266,6 @@ module TypicalUsage
273
266
  <html>
274
267
  <head><title>404 Missing</title>
275
268
  <meta name="something" content="yes">
276
- <meta name="x-section-name" content="This section">
277
- <meta name="x-section-link" content="/this_section">
278
269
  <script src="blah.js"></script>
279
270
  <link href="app.css" rel="stylesheet" type="text/css">
280
271
  </head>
@@ -304,8 +295,6 @@ module TypicalUsage
304
295
  <html>
305
296
  <head><title>406 Not Acceptable</title>
306
297
  <meta name="something" content="yes">
307
- <meta name="x-section-name" content="This section">
308
- <meta name="x-section-link" content="/this_section">
309
298
  <script src="blah.js"></script>
310
299
  <link href="app.css" rel="stylesheet" type="text/css">
311
300
  </head>
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: slimmer
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 3.0.0
5
+ version: 3.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ben Griffiths
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2012-09-12 00:00:00 Z
14
+ date: 2012-09-14 00:00:00 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: nokogiri
@@ -141,7 +141,7 @@ dependencies:
141
141
  requirements:
142
142
  - - "="
143
143
  - !ruby/object:Gem::Version
144
- version: 1.9.1
144
+ version: 1.9.2
145
145
  type: :development
146
146
  prerelease: false
147
147
  version_requirements: *id012
@@ -217,7 +217,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
217
217
  requirements:
218
218
  - - ">="
219
219
  - !ruby/object:Gem::Version
220
- hash: -2567614616492657902
220
+ hash: 2041451578136689132
221
221
  segments:
222
222
  - 0
223
223
  version: "0"
@@ -226,7 +226,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
226
226
  requirements:
227
227
  - - ">="
228
228
  - !ruby/object:Gem::Version
229
- hash: -2567614616492657902
229
+ hash: 2041451578136689132
230
230
  segments:
231
231
  - 0
232
232
  version: "0"